MCPcopy Index your code
hub / github.com/TanStack/ai / chatStream

Method chatStream

packages/ai-claude-code/src/adapters/text.ts:288–507  ·  view source on GitHub ↗
(
    options: TextOptions<ClaudeCodeTextProviderOptions>,
  )

Source from the content-addressed store, hash-verified

286 }
287
288 async *chatStream(
289 options: TextOptions<ClaudeCodeTextProviderOptions>,
290 ): AsyncIterable<StreamChunk> {
291 const { logger } = options
292 let bridge: HostToolBridge | undefined
293 let channel: BridgeEventChannel | undefined
294 const approvalRequests: Array<StreamChunk> = []
295 // Temp files written for the run (bridge MCP config, redirected prompt) that
296 // carry the bearer token / prompt; removed in `finally` so they don't linger
297 // in the sandbox after the run.
298 let cleanupSandbox: SandboxHandle | undefined
299 const tempFiles: Array<string> = []
300 try {
301 const sandbox = this.sandboxFrom(options)
302 cleanupSandbox = sandbox
303 const cwd = this.workdir(options)
304 const runId = options.runId ?? this.generateId()
305 const threadId = options.threadId ?? this.generateId()
306 // Surfaces custom events from bridged tools (e.g. code mode console logs)
307 // on this run's live output stream.
308 channel = createBridgeEventChannel({ model: this.model, threadId, runId })
309
310 // Idempotently project workspace skills/plugins/MCP into the sandbox in
311 // claude's native format (guarded by the projection marker file).
312 const projection = options.capabilities
313 ? getWorkspaceProjection(options.capabilities, { optional: true })
314 : undefined
315 if (projection) await projectClaudeWorkspace(sandbox, projection)
316
317 const policy = options.capabilities
318 ? getSandboxPolicy(options.capabilities, { optional: true })
319 : undefined
320
321 // A permission-prompt tool gates the agent's native tools when a policy
322 // can `ask`/`deny` (interactive approvals).
323 const permission =
324 policy !== undefined
325 ? {
326 toolName: 'approval_prompt',
327 resolve: this.buildPermissionResolver(
328 policy,
329 options.approvals,
330 projection?.scripts,
331 approvalRequests,
332 threadId,
333 runId,
334 ),
335 }
336 : undefined
337
338 // Bridge chat()-provided server tools (and/or the permission tool) into
339 // the sandbox over MCP.
340 const hasTools = options.tools !== undefined && options.tools.length > 0
341 if (hasTools || permission !== undefined) {
342 const provisioner =
343 (options.capabilities
344 ? getToolBridgeProvisioner(options.capabilities, { optional: true })
345 : undefined) ?? nodeHttpBridgeProvisioner

Callers 2

Calls 15

sandboxFromMethod · 0.95
workdirMethod · 0.95
buildCommandMethod · 0.95
createBridgeEventChannelFunction · 0.90
projectClaudeWorkspaceFunction · 0.90
buildPromptFunction · 0.90
mapPolicyToClaudeFlagsFunction · 0.90
spawnNdjsonFunction · 0.90
mergeChunkStreamsFunction · 0.90
translateSdkStreamFunction · 0.90
bridgeToMcpConfigFunction · 0.85

Tested by

no test coverage detected