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

harnesses.md

137 lines6.4 KB

1---
2title: Harnesses
3id: sandbox-harnesses
4description: "Pick which coding agent runs inside a TanStack AI sandbox: Grok Build, Claude Code, Codex, OpenCode, or any ACP-compliant agent via acpCompatible."
5---
6​
7A **harness adapter** is the second axis of a sandboxed run: it decides _which
8coding agent runs_ and translates that agent's work back into `chat()` stream
9chunks. The [provider](./providers) decides _where_ the agent runs; the harness
10decides _what_ runs. Both sit behind the same `chat()` + `withSandbox()` wiring,
11so you can swap a harness without touching your provider or [workspace](./workspace).
12​
13Every harness adapter declares `requires: [SandboxCapability]`, so `chat()` fails
14fast at the call site unless a sandbox is provided via `withSandbox(...)`.
15​
16## Built-in harness adapters
17​
18Each agent has its own package with curated per-model metadata. Pass the adapter
19to `chat({ adapter })` and run it under any provider.
20​
21| Harness | Package | Adapter | Auth |
22| --- | --- | --- | --- |
23| [Grok Build](../adapters/grok-build) | `@tanstack/ai-grok-build` | `grokBuildText` | Default `'api-key'` (`XAI_API_KEY`). `'host'` uses `grok login`. |
24| [Claude Code](../adapters/claude-code) | `@tanstack/ai-claude-code` | `claudeCodeText` | Default `'api-key'` (`ANTHROPIC_API_KEY`). `'host'` uses `claude login`. |
25| [Codex](../adapters/codex) | `@tanstack/ai-codex` | `codexText` | Default `'api-key'` (`CODEX_API_KEY`). `'host'` uses `codex login`. |
26| [OpenCode](../adapters/opencode) | `@tanstack/ai-opencode` | `opencodeText` | `OPENAI_API_KEY` from the process env. No `authMode` flag. |
27| [ACP-Compatible](../adapters/acp-compatible) | `@tanstack/ai-acp` | `acpCompatible` / `acpCompatibleText` | Default `'api-key'` uses `authMethodId`. `'host'` skips ACP `authenticate`. |
28​
29The provider is where the agent runs, not how it signs in. The default
30`authMode` is `'api-key'`. Set `'host'` when the machine already has a CLI
31login. See [Harness Auth](./auth).
32​
33```ts
34import { chat } from '@tanstack/ai'
35import { grokBuildText } from '@tanstack/ai-grok-build'
36import { withSandbox } from '@tanstack/ai-sandbox'
37import { sandbox } from './sandbox'
38import { messages } from './chat-context'
39​
40const stream = chat({
41 adapter: grokBuildText('grok-build'),
42 messages,
43 middleware: [withSandbox(sandbox)],
44})
45```
46​
47## Typed report from a harness
48​
49If you want a typed object after the agent inspects the repo, pass `outputSchema` on the same `chat()` call. Dedicated adapters and `acpCompatible` honor it. See [Harness Agents](../structured-outputs/harnesses).
50​
51## Harness output can go to a journal
52​
53`grokBuildText`, `claudeCodeText`, and `codexText` can stop holding the agent's
54stdout pipe: instead they redirect it to an append-only NDJSON file inside the
55sandbox (`/tmp/tanstack-runs/<runId>.ndjson`) and tail that, so losing the host
56cannot signal the agent and any later reader can replay the run from byte 0.
57​
58**They do that only for a durable run.** Journaling is opt-in, and the opt-in is
59passing `withSandbox` **both** `runs` and `durability` (the snippet above), a
60plain `withSandbox(sandbox)`, writes no journal and streams over a pipe exactly
61as it always has. Pass both and forward the `runId`, whose value the journal path
62is derived from (a durable run with no caller-supplied `runId` throws
63`DurableRunIdRequiredError` rather than minting one nothing can recompute):
64​
65```ts
66import {
67 chat,
68 chatParamsFromRequest,
69 memoryStream,
70 toServerSentEventsResponse,
71} from '@tanstack/ai'
72import { codexText } from '@tanstack/ai-codex'
73import { memoryPersistence } from '@tanstack/ai-persistence'
74import { withSandbox } from '@tanstack/ai-sandbox'
75import { sandbox } from './sandbox'
76​
77// Single-process stand-ins; ./takeover has the multi-replica wiring.
78const persistence = memoryPersistence()
79const { runs } = persistence.stores
80​
81export async function POST(request: Request) {
82 const { messages, threadId, runId } = await chatParamsFromRequest(request)
83 const adapter = memoryStream(request)
84 const stream = chat({
85 adapter: codexText('gpt-5.3-codex'),
86 messages,
87 threadId,
88 runId, // makes the run's journal findable again
89 // BOTH stores, or there is no journal: this is the whole opt-in.
90 middleware: [withSandbox(sandbox, { runs, durability: { adapter } })],
91 })
92 return toServerSentEventsResponse(stream, { durability: { adapter } })
94```
95​
96The id must be unique per run: the journal appends, so reusing one makes a reader
97stop at the previous run's exit sentinel. Full details, including what you get
98without the opt-in and replay against an already-delivered log, are in
99[The Run Journal](./journal).
100​
101`opencodeText` and `acpCompatible` harnesses do not read NDJSON off stdout at
102all, so they have no journal even when a run is durable.
103​
104## Any ACP agent (`acpCompatible`)
105​
106Many coding agents speak the [Agent Client Protocol](https://agentclientprotocol.com)
107(ACP), `pi`, `gemini --acp`, and [dozens of others](https://agentclientprotocol.com/get-started/agents).
108For any of them that doesn't have a dedicated package, `acpCompatible` (from
109`@tanstack/ai-acp`) builds a harness adapter on the spot, the harness equivalent
110of `openaiCompatible`. Configure how to launch it once, then run it under any
111provider like the built-in adapters:
112​
113```ts
114import { acpCompatible } from '@tanstack/ai-acp'
115​
116const pi = acpCompatible({
117 name: 'pi',
118 models: ['pi-fast', 'pi-pro'],
119 command: ({ model, harnessCwd }) => `pi --acp -m ${model} --cwd ${harnessCwd}`,
120 authMethodId: 'pi-api-key',
121})
122```
123​
124See the [ACP-Compatible Harness](../adapters/acp-compatible) guide for the full
125config (typed models, per-call `modelOptions`, WebSocket transports, permissions,
126and protocol coverage). For which agents you can plug in, browse the official
127**[ACP agents list](https://agentclientprotocol.com/get-started/agents)** and the
128**[ACP registry](https://agentclientprotocol.com/get-started/registry)**.
129​
130## Where to go next
131​
132- **[Harness Auth](./auth)**: pick host login or an API key. The sandbox type does not pick this.
133- **[Providers](./providers)**: where the harness runs (local, Docker, Daytona, Vercel, Sprites).
134- **[The Run Journal](./journal)**: how a run's output survives the host that started it.
135- **[Takeover & Detached Runs](./takeover)**: detach on disconnect, and the three exports a harness adapter implements to support attach.
136- **[Tools](./tools)**: bridge your app's own tools into the in-sandbox agent.
137- **[Events & File Hooks](./events)**: stream the agent's edits and activity to a UI.