@tanstack/ai 0.10.1 → 0.10.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -379,11 +379,16 @@ class TextEngine {
379
379
  needsApproval: executionResult.needsApproval,
380
380
  needsClientExecution: executionResult.needsClientExecution
381
381
  });
382
+ const argsMap = /* @__PURE__ */ new Map();
383
+ for (const tc of pendingToolCalls) {
384
+ argsMap.set(tc.id, tc.function.arguments);
385
+ }
382
386
  if (executionResult.needsApproval.length > 0 || executionResult.needsClientExecution.length > 0) {
383
387
  if (executionResult.results.length > 0) {
384
388
  for (const chunk of this.buildToolResultChunks(
385
389
  executionResult.results,
386
- finishEvent
390
+ finishEvent,
391
+ argsMap
387
392
  )) {
388
393
  yield chunk;
389
394
  }
@@ -405,7 +410,8 @@ class TextEngine {
405
410
  }
406
411
  const toolResultChunks = this.buildToolResultChunks(
407
412
  executionResult.results,
408
- finishEvent
413
+ finishEvent,
414
+ argsMap
409
415
  );
410
416
  for (const chunk of toolResultChunks) {
411
417
  yield chunk;
@@ -631,10 +637,28 @@ class TextEngine {
631
637
  }
632
638
  return chunks;
633
639
  }
634
- buildToolResultChunks(results, finishEvent) {
640
+ buildToolResultChunks(results, finishEvent, argsMap) {
635
641
  const chunks = [];
636
642
  for (const result of results) {
637
643
  const content = JSON.stringify(result.result);
644
+ if (argsMap) {
645
+ chunks.push({
646
+ type: "TOOL_CALL_START",
647
+ timestamp: Date.now(),
648
+ model: finishEvent.model,
649
+ toolCallId: result.toolCallId,
650
+ toolName: result.toolName
651
+ });
652
+ const args = argsMap.get(result.toolCallId) ?? "{}";
653
+ chunks.push({
654
+ type: "TOOL_CALL_ARGS",
655
+ timestamp: Date.now(),
656
+ model: finishEvent.model,
657
+ toolCallId: result.toolCallId,
658
+ delta: args,
659
+ args
660
+ });
661
+ }
638
662
  chunks.push({
639
663
  type: "TOOL_CALL_END",
640
664
  timestamp: Date.now(),
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../../src/activities/chat/index.ts"],"sourcesContent":["/**\n * Text Activity\n *\n * Handles agentic text generation, one-shot text generation, and agentic structured output.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { devtoolsMiddleware } from '@tanstack/ai-event-client'\nimport { streamToText } from '../../stream-to-response.js'\nimport { LazyToolManager } from './tools/lazy-tool-manager'\nimport {\n MiddlewareAbortError,\n ToolCallManager,\n executeToolCalls,\n} from './tools/tool-calls'\nimport {\n convertSchemaToJsonSchema,\n isStandardSchema,\n parseWithStandardSchema,\n} from './tools/schema-converter'\nimport { maxIterations as maxIterationsStrategy } from './agent-loop-strategies'\nimport { convertMessagesToModelMessages } from './messages'\nimport { MiddlewareRunner } from './middleware/compose'\nimport type {\n ApprovalRequest,\n ClientToolRequest,\n ToolResult,\n} from './tools/tool-calls'\nimport type { AnyTextAdapter } from './adapter'\nimport type {\n AgentLoopStrategy,\n ConstrainedModelMessage,\n CustomEvent,\n InferSchemaType,\n ModelMessage,\n RunFinishedEvent,\n SchemaInput,\n StreamChunk,\n TextMessageContentEvent,\n TextOptions,\n Tool,\n ToolCall,\n ToolCallArgsEvent,\n ToolCallEndEvent,\n ToolCallStartEvent,\n} from '../../types'\nimport type {\n ChatMiddleware,\n ChatMiddlewareConfig,\n ChatMiddlewareContext,\n ChatMiddlewarePhase,\n} from './middleware/types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'text' as const\n\n// ===========================\n// Activity Options Type\n// ===========================\n\n/**\n * Options for the text activity.\n * Types are extracted directly from the adapter (which has pre-resolved generics).\n *\n * @template TAdapter - The text adapter type (created by a provider function)\n * @template TSchema - Optional Standard Schema for structured output\n * @template TStream - Whether to stream the output (default: true)\n */\nexport interface TextActivityOptions<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined,\n TStream extends boolean,\n> {\n /** The text adapter to use (created by a provider function like openaiText('gpt-4o')) */\n adapter: TAdapter\n /** Conversation messages - content types are constrained by the adapter's input modalities and metadata */\n messages?: Array<\n ConstrainedModelMessage<{\n inputModalities: TAdapter['~types']['inputModalities']\n messageMetadataByModality: TAdapter['~types']['messageMetadataByModality']\n }>\n >\n /** System prompts to prepend to the conversation */\n systemPrompts?: TextOptions['systemPrompts']\n /** Tools for function calling (auto-executed when called) */\n tools?: TextOptions['tools']\n /** Controls the randomness of the output. Higher values make output more random. Range: [0.0, 2.0] */\n temperature?: TextOptions['temperature']\n /** Nucleus sampling parameter. The model considers tokens with topP probability mass. */\n topP?: TextOptions['topP']\n /** The maximum number of tokens to generate in the response. */\n maxTokens?: TextOptions['maxTokens']\n /** Additional metadata to attach to the request. */\n metadata?: TextOptions['metadata']\n /** Model-specific provider options (type comes from adapter) */\n modelOptions?: TAdapter['~types']['providerOptions']\n /** AbortController for cancellation */\n abortController?: TextOptions['abortController']\n /** Strategy for controlling the agent loop */\n agentLoopStrategy?: TextOptions['agentLoopStrategy']\n /** Unique conversation identifier for tracking */\n conversationId?: TextOptions['conversationId']\n /**\n * Optional Standard Schema for structured output.\n * When provided, the activity will:\n * 1. Run the full agentic loop (executing tools as needed)\n * 2. Once complete, return a Promise with the parsed output matching the schema\n *\n * Supports any Standard Schema compliant library (Zod v4+, ArkType, Valibot, etc.)\n *\n * @example\n * ```ts\n * const result = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Generate a person' }],\n * outputSchema: z.object({ name: z.string(), age: z.number() })\n * })\n * // result is { name: string, age: number }\n * ```\n */\n outputSchema?: TSchema\n /**\n * Whether to stream the text result.\n * When true (default), returns an AsyncIterable<StreamChunk> for streaming output.\n * When false, returns a Promise<string> with the collected text content.\n *\n * Note: If outputSchema is provided, this option is ignored and the result\n * is always a Promise<InferSchemaType<TSchema>>.\n *\n * @default true\n *\n * @example Non-streaming text\n * ```ts\n * const text = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }],\n * stream: false\n * })\n * // text is a string with the full response\n * ```\n */\n stream?: TStream\n /**\n * Optional middleware array for observing/transforming chat behavior.\n * Middleware hooks are called in array order. See {@link ChatMiddleware} for available hooks.\n *\n * @example\n * ```ts\n * const stream = chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [...],\n * middleware: [loggingMiddleware, redactionMiddleware],\n * })\n * ```\n */\n middleware?: Array<ChatMiddleware>\n /**\n * Opaque user-provided context value passed to middleware hooks.\n * Can be used to pass request-scoped data (e.g., user ID, request context).\n */\n context?: unknown\n}\n\n// ===========================\n// Chat Options Helper\n// ===========================\n\n/**\n * Create typed options for the chat() function without executing.\n * This is useful for pre-defining configurations with full type inference.\n *\n * @example\n * ```ts\n * const chatOptions = createChatOptions({\n * adapter: anthropicText('claude-sonnet-4-5'),\n * })\n *\n * const stream = chat({ ...chatOptions, messages })\n * ```\n */\nexport function createChatOptions<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined = undefined,\n TStream extends boolean = true,\n>(\n options: TextActivityOptions<TAdapter, TSchema, TStream>,\n): TextActivityOptions<TAdapter, TSchema, TStream> {\n return options\n}\n\n// ===========================\n// Activity Result Type\n// ===========================\n\n/**\n * Result type for the text activity.\n * - If outputSchema is provided: Promise<InferSchemaType<TSchema>>\n * - If stream is false: Promise<string>\n * - Otherwise (stream is true, default): AsyncIterable<StreamChunk>\n */\nexport type TextActivityResult<\n TSchema extends SchemaInput | undefined,\n TStream extends boolean = true,\n> = TSchema extends SchemaInput\n ? Promise<InferSchemaType<TSchema>>\n : TStream extends false\n ? Promise<string>\n : AsyncIterable<StreamChunk>\n\n// ===========================\n// ChatEngine Implementation\n// ===========================\n\ninterface TextEngineConfig<\n TAdapter extends AnyTextAdapter,\n TParams extends TextOptions<any, any> = TextOptions<any>,\n> {\n adapter: TAdapter\n systemPrompts?: Array<string>\n params: TParams\n middleware?: Array<ChatMiddleware>\n context?: unknown\n}\n\ntype ToolPhaseResult = 'continue' | 'stop' | 'wait'\ntype CyclePhase = 'processText' | 'executeToolCalls'\n\nclass TextEngine<\n TAdapter extends AnyTextAdapter,\n TParams extends TextOptions<any, any> = TextOptions<any>,\n> {\n private readonly adapter: TAdapter\n private params: TParams\n private systemPrompts: Array<string>\n private tools: Array<Tool>\n private readonly loopStrategy: AgentLoopStrategy\n private toolCallManager: ToolCallManager\n private readonly lazyToolManager: LazyToolManager\n private readonly initialMessageCount: number\n private readonly requestId: string\n private readonly streamId: string\n private readonly effectiveRequest?: Request | RequestInit\n private readonly effectiveSignal?: AbortSignal\n\n private messages: Array<ModelMessage>\n private iterationCount = 0\n private lastFinishReason: string | null = null\n private streamStartTime = 0\n private totalChunkCount = 0\n private currentMessageId: string | null = null\n private accumulatedContent = ''\n private eventOptions?: Record<string, unknown>\n private eventToolNames?: Array<string>\n private finishedEvent: RunFinishedEvent | null = null\n private earlyTermination = false\n private toolPhase: ToolPhaseResult = 'continue'\n private cyclePhase: CyclePhase = 'processText'\n // Client state extracted from initial messages (before conversion to ModelMessage)\n private readonly initialApprovals: Map<string, boolean>\n private readonly initialClientToolResults: Map<string, any>\n\n // Middleware support\n private readonly middlewareRunner: MiddlewareRunner\n private readonly middlewareCtx: ChatMiddlewareContext\n private readonly deferredPromises: Array<Promise<unknown>> = []\n private abortReason?: string\n private middlewareAbortController?: AbortController\n private terminalHookCalled = false\n\n constructor(config: TextEngineConfig<TAdapter, TParams>) {\n this.adapter = config.adapter\n this.params = config.params\n this.systemPrompts = config.params.systemPrompts || []\n this.loopStrategy =\n config.params.agentLoopStrategy || maxIterationsStrategy(5)\n this.initialMessageCount = config.params.messages.length\n\n // Extract client state (approvals, client tool results) from original messages BEFORE conversion\n // This preserves UIMessage parts data that would be lost during conversion to ModelMessage\n const { approvals, clientToolResults } =\n this.extractClientStateFromOriginalMessages(\n config.params.messages as Array<any>,\n )\n this.initialApprovals = approvals\n this.initialClientToolResults = clientToolResults\n\n // Convert messages to ModelMessage format (handles both UIMessage and ModelMessage input)\n // This ensures consistent internal format regardless of what the client sends\n this.messages = convertMessagesToModelMessages(\n config.params.messages as Array<any>,\n )\n\n // Initialize lazy tool manager after messages are converted (needs message history for scanning)\n this.lazyToolManager = new LazyToolManager(\n config.params.tools || [],\n this.messages,\n )\n this.tools = this.lazyToolManager.getActiveTools()\n this.toolCallManager = new ToolCallManager(this.tools)\n this.requestId = this.createId('chat')\n this.streamId = this.createId('stream')\n this.effectiveRequest = config.params.abortController\n ? { signal: config.params.abortController.signal }\n : undefined\n this.effectiveSignal = config.params.abortController?.signal\n\n // Initialize middleware — devtools middleware is always first\n const allMiddleware = [devtoolsMiddleware(), ...(config.middleware || [])]\n this.middlewareRunner = new MiddlewareRunner(allMiddleware)\n this.middlewareAbortController = new AbortController()\n this.middlewareCtx = {\n requestId: this.requestId,\n streamId: this.streamId,\n conversationId: config.params.conversationId,\n phase: 'init' as ChatMiddlewarePhase,\n iteration: 0,\n chunkIndex: 0,\n signal: this.effectiveSignal,\n abort: (reason?: string) => {\n this.abortReason = reason\n this.middlewareAbortController?.abort(reason)\n },\n context: config.context,\n defer: (promise: Promise<unknown>) => {\n this.deferredPromises.push(promise)\n },\n // Provider / adapter info\n provider: config.adapter.name,\n model: config.params.model,\n source: 'server',\n streaming: true,\n // Config-derived (updated in beforeRun and applyMiddlewareConfig)\n systemPrompts: this.systemPrompts,\n toolNames: undefined,\n options: undefined,\n modelOptions: config.params.modelOptions,\n // Computed\n messageCount: this.initialMessageCount,\n hasTools: this.tools.length > 0,\n // Mutable per-iteration\n currentMessageId: null,\n accumulatedContent: '',\n // References\n messages: this.messages,\n createId: (prefix: string) => this.createId(prefix),\n }\n }\n\n /** Get the accumulated content after the chat loop completes */\n getAccumulatedContent(): string {\n return this.accumulatedContent\n }\n\n /** Get the final messages array after the chat loop completes */\n getMessages(): Array<ModelMessage> {\n return this.messages\n }\n\n async *run(): AsyncGenerator<StreamChunk> {\n this.beforeRun()\n\n try {\n // Run initial onConfig (phase = init)\n this.middlewareCtx.phase = 'init'\n const initialConfig = this.buildMiddlewareConfig()\n const transformedConfig = await this.middlewareRunner.runOnConfig(\n this.middlewareCtx,\n initialConfig,\n )\n this.applyMiddlewareConfig(transformedConfig)\n\n // Run onStart (devtools middleware emits text:request:started and initial messages here)\n await this.middlewareRunner.runOnStart(this.middlewareCtx)\n\n const pendingPhase = yield* this.checkForPendingToolCalls()\n if (pendingPhase === 'wait') {\n return\n }\n\n do {\n if (this.earlyTermination || this.isCancelled()) {\n return\n }\n\n await this.beginCycle()\n\n if (this.cyclePhase === 'processText') {\n // Run onConfig before each model call (phase = beforeModel)\n this.middlewareCtx.phase = 'beforeModel'\n this.middlewareCtx.iteration = this.iterationCount\n const iterConfig = this.buildMiddlewareConfig()\n const transformedConfig = await this.middlewareRunner.runOnConfig(\n this.middlewareCtx,\n iterConfig,\n )\n this.applyMiddlewareConfig(transformedConfig)\n\n yield* this.streamModelResponse()\n } else {\n yield* this.processToolCalls()\n }\n\n this.endCycle()\n } while (this.shouldContinue())\n\n // Call terminal onFinish hook (skip when waiting for client — stream is paused, not finished)\n if (!this.terminalHookCalled && this.toolPhase !== 'wait') {\n this.terminalHookCalled = true\n await this.middlewareRunner.runOnFinish(this.middlewareCtx, {\n finishReason: this.lastFinishReason,\n duration: Date.now() - this.streamStartTime,\n content: this.accumulatedContent,\n usage: this.finishedEvent?.usage,\n })\n }\n } catch (error: unknown) {\n if (!this.terminalHookCalled) {\n this.terminalHookCalled = true\n if (error instanceof MiddlewareAbortError) {\n // Middleware abort decision — call onAbort, not onError\n this.abortReason = error.message\n await this.middlewareRunner.runOnAbort(this.middlewareCtx, {\n reason: error.message,\n duration: Date.now() - this.streamStartTime,\n })\n } else {\n // Genuine error — call onError\n await this.middlewareRunner.runOnError(this.middlewareCtx, {\n error,\n duration: Date.now() - this.streamStartTime,\n })\n }\n }\n // Don't rethrow middleware abort errors — the run just stops gracefully\n if (!(error instanceof MiddlewareAbortError)) {\n throw error\n }\n } finally {\n // Check for abort terminal hook\n if (!this.terminalHookCalled && this.isCancelled()) {\n this.terminalHookCalled = true\n await this.middlewareRunner.runOnAbort(this.middlewareCtx, {\n reason: this.abortReason,\n duration: Date.now() - this.streamStartTime,\n })\n }\n\n // Await deferred promises (non-blocking side effects)\n if (this.deferredPromises.length > 0) {\n await Promise.allSettled(this.deferredPromises)\n }\n }\n }\n\n private beforeRun(): void {\n this.streamStartTime = Date.now()\n const { tools, temperature, topP, maxTokens, metadata } = this.params\n\n // Gather flattened options into an object for context\n const options: Record<string, unknown> = {}\n if (temperature !== undefined) options.temperature = temperature\n if (topP !== undefined) options.topP = topP\n if (maxTokens !== undefined) options.maxTokens = maxTokens\n if (metadata !== undefined) options.metadata = metadata\n\n this.eventOptions = Object.keys(options).length > 0 ? options : undefined\n this.eventToolNames = tools?.map((t) => t.name)\n\n // Update middleware context with computed fields\n this.middlewareCtx.options = this.eventOptions\n this.middlewareCtx.toolNames = this.eventToolNames\n }\n\n private async beginCycle(): Promise<void> {\n if (this.cyclePhase === 'processText') {\n await this.beginIteration()\n }\n }\n\n private endCycle(): void {\n if (this.cyclePhase === 'processText') {\n this.cyclePhase = 'executeToolCalls'\n return\n }\n\n this.cyclePhase = 'processText'\n this.iterationCount++\n }\n\n private async beginIteration(): Promise<void> {\n this.currentMessageId = this.createId('msg')\n this.accumulatedContent = ''\n this.finishedEvent = null\n\n // Update mutable context fields\n this.middlewareCtx.currentMessageId = this.currentMessageId\n this.middlewareCtx.accumulatedContent = ''\n\n // Notify middleware of new iteration (devtools emits assistant message:created here)\n await this.middlewareRunner.runOnIteration(this.middlewareCtx, {\n iteration: this.iterationCount,\n messageId: this.currentMessageId,\n })\n }\n\n private async *streamModelResponse(): AsyncGenerator<StreamChunk> {\n const { temperature, topP, maxTokens, metadata, modelOptions } = this.params\n const tools = this.tools\n\n // Convert tool schemas to JSON Schema before passing to adapter\n const toolsWithJsonSchemas = tools.map((tool) => ({\n ...tool,\n inputSchema: tool.inputSchema\n ? convertSchemaToJsonSchema(tool.inputSchema)\n : undefined,\n outputSchema: tool.outputSchema\n ? convertSchemaToJsonSchema(tool.outputSchema)\n : undefined,\n }))\n\n this.middlewareCtx.phase = 'modelStream'\n\n for await (const chunk of this.adapter.chatStream({\n model: this.params.model,\n messages: this.messages,\n tools: toolsWithJsonSchemas,\n temperature,\n topP,\n maxTokens,\n metadata,\n request: this.effectiveRequest,\n modelOptions,\n systemPrompts: this.systemPrompts,\n })) {\n if (this.isCancelled()) {\n break\n }\n\n this.totalChunkCount++\n\n // Pipe chunk through middleware (devtools middleware observes and emits events)\n const outputChunks = await this.middlewareRunner.runOnChunk(\n this.middlewareCtx,\n chunk,\n )\n for (const outputChunk of outputChunks) {\n yield outputChunk\n this.handleStreamChunk(outputChunk)\n this.middlewareCtx.chunkIndex++\n }\n\n // Handle usage via middleware\n if (chunk.type === 'RUN_FINISHED' && chunk.usage) {\n await this.middlewareRunner.runOnUsage(this.middlewareCtx, chunk.usage)\n }\n\n if (this.earlyTermination) {\n break\n }\n }\n }\n\n private handleStreamChunk(chunk: StreamChunk): void {\n switch (chunk.type) {\n // AG-UI Events\n case 'TEXT_MESSAGE_CONTENT':\n this.handleTextMessageContentEvent(chunk)\n break\n case 'TOOL_CALL_START':\n this.handleToolCallStartEvent(chunk)\n break\n case 'TOOL_CALL_ARGS':\n this.handleToolCallArgsEvent(chunk)\n break\n case 'TOOL_CALL_END':\n this.handleToolCallEndEvent(chunk)\n break\n case 'RUN_FINISHED':\n this.handleRunFinishedEvent(chunk)\n break\n case 'RUN_ERROR':\n this.handleRunErrorEvent(chunk)\n break\n case 'STEP_FINISHED':\n this.handleStepFinishedEvent(chunk)\n break\n\n default:\n // RUN_STARTED, TEXT_MESSAGE_START, TEXT_MESSAGE_END, STEP_STARTED,\n // STATE_SNAPSHOT, STATE_DELTA, CUSTOM\n // - no special handling needed in chat activity\n break\n }\n }\n\n // ===========================\n // AG-UI Event Handlers\n // ===========================\n\n private handleTextMessageContentEvent(chunk: TextMessageContentEvent): void {\n if (chunk.content) {\n this.accumulatedContent = chunk.content\n } else {\n this.accumulatedContent += chunk.delta\n }\n this.middlewareCtx.accumulatedContent = this.accumulatedContent\n }\n\n private handleToolCallStartEvent(chunk: ToolCallStartEvent): void {\n this.toolCallManager.addToolCallStartEvent(chunk)\n }\n\n private handleToolCallArgsEvent(chunk: ToolCallArgsEvent): void {\n this.toolCallManager.addToolCallArgsEvent(chunk)\n }\n\n private handleToolCallEndEvent(chunk: ToolCallEndEvent): void {\n this.toolCallManager.completeToolCall(chunk)\n }\n\n private handleRunFinishedEvent(chunk: RunFinishedEvent): void {\n this.finishedEvent = chunk\n this.lastFinishReason = chunk.finishReason\n }\n\n private handleRunErrorEvent(\n _chunk: Extract<StreamChunk, { type: 'RUN_ERROR' }>,\n ): void {\n this.earlyTermination = true\n }\n\n private handleStepFinishedEvent(\n _chunk: Extract<StreamChunk, { type: 'STEP_FINISHED' }>,\n ): void {\n // State tracking for STEP_FINISHED is handled by middleware\n }\n\n private async *checkForPendingToolCalls(): AsyncGenerator<\n StreamChunk,\n ToolPhaseResult,\n void\n > {\n const pendingToolCalls = this.getPendingToolCallsFromMessages()\n if (pendingToolCalls.length === 0) {\n return 'continue'\n }\n\n const finishEvent = this.createSyntheticFinishedEvent()\n\n // Handle undiscovered lazy tool calls with self-correcting error messages\n const undiscoveredLazyResults: Array<ToolResult> = []\n const executablePendingCalls = pendingToolCalls.filter((tc) => {\n if (this.lazyToolManager.isUndiscoveredLazyTool(tc.function.name)) {\n undiscoveredLazyResults.push({\n toolCallId: tc.id,\n toolName: tc.function.name,\n result: {\n error: this.lazyToolManager.getUndiscoveredToolError(\n tc.function.name,\n ),\n },\n state: 'output-error',\n })\n return false\n }\n return true\n })\n\n if (undiscoveredLazyResults.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n undiscoveredLazyResults,\n finishEvent,\n )) {\n yield chunk\n }\n }\n\n if (executablePendingCalls.length === 0) {\n return 'continue'\n }\n\n const { approvals, clientToolResults } = this.collectClientState()\n\n const generator = executeToolCalls(\n executablePendingCalls,\n this.tools,\n approvals,\n clientToolResults,\n (eventName, data) => this.createCustomEventChunk(eventName, data),\n {\n onBeforeToolCall: async (toolCall, tool, args) => {\n const hookCtx = {\n toolCall,\n tool,\n args,\n toolName: toolCall.function.name,\n toolCallId: toolCall.id,\n }\n return this.middlewareRunner.runOnBeforeToolCall(\n this.middlewareCtx,\n hookCtx,\n )\n },\n onAfterToolCall: async (info) => {\n await this.middlewareRunner.runOnAfterToolCall(\n this.middlewareCtx,\n info,\n )\n },\n },\n )\n\n // Consume the async generator, yielding custom events and collecting the return value\n const executionResult = yield* this.drainToolCallGenerator(generator)\n\n // Check if middleware aborted during pending tool execution\n if (this.isMiddlewareAborted()) {\n this.setToolPhase('stop')\n return 'stop'\n }\n\n // Notify middleware of tool phase completion (devtools emits aggregate events here)\n await this.middlewareRunner.runOnToolPhaseComplete(this.middlewareCtx, {\n toolCalls: pendingToolCalls,\n results: executionResult.results,\n needsApproval: executionResult.needsApproval,\n needsClientExecution: executionResult.needsClientExecution,\n })\n\n if (\n executionResult.needsApproval.length > 0 ||\n executionResult.needsClientExecution.length > 0\n ) {\n if (executionResult.results.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )) {\n yield chunk\n }\n }\n\n for (const chunk of this.buildApprovalChunks(\n executionResult.needsApproval,\n finishEvent,\n )) {\n yield chunk\n }\n\n for (const chunk of this.buildClientToolChunks(\n executionResult.needsClientExecution,\n finishEvent,\n )) {\n yield chunk\n }\n\n this.setToolPhase('wait')\n return 'wait'\n }\n\n const toolResultChunks = this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )\n\n for (const chunk of toolResultChunks) {\n yield chunk\n }\n\n return 'continue'\n }\n\n private async *processToolCalls(): AsyncGenerator<StreamChunk, void, void> {\n if (!this.shouldExecuteToolPhase()) {\n this.setToolPhase('stop')\n return\n }\n\n const toolCalls = this.toolCallManager.getToolCalls()\n const finishEvent = this.finishedEvent\n\n if (!finishEvent || toolCalls.length === 0) {\n this.setToolPhase('stop')\n return\n }\n\n this.addAssistantToolCallMessage(toolCalls)\n\n // Handle undiscovered lazy tool calls with self-correcting error messages\n const undiscoveredLazyResults: Array<ToolResult> = []\n const executableToolCalls = toolCalls.filter((tc) => {\n if (this.lazyToolManager.isUndiscoveredLazyTool(tc.function.name)) {\n undiscoveredLazyResults.push({\n toolCallId: tc.id,\n toolName: tc.function.name,\n result: {\n error: this.lazyToolManager.getUndiscoveredToolError(\n tc.function.name,\n ),\n },\n state: 'output-error',\n })\n return false\n }\n return true\n })\n\n if (undiscoveredLazyResults.length > 0) {\n const finishEvt = this.finishedEvent!\n for (const chunk of this.buildToolResultChunks(\n undiscoveredLazyResults,\n finishEvt,\n )) {\n yield chunk\n }\n }\n\n if (executableToolCalls.length === 0) {\n // All tool calls were undiscovered lazy tools — errors emitted, continue loop\n this.toolCallManager.clear()\n this.setToolPhase('continue')\n return\n }\n this.middlewareCtx.phase = 'beforeTools'\n\n const { approvals, clientToolResults } = this.collectClientState()\n\n const generator = executeToolCalls(\n executableToolCalls,\n this.tools,\n approvals,\n clientToolResults,\n (eventName, data) => this.createCustomEventChunk(eventName, data),\n {\n onBeforeToolCall: async (toolCall, tool, args) => {\n const hookCtx = {\n toolCall,\n tool,\n args,\n toolName: toolCall.function.name,\n toolCallId: toolCall.id,\n }\n return this.middlewareRunner.runOnBeforeToolCall(\n this.middlewareCtx,\n hookCtx,\n )\n },\n onAfterToolCall: async (info) => {\n await this.middlewareRunner.runOnAfterToolCall(\n this.middlewareCtx,\n info,\n )\n },\n },\n )\n\n // Consume the async generator, yielding custom events and collecting the return value\n const executionResult = yield* this.drainToolCallGenerator(generator)\n\n this.middlewareCtx.phase = 'afterTools'\n\n // Check if middleware aborted during tool execution\n if (this.isMiddlewareAborted()) {\n this.setToolPhase('stop')\n return\n }\n\n // Notify middleware of tool phase completion (devtools emits aggregate events here)\n await this.middlewareRunner.runOnToolPhaseComplete(this.middlewareCtx, {\n toolCalls,\n results: executionResult.results,\n needsApproval: executionResult.needsApproval,\n needsClientExecution: executionResult.needsClientExecution,\n })\n\n if (\n executionResult.needsApproval.length > 0 ||\n executionResult.needsClientExecution.length > 0\n ) {\n if (executionResult.results.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )) {\n yield chunk\n }\n }\n\n for (const chunk of this.buildApprovalChunks(\n executionResult.needsApproval,\n finishEvent,\n )) {\n yield chunk\n }\n\n for (const chunk of this.buildClientToolChunks(\n executionResult.needsClientExecution,\n finishEvent,\n )) {\n yield chunk\n }\n\n this.setToolPhase('wait')\n return\n }\n\n const toolResultChunks = this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )\n\n for (const chunk of toolResultChunks) {\n yield chunk\n }\n\n // Refresh tools if lazy tools were discovered in this batch\n if (this.lazyToolManager.hasNewlyDiscoveredTools()) {\n this.tools = this.lazyToolManager.getActiveTools()\n this.toolCallManager = new ToolCallManager(this.tools)\n this.setToolPhase('continue')\n return\n }\n\n this.toolCallManager.clear()\n\n this.setToolPhase('continue')\n }\n\n private shouldExecuteToolPhase(): boolean {\n return (\n this.finishedEvent?.finishReason === 'tool_calls' &&\n this.tools.length > 0 &&\n this.toolCallManager.hasToolCalls()\n )\n }\n\n private addAssistantToolCallMessage(toolCalls: Array<ToolCall>): void {\n this.messages = [\n ...this.messages,\n {\n role: 'assistant',\n content: this.accumulatedContent || null,\n toolCalls,\n },\n ]\n }\n\n /**\n * Extract client state (approvals and client tool results) from original messages.\n * This is called in the constructor BEFORE converting to ModelMessage format,\n * because the parts array (which contains approval state) is lost during conversion.\n */\n private extractClientStateFromOriginalMessages(\n originalMessages: Array<any>,\n ): {\n approvals: Map<string, boolean>\n clientToolResults: Map<string, any>\n } {\n const approvals = new Map<string, boolean>()\n const clientToolResults = new Map<string, any>()\n\n for (const message of originalMessages) {\n // Check for UIMessage format (parts array) - extract client tool results and approvals\n if (message.role === 'assistant' && message.parts) {\n for (const part of message.parts) {\n if (part.type === 'tool-call') {\n // Extract client tool results (tools without approval that have output)\n if (part.output !== undefined && !part.approval) {\n clientToolResults.set(part.id, part.output)\n }\n // Extract approval responses from UIMessage format parts\n if (\n part.approval?.id &&\n part.approval?.approved !== undefined &&\n part.state === 'approval-responded'\n ) {\n approvals.set(part.approval.id, part.approval.approved)\n }\n }\n }\n }\n }\n\n return { approvals, clientToolResults }\n }\n\n private collectClientState(): {\n approvals: Map<string, boolean>\n clientToolResults: Map<string, any>\n } {\n // Start with the initial client state extracted from original messages\n const approvals = new Map(this.initialApprovals)\n const clientToolResults = new Map(this.initialClientToolResults)\n\n // Also check current messages for any additional tool results (from server tools)\n for (const message of this.messages) {\n // Check for ModelMessage format (role: 'tool' messages contain tool results)\n // This handles results sent back from the client after executing client-side tools\n if (message.role === 'tool' && message.toolCallId) {\n // Parse content back to original output (was stringified by uiMessageToModelMessages)\n let output: unknown\n try {\n output = JSON.parse(message.content as string)\n } catch {\n output = message.content\n }\n // Skip approval response messages (they have pendingExecution marker)\n // These are NOT real client tool results — they are synthetic tool messages\n // created by uiMessageToModelMessages for approved-but-not-yet-executed tools.\n // Treating them as results would prevent the server from requesting actual\n // client-side execution after approval (see GitHub issue #225).\n if (\n output &&\n typeof output === 'object' &&\n (output as any).pendingExecution === true\n ) {\n continue\n }\n clientToolResults.set(message.toolCallId, output)\n }\n }\n\n return { approvals, clientToolResults }\n }\n\n private buildApprovalChunks(\n approvals: Array<ApprovalRequest>,\n finishEvent: RunFinishedEvent,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const approval of approvals) {\n chunks.push({\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: finishEvent.model,\n name: 'approval-requested',\n value: {\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n input: approval.input,\n approval: {\n id: approval.approvalId,\n needsApproval: true,\n },\n },\n })\n }\n\n return chunks\n }\n\n private buildClientToolChunks(\n clientRequests: Array<ClientToolRequest>,\n finishEvent: RunFinishedEvent,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const clientTool of clientRequests) {\n chunks.push({\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: finishEvent.model,\n name: 'tool-input-available',\n value: {\n toolCallId: clientTool.toolCallId,\n toolName: clientTool.toolName,\n input: clientTool.input,\n },\n })\n }\n\n return chunks\n }\n\n private buildToolResultChunks(\n results: Array<ToolResult>,\n finishEvent: RunFinishedEvent,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const result of results) {\n const content = JSON.stringify(result.result)\n\n chunks.push({\n type: 'TOOL_CALL_END',\n timestamp: Date.now(),\n model: finishEvent.model,\n toolCallId: result.toolCallId,\n toolName: result.toolName,\n result: content,\n })\n\n this.messages = [\n ...this.messages,\n {\n role: 'tool',\n content,\n toolCallId: result.toolCallId,\n },\n ]\n }\n\n return chunks\n }\n\n private getPendingToolCallsFromMessages(): Array<ToolCall> {\n // Build a set of completed tool IDs, but exclude tools with pendingExecution marker\n // (these are approved tools that still need to execute)\n const completedToolIds = new Set<string>()\n\n for (const message of this.messages) {\n if (message.role === 'tool' && message.toolCallId) {\n // Check if this is an approval response with pendingExecution marker\n let hasPendingExecution = false\n if (typeof message.content === 'string') {\n try {\n const parsed = JSON.parse(message.content)\n if (parsed.pendingExecution === true) {\n hasPendingExecution = true\n }\n } catch {\n // Not JSON, treat as regular tool result\n }\n }\n\n // Only mark as complete if NOT pending execution\n if (!hasPendingExecution) {\n completedToolIds.add(message.toolCallId)\n }\n }\n }\n\n const pending: Array<ToolCall> = []\n\n for (const message of this.messages) {\n if (message.role === 'assistant' && message.toolCalls) {\n for (const toolCall of message.toolCalls) {\n if (!completedToolIds.has(toolCall.id)) {\n pending.push(toolCall)\n }\n }\n }\n }\n\n return pending\n }\n\n private createSyntheticFinishedEvent(): RunFinishedEvent {\n return {\n type: 'RUN_FINISHED',\n runId: this.createId('pending'),\n model: this.params.model,\n timestamp: Date.now(),\n finishReason: 'tool_calls',\n }\n }\n\n private shouldContinue(): boolean {\n if (this.cyclePhase === 'executeToolCalls') {\n return true\n }\n\n return (\n this.loopStrategy({\n iterationCount: this.iterationCount,\n messages: this.messages,\n finishReason: this.lastFinishReason,\n }) && this.toolPhase === 'continue'\n )\n }\n\n private isAborted(): boolean {\n return !!this.effectiveSignal?.aborted\n }\n\n private isMiddlewareAborted(): boolean {\n return !!this.middlewareAbortController?.signal.aborted\n }\n\n private isCancelled(): boolean {\n return this.isAborted() || this.isMiddlewareAborted()\n }\n\n private buildMiddlewareConfig(): ChatMiddlewareConfig {\n return {\n messages: this.messages,\n systemPrompts: [...this.systemPrompts],\n tools: [...this.tools],\n temperature: this.params.temperature,\n topP: this.params.topP,\n maxTokens: this.params.maxTokens,\n metadata: this.params.metadata,\n modelOptions: this.params.modelOptions,\n }\n }\n\n private applyMiddlewareConfig(config: ChatMiddlewareConfig): void {\n this.messages = config.messages\n this.systemPrompts = config.systemPrompts\n this.tools = config.tools\n this.params = {\n ...this.params,\n temperature: config.temperature,\n topP: config.topP,\n maxTokens: config.maxTokens,\n metadata: config.metadata,\n modelOptions: config.modelOptions,\n }\n\n // Sync context fields that depend on config\n this.middlewareCtx.messages = this.messages\n this.middlewareCtx.systemPrompts = this.systemPrompts\n this.middlewareCtx.hasTools = this.tools.length > 0\n this.middlewareCtx.toolNames = this.tools.map((t) => t.name)\n this.middlewareCtx.modelOptions = config.modelOptions\n }\n\n private setToolPhase(phase: ToolPhaseResult): void {\n this.toolPhase = phase\n }\n\n /**\n * Drain an executeToolCalls async generator, yielding any CustomEvent chunks\n * and returning the final ExecuteToolCallsResult.\n */\n private async *drainToolCallGenerator(\n generator: AsyncGenerator<\n CustomEvent,\n {\n results: Array<ToolResult>\n needsApproval: Array<ApprovalRequest>\n needsClientExecution: Array<ClientToolRequest>\n },\n void\n >,\n ): AsyncGenerator<\n StreamChunk,\n {\n results: Array<ToolResult>\n needsApproval: Array<ApprovalRequest>\n needsClientExecution: Array<ClientToolRequest>\n },\n void\n > {\n let next = await generator.next()\n while (!next.done) {\n yield next.value\n next = await generator.next()\n }\n return next.value\n }\n\n private createCustomEventChunk(\n eventName: string,\n value: Record<string, any>,\n ): CustomEvent {\n return {\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: this.params.model,\n name: eventName,\n value,\n }\n }\n\n private createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n }\n}\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.\n *\n * This activity supports four modes:\n * 1. **Streaming agentic text**: Stream responses with automatic tool execution\n * 2. **Streaming one-shot text**: Simple streaming request/response without tools\n * 3. **Non-streaming text**: Returns collected text as a string (stream: false)\n * 4. **Agentic structured output**: Run tools, then return structured data\n *\n * @example Full agentic text (streaming with tools)\n * ```ts\n * import { chat } from '@tanstack/ai'\n * import { openaiText } from '@tanstack/ai-openai'\n *\n * for await (const chunk of chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'What is the weather?' }],\n * tools: [weatherTool]\n * })) {\n * if (chunk.type === 'content') {\n * console.log(chunk.delta)\n * }\n * }\n * ```\n *\n * @example One-shot text (streaming without tools)\n * ```ts\n * for await (const chunk of chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }]\n * })) {\n * console.log(chunk)\n * }\n * ```\n *\n * @example Non-streaming text (stream: false)\n * ```ts\n * const text = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }],\n * stream: false\n * })\n * // text is a string with the full response\n * ```\n *\n * @example Agentic structured output (tools + structured response)\n * ```ts\n * import { z } from 'zod'\n *\n * const result = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Research and summarize the topic' }],\n * tools: [researchTool, analyzeTool],\n * outputSchema: z.object({\n * summary: z.string(),\n * keyPoints: z.array(z.string())\n * })\n * })\n * // result is { summary: string, keyPoints: string[] }\n * ```\n */\nexport function chat<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined = undefined,\n TStream extends boolean = true,\n>(\n options: TextActivityOptions<TAdapter, TSchema, TStream>,\n): TextActivityResult<TSchema, TStream> {\n const { outputSchema, stream } = options\n\n // If outputSchema is provided, run agentic structured output\n if (outputSchema) {\n return runAgenticStructuredOutput(\n options as unknown as TextActivityOptions<\n AnyTextAdapter,\n SchemaInput,\n boolean\n >,\n ) as TextActivityResult<TSchema, TStream>\n }\n\n // If stream is explicitly false, run non-streaming text\n if (stream === false) {\n return runNonStreamingText(\n options as unknown as TextActivityOptions<\n AnyTextAdapter,\n undefined,\n false\n >,\n ) as TextActivityResult<TSchema, TStream>\n }\n\n // Otherwise, run streaming text (default)\n return runStreamingText(\n options as unknown as TextActivityOptions<AnyTextAdapter, undefined, true>,\n ) as TextActivityResult<TSchema, TStream>\n}\n\n/**\n * Run streaming text (agentic or one-shot depending on tools)\n */\nasync function* runStreamingText(\n options: TextActivityOptions<AnyTextAdapter, undefined, true>,\n): AsyncIterable<StreamChunk> {\n const { adapter, middleware, context, ...textOptions } = options\n const model = adapter.model\n\n const engine = new TextEngine({\n adapter,\n params: { ...textOptions, model } as TextOptions<\n Record<string, any>,\n Record<string, any>\n >,\n middleware,\n context,\n })\n\n for await (const chunk of engine.run()) {\n yield chunk\n }\n}\n\n/**\n * Run non-streaming text - collects all content and returns as a string.\n * Runs the full agentic loop (if tools are provided) but returns collected text.\n */\nfunction runNonStreamingText(\n options: TextActivityOptions<AnyTextAdapter, undefined, false>,\n): Promise<string> {\n // Run the streaming text and collect all text using streamToText\n const stream = runStreamingText(\n options as unknown as TextActivityOptions<AnyTextAdapter, undefined, true>,\n )\n\n return streamToText(stream)\n}\n\n/**\n * Run agentic structured output:\n * 1. Execute the full agentic loop (with tools)\n * 2. Once complete, call adapter.structuredOutput with the conversation context\n * 3. Validate and return the structured result\n */\nasync function runAgenticStructuredOutput<TSchema extends SchemaInput>(\n options: TextActivityOptions<AnyTextAdapter, TSchema, boolean>,\n): Promise<InferSchemaType<TSchema>> {\n const { adapter, outputSchema, middleware, context, ...textOptions } = options\n const model = adapter.model\n\n if (!outputSchema) {\n throw new Error('outputSchema is required for structured output')\n }\n\n // Create the engine and run the agentic loop\n const engine = new TextEngine({\n adapter,\n params: { ...textOptions, model } as TextOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n middleware,\n context,\n })\n\n // Consume the stream to run the agentic loop\n for await (const _chunk of engine.run()) {\n // Just consume the stream to execute the agentic loop\n }\n\n // Get the final messages from the engine (includes tool results)\n const finalMessages = engine.getMessages()\n\n // Build text options for structured output, excluding tools since\n // the agentic loop is complete and we only need the final response\n const {\n tools: _tools,\n agentLoopStrategy: _als,\n ...structuredTextOptions\n } = textOptions\n\n // Convert the schema to JSON Schema before passing to the adapter\n const jsonSchema = convertSchemaToJsonSchema(outputSchema)\n if (!jsonSchema) {\n throw new Error('Failed to convert output schema to JSON Schema')\n }\n\n // Call the adapter's structured output method with the conversation context\n // The adapter receives JSON Schema and can apply vendor-specific patches\n const result = await adapter.structuredOutput({\n chatOptions: {\n ...structuredTextOptions,\n model,\n messages: finalMessages,\n },\n outputSchema: jsonSchema,\n })\n\n // Validate the result against the schema if it's a Standard Schema\n if (isStandardSchema(outputSchema)) {\n return parseWithStandardSchema<InferSchemaType<TSchema>>(\n outputSchema,\n result.data,\n )\n }\n\n // For plain JSON Schema, return the data as-is\n return result.data as InferSchemaType<TSchema>\n}\n\n// Re-export adapter types\nexport type {\n TextAdapter,\n TextAdapterConfig,\n StructuredOutputOptions,\n StructuredOutputResult,\n} from './adapter'\nexport { BaseTextAdapter } from './adapter'\n"],"names":["maxIterationsStrategy","transformedConfig"],"mappings":";;;;;;;;AA0DO,MAAM,OAAO;AA8Hb,SAAS,kBAKd,SACiD;AACjD,SAAO;AACT;AAuCA,MAAM,WAGJ;AAAA,EAuCA,YAAY,QAA6C;AAxBzD,SAAQ,iBAAiB;AACzB,SAAQ,mBAAkC;AAC1C,SAAQ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B;"}
1
+ {"version":3,"file":"index.js","sources":["../../../../src/activities/chat/index.ts"],"sourcesContent":["/**\n * Text Activity\n *\n * Handles agentic text generation, one-shot text generation, and agentic structured output.\n * This is a self-contained module with implementation, types, and JSDoc.\n */\n\nimport { devtoolsMiddleware } from '@tanstack/ai-event-client'\nimport { streamToText } from '../../stream-to-response.js'\nimport { LazyToolManager } from './tools/lazy-tool-manager'\nimport {\n MiddlewareAbortError,\n ToolCallManager,\n executeToolCalls,\n} from './tools/tool-calls'\nimport {\n convertSchemaToJsonSchema,\n isStandardSchema,\n parseWithStandardSchema,\n} from './tools/schema-converter'\nimport { maxIterations as maxIterationsStrategy } from './agent-loop-strategies'\nimport { convertMessagesToModelMessages } from './messages'\nimport { MiddlewareRunner } from './middleware/compose'\nimport type {\n ApprovalRequest,\n ClientToolRequest,\n ToolResult,\n} from './tools/tool-calls'\nimport type { AnyTextAdapter } from './adapter'\nimport type {\n AgentLoopStrategy,\n ConstrainedModelMessage,\n CustomEvent,\n InferSchemaType,\n ModelMessage,\n RunFinishedEvent,\n SchemaInput,\n StreamChunk,\n TextMessageContentEvent,\n TextOptions,\n Tool,\n ToolCall,\n ToolCallArgsEvent,\n ToolCallEndEvent,\n ToolCallStartEvent,\n} from '../../types'\nimport type {\n ChatMiddleware,\n ChatMiddlewareConfig,\n ChatMiddlewareContext,\n ChatMiddlewarePhase,\n} from './middleware/types'\n\n// ===========================\n// Activity Kind\n// ===========================\n\n/** The adapter kind this activity handles */\nexport const kind = 'text' as const\n\n// ===========================\n// Activity Options Type\n// ===========================\n\n/**\n * Options for the text activity.\n * Types are extracted directly from the adapter (which has pre-resolved generics).\n *\n * @template TAdapter - The text adapter type (created by a provider function)\n * @template TSchema - Optional Standard Schema for structured output\n * @template TStream - Whether to stream the output (default: true)\n */\nexport interface TextActivityOptions<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined,\n TStream extends boolean,\n> {\n /** The text adapter to use (created by a provider function like openaiText('gpt-4o')) */\n adapter: TAdapter\n /** Conversation messages - content types are constrained by the adapter's input modalities and metadata */\n messages?: Array<\n ConstrainedModelMessage<{\n inputModalities: TAdapter['~types']['inputModalities']\n messageMetadataByModality: TAdapter['~types']['messageMetadataByModality']\n }>\n >\n /** System prompts to prepend to the conversation */\n systemPrompts?: TextOptions['systemPrompts']\n /** Tools for function calling (auto-executed when called) */\n tools?: TextOptions['tools']\n /** Controls the randomness of the output. Higher values make output more random. Range: [0.0, 2.0] */\n temperature?: TextOptions['temperature']\n /** Nucleus sampling parameter. The model considers tokens with topP probability mass. */\n topP?: TextOptions['topP']\n /** The maximum number of tokens to generate in the response. */\n maxTokens?: TextOptions['maxTokens']\n /** Additional metadata to attach to the request. */\n metadata?: TextOptions['metadata']\n /** Model-specific provider options (type comes from adapter) */\n modelOptions?: TAdapter['~types']['providerOptions']\n /** AbortController for cancellation */\n abortController?: TextOptions['abortController']\n /** Strategy for controlling the agent loop */\n agentLoopStrategy?: TextOptions['agentLoopStrategy']\n /** Unique conversation identifier for tracking */\n conversationId?: TextOptions['conversationId']\n /**\n * Optional Standard Schema for structured output.\n * When provided, the activity will:\n * 1. Run the full agentic loop (executing tools as needed)\n * 2. Once complete, return a Promise with the parsed output matching the schema\n *\n * Supports any Standard Schema compliant library (Zod v4+, ArkType, Valibot, etc.)\n *\n * @example\n * ```ts\n * const result = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Generate a person' }],\n * outputSchema: z.object({ name: z.string(), age: z.number() })\n * })\n * // result is { name: string, age: number }\n * ```\n */\n outputSchema?: TSchema\n /**\n * Whether to stream the text result.\n * When true (default), returns an AsyncIterable<StreamChunk> for streaming output.\n * When false, returns a Promise<string> with the collected text content.\n *\n * Note: If outputSchema is provided, this option is ignored and the result\n * is always a Promise<InferSchemaType<TSchema>>.\n *\n * @default true\n *\n * @example Non-streaming text\n * ```ts\n * const text = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }],\n * stream: false\n * })\n * // text is a string with the full response\n * ```\n */\n stream?: TStream\n /**\n * Optional middleware array for observing/transforming chat behavior.\n * Middleware hooks are called in array order. See {@link ChatMiddleware} for available hooks.\n *\n * @example\n * ```ts\n * const stream = chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [...],\n * middleware: [loggingMiddleware, redactionMiddleware],\n * })\n * ```\n */\n middleware?: Array<ChatMiddleware>\n /**\n * Opaque user-provided context value passed to middleware hooks.\n * Can be used to pass request-scoped data (e.g., user ID, request context).\n */\n context?: unknown\n}\n\n// ===========================\n// Chat Options Helper\n// ===========================\n\n/**\n * Create typed options for the chat() function without executing.\n * This is useful for pre-defining configurations with full type inference.\n *\n * @example\n * ```ts\n * const chatOptions = createChatOptions({\n * adapter: anthropicText('claude-sonnet-4-5'),\n * })\n *\n * const stream = chat({ ...chatOptions, messages })\n * ```\n */\nexport function createChatOptions<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined = undefined,\n TStream extends boolean = true,\n>(\n options: TextActivityOptions<TAdapter, TSchema, TStream>,\n): TextActivityOptions<TAdapter, TSchema, TStream> {\n return options\n}\n\n// ===========================\n// Activity Result Type\n// ===========================\n\n/**\n * Result type for the text activity.\n * - If outputSchema is provided: Promise<InferSchemaType<TSchema>>\n * - If stream is false: Promise<string>\n * - Otherwise (stream is true, default): AsyncIterable<StreamChunk>\n */\nexport type TextActivityResult<\n TSchema extends SchemaInput | undefined,\n TStream extends boolean = true,\n> = TSchema extends SchemaInput\n ? Promise<InferSchemaType<TSchema>>\n : TStream extends false\n ? Promise<string>\n : AsyncIterable<StreamChunk>\n\n// ===========================\n// ChatEngine Implementation\n// ===========================\n\ninterface TextEngineConfig<\n TAdapter extends AnyTextAdapter,\n TParams extends TextOptions<any, any> = TextOptions<any>,\n> {\n adapter: TAdapter\n systemPrompts?: Array<string>\n params: TParams\n middleware?: Array<ChatMiddleware>\n context?: unknown\n}\n\ntype ToolPhaseResult = 'continue' | 'stop' | 'wait'\ntype CyclePhase = 'processText' | 'executeToolCalls'\n\nclass TextEngine<\n TAdapter extends AnyTextAdapter,\n TParams extends TextOptions<any, any> = TextOptions<any>,\n> {\n private readonly adapter: TAdapter\n private params: TParams\n private systemPrompts: Array<string>\n private tools: Array<Tool>\n private readonly loopStrategy: AgentLoopStrategy\n private toolCallManager: ToolCallManager\n private readonly lazyToolManager: LazyToolManager\n private readonly initialMessageCount: number\n private readonly requestId: string\n private readonly streamId: string\n private readonly effectiveRequest?: Request | RequestInit\n private readonly effectiveSignal?: AbortSignal\n\n private messages: Array<ModelMessage>\n private iterationCount = 0\n private lastFinishReason: string | null = null\n private streamStartTime = 0\n private totalChunkCount = 0\n private currentMessageId: string | null = null\n private accumulatedContent = ''\n private eventOptions?: Record<string, unknown>\n private eventToolNames?: Array<string>\n private finishedEvent: RunFinishedEvent | null = null\n private earlyTermination = false\n private toolPhase: ToolPhaseResult = 'continue'\n private cyclePhase: CyclePhase = 'processText'\n // Client state extracted from initial messages (before conversion to ModelMessage)\n private readonly initialApprovals: Map<string, boolean>\n private readonly initialClientToolResults: Map<string, any>\n\n // Middleware support\n private readonly middlewareRunner: MiddlewareRunner\n private readonly middlewareCtx: ChatMiddlewareContext\n private readonly deferredPromises: Array<Promise<unknown>> = []\n private abortReason?: string\n private middlewareAbortController?: AbortController\n private terminalHookCalled = false\n\n constructor(config: TextEngineConfig<TAdapter, TParams>) {\n this.adapter = config.adapter\n this.params = config.params\n this.systemPrompts = config.params.systemPrompts || []\n this.loopStrategy =\n config.params.agentLoopStrategy || maxIterationsStrategy(5)\n this.initialMessageCount = config.params.messages.length\n\n // Extract client state (approvals, client tool results) from original messages BEFORE conversion\n // This preserves UIMessage parts data that would be lost during conversion to ModelMessage\n const { approvals, clientToolResults } =\n this.extractClientStateFromOriginalMessages(\n config.params.messages as Array<any>,\n )\n this.initialApprovals = approvals\n this.initialClientToolResults = clientToolResults\n\n // Convert messages to ModelMessage format (handles both UIMessage and ModelMessage input)\n // This ensures consistent internal format regardless of what the client sends\n this.messages = convertMessagesToModelMessages(\n config.params.messages as Array<any>,\n )\n\n // Initialize lazy tool manager after messages are converted (needs message history for scanning)\n this.lazyToolManager = new LazyToolManager(\n config.params.tools || [],\n this.messages,\n )\n this.tools = this.lazyToolManager.getActiveTools()\n this.toolCallManager = new ToolCallManager(this.tools)\n this.requestId = this.createId('chat')\n this.streamId = this.createId('stream')\n this.effectiveRequest = config.params.abortController\n ? { signal: config.params.abortController.signal }\n : undefined\n this.effectiveSignal = config.params.abortController?.signal\n\n // Initialize middleware — devtools middleware is always first\n const allMiddleware = [devtoolsMiddleware(), ...(config.middleware || [])]\n this.middlewareRunner = new MiddlewareRunner(allMiddleware)\n this.middlewareAbortController = new AbortController()\n this.middlewareCtx = {\n requestId: this.requestId,\n streamId: this.streamId,\n conversationId: config.params.conversationId,\n phase: 'init' as ChatMiddlewarePhase,\n iteration: 0,\n chunkIndex: 0,\n signal: this.effectiveSignal,\n abort: (reason?: string) => {\n this.abortReason = reason\n this.middlewareAbortController?.abort(reason)\n },\n context: config.context,\n defer: (promise: Promise<unknown>) => {\n this.deferredPromises.push(promise)\n },\n // Provider / adapter info\n provider: config.adapter.name,\n model: config.params.model,\n source: 'server',\n streaming: true,\n // Config-derived (updated in beforeRun and applyMiddlewareConfig)\n systemPrompts: this.systemPrompts,\n toolNames: undefined,\n options: undefined,\n modelOptions: config.params.modelOptions,\n // Computed\n messageCount: this.initialMessageCount,\n hasTools: this.tools.length > 0,\n // Mutable per-iteration\n currentMessageId: null,\n accumulatedContent: '',\n // References\n messages: this.messages,\n createId: (prefix: string) => this.createId(prefix),\n }\n }\n\n /** Get the accumulated content after the chat loop completes */\n getAccumulatedContent(): string {\n return this.accumulatedContent\n }\n\n /** Get the final messages array after the chat loop completes */\n getMessages(): Array<ModelMessage> {\n return this.messages\n }\n\n async *run(): AsyncGenerator<StreamChunk> {\n this.beforeRun()\n\n try {\n // Run initial onConfig (phase = init)\n this.middlewareCtx.phase = 'init'\n const initialConfig = this.buildMiddlewareConfig()\n const transformedConfig = await this.middlewareRunner.runOnConfig(\n this.middlewareCtx,\n initialConfig,\n )\n this.applyMiddlewareConfig(transformedConfig)\n\n // Run onStart (devtools middleware emits text:request:started and initial messages here)\n await this.middlewareRunner.runOnStart(this.middlewareCtx)\n\n const pendingPhase = yield* this.checkForPendingToolCalls()\n if (pendingPhase === 'wait') {\n return\n }\n\n do {\n if (this.earlyTermination || this.isCancelled()) {\n return\n }\n\n await this.beginCycle()\n\n if (this.cyclePhase === 'processText') {\n // Run onConfig before each model call (phase = beforeModel)\n this.middlewareCtx.phase = 'beforeModel'\n this.middlewareCtx.iteration = this.iterationCount\n const iterConfig = this.buildMiddlewareConfig()\n const transformedConfig = await this.middlewareRunner.runOnConfig(\n this.middlewareCtx,\n iterConfig,\n )\n this.applyMiddlewareConfig(transformedConfig)\n\n yield* this.streamModelResponse()\n } else {\n yield* this.processToolCalls()\n }\n\n this.endCycle()\n } while (this.shouldContinue())\n\n // Call terminal onFinish hook (skip when waiting for client — stream is paused, not finished)\n if (!this.terminalHookCalled && this.toolPhase !== 'wait') {\n this.terminalHookCalled = true\n await this.middlewareRunner.runOnFinish(this.middlewareCtx, {\n finishReason: this.lastFinishReason,\n duration: Date.now() - this.streamStartTime,\n content: this.accumulatedContent,\n usage: this.finishedEvent?.usage,\n })\n }\n } catch (error: unknown) {\n if (!this.terminalHookCalled) {\n this.terminalHookCalled = true\n if (error instanceof MiddlewareAbortError) {\n // Middleware abort decision — call onAbort, not onError\n this.abortReason = error.message\n await this.middlewareRunner.runOnAbort(this.middlewareCtx, {\n reason: error.message,\n duration: Date.now() - this.streamStartTime,\n })\n } else {\n // Genuine error — call onError\n await this.middlewareRunner.runOnError(this.middlewareCtx, {\n error,\n duration: Date.now() - this.streamStartTime,\n })\n }\n }\n // Don't rethrow middleware abort errors — the run just stops gracefully\n if (!(error instanceof MiddlewareAbortError)) {\n throw error\n }\n } finally {\n // Check for abort terminal hook\n if (!this.terminalHookCalled && this.isCancelled()) {\n this.terminalHookCalled = true\n await this.middlewareRunner.runOnAbort(this.middlewareCtx, {\n reason: this.abortReason,\n duration: Date.now() - this.streamStartTime,\n })\n }\n\n // Await deferred promises (non-blocking side effects)\n if (this.deferredPromises.length > 0) {\n await Promise.allSettled(this.deferredPromises)\n }\n }\n }\n\n private beforeRun(): void {\n this.streamStartTime = Date.now()\n const { tools, temperature, topP, maxTokens, metadata } = this.params\n\n // Gather flattened options into an object for context\n const options: Record<string, unknown> = {}\n if (temperature !== undefined) options.temperature = temperature\n if (topP !== undefined) options.topP = topP\n if (maxTokens !== undefined) options.maxTokens = maxTokens\n if (metadata !== undefined) options.metadata = metadata\n\n this.eventOptions = Object.keys(options).length > 0 ? options : undefined\n this.eventToolNames = tools?.map((t) => t.name)\n\n // Update middleware context with computed fields\n this.middlewareCtx.options = this.eventOptions\n this.middlewareCtx.toolNames = this.eventToolNames\n }\n\n private async beginCycle(): Promise<void> {\n if (this.cyclePhase === 'processText') {\n await this.beginIteration()\n }\n }\n\n private endCycle(): void {\n if (this.cyclePhase === 'processText') {\n this.cyclePhase = 'executeToolCalls'\n return\n }\n\n this.cyclePhase = 'processText'\n this.iterationCount++\n }\n\n private async beginIteration(): Promise<void> {\n this.currentMessageId = this.createId('msg')\n this.accumulatedContent = ''\n this.finishedEvent = null\n\n // Update mutable context fields\n this.middlewareCtx.currentMessageId = this.currentMessageId\n this.middlewareCtx.accumulatedContent = ''\n\n // Notify middleware of new iteration (devtools emits assistant message:created here)\n await this.middlewareRunner.runOnIteration(this.middlewareCtx, {\n iteration: this.iterationCount,\n messageId: this.currentMessageId,\n })\n }\n\n private async *streamModelResponse(): AsyncGenerator<StreamChunk> {\n const { temperature, topP, maxTokens, metadata, modelOptions } = this.params\n const tools = this.tools\n\n // Convert tool schemas to JSON Schema before passing to adapter\n const toolsWithJsonSchemas = tools.map((tool) => ({\n ...tool,\n inputSchema: tool.inputSchema\n ? convertSchemaToJsonSchema(tool.inputSchema)\n : undefined,\n outputSchema: tool.outputSchema\n ? convertSchemaToJsonSchema(tool.outputSchema)\n : undefined,\n }))\n\n this.middlewareCtx.phase = 'modelStream'\n\n for await (const chunk of this.adapter.chatStream({\n model: this.params.model,\n messages: this.messages,\n tools: toolsWithJsonSchemas,\n temperature,\n topP,\n maxTokens,\n metadata,\n request: this.effectiveRequest,\n modelOptions,\n systemPrompts: this.systemPrompts,\n })) {\n if (this.isCancelled()) {\n break\n }\n\n this.totalChunkCount++\n\n // Pipe chunk through middleware (devtools middleware observes and emits events)\n const outputChunks = await this.middlewareRunner.runOnChunk(\n this.middlewareCtx,\n chunk,\n )\n for (const outputChunk of outputChunks) {\n yield outputChunk\n this.handleStreamChunk(outputChunk)\n this.middlewareCtx.chunkIndex++\n }\n\n // Handle usage via middleware\n if (chunk.type === 'RUN_FINISHED' && chunk.usage) {\n await this.middlewareRunner.runOnUsage(this.middlewareCtx, chunk.usage)\n }\n\n if (this.earlyTermination) {\n break\n }\n }\n }\n\n private handleStreamChunk(chunk: StreamChunk): void {\n switch (chunk.type) {\n // AG-UI Events\n case 'TEXT_MESSAGE_CONTENT':\n this.handleTextMessageContentEvent(chunk)\n break\n case 'TOOL_CALL_START':\n this.handleToolCallStartEvent(chunk)\n break\n case 'TOOL_CALL_ARGS':\n this.handleToolCallArgsEvent(chunk)\n break\n case 'TOOL_CALL_END':\n this.handleToolCallEndEvent(chunk)\n break\n case 'RUN_FINISHED':\n this.handleRunFinishedEvent(chunk)\n break\n case 'RUN_ERROR':\n this.handleRunErrorEvent(chunk)\n break\n case 'STEP_FINISHED':\n this.handleStepFinishedEvent(chunk)\n break\n\n default:\n // RUN_STARTED, TEXT_MESSAGE_START, TEXT_MESSAGE_END, STEP_STARTED,\n // STATE_SNAPSHOT, STATE_DELTA, CUSTOM\n // - no special handling needed in chat activity\n break\n }\n }\n\n // ===========================\n // AG-UI Event Handlers\n // ===========================\n\n private handleTextMessageContentEvent(chunk: TextMessageContentEvent): void {\n if (chunk.content) {\n this.accumulatedContent = chunk.content\n } else {\n this.accumulatedContent += chunk.delta\n }\n this.middlewareCtx.accumulatedContent = this.accumulatedContent\n }\n\n private handleToolCallStartEvent(chunk: ToolCallStartEvent): void {\n this.toolCallManager.addToolCallStartEvent(chunk)\n }\n\n private handleToolCallArgsEvent(chunk: ToolCallArgsEvent): void {\n this.toolCallManager.addToolCallArgsEvent(chunk)\n }\n\n private handleToolCallEndEvent(chunk: ToolCallEndEvent): void {\n this.toolCallManager.completeToolCall(chunk)\n }\n\n private handleRunFinishedEvent(chunk: RunFinishedEvent): void {\n this.finishedEvent = chunk\n this.lastFinishReason = chunk.finishReason\n }\n\n private handleRunErrorEvent(\n _chunk: Extract<StreamChunk, { type: 'RUN_ERROR' }>,\n ): void {\n this.earlyTermination = true\n }\n\n private handleStepFinishedEvent(\n _chunk: Extract<StreamChunk, { type: 'STEP_FINISHED' }>,\n ): void {\n // State tracking for STEP_FINISHED is handled by middleware\n }\n\n private async *checkForPendingToolCalls(): AsyncGenerator<\n StreamChunk,\n ToolPhaseResult,\n void\n > {\n const pendingToolCalls = this.getPendingToolCallsFromMessages()\n if (pendingToolCalls.length === 0) {\n return 'continue'\n }\n\n const finishEvent = this.createSyntheticFinishedEvent()\n\n // Handle undiscovered lazy tool calls with self-correcting error messages\n const undiscoveredLazyResults: Array<ToolResult> = []\n const executablePendingCalls = pendingToolCalls.filter((tc) => {\n if (this.lazyToolManager.isUndiscoveredLazyTool(tc.function.name)) {\n undiscoveredLazyResults.push({\n toolCallId: tc.id,\n toolName: tc.function.name,\n result: {\n error: this.lazyToolManager.getUndiscoveredToolError(\n tc.function.name,\n ),\n },\n state: 'output-error',\n })\n return false\n }\n return true\n })\n\n if (undiscoveredLazyResults.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n undiscoveredLazyResults,\n finishEvent,\n )) {\n yield chunk\n }\n }\n\n if (executablePendingCalls.length === 0) {\n return 'continue'\n }\n\n const { approvals, clientToolResults } = this.collectClientState()\n\n const generator = executeToolCalls(\n executablePendingCalls,\n this.tools,\n approvals,\n clientToolResults,\n (eventName, data) => this.createCustomEventChunk(eventName, data),\n {\n onBeforeToolCall: async (toolCall, tool, args) => {\n const hookCtx = {\n toolCall,\n tool,\n args,\n toolName: toolCall.function.name,\n toolCallId: toolCall.id,\n }\n return this.middlewareRunner.runOnBeforeToolCall(\n this.middlewareCtx,\n hookCtx,\n )\n },\n onAfterToolCall: async (info) => {\n await this.middlewareRunner.runOnAfterToolCall(\n this.middlewareCtx,\n info,\n )\n },\n },\n )\n\n // Consume the async generator, yielding custom events and collecting the return value\n const executionResult = yield* this.drainToolCallGenerator(generator)\n\n // Check if middleware aborted during pending tool execution\n if (this.isMiddlewareAborted()) {\n this.setToolPhase('stop')\n return 'stop'\n }\n\n // Notify middleware of tool phase completion (devtools emits aggregate events here)\n await this.middlewareRunner.runOnToolPhaseComplete(this.middlewareCtx, {\n toolCalls: pendingToolCalls,\n results: executionResult.results,\n needsApproval: executionResult.needsApproval,\n needsClientExecution: executionResult.needsClientExecution,\n })\n\n // Build args lookup so buildToolResultChunks can emit TOOL_CALL_START +\n // TOOL_CALL_ARGS before TOOL_CALL_END during continuation re-executions.\n const argsMap = new Map<string, string>()\n for (const tc of pendingToolCalls) {\n argsMap.set(tc.id, tc.function.arguments)\n }\n\n if (\n executionResult.needsApproval.length > 0 ||\n executionResult.needsClientExecution.length > 0\n ) {\n if (executionResult.results.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n argsMap,\n )) {\n yield chunk\n }\n }\n\n for (const chunk of this.buildApprovalChunks(\n executionResult.needsApproval,\n finishEvent,\n )) {\n yield chunk\n }\n\n for (const chunk of this.buildClientToolChunks(\n executionResult.needsClientExecution,\n finishEvent,\n )) {\n yield chunk\n }\n\n this.setToolPhase('wait')\n return 'wait'\n }\n\n const toolResultChunks = this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n argsMap,\n )\n\n for (const chunk of toolResultChunks) {\n yield chunk\n }\n\n return 'continue'\n }\n\n private async *processToolCalls(): AsyncGenerator<StreamChunk, void, void> {\n if (!this.shouldExecuteToolPhase()) {\n this.setToolPhase('stop')\n return\n }\n\n const toolCalls = this.toolCallManager.getToolCalls()\n const finishEvent = this.finishedEvent\n\n if (!finishEvent || toolCalls.length === 0) {\n this.setToolPhase('stop')\n return\n }\n\n this.addAssistantToolCallMessage(toolCalls)\n\n // Handle undiscovered lazy tool calls with self-correcting error messages\n const undiscoveredLazyResults: Array<ToolResult> = []\n const executableToolCalls = toolCalls.filter((tc) => {\n if (this.lazyToolManager.isUndiscoveredLazyTool(tc.function.name)) {\n undiscoveredLazyResults.push({\n toolCallId: tc.id,\n toolName: tc.function.name,\n result: {\n error: this.lazyToolManager.getUndiscoveredToolError(\n tc.function.name,\n ),\n },\n state: 'output-error',\n })\n return false\n }\n return true\n })\n\n if (undiscoveredLazyResults.length > 0) {\n const finishEvt = this.finishedEvent!\n for (const chunk of this.buildToolResultChunks(\n undiscoveredLazyResults,\n finishEvt,\n )) {\n yield chunk\n }\n }\n\n if (executableToolCalls.length === 0) {\n // All tool calls were undiscovered lazy tools — errors emitted, continue loop\n this.toolCallManager.clear()\n this.setToolPhase('continue')\n return\n }\n this.middlewareCtx.phase = 'beforeTools'\n\n const { approvals, clientToolResults } = this.collectClientState()\n\n const generator = executeToolCalls(\n executableToolCalls,\n this.tools,\n approvals,\n clientToolResults,\n (eventName, data) => this.createCustomEventChunk(eventName, data),\n {\n onBeforeToolCall: async (toolCall, tool, args) => {\n const hookCtx = {\n toolCall,\n tool,\n args,\n toolName: toolCall.function.name,\n toolCallId: toolCall.id,\n }\n return this.middlewareRunner.runOnBeforeToolCall(\n this.middlewareCtx,\n hookCtx,\n )\n },\n onAfterToolCall: async (info) => {\n await this.middlewareRunner.runOnAfterToolCall(\n this.middlewareCtx,\n info,\n )\n },\n },\n )\n\n // Consume the async generator, yielding custom events and collecting the return value\n const executionResult = yield* this.drainToolCallGenerator(generator)\n\n this.middlewareCtx.phase = 'afterTools'\n\n // Check if middleware aborted during tool execution\n if (this.isMiddlewareAborted()) {\n this.setToolPhase('stop')\n return\n }\n\n // Notify middleware of tool phase completion (devtools emits aggregate events here)\n await this.middlewareRunner.runOnToolPhaseComplete(this.middlewareCtx, {\n toolCalls,\n results: executionResult.results,\n needsApproval: executionResult.needsApproval,\n needsClientExecution: executionResult.needsClientExecution,\n })\n\n if (\n executionResult.needsApproval.length > 0 ||\n executionResult.needsClientExecution.length > 0\n ) {\n if (executionResult.results.length > 0) {\n for (const chunk of this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )) {\n yield chunk\n }\n }\n\n for (const chunk of this.buildApprovalChunks(\n executionResult.needsApproval,\n finishEvent,\n )) {\n yield chunk\n }\n\n for (const chunk of this.buildClientToolChunks(\n executionResult.needsClientExecution,\n finishEvent,\n )) {\n yield chunk\n }\n\n this.setToolPhase('wait')\n return\n }\n\n const toolResultChunks = this.buildToolResultChunks(\n executionResult.results,\n finishEvent,\n )\n\n for (const chunk of toolResultChunks) {\n yield chunk\n }\n\n // Refresh tools if lazy tools were discovered in this batch\n if (this.lazyToolManager.hasNewlyDiscoveredTools()) {\n this.tools = this.lazyToolManager.getActiveTools()\n this.toolCallManager = new ToolCallManager(this.tools)\n this.setToolPhase('continue')\n return\n }\n\n this.toolCallManager.clear()\n\n this.setToolPhase('continue')\n }\n\n private shouldExecuteToolPhase(): boolean {\n return (\n this.finishedEvent?.finishReason === 'tool_calls' &&\n this.tools.length > 0 &&\n this.toolCallManager.hasToolCalls()\n )\n }\n\n private addAssistantToolCallMessage(toolCalls: Array<ToolCall>): void {\n this.messages = [\n ...this.messages,\n {\n role: 'assistant',\n content: this.accumulatedContent || null,\n toolCalls,\n },\n ]\n }\n\n /**\n * Extract client state (approvals and client tool results) from original messages.\n * This is called in the constructor BEFORE converting to ModelMessage format,\n * because the parts array (which contains approval state) is lost during conversion.\n */\n private extractClientStateFromOriginalMessages(\n originalMessages: Array<any>,\n ): {\n approvals: Map<string, boolean>\n clientToolResults: Map<string, any>\n } {\n const approvals = new Map<string, boolean>()\n const clientToolResults = new Map<string, any>()\n\n for (const message of originalMessages) {\n // Check for UIMessage format (parts array) - extract client tool results and approvals\n if (message.role === 'assistant' && message.parts) {\n for (const part of message.parts) {\n if (part.type === 'tool-call') {\n // Extract client tool results (tools without approval that have output)\n if (part.output !== undefined && !part.approval) {\n clientToolResults.set(part.id, part.output)\n }\n // Extract approval responses from UIMessage format parts\n if (\n part.approval?.id &&\n part.approval?.approved !== undefined &&\n part.state === 'approval-responded'\n ) {\n approvals.set(part.approval.id, part.approval.approved)\n }\n }\n }\n }\n }\n\n return { approvals, clientToolResults }\n }\n\n private collectClientState(): {\n approvals: Map<string, boolean>\n clientToolResults: Map<string, any>\n } {\n // Start with the initial client state extracted from original messages\n const approvals = new Map(this.initialApprovals)\n const clientToolResults = new Map(this.initialClientToolResults)\n\n // Also check current messages for any additional tool results (from server tools)\n for (const message of this.messages) {\n // Check for ModelMessage format (role: 'tool' messages contain tool results)\n // This handles results sent back from the client after executing client-side tools\n if (message.role === 'tool' && message.toolCallId) {\n // Parse content back to original output (was stringified by uiMessageToModelMessages)\n let output: unknown\n try {\n output = JSON.parse(message.content as string)\n } catch {\n output = message.content\n }\n // Skip approval response messages (they have pendingExecution marker)\n // These are NOT real client tool results — they are synthetic tool messages\n // created by uiMessageToModelMessages for approved-but-not-yet-executed tools.\n // Treating them as results would prevent the server from requesting actual\n // client-side execution after approval (see GitHub issue #225).\n if (\n output &&\n typeof output === 'object' &&\n (output as any).pendingExecution === true\n ) {\n continue\n }\n clientToolResults.set(message.toolCallId, output)\n }\n }\n\n return { approvals, clientToolResults }\n }\n\n private buildApprovalChunks(\n approvals: Array<ApprovalRequest>,\n finishEvent: RunFinishedEvent,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const approval of approvals) {\n chunks.push({\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: finishEvent.model,\n name: 'approval-requested',\n value: {\n toolCallId: approval.toolCallId,\n toolName: approval.toolName,\n input: approval.input,\n approval: {\n id: approval.approvalId,\n needsApproval: true,\n },\n },\n })\n }\n\n return chunks\n }\n\n private buildClientToolChunks(\n clientRequests: Array<ClientToolRequest>,\n finishEvent: RunFinishedEvent,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const clientTool of clientRequests) {\n chunks.push({\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: finishEvent.model,\n name: 'tool-input-available',\n value: {\n toolCallId: clientTool.toolCallId,\n toolName: clientTool.toolName,\n input: clientTool.input,\n },\n })\n }\n\n return chunks\n }\n\n private buildToolResultChunks(\n results: Array<ToolResult>,\n finishEvent: RunFinishedEvent,\n argsMap?: Map<string, string>,\n ): Array<StreamChunk> {\n const chunks: Array<StreamChunk> = []\n\n for (const result of results) {\n const content = JSON.stringify(result.result)\n\n // Emit TOOL_CALL_START + TOOL_CALL_ARGS before TOOL_CALL_END so that\n // the client can reconstruct the full tool call during continuations.\n if (argsMap) {\n chunks.push({\n type: 'TOOL_CALL_START',\n timestamp: Date.now(),\n model: finishEvent.model,\n toolCallId: result.toolCallId,\n toolName: result.toolName,\n })\n\n const args = argsMap.get(result.toolCallId) ?? '{}'\n chunks.push({\n type: 'TOOL_CALL_ARGS',\n timestamp: Date.now(),\n model: finishEvent.model,\n toolCallId: result.toolCallId,\n delta: args,\n args,\n })\n }\n\n chunks.push({\n type: 'TOOL_CALL_END',\n timestamp: Date.now(),\n model: finishEvent.model,\n toolCallId: result.toolCallId,\n toolName: result.toolName,\n result: content,\n })\n\n this.messages = [\n ...this.messages,\n {\n role: 'tool',\n content,\n toolCallId: result.toolCallId,\n },\n ]\n }\n\n return chunks\n }\n\n private getPendingToolCallsFromMessages(): Array<ToolCall> {\n // Build a set of completed tool IDs, but exclude tools with pendingExecution marker\n // (these are approved tools that still need to execute)\n const completedToolIds = new Set<string>()\n\n for (const message of this.messages) {\n if (message.role === 'tool' && message.toolCallId) {\n // Check if this is an approval response with pendingExecution marker\n let hasPendingExecution = false\n if (typeof message.content === 'string') {\n try {\n const parsed = JSON.parse(message.content)\n if (parsed.pendingExecution === true) {\n hasPendingExecution = true\n }\n } catch {\n // Not JSON, treat as regular tool result\n }\n }\n\n // Only mark as complete if NOT pending execution\n if (!hasPendingExecution) {\n completedToolIds.add(message.toolCallId)\n }\n }\n }\n\n const pending: Array<ToolCall> = []\n\n for (const message of this.messages) {\n if (message.role === 'assistant' && message.toolCalls) {\n for (const toolCall of message.toolCalls) {\n if (!completedToolIds.has(toolCall.id)) {\n pending.push(toolCall)\n }\n }\n }\n }\n\n return pending\n }\n\n private createSyntheticFinishedEvent(): RunFinishedEvent {\n return {\n type: 'RUN_FINISHED',\n runId: this.createId('pending'),\n model: this.params.model,\n timestamp: Date.now(),\n finishReason: 'tool_calls',\n }\n }\n\n private shouldContinue(): boolean {\n if (this.cyclePhase === 'executeToolCalls') {\n return true\n }\n\n return (\n this.loopStrategy({\n iterationCount: this.iterationCount,\n messages: this.messages,\n finishReason: this.lastFinishReason,\n }) && this.toolPhase === 'continue'\n )\n }\n\n private isAborted(): boolean {\n return !!this.effectiveSignal?.aborted\n }\n\n private isMiddlewareAborted(): boolean {\n return !!this.middlewareAbortController?.signal.aborted\n }\n\n private isCancelled(): boolean {\n return this.isAborted() || this.isMiddlewareAborted()\n }\n\n private buildMiddlewareConfig(): ChatMiddlewareConfig {\n return {\n messages: this.messages,\n systemPrompts: [...this.systemPrompts],\n tools: [...this.tools],\n temperature: this.params.temperature,\n topP: this.params.topP,\n maxTokens: this.params.maxTokens,\n metadata: this.params.metadata,\n modelOptions: this.params.modelOptions,\n }\n }\n\n private applyMiddlewareConfig(config: ChatMiddlewareConfig): void {\n this.messages = config.messages\n this.systemPrompts = config.systemPrompts\n this.tools = config.tools\n this.params = {\n ...this.params,\n temperature: config.temperature,\n topP: config.topP,\n maxTokens: config.maxTokens,\n metadata: config.metadata,\n modelOptions: config.modelOptions,\n }\n\n // Sync context fields that depend on config\n this.middlewareCtx.messages = this.messages\n this.middlewareCtx.systemPrompts = this.systemPrompts\n this.middlewareCtx.hasTools = this.tools.length > 0\n this.middlewareCtx.toolNames = this.tools.map((t) => t.name)\n this.middlewareCtx.modelOptions = config.modelOptions\n }\n\n private setToolPhase(phase: ToolPhaseResult): void {\n this.toolPhase = phase\n }\n\n /**\n * Drain an executeToolCalls async generator, yielding any CustomEvent chunks\n * and returning the final ExecuteToolCallsResult.\n */\n private async *drainToolCallGenerator(\n generator: AsyncGenerator<\n CustomEvent,\n {\n results: Array<ToolResult>\n needsApproval: Array<ApprovalRequest>\n needsClientExecution: Array<ClientToolRequest>\n },\n void\n >,\n ): AsyncGenerator<\n StreamChunk,\n {\n results: Array<ToolResult>\n needsApproval: Array<ApprovalRequest>\n needsClientExecution: Array<ClientToolRequest>\n },\n void\n > {\n let next = await generator.next()\n while (!next.done) {\n yield next.value\n next = await generator.next()\n }\n return next.value\n }\n\n private createCustomEventChunk(\n eventName: string,\n value: Record<string, any>,\n ): CustomEvent {\n return {\n type: 'CUSTOM',\n timestamp: Date.now(),\n model: this.params.model,\n name: eventName,\n value,\n }\n }\n\n private createId(prefix: string): string {\n return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`\n }\n}\n\n// ===========================\n// Activity Implementation\n// ===========================\n\n/**\n * Text activity - handles agentic text generation, one-shot text generation, and agentic structured output.\n *\n * This activity supports four modes:\n * 1. **Streaming agentic text**: Stream responses with automatic tool execution\n * 2. **Streaming one-shot text**: Simple streaming request/response without tools\n * 3. **Non-streaming text**: Returns collected text as a string (stream: false)\n * 4. **Agentic structured output**: Run tools, then return structured data\n *\n * @example Full agentic text (streaming with tools)\n * ```ts\n * import { chat } from '@tanstack/ai'\n * import { openaiText } from '@tanstack/ai-openai'\n *\n * for await (const chunk of chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'What is the weather?' }],\n * tools: [weatherTool]\n * })) {\n * if (chunk.type === 'content') {\n * console.log(chunk.delta)\n * }\n * }\n * ```\n *\n * @example One-shot text (streaming without tools)\n * ```ts\n * for await (const chunk of chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }]\n * })) {\n * console.log(chunk)\n * }\n * ```\n *\n * @example Non-streaming text (stream: false)\n * ```ts\n * const text = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Hello!' }],\n * stream: false\n * })\n * // text is a string with the full response\n * ```\n *\n * @example Agentic structured output (tools + structured response)\n * ```ts\n * import { z } from 'zod'\n *\n * const result = await chat({\n * adapter: openaiText('gpt-4o'),\n * messages: [{ role: 'user', content: 'Research and summarize the topic' }],\n * tools: [researchTool, analyzeTool],\n * outputSchema: z.object({\n * summary: z.string(),\n * keyPoints: z.array(z.string())\n * })\n * })\n * // result is { summary: string, keyPoints: string[] }\n * ```\n */\nexport function chat<\n TAdapter extends AnyTextAdapter,\n TSchema extends SchemaInput | undefined = undefined,\n TStream extends boolean = true,\n>(\n options: TextActivityOptions<TAdapter, TSchema, TStream>,\n): TextActivityResult<TSchema, TStream> {\n const { outputSchema, stream } = options\n\n // If outputSchema is provided, run agentic structured output\n if (outputSchema) {\n return runAgenticStructuredOutput(\n options as unknown as TextActivityOptions<\n AnyTextAdapter,\n SchemaInput,\n boolean\n >,\n ) as TextActivityResult<TSchema, TStream>\n }\n\n // If stream is explicitly false, run non-streaming text\n if (stream === false) {\n return runNonStreamingText(\n options as unknown as TextActivityOptions<\n AnyTextAdapter,\n undefined,\n false\n >,\n ) as TextActivityResult<TSchema, TStream>\n }\n\n // Otherwise, run streaming text (default)\n return runStreamingText(\n options as unknown as TextActivityOptions<AnyTextAdapter, undefined, true>,\n ) as TextActivityResult<TSchema, TStream>\n}\n\n/**\n * Run streaming text (agentic or one-shot depending on tools)\n */\nasync function* runStreamingText(\n options: TextActivityOptions<AnyTextAdapter, undefined, true>,\n): AsyncIterable<StreamChunk> {\n const { adapter, middleware, context, ...textOptions } = options\n const model = adapter.model\n\n const engine = new TextEngine({\n adapter,\n params: { ...textOptions, model } as TextOptions<\n Record<string, any>,\n Record<string, any>\n >,\n middleware,\n context,\n })\n\n for await (const chunk of engine.run()) {\n yield chunk\n }\n}\n\n/**\n * Run non-streaming text - collects all content and returns as a string.\n * Runs the full agentic loop (if tools are provided) but returns collected text.\n */\nfunction runNonStreamingText(\n options: TextActivityOptions<AnyTextAdapter, undefined, false>,\n): Promise<string> {\n // Run the streaming text and collect all text using streamToText\n const stream = runStreamingText(\n options as unknown as TextActivityOptions<AnyTextAdapter, undefined, true>,\n )\n\n return streamToText(stream)\n}\n\n/**\n * Run agentic structured output:\n * 1. Execute the full agentic loop (with tools)\n * 2. Once complete, call adapter.structuredOutput with the conversation context\n * 3. Validate and return the structured result\n */\nasync function runAgenticStructuredOutput<TSchema extends SchemaInput>(\n options: TextActivityOptions<AnyTextAdapter, TSchema, boolean>,\n): Promise<InferSchemaType<TSchema>> {\n const { adapter, outputSchema, middleware, context, ...textOptions } = options\n const model = adapter.model\n\n if (!outputSchema) {\n throw new Error('outputSchema is required for structured output')\n }\n\n // Create the engine and run the agentic loop\n const engine = new TextEngine({\n adapter,\n params: { ...textOptions, model } as TextOptions<\n Record<string, unknown>,\n Record<string, unknown>\n >,\n middleware,\n context,\n })\n\n // Consume the stream to run the agentic loop\n for await (const _chunk of engine.run()) {\n // Just consume the stream to execute the agentic loop\n }\n\n // Get the final messages from the engine (includes tool results)\n const finalMessages = engine.getMessages()\n\n // Build text options for structured output, excluding tools since\n // the agentic loop is complete and we only need the final response\n const {\n tools: _tools,\n agentLoopStrategy: _als,\n ...structuredTextOptions\n } = textOptions\n\n // Convert the schema to JSON Schema before passing to the adapter\n const jsonSchema = convertSchemaToJsonSchema(outputSchema)\n if (!jsonSchema) {\n throw new Error('Failed to convert output schema to JSON Schema')\n }\n\n // Call the adapter's structured output method with the conversation context\n // The adapter receives JSON Schema and can apply vendor-specific patches\n const result = await adapter.structuredOutput({\n chatOptions: {\n ...structuredTextOptions,\n model,\n messages: finalMessages,\n },\n outputSchema: jsonSchema,\n })\n\n // Validate the result against the schema if it's a Standard Schema\n if (isStandardSchema(outputSchema)) {\n return parseWithStandardSchema<InferSchemaType<TSchema>>(\n outputSchema,\n result.data,\n )\n }\n\n // For plain JSON Schema, return the data as-is\n return result.data as InferSchemaType<TSchema>\n}\n\n// Re-export adapter types\nexport type {\n TextAdapter,\n TextAdapterConfig,\n StructuredOutputOptions,\n StructuredOutputResult,\n} from './adapter'\nexport { BaseTextAdapter } from './adapter'\n"],"names":["maxIterationsStrategy","transformedConfig"],"mappings":";;;;;;;;AA0DO,MAAM,OAAO;AA8Hb,SAAS,kBAKd,SACiD;AACjD,SAAO;AACT;AAuCA,MAAM,WAGJ;AAAA,EAuCA,YAAY,QAA6C;AAxBzD,SAAQ,iBAAiB;AACzB,SAAQ,mBAAkC;AAC1C,SAAQ,kBAAkB;AAC1B,SAAQ,kBAAkB;AAC1B,SAAQ,mBAAkC;AAC1C,SAAQ,qBAAqB;AAG7B,SAAQ,gBAAyC;AACjD,SAAQ,mBAAmB;AAC3B,SAAQ,YAA6B;AACrC,SAAQ,aAAyB;AAQjC,SAAiB,mBAA4C,CAAA;AAG7D,SAAQ,qBAAqB;AAG3B,SAAK,UAAU,OAAO;AACtB,SAAK,SAAS,OAAO;AACrB,SAAK,gBAAgB,OAAO,OAAO,iBAAiB,CAAA;AACpD,SAAK,eACH,OAAO,OAAO,qBAAqBA,cAAsB,CAAC;AAC5D,SAAK,sBAAsB,OAAO,OAAO,SAAS;AAIlD,UAAM,EAAE,WAAW,kBAAA,IACjB,KAAK;AAAA,MACH,OAAO,OAAO;AAAA,IAAA;AAElB,SAAK,mBAAmB;AACxB,SAAK,2BAA2B;AAIhC,SAAK,WAAW;AAAA,MACd,OAAO,OAAO;AAAA,IAAA;AAIhB,SAAK,kBAAkB,IAAI;AAAA,MACzB,OAAO,OAAO,SAAS,CAAA;AAAA,MACvB,KAAK;AAAA,IAAA;AAEP,SAAK,QAAQ,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A,QAAQ,EAAE,GAAG,aAAa,MAAA;AAAA,IAI1B;AAAA,IACA;AAAA,EAAA,CACD;AAED,mBAAiB,SAAS,OAAO,OAAO;AACtC,UAAM;AAAA,EACR;AACF;AAMA,SAAS,oBACP,SACiB;AAEjB,QAAM,SAAS;AAAA,IACb;AAAA,EAAA;AAGF,SAAO,aAAa,MAAM;AAC5B;AAQA,eAAe,2BACb,SACmC;AACnC,QAAM,EAAE,SAAS,cAAc,YAAY,SAAS,GAAG,gBAAgB;AACvE,QAAM,QAAQ,QAAQ;AAEtB,MAAI,CAAC,cAAc;AACjB,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAGA,QAAM,SAAS,IAAI,WAAW;AAAA,IAC5B;AAAA,IACA,QAAQ,EAAE,GAAG,aAAa,MAAA;AAAA,IAI1B;AAAA,IACA;AAAA,EAAA,CACD;AAGD,mBAAiB,UAAU,OAAO,OAAO;AAAA,EAEzC;AAGA,QAAM,gBAAgB,OAAO,YAAA;AAI7B,QAAM;AAAA,IACJ,OAAO;AAAA,IACP,mBAAmB;AAAA,IACnB,GAAG;AAAA,EAAA,IACD;AAGJ,QAAM,aAAa,0BAA0B,YAAY;AACzD,MAAI,CAAC,YAAY;AACf,UAAM,IAAI,MAAM,gDAAgD;AAAA,EAClE;AAIA,QAAM,SAAS,MAAM,QAAQ,iBAAiB;AAAA,IAC5C,aAAa;AAAA,MACX,GAAG;AAAA,MACH;AAAA,MACA,UAAU;AAAA,IAAA;AAAA,IAEZ,cAAc;AAAA,EAAA,CACf;AAGD,MAAI,iBAAiB,YAAY,GAAG;AAClC,WAAO;AAAA,MACL;AAAA,MACA,OAAO;AAAA,IAAA;AAAA,EAEX;AAGA,SAAO,OAAO;AAChB;"}
@@ -53,7 +53,8 @@ class ToolCallManager {
53
53
  for (const [, toolCall] of this.toolCallsMap.entries()) {
54
54
  if (toolCall.id === event.toolCallId) {
55
55
  if (event.input !== void 0) {
56
- toolCall.function.arguments = JSON.stringify(event.input);
56
+ const normalized = event.input && typeof event.input === "object" ? event.input : {};
57
+ toolCall.function.arguments = JSON.stringify(normalized);
57
58
  }
58
59
  break;
59
60
  }
@@ -89,7 +90,8 @@ class ToolCallManager {
89
90
  let args;
90
91
  try {
91
92
  const argsString = toolCall.function.arguments.trim() || "{}";
92
- args = JSON.parse(argsString === "null" ? "{}" : argsString);
93
+ const parsed = JSON.parse(argsString);
94
+ args = parsed && typeof parsed === "object" ? parsed : {};
93
95
  } catch (parseError) {
94
96
  throw new Error(
95
97
  `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`
@@ -294,7 +296,8 @@ async function* executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ n
294
296
  const argsStr = toolCall.function.arguments.trim() || "{}";
295
297
  {
296
298
  try {
297
- input = JSON.parse(argsStr);
299
+ const parsed = JSON.parse(argsStr);
300
+ input = parsed && typeof parsed === "object" ? parsed : {};
298
301
  } catch (parseError) {
299
302
  throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`);
300
303
  }
@@ -1 +1 @@
1
- {"version":3,"file":"tool-calls.js","sources":["../../../../../src/activities/chat/tools/tool-calls.ts"],"sourcesContent":["import { isStandardSchema, parseWithStandardSchema } from './schema-converter'\nimport type {\n CustomEvent,\n ModelMessage,\n RunFinishedEvent,\n Tool,\n ToolCall,\n ToolCallArgsEvent,\n ToolCallEndEvent,\n ToolCallStartEvent,\n ToolExecutionContext,\n} from '../../../types'\nimport type {\n AfterToolCallInfo,\n BeforeToolCallDecision,\n} from '../middleware/types'\n\nfunction safeJsonParse(value: string): unknown {\n try {\n return JSON.parse(value)\n } catch {\n return value\n }\n}\n\n/**\n * Optional middleware hooks for tool execution.\n * When provided, these callbacks are invoked before/after each tool execution.\n */\nexport interface ToolExecutionMiddlewareHooks {\n onBeforeToolCall?: (\n toolCall: ToolCall,\n tool: Tool | undefined,\n args: unknown,\n ) => Promise<BeforeToolCallDecision>\n onAfterToolCall?: (info: AfterToolCallInfo) => Promise<void>\n}\n\n/**\n * Error thrown when middleware decides to abort the chat run during tool execution.\n */\nexport class MiddlewareAbortError extends Error {\n constructor(reason: string) {\n super(reason)\n this.name = 'MiddlewareAbortError'\n }\n}\n\n/**\n * Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.\n *\n * Responsibilities:\n * - Accumulates streaming tool call events (ID, name, arguments)\n * - Validates tool calls (filters out incomplete ones)\n * - Executes tool `execute` functions with parsed arguments\n * - Emits `TOOL_CALL_END` events for client visibility\n * - Returns tool result messages for conversation history\n *\n * This class is used internally by the AI.chat() method to handle the automatic\n * tool execution loop. It can also be used independently for custom tool execution logic.\n *\n * @example\n * ```typescript\n * const manager = new ToolCallManager(tools);\n *\n * // During streaming, accumulate tool calls\n * for await (const chunk of stream) {\n * if (chunk.type === 'TOOL_CALL_START') {\n * manager.addToolCallStartEvent(chunk);\n * } else if (chunk.type === 'TOOL_CALL_ARGS') {\n * manager.addToolCallArgsEvent(chunk);\n * }\n * }\n *\n * // After stream completes, execute tools\n * if (manager.hasToolCalls()) {\n * const toolResults = yield* manager.executeTools(finishEvent);\n * messages = [...messages, ...toolResults];\n * manager.clear();\n * }\n * ```\n */\nexport class ToolCallManager {\n private toolCallsMap = new Map<number, ToolCall>()\n private tools: ReadonlyArray<Tool>\n\n constructor(tools: ReadonlyArray<Tool>) {\n this.tools = tools\n }\n\n /**\n * Add a TOOL_CALL_START event to begin tracking a tool call (AG-UI)\n */\n addToolCallStartEvent(event: ToolCallStartEvent): void {\n const index = event.index ?? this.toolCallsMap.size\n this.toolCallsMap.set(index, {\n id: event.toolCallId,\n type: 'function',\n function: {\n name: event.toolName,\n arguments: '',\n },\n ...(event.providerMetadata && {\n providerMetadata: event.providerMetadata,\n }),\n })\n }\n\n /**\n * Add a TOOL_CALL_ARGS event to accumulate arguments (AG-UI)\n */\n addToolCallArgsEvent(event: ToolCallArgsEvent): void {\n // Find the tool call by ID\n for (const [, toolCall] of this.toolCallsMap.entries()) {\n if (toolCall.id === event.toolCallId) {\n toolCall.function.arguments += event.delta\n break\n }\n }\n }\n\n /**\n * Complete a tool call with its final input\n * Called when TOOL_CALL_END is received\n */\n completeToolCall(event: ToolCallEndEvent): void {\n for (const [, toolCall] of this.toolCallsMap.entries()) {\n if (toolCall.id === event.toolCallId) {\n if (event.input !== undefined) {\n toolCall.function.arguments = JSON.stringify(event.input)\n }\n break\n }\n }\n }\n\n /**\n * Check if there are any complete tool calls to execute\n */\n hasToolCalls(): boolean {\n return this.getToolCalls().length > 0\n }\n\n /**\n * Get all complete tool calls (filtered for valid ID and name)\n */\n getToolCalls(): Array<ToolCall> {\n return Array.from(this.toolCallsMap.values()).filter(\n (tc) => tc.id && tc.function.name && tc.function.name.trim().length > 0,\n )\n }\n\n /**\n * Execute all tool calls and return tool result messages\n * Yields TOOL_CALL_END events for streaming\n * @param finishEvent - RUN_FINISHED event from the stream\n */\n async *executeTools(\n finishEvent: RunFinishedEvent,\n ): AsyncGenerator<ToolCallEndEvent, Array<ModelMessage>, void> {\n const toolCallsArray = this.getToolCalls()\n const toolResults: Array<ModelMessage> = []\n\n for (const toolCall of toolCallsArray) {\n const tool = this.tools.find((t) => t.name === toolCall.function.name)\n\n let toolResultContent: string\n if (tool?.execute) {\n try {\n // Parse arguments (normalize \"null\" to \"{}\" for empty tool_use blocks)\n let args: unknown\n try {\n const argsString = toolCall.function.arguments.trim() || '{}'\n args = JSON.parse(argsString === 'null' ? '{}' : argsString)\n } catch (parseError) {\n throw new Error(\n `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,\n )\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n args = parseWithStandardSchema(tool.inputSchema, args)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Input validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n // Execute the tool\n let result = await tool.execute(args)\n\n // Validate output against outputSchema if provided (for Standard Schema compliant schemas)\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n try {\n result = parseWithStandardSchema(tool.outputSchema, result)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n toolResultContent =\n typeof result === 'string' ? result : JSON.stringify(result)\n } catch (error: unknown) {\n // If tool execution fails, add error message\n const message =\n error instanceof Error ? error.message : 'Unknown error'\n toolResultContent = `Error executing tool: ${message}`\n }\n } else {\n // Tool doesn't have execute function, add placeholder\n toolResultContent = `Tool ${toolCall.function.name} does not have an execute function`\n }\n\n // Emit TOOL_CALL_END event\n yield {\n type: 'TOOL_CALL_END',\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n model: finishEvent.model,\n timestamp: Date.now(),\n result: toolResultContent,\n }\n\n // Add tool result message\n toolResults.push({\n role: 'tool',\n content: toolResultContent,\n toolCallId: toolCall.id,\n })\n }\n\n return toolResults\n }\n\n /**\n * Clear the tool calls map for the next iteration\n */\n clear(): void {\n this.toolCallsMap.clear()\n }\n}\n\nexport interface ToolResult {\n toolCallId: string\n toolName: string\n result: any\n state?: 'output-available' | 'output-error'\n /** Duration of tool execution in milliseconds (only for server-executed tools) */\n duration?: number\n}\n\nexport interface ApprovalRequest {\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n}\n\nexport interface ClientToolRequest {\n toolCallId: string\n toolName: string\n input: any\n}\n\ninterface ExecuteToolCallsResult {\n /** Tool results ready to send to LLM */\n results: Array<ToolResult>\n /** Tools that need user approval before execution */\n needsApproval: Array<ApprovalRequest>\n /** Tools that need client-side execution */\n needsClientExecution: Array<ClientToolRequest>\n}\n\n/**\n * Helper that runs a tool execution promise while polling for pending custom events.\n * Yields any custom events that are emitted during execution, then returns the\n * execution result.\n */\nasync function* executeWithEventPolling<T>(\n executionPromise: Promise<T>,\n pendingEvents: Array<CustomEvent>,\n): AsyncGenerator<CustomEvent, T, void> {\n // Use an object to track mutable state across the async boundary\n const state = { done: false, result: undefined as T }\n const executionWithFlag = executionPromise.then((r) => {\n state.done = true\n state.result = r\n return r\n })\n\n while (!state.done) {\n // Wait for either the execution to complete or a short timeout\n await Promise.race([\n executionWithFlag,\n new Promise((resolve) => setTimeout(resolve, 10)),\n ])\n\n // Flush any pending events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n }\n\n // Final flush in case events were emitted right at completion\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n return state.result\n}\n\n/**\n * Apply a middleware onBeforeToolCall decision.\n * Returns the (possibly transformed) input if execution should proceed,\n * or undefined if the tool call was skipped (result already pushed).\n * Throws MiddlewareAbortError if the decision is 'abort'.\n */\nasync function applyBeforeToolCallDecision(\n toolCall: ToolCall,\n tool: Tool,\n input: unknown,\n toolName: string,\n middlewareHooks: ToolExecutionMiddlewareHooks,\n results: Array<ToolResult>,\n): Promise<{ proceed: true; input: unknown } | { proceed: false }> {\n if (!middlewareHooks.onBeforeToolCall) {\n return { proceed: true, input }\n }\n\n const decision = await middlewareHooks.onBeforeToolCall(toolCall, tool, input)\n if (!decision) {\n return { proceed: true, input }\n }\n\n if (decision.type === 'abort') {\n throw new MiddlewareAbortError(decision.reason || 'Aborted by middleware')\n }\n\n if (decision.type === 'skip') {\n const skipResult = decision.result\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof skipResult === 'string'\n ? safeJsonParse(skipResult)\n : skipResult || null,\n duration: 0,\n })\n if (middlewareHooks.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: true,\n duration: 0,\n result: skipResult,\n })\n }\n return { proceed: false }\n }\n\n return { proceed: true, input: decision.args }\n}\n\n/**\n * Execute a server-side tool with event polling, output validation, and middleware hooks.\n * Yields CustomEvent chunks during execution and pushes the result to the results array.\n */\nasync function* executeServerTool(\n toolCall: ToolCall,\n tool: Tool,\n toolName: string,\n input: unknown,\n context: ToolExecutionContext,\n pendingEvents: Array<CustomEvent>,\n results: Array<ToolResult>,\n middlewareHooks?: ToolExecutionMiddlewareHooks,\n): AsyncGenerator<CustomEvent, void, void> {\n const startTime = Date.now()\n try {\n const executionPromise = Promise.resolve(tool.execute!(input, context))\n let result = yield* executeWithEventPolling(executionPromise, pendingEvents)\n const duration = Date.now() - startTime\n\n // Flush remaining events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n // Validate output against outputSchema if provided\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n result = parseWithStandardSchema(tool.outputSchema, result)\n }\n\n const finalResult =\n typeof result === 'string' ? safeJsonParse(result) : result || null\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: finalResult,\n duration,\n })\n\n if (middlewareHooks?.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: true,\n duration,\n result: finalResult,\n })\n }\n } catch (error: unknown) {\n const duration = Date.now() - startTime\n\n // Flush remaining events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n if (error instanceof MiddlewareAbortError) {\n throw error\n }\n\n const message = error instanceof Error ? error.message : 'Unknown error'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: message },\n state: 'output-error',\n duration,\n })\n\n if (middlewareHooks?.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: false,\n duration,\n error,\n })\n }\n }\n}\n\n/**\n * Execute tool calls based on their configuration.\n * Yields CustomEvent chunks during tool execution for real-time progress updates.\n *\n * Handles three cases:\n * 1. Client tools (no execute) - request client to execute\n * 2. Server tools with approval - check approval before executing\n * 3. Normal server tools - execute immediately\n *\n * @param toolCalls - Tool calls from the LLM\n * @param tools - Available tools with their configurations\n * @param approvals - Map of approval decisions (approval.id -> approved boolean)\n * @param clientResults - Map of client-side execution results (toolCallId -> result)\n * @param createCustomEventChunk - Factory to create CustomEvent chunks (optional)\n */\nexport async function* executeToolCalls(\n toolCalls: Array<ToolCall>,\n tools: ReadonlyArray<Tool>,\n approvals: Map<string, boolean> = new Map(),\n clientResults: Map<string, any> = new Map(),\n createCustomEventChunk?: (\n eventName: string,\n value: Record<string, any>,\n ) => CustomEvent,\n middlewareHooks?: ToolExecutionMiddlewareHooks,\n): AsyncGenerator<CustomEvent, ExecuteToolCallsResult, void> {\n const results: Array<ToolResult> = []\n const needsApproval: Array<ApprovalRequest> = []\n const needsClientExecution: Array<ClientToolRequest> = []\n\n // Create tool lookup map\n const toolMap = new Map<string, Tool>()\n for (const tool of tools) {\n toolMap.set(tool.name, tool)\n }\n\n // Batch gating: when any tool in the batch still needs an approval decision,\n // defer all execution so side effects don't happen before the user decides.\n const hasPendingApprovals = toolCalls.some((tc) => {\n const t = toolMap.get(tc.function.name)\n return t?.needsApproval && !approvals.has(`approval_${tc.id}`)\n })\n\n for (const toolCall of toolCalls) {\n const tool = toolMap.get(toolCall.function.name)\n const toolName = toolCall.function.name\n\n if (!tool) {\n // Unknown tool - return error\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: `Unknown tool: ${toolName}` },\n state: 'output-error',\n })\n continue\n }\n\n // Skip non-pending tools while approvals are outstanding\n if (hasPendingApprovals) {\n if (!tool.needsApproval || approvals.has(`approval_${toolCall.id}`)) {\n continue\n }\n }\n\n // Parse arguments, throwing error if invalid JSON\n let input: unknown = {}\n const argsStr = toolCall.function.arguments.trim() || '{}'\n if (argsStr) {\n try {\n input = JSON.parse(argsStr)\n } catch (parseError) {\n // If parsing fails, throw error to fail fast\n throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`)\n }\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n input = parseWithStandardSchema(tool.inputSchema, input)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: {\n error: `Input validation failed for tool ${tool.name}: ${message}`,\n },\n state: 'output-error',\n })\n continue\n }\n }\n\n // Create a ToolExecutionContext for this tool call with event emission\n const pendingEvents: Array<CustomEvent> = []\n const context: ToolExecutionContext = {\n toolCallId: toolCall.id,\n emitCustomEvent: (eventName: string, value: Record<string, any>) => {\n if (createCustomEventChunk) {\n pendingEvents.push(\n createCustomEventChunk(eventName, {\n ...value,\n toolCallId: toolCall.id,\n }),\n )\n }\n },\n }\n\n // CASE 1: Client-side tool (no execute function)\n if (!tool.execute) {\n // Check if tool needs approval\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Approved - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Approved but not executed yet - request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval first\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n input,\n approvalId,\n })\n }\n } else {\n // No approval needed - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n }\n continue\n }\n\n // CASE 2: Server tool with approval required\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Apply middleware before-hook for approved tools\n if (middlewareHooks) {\n const decision = await applyBeforeToolCallDecision(\n toolCall,\n tool,\n input,\n toolName,\n middlewareHooks,\n results,\n )\n if (!decision.proceed) continue\n input = decision.input\n }\n\n yield* executeServerTool(\n toolCall,\n tool,\n toolName,\n input,\n context,\n pendingEvents,\n results,\n middlewareHooks,\n )\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n approvalId,\n })\n }\n continue\n }\n\n // CASE 3: Normal server tool - execute immediately\n if (middlewareHooks) {\n const decision = await applyBeforeToolCallDecision(\n toolCall,\n tool,\n input,\n toolName,\n middlewareHooks,\n results,\n )\n if (!decision.proceed) continue\n input = decision.input\n }\n\n yield* executeServerTool(\n toolCall,\n tool,\n toolName,\n input,\n context,\n pendingEvents,\n results,\n middlewareHooks,\n )\n }\n\n return { results, needsApproval, needsClientExecution }\n}\n"],"names":[],"mappings":";AAiBA,SAAS,cAAc,OAAwB;AAC7C,MAAI;AACF,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAkBO,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YAAY,QAAgB;AAC1B,UAAM,MAAM;AACZ,SAAK,OAAO;AAAA,EACd;AACF;AAoCO,MAAM,gBAAgB;AAAA,EAI3B,YAAY,OAA4B;AAHxC,SAAQ,mCAAmB,IAAA;AAIzB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKA,sBAAsB,OAAiC;AACrD,UAAM,QAAQ,MAAM,SAAS,KAAK,aAAa;AAC/C,SAAK,aAAa,IAAI,OAAO;AAAA,MAC3B,IAAI,MAAM;AAAA,MACV,MAAM;AAAA,MACN,UAAU;AAAA,QACR,MAAM,MAAM;AAAA,QACZ,WAAW;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AAA,YACX,YAAY,SAAS;AAAA,YACrB;AAAA,YACA,QAAQ,EAAE,OAAO,+BAAA;AAAA,YACjB,OAAO;AAAA,UAAA,CACR;AAAA,QACH;AAAA,MACF,OAAO;AAEL,sBAAc,KAAK;AAAA,UACjB,YAAY,SAAS;AAAA,UACrB;AAAA,UACA;AAAA,UACA;AAAA,QAAA,CACD;AAAA,MACH;AACA;AAAA,IACF;AAGA,QAAI,iBAAiB;AACnB,YAAM,WAAW,MAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAEF,UAAI,CAAC,SAAS,QAAS;AACvB,cAAQ,SAAS;AAAA,IACnB;AAEA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO,EAAE,SAAS,eAAe,qBAAA;AACnC;"}
1
+ {"version":3,"file":"tool-calls.js","sources":["../../../../../src/activities/chat/tools/tool-calls.ts"],"sourcesContent":["import { isStandardSchema, parseWithStandardSchema } from './schema-converter'\nimport type {\n CustomEvent,\n ModelMessage,\n RunFinishedEvent,\n Tool,\n ToolCall,\n ToolCallArgsEvent,\n ToolCallEndEvent,\n ToolCallStartEvent,\n ToolExecutionContext,\n} from '../../../types'\nimport type {\n AfterToolCallInfo,\n BeforeToolCallDecision,\n} from '../middleware/types'\n\nfunction safeJsonParse(value: string): unknown {\n try {\n return JSON.parse(value)\n } catch {\n return value\n }\n}\n\n/**\n * Optional middleware hooks for tool execution.\n * When provided, these callbacks are invoked before/after each tool execution.\n */\nexport interface ToolExecutionMiddlewareHooks {\n onBeforeToolCall?: (\n toolCall: ToolCall,\n tool: Tool | undefined,\n args: unknown,\n ) => Promise<BeforeToolCallDecision>\n onAfterToolCall?: (info: AfterToolCallInfo) => Promise<void>\n}\n\n/**\n * Error thrown when middleware decides to abort the chat run during tool execution.\n */\nexport class MiddlewareAbortError extends Error {\n constructor(reason: string) {\n super(reason)\n this.name = 'MiddlewareAbortError'\n }\n}\n\n/**\n * Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.\n *\n * Responsibilities:\n * - Accumulates streaming tool call events (ID, name, arguments)\n * - Validates tool calls (filters out incomplete ones)\n * - Executes tool `execute` functions with parsed arguments\n * - Emits `TOOL_CALL_END` events for client visibility\n * - Returns tool result messages for conversation history\n *\n * This class is used internally by the AI.chat() method to handle the automatic\n * tool execution loop. It can also be used independently for custom tool execution logic.\n *\n * @example\n * ```typescript\n * const manager = new ToolCallManager(tools);\n *\n * // During streaming, accumulate tool calls\n * for await (const chunk of stream) {\n * if (chunk.type === 'TOOL_CALL_START') {\n * manager.addToolCallStartEvent(chunk);\n * } else if (chunk.type === 'TOOL_CALL_ARGS') {\n * manager.addToolCallArgsEvent(chunk);\n * }\n * }\n *\n * // After stream completes, execute tools\n * if (manager.hasToolCalls()) {\n * const toolResults = yield* manager.executeTools(finishEvent);\n * messages = [...messages, ...toolResults];\n * manager.clear();\n * }\n * ```\n */\nexport class ToolCallManager {\n private toolCallsMap = new Map<number, ToolCall>()\n private tools: ReadonlyArray<Tool>\n\n constructor(tools: ReadonlyArray<Tool>) {\n this.tools = tools\n }\n\n /**\n * Add a TOOL_CALL_START event to begin tracking a tool call (AG-UI)\n */\n addToolCallStartEvent(event: ToolCallStartEvent): void {\n const index = event.index ?? this.toolCallsMap.size\n this.toolCallsMap.set(index, {\n id: event.toolCallId,\n type: 'function',\n function: {\n name: event.toolName,\n arguments: '',\n },\n ...(event.providerMetadata && {\n providerMetadata: event.providerMetadata,\n }),\n })\n }\n\n /**\n * Add a TOOL_CALL_ARGS event to accumulate arguments (AG-UI)\n */\n addToolCallArgsEvent(event: ToolCallArgsEvent): void {\n // Find the tool call by ID\n for (const [, toolCall] of this.toolCallsMap.entries()) {\n if (toolCall.id === event.toolCallId) {\n toolCall.function.arguments += event.delta\n break\n }\n }\n }\n\n /**\n * Complete a tool call with its final input\n * Called when TOOL_CALL_END is received\n */\n completeToolCall(event: ToolCallEndEvent): void {\n for (const [, toolCall] of this.toolCallsMap.entries()) {\n if (toolCall.id === event.toolCallId) {\n if (event.input !== undefined) {\n // Normalize null/non-object to {} (e.g. Anthropic empty tool_use blocks)\n const normalized =\n event.input && typeof event.input === 'object' ? event.input : {}\n toolCall.function.arguments = JSON.stringify(normalized)\n }\n break\n }\n }\n }\n\n /**\n * Check if there are any complete tool calls to execute\n */\n hasToolCalls(): boolean {\n return this.getToolCalls().length > 0\n }\n\n /**\n * Get all complete tool calls (filtered for valid ID and name)\n */\n getToolCalls(): Array<ToolCall> {\n return Array.from(this.toolCallsMap.values()).filter(\n (tc) => tc.id && tc.function.name && tc.function.name.trim().length > 0,\n )\n }\n\n /**\n * Execute all tool calls and return tool result messages\n * Yields TOOL_CALL_END events for streaming\n * @param finishEvent - RUN_FINISHED event from the stream\n */\n async *executeTools(\n finishEvent: RunFinishedEvent,\n ): AsyncGenerator<ToolCallEndEvent, Array<ModelMessage>, void> {\n const toolCallsArray = this.getToolCalls()\n const toolResults: Array<ModelMessage> = []\n\n for (const toolCall of toolCallsArray) {\n const tool = this.tools.find((t) => t.name === toolCall.function.name)\n\n let toolResultContent: string\n if (tool?.execute) {\n try {\n // Parse arguments (normalize null/non-object to {} for empty tool_use blocks)\n let args: unknown\n try {\n const argsString = toolCall.function.arguments.trim() || '{}'\n const parsed = JSON.parse(argsString)\n args = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n throw new Error(\n `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,\n )\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n args = parseWithStandardSchema(tool.inputSchema, args)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Input validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n // Execute the tool\n let result = await tool.execute(args)\n\n // Validate output against outputSchema if provided (for Standard Schema compliant schemas)\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n try {\n result = parseWithStandardSchema(tool.outputSchema, result)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n toolResultContent =\n typeof result === 'string' ? result : JSON.stringify(result)\n } catch (error: unknown) {\n // If tool execution fails, add error message\n const message =\n error instanceof Error ? error.message : 'Unknown error'\n toolResultContent = `Error executing tool: ${message}`\n }\n } else {\n // Tool doesn't have execute function, add placeholder\n toolResultContent = `Tool ${toolCall.function.name} does not have an execute function`\n }\n\n // Emit TOOL_CALL_END event\n yield {\n type: 'TOOL_CALL_END',\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n model: finishEvent.model,\n timestamp: Date.now(),\n result: toolResultContent,\n }\n\n // Add tool result message\n toolResults.push({\n role: 'tool',\n content: toolResultContent,\n toolCallId: toolCall.id,\n })\n }\n\n return toolResults\n }\n\n /**\n * Clear the tool calls map for the next iteration\n */\n clear(): void {\n this.toolCallsMap.clear()\n }\n}\n\nexport interface ToolResult {\n toolCallId: string\n toolName: string\n result: any\n state?: 'output-available' | 'output-error'\n /** Duration of tool execution in milliseconds (only for server-executed tools) */\n duration?: number\n}\n\nexport interface ApprovalRequest {\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n}\n\nexport interface ClientToolRequest {\n toolCallId: string\n toolName: string\n input: any\n}\n\ninterface ExecuteToolCallsResult {\n /** Tool results ready to send to LLM */\n results: Array<ToolResult>\n /** Tools that need user approval before execution */\n needsApproval: Array<ApprovalRequest>\n /** Tools that need client-side execution */\n needsClientExecution: Array<ClientToolRequest>\n}\n\n/**\n * Helper that runs a tool execution promise while polling for pending custom events.\n * Yields any custom events that are emitted during execution, then returns the\n * execution result.\n */\nasync function* executeWithEventPolling<T>(\n executionPromise: Promise<T>,\n pendingEvents: Array<CustomEvent>,\n): AsyncGenerator<CustomEvent, T, void> {\n // Use an object to track mutable state across the async boundary\n const state = { done: false, result: undefined as T }\n const executionWithFlag = executionPromise.then((r) => {\n state.done = true\n state.result = r\n return r\n })\n\n while (!state.done) {\n // Wait for either the execution to complete or a short timeout\n await Promise.race([\n executionWithFlag,\n new Promise((resolve) => setTimeout(resolve, 10)),\n ])\n\n // Flush any pending events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n }\n\n // Final flush in case events were emitted right at completion\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n return state.result\n}\n\n/**\n * Apply a middleware onBeforeToolCall decision.\n * Returns the (possibly transformed) input if execution should proceed,\n * or undefined if the tool call was skipped (result already pushed).\n * Throws MiddlewareAbortError if the decision is 'abort'.\n */\nasync function applyBeforeToolCallDecision(\n toolCall: ToolCall,\n tool: Tool,\n input: unknown,\n toolName: string,\n middlewareHooks: ToolExecutionMiddlewareHooks,\n results: Array<ToolResult>,\n): Promise<{ proceed: true; input: unknown } | { proceed: false }> {\n if (!middlewareHooks.onBeforeToolCall) {\n return { proceed: true, input }\n }\n\n const decision = await middlewareHooks.onBeforeToolCall(toolCall, tool, input)\n if (!decision) {\n return { proceed: true, input }\n }\n\n if (decision.type === 'abort') {\n throw new MiddlewareAbortError(decision.reason || 'Aborted by middleware')\n }\n\n if (decision.type === 'skip') {\n const skipResult = decision.result\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof skipResult === 'string'\n ? safeJsonParse(skipResult)\n : skipResult || null,\n duration: 0,\n })\n if (middlewareHooks.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: true,\n duration: 0,\n result: skipResult,\n })\n }\n return { proceed: false }\n }\n\n return { proceed: true, input: decision.args }\n}\n\n/**\n * Execute a server-side tool with event polling, output validation, and middleware hooks.\n * Yields CustomEvent chunks during execution and pushes the result to the results array.\n */\nasync function* executeServerTool(\n toolCall: ToolCall,\n tool: Tool,\n toolName: string,\n input: unknown,\n context: ToolExecutionContext,\n pendingEvents: Array<CustomEvent>,\n results: Array<ToolResult>,\n middlewareHooks?: ToolExecutionMiddlewareHooks,\n): AsyncGenerator<CustomEvent, void, void> {\n const startTime = Date.now()\n try {\n const executionPromise = Promise.resolve(tool.execute!(input, context))\n let result = yield* executeWithEventPolling(executionPromise, pendingEvents)\n const duration = Date.now() - startTime\n\n // Flush remaining events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n // Validate output against outputSchema if provided\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n result = parseWithStandardSchema(tool.outputSchema, result)\n }\n\n const finalResult =\n typeof result === 'string' ? safeJsonParse(result) : result || null\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: finalResult,\n duration,\n })\n\n if (middlewareHooks?.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: true,\n duration,\n result: finalResult,\n })\n }\n } catch (error: unknown) {\n const duration = Date.now() - startTime\n\n // Flush remaining events\n while (pendingEvents.length > 0) {\n yield pendingEvents.shift()!\n }\n\n if (error instanceof MiddlewareAbortError) {\n throw error\n }\n\n const message = error instanceof Error ? error.message : 'Unknown error'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: message },\n state: 'output-error',\n duration,\n })\n\n if (middlewareHooks?.onAfterToolCall) {\n await middlewareHooks.onAfterToolCall({\n toolCall,\n tool,\n toolName,\n toolCallId: toolCall.id,\n ok: false,\n duration,\n error,\n })\n }\n }\n}\n\n/**\n * Execute tool calls based on their configuration.\n * Yields CustomEvent chunks during tool execution for real-time progress updates.\n *\n * Handles three cases:\n * 1. Client tools (no execute) - request client to execute\n * 2. Server tools with approval - check approval before executing\n * 3. Normal server tools - execute immediately\n *\n * @param toolCalls - Tool calls from the LLM\n * @param tools - Available tools with their configurations\n * @param approvals - Map of approval decisions (approval.id -> approved boolean)\n * @param clientResults - Map of client-side execution results (toolCallId -> result)\n * @param createCustomEventChunk - Factory to create CustomEvent chunks (optional)\n */\nexport async function* executeToolCalls(\n toolCalls: Array<ToolCall>,\n tools: ReadonlyArray<Tool>,\n approvals: Map<string, boolean> = new Map(),\n clientResults: Map<string, any> = new Map(),\n createCustomEventChunk?: (\n eventName: string,\n value: Record<string, any>,\n ) => CustomEvent,\n middlewareHooks?: ToolExecutionMiddlewareHooks,\n): AsyncGenerator<CustomEvent, ExecuteToolCallsResult, void> {\n const results: Array<ToolResult> = []\n const needsApproval: Array<ApprovalRequest> = []\n const needsClientExecution: Array<ClientToolRequest> = []\n\n // Create tool lookup map\n const toolMap = new Map<string, Tool>()\n for (const tool of tools) {\n toolMap.set(tool.name, tool)\n }\n\n // Batch gating: when any tool in the batch still needs an approval decision,\n // defer all execution so side effects don't happen before the user decides.\n const hasPendingApprovals = toolCalls.some((tc) => {\n const t = toolMap.get(tc.function.name)\n return t?.needsApproval && !approvals.has(`approval_${tc.id}`)\n })\n\n for (const toolCall of toolCalls) {\n const tool = toolMap.get(toolCall.function.name)\n const toolName = toolCall.function.name\n\n if (!tool) {\n // Unknown tool - return error\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: `Unknown tool: ${toolName}` },\n state: 'output-error',\n })\n continue\n }\n\n // Skip non-pending tools while approvals are outstanding\n if (hasPendingApprovals) {\n if (!tool.needsApproval || approvals.has(`approval_${toolCall.id}`)) {\n continue\n }\n }\n\n // Parse arguments, throwing error if invalid JSON\n let input: unknown = {}\n const argsStr = toolCall.function.arguments.trim() || '{}'\n if (argsStr) {\n try {\n const parsed = JSON.parse(argsStr)\n // Normalize null/non-object to {} (e.g. Anthropic empty tool_use blocks)\n input = parsed && typeof parsed === 'object' ? parsed : {}\n } catch (parseError) {\n // If parsing fails, throw error to fail fast\n throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`)\n }\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n input = parseWithStandardSchema(tool.inputSchema, input)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: {\n error: `Input validation failed for tool ${tool.name}: ${message}`,\n },\n state: 'output-error',\n })\n continue\n }\n }\n\n // Create a ToolExecutionContext for this tool call with event emission\n const pendingEvents: Array<CustomEvent> = []\n const context: ToolExecutionContext = {\n toolCallId: toolCall.id,\n emitCustomEvent: (eventName: string, value: Record<string, any>) => {\n if (createCustomEventChunk) {\n pendingEvents.push(\n createCustomEventChunk(eventName, {\n ...value,\n toolCallId: toolCall.id,\n }),\n )\n }\n },\n }\n\n // CASE 1: Client-side tool (no execute function)\n if (!tool.execute) {\n // Check if tool needs approval\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Approved - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Approved but not executed yet - request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval first\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n input,\n approvalId,\n })\n }\n } else {\n // No approval needed - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n }\n continue\n }\n\n // CASE 2: Server tool with approval required\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Apply middleware before-hook for approved tools\n if (middlewareHooks) {\n const decision = await applyBeforeToolCallDecision(\n toolCall,\n tool,\n input,\n toolName,\n middlewareHooks,\n results,\n )\n if (!decision.proceed) continue\n input = decision.input\n }\n\n yield* executeServerTool(\n toolCall,\n tool,\n toolName,\n input,\n context,\n pendingEvents,\n results,\n middlewareHooks,\n )\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n approvalId,\n })\n }\n continue\n }\n\n // CASE 3: Normal server tool - execute immediately\n if (middlewareHooks) {\n const decision = await applyBeforeToolCallDecision(\n toolCall,\n tool,\n input,\n toolName,\n middlewareHooks,\n results,\n )\n if (!decision.proceed) continue\n input = decision.input\n }\n\n yield* executeServerTool(\n toolCall,\n tool,\n toolName,\n input,\n context,\n pendingEvents,\n results,\n middlewareHooks,\n )\n }\n\n return { results, needsApproval, needsClientExecution }\n}\n"],"names":[],"mappings":";AAiBA,SAAS,cAAc,OAAwB;AAC7C,MAAI;AACF,WAAO,KAAK,MAAM,KAAK;AAAA,EACzB,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAkBO,MAAM,6BAA6B,MAAM;AAAA,EAC9C,YAAY,QAAgB;AAC1B,UAAM,MAAM;AACZ,SAAK,OAAO;AAAA,EACd;AACF;AAoCO,MAAM,gBAAgB;AAAA,EAI3B,YAAY,OAA4B;AAHxC,SAAQ,mCAAmB,IAAA;AAIzB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA,EAKA,sBAAsB,OAAiC;AACrD,UAAM,QAAQ,MAAM,SAAS,KAAK,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;AAAA,cACrB;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,cACA;AAAA,YAAA;AAEF,gBAAI,CAAC,SAAS,QAAS;AACvB,oBAAQ,SAAS;AAAA,UACnB;AAEA,iBAAO;AAAA,YACL;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAAA,QAEJ,OAAO;AAEL,kBAAQ,KAAK;AAAA,YACX,YAAY,SAAS;AAAA,YACrB;AAAA,YACA,QAAQ,EAAE,OAAO,+BAAA;AAAA,YACjB,OAAO;AAAA,UAAA,CACR;AAAA,QACH;AAAA,MACF,OAAO;AAEL,sBAAc,KAAK;AAAA,UACjB,YAAY,SAAS;AAAA,UACrB;AAAA,UACA;AAAA,UACA;AAAA,QAAA,CACD;AAAA,MACH;AACA;AAAA,IACF;AAGA,QAAI,iBAAiB;AACnB,YAAM,WAAW,MAAM;AAAA,QACrB;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,QACA;AAAA,MAAA;AAEF,UAAI,CAAC,SAAS,QAAS;AACvB,cAAQ,SAAS;AAAA,IACnB;AAEA,WAAO;AAAA,MACL;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,MACA;AAAA,IAAA;AAAA,EAEJ;AAEA,SAAO,EAAE,SAAS,eAAe,qBAAA;AACnC;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/ai",
3
- "version": "0.10.1",
3
+ "version": "0.10.3",
4
4
  "description": "Core TanStack AI library - Open source AI SDK",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -46,7 +46,7 @@
46
46
  ],
47
47
  "dependencies": {
48
48
  "partial-json": "^0.1.7",
49
- "@tanstack/ai-event-client": "0.2.1"
49
+ "@tanstack/ai-event-client": "0.2.3"
50
50
  },
51
51
  "devDependencies": {
52
52
  "@standard-schema/spec": "^1.1.0",
@@ -731,6 +731,13 @@ class TextEngine<
731
731
  needsClientExecution: executionResult.needsClientExecution,
732
732
  })
733
733
 
734
+ // Build args lookup so buildToolResultChunks can emit TOOL_CALL_START +
735
+ // TOOL_CALL_ARGS before TOOL_CALL_END during continuation re-executions.
736
+ const argsMap = new Map<string, string>()
737
+ for (const tc of pendingToolCalls) {
738
+ argsMap.set(tc.id, tc.function.arguments)
739
+ }
740
+
734
741
  if (
735
742
  executionResult.needsApproval.length > 0 ||
736
743
  executionResult.needsClientExecution.length > 0
@@ -739,6 +746,7 @@ class TextEngine<
739
746
  for (const chunk of this.buildToolResultChunks(
740
747
  executionResult.results,
741
748
  finishEvent,
749
+ argsMap,
742
750
  )) {
743
751
  yield chunk
744
752
  }
@@ -765,6 +773,7 @@ class TextEngine<
765
773
  const toolResultChunks = this.buildToolResultChunks(
766
774
  executionResult.results,
767
775
  finishEvent,
776
+ argsMap,
768
777
  )
769
778
 
770
779
  for (const chunk of toolResultChunks) {
@@ -1080,12 +1089,35 @@ class TextEngine<
1080
1089
  private buildToolResultChunks(
1081
1090
  results: Array<ToolResult>,
1082
1091
  finishEvent: RunFinishedEvent,
1092
+ argsMap?: Map<string, string>,
1083
1093
  ): Array<StreamChunk> {
1084
1094
  const chunks: Array<StreamChunk> = []
1085
1095
 
1086
1096
  for (const result of results) {
1087
1097
  const content = JSON.stringify(result.result)
1088
1098
 
1099
+ // Emit TOOL_CALL_START + TOOL_CALL_ARGS before TOOL_CALL_END so that
1100
+ // the client can reconstruct the full tool call during continuations.
1101
+ if (argsMap) {
1102
+ chunks.push({
1103
+ type: 'TOOL_CALL_START',
1104
+ timestamp: Date.now(),
1105
+ model: finishEvent.model,
1106
+ toolCallId: result.toolCallId,
1107
+ toolName: result.toolName,
1108
+ })
1109
+
1110
+ const args = argsMap.get(result.toolCallId) ?? '{}'
1111
+ chunks.push({
1112
+ type: 'TOOL_CALL_ARGS',
1113
+ timestamp: Date.now(),
1114
+ model: finishEvent.model,
1115
+ toolCallId: result.toolCallId,
1116
+ delta: args,
1117
+ args,
1118
+ })
1119
+ }
1120
+
1089
1121
  chunks.push({
1090
1122
  type: 'TOOL_CALL_END',
1091
1123
  timestamp: Date.now(),
@@ -127,7 +127,10 @@ export class ToolCallManager {
127
127
  for (const [, toolCall] of this.toolCallsMap.entries()) {
128
128
  if (toolCall.id === event.toolCallId) {
129
129
  if (event.input !== undefined) {
130
- toolCall.function.arguments = JSON.stringify(event.input)
130
+ // Normalize null/non-object to {} (e.g. Anthropic empty tool_use blocks)
131
+ const normalized =
132
+ event.input && typeof event.input === 'object' ? event.input : {}
133
+ toolCall.function.arguments = JSON.stringify(normalized)
131
134
  }
132
135
  break
133
136
  }
@@ -167,11 +170,12 @@ export class ToolCallManager {
167
170
  let toolResultContent: string
168
171
  if (tool?.execute) {
169
172
  try {
170
- // Parse arguments (normalize "null" to "{}" for empty tool_use blocks)
173
+ // Parse arguments (normalize null/non-object to {} for empty tool_use blocks)
171
174
  let args: unknown
172
175
  try {
173
176
  const argsString = toolCall.function.arguments.trim() || '{}'
174
- args = JSON.parse(argsString === 'null' ? '{}' : argsString)
177
+ const parsed = JSON.parse(argsString)
178
+ args = parsed && typeof parsed === 'object' ? parsed : {}
175
179
  } catch (parseError) {
176
180
  throw new Error(
177
181
  `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,
@@ -543,7 +547,9 @@ export async function* executeToolCalls(
543
547
  const argsStr = toolCall.function.arguments.trim() || '{}'
544
548
  if (argsStr) {
545
549
  try {
546
- input = JSON.parse(argsStr)
550
+ const parsed = JSON.parse(argsStr)
551
+ // Normalize null/non-object to {} (e.g. Anthropic empty tool_use blocks)
552
+ input = parsed && typeof parsed === 'object' ? parsed : {}
547
553
  } catch (parseError) {
548
554
  // If parsing fails, throw error to fail fast
549
555
  throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`)