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

Let a new chat cancel a chat that is still opening

Esteban Suarez9f7be4fe6429
diff --git a/components/chat-app.tsx b/components/chat-app.tsx
index 6001c24..5db65c6 100644
--- a/components/chat-app.tsx
+++ b/components/chat-app.tsx
@@ -1012,6 +1012,8 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod
} catch (error) {
if (switchTarget.current !== id) return
document.documentElement.classList.remove('chat-url')
+ // An expired sign-in hands over to the auth flow. The URL keeps naming the chat so
+ // signing in again opens it.
if (error instanceof AuthRouteError) return
console.error('[fx0] session load failed', { sessionId: id, error })
// The URL already names the chat; back to a new chat, keeping what was typed.
@@ -1137,6 +1139,10 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod
}, [booting])
function resetLocalDraft(pushHistory = false, { keepTyping = false } = {}) {
+ // A new chat replaces any chat still opening; its snapshot is dropped when it arrives.
+ switchTarget.current = null
+ setSwitchingTo(null)
+ setSwitchPreview(null)
if (!keepTyping) {
composerRef.current?.clearDraft()
try { clearGuestDraft(window.sessionStorage) } catch { /* Storage is optional. */ }
diff --git a/lib/journal.ts b/lib/journal.ts
index 72cbc75..1e98ec9 100644
--- a/lib/journal.ts
+++ b/lib/journal.ts
@@ -347,7 +347,7 @@ export const readTranscript = createTranscriptReader({
events: tail.map((item) => parseJson<StreamItem>(item)),
view,
summary: stored ? { id, title: stored.title, createdAt: stored.createdAt, lastActivity: stored.lastActivity, busy: busy > 0, ...(stored.trialChat ? { trialChat: true as const } : {}),
- ...(view.artifactRevision > 0 ? { artifactRevision: view.artifactRevision } : {}) } : null,
+ ...(Number.isSafeInteger(view.artifactRevision) && view.artifactRevision > 0 ? { artifactRevision: view.artifactRevision } : {}) } : null,
}
},
publish: publishTranscriptSnapshot,
diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjs
index 375946b..d206963 100644
--- a/tests/guest-ui.test.mjs
+++ b/tests/guest-ui.test.mjs
@@ -397,6 +397,39 @@ 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('New design while a chat opens wins over the late snapshot', async (t) => {
+ const summary = (id) => ({ id, title: id, createdAt: 1, lastActivity: 1, busy: false })
+ let releaseSlow
+ const slow = new Promise((resolve) => { releaseSlow = resolve })
+ const ui = browser(t, {
+ '/api/events?session=one&after=1': () => new Response(''),
+ '/api/model?session=one': () => Response.json({ model: '', effort: '', fast: false }),
+ '/api/session?session=slow': () => slow.then(() => Response.json({ transcript: compactTranscript([{ type: 'user', seq: 1, id: 1, text: 'Design slow' }], guestBoot.view), summary: summary('slow') })),
+ })
+ window.history.pushState = (_state, _title, url) => { window.location.search = new URL(String(url)).search }
+ window.history.replaceState = (_state, _title, url) => { window.location.search = new URL(String(url)).search }
+ const app = mount(t, ui, {
+ ...guestBoot, user: { id: 'user-1', name: 'User', email: null, picture: null },
+ sessionId: 'one', sessions: [summary('one'), summary('slow')],
+ events: [{ type: 'user', seq: 1, id: 1, text: 'Design one' }],
+ })
+ await tick()
+ app.render()
+ const rail = () => find(app.tree(), (node) => node.type === Sidebar).props
+ const entries = () => find(app.tree(), (node) => node.type === Transcript).props.entries
+ rail().onSelect('slow')
+ app.render()
+ rail().onNew()
+ app.render()
+ releaseSlow()
+ await tick()
+ await tick()
+ app.render()
+ assert.deepEqual(entries(), [], 'the late snapshot does not replace the new design')
+ assert.equal(rail().activeId, 'draft')
+ assert.doesNotMatch(window.location.search, /[?&]s=/, 'the URL stays on the new design')
+})
+
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) })