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

Warm the prompt's own function while the user types

Esteban Suarez87f1a5902658
diff --git a/app/api/warm/route.ts b/app/api/warm/route.ts
index eeaacd4..5a2c6fa 100644
--- a/app/api/warm/route.ts
+++ b/app/api/warm/route.ts
@@ -1,7 +1,10 @@
import { log, warmChat } from '../../../lib/agent'
import { withSessionRoute } from '../../../lib/route-auth'
-export const maxDuration = 60
+// The same maxDuration as /api/prompt, so Vercel bundles both routes into one function
+// and this warm-up runs on the instances that serve the prompt. With a different value
+// they become separate functions, and warming one leaves the other cold.
+export const maxDuration = 800
// Called while the user types. The owner check, runtime binding, state hydration and
// the process's libfx agent happen now, so the send that follows skips them. Best
diff --git a/lib/agent.ts b/lib/agent.ts
index cd31a44..11e7995 100644
--- a/lib/agent.ts
+++ b/lib/agent.ts
@@ -207,7 +207,9 @@ async function hydrateSession(host: SessionState): Promise<StoredSession | null>
// Warm-up while the user types: the chat's state and the process's agent.
export async function warmChat(host: SessionState): Promise<void> {
await hydrateSession(host)
- agent()
+ // newSessionId() loads libfx's durable backend (about 330 ms of module loading on a
+ // new instance) and returns an id without writing anything; the id is discarded.
+ await agent().newSessionId()
}
// Prompts this instance has submitted; the first one also pays for loading
diff --git a/package.json b/package.json
index b628117..293b6f7 100644
--- a/package.json
+++ b/package.json
@@ -9,7 +9,7 @@
"build": "next build",
"typecheck": "next typegen && tsc --noEmit --incremental false",
"start": "next start",
- "test:core": "node --experimental-strip-types --test tests/core.test.mjs tests/transcript.test.mjs tests/design-draft.test.mjs tests/design-publish.test.mjs tests/event-delivery.test.mjs tests/event-stream.test.mjs tests/public-fetch.test.mjs tests/remote-image.test.mjs tests/prompt-images.test.mjs tests/design-assets.test.mjs tests/capture-protocol.test.mjs tests/runtime.test.mjs",
+ "test:core": "node --experimental-strip-types --test tests/core.test.mjs tests/function-grouping.test.mjs tests/transcript.test.mjs tests/design-draft.test.mjs tests/design-publish.test.mjs tests/event-delivery.test.mjs tests/event-stream.test.mjs tests/public-fetch.test.mjs tests/remote-image.test.mjs tests/prompt-images.test.mjs tests/design-assets.test.mjs tests/capture-protocol.test.mjs tests/runtime.test.mjs",
"test:auth": "node --experimental-strip-types --test tests/auth.test.mjs tests/auth-callback.test.mjs tests/signout.test.mjs tests/ownership.test.mjs tests/history.test.mjs tests/route-security.test.mjs tests/gateway-session.test.mjs tests/libfx-transport.test.mjs tests/libfx-agent.test.mjs tests/auth-ui.test.mjs tests/html-ui.test.mjs tests/guest-ui.test.mjs tests/guest-draft.test.mjs tests/trial.test.mjs tests/turn-funding.test.mjs",
"test:journal:integration": "node --env-file-if-exists=.env.local --experimental-strip-types --test tests/journal.integration.mjs",
"test:smoke": "node tests/local-auth-smoke.mjs",
diff --git a/tests/function-grouping.test.mjs b/tests/function-grouping.test.mjs
new file mode 100644
index 0000000..3e2a816
--- /dev/null
+++ b/tests/function-grouping.test.mjs
@@ -0,0 +1,17 @@
+import assert from 'node:assert/strict'
+import test from 'node:test'
+import { readFile } from 'node:fs/promises'
+
+// Vercel bundles routes with the same function configuration into one function. The
+// typing warm-up only helps if it runs on the instances that serve the prompt, so both
+// routes must declare the same maxDuration.
+const maxDuration = async route => {
+ const source = await readFile(new URL(`../app/api/${route}/route.ts`, import.meta.url), 'utf8')
+ const match = source.match(/^export const maxDuration = (\d+)$/m)
+ assert.ok(match, `${route} declares maxDuration`)
+ return Number(match[1])
+}
+
+test('the typing warm-up shares the prompt route\'s function configuration', async () => {
+ assert.equal(await maxDuration('warm'), await maxDuration('prompt'))
+})