{"version":3,"file":"tool.cjs","names":["BaseMessage","BaseMessageChunk","mergeContent","_mergeDicts","_mergeObj","_mergeStatus"],"sources":["../../src/messages/tool.ts"],"sourcesContent":["import {\n  BaseMessage,\n  BaseMessageChunk,\n  type BaseMessageFields,\n  mergeContent,\n  _mergeDicts,\n  _mergeObj,\n  _mergeStatus,\n} from \"./base.js\";\nimport { $InferMessageContent, MessageStructure } from \"./message.js\";\nimport { Constructor } from \"./utils.js\";\n\nexport interface ToolMessageFields<\n  TStructure extends MessageStructure = MessageStructure,\n> extends BaseMessageFields<TStructure, \"tool\"> {\n  /**\n   * Artifact of the Tool execution which is not meant to be sent to the model.\n   *\n   * Should only be specified if it is different from the message content, e.g. if only\n   * a subset of the full tool output is being passed as message content but the full\n   * output is needed in other parts of the code.\n   */\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  artifact?: any;\n  tool_call_id: string;\n  status?: \"success\" | \"error\";\n  metadata?: Record<string, unknown>;\n}\n\n/**\n * Marker parameter for objects that tools can return directly.\n *\n * If a custom BaseTool is invoked with a ToolCall and the output of custom code is\n * not an instance of DirectToolOutput, the output will automatically be coerced to\n * a string and wrapped in a ToolMessage.\n */\nexport interface DirectToolOutput {\n  readonly lc_direct_tool_output: true;\n}\n\nexport function isDirectToolOutput(x: unknown): x is DirectToolOutput {\n  return (\n    x != null &&\n    typeof x === \"object\" &&\n    \"lc_direct_tool_output\" in x &&\n    x.lc_direct_tool_output === true\n  );\n}\n\n/**\n * Represents a tool message in a conversation.\n */\nexport class ToolMessage<TStructure extends MessageStructure = MessageStructure>\n  extends BaseMessage<TStructure, \"tool\">\n  implements DirectToolOutput\n{\n  static lc_name() {\n    return \"ToolMessage\";\n  }\n\n  get lc_aliases(): Record<string, string> {\n    // exclude snake case conversion to pascal case\n    return { tool_call_id: \"tool_call_id\" };\n  }\n\n  lc_direct_tool_output = true as const;\n\n  readonly type = \"tool\" as const;\n\n  /**\n   * Status of the tool invocation.\n   * @version 0.2.19\n   */\n  status?: \"success\" | \"error\";\n\n  tool_call_id: string;\n\n  metadata?: Record<string, unknown>;\n\n  /**\n   * Artifact of the Tool execution which is not meant to be sent to the model.\n   *\n   * Should only be specified if it is different from the message content, e.g. if only\n   * a subset of the full tool output is being passed as message content but the full\n   * output is needed in other parts of the code.\n   */\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  artifact?: any;\n\n  constructor(\n    fields: $InferMessageContent<TStructure, \"tool\"> | ToolMessageFields,\n    tool_call_id: string,\n    name?: string\n  );\n\n  constructor(fields: ToolMessageFields<TStructure>);\n\n  constructor(\n    fields:\n      | $InferMessageContent<TStructure, \"tool\">\n      | ToolMessageFields<TStructure>,\n    tool_call_id?: string,\n    name?: string\n  ) {\n    const toolMessageFields: ToolMessageFields<TStructure> =\n      typeof fields === \"string\" || Array.isArray(fields)\n        ? ({\n            content: fields,\n            name,\n            tool_call_id: tool_call_id!,\n          } as ToolMessageFields<TStructure>)\n        : fields;\n    super(toolMessageFields);\n    this.tool_call_id = toolMessageFields.tool_call_id;\n    this.artifact = toolMessageFields.artifact;\n    this.status = toolMessageFields.status;\n    this.metadata = toolMessageFields.metadata;\n  }\n\n  /**\n   * Type guard to check if an object is a ToolMessage.\n   * Preserves the MessageStructure type parameter when called with a typed BaseMessage.\n   * @overload When called with a typed BaseMessage, preserves the TStructure type\n   */\n  static isInstance<T extends MessageStructure>(\n    message: BaseMessage<T>\n  ): message is BaseMessage<T> & ToolMessage<T>;\n  /**\n   * Type guard to check if an object is a ToolMessage.\n   * @overload When called with unknown, returns base ToolMessage type\n   */\n  static isInstance(message: unknown): message is ToolMessage;\n  static isInstance<T extends MessageStructure = MessageStructure>(\n    message: BaseMessage<T> | unknown\n  ): message is ToolMessage<T> {\n    return (\n      super.isInstance(message) && (message as { type: string }).type === \"tool\"\n    );\n  }\n\n  override get _printableFields(): Record<string, unknown> {\n    return {\n      ...super._printableFields,\n      tool_call_id: this.tool_call_id,\n      artifact: this.artifact,\n    };\n  }\n}\n\n/**\n * Represents a chunk of a tool message, which can be concatenated\n * with other tool message chunks.\n */\nexport class ToolMessageChunk<\n  TStructure extends MessageStructure = MessageStructure,\n> extends BaseMessageChunk<TStructure, \"tool\"> {\n  readonly type = \"tool\" as const;\n\n  tool_call_id: string;\n\n  /**\n   * Status of the tool invocation.\n   * @version 0.2.19\n   */\n  status?: \"success\" | \"error\";\n\n  /**\n   * Artifact of the Tool execution which is not meant to be sent to the model.\n   *\n   * Should only be specified if it is different from the message content, e.g. if only\n   * a subset of the full tool output is being passed as message content but the full\n   * output is needed in other parts of the code.\n   */\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  artifact?: any;\n\n  constructor(fields: ToolMessageFields<TStructure>) {\n    super(fields);\n    this.tool_call_id = fields.tool_call_id;\n    this.artifact = fields.artifact;\n    this.status = fields.status;\n  }\n\n  static lc_name() {\n    return \"ToolMessageChunk\";\n  }\n\n  concat(chunk: ToolMessageChunk<TStructure>) {\n    const Cls = this.constructor as Constructor<this>;\n    return new Cls({\n      content: mergeContent(this.content, chunk.content),\n      additional_kwargs: _mergeDicts(\n        this.additional_kwargs,\n        chunk.additional_kwargs\n      ),\n      response_metadata: _mergeDicts(\n        this.response_metadata,\n        chunk.response_metadata\n      ),\n      artifact: _mergeObj(this.artifact, chunk.artifact),\n      tool_call_id: this.tool_call_id,\n      id: this.id ?? chunk.id,\n      status: _mergeStatus(this.status, chunk.status),\n    });\n  }\n\n  override get _printableFields(): Record<string, unknown> {\n    return {\n      ...super._printableFields,\n      tool_call_id: this.tool_call_id,\n      artifact: this.artifact,\n    };\n  }\n}\n\nexport interface ToolCall<\n  TName extends string = string,\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  TArgs extends Record<string, any> = Record<string, any>,\n> {\n  readonly type?: \"tool_call\";\n  /**\n   * If provided, an identifier associated with the tool call\n   */\n  id?: string;\n  /**\n   * The name of the tool being called\n   */\n  name: TName;\n  /**\n   * The arguments to the tool call\n   */\n  args: TArgs;\n}\n\n/**\n * A chunk of a tool call (e.g., as part of a stream).\n * When merging ToolCallChunks (e.g., via AIMessageChunk.__add__),\n * all string attributes are concatenated. Chunks are only merged if their\n * values of `index` are equal and not None.\n *\n * @example\n * ```ts\n * const leftChunks = [\n *   {\n *     name: \"foo\",\n *     args: '{\"a\":',\n *     index: 0\n *   }\n * ];\n *\n * const leftAIMessageChunk = new AIMessageChunk({\n *   content: \"\",\n *   tool_call_chunks: leftChunks\n * });\n *\n * const rightChunks = [\n *   {\n *     name: undefined,\n *     args: '1}',\n *     index: 0\n *   }\n * ];\n *\n * const rightAIMessageChunk = new AIMessageChunk({\n *   content: \"\",\n *   tool_call_chunks: rightChunks\n * });\n *\n * const result = leftAIMessageChunk.concat(rightAIMessageChunk);\n * // result.tool_call_chunks is equal to:\n * // [\n * //   {\n * //     name: \"foo\",\n * //     args: '{\"a\":1}'\n * //     index: 0\n * //   }\n * // ]\n * ```\n */\nexport interface ToolCallChunk<TName extends string = string> {\n  readonly type?: \"tool_call_chunk\";\n  /**\n   * If provided, a substring of an identifier for the tool call\n   */\n  id?: string;\n  /**\n   * If provided, a substring of the name of the tool to be called\n   */\n  name?: TName;\n  /**\n   * If provided, a JSON substring of the arguments to the tool call\n   */\n  args?: string;\n  /**\n   * If provided, the index of the tool call in a sequence\n   */\n  index?: number;\n}\n\nexport interface InvalidToolCall<TName extends string = string> {\n  readonly type?: \"invalid_tool_call\";\n  /**\n   * If provided, an identifier associated with the tool call\n   */\n  id?: string;\n  /**\n      /**\n     * The name of the tool being called\n     */\n  name?: TName;\n  /**\n   * The arguments to the tool call\n   */\n  args?: string;\n  /**\n   * An error message associated with the tool call\n   */\n  error?: string;\n  /**\n   * Index of block in aggregate response\n   */\n  index?: string | number;\n}\n\nexport function defaultToolCallParser(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  rawToolCalls: Record<string, any>[]\n): [ToolCall[], InvalidToolCall[]] {\n  const toolCalls: ToolCall[] = [];\n  const invalidToolCalls: InvalidToolCall[] = [];\n  for (const toolCall of rawToolCalls) {\n    if (!toolCall.function) {\n      continue;\n    } else {\n      const functionName = toolCall.function.name;\n      try {\n        const functionArgs = JSON.parse(toolCall.function.arguments);\n        toolCalls.push({\n          name: functionName || \"\",\n          args: functionArgs || {},\n          id: toolCall.id,\n        });\n      } catch {\n        invalidToolCalls.push({\n          name: functionName,\n          args: toolCall.function.arguments,\n          id: toolCall.id,\n          error: \"Malformed args.\",\n        });\n      }\n    }\n  }\n  return [toolCalls, invalidToolCalls];\n}\n\n/**\n * @deprecated Use {@link ToolMessage.isInstance} instead\n */\nexport function isToolMessage(x: unknown): x is ToolMessage {\n  return (\n    typeof x === \"object\" &&\n    x !== null &&\n    \"getType\" in x &&\n    typeof x.getType === \"function\" &&\n    x.getType() === \"tool\"\n  );\n}\n\n/**\n * @deprecated Use {@link ToolMessageChunk.isInstance} instead\n */\nexport function isToolMessageChunk(x: BaseMessageChunk): x is ToolMessageChunk {\n  return x._getType() === \"tool\";\n}\n"],"mappings":";;;;;;;;;;;;AAwCA,SAAgB,mBAAmB,GAAmC;AACpE,QACE,KAAK,QACL,OAAO,MAAM,YACb,2BAA2B,KAC3B,EAAE,0BAA0B;;;;;AAOhC,IAAa,cAAb,cACUA,aAAAA,YAEV;CACE,OAAO,UAAU;AACf,SAAO;;CAGT,IAAI,aAAqC;AAEvC,SAAO,EAAE,cAAc,gBAAgB;;CAGzC,wBAAwB;CAExB,OAAgB;;;;;CAMhB;CAEA;CAEA;;;;;;;;CAUA;CAUA,YACE,QAGA,cACA,MACA;EACA,MAAM,oBACJ,OAAO,WAAW,YAAY,MAAM,QAAQ,OAAO,GAC9C;GACC,SAAS;GACT;GACc;GACf,GACD;AACN,QAAM,kBAAkB;AACxB,OAAK,eAAe,kBAAkB;AACtC,OAAK,WAAW,kBAAkB;AAClC,OAAK,SAAS,kBAAkB;AAChC,OAAK,WAAW,kBAAkB;;CAgBpC,OAAO,WACL,SAC2B;AAC3B,SACE,MAAM,WAAW,QAAQ,IAA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