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

agent.ts

92 lines4.1 KB

1/**
2 * `@tanstack/ai-sandbox-cloudflare/agent` — the Workers-runtime building blocks
3 * for running a TanStack AI sandbox agent on Cloudflare with minimal app code.
4 *
5 * The headline API is {@link createCloudflareSandboxAgent}: one configured
6 * function call returns the Durable Object coordinator + the Sandbox DO + the
7 * Worker fetch handler, so an app's `worker.ts` is just export wiring. The
8 * coordinator base classes, the concrete coordinators, the Worker factory, and
9 * the durable run-log are exported too for apps that want to compose them
10 * directly.
11 *
12 * This entry imports `cloudflare:workers` and is Workers-only — keep it out of
13 * the node-importable main entry (`@tanstack/ai-sandbox-cloudflare`).
14 */
15​
16// The headline factory + its config/result types.
17export { createCloudflareSandboxAgent } from './factory'
18export type {
19 CloudflareSandboxAgent,
20 CloudflareSandboxAgentConfig,
21 DoDrivesAgentConfig,
22 ColocatedAgentConfig,
23 SandboxAgentEnv,
24} from './factory'
25​
26// The abstract base + its run input + the host resolvers (so apps that build their
27// own host tools / sandbox providers resolve the callback hosts the same way the
28// coordinators do): `resolveBridgeOrigin` for the container→Worker bridge/tool-exec
29// origin, `resolvePreviewHost` for browser-facing `exposePort` preview URLs.
30export {
31 SandboxCoordinator,
32 resolveBridgeOrigin,
33 resolvePreviewHost,
34} from './coordinator'
35export type { StartRunInput } from './coordinator'
36​
37// The browser-preview building blocks: a ready-made `exposePreview` server tool
38// (mints a preview URL for an in-sandbox dev server) and the system-prompt guidance
39// that keeps previews from reload-looping (the proxy can't tunnel HMR). Wire both
40// into the agent — `tools: (i, e) => [exposePreviewTool(i, e)]`,
41// `systemPrompts: [PREVIEW_GUIDANCE]`. Owned here because the limitation is the
42// transport's, not any app's.
43export { exposePreviewTool, PREVIEW_GUIDANCE } from './preview-tool'
44export type { PreviewToolEnv } from './preview-tool'
45​
46// The two concrete coordinators + their per-run config + Env types.
47export { ChatSandboxCoordinator } from './chat-coordinator'
48export type { ChatCoordinatorEnv, ChatRunConfig } from './chat-coordinator'
49export { ContainerSandboxCoordinator } from './container-coordinator'
50export type {
51 ContainerCoordinatorEnv,
52 ContainerRunConfig,
53} from './container-coordinator'
54​
55// The shared `POST /run` wire contract (built by the coordinator, validated by
56// the `/runner` entry). Defined runtime-agnostically in `./protocol`.
57export { parseContainerRunRequest } from './protocol'
58export type { ContainerRunRequest, HarnessId } from './protocol'
59​
60// The Worker fetch-handler factory + its resolver type.
61export { createSandboxAgentWorker } from './worker'
62export type { ResolveCoordinator } from './worker'
63​
64// The durable run-log + the Web Crypto bearer helper (for direct composition).
65export { DurableObjectRunEventLog } from './run-log-do'
66export { timingSafeBearerEqualWeb } from './web-crypto'
67​
68// The run event-log surface, for apps bringing their own backend.
69// `DurableObjectRunEventLog` (above) is this log's DO-storage-backed mirror;
70// `InMemoryRunEventLog` is the single-process reference implementation.
71//
72// The vocabulary is core's: statuses, `RunError`, and `isTerminalRunStatus`
73// come from `@tanstack/ai` (the pre-1.0 `Legacy*`-prefixed types are gone —
74// see the CONVERGED VOCABULARY note in `./run-log` for the storage
75// migration). `RunLogRecord` is core's `RunRecord` plus the log's own
76// `lastSeq` cursor and `updatedAt` activity clock.
77export { InMemoryRunEventLog, migrateStoredRunRecord } from './run-log'
78export type {
79 RunEventLog,
80 RunEvent,
81 RunEventLogReadOptions,
82 RunLogRecord,
83 RunRecordPatch,
84} from './run-log'
85​
86// The portable seams the coordinator itself is built on: a `RunEventLog` as
87// core's `RunStore` (`runLogStore`) and one of its runs as core's
88// `StreamDurability` (`runLogStream`). Apps composing directly bind core's
89// `RunController` from `@tanstack/ai-sandbox` with these, exactly as
90// `SandboxCoordinator` does.
91export { runLogStore, runLogStream } from './durability'
92export type { RunLogStreamInit } from './durability'