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

auth.md

167 lines5.1 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 that the host process inherited. If the CLI sees `XAI_API_KEY`, it can use that key and not your login.
84​
85## What each harness reads
86​
87| Adapter | `authMode: 'api-key'` (default) | `authMode: 'host'` |
88| --- | --- | --- |
89| [Grok Build](../adapters/grok-build) | `XAI_API_KEY` | `grok login` |
90| [Claude Code](../adapters/claude-code) | `ANTHROPIC_API_KEY` | `claude login` |
91| [Codex](../adapters/codex) | `CODEX_API_KEY` | `codex login` |
92| [ACP-Compatible](../adapters/acp-compatible) | Set `authMethodId` (for Grok, `xai.api_key`). | Skips ACP `authenticate`. |
93​
94OpenCode reads `OPENAI_API_KEY` from the process env. It has no `authMode` flag.
95​
96## Let the user pick (client and server)
97​
98The React chat example has this choice on `/repo-report`. The client sends `authMode`. The server builds the adapter with that value.
99​
100Client:
101​
102```tsx
103import { fetchServerSentEvents, useChat } from '@tanstack/ai-react'
104​
105function Report() {
106 const { sendMessage } = useChat({
107 connection: fetchServerSentEvents('/api/sandbox-repo-report'),
108 threadId: 'support-chat',
109 forwardedProps: { authMode: 'api-key' },
110 })
111​
112 return (
113 <button type="button" onClick={() => sendMessage('Report on this repo')}>
114 Run
115 </button>
116 )
118```
119​
120Server:
121​
122```ts
123import { chat, toServerSentEventsResponse } from '@tanstack/ai'
124import { grokBuildText } from '@tanstack/ai-grok-build'
125import { withSandbox } from '@tanstack/ai-sandbox'
126import { sandbox } from './sandbox'
127​
128export async function POST(request: Request) {
129 const body: unknown = await request.json()
130 const forwarded =
131 typeof body === 'object' &&
132 body !== null &&
133 'forwardedProps' in body &&
134 typeof body.forwardedProps === 'object' &&
135 body.forwardedProps !== null
136 ? body.forwardedProps
137 : {}
138 const authMode =
139 'authMode' in forwarded && forwarded.authMode === 'host'
140 ? 'host'
141 : 'api-key'
142 const threadId =
143 typeof body === 'object' &&
144 body !== null &&
145 'threadId' in body &&
146 typeof body.threadId === 'string'
147 ? body.threadId
148 : undefined
149 if (!threadId) {
150 return new Response('Send a threadId.', { status: 400 })
151 }
152​
153 const stream = chat({
154 adapter: grokBuildText('composer-2.5', { authMode }),
155 messages: [{ role: 'user', content: 'Report on this repo' }],
156 threadId,
157 stream: true,
158 middleware: [withSandbox(sandbox)],
159 })
160​
161 return toServerSentEventsResponse(stream)
163```
164​
165The server accepts only `'host'` or `'api-key'`. Any other value falls back to `'api-key'`.
166​
167To pick the agent, see [Harnesses](./harnesses). For `scrubEnv` and other local-process options, see [Providers (Advanced)](./providers-advanced#local-process).