Skip to content

armgit / fx0

Public Imported from GitHub
0

A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.

TypeScript

Show a listed chat's model the moment it is opened

Esteban Suarez381cd6ac1ab6
diff --git a/components/chat-app.tsx b/components/chat-app.tsx
index d8f1d33..38096f8 100644
--- a/components/chat-app.tsx
+++ b/components/chat-app.tsx
@@ -2004,6 +2004,7 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod
sessionId={sessionId}
initialGeneration={initialGeneration}
sessionReady={chatReady}
+ knownGeneration={sessions.find((session) => session.id === sessionId)?.generation}
preference={preference}
onSend={send}
onStop={() => void stop()}
diff --git a/components/composer.tsx b/components/composer.tsx
index c78d39b..a3dfa98 100644
--- a/components/composer.tsx
+++ b/components/composer.tsx
@@ -211,6 +211,7 @@ export default function Composer({
sessionId,
initialGeneration,
sessionReady = true,
+ knownGeneration,
preference,
trial = false,
freePrompts,
@@ -231,6 +232,8 @@ export default function Composer({
sessionId: string
initialGeneration: GenerationOptions
sessionReady?: boolean
+ // The chat's settings as the sidebar listed them: adopted at once on a switch.
+ knownGeneration?: { model: string; effort: string; fast: boolean }
preference?: Promise<PagePreference>
trial?: boolean
freePrompts?: number | null
@@ -517,13 +520,21 @@ export default function Composer({
setGenerationSession(sessionId)
return
}
- // The page read the first chat's settings as it rendered; later chats ask.
+ // The page read the first chat's settings as it rendered, and the sidebar lists
+ // every other chat's; only a chat missing from both asks the server.
const rendered = renderedSettings.current
renderedSettings.current = undefined
if (rendered) {
adoptGenerationOptions(sessionId, rendered)
return
}
+ // A listed chat's settings apply as stored. An empty model means the app
+ // default; it is not replaced with the previous chat's selection.
+ if (knownGeneration) {
+ applyGenerationOptions(sessionId, knownGeneration, false)
+ setGenerationError('')
+ return
+ }
void loadGenerationOptions(sessionId).catch((error) => {
if (!(error instanceof AuthRouteError)) setGenerationError('Unable to confirm generation settings. Try again.')
}).finally(() => {
diff --git a/lib/events.ts b/lib/events.ts
index 2a79692..c081bf6 100644
--- a/lib/events.ts
+++ b/lib/events.ts
@@ -74,6 +74,8 @@ export interface SessionInfo {
createdAt: number
lastActivity: number
busy: boolean
+ // The chat's stored picker settings, so switching to it needs no settings request.
+ generation?: { model: string; effort: string; fast: boolean }
}
export interface SessionPage {
diff --git a/lib/journal.ts b/lib/journal.ts
index e1b6225..2839c78 100644
--- a/lib/journal.ts
+++ b/lib/journal.ts
@@ -495,7 +495,7 @@ export async function readSessionSummaries(ids: string[]): Promise<SessionInfo[]
const batch = ids.slice(offset, offset + 200)
const pipeline = redis().pipeline()
for (const id of batch) {
- pipeline.hmget(key(id, 'state'), 'id', 'createdAt', 'lastActivity', 'title', 'trialChat')
+ pipeline.hmget(key(id, 'state'), 'id', 'createdAt', 'lastActivity', 'title', 'trialChat', 'preferredModel', 'preferredEffort', 'preferredFast')
pipeline.exists(key(id, 'active'))
pipeline.get(key(id, 'message-seq'))
}
@@ -506,7 +506,8 @@ export async function readSessionSummaries(ids: string[]): Promise<SessionInfo[]
if (!state) continue
sessions.push({ id: state.id, title: state.title, createdAt: state.createdAt,
lastActivity: state.lastActivity, busy: Number(values[index * 3 + 1] ?? 0) > 0,
- ...(state.trialChat ? { trialChat: true as const } : {}) })
+ ...(state.trialChat ? { trialChat: true as const } : {}),
+ generation: { model: state.preferredModel ?? '', effort: state.preferredEffort ?? '', fast: state.preferredFast ?? false } })
}
}
return sessions.sort((a, b) => b.lastActivity - a.lastActivity).slice(0, 100)
diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjs
index f886453..8670df0 100644
--- a/tests/guest-ui.test.mjs
+++ b/tests/guest-ui.test.mjs
@@ -659,13 +659,15 @@ function composerAt(ui, sessionId, initialGeneration) {
const composer = renderHooks(Composer)
const ref = { current: null }
const base = { ref, persistSettings: true, busy: false, stopping: false, initialGeneration, onSend() {}, onStop() {}, onNewSession() {} }
- let tree = composer.render({ ...base, sessionId })
+ let knownGeneration
+ const props = () => ({ ...base, sessionId, knownGeneration })
+ let tree = composer.render(props())
composer.flush()
ui.cleanups.push(() => composer.cleanup())
return {
ref,
- at(next) { sessionId = next; tree = composer.render({ ...base, sessionId }); composer.flush(); tree = composer.render({ ...base, sessionId }) },
- render() { tree = composer.render({ ...base, sessionId }); return tree },
+ at(next, known) { sessionId = next; knownGeneration = known; tree = composer.render(props()); composer.flush(); tree = composer.render(props()) },
+ render() { tree = composer.render(props()); return tree },
}
}
@@ -708,6 +710,30 @@ test('a chat waits for its own model instead of flashing the draft or previous c
assert.ok(find(view.render(), node => node.props?.['aria-label'] === 'Model: a-model'))
})
+test('switching to a chat the sidebar lists shows its model at once, with no skeleton or request', async (t) => {
+ const ui = browser(t, { '/api/model?session=chat-1': () => Response.json({ model: 'first-model', effort: '', fast: false }) })
+ const view = composerAt(ui, 'chat-1', { model: '', defaultModel: 'default-model', effort: '', fast: false })
+ await tick()
+ assert.ok(find(view.render(), node => node.props?.['aria-label'] === 'Model: first-model'))
+ view.at('chat-2', { model: 'listed-model', effort: 'high', fast: false })
+ const tree = view.render()
+ assert.ok(find(tree, node => node.props?.['aria-label'] === 'Model: listed-model'), 'the listed model shows on the first frame')
+ assert.equal(find(tree, node => node.props?.['aria-label'] === 'Model: loading'), null)
+ assert.equal(ui.requests.some(({ url }) => url === '/api/model?session=chat-2'), false, 'no settings request for a listed chat')
+})
+
+test('switching to a listed chat with no stored model does not write the previous chat\'s model to it', async (t) => {
+ const ui = browser(t, { '/api/model?session=chat-1': () => Response.json({ model: 'first-model', effort: '', fast: false }) })
+ const view = composerAt(ui, 'chat-1', { model: '', defaultModel: 'default-model', effort: '', fast: false })
+ await tick()
+ assert.ok(find(view.render(), node => node.props?.['aria-label'] === 'Model: first-model'))
+ view.at('chat-2', { model: '', effort: '', fast: false })
+ await tick()
+ view.render()
+ const writes = ui.requests.filter(({ url, init }) => url === '/api/model?session=chat-2' && init?.method === 'POST')
+ assert.deepEqual(writes.map(({ init }) => JSON.parse(init.body).model), [], 'no model is written to the chat on a switch')
+})
+
test('a chat whose settings fail to load shows the current model with the error, not an endless skeleton', async (t) => {
const ui = browser(t, { '/api/model?session=chat-1': () => Response.json({ error: 'Request failed' }, { status: 503 }) })
const view = composerAt(ui, 'chat-1', { model: 'kept-model', defaultModel: 'default-model', effort: '', fast: false })