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

auth.md

177 lines5.7 KB

1---
2title: Harness Auth
3id: sandbox-auth
4description: "Pick host login or an API key for a coding-agent harness. The same local-process sandbox can run on your laptop or on a CI runner."
5---
6​
7Pass credentials into `localProcessSandbox()` yourself. The sandbox type does not know whether the machine has `grok login` or only an API key.
8​
9You tell it with `authMode` on the harness adapter:
10​
11- `'api-key'` (default): inject the key and use it. Use this in Docker, in a cloud sandbox, on a CI runner, or on any machine with no CLI login.
12- `'host'`: use the CLI login on the machine. Do not inject an API key into that process.
13​
14You can set `authMode` in two places:
15​
16- On the adapter factory: `grokBuildText('composer-2.5', { authMode: 'host' })`.
17- For one call: `chat({ modelOptions: { authMode: 'host' } })`.
18​
19## Use an API key
20​
21This is the default, so you can omit `authMode`. Pass the key as a workspace secret:
22​
23```ts
24import { chat } from '@tanstack/ai'
25import { GROK_CLI_INSTALL_COMMAND, grokBuildText } from '@tanstack/ai-grok-build'
26import {
27 createSecrets,
28 defineSandbox,
29 defineWorkspace,
30 githubRepo,
31 withSandbox,
32} from '@tanstack/ai-sandbox'
33import { dockerSandbox } from '@tanstack/ai-sandbox-docker'
34​
35const sandbox = defineSandbox({
36 id: 'repo-agent',
37 provider: dockerSandbox({ image: 'node:22' }),
38 workspace: defineWorkspace({
39 source: githubRepo({ repo: 'owner/app' }),
40 setup: [GROK_CLI_INSTALL_COMMAND],
41 secrets: createSecrets({
42 XAI_API_KEY: process.env.XAI_API_KEY ?? '',
43 }),
44 }),
45})
46​
47const stream = chat({
48 threadId: 'thread-1',
49 adapter: grokBuildText('composer-2.5'),
50 messages: [{ role: 'user', content: 'Summarize this repo.' }],
51 middleware: [withSandbox(sandbox)],
52})
53```
54​
55## Use the host login
56​
57If the machine already ran `grok login`, `claude login`, or `codex login`, use this setup:
58​
59```ts
60import { chat } from '@tanstack/ai'
61import { grokBuildText } from '@tanstack/ai-grok-build'
62import { defineSandbox, defineWorkspace, withSandbox } from '@tanstack/ai-sandbox'
63import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
64​
65const sandbox = defineSandbox({
66 id: 'repo-agent',
67 provider: localProcessSandbox({
68 scrubEnv: ['XAI_API_KEY', 'GROK_API_KEY'],
69 }),
70 workspace: defineWorkspace({
71 source: { type: 'local', path: '/abs/path/to/repo' },
72 }),
73})
74​
75const stream = chat({
76 threadId: 'thread-1',
77 adapter: grokBuildText('composer-2.5', { authMode: 'host' }),
78 messages: [{ role: 'user', content: 'Summarize this repo.' }],
79 middleware: [withSandbox(sandbox)],
80})
81```
82​
83`scrubEnv` removes keys from the env of each command, for example a key that a workspace secret sets. If the CLI sees `XAI_API_KEY`, it can use that key and not your login. The keys of the host process do not reach the command.
84​
85The command gets only a few variables of the host process. If your login or your network needs more, for example `CLAUDE_CONFIG_DIR`, `CODEX_HOME`, `HTTPS_PROXY`, or `SSH_AUTH_SOCK`, name them in `passEnv`:
86​
87```ts
88import { localProcessSandbox } from '@tanstack/ai-sandbox-local-process'
89​
90const provider = localProcessSandbox({
91 passEnv: ['CODEX_HOME', 'HTTPS_PROXY'],
92})
93```
94​
95## What each harness reads
96​
97| Adapter | `authMode: 'api-key'` (default) | `authMode: 'host'` |
98| --- | --- | --- |
99| [Grok Build](../adapters/grok-build) | `XAI_API_KEY` | `grok login` |
100| [Claude Code](../adapters/claude-code) | `ANTHROPIC_API_KEY` | `claude login` |
101| [Codex](../adapters/codex) | `CODEX_API_KEY` | `codex login` |
102| [ACP-Compatible](../adapters/acp-compatible) | Set `authMethodId` (for Grok, `xai.api_key`). | Skips ACP `authenticate`. |
103​
104OpenCode reads `OPENAI_API_KEY` from its env. It has no `authMode` flag. On local-process, the key of the host process does not reach it: set the key as a workspace secret or with `env.set`.
105​
106## Let the user pick (client and server)
107​
108The React chat example has this choice on `/repo-report`. The client sends `authMode`. The server builds the adapter with that value.
109​
110Client:
111​
112```tsx
113import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
114​
115function Report() {
116 const { sendMessage } = useChat({
117 connection: fetchServerSentEvents('/api/sandbox-repo-report'),
118 threadId: 'support-chat',
119 forwardedProps: { authMode: 'api-key' },
120 })
121​
122 return (
123 <button type="button" onClick={() => sendMessage('Report on this repo')}>
124 Run
125 </button>
126 )
128```
129​
130Server:
131​
132```ts
133import { chat, toServerSentEventsResponse } from '@tanstack/ai'
134import { grokBuildText } from '@tanstack/ai-grok-build'
135import { withSandbox } from '@tanstack/ai-sandbox'
136import { sandbox } from './sandbox'
137​
138export async function POST(request: Request) {
139 const body: unknown = await request.json()
140 const forwarded =
141 typeof body === 'object' &&
142 body !== null &&
143 'forwardedProps' in body &&
144 typeof body.forwardedProps === 'object' &&
145 body.forwardedProps !== null
146 ? body.forwardedProps
147 : {}
148 const authMode =
149 'authMode' in forwarded && forwarded.authMode === 'host'
150 ? 'host'
151 : 'api-key'
152 const threadId =
153 typeof body === 'object' &&
154 body !== null &&
155 'threadId' in body &&
156 typeof body.threadId === 'string'
157 ? body.threadId
158 : undefined
159 if (!threadId) {
160 return new Response('Send a threadId.', { status: 400 })
161 }
162​
163 const stream = chat({
164 adapter: grokBuildText('composer-2.5', { authMode }),
165 messages: [{ role: 'user', content: 'Report on this repo' }],
166 threadId,
167 stream: true,
168 middleware: [withSandbox(sandbox)],
169 })
170​
171 return toServerSentEventsResponse(stream)
173```
174​
175The server accepts only `'host'` or `'api-key'`. Any other value falls back to `'api-key'`.
176​
177To pick the agent, see [Harnesses](./harnesses). For `scrubEnv`, `passEnv`, and other local-process options, see [Providers (Advanced)](./providers-advanced#local-process).