{"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"],"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,EAAG;EAEvD,MAAM,mBAAmB,WAAW;EACpC,MAAM,MAAM,iBAAiB,WAC1B,aAAa,SAAS,WAAW,OACnC;AACD,MAAI,MAAM,EAAG;EAEb,MAAM,UAAU,iBAAiB;AACjC,MAAI,QAAQ,WAAW,cAAc,QAAQ,UAAU,KAAM;EAE7D,MAAM,SAASC,cAAAA,gBAAgB,QAAQ,QAAQ;AAC/C,MAAI,UAAU,KAAM;AAEpB,YAAU,eAAe,kBAAkB;GACzC,GAAG;GACH;GACA,QAAQ;GACR,OAAO,KAAA;GACR,CAAC;;AAEJ,QAAO,WAAY"}