A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Show thinking once the server accepts a prompt
Pranit9d191d02ff6d
diff --git a/app/api/prompt/route.ts b/app/api/prompt/route.tsindex 72f4398..c28e1d7 100644--- a/app/api/prompt/route.ts+++ b/app/api/prompt/route.ts@@ -9,7 +9,7 @@ import { touchOwnedSession } from '../../../lib/accounts'import { checkBotId } from 'botid/server'import { trialEnabled, trialIpHash, trialStore, type TrialCredential, type TrialReservation } from '../../../lib/trial'import { TRIAL_MODEL, usesFreePrompts } from '../../../lib/trial-policy'-import { DEFAULT_MODEL } from '../../../lib/session'+import { DEFAULT_MODEL, log } from '../../../lib/session'export const maxDuration = 800@@ -24,6 +24,7 @@ async function catalogDesignMd(id: string, name: string): Promise<DesignMdInput>}export const POST = withPromptRoute(async (request, context) => {+ const started = performance.now()const { user, session, existing } = contextlet body: {text?: unknown@@ -115,8 +116,11 @@ export const POST = withPromptRoute(async (request, context) => {const clientId = typeof body?.clientId === 'string' ? body.clientId.slice(0, 40) : undefinedlet submitted: { id: number; seq: number; trialChat: boolean }let activated: SessionRouteContext+ const checked = performance.now()+ let activatedAt = checkedtry {activated = await context.activate()+ activatedAt = performance.now()submitted = await submitPrompt(activated.host,author,@@ -137,6 +141,13 @@ export const POST = withPromptRoute(async (request, context) => {if (reservation) await trialStore().commit(reservation).catch(() => console.error('[fx0] free prompt confirmation failed'))const { id, seq } = submittedconst { host, id: sessionId, created } = activated+ // Where a prompt's time goes before it is acknowledged, after sign-in.+ log(sessionId, 'prompt:route', {+ id, created,+ checksMs: Math.round(checked - started),+ activateMs: Math.round(activatedAt - checked),+ submitMs: Math.round(performance.now() - activatedAt),+ })// libfx runs the turn elsewhere; this function copies its stream into the// transcript until the chat goes idle, even after the client disconnects.diff --git a/components/chat-app.tsx b/components/chat-app.tsxindex 38096f8..e70f09e 100644--- a/components/chat-app.tsx+++ b/components/chat-app.tsx@@ -284,6 +284,9 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultMod// counter runs through "sending" and "thinking" instead of sitting at 0s.const [sentAt, setSentAt] = useState<number | null>(null)const [awaiting, setAwaiting] = useState(false)+ // The server accepted the awaited prompt: it is stored and its turn is+ // starting, so the activity line says "thinking" before turn_start arrives.+ const [sendAccepted, setSendAccepted] = useState(false)const [authStatus, setAuthStatus] = useState<401 | 403 | null>(null)const [signOutStatus, setSignOutStatus] = useState<'pending' | 'failed' | null>(null)const signOutPending = useRef(false)@@ -1215,6 +1218,7 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultModconst designPick = targetSession === 'draft' ? composerRef.current?.getDesignMd() ?? null : null// The bubble shows at once, before a draft's chat is reserved on the server.setAwaiting(true)+ setSendAccepted(false)setSentAt((current) => current ?? Date.now())setInteractionNotice(null)setEntries((prev) => [@@ -1359,6 +1363,7 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultModtargetSession = created}promptAdmitted = true+ if (activeSession.current === targetSession) setSendAccepted(true)setSessions((prev) => upsertSession(withCurrentSession(createdSummary ? mergeSessions(prev, [createdSummary]) : prev, targetSession, acceptedAt, [acceptedUser], currentView.current),targetSession,@@ -1630,7 +1635,7 @@ export default function ChatApp({ account, sidebar, boot, preference, defaultModconst activityText =awaiting && activeTurnId == null- ? 'sending'+ ? sendAccepted ? 'thinking' : 'sending': activeTurnId == null? '': entries.at(-1)?.type === 'text'diff --git a/lib/agent.ts b/lib/agent.tsindex 166cebf..95ab664 100644--- a/lib/agent.ts+++ b/lib/agent.ts@@ -191,6 +191,25 @@ export async function warmChat(host: SessionState): Promise<void> {agent()}+// Prompts this instance has submitted; the first one also pays for loading+// libfx and creating its agent.+let instancePrompts = 0++// Milliseconds each stage of a prompt's submission took, in order.+function promptStages() {+ const instancePrompt = ++instancePrompts+ let last = performance.now()+ const ms: Record<string, number> = {}+ return {+ mark(stage: string) {+ const now = performance.now()+ ms[`${stage}Ms`] = Math.round(now - last)+ last = now+ },+ done: () => ({ ...ms, instancePrompt }),+ }+}+export async function submitPrompt(host: SessionState,author: string,@@ -207,27 +226,35 @@ export async function submitPrompt(const credential = submitter ?? host.gatewaySessionconst sender = credential && { ...credential }if (!funding) assertGenerationCredential({ ...host, gatewaySession: sender })+ const stages = promptStages()const submitted = await journalPrompt(host.id, { author, text, clientId, images, previews }, initial, !!funding)+ stages.mark('journal')log(host.id, 'prompt:submitted', { id: submitted.id, clientId, imageCount: images?.length ?? 0, idle: submitted.idle })const id = submitted.idtry {if (funding) await saveTrialCredential(host.id, funding, id)else await saveTurnCredential(host.id, sender!, submitted.trialChat ? id : undefined)+ stages.mark('credential')const stored = await hydrateSession(host)+ stages.mark('hydrate')const { session } = await chatSession(agent(), host, stored?.libfxSessionId ?? null, agentInstructions(host), id, !!funding)+ stages.mark('bind')const input = images?.length ? [{ type: 'text' as const, text: `[${author}] ${text}` }, ...images] : `[${author}] ${text}`// prompt() runs the turn in this request; steer() always goes through// Vercel Queues, which delays the turn's start by seconds. So a message to// an idle chat is a prompt, and only one sent while a turn runs or waits// steers it. Images need their own turn: the installed SDK can reapply an// image steer as an interruption of the same turn during durable admission.- if (images?.length || submitted.idle || submitted.trialChat) {+ const prompted = !!(images?.length || submitted.idle || submitted.trialChat)+ if (prompted) {const turn = session.prompt(input, { messageId: `m${id}` })void turn.result.catch(() => {})await turn.accepted} else {await session.steer(input, { messageId: `m${id}` })}+ stages.mark('accepted')+ log(host.id, 'prompt:stages', { id, path: prompted ? 'prompt' : 'steer', ...stages.done() })} catch (error) {log(host.id, 'prompt:rejected', { id, error: errorText(error) })await emit(host, { type: 'error', id, turnId: id, text: `prompt rejected: ${errorText(error)}` })diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjsindex 8670df0..0ab0298 100644--- a/tests/guest-ui.test.mjs+++ b/tests/guest-ui.test.mjs@@ -460,6 +460,43 @@ test('a send released by chat boot targets the requested chat rather than reservassert.ok(!ui.requests.some(({ url }) => url === '/api/sessions' || url.includes('session=new')))})+test('a prompt says sending until the server accepts it, then thinking while its turn starts', async (t) => {+ t.mock.method(console, 'info', () => {})+ let respond+ const accepted = new Promise((resolve) => { respond = resolve })+ let events+ const body = new ReadableStream({ start(controller) { events = controller } })+ const push = (item) => events.enqueue(new TextEncoder().encode(`data: ${JSON.stringify(item)}\n\n`))+ const emptyStream = () => new Response(new ReadableStream({ start(controller) { controller.close() } }))+ const ui = browser(t, { '/api/events?session=saved&after=0': emptyStream, '/api/prompt?session=saved': () => accepted })+ const app = mount(t, ui, {+ ...guestBoot, user: { id: 'owner', name: 'Owner' }, sessionId: 'saved',+ sessions: [{ id: 'saved', title: 'Saved', createdAt: 1, lastActivity: 1, busy: false }],+ })+ await tick()+ app.render()+ const activity = () => find(app.render(), (node) => node.type?.name === 'ActivityLine')?.props.label ?? null+ const stoppable = () => find(app.tree(), (node) => node.type === Composer).props.busy+ app.handle().restoreDraft({ text: 'Make it blue', images: [] })+ app.render()+ app.send()+ await tick()+ assert.equal(activity(), 'sending', 'nothing is stored before the server answers')++ respond(new Response(body, { headers: { 'content-type': 'text/event-stream' } }))+ await tick()+ assert.equal(activity(), 'thinking', 'the accepted prompt is stored and its turn is starting')+ assert.equal(stoppable(), false, 'Stop still waits for the turn to start')++ push({ type: 'user', seq: 1, id: 1, text: 'Make it blue', at: 10 })+ push({ type: 'turn_start', seq: 2, id: 1, turnId: 1, at: 11 })+ await tick()+ await tick()+ assert.equal(activity(), 'thinking')+ assert.equal(stoppable(), true)+ events.close()+})+test('new design chosen before chat boot cannot be overwritten by its delayed transcript', (t) => {const ui = browser(t)const app = mount(t, ui, { ...guestBoot, user: { id: 'owner', name: 'Owner' }, sessionId: 'old', events: [{ type: 'user', seq: 1, text: 'Old chat' }] }, { booted: false })