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

Load the most recent chats in the background once idle

Esteban Suarez19f1986ed61f
diff --git a/components/chat-app.tsx b/components/chat-app.tsx
index 65a39cc..6001c24 100644
--- a/components/chat-app.tsx
+++ b/components/chat-app.tsx
@@ -956,6 +956,28 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod
if (hasIdentity && !authBlocked && id !== 'draft' && id !== sessionId) void loadSession(id).catch(() => {})
}
+ // Once per page, when the app is idle after the history arrives, the most recent chats
+ // load in the background (as hovering one does), so opening them shows their content
+ // at once. They stay within the client cache's five chats.
+ const recentPrefetched = useRef(false)
+ useEffect(() => {
+ if (recentPrefetched.current || !sidebarReady || !hasIdentity || authBlocked) return
+ const run = () => {
+ if (recentPrefetched.current) return
+ recentPrefetched.current = true
+ for (const session of sessions.filter((session) => session.id !== activeSession.current).slice(0, 3)) prefetchSession(session.id)
+ }
+ const idleApi = typeof requestIdleCallback === 'function' && typeof cancelIdleCallback === 'function'
+ const idle = idleApi ? requestIdleCallback(run, { timeout: 3000 }) : setTimeout(run, 1500)
+ return () => {
+ if (idleApi) cancelIdleCallback(idle as number)
+ else clearTimeout(idle as ReturnType<typeof setTimeout>)
+ }
+ // run reads the current list and auth through this render's closure; re-arm only
+ // when readiness or auth changes.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [sidebarReady, hasIdentity, authBlocked])
+
// The chat the latest switch is moving to; a later click replaces it, and an earlier
// load that finishes afterwards is dropped.
const switchTarget = useRef<string | null>(null)
diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjs
index b563285..375946b 100644
--- a/tests/guest-ui.test.mjs
+++ b/tests/guest-ui.test.mjs
@@ -397,6 +397,44 @@ test('opening an uncached chat moves to its URL and an empty chat at once; a lat
assert.ok(find(app.tree(), (node) => node.props?.role === 'alert' && node.props.children === 'Unable to load that session. Try again.'))
})
+test('once idle, the three most recent other chats load in the background and then open from the cache', async (t) => {
+ const summary = (id, at) => ({ id, title: id, createdAt: 1, lastActivity: at, busy: false })
+ const snapshot = (id) => Response.json({ transcript: compactTranscript([{ type: 'user', seq: 1, id: 1, text: `Design ${id}` }], guestBoot.view), summary: summary(id, 1) })
+ const ui = browser(t, {
+ '/api/events?session=one&after=1': () => new Response(''),
+ '/api/events?session=a&after=1': () => new Response(''),
+ '/api/model?session=one': () => Response.json({ model: '', effort: '', fast: false }),
+ '/api/model?session=a': () => Response.json({ model: '', effort: '', fast: false }),
+ '/api/session?session=a': () => snapshot('a'),
+ '/api/session?session=b': () => snapshot('b'),
+ '/api/session?session=c': () => snapshot('c'),
+ })
+ const app = mount(t, ui, {
+ ...guestBoot, user: { id: 'user-1', name: 'User', email: null, picture: null },
+ sessionId: 'one', sessions: [summary('one', 9), summary('a', 8), summary('b', 7), summary('c', 6), summary('d', 5)],
+ events: [{ type: 'user', seq: 1, id: 1, text: 'Design one' }],
+ })
+ await tick()
+ app.render()
+ const sessionRequests = () => ui.requests.filter(({ url }) => url.startsWith('/api/session?')).map(({ url }) => url)
+ assert.deepEqual(sessionRequests(), [], 'nothing loads before the app is idle')
+ for (const run of ui.timers.splice(0)) run()
+ await tick()
+ assert.deepEqual(sessionRequests().sort(), ['/api/session?session=a', '/api/session?session=b', '/api/session?session=c'],
+ 'the open chat and older chats are not loaded')
+ app.render()
+ for (const run of ui.timers.splice(0)) run()
+ await tick()
+ assert.equal(sessionRequests().length, 3, 'once per page')
+
+ const rail = () => find(app.tree(), (node) => node.type === Sidebar).props
+ rail().onSelect('a')
+ await tick()
+ app.render()
+ assert.equal(find(app.tree(), (node) => node.type === Transcript).props.entries[0]?.text, 'Design a')
+ assert.equal(sessionRequests().filter((url) => url.endsWith('=a')).length, 1, 'opening it uses the background load')
+})
+
test('the first prompt response retains trial eligibility when a signed-in draft becomes a cached chat', async (t) => {
t.mock.method(console, 'info', () => {})
const ui = browser(t, {