{"version":3,"file":"handlers.cjs","sources":["../../../src/tools/handlers.ts"],"sourcesContent":["/* eslint-disable no-console */\n// src/tools/handlers.ts\nimport { nanoid } from 'nanoid';\nimport { ToolMessage } from '@langchain/core/messages';\nimport type { AnthropicWebSearchResultBlockParam } from '@/llm/anthropic/types';\nimport type { ToolCall, ToolCallChunk } from '@langchain/core/messages/tool';\nimport type { Graph, MultiAgentGraph, StandardGraph } from '@/graphs';\nimport type { AgentContext } from '@/agents/AgentContext';\nimport type * as t from '@/types';\nimport {\n  ToolCallTypes,\n  GraphEvents,\n  StepTypes,\n  Providers,\n  Constants,\n} from '@/common';\nimport {\n  coerceAnthropicSearchResults,\n  isAnthropicWebSearchResult,\n} from '@/tools/search/anthropic';\nimport { formatResultsForLLM } from '@/tools/search/format';\nimport { getMessageId } from '@/messages';\n\nexport async function handleToolCallChunks({\n  graph,\n  stepKey,\n  toolCallChunks,\n  metadata,\n}: {\n  graph: StandardGraph | MultiAgentGraph;\n  stepKey: string;\n  toolCallChunks: ToolCallChunk[];\n  metadata?: Record<string, unknown>;\n}): Promise<void> {\n  let prevStepId: string;\n  let prevRunStep: t.RunStep | undefined;\n  try {\n    prevStepId = graph.getStepIdByKey(stepKey);\n    prevRunStep = graph.getRunStep(prevStepId);\n  } catch {\n    /** Edge Case: If no previous step exists, create a new message creation step */\n    const message_id = getMessageId(stepKey, graph, true) ?? '';\n    prevStepId = await graph.dispatchRunStep(\n      stepKey,\n      {\n        type: StepTypes.MESSAGE_CREATION,\n        message_creation: {\n          message_id,\n        },\n      },\n      metadata\n    );\n    prevRunStep = graph.getRunStep(prevStepId);\n  }\n\n  const _stepId = graph.getStepIdByKey(stepKey);\n\n  /** Edge Case: Tool Call Run Step or `tool_call_ids` never dispatched */\n  const tool_calls: ToolCall[] | undefined =\n    prevStepId && prevRunStep && prevRunStep.type === StepTypes.MESSAGE_CREATION\n      ? []\n      : undefined;\n\n  /**\n   * Feed streaming tool call buffer — accumulate raw arg strings for truncation recovery.\n   *\n   * Provider chunk patterns:\n   * - OpenAI/Anthropic: every chunk has {id, args} → direct append\n   * - Bedrock: START chunk has {id, name, index}, DELTA chunks have {args, index} (no id)\n   *   → use index-to-id mapping to resolve the target buffer entry\n   *\n   * Duplicate detection: LangChain's `streamEvents` can emit the same `on_chat_model_stream`\n   * event multiple times when the model is wrapped in nested runnables. The buffer's `append()`\n   * tracks the last fragment per tool call and returns false for exact duplicates.\n   * When ALL chunks in a batch are duplicates, we skip the entire delta dispatch.\n   */\n  let allDuplicates = true;\n  for (const toolCallChunk of toolCallChunks) {\n    const chunkIndex = toolCallChunk.index;\n\n    // START chunk: has id (and usually name). Store index→id mapping for future DELTA chunks.\n    if (toolCallChunk.id) {\n      if (typeof chunkIndex === 'number') {\n        graph.streamingToolCallBuffer.setIndexMapping(\n          chunkIndex,\n          toolCallChunk.id\n        );\n      }\n      if (toolCallChunk.name) {\n        graph.streamingToolCallBuffer.setToolName(\n          toolCallChunk.id,\n          toolCallChunk.name\n        );\n      }\n      // Append args if present on the same chunk (OpenAI/Anthropic pattern)\n      if (toolCallChunk.args) {\n        const appended = graph.streamingToolCallBuffer.append(\n          toolCallChunk.id,\n          toolCallChunk.args\n        );\n        if (appended) allDuplicates = false;\n      } else {\n        // START chunk without args is never a duplicate (it sets up id/name)\n        allDuplicates = false;\n      }\n    } else if (toolCallChunk.args && typeof chunkIndex === 'number') {\n      // DELTA chunk: no id, but has args + index. Resolve id via index mapping (Bedrock pattern).\n      const resolvedId = graph.streamingToolCallBuffer.getIdByIndex(chunkIndex);\n      if (resolvedId) {\n        const appended = graph.streamingToolCallBuffer.append(\n          resolvedId,\n          toolCallChunk.args\n        );\n        if (appended) allDuplicates = false;\n      }\n    } else {\n      // Chunks without args or index are not duplicates\n      allDuplicates = false;\n    }\n  }\n\n  // If every chunk in this batch was an exact duplicate, skip the delta dispatch entirely.\n  // This prevents the frontend from receiving doubled fragments that corrupt accumulated args.\n  if (allDuplicates && toolCallChunks.length > 0) {\n    return;\n  }\n\n  /** Edge Case: `id` and `name` fields cannot be empty strings */\n  for (const toolCallChunk of toolCallChunks) {\n    if (toolCallChunk.name === '') {\n      toolCallChunk.name = undefined;\n    }\n    if (toolCallChunk.id === '') {\n      toolCallChunk.id = undefined;\n    } else if (\n      tool_calls != null &&\n      toolCallChunk.id != null &&\n      toolCallChunk.name != null\n    ) {\n      tool_calls.push({\n        args: {},\n        id: toolCallChunk.id,\n        name: toolCallChunk.name,\n        type: ToolCallTypes.TOOL_CALL,\n      });\n    }\n  }\n\n  let stepId: string = _stepId;\n  const alreadyDispatched =\n    prevRunStep?.type === StepTypes.MESSAGE_CREATION &&\n    graph.messageStepHasToolCalls.has(prevStepId);\n\n  if (prevRunStep?.type === StepTypes.TOOL_CALLS) {\n    /**\n     * If previous step is already a tool_calls step, use that step ID\n     * This ensures tool call deltas are dispatched to the correct step\n     */\n    stepId = prevStepId;\n  } else if (\n    !alreadyDispatched &&\n    prevRunStep?.type === StepTypes.MESSAGE_CREATION\n  ) {\n    /**\n     * Create tool_calls step as soon as we receive the first tool call chunk\n     * This ensures deltas are always associated with the correct step\n     *\n     * NOTE: We do NOT dispatch an empty text block here because:\n     * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n     * - The tool_calls themselves are sufficient for the step\n     * - Empty content with tool_call_ids gets stored in conversation history\n     *   and causes \"messages must have non-empty content\" errors on replay\n     */\n    graph.messageStepHasToolCalls.set(prevStepId, true);\n    stepId = await graph.dispatchRunStep(\n      stepKey,\n      {\n        type: StepTypes.TOOL_CALLS,\n        tool_calls: tool_calls ?? [],\n      },\n      metadata\n    );\n  }\n\n  await graph.dispatchRunStepDelta(stepId, {\n    type: StepTypes.TOOL_CALLS,\n    tool_calls: toolCallChunks,\n  });\n}\n\nexport const handleToolCalls = async (\n  toolCalls?: ToolCall[],\n  metadata?: Record<string, unknown>,\n  graph?: Graph | StandardGraph | MultiAgentGraph\n): Promise<void> => {\n  if (!graph || !metadata) {\n    console.warn('Graph or metadata not found in `handleToolCalls`');\n    return;\n  }\n\n  if (!toolCalls) {\n    return;\n  }\n\n  if (toolCalls.length === 0) {\n    return;\n  }\n\n  const stepKey = graph.getStepKey(metadata);\n\n  /**\n   * Track whether we've already reused an empty TOOL_CALLS step created by\n   * handleToolCallChunks during streaming. Only reuse it once (for the first\n   * tool call); subsequent parallel tool calls must create their own steps.\n   */\n  let reusedChunkStepId: string | undefined;\n\n  for (const tool_call of toolCalls) {\n    const toolCallId = tool_call.id ?? `toolu_${nanoid()}`;\n    tool_call.id = toolCallId;\n\n    // If this tool call ID was already tracked via handleToolCallChunks,\n    // the step exists but may lack the name (Bedrock sends name only at model end).\n    // Dispatch a delta with the complete data so the client can fill in the name.\n    if (toolCallId && graph.toolCallStepIds.has(toolCallId)) {\n      const existingStepId = graph.toolCallStepIds.get(toolCallId);\n      if (existingStepId != null && existingStepId !== '') {\n        const argsStr =\n          typeof tool_call.args === 'string'\n            ? tool_call.args\n            : JSON.stringify(tool_call.args);\n        await graph.dispatchRunStepDelta(existingStepId, {\n          type: StepTypes.TOOL_CALLS,\n          tool_calls: [{ name: tool_call.name, args: argsStr, id: toolCallId }],\n        });\n      }\n      continue;\n    }\n\n    let prevStepId = '';\n    let prevRunStep: t.RunStep | undefined;\n    try {\n      prevStepId = graph.getStepIdByKey(stepKey);\n      prevRunStep = graph.getRunStep(prevStepId);\n    } catch {\n      // no previous step\n    }\n\n    // If the previous step is TOOL_CALLS (created by handleToolCallChunks),\n    // either reuse it (if empty) or dispatch a new TOOL_CALLS step directly —\n    // skip the intermediate MESSAGE_CREATION to avoid orphaned gaps.\n    if (prevRunStep?.type === StepTypes.TOOL_CALLS) {\n      const details = prevRunStep.stepDetails as t.ToolCallsDetails;\n      const isEmpty = !details.tool_calls || details.tool_calls.length === 0;\n      if (isEmpty && prevStepId !== reusedChunkStepId) {\n        graph.toolCallStepIds.set(toolCallId, prevStepId);\n        reusedChunkStepId = prevStepId;\n        continue;\n      }\n      await graph.dispatchRunStep(\n        stepKey,\n        { type: StepTypes.TOOL_CALLS, tool_calls: [tool_call] },\n        metadata\n      );\n      continue;\n    }\n\n    /**\n     * NOTE: We do NOT dispatch empty text blocks with tool_call_ids because:\n     * - Empty text blocks cause providers (Anthropic, Bedrock) to reject messages\n     * - They get stored in conversation history and cause errors on replay:\n     *   \"messages must have non-empty content\" (Anthropic)\n     *   \"The content field in the Message object is empty\" (Bedrock)\n     * - The tool_calls themselves are sufficient\n     */\n\n    /* If the previous step exists and is a message creation */\n    if (prevStepId && prevRunStep) {\n      graph.messageStepHasToolCalls.set(prevStepId, true);\n      /* If the previous step doesn't exist */\n    } else if (!prevRunStep) {\n      const messageId = getMessageId(stepKey, graph, true) ?? '';\n      const stepId = await graph.dispatchRunStep(\n        stepKey,\n        {\n          type: StepTypes.MESSAGE_CREATION,\n          message_creation: {\n            message_id: messageId,\n          },\n        },\n        metadata\n      );\n      graph.messageStepHasToolCalls.set(stepId, true);\n    }\n\n    await graph.dispatchRunStep(\n      stepKey,\n      {\n        type: StepTypes.TOOL_CALLS,\n        tool_calls: [tool_call],\n      },\n      metadata\n    );\n  }\n};\n\nexport const toolResultTypes = new Set([\n  // 'tool_use',\n  // 'server_tool_use',\n  // 'input_json_delta',\n  'tool_result',\n  'web_search_result',\n  'web_search_tool_result',\n]);\n\n/**\n * Handles the result of a server tool call; in other words, a provider's built-in tool.\n * As of 2025-07-06, only Anthropic handles server tool calls with this pattern.\n */\nexport async function handleServerToolResult({\n  graph,\n  content,\n  metadata,\n  agentContext,\n}: {\n  graph: StandardGraph | MultiAgentGraph;\n  content?: string | t.MessageContentComplex[];\n  metadata?: Record<string, unknown>;\n  agentContext?: AgentContext;\n}): Promise<boolean> {\n  let skipHandling = false;\n  if (agentContext?.provider !== Providers.ANTHROPIC) {\n    return skipHandling;\n  }\n  if (\n    typeof content === 'string' ||\n    content == null ||\n    content.length === 0 ||\n    (content.length === 1 &&\n      (content[0] as t.ToolResultContent).tool_use_id == null)\n  ) {\n    return skipHandling;\n  }\n\n  for (const contentPart of content) {\n    const toolUseId = (contentPart as t.ToolResultContent).tool_use_id;\n    if (toolUseId == null || toolUseId === '') {\n      continue;\n    }\n    const stepId = graph.toolCallStepIds.get(toolUseId);\n    if (stepId == null || stepId === '') {\n      console.warn(\n        `Tool use ID ${toolUseId} not found in graph, cannot dispatch tool result.`\n      );\n      continue;\n    }\n    const runStep = graph.getRunStep(stepId);\n    if (!runStep) {\n      console.warn(\n        `Run step for ${stepId} does not exist, cannot dispatch tool result.`\n      );\n      continue;\n    } else if (runStep.type !== StepTypes.TOOL_CALLS) {\n      console.warn(\n        `Run step for ${stepId} is not a tool call step, cannot dispatch tool result.`\n      );\n      continue;\n    }\n\n    const toolCall =\n      runStep.stepDetails.type === StepTypes.TOOL_CALLS\n        ? (runStep.stepDetails.tool_calls?.find(\n            (toolCall) => toolCall.id === toolUseId\n          ) as ToolCall)\n        : undefined;\n\n    if (!toolCall) {\n      continue;\n    }\n\n    if (\n      contentPart.type === 'web_search_result' ||\n      contentPart.type === 'web_search_tool_result'\n    ) {\n      await handleAnthropicSearchResults({\n        contentPart: contentPart as t.ToolResultContent,\n        toolCall,\n        metadata,\n        graph,\n      });\n    }\n\n    if (!skipHandling) {\n      skipHandling = true;\n    }\n  }\n\n  return skipHandling;\n}\n\nasync function handleAnthropicSearchResults({\n  contentPart,\n  toolCall,\n  metadata,\n  graph,\n}: {\n  contentPart: t.ToolResultContent;\n  toolCall: Partial<ToolCall>;\n  metadata?: Record<string, unknown>;\n  graph: StandardGraph | MultiAgentGraph;\n}): Promise<void> {\n  if (!Array.isArray(contentPart.content)) {\n    console.warn(\n      `Expected content to be an array, got ${typeof contentPart.content}`\n    );\n    return;\n  }\n\n  if (!isAnthropicWebSearchResult(contentPart.content[0])) {\n    console.warn(\n      `Expected content to be an Anthropic web search result, got ${JSON.stringify(\n        contentPart.content\n      )}`\n    );\n    return;\n  }\n\n  const turn = graph.invokedToolIds?.size ?? 0;\n  const searchResultData = coerceAnthropicSearchResults({\n    turn,\n    results: contentPart.content as AnthropicWebSearchResultBlockParam[],\n  });\n\n  const name = toolCall.name;\n  const input = toolCall.args ?? {};\n  const artifact = {\n    [Constants.WEB_SEARCH]: searchResultData,\n  };\n  const { output: formattedOutput } = formatResultsForLLM(\n    turn,\n    searchResultData\n  );\n  const output = new ToolMessage({\n    name,\n    artifact,\n    content: formattedOutput,\n    tool_call_id: toolCall.id!,\n  });\n  const toolEndData: t.ToolEndData = {\n    input,\n    output,\n  };\n  await graph.handlerRegistry\n    ?.getHandler(GraphEvents.TOOL_END)\n    ?.handle(GraphEvents.TOOL_END, toolEndData, metadata, graph);\n\n  if (graph.invokedToolIds == null) {\n    graph.invokedToolIds = new Set<string>();\n  }\n\n  graph.invokedToolIds.add(toolCall.id!);\n}\n"],"names":["getMessageId","StepTypes","ToolCallTypes","nanoid","Providers","isAnthropicWebSearchResult","coerceAnthropicSearchResults","Constants","formatResultsForLLM","ToolMessage","GraphEvents"],"mappings":";;;;;;;;;;;;AAAA;AACA;AAsBO,eAAe,oBAAoB,CAAC,EACzC,KAAK,EACL,OAAO,EACP,cAAc,EACd,QAAQ,GAMT,EAAA;AACC,IAAA,IAAI,UAAkB;AACtB,IAAA,IAAI,WAAkC;AACtC,IAAA,IAAI;AACF,QAAA,UAAU,GAAG,KAAK,CAAC,cAAc,CAAC,OAAO,CAAC;AAC1C,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU,CAAC;IAC5C;AAAE,IAAA,MAAM;;AAEN,QAAA,MAAM,UAAU,GAAGA,gBAAY,CAAC,OAAO,EAAE,KAAK,EAAE,IAAI,CAAC,IAAI,EAAE;AAC3D,QAAA,UAAU,GAAG,MAAM,KAAK,CAAC,eAAe,CACtC,OAAO,EACP;YACE,IAAI,EAAEC,eAAS,CAAC,gBAAgB;AAChC,YAAA,gBAAgB,EAAE;gBAChB,UAAU;AACX,aAAA;SACF,EACD,QAAQ,CACT;AACD,QAAA,WAAW,GAAG,KAAK,CAAC,UAAU,CAAC,UAAU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