Skip to content
Source
Sign inSign up
TanStack/ai
Open History to see the last commit

server.ts

60 lines2.9 KB

1/**
2 * Custom Cloudflare Workers entry point — the whole app in one Worker.
3 *
4 * TanStack Start's default Worker entry only serves the SSR app. To ALSO ship the
5 * sandbox agent's Durable Objects + container in the same deploy, we use the
6 * documented escape hatch: a custom `src/server.ts` (wired via `wrangler.jsonc`
7 * `main`) that re-exports the DO classes and wraps the Start `fetch` handler.
8 *
9 * • `RunCoordinator` / `Sandbox` named exports → the wrangler DO + container
10 * bindings.
11 * • `default.fetch` composes three handlers, in order:
12 * 1. `proxyToSandbox` — sandbox preview-port traffic, routed by hostname.
13 * 2. the agent Worker (`agent.worker`) — the agent's HTTP surface
14 * (`POST /runs`, `GET /runs/:id/stream`, `/_bridge`, `/tool-exec`). The
15 * container calls back on the root-level `/_bridge` + `/tool-exec` paths
16 * (built from `PUBLIC_HOSTNAME`), so those roots are reserved for it.
17 * 3. TanStack Start (`handler.fetch`) — the UI + its `/api/*` routes.
18 *
19 * Binding types come from `worker-configuration.d.ts` (`pnpm cf-typegen`); the
20 * env *shape* the agent expects is this app's `AppEnv` (the package's harness-
21 * agnostic `SandboxAgentEnv` plus the app's `ANTHROPIC_API_KEY`).
22 *
23 * @see https://developers.cloudflare.com/workers/framework-guides/web-apps/tanstack-start/
24 */
25import handler from '@tanstack/react-start/server-entry'
26import { proxyToSandbox } from '@cloudflare/sandbox'
27import { agent } from './agent'
28import type { AppEnv } from './agent'
29​
30// Re-exported so the DO + container `class_name`s in wrangler.jsonc
31// (`RunCoordinator`, `Sandbox`) resolve in the Worker bundle.
32export const RunCoordinator = agent.Coordinator
33export const Sandbox = agent.Sandbox
34​
35/** Root paths owned by the agent Worker; everything else falls through to Start. */
36const AGENT_PATHS = ['/runs', '/_bridge', '/tool-exec']
37​
38const ownedByAgent = (pathname: string): boolean =>
39 AGENT_PATHS.some((p) => pathname === p || pathname.startsWith(`${p}/`))
40​
41export default {
42 async fetch(request, env, ctx) {
43 // 1. Sandbox preview-port traffic is routed by hostname, so it gets first
44 // refusal on every request before any path-based routing.
45 const proxied = await proxyToSandbox(request, env)
46 if (proxied) return proxied
47​
48 // 2. The agent's own HTTP surface. `agent.worker` is built by the package's
49 // `createSandboxAgentWorker`, so its `fetch` is always present.
50 const { pathname } = new URL(request.url)
51 if (ownedByAgent(pathname) && agent.worker.fetch) {
52 return agent.worker.fetch(request, env, ctx)
53 }
54​
55 // 3. The TanStack Start app (UI + `/api/*` server routes). Start's handler
56 // takes the request (its 2nd arg is an SSR-context option, not the Worker
57 // env); bindings are read ambiently via the Cloudflare Vite plugin.
58 return handler.fetch(request)
59 },
60} satisfies ExportedHandler<AppEnv>