diff --git a/.changeset/sandboxed-chat-tools.md b/.changeset/sandboxed-chat-tools.md new file mode 100644 index 0000000000..c3c32cc5eb --- /dev/null +++ b/.changeset/sandboxed-chat-tools.md @@ -0,0 +1,5 @@ +--- +'@tanstack/ai-mcp-sandbox': minor +--- + +Add `runSandboxed()` and `sandboxedChatTool()`: run an untrusted snippet in an isolate with injected facades and hard limits, for `chat()` tools and server functions. These are the unscoped siblings of the MCP-only `sandboxedTool()` (no OAuth scope gate or manifest). diff --git a/docs/config.json b/docs/config.json index c93c494979..9ea7ac8ff9 100644 --- a/docs/config.json +++ b/docs/config.json @@ -382,6 +382,11 @@ "addedAt": "2026-10-21", "updatedAt": "2026-11-09" }, + { + "label": "Sandboxed Chat Tools", + "to": "tools/sandboxed-tools", + "addedAt": "2026-11-20" + }, { "label": "Tool Result Metadata", "to": "tools/tool-result-metadata", diff --git a/docs/tools/sandboxed-tools.md b/docs/tools/sandboxed-tools.md new file mode 100644 index 0000000000..16b8af6696 --- /dev/null +++ b/docs/tools/sandboxed-tools.md @@ -0,0 +1,166 @@ +--- +title: Sandboxed Chat Tools +id: tools-sandboxed-tools +order: 14 +description: "Run a chat tool or a server endpoint body in an isolate, so untrusted or AI-authored code can only reach the host functions you inject, under limits you set." +keywords: + - tanstack ai + - sandbox + - isolate + - sandboxedChatTool + - runSandboxed + - chat tools + - server function + - code mode +--- + +Wrap a tool's body in an isolate with `sandboxedChatTool()`. The tool reaches the host only through facades you declare, under limits you set. Drop it into `chat({ tools })` like any other server tool. + +Reach for this when the tool body is not code you fully trust: a model wrote it, a user uploaded it, or you want a hard blast-radius wall around a tool that touches money, data, or the network. The isolate has no `fetch`, no `fs`, no `process`. The only way out is a facade you pass in. + +For an MCP server tool, use [`sandboxedTool()`](../mcp/server-sandboxed-tools) instead. It adds a per-caller OAuth scope gate and a signed manifest on top of the same engine. + +## Install + + + +react: @tanstack/ai-mcp-sandbox @tanstack/ai-isolate-quickjs +vue: @tanstack/ai-mcp-sandbox @tanstack/ai-isolate-quickjs +solid: @tanstack/ai-mcp-sandbox @tanstack/ai-isolate-quickjs +svelte: @tanstack/ai-mcp-sandbox @tanstack/ai-isolate-quickjs +vanilla: @tanstack/ai-mcp-sandbox @tanstack/ai-isolate-quickjs + + + +The driver is swappable. `@tanstack/ai-isolate-quickjs` is a WASM VM that runs server-side, including edge runtimes. See [Isolate Drivers](../code-mode/code-mode-isolates) for the others. + +## A sandboxed chat tool + +`sandboxedChatTool()` returns a plain server tool. The `snippet` is the tool body: it reads its input from `input` and calls facades on `host`. + +```ts +import { + chat, + chatParamsFromRequest, + toServerSentEventsResponse, +} from '@tanstack/ai' +import { openaiText } from '@tanstack/ai-openai' +import { sandboxedChatTool } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' +import { z } from 'zod' + +const driver = createQuickJSIsolateDriver() + +// The host-side data. The snippet never sees this, only the facade. +const INVENTORY: Record = { + 'SKU-123': { qty: 42, warehouse: 'PDX' }, +} + +const checkInventory = sandboxedChatTool({ + name: 'checkInventory', + description: 'Check whether a SKU is in stock.', + inputSchema: z.object({ sku: z.string() }), + snippet: ` + const row = await host.db.inventory({ sku: input.sku }) + if (!row) return { sku: input.sku, found: false } + return { sku: input.sku, inStock: row.qty > 0, qty: row.qty } + `, + facades: { + 'db.inventory': { + description: 'Look up one SKU', + execute: async (args: unknown) => { + if (typeof args !== 'object' || args === null || !('sku' in args)) { + throw new Error('sku is required') + } + return INVENTORY[String(args.sku)] ?? null + }, + }, + }, + driver, + limits: { + wallClockMs: 2000, + memoryMb: 64, + maxInjectedCalls: 5, + resultBytes: 16 * 1024, + }, +}) + +export async function POST(request: Request) { + const { messages, threadId } = await chatParamsFromRequest(request) + const stream = chat({ + adapter: openaiText('gpt-6.1-sol'), + messages, + threadId, + tools: [checkInventory], + }) + return toServerSentEventsResponse(stream) +} +``` + +The agent calls `checkInventory`. Its body runs in the isolate and can only call `host.db.inventory`. A body that tried to `fetch`, read a file, or loop forever would be stopped. A blocked run throws, so the agent loop sees a normal tool error. + +## The limits + +Every run is held to four limits. A run that trips one stops: + +- `wallClockMs`: wall-clock budget. Kills an infinite loop. +- `memoryMb`: isolate heap cap. +- `maxInjectedCalls`: how many facade calls one run may make. +- `resultBytes`: max serialized result size. + +Facades are the only egress. Secrets and backend URLs live in the facade closure, in your process, and never enter the isolate. A facade also enforces its own authorization: the isolate guarantees the snippet can reach only that facade, and the facade guarantees the caller may use it this way. + +## A sandboxed endpoint body + +For a server endpoint, call `runSandboxed()` directly. Validate input on the host, run the snippet with injected facades, then return a result. `runSandboxed()` returns a structured result and never throws: + +```ts +import { createServerFn } from '@tanstack/react-start' +import { runSandboxed } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' +import { z } from 'zod' + +const driver = createQuickJSIsolateDriver() + +export const getReport = createServerFn({ method: 'POST' }) + .validator(z.object({ team: z.string() })) + .handler(async ({ data }) => { + const run = await runSandboxed({ + snippet: ` + const m = await host.db.metrics({ team: input.team }) + return { team: m.team, p95: m.p95 } + `, + input: data, + facades: { + 'db.metrics': { + execute: async () => ({ team: data.team, p95: 123 }), + }, + }, + driver, + limits: { + wallClockMs: 3000, + memoryMb: 128, + maxInjectedCalls: 10, + resultBytes: 256 * 1024, + }, + }) + if (!run.ok) throw new Error(run.error) + return { ok: run.ok, ms: run.ms } + }) +``` + +`run.value` holds the snippet's return value, typed `unknown` because the snippet is untrusted. Validate or narrow it with a schema before you forward it to the client. + +Whole-body sandboxing fits a non-streaming data endpoint or server function. The isolate boundary is JSON, so a streaming chat route cannot marshal its stream out of the isolate. For a streaming route, sandbox the tools with `sandboxedChatTool()` and let the route code own the stream. + +## Result shape + +`runSandboxed()` returns: + +- `ok`: `true` when the snippet returned a value. +- `value`: the snippet's return value, when `ok` is `true`. +- `error`: why the run stopped, when `ok` is `false`. A thrown error, a timeout, or a tripped limit. +- `injectedCalls`: how many facade calls the run made. +- `ms`: wall-clock duration. + +`sandboxedChatTool()` wraps that for you: a blocked run throws, and a successful run returns the snippet's value to the agent. diff --git a/examples/react/secure-sandbox-tools/.env.example b/examples/react/secure-sandbox-tools/.env.example new file mode 100644 index 0000000000..60eb9bdc41 --- /dev/null +++ b/examples/react/secure-sandbox-tools/.env.example @@ -0,0 +1,41 @@ +# OpenAI +OPENAI_API_KEY= + +# Anthropic +ANTHROPIC_API_KEY= + +# Google Gemini. The adapter reads GOOGLE_API_KEY, then GEMINI_API_KEY. +GOOGLE_API_KEY= +GEMINI_API_KEY= + +# OpenRouter +OPENROUTER_API_KEY= + +# Groq +GROQ_API_KEY= + +# xAI Grok. The adapter reads XAI_API_KEY. GROK_API_KEY is an alias you can set. +XAI_API_KEY= +GROK_API_KEY= + +# BytePlus ModelArk. The adapter reads ARK_API_KEY, then BYTEPLUS_API_KEY. +ARK_API_KEY= +BYTEPLUS_API_KEY= + +# BytePlus Voice (Seed Speech). This is a different key from ModelArk. +BYTEPLUS_VOICE_API_KEY= + +# Cloudflare Workers AI. Account id: `wrangler whoami`. Token: dashboard API tokens. +CLOUDFLARE_ACCOUNT_ID= +CLOUDFLARE_API_TOKEN= +# Optional. Send chat through this AI Gateway (caching, logs, stored keys). +# CLOUDFLARE_AI_GATEWAY_ID=default + +# Amazon Bedrock (default Converse API) +# Create a key at: https://docs.aws.amazon.com/bedrock/latest/userguide/api-keys.html +BEDROCK_API_KEY= +# AWS_BEARER_TOKEN_BEDROCK= +# AWS_REGION=us-east-1 +# To use SigV4 instead of the API key: set BEDROCK_AUTH=sigv4 and provide AWS +# IAM credentials via env vars or `aws configure`. +# BEDROCK_AUTH=sigv4 diff --git a/examples/react/secure-sandbox-tools/.gitignore b/examples/react/secure-sandbox-tools/.gitignore new file mode 100644 index 0000000000..b6ceee9913 --- /dev/null +++ b/examples/react/secure-sandbox-tools/.gitignore @@ -0,0 +1,7 @@ +node_modules +dist +.env +.env.local +.nitro +.tanstack +.output diff --git a/examples/react/secure-sandbox-tools/DESIGN.md b/examples/react/secure-sandbox-tools/DESIGN.md new file mode 100644 index 0000000000..3ffdafbc6e --- /dev/null +++ b/examples/react/secure-sandbox-tools/DESIGN.md @@ -0,0 +1,181 @@ +# Sandboxing endpoint & tool implementations — design notes + +## The problem (the value) + +A server endpoint or an agent tool is a piece of code that runs with **your +server's full authority**: your database, your secrets, your network, your file +system. That's fine when you wrote the code and trust it. It stops being fine +the moment the body is: + +- **AI-authored** — a model wrote or edited the tool/endpoint body (code mode, + "write me a tool" flows). +- **User-supplied** — a plugin, a saved "custom tool", an uploaded snippet. +- **Trusted but blast-radius-sensitive** — you wrote it, but a bug (an infinite + loop, an accidental `fetch` to the cloud metadata endpoint, a runaway query) + shouldn't be able to take down the box or exfiltrate anything. + +In all three cases you want the same guarantee: **this code can only do the +specific things I handed it, for a bounded amount of time, and nothing else.** +That's what sandboxing buys. The code "can't go off the rails" because the rails +are the only thing it's standing on. + +## The mechanism + +Run the body inside a **QuickJS isolate** (a WASM JavaScript VM, in-process, +server-side only). The isolate has standard JS built-ins but **no ambient +capabilities** — no `fetch`, no `fs`, no `process`, no `require`. The only way +out is through **facades**: host functions you explicitly inject, named +`host..`. Everything is held to hard **limits**: wall-clock, memory, +number of host calls, result size. + +This is the engine from `@tanstack/ai-mcp-sandbox` (PR #2171). That package wraps +it into a signed, OAuth-scoped **MCP** `ServerTool`. Here we reuse the same +primitives for two surfaces #2171 doesn't cover — a `chat()` tool and a TanStack +Start endpoint — by dropping the MCP scope/auth/manifest layer and keeping just +the isolate + facades + limits. + +``` + ┌──────────────────────── your server (full authority) ────────────────────────┐ + request ──► │ validate input (Zod) │ + │ │ │ + │ ▼ │ + │ runSandboxed({ snippet, input, facades, limits }) │ + │ │ │ + │ ▼ │ + │ ┌─── QuickJS isolate (no fetch / fs / process) ───┐ tick++ (budget) │ + │ │ const input = {...} │ ───────────► host.db.* │ + │ │ const host = { db: { metrics: } } │ ◄─────────── (facade) │ + │ │ // top-level await / return │ │ + │ └──────────────┬──────────────────────────────────┘ │ + │ ▼ JSON value │ + │ shapeResult() → { ok, value | error, injectedCalls, ms } │ + └──────────────────────────────────────────────────────────────────────────────┘ +``` + +## The engine — `@tanstack/ai-mcp-sandbox` + +`runSandboxed()` is a first-class export of `@tanstack/ai-mcp-sandbox`. You pass +it a driver, so it stays framework- and runtime-agnostic: + +```ts +import { runSandboxed } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' + +const run = await runSandboxed({ + snippet, + input, + facades, + driver: createQuickJSIsolateDriver(), + limits, +}) +// run: { ok, value? | error?, injectedCalls, ms } — structured, never throws +``` + +Internally it composes the package's own lower-level pieces: `resolveAllowed()` +(verify facades), `makeCallCounter()` (host-call budget), `buildBindings()` +(wrap each facade, ticking the counter, passing a `FacadeContext` where +auth/secrets live), `buildPrelude()` (rebuild the `host..` tree and bind +`input` via `JSON.parse`, so a `__proto__` key stays inert), the driver's +`createContext()`/`execute()`, then `shapeResult()` (truncate, else error over +the size cap). + +`src/lib/sandbox.ts` in this example is now a thin wrapper: it picks the QuickJS +driver and narrows the JSON result so a server function can return it. + +## Surface A — a secure `chat()` tool (`src/lib/secure-chat-tools.ts`) + +The package ships a `sandboxedChatTool()` helper that turns a snippet into a +plain `ServerTool`: + +```ts +import { sandboxedChatTool } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' + +const checkInventory = sandboxedChatTool({ + name: 'checkInventory', + description: 'Check whether a SKU is in stock.', + inputSchema: z.object({ sku: z.string() }), + snippet: INVENTORY_SNIPPET, // can only call host.db.inventory + facades: { 'db.inventory': { execute: async ({ sku }) => db.lookup(sku) } }, + driver: createQuickJSIsolateDriver(), + limits: { + wallClockMs: 2000, + memoryMb: 64, + maxInjectedCalls: 5, + resultBytes: 16_384, + }, +}) +``` + +Because the output is a plain `ServerTool`, it drops straight into +`chat({ tools: [checkInventory] })`. The agent can call the tool; the tool's body +cannot exceed its facade + limits no matter what code sits in it. A blocked run +throws, so the agent loop sees a normal tool error. + +## Surface B — a sandboxed Start endpoint + +Two entry points onto the **same** `runScenario()` (`src/lib/sandbox-scenarios.ts`): + +- **Server function** (`src/lib/sandbox-demo.ts`) — `createServerFn().validator(…).handler(…)`, + called from React as `runSandboxDemo({ data: { scenario } })`. +- **HTTP API route** (`src/routes/api.sandbox-demo.ts`) — a plain + `POST /api/sandbox-demo` you can `curl` or hit from any client. + +```bash +curl -X POST http://localhost:3100/api/sandbox-demo \ + -H 'content-type: application/json' -d '{"scenario":"ok"}' +# → {"ok":true,"value":{"team":"payments","p95":123,"errorRate":0.004},"injectedCalls":1,"ms":13} +``` + +The pattern: **validate input on the host, pass a plain JSON `input` into the +isolate, inject facades, get a JSON result out, build the Response on the host.** + +### The guardrails, demonstrated + +Four scenarios, verified live: + +| Scenario | Snippet | Outcome | Guardrail | +| -------- | ----------------------------------- | ----------------------------------------------- | ----------- | +| `ok` | reads `host.db.metrics` | `ok:true`, metrics, ~13ms | — | +| `loop` | `while (true) {}` | `blocked` — "Code execution timed out", ~2004ms | wall-clock | +| `fetch` | `fetch('http://169.254.169.254/…')` | `blocked` — "'fetch' is not defined" | no network | +| `budget` | 100× `host.db.metrics` | `blocked` — "Injected-call limit reached (5)" | call budget | + +## Why not sandbox the chat endpoint's whole body? + +The QuickJS boundary is **JSON**: you marshal `input` in and a value out. A +streaming chat route (`chat()` → SSE) produces an async stream that can't be +marshaled out of the isolate. So: + +- **Non-streaming data endpoints / server functions** → sandbox the whole body + (Surface B). +- **Streaming chat routes** → sandbox the **tools** (Surface A), and let the + trusted route code own the stream. + +That split is the whole reason both surfaces exist. + +## Threat model — what this does and doesn't cover + +**Covers:** untrusted/buggy _JS logic_ escalating beyond its grant — no ambient +network/fs/process, hard timeout (kills infinite loops), heap cap, host-call +budget, result-size cap, and a facade allowlist that is the sole egress. + +**Does not cover (the host's job):** what a facade _itself_ does. `host.db.metrics` +must enforce its own authorization and value-level policy (which team, which +rows). The isolate guarantees _only this facade is reachable_; the facade +guarantees _this caller may use it this way_. Secrets and backend URLs live in +the facade closure, never in the isolate. + +## Status + +`runSandboxed()` and `sandboxedChatTool()` are now first-class exports of +`@tanstack/ai-mcp-sandbox` (alongside the existing `sandboxedTool()` for MCP). +This example consumes them directly. + +Not promoted into the package: the Start server-function wrapper. It is a +one-liner the app owns (`createServerFn().validator(…).handler(({ data }) => +runSandboxed(…))`), and the package stays free of a `@tanstack/react-start` +dependency. + +Still open for the "full feature" step: an E2E spec and a changeset (this rides +on the open `ai-mcp-sandbox` PR branch). diff --git a/examples/react/secure-sandbox-tools/README.md b/examples/react/secure-sandbox-tools/README.md new file mode 100644 index 0000000000..295246f675 --- /dev/null +++ b/examples/react/secure-sandbox-tools/README.md @@ -0,0 +1,54 @@ +# secure-sandbox-tools + +A playground for running **untrusted endpoint / tool bodies in a QuickJS isolate**, +built on the engine from `@tanstack/ai-mcp-sandbox` (PR #2171) — but applied to +two surfaces that PR doesn't cover: a chat tool and a TanStack Start server +function. + +The idea: the endpoint/tool _implementation_ is a snippet that can touch **only +the host functions ("facades") you inject**, under hard limits. No `fetch`, no +`fs`, no `process`, no surviving a timeout. So a tool "can't go off the rails" +no matter what code (AI-authored, user-uploaded, drifted) sits in the body. + +## The shared engine + +`runSandboxed()` and `sandboxedChatTool()` are first-class exports of +`@tanstack/ai-mcp-sandbox` (the unscoped siblings of its MCP `sandboxedTool()`). +`src/lib/sandbox.ts` is a thin wrapper that picks the QuickJS driver. Limits: +wall-clock ms, memory MB, max injected host-calls, max result bytes. + +## Surface A — secure chat tool (`src/lib/secure-chat-tools.ts`) + +`checkInventory` is a normal `chat()` tool whose body runs in the sandbox. It can +only call `host.db.inventory`; its blast radius is exactly that facade. Wired into +`/api/chat` via `tools: secureTools`. Ask the chat _"is SKU-123 in stock?"_ +(needs a model key — set one with the 🔑 dialog or `.env.local`). + +## Surface B — sandboxed Start server function (`src/lib/sandbox-demo.ts`) + +`runSandboxDemo` is a `createServerFn` whose body is sandboxed. The four buttons +on the index route each run a snippet with only `host.db.metrics`: + +| Button | What the snippet does | Outcome | +| --------------------- | ----------------------- | ----------------------------------- | +| Run (allowed) | reads `host.db.metrics` | returns metrics | +| Infinite loop | `while (true) {}` | blocked — wall-clock timeout (~2s) | +| Reach 169.254.169.254 | `fetch(metadata-url)` | blocked — no `fetch` in the isolate | +| Exceed call budget | 100× `host.db.metrics` | blocked — injected-call limit (5) | + +No API key needed for Surface B — it's pure isolate execution. + +> **Streaming caveat.** Whole-body endpoint sandboxing fits _non-streaming_ data +> endpoints and server functions. A streaming chat route (SSE) can't marshal its +> stream out of the isolate, so there you sandbox the **tools** (Surface A), not +> the whole handler. + +## Run + +```bash +pnpm install +pnpm --filter secure-sandbox-tools dev # http://localhost:3100 +``` + +QuickJS is a WASM VM that runs **server-side only** — the sandbox code is reached +only through the server function handler and the `/api/chat` route. diff --git a/examples/react/secure-sandbox-tools/package.json b/examples/react/secure-sandbox-tools/package.json new file mode 100644 index 0000000000..7168fac671 --- /dev/null +++ b/examples/react/secure-sandbox-tools/package.json @@ -0,0 +1,86 @@ +{ + "name": "secure-sandbox-tools", + "private": true, + "type": "module", + "scripts": { + "dev": "vite dev --port 3100", + "build": "vite build", + "test:types": "tsc" + }, + "dependencies": { + "@tailwindcss/vite": "^4.1.18", + "@tanstack/ai": "workspace:*", + "@tanstack/ai-acp": "workspace:*", + "@tanstack/ai-anthropic": "workspace:*", + "@tanstack/ai-bedrock": "workspace:*", + "@tanstack/ai-byteplus": "workspace:*", + "@tanstack/ai-claude-code": "workspace:*", + "@tanstack/ai-client": "workspace:*", + "@tanstack/ai-cloudflare": "workspace:*", + "@tanstack/ai-code-mode": "workspace:*", + "@tanstack/ai-code-mode-snippets": "workspace:*", + "@tanstack/ai-codex": "workspace:*", + "@tanstack/ai-cohere": "workspace:*", + "@tanstack/ai-compaction": "workspace:*", + "@tanstack/ai-devtools-core": "workspace:*", + "@tanstack/ai-durable-stream": "workspace:*", + "@tanstack/ai-elevenlabs": "workspace:*", + "@tanstack/ai-event-client": "workspace:*", + "@tanstack/ai-fal": "workspace:*", + "@tanstack/ai-gemini": "workspace:*", + "@tanstack/ai-grok": "workspace:*", + "@tanstack/ai-grok-build": "workspace:*", + "@tanstack/ai-groq": "workspace:*", + "@tanstack/ai-isolate-cloudflare": "workspace:*", + "@tanstack/ai-isolate-daytona": "workspace:*", + "@tanstack/ai-isolate-node": "workspace:*", + "@tanstack/ai-isolate-quickjs": "workspace:*", + "@tanstack/ai-isolate-quickjs-bun": "workspace:*", + "@tanstack/ai-llmgateway": "workspace:*", + "@tanstack/ai-lovable": "workspace:*", + "@tanstack/ai-mcp": "workspace:*", + "@tanstack/ai-mcp-sandbox": "workspace:*", + "@tanstack/ai-memory": "workspace:*", + "@tanstack/ai-mistral": "workspace:*", + "@tanstack/ai-neon": "workspace:*", + "@tanstack/ai-ollama": "workspace:*", + "@tanstack/ai-openai": "workspace:*", + "@tanstack/ai-opencode": "workspace:*", + "@tanstack/ai-openrouter": "workspace:*", + "@tanstack/ai-perplexity": "workspace:*", + "@tanstack/ai-persistence": "workspace:*", + "@tanstack/ai-react": "workspace:*", + "@tanstack/ai-reactor": "workspace:*", + "@tanstack/ai-sandbox": "workspace:*", + "@tanstack/ai-sandbox-blaxel": "workspace:*", + "@tanstack/ai-sandbox-cloudflare": "workspace:*", + "@tanstack/ai-sandbox-daytona": "workspace:*", + "@tanstack/ai-sandbox-docker": "workspace:*", + "@tanstack/ai-sandbox-local-process": "workspace:*", + "@tanstack/ai-sandbox-sprites": "workspace:*", + "@tanstack/ai-sandbox-upstash-box": "workspace:*", + "@tanstack/ai-sandbox-vercel": "workspace:*", + "@tanstack/ai-skills": "workspace:*", + "@tanstack/ai-utils": "workspace:*", + "@tanstack/ai-vercel-gateway": "workspace:*", + "@tanstack/ai-vertex": "workspace:*", + "@tanstack/openai-base": "workspace:*", + "@tanstack/react-ai-devtools": "workspace:*", + "@tanstack/react-router": "^1.170.41", + "@tanstack/react-start": "^1.168.60", + "@tanstack/router-plugin": "^1.168.42", + "lucide-react": "^0.561.0", + "react": "^19.2.3", + "react-dom": "^19.2.3", + "tailwindcss": "^4.1.18", + "zod": "^4.2.0" + }, + "devDependencies": { + "@types/node": "^24.10.1", + "@types/react": "^19.2.7", + "@types/react-dom": "^19.2.3", + "@vitejs/plugin-react": "^6.3.0", + "typescript": "5.9.3", + "vite": "^8.3.8" + } +} diff --git a/examples/react/secure-sandbox-tools/src/components/ByokKeyDialog.tsx b/examples/react/secure-sandbox-tools/src/components/ByokKeyDialog.tsx new file mode 100644 index 0000000000..a65d5c3b89 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/components/ByokKeyDialog.tsx @@ -0,0 +1,501 @@ +import { useState } from 'react' +import { KeyRound, X } from 'lucide-react' +import { useByok } from '@tanstack/ai-react' +import { byok, KEYED_PROVIDERS, KEY_GROUPS } from '@/lib/byok' +import type { KeyStatus } from '@tanstack/ai-client/byok' +import type { ProviderId } from '@tanstack/ai/byok' +import type { ReactNode } from 'react' + +export type ByokKeyDialogProps = { + open: boolean + onOpenChange: (open: boolean) => void + envStatus?: Record + activeProvider?: ProviderId | null + highlightProvider?: ProviderId | null + openRouter?: { + onLogin: () => void + completing?: boolean + error?: string | null + } + trigger?: ReactNode + description?: string +} + +const STATUS_LABEL: Record< + KeyStatus['state'], + { label: string; className: string } +> = { + empty: { label: 'Not set', className: 'text-gray-500' }, + set: { label: 'Saved', className: 'text-gray-400' }, + locked: { label: 'Locked', className: 'text-amber-400' }, + error: { label: 'Check failed', className: 'text-red-400' }, +} + +export function ByokKeyDialog({ + open, + onOpenChange, + envStatus, + activeProvider = null, + highlightProvider = null, + openRouter, + trigger, + description = 'Keys stay in your browser and are sent per-request in a header — never stored on the server. Providers with a server key already work without one.', +}: ByokKeyDialogProps) { + const snapshot = useByok(byok) + const [unlocking, setUnlocking] = useState(false) + const [actionError, setActionError] = useState('') + + const activeStatus = activeProvider + ? snapshot.status[activeProvider] + : undefined + const activeNeedsKey = + activeProvider != null && + !envStatus?.[activeProvider] && + (!activeStatus || activeStatus.state === 'empty') + + return ( + <> + {trigger ?? ( + + )} + + {open ? ( +
onOpenChange(false)} + > +
+
event.stopPropagation()} + > +
+

API keys

+ +
+

+ {description} +

+ + {snapshot.locked ? ( +
+ + Your saved keys are locked ({byok.storage.label}). + + +
+ ) : null} + + {byok.storage.warning ? ( +

+ {byok.storage.warning} +

+ ) : null} + {snapshot.storageError ? ( +

+ {snapshot.storageError} +

+ ) : null} + {actionError ? ( +

{actionError}

+ ) : null} + {openRouter?.completing ? ( +

+ Completing OpenRouter sign-in… +

+ ) : null} + {openRouter?.error ? ( +

{openRouter.error}

+ ) : null} + +
+ {KEYED_PROVIDERS.map((provider) => { + const group = KEY_GROUPS.find((g) => + g.fields.some((f) => f.provider.id === provider.id), + ) + if (group) { + // Render the group once, where its first field sits. + if (group.fields[0].provider.id !== provider.id) return null + return ( + f.provider.id === highlightProvider, + )} + /> + ) + } + return ( + + ) + })} +
+
+
+
+ ) : null} + + ) +} + +function ProviderRow({ + id, + label, + status, + locked, + hasEnvKey, + highlight, + openRouter, +}: { + id: ProviderId + label: string + status: KeyStatus | undefined + locked: boolean + hasEnvKey: boolean + highlight: boolean + openRouter?: ByokKeyDialogProps['openRouter'] +}) { + const [draft, setDraft] = useState('') + const [rowError, setRowError] = useState('') + const state = status?.state ?? 'empty' + const masked = status && 'masked' in status ? status.masked : undefined + const isLocked = locked || state === 'locked' + const hasKey = state !== 'empty' + + return ( +
+
+ {label} + +
+ + {hasKey && masked ? ( +
+ + {masked} + +
+ +
+
+ ) : null} + + {id === 'openrouter' && openRouter && !hasKey ? ( + + ) : null} + + {state === 'error' && status && 'message' in status ? ( +

{status.message}

+ ) : null} + {rowError ?

{rowError}

: null} + +
{ + event.preventDefault() + const next = draft.trim() + if (!next || isLocked) return + setRowError('') + void byok + .update(id, next) + .then(() => setDraft('')) + .catch((error: unknown) => + setRowError( + error instanceof Error ? error.message : 'Could not save key', + ), + ) + }} + > + setDraft(event.target.value)} + className="flex-1 rounded-md border border-gray-600 bg-gray-950 px-2 py-1.5 text-sm text-white placeholder-gray-500 disabled:opacity-50" + /> + +
+
+ ) +} + +/** + * One card for a credential made of several stored values (for example + * Cloudflare's account id + API token). Each field saves to its own id. + */ +function GroupRow({ + label, + fields, + statuses, + locked, + envStatus, + highlight, +}: { + label: string + fields: ReadonlyArray<{ + provider: { id: ProviderId; label: string } + label: string + secret: boolean + }> + statuses: Record + locked: boolean + envStatus?: Record + highlight: boolean +}) { + const [drafts, setDrafts] = useState>({}) + const [rowError, setRowError] = useState('') + const states = fields.map((f) => statuses[f.provider.id]?.state ?? 'empty') + const allSet = states.every((s) => s !== 'empty') + const anyLocked = locked || states.some((s) => s === 'locked') + const allEnv = fields.every((f) => Boolean(envStatus?.[f.provider.id])) + const state: KeyStatus['state'] = anyLocked + ? 'locked' + : states.includes('error') + ? 'error' + : allSet + ? 'set' + : 'empty' + const filled = fields.filter((f) => drafts[f.provider.id]?.trim()) + + return ( +
+
+ {label} + +
+ + {rowError ?

{rowError}

: null} + +
{ + event.preventDefault() + if (filled.length === 0 || anyLocked) return + setRowError('') + // One at a time: passkey-backed storage runs a WebAuthn ceremony + // per write, and the browser rejects a second concurrent request. + void (async () => { + for (const f of filled) { + await byok.update(f.provider.id, drafts[f.provider.id]!.trim()) + } + })() + .then(() => setDrafts({})) + .catch((error: unknown) => + setRowError( + error instanceof Error ? error.message : 'Could not save keys', + ), + ) + }} + > + {fields.map((field) => { + const status = statuses[field.provider.id] + const masked = + status && 'masked' in status ? status.masked : undefined + const hasKey = (status?.state ?? 'empty') !== 'empty' + return ( + + ) + })} +
+ {allSet ? ( + + ) : null} + +
+
+
+ ) +} + +function PresenceBadge({ + state, + hasKey, + isLocked, + hasEnvKey, +}: { + state: KeyStatus['state'] + hasKey: boolean + isLocked: boolean + hasEnvKey: boolean +}) { + if (hasKey && !isLocked) { + return ( + Your key + ) + } + if (isLocked) { + return Locked + } + if (hasEnvKey) { + return ( + Server key + ) + } + const { label, className } = STATUS_LABEL[state] + return {label} +} diff --git a/examples/react/secure-sandbox-tools/src/lib/byok.ts b/examples/react/secure-sandbox-tools/src/lib/byok.ts new file mode 100644 index 0000000000..418931bc8c --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/byok.ts @@ -0,0 +1,73 @@ +import { anthropicByok } from '@tanstack/ai-anthropic/byok' +import { defineByok, defaultByokStorage } from '@tanstack/ai-client/byok' +import { byteplusByok, byteplusVoiceByok } from '@tanstack/ai-byteplus/byok' +import { + cloudflareAccountByok, + cloudflareByok, +} from '@tanstack/ai-cloudflare/byok' +import { geminiByok } from '@tanstack/ai-gemini/byok' +import { grokByok } from '@tanstack/ai-grok/byok' +import { groqByok } from '@tanstack/ai-groq/byok' +import { openaiByok } from '@tanstack/ai-openai/byok' +import { openrouterByok } from '@tanstack/ai-openrouter/byok' +import { createServerFn } from '@tanstack/react-start' +import type { ProviderId } from '@tanstack/ai/byok' +import type { Provider } from '@/lib/model-selection' + +export const KEYED_PROVIDERS = [ + openaiByok, + anthropicByok, + geminiByok, + openrouterByok, + groqByok, + grokByok, + byteplusByok, + byteplusVoiceByok, + cloudflareByok, + cloudflareAccountByok, +] as const + +/** + * Providers whose credential is more than one value. The store still keeps + * one entry per id (so env fallback and relay headers work per value), but + * the key dialog shows each group as a single card. + */ +export const KEY_GROUPS = [ + { + id: 'cloudflare', + label: 'Cloudflare', + fields: [ + { provider: cloudflareAccountByok, label: 'Account ID', secret: false }, + { provider: cloudflareByok, label: 'API token', secret: true }, + ], + }, +] as const + +export const byok = defineByok({ + storage: defaultByokStorage(), + providers: KEYED_PROVIDERS, +}) + +// Let the relay decide when a key is missing. The server prefers the +// `x-byok-*` header, then env. Without coverage, the client would block +// the send before env fallback can run. +byok.setServerCoverage(true) + +export function toByokProvider(provider: Provider): ProviderId | undefined { + if (provider === 'gemini-interactions') return geminiByok.id + const match = KEYED_PROVIDERS.find((entry) => entry.id === provider) + return match?.id +} + +/** Booleans only — which keyed providers have an env key on the relay. */ +export const getEnvKeyStatus = createServerFn({ method: 'GET' }).handler( + (): Record => { + const status: Record = {} + for (const provider of KEYED_PROVIDERS) { + status[provider.id] = (provider.env ?? []).some((name) => + Boolean(process.env[name]), + ) + } + return status + }, +) diff --git a/examples/react/secure-sandbox-tools/src/lib/model-selection.ts b/examples/react/secure-sandbox-tools/src/lib/model-selection.ts new file mode 100644 index 0000000000..aadb2c2e0e --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/model-selection.ts @@ -0,0 +1,331 @@ +export type Provider = + | 'openai' + | 'anthropic' + | 'gemini' + | 'gemini-interactions' + | 'ollama' + | 'grok' + | 'groq' + | 'openrouter' + | 'bedrock' + | 'byteplus' + | 'cloudflare' + +export interface ModelOption { + provider: Provider + model: string + label: string +} + +export const MODEL_OPTIONS: Array = [ + // OpenAI + { provider: 'openai', model: 'gpt-6.1-sol', label: 'OpenAI - GPT-6.1 Sol' }, + { provider: 'openai', model: 'gpt-5.6-sol', label: 'OpenAI - GPT-5.6 Sol' }, + { + provider: 'openai', + model: 'gpt-5.6-terra', + label: 'OpenAI - GPT-5.6 Terra', + }, + { provider: 'openai', model: 'gpt-5.6-luna', label: 'OpenAI - GPT-5.6 Luna' }, + { provider: 'openai', model: 'gpt-5.5', label: 'OpenAI - GPT-5.5' }, + { provider: 'openai', model: 'gpt-5.2', label: 'OpenAI - GPT-5.2' }, + { provider: 'openai', model: 'gpt-5.2-pro', label: 'OpenAI - GPT-5.2 Pro' }, + { provider: 'openai', model: 'gpt-5.1', label: 'OpenAI - GPT-5.1' }, + { provider: 'openai', model: 'gpt-5', label: 'OpenAI - GPT-5' }, + { provider: 'openai', model: 'gpt-5-mini', label: 'OpenAI - GPT-5 Mini' }, + { provider: 'openai', model: 'gpt-5-nano', label: 'OpenAI - GPT-5 Nano' }, + { provider: 'openai', model: 'gpt-4.1', label: 'OpenAI - GPT-4.1' }, + { provider: 'openai', model: 'gpt-4o', label: 'OpenAI - GPT-4o' }, + { provider: 'openai', model: 'gpt-4o-mini', label: 'OpenAI - GPT-4o Mini' }, + + // Cloudflare Workers AI. `provider/model` ids route through AI Gateway. + { + provider: 'cloudflare', + model: '@cf/deepseek-ai/deepseek-v4.1-flash-1017', + label: 'Cloudflare - DeepSeek V4.1 Flash', + }, + { + provider: 'cloudflare', + model: '@cf/meta/llama-3.3-70b-instruct-fp8-fast', + label: 'Cloudflare - Llama 3.3 70B', + }, + { + provider: 'cloudflare', + model: 'openai/gpt-5.5', + label: 'Cloudflare Gateway - GPT-5.5', + }, + + // Anthropic + { + provider: 'anthropic', + model: 'claude-opus-4-7', + label: 'Anthropic - Claude Opus 4.7', + }, + { + provider: 'anthropic', + model: 'claude-opus-4-6', + label: 'Anthropic - Claude Opus 4.6', + }, + { + provider: 'anthropic', + model: 'claude-sonnet-4-6', + label: 'Anthropic - Claude Sonnet 4.6', + }, + { + provider: 'anthropic', + model: 'claude-sonnet-4-5', + label: 'Anthropic - Claude Sonnet 4.5', + }, + { + provider: 'anthropic', + model: 'claude-haiku-4-5', + label: 'Anthropic - Claude Haiku 4.5', + }, + + // Gemini (stateless `geminiText`) + { + provider: 'gemini', + model: 'gemini-3.8-flash', + label: 'Gemini - 3.8 Flash', + }, + { + provider: 'gemini', + model: 'gemini-3.7-flash', + label: 'Gemini - 3.7 Flash', + }, + { + provider: 'gemini', + model: 'gemini-3.6-flash', + label: 'Gemini - 3.6 Flash', + }, + { + provider: 'gemini', + model: 'gemini-3.5-flash', + label: 'Gemini - 3.5 Flash', + }, + { + provider: 'gemini', + model: 'gemini-3.5-flash-lite', + label: 'Gemini - 3.5 Flash Lite', + }, + { + provider: 'gemini', + model: 'gemini-3.1-pro-preview', + label: 'Gemini - 3.1 Pro Preview', + }, + { + provider: 'gemini', + model: 'gemini-3.1-flash-lite-preview', + label: 'Gemini - 3.1 Flash Lite Preview', + }, + { + provider: 'gemini', + model: 'gemini-2.5-pro', + label: 'Gemini - 2.5 Pro', + }, + { + provider: 'gemini', + model: 'gemini-2.5-flash', + label: 'Gemini - 2.5 Flash', + }, + + // Gemini Interactions (stateful, experimental — `@tanstack/ai-gemini/experimental`) + { + provider: 'gemini-interactions', + model: 'gemini-3.8-flash', + label: 'Gemini Interactions - 3.8 Flash (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.7-flash', + label: 'Gemini Interactions - 3.7 Flash (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.6-flash', + label: 'Gemini Interactions - 3.6 Flash (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.5-flash', + label: 'Gemini Interactions - 3.5 Flash (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.5-flash-lite', + label: 'Gemini Interactions - 3.5 Flash Lite (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.1-pro-preview', + label: 'Gemini Interactions - 3.1 Pro Preview (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3-flash-preview', + label: 'Gemini Interactions - 3 Flash Preview (experimental)', + }, + { + provider: 'gemini-interactions', + model: 'gemini-3.1-flash-lite-preview', + label: 'Gemini Interactions - 3.1 Flash Lite Preview (experimental)', + }, + + // Openrouter — multi-provider via OpenRouter's unified API + { + provider: 'openrouter', + model: 'openai/gpt-5.2', + label: 'OpenRouter - OpenAI GPT-5.2', + }, + { + provider: 'openrouter', + model: 'openai/gpt-5.1', + label: 'OpenRouter - OpenAI GPT-5.1', + }, + { + provider: 'openrouter', + model: 'openai/gpt-5', + label: 'OpenRouter - OpenAI GPT-5', + }, + { + provider: 'openrouter', + model: 'openai/gpt-4o', + label: 'OpenRouter - OpenAI GPT-4o', + }, + { + provider: 'openrouter', + model: 'anthropic/claude-opus-4.7', + label: 'OpenRouter - Anthropic Claude Opus 4.7', + }, + { + provider: 'openrouter', + model: 'anthropic/claude-sonnet-4.6', + label: 'OpenRouter - Anthropic Claude Sonnet 4.6', + }, + { + provider: 'openrouter', + model: 'anthropic/claude-haiku-4.5', + label: 'OpenRouter - Anthropic Claude Haiku 4.5', + }, + { + provider: 'openrouter', + model: 'google/gemini-2.5-pro', + label: 'OpenRouter - Google Gemini 2.5 Pro', + }, + { + provider: 'openrouter', + model: 'x-ai/grok-4', + label: 'OpenRouter - xAI Grok 4', + }, + { + provider: 'openrouter', + model: 'meta-llama/llama-3.3-70b-instruct', + label: 'OpenRouter - Meta Llama 3.3 70B (Groq-routed)', + }, + + // Ollama + { + provider: 'ollama', + model: 'gpt-oss:20b', + label: 'Ollama - GPT-OSS 20B', + }, + { + provider: 'ollama', + model: 'granite4:3b', + label: 'Ollama - Granite4 3B', + }, + { + provider: 'ollama', + model: 'mistral', + label: 'Ollama - Mistral', + }, + + // Groq + { + provider: 'groq', + model: 'openai/gpt-oss-120b', + label: 'Groq - GPT-OSS 120B', + }, + { + provider: 'groq', + model: 'qwen/qwen3.8-27b', + label: 'Groq - Qwen3.8 27B', + }, + + // Grok + { + provider: 'grok', + model: 'grok-build-0.1', + label: 'Grok - Grok Build 0.1', + }, + { + provider: 'grok', + model: 'grok-4.3', + label: 'Grok - Grok 4.3', + }, + + // Bedrock (default Converse API — reaches Claude, Nova, Llama, gpt-oss, …) + { + provider: 'bedrock', + model: 'us.anthropic.claude-haiku-4-5-20251001-v1:0', + label: 'Bedrock - Claude Haiku 4.5 (Converse)', + }, + { + provider: 'bedrock', + model: 'us.amazon.nova-pro-v1:0', + label: 'Bedrock - Nova Pro (Converse)', + }, + { + provider: 'bedrock', + model: 'openai.gpt-oss-120b-1:0', + label: 'Bedrock - GPT-OSS 120B (Converse)', + }, + + // BytePlus ModelArk (ARK_API_KEY) — Seed models plus third-party models + // (GLM, DeepSeek, gpt-oss) served from the same Ark endpoint. + { + provider: 'byteplus', + model: 'dola-seed-2-1-turbo-260628', + label: 'BytePlus - Seed 2.1 Turbo', + }, + { + provider: 'byteplus', + model: 'seed-2-0-pro-260328', + label: 'BytePlus - Seed 2.0 Pro', + }, + { + provider: 'byteplus', + model: 'seed-2-0-lite-260428', + label: 'BytePlus - Seed 2.0 Lite', + }, + { + provider: 'byteplus', + model: 'seed-2-0-mini-260428', + label: 'BytePlus - Seed 2.0 Mini', + }, + { + provider: 'byteplus', + model: 'seed-2-0-code-preview-260328', + label: 'BytePlus - Seed 2.0 Code Preview', + }, + { + provider: 'byteplus', + model: 'seed-1-6-flash-250715', + label: 'BytePlus - Seed 1.6 Flash', + }, + { + provider: 'byteplus', + model: 'glm-5-2-260617', + label: 'BytePlus - GLM 5.2', + }, + { + provider: 'byteplus', + model: 'deepseek-v4-pro-260425', + label: 'BytePlus - DeepSeek V4 Pro', + }, + // gpt-oss-120b-250805 is deliberately absent: this route always merges the + // server tool set into the request, and that model's tool support is + // undeclared in model-meta and unverified against the live API. +] + +export const DEFAULT_MODEL_OPTION = MODEL_OPTIONS[0] diff --git a/examples/react/secure-sandbox-tools/src/lib/sandbox-demo.ts b/examples/react/secure-sandbox-tools/src/lib/sandbox-demo.ts new file mode 100644 index 0000000000..bca3d6609d --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/sandbox-demo.ts @@ -0,0 +1,17 @@ +// Surface B (server-function entry) — a TanStack Start server function whose +// body is sandboxed. See src/lib/sandbox-scenarios.ts for the shared snippets, +// and src/routes/api.sandbox-demo.ts for the plain HTTP-API version of the same +// thing. This is the non-streaming data-endpoint shape; a streaming chat route +// sandboxes its *tools* instead (see secure-chat-tools.ts). +import { createServerFn } from '@tanstack/react-start' +import { z } from 'zod' +import { SCENARIO_IDS, runScenario } from './sandbox-scenarios' + +export const runSandboxDemo = createServerFn({ method: 'POST' }) + .validator( + z.object({ + scenario: z.enum(SCENARIO_IDS), + team: z.string().default('payments'), + }), + ) + .handler(async ({ data }) => runScenario(data.scenario, data.team)) diff --git a/examples/react/secure-sandbox-tools/src/lib/sandbox-scenarios.ts b/examples/react/secure-sandbox-tools/src/lib/sandbox-scenarios.ts new file mode 100644 index 0000000000..f2cc7b6598 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/sandbox-scenarios.ts @@ -0,0 +1,55 @@ +// The demo scenario bodies, shared by BOTH entry points: +// - the server function (src/lib/sandbox-demo.ts) +// - the HTTP API route (src/routes/api.sandbox-demo.ts) +// +// One snippet does real work; the other three each try to "go off the rails" +// and get stopped by a different guardrail. +import { DEFAULT_LIMITS, runSandboxed } from './sandbox' +import type { SandboxRun } from './sandbox' +import type { Facades } from '@tanstack/ai-mcp-sandbox' + +export const SCENARIO_IDS = ['ok', 'loop', 'fetch', 'budget'] as const +export type Scenario = (typeof SCENARIO_IDS)[number] + +// The only thing any snippet can touch. Blast radius = exactly this facade. +const facades: Facades = { + 'db.metrics': { + description: 'Read canned team metrics', + execute: async (args) => { + const team = (args as { team?: unknown }).team + return { team, p95: 123, errorRate: 0.004, requests: 98213 } + }, + }, +} + +// Each value is a raw endpoint body; `input` holds the validated args. +const SNIPPETS: Record = { + // Real work: read the allowlisted backend and shape a response. + ok: ` + const m = await host.db.metrics({ team: input.team }) + return { team: m.team, p95: m.p95, errorRate: m.errorRate } + `, + // Runaway compute → interrupted at the wall-clock deadline (2s). + loop: `while (true) {}`, + // Tries the cloud metadata endpoint → there is no fetch in the isolate, so + // the reference throws. The snippet has no network at all. + fetch: `return await fetch('http://169.254.169.254/latest/meta-data/')`, + // Hammers the host facade → stopped at the injected-call budget (5). + budget: ` + for (let i = 0; i < 100; i++) await host.db.metrics({ team: input.team }) + return 'unreachable' + `, +} + +/** Run one scenario in the isolate. Returns a structured outcome, never throws. */ +export function runScenario( + scenario: Scenario, + team: string, +): Promise { + return runSandboxed({ + snippet: SNIPPETS[scenario], + input: { team }, + facades, + limits: DEFAULT_LIMITS, + }) +} diff --git a/examples/react/secure-sandbox-tools/src/lib/sandbox.ts b/examples/react/secure-sandbox-tools/src/lib/sandbox.ts new file mode 100644 index 0000000000..c99bec4e49 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/sandbox.ts @@ -0,0 +1,50 @@ +// Thin app-side wrapper over @tanstack/ai-mcp-sandbox's runSandboxed: it picks +// the QuickJS driver and narrows the result to JSON so a server function can +// return it. The isolate engine itself now lives in the package. +import { runSandboxed as runSandboxedCore } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' +import type { Facades, Limits } from '@tanstack/ai-mcp-sandbox' + +// One driver per process. QuickJS is a WASM VM that only runs server-side. +// ponytail: lazy singleton so importing this module on the client (the +// server-fn compiler tree-shakes it out) never boots the VM. +let driverSingleton: ReturnType | undefined +const driver = () => (driverSingleton ??= createQuickJSIsolateDriver()) + +/** What crosses the isolate boundary is always JSON — type it so a server + * function accepts `SandboxRun` as a serializable return. */ +export type JsonValue = + | string + | number + | boolean + | null + | { [key: string]: JsonValue } + | Array + +export interface SandboxRun { + ok: boolean + value?: JsonValue + error?: string + injectedCalls: number + ms: number +} + +/** Sensible defaults for a short, read-only tool/endpoint body. */ +export const DEFAULT_LIMITS: Limits = { + wallClockMs: 2000, + memoryMb: 64, + maxInjectedCalls: 5, + resultBytes: 16 * 1024, +} + +export async function runSandboxed(opts: { + snippet: string + input: unknown + facades: Facades + limits: Limits + truncatable?: ReadonlyArray +}): Promise { + const run = await runSandboxedCore({ ...opts, driver: driver() }) + // The isolate round-trips through JSON, so value is a JsonValue by construction. + return { ...run, value: run.value as JsonValue | undefined } +} diff --git a/examples/react/secure-sandbox-tools/src/lib/secure-chat-tools.ts b/examples/react/secure-sandbox-tools/src/lib/secure-chat-tools.ts new file mode 100644 index 0000000000..e9ef8ac2aa --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/lib/secure-chat-tools.ts @@ -0,0 +1,57 @@ +// Surface A — a chat tool whose IMPLEMENTATION runs in the sandbox, built with +// the promoted `sandboxedChatTool` helper from @tanstack/ai-mcp-sandbox. +// +// The model calls `checkInventory`; its body can only reach `host.db.inventory`. +// If the body tried to `fetch`, read a file, or loop forever, the isolate would +// stop it — the tool can't "go off the rails" no matter what code sits in it. +import { sandboxedChatTool } from '@tanstack/ai-mcp-sandbox' +import { createQuickJSIsolateDriver } from '@tanstack/ai-isolate-quickjs' +import { z } from 'zod' +import { DEFAULT_LIMITS } from './sandbox' +import type { Facades } from '@tanstack/ai-mcp-sandbox' + +// The canned "database" — the ONLY thing the snippet can touch. Blast radius is +// exactly this facade; swap for a real query in production (the query/secret +// stays in this closure, never in the isolate). +const INVENTORY: Record = { + 'SKU-123': { qty: 42, warehouse: 'PDX' }, + 'SKU-777': { qty: 0, warehouse: 'LAX' }, +} + +const inventoryFacades: Facades = { + 'db.inventory': { + description: 'Look up one SKU in the warehouse table', + execute: async (args) => { + const sku = (args as { sku?: unknown }).sku + return typeof sku === 'string' ? (INVENTORY[sku] ?? null) : null + }, + }, +} + +const INVENTORY_SNIPPET = ` + const row = await host.db.inventory({ sku: input.sku }) + if (!row) return { sku: input.sku, found: false } + return { + sku: input.sku, + found: true, + inStock: row.qty > 0, + qty: row.qty, + warehouse: row.warehouse, + } +` + +export const secureTools = [ + sandboxedChatTool({ + name: 'checkInventory', + description: + 'Check whether a SKU is in stock and how many units are available. ' + + 'Known SKUs: SKU-123, SKU-777.', + inputSchema: z.object({ + sku: z.string().describe('The SKU to look up, e.g. "SKU-123"'), + }), + snippet: INVENTORY_SNIPPET, + facades: inventoryFacades, + driver: createQuickJSIsolateDriver(), + limits: DEFAULT_LIMITS, + }), +] diff --git a/examples/react/secure-sandbox-tools/src/routeTree.gen.ts b/examples/react/secure-sandbox-tools/src/routeTree.gen.ts new file mode 100644 index 0000000000..2e622c9cfe --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/routeTree.gen.ts @@ -0,0 +1,104 @@ +/* eslint-disable */ + +// @ts-nocheck + +// noinspection JSUnusedGlobalSymbols + +// This file was automatically generated by TanStack Router. +// You should NOT make any changes in this file as it will be overwritten. +// Additionally, you should also exclude this file from your linter and/or formatter to prevent it from being checked or modified. + +import { Route as rootRouteImport } from './routes/__root' +import { Route as IndexRouteImport } from './routes/index' +import { Route as ApiChatRouteImport } from './routes/api.chat' +import { Route as ApiSandboxDemoRouteImport } from './routes/api.sandbox-demo' + +const IndexRoute = IndexRouteImport.update({ + id: '/', + path: '/', + getParentRoute: () => rootRouteImport, +} as any) +const ApiChatRoute = ApiChatRouteImport.update({ + id: '/api/chat', + path: '/api/chat', + getParentRoute: () => rootRouteImport, +} as any) +const ApiSandboxDemoRoute = ApiSandboxDemoRouteImport.update({ + id: '/api/sandbox-demo', + path: '/api/sandbox-demo', + getParentRoute: () => rootRouteImport, +} as any) + +export interface FileRoutesByFullPath { + '/': typeof IndexRoute + '/api/chat': typeof ApiChatRoute + '/api/sandbox-demo': typeof ApiSandboxDemoRoute +} +export interface FileRoutesByTo { + '/': typeof IndexRoute + '/api/chat': typeof ApiChatRoute + '/api/sandbox-demo': typeof ApiSandboxDemoRoute +} +export interface FileRoutesById { + __root__: typeof rootRouteImport + '/': typeof IndexRoute + '/api/chat': typeof ApiChatRoute + '/api/sandbox-demo': typeof ApiSandboxDemoRoute +} +export interface FileRouteTypes { + fileRoutesByFullPath: FileRoutesByFullPath + fullPaths: '/' | '/api/chat' | '/api/sandbox-demo' + fileRoutesByTo: FileRoutesByTo + to: '/' | '/api/chat' | '/api/sandbox-demo' + id: '__root__' | '/' | '/api/chat' | '/api/sandbox-demo' + fileRoutesById: FileRoutesById +} +export interface RootRouteChildren { + IndexRoute: typeof IndexRoute + ApiChatRoute: typeof ApiChatRoute + ApiSandboxDemoRoute: typeof ApiSandboxDemoRoute +} + +declare module '@tanstack/react-router' { + interface FileRoutesByPath { + '/': { + id: '/' + path: '/' + fullPath: '/' + preLoaderRoute: typeof IndexRouteImport + parentRoute: typeof rootRouteImport + } + '/api/chat': { + id: '/api/chat' + path: '/api/chat' + fullPath: '/api/chat' + preLoaderRoute: typeof ApiChatRouteImport + parentRoute: typeof rootRouteImport + } + '/api/sandbox-demo': { + id: '/api/sandbox-demo' + path: '/api/sandbox-demo' + fullPath: '/api/sandbox-demo' + preLoaderRoute: typeof ApiSandboxDemoRouteImport + parentRoute: typeof rootRouteImport + } + } +} + +const rootRouteChildren: RootRouteChildren = { + IndexRoute: IndexRoute, + ApiChatRoute: ApiChatRoute, + ApiSandboxDemoRoute: ApiSandboxDemoRoute, +} +export const routeTree = rootRouteImport + ._addFileChildren(rootRouteChildren) + ._addFileTypes() + +import type { getRouter } from './router.tsx' +import type { createStart } from '@tanstack/react-start' +declare module '@tanstack/react-start' { + interface Register { + ssr: true + router: Awaited> + } +} diff --git a/examples/react/secure-sandbox-tools/src/router.tsx b/examples/react/secure-sandbox-tools/src/router.tsx new file mode 100644 index 0000000000..a595444648 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/router.tsx @@ -0,0 +1,10 @@ +import { createRouter } from '@tanstack/react-router' +import { routeTree } from './routeTree.gen' + +export const getRouter = () => { + return createRouter({ + routeTree, + scrollRestoration: true, + defaultPreloadStaleTime: 0, + }) +} diff --git a/examples/react/secure-sandbox-tools/src/routes/__root.tsx b/examples/react/secure-sandbox-tools/src/routes/__root.tsx new file mode 100644 index 0000000000..58afa6b3fe --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/routes/__root.tsx @@ -0,0 +1,41 @@ +import { HeadContent, Scripts, createRootRoute } from '@tanstack/react-router' +import appCss from '../styles.css?url' + +export const Route = createRootRoute({ + head: () => ({ + meta: [ + { + charSet: 'utf-8', + }, + { + name: 'viewport', + content: 'width=device-width, initial-scale=1', + }, + { + title: 'secure-sandbox-tools', + }, + ], + links: [ + { + rel: 'stylesheet', + href: appCss, + }, + ], + }), + + shellComponent: RootDocument, +}) + +function RootDocument({ children }: { children: React.ReactNode }) { + return ( + + + + + + {children} + + + + ) +} diff --git a/examples/react/secure-sandbox-tools/src/routes/api.chat.ts b/examples/react/secure-sandbox-tools/src/routes/api.chat.ts new file mode 100644 index 0000000000..84d3311cd1 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/routes/api.chat.ts @@ -0,0 +1,287 @@ +import { createFileRoute } from '@tanstack/react-router' +import { + chat, + chatParamsFromRequestBody, + createChatOptions, + toServerSentEventsResponse, +} from '@tanstack/ai' +import { OPENAI_CHAT_MODELS, createOpenaiChat } from '@tanstack/ai-openai' +import { openaiByok } from '@tanstack/ai-openai/byok' +import { OllamaTextModels, ollamaText } from '@tanstack/ai-ollama' +import { ANTHROPIC_MODELS, createAnthropicChat } from '@tanstack/ai-anthropic' +import { anthropicByok } from '@tanstack/ai-anthropic/byok' +import { GEMINI_MODELS, createGeminiChat } from '@tanstack/ai-gemini' +import { geminiByok } from '@tanstack/ai-gemini/byok' +import { createGeminiTextInteractions } from '@tanstack/ai-gemini/experimental' +import { createOpenRouterText } from '@tanstack/ai-openrouter' +import { OPENROUTER_CHAT_MODELS } from '@tanstack/ai-openrouter/model-meta' +import { openrouterByok } from '@tanstack/ai-openrouter/byok' +import { GROK_CHAT_MODELS, createGrokText } from '@tanstack/ai-grok' +import { grokByok } from '@tanstack/ai-grok/byok' +import { GROQ_CHAT_MODELS, createGroqText } from '@tanstack/ai-groq' +import { groqByok } from '@tanstack/ai-groq/byok' +import { createCloudflareText } from '@tanstack/ai-cloudflare' +import { + cloudflareAccountByok, + cloudflareByok, +} from '@tanstack/ai-cloudflare/byok' +import { BEDROCK_CONVERSE_MODELS, bedrockText } from '@tanstack/ai-bedrock' +import { BYTEPLUS_CHAT_MODELS, createBytePlusText } from '@tanstack/ai-byteplus' +import { byteplusByok } from '@tanstack/ai-byteplus/byok' +import { byokMissing, getByokKey } from '@tanstack/ai/byok/server' +import { z } from 'zod' +import { secureTools } from '@/lib/secure-chat-tools' +import type { ByokProvider } from '@tanstack/ai/byok' + +/** + * Client-supplied provider + model, validated once per request. An unknown + * provider falls back to OpenAI and an unknown model falls back to that + * provider's default, so arbitrary client strings never reach an adapter. + * The parsed `model` is narrowed to each adapter's literal model union. + */ +const modelSelectionSchema = z + .discriminatedUnion('provider', [ + z.object({ + provider: z.literal('openai'), + model: z.enum(OPENAI_CHAT_MODELS).catch('gpt-5.2'), + }), + z.object({ + provider: z.literal('anthropic'), + model: z.enum(ANTHROPIC_MODELS).catch('claude-sonnet-4-6'), + }), + z.object({ + provider: z.literal('gemini'), + model: z.enum(GEMINI_MODELS).catch('gemini-3.1-pro-preview'), + }), + z.object({ + provider: z.literal('gemini-interactions'), + model: z.enum(GEMINI_MODELS).catch('gemini-3.1-pro-preview'), + }), + z.object({ + provider: z.literal('ollama'), + model: z.enum(OllamaTextModels).catch('gpt-oss:20b'), + }), + z.object({ + provider: z.literal('grok'), + model: z.enum(GROK_CHAT_MODELS).catch('grok-build-0.1'), + }), + z.object({ + provider: z.literal('groq'), + model: z.enum(GROQ_CHAT_MODELS).catch('openai/gpt-oss-120b'), + }), + z.object({ + provider: z.literal('openrouter'), + model: z.enum(OPENROUTER_CHAT_MODELS).catch('openai/gpt-5.1'), + }), + z.object({ + provider: z.literal('bedrock'), + model: z + .enum(BEDROCK_CONVERSE_MODELS) + .catch('us.anthropic.claude-haiku-4-5-20251001-v1:0'), + }), + z.object({ + provider: z.literal('byteplus'), + model: z.enum(BYTEPLUS_CHAT_MODELS).catch('seed-2-0-lite-260428'), + }), + z.object({ + provider: z.literal('cloudflare'), + model: z.string().catch('@cf/deepseek-ai/deepseek-v4.1-flash-1017'), + }), + ]) + .catch({ provider: 'openai', model: 'gpt-5.2' }) + +type ModelSelection = z.infer +type Provider = ModelSelection['provider'] + +const BYOK_PROVIDERS: Partial> = { + openai: openaiByok, + anthropic: anthropicByok, + gemini: geminiByok, + openrouter: openrouterByok, + groq: groqByok, + grok: grokByok, + byteplus: byteplusByok, + cloudflare: cloudflareByok, +} + +function chatByokProvider(provider: Provider) { + if (provider === 'gemini-interactions') return geminiByok + return BYOK_PROVIDERS[provider] +} + +function resolveByokApiKey(request: Request, provider: Provider) { + const byokProvider = chatByokProvider(provider) + if (!byokProvider) return { missing: false as const, apiKey: null } + const apiKey = getByokKey(request, byokProvider) + if (!apiKey) return { missing: true as const, provider: byokProvider } + return { missing: false as const, apiKey } +} + +function requireApiKey(apiKey: string | null) { + if (!apiKey) { + throw new Error('API key is required') + } + return apiKey +} + +const SYSTEM_PROMPT = + 'You are a helpful assistant for a warehouse. When asked about stock levels ' + + 'or availability, use the checkInventory tool. The tool runs in a sandbox, ' + + 'so it can only read the inventory table — nothing else.' + +export const Route = createFileRoute('/api/chat')({ + server: { + handlers: { + POST: async ({ request }) => { + const requestSignal = request.signal + + if (requestSignal.aborted) { + return new Response(null, { status: 499 }) + } + + const abortController = new AbortController() + + let params + try { + params = await chatParamsFromRequestBody(await request.json()) + } catch (error) { + return new Response( + error instanceof Error ? error.message : 'Bad request', + { status: 400 }, + ) + } + + const selection = modelSelectionSchema.parse({ + provider: params.forwardedProps.provider, + model: params.forwardedProps.model, + }) + const previousInteractionId = + typeof params.forwardedProps.previousInteractionId === 'string' + ? params.forwardedProps.previousInteractionId + : undefined + + const buildChatOptions = ( + { provider, model }: ModelSelection, + apiKey: string | null, + cloudflareAccountId: string | null, + ) => { + switch (provider) { + case 'anthropic': + return createChatOptions({ + adapter: createAnthropicChat(model, requireApiKey(apiKey)), + }) + case 'openrouter': + return createChatOptions({ + adapter: createOpenRouterText(model, requireApiKey(apiKey)), + }) + case 'gemini': + return createChatOptions({ + adapter: createGeminiChat(model, requireApiKey(apiKey)), + }) + case 'gemini-interactions': + return createChatOptions({ + adapter: createGeminiTextInteractions( + model, + requireApiKey(apiKey), + ), + modelOptions: { + previous_interaction_id: previousInteractionId, + store: true, + }, + }) + case 'grok': + return createChatOptions({ + adapter: createGrokText(model, requireApiKey(apiKey)), + modelOptions: {}, + }) + case 'groq': + return createChatOptions({ + adapter: createGroqText(model, requireApiKey(apiKey)), + }) + case 'bedrock': + return createChatOptions({ + adapter: bedrockText(model, { + region: process.env.AWS_REGION || 'us-east-1', + ...(process.env.BEDROCK_AUTH === 'sigv4' && { + auth: 'sigv4' as const, + }), + }), + }) + case 'byteplus': + return createChatOptions({ + adapter: createBytePlusText(model, requireApiKey(apiKey)), + }) + case 'cloudflare': + return createChatOptions({ + adapter: createCloudflareText(model, { + accountId: requireApiKey(cloudflareAccountId), + apiKey: requireApiKey(apiKey), + ...(process.env.CLOUDFLARE_AI_GATEWAY_ID && { + gateway: { id: process.env.CLOUDFLARE_AI_GATEWAY_ID }, + }), + }), + }) + case 'ollama': + return createChatOptions({ + adapter: ollamaText(model), + }) + case 'openai': + return createChatOptions({ + adapter: createOpenaiChat(model, requireApiKey(apiKey)), + modelOptions: { + prompt_cache_key: 'user-session-12345', + prompt_cache_retention: '24h', + }, + }) + } + } + + try { + const provider: Provider = selection.provider + const resolvedKey = resolveByokApiKey(request, provider) + if (resolvedKey.missing) { + return byokMissing(resolvedKey.provider) + } + const cloudflareAccountId = + provider === 'cloudflare' + ? getByokKey(request, cloudflareAccountByok) + : null + if (provider === 'cloudflare' && !cloudflareAccountId) { + return byokMissing(cloudflareAccountByok) + } + // buildChatOptions returns a union across every provider. The spread + // is typed loosely so chat() still infers its own adapter/return from + // the concrete options built above. + const options: Record = buildChatOptions( + selection, + resolvedKey.apiKey, + cloudflareAccountId, + ) + + const stream = chat({ + ...options, + adapter: options.adapter as Parameters[0]['adapter'], + systemPrompts: [SYSTEM_PROMPT], + tools: secureTools, + messages: params.messages, + threadId: params.threadId, + runId: params.runId, + abortController, + }) + return toServerSentEventsResponse(stream, { abortController }) + } catch (error: unknown) { + const message = + error instanceof Error ? error.message : 'An error occurred' + const name = error instanceof Error ? error.name : '' + if (name === 'AbortError' || abortController.signal.aborted) { + return new Response(null, { status: 499 }) + } + return new Response(JSON.stringify({ error: message }), { + status: 500, + headers: { 'Content-Type': 'application/json' }, + }) + } + }, + }, + }, +}) diff --git a/examples/react/secure-sandbox-tools/src/routes/api.sandbox-demo.ts b/examples/react/secure-sandbox-tools/src/routes/api.sandbox-demo.ts new file mode 100644 index 0000000000..cd636237e3 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/routes/api.sandbox-demo.ts @@ -0,0 +1,43 @@ +// Surface B (HTTP-API entry) — the same sandboxed scenarios as the server +// function (src/lib/sandbox-demo.ts), exposed as a plain POST endpoint you can +// curl or call from any client: +// +// curl -X POST http://localhost:3100/api/sandbox-demo \ +// -H 'content-type: application/json' -d '{"scenario":"loop"}' +// +// → { "ok": false, "error": "Code execution timed out", "injectedCalls": 0, "ms": 2004 } +import { createFileRoute } from '@tanstack/react-router' +import { z } from 'zod' +import { SCENARIO_IDS, runScenario } from '@/lib/sandbox-scenarios' + +const bodySchema = z.object({ + scenario: z.enum(SCENARIO_IDS), + team: z.string().default('payments'), +}) + +function json(body: unknown, status = 200) { + return new Response(JSON.stringify(body), { + status, + headers: { 'Content-Type': 'application/json' }, + }) +} + +export const Route = createFileRoute('/api/sandbox-demo')({ + server: { + handlers: { + POST: async ({ request }) => { + let body + try { + body = bodySchema.parse(await request.json()) + } catch (error) { + return json( + { error: error instanceof Error ? error.message : 'Bad request' }, + 400, + ) + } + // runScenario never throws — a blocked run is a normal 200 with ok:false. + return json(await runScenario(body.scenario, body.team)) + }, + }, + }, +}) diff --git a/examples/react/secure-sandbox-tools/src/routes/index.tsx b/examples/react/secure-sandbox-tools/src/routes/index.tsx new file mode 100644 index 0000000000..8cd27a5996 --- /dev/null +++ b/examples/react/secure-sandbox-tools/src/routes/index.tsx @@ -0,0 +1,383 @@ +import { useEffect, useMemo, useRef, useState } from 'react' +import { createFileRoute } from '@tanstack/react-router' +import { Send, Square } from 'lucide-react' +import { fetchServerSentEvents, useByok, useChat } from '@tanstack/ai-react' +import { + completeOpenRouterPkceIntoByok, + startOpenRouterPkceLogin, +} from '@tanstack/ai-openrouter/pkce' +import { ThinkingPart } from '@tanstack/ai-react/ui' +import type { UIMessage } from '@tanstack/ai-react' +import type { GeminiInteractionsCustomEventValue } from '@tanstack/ai-gemini/experimental' +import type { ProviderId } from '@tanstack/ai/byok' +import type { ModelOption } from '@/lib/model-selection' +import { DEFAULT_MODEL_OPTION, MODEL_OPTIONS } from '@/lib/model-selection' +import { byok, getEnvKeyStatus, toByokProvider } from '@/lib/byok' +import { ByokKeyDialog } from '@/components/ByokKeyDialog' +import { runSandboxDemo } from '@/lib/sandbox-demo' +import type { SandboxRun } from '@/lib/sandbox' + +const SCENARIOS = [ + { id: 'ok', label: 'Run (allowed)' }, + { id: 'loop', label: 'Infinite loop' }, + { id: 'fetch', label: 'Reach 169.254.169.254' }, + { id: 'budget', label: 'Exceed call budget' }, +] as const + +/** + * Surface B demo: a TanStack Start server function whose body runs in a QuickJS + * isolate with only `host.db.metrics`. The first button does real work; the + * other three each try to go off the rails and get stopped by a guardrail. + */ +function SandboxDemo() { + const [busy, setBusy] = useState(null) + const [run, setRun] = useState<{ + scenario: string + result: SandboxRun + } | null>(null) + + const go = async (scenario: (typeof SCENARIOS)[number]['id']) => { + setBusy(scenario) + setRun(null) + try { + setRun({ scenario, result: await runSandboxDemo({ data: { scenario } }) }) + } finally { + setBusy(null) + } + } + + return ( +
+

+ Sandboxed endpoint (server function) +

+

+ Each button runs a snippet in a QuickJS isolate with only{' '} + host.db.metrics. Three of them + try to escape. +

+
+ {SCENARIOS.map((s) => ( + + ))} +
+ {run ? ( +
+
+ + {run.result.ok ? 'returned' : 'blocked'} + + + {run.result.ms}ms · {run.result.injectedCalls} host call(s) + +
+
+            {run.result.ok
+              ? JSON.stringify(run.result.value, null, 2)
+              : run.result.error}
+          
+
+ ) : null} +
+ ) +} + +function Messages({ messages }: { messages: ReadonlyArray }) { + const messagesContainerRef = useRef(null) + const visibleMessages = messages.filter((message) => + message.parts.some( + (part) => + part.type === 'thinking' || + (part.type === 'text' && part.content.trim()), + ), + ) + + useEffect(() => { + if (messagesContainerRef.current) { + messagesContainerRef.current.scrollTop = + messagesContainerRef.current.scrollHeight + } + }, [visibleMessages]) + + if (!visibleMessages.length) { + return ( +
+
+

+ Welcome to TanStack AI +

+

Start a conversation below.

+
+
+ ) + } + + return ( +
+ {visibleMessages.map((message) => ( +
+
+
+ {message.role === 'assistant' ? 'AI' : 'U'} +
+
+ {message.parts.map((part, index) => { + if (part.type === 'thinking') { + const isComplete = message.parts + .slice(index + 1) + .some((p) => p.type === 'text') + return ( +
+ +
+ ) + } + if (part.type === 'text' && part.content) { + return ( +
+ {part.content} +
+ ) + } + return null + })} +
+
+
+ ))} +
+ ) +} + +function ChatPage() { + const [selectedModel, setSelectedModel] = + useState(DEFAULT_MODEL_OPTION) + const [interactionId, setInteractionId] = useState( + undefined, + ) + const snapshot = useByok(byok) + const [envKeyStatus, setEnvKeyStatus] = useState>({}) + const [keyDialog, setKeyDialog] = useState<{ + open: boolean + provider: ProviderId | null + }>({ open: false, provider: null }) + const [openRouterCompleting, setOpenRouterCompleting] = useState(false) + const [openRouterError, setOpenRouterError] = useState(null) + const [input, setInput] = useState('') + + useEffect(() => { + void getEnvKeyStatus().then(setEnvKeyStatus) + }, []) + + useEffect(() => { + setOpenRouterCompleting(true) + void completeOpenRouterPkceIntoByok(byok) + .catch((error: unknown) => + setOpenRouterError( + error instanceof Error ? error.message : 'OpenRouter sign-in failed', + ), + ) + .finally(() => setOpenRouterCompleting(false)) + }, []) + + useEffect(() => { + if (snapshot.prompt?.reason === 'missing') { + setKeyDialog({ open: true, provider: snapshot.prompt.provider }) + } + }, [snapshot.prompt]) + + const activeByokId = toByokProvider(selectedModel.provider) + + const forwardedProps = useMemo( + () => ({ + provider: selectedModel.provider, + model: selectedModel.model, + ...(selectedModel.provider === 'gemini-interactions' && interactionId + ? { previousInteractionId: interactionId } + : {}), + }), + [selectedModel.provider, selectedModel.model, interactionId], + ) + + const { messages, setMessages, sendMessage, isLoading, error, stop } = + useChat({ + connection: fetchServerSentEvents('/api/chat'), + byok, + threadId: 'secure-sandbox-lab', + forwardedProps, + onCustomEvent: (eventType, data) => { + if (eventType === 'gemini.interactionId') { + const value = data as + | GeminiInteractionsCustomEventValue<'gemini.interactionId'> + | undefined + if (value?.interactionId) setInteractionId(value.interactionId) + } + }, + }) + + useEffect(() => { + setInteractionId(undefined) + setMessages([]) + }, [selectedModel.provider, selectedModel.model, setMessages]) + + const handleSendMessage = () => { + if (!input.trim()) return + sendMessage(input.trim()) + setInput('') + } + + return ( +
+
+
+
+
+ + +
+ setKeyDialog((s) => ({ ...s, open }))} + envStatus={envKeyStatus} + activeProvider={activeByokId} + highlightProvider={keyDialog.provider} + openRouter={{ + onLogin: () => { + setOpenRouterError(null) + void startOpenRouterPkceLogin().catch((error: unknown) => + setOpenRouterError( + error instanceof Error + ? error.message + : 'OpenRouter sign-in failed', + ), + ) + }, + completing: openRouterCompleting, + error: openRouterError, + }} + /> +
+
+ + + + + + {error ? ( +
+ {error.message} +
+ ) : null} + +
+
+ {isLoading ? ( +
+ +
+ ) : null} +
+