{"version":3,"file":"function_calling.cjs","names":["toJsonSchema","isLangChainTool"],"sources":["../../src/utils/function_calling.ts"],"sourcesContent":["import {\n  StructuredToolInterface,\n  StructuredToolParams,\n  isLangChainTool,\n} from \"../tools/types.js\";\nimport { FunctionDefinition, ToolDefinition } from \"../language_models/base.js\";\nimport { RunnableToolLike } from \"../runnables/base.js\";\nimport { toJsonSchema } from \"./json_schema.js\";\n\n// These utility functions were moved to a more appropriate location,\n// but we still export them here for backwards compatibility.\nexport {\n  isStructuredTool,\n  isStructuredToolParams,\n  isRunnableToolLike,\n  isLangChainTool,\n} from \"../tools/types.js\";\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a format\n * that is compatible with OpenAI function calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | RunnableToolLike} tool The tool to convert to an OpenAI function.\n * @returns {FunctionDefinition} The inputted tool in OpenAI function format.\n */\nexport function convertToOpenAIFunction(\n  tool: StructuredToolInterface | RunnableToolLike | StructuredToolParams,\n  fields?:\n    | {\n        /**\n         * If `true`, model output is guaranteed to exactly match the JSON Schema\n         * provided in the function definition.\n         */\n        strict?: boolean;\n      }\n    | number\n): FunctionDefinition {\n  // @TODO 0.3.0 Remove the `number` typing\n  const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n  return {\n    name: tool.name,\n    description: tool.description,\n    parameters: toJsonSchema(tool.schema),\n    // Do not include the `strict` field if it is `undefined`.\n    ...(fieldsCopy?.strict !== undefined ? { strict: fieldsCopy.strict } : {}),\n  };\n}\n\n/**\n * Formats a `StructuredTool` or `RunnableToolLike` instance into a\n * format that is compatible with OpenAI tool calling. If `StructuredTool` or\n * `RunnableToolLike` has a zod schema, the output will be converted into a\n * JSON schema, which is then used as the parameters for the OpenAI tool.\n *\n * @param {StructuredToolInterface | Record<string, any> | RunnableToolLike} tool The tool to convert to an OpenAI tool.\n * @returns {ToolDefinition} The inputted tool in OpenAI tool format.\n */\nexport function convertToOpenAITool(\n  // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n  tool: StructuredToolInterface | Record<string, any> | RunnableToolLike,\n  fields?:\n    | {\n        /**\n         * If `true`, model output is guaranteed to exactly match the JSON Schema\n         * provided in the function definition.\n         */\n        strict?: boolean;\n      }\n    | number\n): ToolDefinition {\n  // @TODO 0.3.0 Remove the `number` typing\n  const fieldsCopy = typeof fields === \"number\" ? undefined : fields;\n\n  let toolDef: ToolDefinition | undefined;\n  if (isLangChainTool(tool)) {\n    toolDef = {\n      type: \"function\",\n      function: convertToOpenAIFunction(tool),\n    };\n  } else {\n    toolDef = tool as ToolDefinition;\n  }\n\n  if (fieldsCopy?.strict !== undefined) {\n    // oxlint-disable-next-line @typescript-eslint/no-explicit-any\n    (toolDef.function as any).strict = fieldsCopy.strict;\n  }\n\n  return toolDef;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AA2BA,SAAgB,wBACd,MACA,QASoB;CAEpB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;AAE5D,QAAO;EACL,MAAM,KAAK;EACX,aAAa,KAAK;EAClB,YAAYA,0BAAAA,aAAa,KAAK,OAAO;EAErC,GAAI,YAAY,WAAW,KAAA,IAAY,EAAE,QAAQ,WAAW,QAAQ,GAAG,EAAE;EAC1E;;;;;;;;;;;AAYH,SAAgB,oBAEd,MACA,QASgB;CAEhB,MAAM,aAAa,OAAO,WAAW,WAAW,KAAA,IAAY;CAE5D,IAAI;AACJ,KAAIC,cAAAA,gBAAgB,KAAK,CACvB,WAAU;EACR,MAAM;EACN,UAAU,wBAAwB,KAAK;EACxC;KAED,WAAU;AAGZ,KAAI,YAAY,WAAW,KAAA,EAExB,SAAQ,SAAiB,SAAS,WAAW;AAGhD,QAAO"}