A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Upload attached images while libfx admits the prompt, and keep them five minutes
Esteban Suarez44254b123f51
diff --git a/app/api/artifact-image/route.ts b/app/api/artifact-image/route.tsindex f636d75..de1a084 100644--- a/app/api/artifact-image/route.ts+++ b/app/api/artifact-image/route.ts@@ -17,11 +17,14 @@ export const GET = withSessionRoute(async (request, { id: sessionId }) => {if (!url) return new Response(null, { status: 404, headers: { 'cache-control': 'private, no-store' } })const asset = assetReference(url)const handle = asset?.slice('fx0-asset:'.length)- // Handles are immutable. A kept attachment must revalidate through ownership and- // the manifest, even after logout or deletion, but a 304 needs no Blob download.+ // Handles are immutable. The browser may reuse an attachment for five minutes, so+ // edits to a design do not wait on a round trip per image; after that it revalidates+ // through ownership and the manifest, and a 304 needs no Blob download. Deleting the+ // chat stops it at the next revalidation; only that same browser could still open+ // the exact URL in the meantime.const etag = asset ? `"${asset}"` : nullconst cacheHeaders: Record<string, string> = etag- ? { 'cache-control': 'private, no-cache', etag }+ ? { 'cache-control': 'private, max-age=300', etag }: { 'cache-control': 'private, max-age=86400' }let image: { bytes: Buffer; mimeType: string } | nulltry {diff --git a/app/api/prompt/route.ts b/app/api/prompt/route.tsindex 571f817..cd2bbb2 100644--- a/app/api/prompt/route.ts+++ b/app/api/prompt/route.ts@@ -11,7 +11,7 @@ import { trialEnabled, trialIpHash, trialStore, type TrialCredential, type Trialimport { TRIAL_MODEL, usesFreePrompts } from '../../../lib/trial-policy'import { DEFAULT_MODEL, log } from '../../../lib/session'import { validatePromptImages, validatePromptPreviews } from '../../../lib/prompt-images'-import { DesignAssetLimitError, retryDesignAssetCleanup, storeDesignAssets } from '../../../lib/design-assets'+import { DesignAssetLimitError, retryDesignAssetCleanup, startDesignAssets, type PendingDesignAssets } from '../../../lib/design-assets'import { PROMPT_TEXT_LIMIT_ERROR, PROMPT_TEXT_MAX_BYTES, promptTextBytes } from '../../../lib/prompt-limits'export const maxDuration = 800@@ -105,14 +105,13 @@ export const POST = withPromptRoute(async (request, context) => {try {activated = await context.activate()activatedAt = performance.now()- // Stored before libfx sees the prompt, so a design can name them at once; a failure- // refuses the prompt here and the composer keeps the draft. Funding is checked first,- // so nothing is stored for a prompt that could not run.- let assets: string[] = []+ // Funding is checked first, so nothing is stored for a prompt that could not run.+ // The caps are taken here; the upload then runs while libfx admits the prompt.+ let assets: PendingDesignAssets | undefinedif (images) {after(() => retryDesignAssetCleanup().catch(() => console.error('[fx0] asset cleanup retry unavailable')))assertPromptFunding(activated.host, funding, 'kind' in session ? undefined : session)- assets = await storeDesignAssets(activated.id, user.id, images)+ assets = await startDesignAssets(activated.id, user.id, images)}submitted = await submitPrompt(activated.host,diff --git a/lib/agent.ts b/lib/agent.tsindex 99ae505..f1e3514 100644--- a/lib/agent.ts+++ b/lib/agent.ts@@ -1,9 +1,9 @@-import type { FxAgent } from 'libfx'+import type { FxAgent, FxSession } from 'libfx'import { createTools, type AskQuestion, type ToolHost } from './tools'import { assertGenerationCredential } from './gateway-session'import { remoteImagesEnabled } from './design-artifact'import { prefetchRemoteImages } from './remote-image-cache'-import { deleteDesignAssets } from './design-assets'+import { deleteDesignAssets, type PendingDesignAssets } from './design-assets'import { chatSession, durableAgent, runSubagent, type ChatRuntime } from './libfx-agent'import { project } from './projector'import {@@ -237,7 +237,7 @@ export async function submitPrompt(previews?: string[],funding?: TrialCredential,submitter?: SessionData,- assets: string[] = [],+ assets?: PendingDesignAssets,): Promise<{ id: number; seq: number; trialChat: boolean }> {// A turn needs at least 15 minutes left on the credential it bills; libfx runs// it, including a crash recovery, within that.@@ -249,6 +249,7 @@ export async function submitPrompt(stages.mark('journal')log(host.id, 'prompt:submitted', { id: submitted.id, clientId, imageCount: images?.length ?? 0, idle: submitted.idle })const id = submitted.id+ let accepted: FxSession | null = nulltry {// Pinned before libfx sees the message: whether it starts a turn or joins// the running one, no turn can run without its funding record.@@ -260,8 +261,8 @@ export async function submitPrompt(const { session } = await chatSession(agent(), host, stored?.libfxSessionId ?? null, agentInstructions(host), id, !!funding)stages.mark('bind')// Each attached image, in order, as the design can name it.- const placeable = assets.length- ? `\n\n[Attached images the design can show: ${assets.map((handle, index) => `Image #${index + 1} = fx0-asset:${handle}`).join(', ')}]`+ const placeable = assets?.handles.length+ ? `\n\n[Attached images the design can show: ${assets.handles.map((handle, index) => `Image #${index + 1} = fx0-asset:${handle}`).join(', ')}]`: ''const message = `[${author}] ${text}${placeable}`const input = images?.length ? [{ type: 'text' as const, text: message }, ...images] : message@@ -279,10 +280,20 @@ export async function submitPrompt(await session.steer(input, { messageId: `m${id}` })}stages.mark('accepted')+ // The images uploaded while libfx admitted the prompt; a design can name them only+ // once they are stored, which comes long before the model publishes one.+ if (assets) {+ accepted = session+ await assets.stored+ stages.mark('assets')+ }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)}` })+ // A turn already admitted must not go on naming images that were never stored.+ if (accepted) await accepted.cancel().catch(() => {})+ const text = accepted ? 'The attached images could not be stored. Send the message again.' : `prompt rejected: ${errorText(error)}`+ await emit(host, { type: 'error', id, turnId: id, text })}return { id, seq: submitted.event.seq ?? 0, trialChat: submitted.trialChat }}diff --git a/lib/design-assets.ts b/lib/design-assets.tsindex 0141197..decc4bb 100644--- a/lib/design-assets.ts+++ b/lib/design-assets.ts@@ -31,12 +31,21 @@ export class DesignAssetLimitError extends Error {const pathname = (sessionId: string, handle: string) => `chats/${sessionId}/${handle}`+export interface PendingDesignAssets {+ /** Usable at once, in order: what the model is told. */+ handles: string[]+ /** Settles when the files and the chat's manifest are in place; rejects after cleaning up. */+ stored: Promise<void>+}+/**- * Stores a message's images and records them for the chat, all or none, within the- * chat's and the user's caps. Returns their handles in order.+ * Takes the chat's and the user's caps for a message's images and starts storing them,+ * so the prompt can go to libfx while the files upload. The handles are final now; a+ * design can name them once `stored` settles. Throws DesignAssetLimitError before+ * anything is stored when a cap would be passed.*/-export async function storeDesignAssets(sessionId: string, userId: string, images: PromptImage[], store = BLOB): Promise<string[]> {- if (!images.length) return []+export async function startDesignAssets(sessionId: string, userId: string, images: PromptImage[], store = BLOB): Promise<PendingDesignAssets> {+ if (!images.length) return { handles: [], stored: Promise.resolve() }const reservedAt = Date.now()const reserved = await reserveDesignAssets(sessionId, userId, images.length, reservedAt)if (reserved !== 'ok') throw new DesignAssetLimitError(reserved)@@ -46,31 +55,42 @@ export async function storeDesignAssets(sessionId: string, userId: string, imagebytes: Buffer.from(image.data, 'base64'),}))const handles = assets.map(({ handle }) => handle)- try {- // Wait for every put before rolling back: a sibling can still be in flight when- // another fails. Delete every attempted path, including an ambiguous timeout.- const results = await Promise.allSettled(assets.map(({ handle, mimeType, bytes }) =>- store.put(pathname(sessionId, handle), bytes, { access: 'private', contentType: mimeType, addRandomSuffix: false })))- if (results.some((result) => result.status === 'rejected')) throw new Error('Unable to store the attached images')- await recordDesignAssets(sessionId, assets.map(({ handle, mimeType, bytes }) => ({ handle, mimeType, bytes: bytes.length })))- return handles- } catch (error) {- const cleanup = { sessionId, userId, handles, reservedAt }- try {- await queueDesignAssetCleanup(cleanup)- } catch {- console.error('[fx0] asset cleanup persistence failed', { sessionId })- }+ const stored = (async () => {try {- await store.del(handles.map((handle) => pathname(sessionId, handle)))- // Complete atomically even if the enqueue response was lost. The receipt- // prevents a delayed enqueue or another worker from refunding this twice.- await finishDesignAssetCleanup(cleanup)- } catch {- console.error('[fx0] asset cleanup pending', { sessionId })+ // Wait for every put before rolling back: a sibling can still be in flight when+ // another fails. Delete every attempted path, including an ambiguous timeout.+ const results = await Promise.allSettled(assets.map(({ handle, mimeType, bytes }) =>+ store.put(pathname(sessionId, handle), bytes, { access: 'private', contentType: mimeType, addRandomSuffix: false })))+ if (results.some((result) => result.status === 'rejected')) throw new Error('Unable to store the attached images')+ await recordDesignAssets(sessionId, assets.map(({ handle, mimeType, bytes }) => ({ handle, mimeType, bytes: bytes.length })))+ } catch (error) {+ const cleanup = { sessionId, userId, handles, reservedAt }+ try {+ await queueDesignAssetCleanup(cleanup)+ } catch {+ console.error('[fx0] asset cleanup persistence failed', { sessionId })+ }+ try {+ await store.del(handles.map((handle) => pathname(sessionId, handle)))+ // Complete atomically even if the enqueue response was lost. The receipt+ // prevents a delayed enqueue or another worker from refunding this twice.+ await finishDesignAssetCleanup(cleanup)+ } catch {+ console.error('[fx0] asset cleanup pending', { sessionId })+ }+ throw error}- throw error- }+ })()+ // The caller awaits it; this only keeps an early failure from being unhandled.+ stored.catch(() => {})+ return { handles, stored }+}++/** Stores a message's images, all or none, within the caps. Returns their handles in order. */+export async function storeDesignAssets(sessionId: string, userId: string, images: PromptImage[], store = BLOB): Promise<string[]> {+ const pending = await startDesignAssets(sessionId, userId, images, store)+ await pending.stored+ return pending.handles}/** Retry failed rollbacks without losing their records or refunding quota twice. */diff --git a/lib/remote-image-cache.ts b/lib/remote-image-cache.tsindex 650c851..c523140 100644--- a/lib/remote-image-cache.ts+++ b/lib/remote-image-cache.ts@@ -38,7 +38,9 @@ export function prefetchRemoteImages(observer: DesignDraftObserver | undefined,drafts.set(callId, draft)if (Date.now() - draft.scannedAt < SCAN_MS) returndraft.scannedAt = Date.now()- scan(draft.html)+ // Off the stream's path: the model's tokens reach libfx before this runs.+ const html = draft.html+ setImmediate(() => scan(html))},end(callId) {const draft = drafts.get(callId)diff --git a/tests/remote-image.test.mjs b/tests/remote-image.test.mjsindex 45f4c94..a6e5522 100644--- a/tests/remote-image.test.mjs+++ b/tests/remote-image.test.mjs@@ -214,9 +214,12 @@ test('images are prefetched from finished tags while the model writes, once eachconst chunks = ['<!doctype html><html><head><style>.h{background:url(https://example.com/bg', '.png)}</style></head><body><img src="https://example.com/he','ro.png" alt=""><img src="https://example.com/hero.png">',...Array.from({ length: 14 }, (_, index) => `<img src="https://example.com/${index}.png">`), '</body></html>']+ const tick = () => new Promise((resolve) => setImmediate(resolve))observer.delta('call', chunks[0])+ await tick()assert.deepEqual(loads, [], 'an unfinished url( is never fetched')observer.delta('call', chunks[1])+ await tick()assert.deepEqual(loads, [], 'scans are spaced out while chunks arrive')for (const chunk of chunks.slice(2)) observer.delta('call', chunk)await observer.end('call')@@ -233,9 +236,12 @@ test('prefetching scans again once the spacing has passed, and does nothing withconst loads = []const observer = prefetchRemoteImages(undefined, async (url) => { loads.push(url); return PNG_URL })observer.delta('call', '<img src="https://example.com/early.png">')- assert.deepEqual(loads, ['https://example.com/early.png'], 'the first finished tag starts at once')+ assert.deepEqual(loads, [], 'the scan never runs inside the stream\'s own delta call')+ await new Promise((resolve) => setImmediate(resolve))+ assert.deepEqual(loads, ['https://example.com/early.png'], 'the first finished tag starts right after')await new Promise((resolve) => setTimeout(resolve, 300))observer.delta('call', '<img src="https://example.com/later.png">')+ await new Promise((resolve) => setImmediate(resolve))assert.deepEqual(loads, ['https://example.com/early.png', 'https://example.com/later.png'])process.env.FX0_REMOTE_IMAGES = '0'diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex a2fd875..a9310bc 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -522,7 +522,7 @@ test('the wrapper\'s image route serves only URLs artifact-frame signed for this}})-test('private attachments revalidate ownership and the manifest; a 304 never downloads Blob bytes', async () => {+test('private attachments are kept five minutes, then revalidate ownership and the manifest; a 304 never downloads Blob bytes', async () => {process.env.SESSION_SECRET ??= 'deterministic-route-security-secret-32+'const { artifactImageSrc } = await import('../lib/artifact-image.ts')const handle = 'AbCdEfGhIjKlMnOpQrStUvWx'@@ -534,13 +534,13 @@ test('private attachments revalidate ownership and the manifest; a 304 never dowconst first = await get()assert.equal(first.status, 200)assert.equal(await first.text(), 'image')- assert.equal(first.headers.get('cache-control'), 'private, no-cache')+ assert.equal(first.headers.get('cache-control'), 'private, max-age=300')const etag = first.headers.get('etag')assert.ok(etag)const kept = await get(etag)assert.equal(kept.status, 304)assert.equal(await kept.text(), '')- assert.equal(kept.headers.get('cache-control'), 'private, no-cache')+ assert.equal(kept.headers.get('cache-control'), 'private, max-age=300')assert.equal(kept.headers.get('etag'), etag)assert.equal(s.blobGets, 1)s.assets.clear()diff --git a/tests/runtime.test.mjs b/tests/runtime.test.mjsindex 95966c3..014d87e 100644--- a/tests/runtime.test.mjs+++ b/tests/runtime.test.mjs@@ -407,6 +407,32 @@ test('an image prompt rejected during admission produces a visible error', asyncassert.ok(f.events.some(item => item.type === 'error' && item.text === 'prompt rejected: image admission failed'))})+test('attached images upload while libfx admits the prompt; the model gets their references at once', async (t) => {+ const f = fixture(t)+ const images = [{ type: 'image', mimeType: 'image/png', data: 'cG5n' }]+ let finishUpload+ const stored = new Promise((resolve) => { finishUpload = resolve })+ const submitted = runtime.submitPrompt(boundHost('asset-chat'), 'Ada', 'Use my logo', undefined, images, undefined, undefined, undefined, undefined,+ { handles: ['AbCdEfGhIjKlMnOpQrStUvWx'], stored })+ await new Promise((resolve) => setTimeout(resolve, 20))+ assert.equal(f.sent.length, 1, 'libfx has the prompt before the upload finished')+ assert.equal(f.sent[0].input[0].text, '[Ada] Use my logo\n\n[Attached images the design can show: Image #1 = fx0-asset:AbCdEfGhIjKlMnOpQrStUvWx]')+ finishUpload()+ await submitted+ assert.ok(!f.events.some((item) => item.type === 'error'))+ assert.equal(f.calls.filter((call) => call[0] === 'cancel').length, 0)+})++test('if the upload fails after libfx admitted the prompt, the turn is cancelled with a visible error', async (t) => {+ const f = fixture(t)+ const stored = Promise.reject(new Error('Unable to store the attached images'))+ stored.catch(() => {})+ await runtime.submitPrompt(boundHost('asset-fail'), 'Ada', 'Use my logo', undefined, [{ type: 'image', mimeType: 'image/png', data: 'cG5n' }],+ undefined, undefined, undefined, undefined, { handles: ['AbCdEfGhIjKlMnOpQrStUvWx'], stored })+ assert.equal(f.calls.filter((call) => call[0] === 'cancel').length, 1, 'the admitted turn is stopped')+ assert.ok(f.events.some((item) => item.type === 'error' && item.text === 'The attached images could not be stored. Send the message again.'))+})+test('a prompt libfx rejects ends its message with an error, and one needing renewal is refused before any write', async (t) => {const f = fixture(t)f.acceptError = new Error('input too large')