{"version":3,"file":"tools.cjs","names":[],"sources":["../../src/utils/tools.ts"],"sourcesContent":["import type {\n  Message,\n  AIMessage,\n  ToolMessage,\n  ToolCallState,\n  ToolCallWithResult,\n  DefaultToolCall,\n} from \"../types.messages.js\";\n\n/**\n * Extracts tool calls with their results from a list of messages.\n *\n * @template ToolCall The type of tool calls.\n * @param messages The list of messages to extract tool calls from.\n * @returns An array of ToolCallWithResult objects.\n *\n * @example\n * ```ts\n * const toolCalls = getToolCallsWithResults(messages);\n * for (const { call, result } of toolCalls) {\n *   if (call.name === \"get_weather\") {\n *     console.log(`Weather for ${call.args.location}:`, result?.content);\n *   }\n * }\n * ```\n */\n/**\n * Computes the lifecycle state of a tool call based on its result.\n */\nfunction computeToolCallState(result: ToolMessage | undefined): ToolCallState {\n  if (!result) return \"pending\";\n  return result.status === \"error\" ? \"error\" : \"completed\";\n}\n\nexport function getToolCallsWithResults<ToolCall = DefaultToolCall>(\n  messages: Message<ToolCall>[]\n): ToolCallWithResult<ToolCall>[] {\n  const results: ToolCallWithResult<ToolCall>[] = [];\n\n  // Create a map of tool_call_id to ToolMessage for quick lookup\n  const toolResultsById = new Map<string, ToolMessage>();\n  for (const msg of messages) {\n    if (msg.type === \"tool\") {\n      toolResultsById.set(msg.tool_call_id, msg);\n    }\n  }\n\n  // Find all AI messages with tool calls and pair them with results\n  for (const msg of messages) {\n    if (msg.type === \"ai\" && msg.tool_calls && msg.tool_calls.length > 0) {\n      const aiMessage = msg as AIMessage<ToolCall>;\n      for (let i = 0; i < aiMessage.tool_calls!.length; i += 1) {\n        const call = aiMessage.tool_calls![i] as ToolCall & { id?: string };\n        const callId = call.id as string | undefined;\n        const result = callId ? toolResultsById.get(callId) : undefined;\n\n        results.push({\n          id: callId ?? `${aiMessage.id ?? \"unknown\"}-${i}`,\n          call,\n          result,\n          aiMessage,\n          index: i,\n          state: computeToolCallState(result),\n        });\n      }\n    }\n  }\n\n  return results;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA6BA,SAAS,qBAAqB,QAAgD;AAC5E,KAAI,CAAC,OAAQ,QAAO;AACpB,QAAO,OAAO,WAAW,UAAU,UAAU;;AAG/C,SAAgB,wBACd,UACgC;CAChC,MAAM,UAA0C,EAAE;CAGlD,MAAM,kCAAkB,IAAI,KAA0B;AACtD,MAAK,MAAM,OAAO,SAChB,KAAI,IAAI,SAAS,OACf,iBAAgB,IAAI,IAAI,cAAc,IAAI;AAK9C,MAAK,MAAM,OAAO,SAChB,KAAI,IAAI,SAAS,QAAQ,IAAI,cAAc,IAAI,WAAW,SAAS,GAAG;EACpE,MAAM,YAAY;AAClB,OAAK,IAAI,IAAI,GAAG,IAAI,UAAU,WAAY,QAAQ,KAAK,GAAG;GACxD,MAAM,OAAO,UAAU,WAAY;GACnC,MAAM,SAAS,KAAK;GACpB,MAAM,SAAS,SAAS,gBAAgB,IAAI,OAAO,GAAG;AAEtD,WAAQ,KAAK;IACX,IAAI,UAAU,GAAG,UAAU,MAAM,UAAU,GAAG;IAC9C;IACA;IACA;IACA,OAAO;IACP,OAAO,qBAAqB,OAAO;IACpC,CAAC;;;AAKR,QAAO"}