Repository navigation
fix: persist WebChat chat preferences #10324
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
C10H14N2O5
wants to merge
4
commits into
AstrBotDevs:master
Choose a base branch
from
C10H14N2O5:fix/10316-chat-preferences
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+180
−6
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
75d8436
fix: persist WebChat chat preferences
C10H14N2O5 0faeedc
fix: refresh chat preferences when returning to WebChat
C10H14N2O5 81c6371
chore: sync upstream master into chat preferences fix
C10H14N2O5 7d74420
chore: sync master into chat preferences fix
C10H14N2O5 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| import assert from "node:assert/strict"; | ||
| import { readFileSync } from "node:fs"; | ||
| import test from "node:test"; | ||
| import vm from "node:vm"; | ||
| import ts from "typescript"; | ||
| import { effectScope, nextTick, reactive, ref, watch } from "vue"; | ||
|
|
||
| const components = ["Chat.vue", "StandaloneChat.vue"]; | ||
| const preferences = ["enableStreaming", "enableReasoning", "sendShortcut"]; | ||
|
|
||
| function mountPreferences(component, storage, t) { | ||
| const source = readFileSync( | ||
| new URL(`../src/components/chat/${component}`, import.meta.url), | ||
| "utf8", | ||
| ) | ||
| .split('<script setup lang="ts">')[1] | ||
| .split("</script>")[0]; | ||
| const ast = ts.createSourceFile( | ||
| component, | ||
| source, | ||
| ts.ScriptTarget.Latest, | ||
| true, | ||
| ); | ||
| const statements = ast.statements.filter((node) => { | ||
| if (ts.isVariableStatement(node)) { | ||
| return node.declarationList.declarations.some((declaration) => | ||
| preferences.includes(declaration.name.getText(ast)), | ||
| ); | ||
| } | ||
| return ( | ||
| ts.isExpressionStatement(node) && | ||
| ts.isCallExpression(node.expression) && | ||
| node.expression.expression.getText(ast) === "watch" && | ||
| (preferences.includes(node.expression.arguments[0]?.getText(ast)) || | ||
| (node.expression.arguments[0]?.getText(ast) === "() => props.active" && | ||
| node.expression.arguments[1] | ||
| ?.getText(ast) | ||
| .includes("enableStreaming.value"))) | ||
| ); | ||
| }); | ||
| const context = vm.createContext({ | ||
| ref, | ||
| watch, | ||
| props: reactive({ active: true }), | ||
| localStorage: { | ||
| getItem: (key) => storage.get(key) ?? null, | ||
| setItem: (key, value) => storage.set(key, value), | ||
| }, | ||
| }); | ||
| const scope = effectScope(); | ||
| t.after(() => scope.stop()); | ||
| return scope.run(() => | ||
| vm.runInContext( | ||
| ts.transpile( | ||
| statements.map((node) => node.getText(ast)).join("\n") + | ||
| "\n({ enableStreaming, enableReasoning, sendShortcut, props });", | ||
| ), | ||
| context, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| test("retained Chat.vue refreshes preferences when it becomes active again", async (t) => { | ||
| const storage = new Map([["chat.transportMode", "websocket"]]); | ||
| const mainChat = mountPreferences("Chat.vue", storage, t); | ||
| for (const [streaming, reasoning, shortcut] of [ | ||
| [false, false, "shift_enter"], | ||
| [true, true, "enter"], | ||
| ]) { | ||
| mainChat.props.active = false; | ||
| await nextTick(); | ||
| const testChat = mountPreferences("StandaloneChat.vue", storage, t); | ||
| testChat.enableStreaming.value = streaming; | ||
| testChat.enableReasoning.value = reasoning; | ||
| testChat.sendShortcut.value = shortcut; | ||
| await nextTick(); | ||
|
|
||
| mainChat.props.active = true; | ||
| await nextTick(); | ||
| assert.equal(mainChat.enableStreaming.value, streaming); | ||
| assert.equal(mainChat.enableReasoning.value, reasoning); | ||
| assert.equal(mainChat.sendShortcut.value, shortcut); | ||
| assert.equal(storage.get("chat.transportMode"), "websocket"); | ||
| } | ||
| }); | ||
|
|
||
| for (const component of components) { | ||
| test(`${component} preserves the default chat preferences`, (t) => { | ||
| const state = mountPreferences(component, new Map(), t); | ||
| assert.equal(state.enableStreaming.value, true); | ||
| assert.equal(state.enableReasoning.value, true); | ||
| assert.equal(state.sendShortcut.value, "enter"); | ||
| }); | ||
|
|
||
| test(`${component} saves and restores chat preferences across entry points`, async (t) => { | ||
| const storage = new Map([["chat.transportMode", "websocket"]]); | ||
| const state = mountPreferences(component, storage, t); | ||
| for (const [streaming, reasoning, shortcut] of [ | ||
| [false, false, "shift_enter"], | ||
| [true, true, "enter"], | ||
| ]) { | ||
| state.enableStreaming.value = streaming; | ||
| state.enableReasoning.value = reasoning; | ||
| state.sendShortcut.value = shortcut; | ||
| await nextTick(); | ||
| assert.equal(storage.get("chat.enableStreaming"), String(streaming)); | ||
| assert.equal(storage.get("chat.enableReasoning"), String(reasoning)); | ||
| assert.equal(storage.get("chat.sendShortcut"), shortcut); | ||
| assert.equal(storage.get("chat.transportMode"), "websocket"); | ||
| for (const entryPoint of components) { | ||
| const restored = mountPreferences(entryPoint, storage, t); | ||
| assert.equal(restored.enableStreaming.value, streaming); | ||
| assert.equal(restored.enableReasoning.value, reasoning); | ||
| assert.equal(restored.sendShortcut.value, shortcut); | ||
| } | ||
| } | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.