{"version":3,"file":"tool-calls.cjs","names":["ToolMessage","parseToolOutput"],"sources":["../../src/stream/tool-calls.ts"],"sourcesContent":["import type { BaseMessage } from \"@langchain/core/messages\";\nimport { ToolMessage } from \"@langchain/core/messages\";\nimport type { AssembledToolCall } from \"../client/stream/handles/tools.js\";\nimport { parseToolOutput } from \"../client/stream/handles/tools.js\";\n\n/**\n * Insert or replace an assembled tool call by call id.\n *\n * ToolCallAssembler mutates its active handle in place as `tool-finished` /\n * `tool-error` events arrive. Publish a fresh object here so framework\n * adapters that pass individual tool calls as props (notably Vue's shallow\n * prop tracking) observe status/output changes.\n */\nexport function upsertToolCall(\n  current: readonly AssembledToolCall[],\n  next: AssembledToolCall\n): AssembledToolCall[] {\n  const snapshot = { ...next };\n  const idx = current.findIndex((toolCall) => toolCall.callId === next.callId);\n  if (idx < 0) return [...current, snapshot];\n  const updated = current.slice();\n  updated[idx] = snapshot;\n  return updated;\n}\n\n/**\n * Backfill unfinished tool-call handles from authoritative ToolMessages in a\n * values snapshot. This covers headless tools whose graph state contains the\n * result even when the `tools` channel omits a matching `tool-finished` event.\n */\nexport function reconcileToolCallsFromMessages(\n  toolCalls: readonly AssembledToolCall[],\n  messages: readonly BaseMessage[]\n): AssembledToolCall[] {\n  let updated: AssembledToolCall[] | undefined;\n  for (const message of messages) {\n    if (!ToolMessage.isInstance(message)) continue;\n    const callId = message.tool_call_id;\n    if (typeof callId !== \"string\" || callId.length === 0) continue;\n\n    const currentToolCalls = updated ?? toolCalls;\n    const idx = currentToolCalls.findIndex(\n      (toolCall) => toolCall.callId === callId\n    );\n    if (idx < 0) continue;\n\n    const current = currentToolCalls[idx];\n    if (current.status === \"finished\" && current.output != null) continue;\n\n    const output = parseToolOutput(message.content);\n    if (output == null) continue;\n\n    updated = upsertToolCall(currentToolCalls, {\n      ...current,\n      output,\n      status: \"finished\",\n      error: undefined,\n    });\n  }\n  return updated ?? (toolCalls as AssembledToolCall[]);\n}\n\n/**\n * Build completed scoped tool-call handles from an authoritative\n * `values.messages` snapshot. Used when an idle thread hydrates card panels\n * from checkpoint history instead of replaying scoped `/events`.\n */\nexport function seedToolCallsFromMessages(\n  namespace: readonly string[],\n  messages: readonly BaseMessage[]\n): AssembledToolCall[] {\n  let toolCalls: AssembledToolCall[] = [];\n  for (const message of messages) {\n    const raw = (message as unknown as { tool_calls?: unknown }).tool_calls;\n    if (!Array.isArray(raw)) continue;\n    for (const toolCall of raw) {\n      if (toolCall == null || typeof toolCall !== \"object\") continue;\n      const record = toolCall as {\n        id?: unknown;\n        name?: unknown;\n        args?: unknown;\n      };\n      if (typeof record.id !== \"string\" || record.id.length === 0) continue;\n      if (typeof record.name !== \"string\" || record.name.length === 0) {\n        continue;\n      }\n      // Mirrors `shouldIgnoreScopedTaskToolEvent`: the wrapper `task` call\n      // is represented by the subagent card itself, not as an inner tool.\n      if (namespace.length > 0 && record.name === \"task\") continue;\n      toolCalls = upsertToolCall(toolCalls, {\n        id: record.id,\n        callId: record.id,\n        name: record.name,\n        namespace: [...namespace, `tools:${record.id}`],\n        input: record.args ?? {},\n        args: record.args ?? {},\n        output: null,\n        status: \"running\",\n        error: undefined,\n      });\n    }\n  }\n  return reconcileToolCallsFromMessages(toolCalls, messages);\n}\n"],"mappings":";;;;;;;;;;;;AAaA,SAAgB,eACd,SACA,MACqB;CACrB,MAAM,WAAW,EAAE,GAAG,MAAM;CAC5B,MAAM,MAAM,QAAQ,WAAW,aAAa,SAAS,WAAW,KAAK,OAAO;AAC5E,KAAI,MAAM,EAAG,QAAO,CAAC,GAAG,SAAS,SAAS;CAC1C,MAAM,UAAU,QAAQ,OAAO;AAC/B,SAAQ,OAAO;AACf,QAAO;;;;;;;AAQT,SAAgB,+BACd,WACA,UACqB;CACrB,IAAI;AACJ,MAAK,MAAM,WAAW,UAAU;AAC9B,MAAI,CAACA,yBAAAA,YAAY,WAAW,QAAQ,CAAE;EACtC,MAAM,SAAS,QAAQ;AACvB,MAAI,OAAO,WAAW,YAAY,OAAO,WAAW,E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