(
options: TextOptions<ClaudeCodeTextProviderOptions>,
)
| 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 |
no test coverage detected