A lightweight design agent built with libfx. Describe a page and an agent writes it as one HTML file.
TypeScript
Send long prompts whole instead of cutting them at 4,000 characters
Esteban Suarez3f25ae4e3a8d
diff --git a/app/api/prompt/route.ts b/app/api/prompt/route.tsindex c28e1d7..5e5dd89 100644--- a/app/api/prompt/route.ts+++ b/app/api/prompt/route.ts@@ -10,6 +10,7 @@ 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, log } from '../../../lib/session'+import { PROMPT_TEXT_LIMIT_ERROR, PROMPT_TEXT_MAX_BYTES, promptTextBytes } from '../../../lib/prompt-limits'export const maxDuration = 800@@ -49,6 +50,8 @@ export const POST = withPromptRoute(async (request, context) => {): undefinedif (!text && !images?.length) return Response.json({ error: 'empty prompt' }, { status: 400 })+ // Rejected whole, never cut: the composer keeps the draft and shows this error.+ if (promptTextBytes(text) > PROMPT_TEXT_MAX_BYTES) return Response.json({ error: PROMPT_TEXT_LIMIT_ERROR }, { status: 413 })if ((images?.length ?? 0) > 8) return Response.json({ error: 'prompt cannot contain more than 8 images' }, { status: 413 })let imageBytes = 0for (const [index, image] of (images ?? []).entries()) {@@ -124,7 +127,7 @@ export const POST = withPromptRoute(async (request, context) => {submitted = await submitPrompt(activated.host,author,- text.slice(0, 4000),+ text,clientId,images?.length ? images : undefined,initial,diff --git a/components/composer.tsx b/components/composer.tsxindex a3dfa98..f78aa61 100644--- a/components/composer.tsx+++ b/components/composer.tsx@@ -9,6 +9,7 @@ import { Button } from './ui/button'import { Popover, PopoverContent, PopoverTrigger } from './ui/popover'import { checkDesignMdUpload, DESIGN_MD_CATALOG, DESIGN_MD_MAX_BYTES } from '../lib/design-md'import { fastClick } from '../lib/fast-click'+import { PROMPT_TEXT_LIMIT_ERROR, PROMPT_TEXT_MAX_BYTES, promptTextBytes } from '../lib/prompt-limits'import { focusPrompt, registerPrompt } from '../lib/prompt-focus'import { Textarea } from './ui/textarea'import { Tooltip, TooltipContent, TooltipTrigger } from './ui/tooltip'@@ -638,6 +639,11 @@ export default function Composer({selectCommand(exactCommand)return}+ // The server rejects longer text, so it never leaves the composer.+ if (promptTextBytes(text) > PROMPT_TEXT_MAX_BYTES) {+ setAttachmentError(PROMPT_TEXT_LIMIT_ERROR)+ return+ }if (onSend(text, images) === false) returnsetValue('')setImages([])diff --git a/components/guest-draft.ts b/components/guest-draft.tsindex f025ce4..21e25a9 100644--- a/components/guest-draft.ts+++ b/components/guest-draft.ts@@ -1,4 +1,5 @@import { checkDesignMdUpload, DESIGN_MD_CATALOG, DESIGN_MD_MAX_BYTES } from '../lib/design-md'+import { PROMPT_TEXT_MAX_BYTES } from '../lib/prompt-limits'import type { AttachedImage, DesignMdPick } from './composer'export interface GuestDraft {@@ -11,9 +12,9 @@ export interface GuestDraft {export const GUEST_DRAFT_KEY = 'fx0-guest-draft:v2'export const GUEST_DRAFT_TTL = 15 * 60 * 1000-// Escaping can double the design.md's bytes in JSON.-export const GUEST_DRAFT_MAX_BYTES = 3 * 1024 * 1024 + 128 * 1024 + 2 * DESIGN_MD_MAX_BYTES-const TEXT_CAP = 64 * 1024+// Escaping can double the text's and the design.md's bytes in JSON.+export const GUEST_DRAFT_MAX_BYTES = 3 * 1024 * 1024 + 2 * PROMPT_TEXT_MAX_BYTES + 2 * DESIGN_MD_MAX_BYTES+const TEXT_CAP = PROMPT_TEXT_MAX_BYTESconst IMAGE_CAP = 3 * 1024 * 1024type DraftStorage = Pick<Storage, 'getItem' | 'setItem' | 'removeItem'>diff --git a/lib/prompt-limits.ts b/lib/prompt-limits.tsnew file mode 100644index 0000000..7f6c4c9--- /dev/null+++ b/lib/prompt-limits.ts@@ -0,0 +1,10 @@+// One prompt's text, checked in the composer and again by /api/prompt. With the+// 3 MiB of images, their previews and a DESIGN.md, a full prompt still fits in+// one function request body (4.5 MB) and far inside libfx's 8 MiB durable input.+export const PROMPT_TEXT_MAX_BYTES = 256 * 1024++export const PROMPT_TEXT_LIMIT_ERROR = 'prompt text must be 256 KB or smaller.'++export function promptTextBytes(text: string): number {+ return new TextEncoder().encode(text).byteLength+}diff --git a/tests/guest-draft.test.mjs b/tests/guest-draft.test.mjsindex 819222e..7af90db 100644--- a/tests/guest-draft.test.mjs+++ b/tests/guest-draft.test.mjs@@ -4,7 +4,7 @@ import test from 'node:test'// guest-draft.ts imports lib/design-md without an extension, as Next resolves it.registerHooks({ resolve(specifier, context, next) {- if (specifier === '../lib/design-md') return next('../lib/design-md.ts', context)+ if (specifier === '../lib/design-md' || specifier === '../lib/prompt-limits') return next(`${specifier}.ts`, context)return next(specifier, context)} })const { GUEST_DRAFT_KEY, GUEST_DRAFT_TTL, GUEST_DRAFT_MAX_BYTES, saveGuestDraft, takeGuestDraft, parseGuestDraft, clearGuestDraft } = await import('../components/guest-draft.ts')@@ -29,7 +29,7 @@ test('OAuth draft contains only unsent text and image bytes, restores once and e})test('invalid shapes, credential fields, unsafe previews, timestamps and oversized bytes are rejected', () => {- for (const value of [null, [], {}, { ...envelope, version: 1 }, { ...envelope, submit: 'yes' }, { ...envelope, designMd: { id: 'not-in-catalog', name: 'X' } }, { ...envelope, designMd: { id: 'vercel', name: 'Not Vercel' } }, { ...envelope, designMd: { name: 'design.md', text: '' } }, { ...envelope, designMd: { name: 'design.md', text: 'x'.repeat(100 * 1024 + 1) } }, { ...envelope, designMd: { name: 'design.md', text: 'ok', source: 'upload' } }, { ...envelope, token: 'secret' }, { ...envelope, expiresAt: 100 + GUEST_DRAFT_TTL + 1 }, { ...envelope, text: 42 }, { ...envelope, text: '😀'.repeat(20_000) }, { ...envelope, images: Array(9).fill(envelope.images[0]) }, { ...envelope, images: [{ ...envelope.images[0], preview: 'https://private.example' }] }, { ...envelope, images: [{ data: 'javascript:alert(1)', mimeType: 'image/png' }] }, { ...envelope, images: [{ data: 'aGVsbG8=', mimeType: 'text/html' }] }, { ...envelope, images: [{ data: 'A'.repeat(3 * 1024 * 1024 + 4), mimeType: 'image/png' }] }]) {+ for (const value of [null, [], {}, { ...envelope, version: 1 }, { ...envelope, submit: 'yes' }, { ...envelope, designMd: { id: 'not-in-catalog', name: 'X' } }, { ...envelope, designMd: { id: 'vercel', name: 'Not Vercel' } }, { ...envelope, designMd: { name: 'design.md', text: '' } }, { ...envelope, designMd: { name: 'design.md', text: 'x'.repeat(100 * 1024 + 1) } }, { ...envelope, designMd: { name: 'design.md', text: 'ok', source: 'upload' } }, { ...envelope, token: 'secret' }, { ...envelope, expiresAt: 100 + GUEST_DRAFT_TTL + 1 }, { ...envelope, text: 42 }, { ...envelope, text: '😀'.repeat(64 * 1024 + 1) }, { ...envelope, images: Array(9).fill(envelope.images[0]) }, { ...envelope, images: [{ ...envelope.images[0], preview: 'https://private.example' }] }, { ...envelope, images: [{ data: 'javascript:alert(1)', mimeType: 'image/png' }] }, { ...envelope, images: [{ data: 'aGVsbG8=', mimeType: 'text/html' }] }, { ...envelope, images: [{ data: 'A'.repeat(3 * 1024 * 1024 + 4), mimeType: 'image/png' }] }]) {assert.equal(parseGuestDraft(JSON.stringify(value), 100), null)}assert.equal(parseGuestDraft('{', 100), null)diff --git a/tests/guest-ui.test.mjs b/tests/guest-ui.test.mjsindex a757e0c..8ec1684 100644--- a/tests/guest-ui.test.mjs+++ b/tests/guest-ui.test.mjs@@ -975,7 +975,7 @@ test('how it works explains fx0 and links to forking its repo', (t) => {test('oversized OAuth draft blocks navigation visibly rather than losing text or images', (t) => {const ui = browser(t)const app = mount(t, ui)- const oversized = { ...draft, text: 'a'.repeat(64 * 1024 + 1) }+ const oversized = { ...draft, text: 'a'.repeat(256 * 1024 + 1) }app.handle().restoreDraft(oversized)app.render()app.send()diff --git a/tests/route-security.test.mjs b/tests/route-security.test.mjsindex 6e45448..e973313 100644--- a/tests/route-security.test.mjs+++ b/tests/route-security.test.mjs@@ -109,7 +109,7 @@ const mocks = {}export async function warmChat(host) { effect('warm', host); }export function log() {}- export async function submitPrompt(host, author, ...args) { effect('prompt', host); globalThis.securityTest.author = author; globalThis.securityTest.funding = args[5]; return { id: 1, seq: globalThis.securityTest.submittedSeq ?? 1, trialChat: !!args[5] || !!globalThis.securityTest.generationOptions?.trialChat }; }+ export async function submitPrompt(host, author, ...args) { effect('prompt', host); globalThis.securityTest.author = author; globalThis.securityTest.text = args[0]; globalThis.securityTest.funding = args[5]; return { id: 1, seq: globalThis.securityTest.submittedSeq ?? 1, trialChat: !!args[5] || !!globalThis.securityTest.generationOptions?.trialChat }; }export async function followChat(host) { effect('wake', host); }export async function renewTurnCredential(host) { effect('renew', host); }export async function cancelCurrent(host) { effect('cancel', host); return true; }@@ -310,6 +310,21 @@ test('rejected guest input creates no ownership row or runtime, and a failed resassert.equal(state.trace.includes('host'), false)})+test('long prompt text reaches the agent whole, and text past the limit is refused before any work', async () => {+ let state = reset()+ const long = 'x'.repeat(256 * 1024)+ const response = await routes.prompt.POST(request('prompt', 'POST', own, { text: long }))+ assert.equal(response.status, 200)+ assert.equal(state.text, long)+ await response.text()+ state = reset()+ const refused = await routes.prompt.POST(request('prompt', 'POST', own, { text: 'é'.repeat(128 * 1024 + 1) }))+ assert.equal(refused.status, 413)+ assert.match((await refused.json()).error, /256 KB/)+ assert.equal(state.trace.includes('prompt'), false)+ assert.equal(state.trace.includes('reserve'), false)+})+test('BotID failure does not gate paid-only models or paid continuation after the allowance', async () => {let state = reset({ trialEnabled: true, bot: true, generationOptions: { model: 'paid/model', effort: 'high', fast: false } })let response = await routes.prompt.POST(request('prompt', 'POST', own, { text: 'Paid' }))