-
-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathtext.ts
More file actions
466 lines (430 loc) · 15.9 KB
/
Copy pathtext.ts
File metadata and controls
466 lines (430 loc) · 15.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import { EventType, normalizeSystemPrompts } from '@tanstack/ai'
import { toRunErrorRawEvent } from '@tanstack/ai/adapter-internals'
import { BaseTextAdapter } from '@tanstack/ai/adapters'
import {
SandboxCapability,
approvalId,
buildApprovalRequestedEvent,
getSandbox,
getSandboxPolicy,
hostForSandbox,
resolveApproval,
spawnNdjson,
startHostToolBridge,
} from '@tanstack/ai-sandbox'
import { buildPrompt } from '../messages/prompt'
import { translateSdkStream } from '../stream/translate'
import { mapPolicyToClaudeFlags } from './policy-map'
import type { ClaudePolicyFlags } from './policy-map'
import type {
HostToolBridge,
PermissionToolResult,
SandboxHandle,
SandboxPolicy,
} from '@tanstack/ai-sandbox'
import type {
StructuredOutputOptions,
StructuredOutputResult,
} from '@tanstack/ai/adapters'
import type {
DefaultMessageMetadataByModality,
Modality,
StreamChunk,
TextOptions,
} from '@tanstack/ai'
import type { ClaudeCodeModel } from '../model-meta'
import type { ClaudeCodeTextProviderOptions } from '../provider-options'
import type { AgentSdkMessage } from '../stream/sdk-types'
export type ClaudeCodePermissionMode =
| 'default'
| 'acceptEdits'
| 'bypassPermissions'
| 'plan'
const DEFAULT_WORKDIR = '/workspace'
export interface ClaudeCodeTextConfig {
/**
* Working directory inside the sandbox where `claude` runs. Defaults to
* `/workspace` (the conventional sandbox workspace root).
*/
cwd?: string
/**
* Claude Code permission mode passed via `--permission-mode`. Defaults to
* `'bypassPermissions'` — a sandbox is isolated, so the agent is allowed to
* edit files and run commands without prompting. Tighten via `defineSandboxPolicy`
* / this option for less autonomy.
*/
permissionMode?: ClaudeCodePermissionMode
/** Built-in tools the harness may use (`--allowedTools`). */
allowedTools?: Array<string>
/** Built-in tools removed from the harness (`--disallowedTools`). */
disallowedTools?: Array<string>
/** Extra directories the agent may access (`--add-dir`). */
addDirs?: Array<string>
/** Maximum harness-internal turns (`--max-turns`). */
maxTurns?: number
/**
* How `systemPrompts` from `chat()` are applied:
* - `'append'` (default): `--append-system-prompt` on top of the preset.
* - `'replace'`: `--system-prompt` as the entire system prompt.
*/
systemPromptMode?: 'append' | 'replace'
/** Path/name of the claude executable inside the sandbox. Defaults to `claude`. */
claudeExecutable?: string
/** Emit token-level deltas via `--include-partial-messages` (default true). */
streamPartials?: boolean
/** Extra environment variables for the claude process inside the sandbox. */
env?: Record<string, string>
/** Emit a `file.changed` CUSTOM event with the git diff after the run (default true). */
emitDiff?: boolean
}
/** POSIX single-quote escape for embedding values in the `claude …` command. */
function q(value: string): string {
return `'${value.replace(/'/g, `'\\''`)}'`
}
/** Format a host tool-bridge as claude's `--mcp-config` JSON. */
function bridgeToMcpConfig(bridge: HostToolBridge): string {
return JSON.stringify({
mcpServers: {
[bridge.name]: {
type: 'http',
url: bridge.url,
headers: { Authorization: `Bearer ${bridge.token}` },
},
},
})
}
export class ClaudeCodeTextAdapter<
TModel extends ClaudeCodeModel,
> extends BaseTextAdapter<
TModel,
ClaudeCodeTextProviderOptions,
ReadonlyArray<Modality> & readonly ['text'],
DefaultMessageMetadataByModality,
ReadonlyArray<string>,
unknown,
never
> {
readonly name = 'claude-code' as const
// Harness adapter: requires a sandbox to run the agent CLI inside.
override readonly requires = [SandboxCapability] as const
// The agent runs inside the (persistent) sandbox, so on resume the engine can
// re-attach to the still-running process and continue live after replaying the
// persisted event tail (rather than ending at replay). Live re-attach behavior
// is verified with the real CLI; the engine seam is unit-tested.
readonly supportsReattach = true
private readonly adapterConfig: ClaudeCodeTextConfig
constructor(config: ClaudeCodeTextConfig, model: TModel) {
super({}, model)
this.adapterConfig = config
}
private sandboxFrom(
options: TextOptions<ClaudeCodeTextProviderOptions>,
): SandboxHandle {
const ctx = options.capabilities
if (!ctx) {
throw new Error(
'Adapter "claude-code" requires a sandbox. Add withSandbox(defineSandbox({ ... })) ' +
'to chat() middleware (e.g. with the local-process or docker provider).',
)
}
return getSandbox(ctx)
}
private workdir(options: TextOptions<ClaudeCodeTextProviderOptions>): string {
return (
options.modelOptions?.cwd ?? this.adapterConfig.cwd ?? DEFAULT_WORKDIR
)
}
/** Build the `claude` command line (prompt goes via stdin, not argv). */
private buildCommand(
options: TextOptions<ClaudeCodeTextProviderOptions>,
resume: string | undefined,
policyFlags: ClaudePolicyFlags,
mcpConfigJson: string | undefined,
permissionPromptTool: string | undefined,
): string {
const config = this.adapterConfig
const modelOptions = options.modelOptions
const exe = config.claudeExecutable ?? 'claude'
const args: Array<string> = [
'-p',
'--output-format',
'stream-json',
'--verbose',
'--model',
q(this.model),
]
if (config.streamPartials !== false) args.push('--include-partial-messages')
if (resume !== undefined) args.push('--resume', q(resume))
// Precedence: per-call modelOptions > adapter config > policy > sandbox default.
const permissionMode =
modelOptions?.permissionMode ??
config.permissionMode ??
policyFlags.permissionMode ??
'bypassPermissions'
args.push('--permission-mode', q(permissionMode))
const maxTurns = modelOptions?.maxTurns ?? config.maxTurns
if (maxTurns !== undefined) args.push('--max-turns', String(maxTurns))
for (const dir of config.addDirs ?? []) args.push('--add-dir', q(dir))
const allowedTools = [
...(modelOptions?.allowedTools ?? config.allowedTools ?? []),
...policyFlags.allowedTools,
]
if (allowedTools.length > 0) {
args.push('--allowedTools', q([...new Set(allowedTools)].join(',')))
}
const disallowedTools = [
...(modelOptions?.disallowedTools ?? config.disallowedTools ?? []),
...policyFlags.disallowedTools,
]
if (disallowedTools.length > 0) {
args.push('--disallowedTools', q([...new Set(disallowedTools)].join(',')))
}
const systemPrompts = normalizeSystemPrompts(options.systemPrompts)
.map((prompt) => prompt.content)
.filter((content) => content.trim() !== '')
if (systemPrompts.length > 0) {
const joined = systemPrompts.join('\n\n')
const flag =
config.systemPromptMode === 'replace'
? '--system-prompt'
: '--append-system-prompt'
args.push(flag, q(joined))
}
if (mcpConfigJson !== undefined) args.push('--mcp-config', q(mcpConfigJson))
if (permissionPromptTool !== undefined) {
args.push('--permission-prompt-tool', q(permissionPromptTool))
}
return `${exe} ${args.join(' ')}`
}
/**
* Build the permission-prompt resolver the host MCP bridge exposes to claude
* (`--permission-prompt-tool`). Maps claude's permission request onto the
* sandbox policy + client approvals; on an `ask` action with no decision yet,
* records an approval-requested event and denies (the client re-runs to grant).
*/
private buildPermissionResolver(
policy: SandboxPolicy | undefined,
approvals: ReadonlyMap<string, boolean> | undefined,
sink: Array<StreamChunk>,
threadId: string,
runId: string,
): (input: { tool_name?: string; input?: unknown }) => PermissionToolResult {
const writeTools = new Set(['Write', 'Edit', 'MultiEdit', 'NotebookEdit'])
const networkTools = new Set(['WebFetch', 'WebSearch'])
return (request) => {
const toolName = request.tool_name ?? 'tool'
const cmdInput = request.input
const command =
toolName === 'Bash' &&
cmdInput !== null &&
typeof cmdInput === 'object' &&
'command' in cmdInput &&
typeof (cmdInput as { command?: unknown }).command === 'string'
? (cmdInput as { command: string }).command
: undefined
const capability = writeTools.has(toolName)
? 'fileWrite'
: networkTools.has(toolName)
? 'network'
: undefined
const id = approvalId({
provider: 'claude-code',
kind: command !== undefined ? 'command' : (capability ?? 'tool'),
target: command ?? toolName,
})
const outcome = resolveApproval({
policy,
approvals,
id,
...(command !== undefined ? { command } : {}),
...(capability !== undefined ? { capability } : {}),
})
if (outcome.needsApproval) {
sink.push(
buildApprovalRequestedEvent({
approvalId: id,
title: `Approve ${toolName}${command !== undefined ? `: ${command}` : ''}`,
threadId,
runId,
detail: { provider: 'claude-code', toolName },
}),
)
return {
behavior: 'deny',
message:
'Awaiting client approval. Approve in the UI and re-run to continue.',
}
}
return outcome.decision === 'allow'
? { behavior: 'allow' }
: { behavior: 'deny', message: 'Denied by sandbox policy.' }
}
}
async *chatStream(
options: TextOptions<ClaudeCodeTextProviderOptions>,
): AsyncIterable<StreamChunk> {
const { logger } = options
let bridge: HostToolBridge | undefined
const approvalRequests: Array<StreamChunk> = []
try {
const sandbox = this.sandboxFrom(options)
const cwd = this.workdir(options)
const runId = options.runId ?? this.generateId()
const threadId = options.threadId ?? this.generateId()
const policy = options.capabilities
? getSandboxPolicy(options.capabilities, { optional: true })
: undefined
// A permission-prompt tool gates the agent's native tools when a policy
// can `ask`/`deny` (interactive approvals).
const permission =
policy !== undefined
? {
toolName: 'approval_prompt',
resolve: this.buildPermissionResolver(
policy,
options.approvals,
approvalRequests,
threadId,
runId,
),
}
: undefined
// Bridge chat()-provided server tools (and/or the permission tool) into
// the sandbox over MCP.
const hasTools = options.tools !== undefined && options.tools.length > 0
if (hasTools || permission !== undefined) {
bridge = await startHostToolBridge(options.tools ?? [], {
hostForSandbox: hostForSandbox(sandbox.provider),
context: options.context,
...(permission !== undefined ? { permission } : {}),
...(options.abortController?.signal
? { signal: options.abortController.signal }
: {}),
})
}
const { prompt, resume } = buildPrompt(
options.messages,
options.modelOptions?.sessionId,
)
const command = this.buildCommand(
options,
resume,
mapPolicyToClaudeFlags(policy),
bridge ? bridgeToMcpConfig(bridge) : undefined,
bridge && permission
? `mcp__${bridge.name}__${permission.toolName}`
: undefined,
)
logger.request(
`activity=chat provider=claude-code model=${this.model} sandbox=${sandbox.provider} messages=${options.messages.length} resume=${resume ?? 'none'}`,
{ provider: 'claude-code', model: this.model },
)
const rawEvents = spawnNdjson(sandbox, command, {
cwd,
input: prompt,
...(options.modelOptions === undefined &&
this.adapterConfig.env === undefined
? {}
: { env: this.adapterConfig.env }),
...(options.abortController?.signal
? { signal: options.abortController.signal }
: options.request?.signal
? { signal: options.request.signal }
: {}),
onNonJsonLine: (line) =>
logger.provider(`provider=claude-code non-json line: ${line}`, {
chunk: line,
}),
})
async function* asMessages(): AsyncIterable<AgentSdkMessage> {
for await (const event of rawEvents) yield event as AgentSdkMessage
}
yield* translateSdkStream(asMessages(), {
model: this.model,
runId,
threadId,
...(options.parentRunId !== undefined && {
parentRunId: options.parentRunId,
}),
genId: () => this.generateId(),
onSdkMessage: (message) =>
logger.provider(`provider=claude-code type=${message.type}`, {
chunk: message,
}),
})
// Surface the working-tree diff so UIs can render what the agent changed.
if (this.adapterConfig.emitDiff !== false) {
try {
const diff = await sandbox.process.exec(`git -C ${q(cwd)} diff`, {
cwd,
})
if (diff.exitCode === 0 && diff.stdout.trim() !== '') {
yield {
type: EventType.CUSTOM,
name: 'file.changed',
value: { path: '.', diff: diff.stdout },
timestamp: Date.now(),
threadId,
runId,
}
}
} catch {
// not a git repo / git unavailable — skip the diff event
}
}
// Surface any pending approval requests (policy `ask` actions awaiting a
// client decision); the client approves and re-runs to continue.
for (const event of approvalRequests) yield event
} catch (error: unknown) {
const err = error as Error & { code?: string }
const rawEvent = toRunErrorRawEvent(error)
logger.errors('claude-code.chatStream fatal', {
error,
source: 'claude-code.chatStream',
})
yield {
type: EventType.RUN_ERROR,
model: options.model,
timestamp: Date.now(),
message: err.message || 'Unknown error occurred',
...(err.code !== undefined && { code: err.code }),
...(rawEvent !== undefined && { rawEvent }),
error: {
message: err.message || 'Unknown error occurred',
...(err.code !== undefined && { code: err.code }),
},
}
} finally {
if (bridge) await bridge.close()
}
}
structuredOutput(
_options: StructuredOutputOptions<ClaudeCodeTextProviderOptions>,
): Promise<StructuredOutputResult<unknown>> {
return Promise.reject(
new Error(
'Structured output is not yet supported by the in-sandbox Claude Code adapter. ' +
'Use a model adapter (e.g. anthropic) for structured output, or omit outputSchema.',
),
)
}
}
/**
* Creates a Claude Code harness adapter that runs **inside a sandbox**.
*
* Unlike HTTP provider adapters, this is a *harness* adapter: it spawns the
* `claude` CLI inside the sandbox provided by `withSandbox(...)` (the adapter
* declares `requires: [SandboxCapability]`), streams its `stream-json` stdout
* back as AG-UI events, and lets Claude Code run its own agent loop and native
* tools (Bash, file edits, search, …) against the sandbox workspace. The
* sandbox image must provide the `claude` executable and `ANTHROPIC_API_KEY`
* in its environment (e.g. via `workspace.secrets`). The session id is
* surfaced via a CUSTOM `claude-code.session-id` event so follow-up calls can
* resume through `modelOptions.sessionId`.
*/
export function claudeCodeText<TModel extends ClaudeCodeModel>(
model: TModel,
config: ClaudeCodeTextConfig = {},
): ClaudeCodeTextAdapter<TModel> {
return new ClaudeCodeTextAdapter(config, model)
}