{"version":3,"file":"tools.cjs","names":["tools: GoogleGenerativeAIToolType[]","extra?: {\n    toolChoice?: ToolChoice;\n    allowedFunctionNames?: string[];\n  }","functionDeclarationTools: FunctionDeclaration[]","genAITools: GenerativeAITool[]","convertToGenerativeAITools","tool: ToolDefinition","removeAdditionalProperties","modeMap: Record<string, FunctionCallingMode>","FunctionCallingMode"],"sources":["../../src/utils/tools.ts"],"sourcesContent":["import {\n  Tool as GenerativeAITool,\n  ToolConfig,\n  FunctionCallingMode,\n  FunctionDeclaration,\n  FunctionDeclarationsTool,\n  FunctionDeclarationSchema,\n} from \"@google/generative-ai\";\nimport { ToolChoice } from \"@langchain/core/language_models/chat_models\";\nimport { StructuredToolInterface } from \"@langchain/core/tools\";\nimport { isLangChainTool } from \"@langchain/core/utils/function_calling\";\nimport {\n  isOpenAITool,\n  ToolDefinition,\n} from \"@langchain/core/language_models/base\";\nimport { convertToGenerativeAITools } from \"./common.js\";\nimport { GoogleGenerativeAIToolType } from \"../types.js\";\nimport { removeAdditionalProperties } from \"./zod_to_genai_parameters.js\";\n\nexport function convertToolsToGenAI(\n  tools: GoogleGenerativeAIToolType[],\n  extra?: {\n    toolChoice?: ToolChoice;\n    allowedFunctionNames?: string[];\n  }\n): {\n  tools: GenerativeAITool[];\n  toolConfig?: ToolConfig;\n} {\n  // Extract function declaration processing to a separate function\n  const genAITools = processTools(tools);\n\n  // Simplify tool config creation\n  const toolConfig = createToolConfig(genAITools, extra);\n\n  return { tools: genAITools, toolConfig };\n}\n\nfunction processTools(tools: GoogleGenerativeAIToolType[]): GenerativeAITool[] {\n  let functionDeclarationTools: FunctionDeclaration[] = [];\n  const genAITools: GenerativeAITool[] = [];\n\n  tools.forEach((tool) => {\n    if (isLangChainTool(tool)) {\n      const [convertedTool] = convertToGenerativeAITools([\n        tool as StructuredToolInterface,\n      ]);\n      if (convertedTool.functionDeclarations) {\n        functionDeclarationTools.push(...convertedTool.functionDeclarations);\n      }\n    } else if (isOpenAITool(tool)) {\n      const { functionDeclarations } = convertOpenAIToolToGenAI(tool);\n      if (functionDeclarations) {\n        functionDeclarationTools.push(...functionDeclarations);\n      } else {\n        throw new Error(\n          \"Failed to convert OpenAI structured tool to GenerativeAI tool\"\n        );\n      }\n    } else {\n      genAITools.push(tool as GenerativeAITool);\n    }\n  });\n\n  const genAIFunctionDeclaration = genAITools.find(\n    (t) => \"functionDeclarations\" in t\n  );\n  if (genAIFunctionDeclaration) {\n    return genAITools.map((tool) => {\n      if (\n        functionDeclarationTools?.length > 0 &&\n        \"functionDeclarations\" in tool\n      ) {\n        const newTool = {\n          functionDeclarations: [\n            ...(tool.functionDeclarations || []),\n            ...functionDeclarationTools,\n          ],\n        };\n        // Clear the functionDeclarationTools array so it is not passed again\n        functionDeclarationTools = [];\n        return newTool;\n      }\n      return tool;\n    });\n  }\n\n  return [\n    ...genAITools,\n    ...(functionDeclarationTools.length > 0\n      ? [\n          {\n            functionDeclarations: functionDeclarationTools,\n          },\n        ]\n      : []),\n  ];\n}\n\nfunction convertOpenAIToolToGenAI(\n  tool: ToolDefinition\n): FunctionDeclarationsTool {\n  return {\n    functionDeclarations: [\n      {\n        name: tool.function.name,\n        description: tool.function.description,\n        parameters: removeAdditionalProperties(\n          tool.function.parameters\n        ) as FunctionDeclarationSchema,\n      },\n    ],\n  };\n}\n\nfunction createToolConfig(\n  genAITools: GenerativeAITool[],\n  extra?: {\n    toolChoice?: ToolChoice;\n    allowedFunctionNames?: string[];\n  }\n): ToolConfig | undefined {\n  if (!genAITools.length || !extra) return undefined;\n\n  const { toolChoice, allowedFunctionNames } = extra;\n\n  const modeMap: Record<string, FunctionCallingMode> = {\n    any: FunctionCallingMode.ANY,\n    auto: FunctionCallingMode.AUTO,\n    none: FunctionCallingMode.NONE,\n  };\n\n  if (toolChoice && [\"any\", \"auto\", \"none\"].includes(toolChoice as string)) {\n    return {\n      functionCallingConfig: {\n        mode: modeMap[toolChoice as keyof typeof modeMap] ?? \"MODE_UNSPECIFIED\",\n        allowedFunctionNames,\n      },\n    };\n  }\n\n  if (typeof toolChoice === \"string\" || allowedFunctionNames) {\n    return {\n      functionCallingConfig: {\n        mode: FunctionCallingMode.ANY,\n        allowedFunctionNames: [\n          ...(allowedFunctionNames ?? []),\n          ...(toolChoice && typeof toolChoice === \"string\" ? [toolChoice] : []),\n        ],\n      },\n    };\n  }\n\n  return undefined;\n}\n"],"mappings":";;;;;;;;AAmBA,SAAgB,oBACdA,OACAC,OAOA;CAEA,MAAM,aAAa,aAAa,MAAM;CAGtC,MAAM,aAAa,iBAAiB,YAAY,MAAM;AAEtD,QAAO;EAAE,OAAO;EAAY;CAAY;AACzC;AAED,SAAS,aAAaD,OAAyD;CAC7E,IAAIE,2BAAkD,CAAE;CACxD,MAAMC,aAAiC,CAAE;CAEzC,MAAM,QAAQ,CAAC,SAAS;AACtB,mEAAoB,KAAK,EAAE;GACzB,MAAM,CAAC,cAAc,GAAGC,0CAA2B,CACjD,IACD,EAAC;AACF,OAAI,cAAc,sBAChB,yBAAyB,KAAK,GAAG,cAAc,qBAAqB;EAEvE,mEAAuB,KAAK,EAAE;GAC7B,MAAM,EAAE,sBAAsB,GAAG,yBAAyB,KAAK;AAC/D,OAAI,sBACF,yBAAyB,KAAK,GAAG,qBAAqB;OAEtD,OAAM,IAAI,MACR;EAGL,OACC,WAAW,KAAK,KAAyB;CAE5C,EAAC;CAEF,MAAM,2BAA2B,WAAW,KAC1C,CAAC,MAAM,0BAA0B,EAClC;AACD,KAAI,yBACF,QAAO,WAAW,IAAI,CAAC,SAAS;AAC9B,MACE,0BAA0B,SAAS,KACnC,0BAA0B,MAC1B;GACA,MAAM,UAAU,EACd,sBAAsB,CACpB,GAAI,KAAK,wBAAwB,CAAE,GACnC,GAAG,wBACJ,EACF;GAED,2BAA2B,CAAE;AAC7B,UAAO;EACR;AACD,SAAO;CACR,EAAC;AAGJ,QAAO,CACL,GAAG,YACH,GAAI,yBAAyB,SAAS,IAClC,CACE,EACE,sBAAsB,yBACvB,CACF,IACD,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