@langchain/google 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"params.cjs","names":[],"sources":["../../src/converters/params.ts"],"sourcesContent":["import type {\n ChatGoogleFields,\n GooglePlatformType,\n SimplifiedSpeechConfig,\n SimplifiedSpeechLanguageConfig,\n SpeechSpeakerName,\n SpeechVoice,\n SpeechVoiceLanguage,\n} from \"../chat_models/types.js\";\nimport type { Gemini } from \"../chat_models/api-types.js\";\n\n/**\n * Converts a reasoning \"effort\" level value (e.g., \"minimal\", \"low\", \"medium\", \"high\")\n * to a corresponding reasoning token budget, depending on the target Gemini model.\n *\n * This mapping is essential when configuring Google Gemini API calls with reasoning\n * effort/level fields (as in the Gemini OpenAI-compatible API) or when translating\n * user-friendly configuration to explicit token budgets.\n *\n * - The mapping is refined to reflect the constraints documented by Google:\n * - For \"gemini-2.5-flash\" models, token budgets may be set as low as 0 and up to 24k.\n * - For other Gemini models, minimum is 128, maximum is 32k.\n * - Standard effort levels and their token mappings:\n * - \"none\" / \"minimal\": lowest allowed (minTokens)\n * - \"low\": 1k tokens\n * - \"medium\": 8k tokens\n * - \"high\": maximum allowed for the model (maxTokens)\n * - See reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} modelName\n * The Gemini model name (e.g., \"gemini-2.5-flash\"). Used to determine token range constraints.\n * @param {string | undefined} effort\n * The reasoning effort level: one of \"none\", \"minimal\", \"low\", \"medium\", \"high\"\n * (case-insensitive). If undefined, the function returns undefined.\n * @returns {number | undefined}\n * The corresponding token count for the given effort and model. Returns undefined if\n * the effort is not recognized or not provided.\n *\n * @example\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"medium\") // 8192\n * convertReasoningEffortToReasoningTokens(\"gemini-1.5-pro\", \"high\") // 32768\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"none\") // 0\n * convertReasoningEffortToReasoningTokens(undefined, \"low\") // 1024\n */\nexport function convertReasoningEffortToReasoningTokens(\n modelName?: string,\n effort?: string\n): number | undefined {\n if (effort === undefined) {\n return undefined;\n }\n\n // gemini-2.5-flash and -flash-lite can be disabled. Others can't.\n // https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n const minTokens: number = modelName?.startsWith(\"gemini-2.5-flash\") ? 0 : 128;\n const maxTokens: number = modelName?.startsWith(\"gemini-2.5-flash\")\n ? 24 * 1024\n : 32 * 1024;\n\n const normalizedEffort = effort.toLowerCase();\n switch (normalizedEffort) {\n case \"none\":\n case \"minimal\":\n return minTokens;\n case \"low\":\n // Defined as 1k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 1024;\n case \"medium\":\n // Defined as 8k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 8 * 1024;\n case \"high\":\n // Defined as 24k or 32k (model-dependent) by https://ai.google.dev/gemini-api/docs/openai#thinking\n return maxTokens;\n default:\n return undefined;\n }\n}\n\n/**\n * Converts a reasoning token budget (e.g., 0, 1024, 8192, 32768) to its corresponding\n * reasoning \"effort\" level, as used in the Google Gemini API (\"minimal\", \"low\", \"medium\", \"high\").\n *\n * This is the inverse of {@link convertReasoningEffortToReasoningTokens}. It is useful for\n * translating explicit numeric settings (often surfaced in raw API responses or configs)\n * into user-facing friendly reasoning levels/labels. The mapping relies on Google's\n * documented reasoning levels for different models:\n *\n * - -1 means \"high\" effort (per API documentation: the default, dynamic setting).\n * - 0 means \"minimal\" unless the model is \"gemini-3-pro\", for which it maps to \"low\".\n * - <= 1024 tokens: \"low\"\n * - <= 8192 tokens: \"medium\" (unless model is \"gemini-3-pro\", for which it reverts to \"low\")\n * - > 8192: \"high\"\n *\n * Reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} model\n * The Gemini model name (e.g., \"gemini-3-pro\"). Used for model-specific special cases.\n * @param {number | undefined} reasoningTokens\n * The token budget to convert. If undefined, the function returns undefined.\n * @returns {Lowercase<Gemini.ThinkingLevel> | undefined}\n * The reasoning \"effort\" level (\"minimal\", \"low\", \"medium\", \"high\") or undefined if input is not valid.\n *\n * @example\n * convertReasoningTokensToReasoningEffort(\"gemini-3-pro\", 0) // \"low\"\n * convertReasoningTokensToReasoningEffort(\"gemini-1.5-pro\", 0) // \"minimal\"\n * convertReasoningTokensToReasoningEffort(undefined, 1024) // \"low\"\n * convertReasoningTokensToReasoningEffort(undefined, -1) // \"high\"\n * convertReasoningTokensToReasoningEffort(undefined, 9000) // \"high\"\n */\nexport function convertReasoningTokensToReasoningEffort(\n model?: string,\n reasoningTokens?: number\n): Gemini.ThinkingLevel | undefined {\n if (typeof reasoningTokens === \"undefined\") {\n return undefined;\n } else if (reasoningTokens === -1) {\n // -1 means \"high\"/dynamic (\"default\" per https://ai.google.dev/gemini-api/docs/thinking#thinking-levels)\n return \"high\";\n } else if (reasoningTokens === 0) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"minimal\";\n }\n } else if (reasoningTokens <= 1024) {\n return \"low\";\n } else if (reasoningTokens <= 8192) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"medium\";\n }\n } else {\n return \"high\";\n }\n}\n\n/**\n * Infers and returns the Google platform type from the provided chat model parameters.\n *\n * This function helps determine which Google platform (e.g., Vertex AI on GCP or the public API)\n * should be used for calling Gemini/PaLM endpoints, based on the options provided in\n * {@link BaseChatGoogleParams} (such as model constructors or call options).\n */\nexport function convertParamsToPlatformType(params: {\n platformType?: GooglePlatformType;\n vertexai?: boolean;\n}): GooglePlatformType | undefined {\n if (typeof params === \"undefined\") {\n return undefined;\n }\n if (typeof params.platformType !== \"undefined\") {\n return params.platformType;\n }\n if (params.vertexai === true) {\n return \"gcp\";\n }\n return undefined;\n}\n\n/**\n * Returns the model family from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"gemini\"\n */\nexport function getModelFamily(model: string): string {\n return model.split(\"-\")[0];\n}\n\n/**\n * Returns the model version from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"2.5\"\n */\nexport function getModelVersion(model: string): string {\n return model.split(\"-\")[1];\n}\n\n/**\n * Returns the model level from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"flash\", \"gemini-2.5-flash-lite\" → \"flash-lite\"\n */\nexport function getModelLevel(model: string): string {\n const parts = model.split(\"-\");\n let ret = parts[2];\n if (ret === \"flash\" && parts[3] === \"lite\") {\n ret = \"flash-lite\";\n }\n return ret;\n}\n\n/**\n * Returns the model specialty from a Gemini model name.\n * e.g. \"gemini-2.5-flash-preview-image\" → \"image\",\n * \"gemini-2.5-flash-preview-tts\" → \"tts\",\n * \"gemini-2.5-flash\" → \"\"\n */\nexport function getModelSpecialty(model: string): string {\n if (model.includes(\"image\")) {\n return \"image\";\n } else if (model.includes(\"tts\")) {\n return \"tts\";\n } else {\n return \"\";\n }\n}\n\n/**\n * Builds the `thinkingConfig` object for the Gemini `generationConfig`,\n * translating user-facing reasoning fields into the wire format.\n *\n * @param model The full Gemini model name (e.g. \"gemini-2.5-flash\").\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.ThinkingConfig}, or `{}`\n * when thinking should not be explicitly configured.\n */\nexport function convertFieldsToThinkingConfig(\n model: string,\n fields: ChatGoogleFields\n): Gemini.ThinkingConfig | undefined {\n // Thinking / reasoning\n let includeThoughts = true;\n\n let thinkingBudget =\n fields?.maxReasoningTokens ??\n fields?.thinkingBudget ??\n convertReasoningEffortToReasoningTokens(\n model,\n fields?.reasoningEffort ?? fields?.thinkingLevel\n );\n if (\n thinkingBudget === 0 ||\n (model.includes(\"pro\") && thinkingBudget === 128)\n ) {\n includeThoughts = false;\n }\n if (\n model.startsWith(\"gemini-2.5-pro\") &&\n typeof thinkingBudget !== \"undefined\"\n ) {\n // Can't turn off Gemini 2.5 Pro thinking completely\n if (thinkingBudget >= 0 && thinkingBudget < 128) {\n thinkingBudget = 128;\n }\n }\n\n const thinkingLevelRaw =\n fields?.reasoningEffort ??\n fields?.thinkingLevel ??\n convertReasoningTokensToReasoningEffort(\n model,\n fields?.maxReasoningTokens ?? fields?.thinkingBudget\n );\n let thinkingLevel = thinkingLevelRaw?.toUpperCase();\n if (thinkingLevel === \"MINIMAL\") {\n includeThoughts = false;\n }\n if (model.startsWith(\"gemini-3-pro\")) {\n // Gemini 3 Pro has only low and high.\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n } else if (thinkingLevel === \"MEDIUM\") {\n thinkingLevel = \"HIGH\";\n }\n }\n\n // If we are using a model that doesn't support thinking at all (gemini 2.5 imaging)\n // or we haven't explicitly tried to set a thinking budget/level, then bail out.\n const modelVersion = getModelVersion(model);\n const modelSpecialty = getModelSpecialty(model);\n const thoughtsNotSupported =\n modelVersion === \"2.5\" && modelSpecialty === \"image\";\n if (\n thoughtsNotSupported ||\n typeof thinkingBudget === \"undefined\" ||\n typeof thinkingLevel === \"undefined\"\n ) {\n return undefined;\n }\n\n // If we have gotten this far, then we want to explicitly set if we include thoughts or not.\n const thinkingConfig: Gemini.ThinkingConfig = {\n includeThoughts,\n };\n\n // Explicitly setting the budget/level is only valid (currently) for text models.\n if (modelSpecialty === \"\") {\n if (model.startsWith(\"gemini-2.5\")) {\n thinkingConfig.thinkingBudget = thinkingBudget;\n } else {\n thinkingConfig.thinkingLevel = thinkingLevel as Gemini.ThinkingLevel;\n }\n }\n\n return thinkingConfig;\n}\n\n/**\n * Builds the `speechConfig` object for the Gemini `generationConfig`,\n * normalising the user-friendly simplified forms into the wire format.\n *\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.SpeechConfig}, or `{}`\n * when no speech config was provided.\n */\nexport function convertFieldsToSpeechConfig(\n fields: ChatGoogleFields\n): Gemini.SpeechConfig | undefined {\n const config: Gemini.SpeechConfig | SimplifiedSpeechConfig | undefined =\n fields.speechConfig;\n if (typeof config === \"undefined\") {\n return undefined;\n }\n\n function isSpeechConfig(\n config: Gemini.SpeechConfig | SimplifiedSpeechConfig\n ): config is Gemini.SpeechConfig {\n return (\n typeof config === \"object\" &&\n (Object.hasOwn(config, \"voiceConfig\") ||\n Object.hasOwn(config, \"multiSpeakerVoiceConfig\"))\n );\n }\n\n function hasLanguage(\n config: SimplifiedSpeechConfig\n ): config is SimplifiedSpeechLanguageConfig {\n return typeof config === \"object\" && Object.hasOwn(config, \"languageCode\");\n }\n\n function hasVoice(\n config: SimplifiedSpeechLanguageConfig\n ): config is SpeechVoiceLanguage {\n return Object.hasOwn(config, \"voice\");\n }\n\n // If this is already a SpeechConfig, just return it\n if (isSpeechConfig(config)) {\n return config;\n }\n\n let languageCode: string | undefined;\n let voice: SpeechVoice;\n if (hasLanguage(config)) {\n languageCode = config.languageCode;\n voice = hasVoice(config) ? config.voice : config.voices;\n } else {\n languageCode = undefined;\n voice = config;\n }\n\n let ret: Gemini.SpeechConfig;\n\n if (typeof voice === \"string\") {\n // They just provided the prebuilt voice configuration name. Use it.\n ret = {\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: voice,\n },\n },\n };\n } else {\n // This is multi-speaker, so we have speaker/name pairs\n // If we have just one (why?), turn it into an array for the moment\n const voices: SpeechSpeakerName[] = Array.isArray(voice) ? voice : [voice];\n // Go through all the speaker/name pairs and turn this into the voice config array\n const speakerVoiceConfigs: Gemini.SpeakerVoiceConfig[] = voices.map(\n (v: SpeechSpeakerName): Gemini.SpeakerVoiceConfig => ({\n speaker: v.speaker,\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: v.name,\n },\n },\n })\n );\n // Create the multi-speaker voice configuration\n ret = {\n multiSpeakerVoiceConfig: {\n speakerVoiceConfigs,\n },\n };\n }\n\n if (languageCode) {\n ret.languageCode = languageCode;\n }\n\n return ret;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;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AAa,EACX,qBAAqB,EACnB,WAAW,OACZ,EACF,EACF;KAiBD,OAAM,EACJ,yBAAyB,EACvB,sBAfgC,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAEV,KAC7D,OAAqD;EACpD,SAAS,EAAE;EACX,aAAa,EACX,qBAAqB,EACnB,WAAW,EAAE,MACd,EACF;EACF,EACF,EAKE,EACF;AAGH,KAAI,aACF,KAAI,eAAe;AAGrB,QAAO"}
1
+ {"version":3,"file":"params.cjs","names":[],"sources":["../../src/converters/params.ts"],"sourcesContent":["import type {\n ChatGoogleFields,\n GooglePlatformType,\n SimplifiedSpeechConfig,\n SimplifiedSpeechLanguageConfig,\n SpeechSpeakerName,\n SpeechVoice,\n SpeechVoiceLanguage,\n} from \"../chat_models/types.js\";\nimport type { Gemini } from \"../chat_models/api-types.js\";\n\n/**\n * Converts a reasoning \"effort\" level value (e.g., \"minimal\", \"low\", \"medium\", \"high\")\n * to a corresponding reasoning token budget, depending on the target Gemini model.\n *\n * This mapping is essential when configuring Google Gemini API calls with reasoning\n * effort/level fields (as in the Gemini OpenAI-compatible API) or when translating\n * user-friendly configuration to explicit token budgets.\n *\n * - The mapping is refined to reflect the constraints documented by Google:\n * - For \"gemini-2.5-flash\" models, token budgets may be set as low as 0 and up to 24k.\n * - For other Gemini models, minimum is 128, maximum is 32k.\n * - Standard effort levels and their token mappings:\n * - \"none\" / \"minimal\": lowest allowed (minTokens)\n * - \"low\": 1k tokens\n * - \"medium\": 8k tokens\n * - \"high\": maximum allowed for the model (maxTokens)\n * - See reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} modelName\n * The Gemini model name (e.g., \"gemini-2.5-flash\"). Used to determine token range constraints.\n * @param {string | undefined} effort\n * The reasoning effort level: one of \"none\", \"minimal\", \"low\", \"medium\", \"high\"\n * (case-insensitive). If undefined, the function returns undefined.\n * @returns {number | undefined}\n * The corresponding token count for the given effort and model. Returns undefined if\n * the effort is not recognized or not provided.\n *\n * @example\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"medium\") // 8192\n * convertReasoningEffortToReasoningTokens(\"gemini-1.5-pro\", \"high\") // 32768\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"none\") // 0\n * convertReasoningEffortToReasoningTokens(undefined, \"low\") // 1024\n */\nexport function convertReasoningEffortToReasoningTokens(\n modelName?: string,\n effort?: string\n): number | undefined {\n if (effort === undefined) {\n return undefined;\n }\n\n // gemini-2.5-flash and -flash-lite can be disabled. Others can't.\n // https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n const minTokens: number = modelName?.startsWith(\"gemini-2.5-flash\") ? 0 : 128;\n const maxTokens: number = modelName?.startsWith(\"gemini-2.5-flash\")\n ? 24 * 1024\n : 32 * 1024;\n\n const normalizedEffort = effort.toLowerCase();\n switch (normalizedEffort) {\n case \"none\":\n case \"minimal\":\n return minTokens;\n case \"low\":\n // Defined as 1k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 1024;\n case \"medium\":\n // Defined as 8k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 8 * 1024;\n case \"high\":\n // Defined as 24k or 32k (model-dependent) by https://ai.google.dev/gemini-api/docs/openai#thinking\n return maxTokens;\n default:\n return undefined;\n }\n}\n\n/**\n * Converts a reasoning token budget (e.g., 0, 1024, 8192, 32768) to its corresponding\n * reasoning \"effort\" level, as used in the Google Gemini API (\"minimal\", \"low\", \"medium\", \"high\").\n *\n * This is the inverse of {@link convertReasoningEffortToReasoningTokens}. It is useful for\n * translating explicit numeric settings (often surfaced in raw API responses or configs)\n * into user-facing friendly reasoning levels/labels. The mapping relies on Google's\n * documented reasoning levels for different models:\n *\n * - -1 means \"high\" effort (per API documentation: the default, dynamic setting).\n * - 0 means \"minimal\" unless the model is \"gemini-3-pro\", for which it maps to \"low\".\n * - <= 1024 tokens: \"low\"\n * - <= 8192 tokens: \"medium\" (unless model is \"gemini-3-pro\", for which it reverts to \"low\")\n * - > 8192: \"high\"\n *\n * Reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} model\n * The Gemini model name (e.g., \"gemini-3-pro\"). Used for model-specific special cases.\n * @param {number | undefined} reasoningTokens\n * The token budget to convert. If undefined, the function returns undefined.\n * @returns {Lowercase<Gemini.ThinkingLevel> | undefined}\n * The reasoning \"effort\" level (\"minimal\", \"low\", \"medium\", \"high\") or undefined if input is not valid.\n *\n * @example\n * convertReasoningTokensToReasoningEffort(\"gemini-3-pro\", 0) // \"low\"\n * convertReasoningTokensToReasoningEffort(\"gemini-1.5-pro\", 0) // \"minimal\"\n * convertReasoningTokensToReasoningEffort(undefined, 1024) // \"low\"\n * convertReasoningTokensToReasoningEffort(undefined, -1) // \"high\"\n * convertReasoningTokensToReasoningEffort(undefined, 9000) // \"high\"\n */\nexport function convertReasoningTokensToReasoningEffort(\n model?: string,\n reasoningTokens?: number\n): Gemini.ThinkingLevel | undefined {\n if (typeof reasoningTokens === \"undefined\") {\n return undefined;\n } else if (reasoningTokens === -1) {\n // -1 means \"high\"/dynamic (\"default\" per https://ai.google.dev/gemini-api/docs/thinking#thinking-levels)\n return \"high\";\n } else if (reasoningTokens === 0) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"minimal\";\n }\n } else if (reasoningTokens <= 1024) {\n return \"low\";\n } else if (reasoningTokens <= 8192) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"medium\";\n }\n } else {\n return \"high\";\n }\n}\n\n/**\n * Infers and returns the Google platform type from the provided chat model parameters.\n *\n * This function helps determine which Google platform (e.g., Vertex AI on GCP or the public API)\n * should be used for calling Gemini/PaLM endpoints, based on the options provided in\n * {@link BaseChatGoogleParams} (such as model constructors or call options).\n */\nexport function convertParamsToPlatformType(params: {\n platformType?: GooglePlatformType;\n vertexai?: boolean;\n}): GooglePlatformType | undefined {\n if (typeof params === \"undefined\") {\n return undefined;\n }\n if (typeof params.platformType !== \"undefined\") {\n return params.platformType;\n }\n if (params.vertexai === true) {\n return \"gcp\";\n }\n return undefined;\n}\n\n/**\n * Returns the model family from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"gemini\"\n */\nexport function getModelFamily(model: string): string {\n return model.split(\"-\")[0];\n}\n\n/**\n * Returns the model version from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"2.5\"\n */\nexport function getModelVersion(model: string): string {\n return model.split(\"-\")[1];\n}\n\n/**\n * Returns the model level from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"flash\", \"gemini-2.5-flash-lite\" → \"flash-lite\"\n */\nexport function getModelLevel(model: string): string {\n const parts = model.split(\"-\");\n let ret = parts[2];\n if (ret === \"flash\" && parts[3] === \"lite\") {\n ret = \"flash-lite\";\n }\n return ret;\n}\n\n/**\n * Returns the model specialty from a Gemini model name.\n * e.g. \"gemini-2.5-flash-preview-image\" → \"image\",\n * \"gemini-2.5-flash-preview-tts\" → \"tts\",\n * \"gemini-2.5-flash\" → \"\"\n */\nexport function getModelSpecialty(model: string): string {\n if (model.includes(\"image\")) {\n return \"image\";\n } else if (model.includes(\"tts\")) {\n return \"tts\";\n } else {\n return \"\";\n }\n}\n\n/**\n * Builds the `thinkingConfig` object for the Gemini `generationConfig`,\n * translating user-facing reasoning fields into the wire format.\n *\n * @param model The full Gemini model name (e.g. \"gemini-2.5-flash\").\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.ThinkingConfig}, or `{}`\n * when thinking should not be explicitly configured.\n */\nexport function convertFieldsToThinkingConfig(\n model: string,\n fields: ChatGoogleFields\n): Gemini.ThinkingConfig | undefined {\n // Thinking / reasoning\n let includeThoughts = true;\n\n let thinkingBudget =\n fields?.maxReasoningTokens ??\n fields?.thinkingBudget ??\n convertReasoningEffortToReasoningTokens(\n model,\n fields?.reasoningEffort ?? fields?.thinkingLevel\n );\n if (\n thinkingBudget === 0 ||\n (model.includes(\"pro\") && thinkingBudget === 128)\n ) {\n includeThoughts = false;\n }\n if (\n model.startsWith(\"gemini-2.5-pro\") &&\n typeof thinkingBudget !== \"undefined\"\n ) {\n // Can't turn off Gemini 2.5 Pro thinking completely\n if (thinkingBudget >= 0 && thinkingBudget < 128) {\n thinkingBudget = 128;\n }\n }\n\n const thinkingLevelRaw =\n fields?.reasoningEffort ??\n fields?.thinkingLevel ??\n convertReasoningTokensToReasoningEffort(\n model,\n fields?.maxReasoningTokens ?? fields?.thinkingBudget\n );\n let thinkingLevel = thinkingLevelRaw?.toUpperCase();\n if (thinkingLevel === \"MINIMAL\") {\n includeThoughts = false;\n }\n if (model.startsWith(\"gemini-3-pro\")) {\n // Gemini 3 Pro has only low and high.\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n } else if (thinkingLevel === \"MEDIUM\") {\n thinkingLevel = \"HIGH\";\n }\n } else if (model.startsWith(\"gemini-3.1-pro\")) {\n // Gemini 3.1 Pro does not have \"minimal\"\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n }\n }\n\n // If we are using a model that doesn't support thinking at all (gemini 2.5 imaging)\n // or we haven't explicitly tried to set a thinking budget/level, then bail out.\n const modelVersion = getModelVersion(model);\n const modelSpecialty = getModelSpecialty(model);\n const thoughtsNotSupported =\n modelVersion === \"2.5\" && modelSpecialty === \"image\";\n if (\n thoughtsNotSupported ||\n typeof thinkingBudget === \"undefined\" ||\n typeof thinkingLevel === \"undefined\"\n ) {\n return undefined;\n }\n\n // If we have gotten this far, then we want to explicitly set if we include thoughts or not.\n const thinkingConfig: Gemini.ThinkingConfig = {\n includeThoughts,\n };\n\n // Explicitly setting the budget/level is only valid (currently) for text models.\n if (modelSpecialty === \"\") {\n if (model.startsWith(\"gemini-2.5\")) {\n thinkingConfig.thinkingBudget = thinkingBudget;\n } else {\n thinkingConfig.thinkingLevel = thinkingLevel as Gemini.ThinkingLevel;\n }\n }\n\n return thinkingConfig;\n}\n\n/**\n * Builds the `speechConfig` object for the Gemini `generationConfig`,\n * normalising the user-friendly simplified forms into the wire format.\n *\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.SpeechConfig}, or `{}`\n * when no speech config was provided.\n */\nexport function convertFieldsToSpeechConfig(\n fields: ChatGoogleFields\n): Gemini.SpeechConfig | undefined {\n const config: Gemini.SpeechConfig | SimplifiedSpeechConfig | undefined =\n fields.speechConfig;\n if (typeof config === \"undefined\") {\n return undefined;\n }\n\n function isSpeechConfig(\n config: Gemini.SpeechConfig | SimplifiedSpeechConfig\n ): config is Gemini.SpeechConfig {\n return (\n typeof config === \"object\" &&\n (Object.hasOwn(config, \"voiceConfig\") ||\n Object.hasOwn(config, \"multiSpeakerVoiceConfig\"))\n );\n }\n\n function hasLanguage(\n config: SimplifiedSpeechConfig\n ): config is SimplifiedSpeechLanguageConfig {\n return typeof config === \"object\" && Object.hasOwn(config, \"languageCode\");\n }\n\n function hasVoice(\n config: SimplifiedSpeechLanguageConfig\n ): config is SpeechVoiceLanguage {\n return Object.hasOwn(config, \"voice\");\n }\n\n // If this is already a SpeechConfig, just return it\n if (isSpeechConfig(config)) {\n return config;\n }\n\n let languageCode: string | undefined;\n let voice: SpeechVoice;\n if (hasLanguage(config)) {\n languageCode = config.languageCode;\n voice = hasVoice(config) ? config.voice : config.voices;\n } else {\n languageCode = undefined;\n voice = config;\n }\n\n let ret: Gemini.SpeechConfig;\n\n if (typeof voice === \"string\") {\n // They just provided the prebuilt voice configuration name. Use it.\n ret = {\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: voice,\n },\n },\n };\n } else {\n // This is multi-speaker, so we have speaker/name pairs\n // If we have just one (why?), turn it into an array for the moment\n const voices: SpeechSpeakerName[] = Array.isArray(voice) ? voice : [voice];\n // Go through all the speaker/name pairs and turn this into the voice config array\n const speakerVoiceConfigs: Gemini.SpeakerVoiceConfig[] = voices.map(\n (v: SpeechSpeakerName): Gemini.SpeakerVoiceConfig => ({\n speaker: v.speaker,\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: v.name,\n },\n },\n })\n );\n // Create the multi-speaker voice configuration\n ret = {\n multiSpeakerVoiceConfig: {\n speakerVoiceConfigs,\n },\n };\n }\n\n if (languageCode) {\n ret.languageCode = languageCode;\n }\n\n return ret;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,wCACd,WACA,QACoB;AACpB,KAAI,WAAW,OACb;CAKF,MAAM,YAAoB,WAAW,WAAW,mBAAmB,GAAG,IAAI;CAC1E,MAAM,YAAoB,WAAW,WAAW,mBAAmB,GAC/D,KAAK,OACL,KAAK;AAGT,SADyB,OAAO,aAAa,EAC7C;EACE,KAAK;EACL,KAAK,UACH,QAAO;EACT,KAAK,MAEH,QAAO;EACT,KAAK,SAEH,QAAO,IAAI;EACb,KAAK,OAEH,QAAO;EACT,QACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCN,SAAgB,wCACd,OACA,iBACkC;AAClC,KAAI,OAAO,oBAAoB,YAC7B;UACS,oBAAoB,GAE7B,QAAO;UACE,oBAAoB,EAC7B,KAAI,OAAO,WAAW,eAAe,CACnC,QAAO;KAEP,QAAO;UAEA,mBAAmB,KAC5B,QAAO;UACE,mBAAmB,KAC5B,KAAI,OAAO,WAAW,eAAe,CACnC,QAAO;KAEP,QAAO;KAGT,QAAO;;;;;;;;;AAWX,SAAgB,4BAA4B,QAGT;AACjC,KAAI,OAAO,WAAW,YACpB;AAEF,KAAI,OAAO,OAAO,iBAAiB,YACjC,QAAO,OAAO;AAEhB,KAAI,OAAO,aAAa,KACtB,QAAO;;;;;;AAiBX,SAAgB,gBAAgB,OAAuB;AACrD,QAAO,MAAM,MAAM,IAAI,CAAC;;;;;;;;AAsB1B,SAAgB,kBAAkB,OAAuB;AACvD,KAAI,MAAM,SAAS,QAAQ,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OAAO,OAAO,QAAQ,eAAe;;CAG5E,SAAS,SACP,QAC+B;AAC/B,SAAO,OAAO,OAAO,QAAQ,QAAQ;;AAIvC,KAAI,eAAe,OAAO,CACxB,QAAO;CAGT,IAAI;CACJ,IAAI;AACJ,KAAI,YAAY,OAAO,EAAE;AACvB,iBAAe,OAAO;AACtB,UAAQ,SAAS,OAAO,GAAG,OAAO,QAAQ,OAAO;QAC5C;AACL,iBAAe;AACf,UAAQ;;CAGV,IAAI;AAEJ,KAAI,OAAO,UAAU,SAEnB,OAAM,EACJ,aAAa,EACX,qBAAqB,EACnB,WAAW,OACZ,EACF,EACF;KAiBD,OAAM,EACJ,yBAAyB,EACvB,sBAfgC,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAEV,KAC7D,OAAqD;EACpD,SAAS,EAAE;EACX,aAAa,EACX,qBAAqB,EACnB,WAAW,EAAE,MACd,EACF;EACF,EACF,EAKE,EACF;AAGH,KAAI,aACF,KAAI,eAAe;AAGrB,QAAO"}
@@ -141,6 +141,8 @@ function convertFieldsToThinkingConfig(model, fields) {
141
141
  if (model.startsWith("gemini-3-pro")) {
142
142
  if (thinkingLevel === "MINIMAL") thinkingLevel = "LOW";
143
143
  else if (thinkingLevel === "MEDIUM") thinkingLevel = "HIGH";
144
+ } else if (model.startsWith("gemini-3.1-pro")) {
145
+ if (thinkingLevel === "MINIMAL") thinkingLevel = "LOW";
144
146
  }
145
147
  const modelVersion = getModelVersion(model);
146
148
  const modelSpecialty = getModelSpecialty(model);
@@ -1 +1 @@
1
- {"version":3,"file":"params.js","names":[],"sources":["../../src/converters/params.ts"],"sourcesContent":["import type {\n ChatGoogleFields,\n GooglePlatformType,\n SimplifiedSpeechConfig,\n SimplifiedSpeechLanguageConfig,\n SpeechSpeakerName,\n SpeechVoice,\n SpeechVoiceLanguage,\n} from \"../chat_models/types.js\";\nimport type { Gemini } from \"../chat_models/api-types.js\";\n\n/**\n * Converts a reasoning \"effort\" level value (e.g., \"minimal\", \"low\", \"medium\", \"high\")\n * to a corresponding reasoning token budget, depending on the target Gemini model.\n *\n * This mapping is essential when configuring Google Gemini API calls with reasoning\n * effort/level fields (as in the Gemini OpenAI-compatible API) or when translating\n * user-friendly configuration to explicit token budgets.\n *\n * - The mapping is refined to reflect the constraints documented by Google:\n * - For \"gemini-2.5-flash\" models, token budgets may be set as low as 0 and up to 24k.\n * - For other Gemini models, minimum is 128, maximum is 32k.\n * - Standard effort levels and their token mappings:\n * - \"none\" / \"minimal\": lowest allowed (minTokens)\n * - \"low\": 1k tokens\n * - \"medium\": 8k tokens\n * - \"high\": maximum allowed for the model (maxTokens)\n * - See reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} modelName\n * The Gemini model name (e.g., \"gemini-2.5-flash\"). Used to determine token range constraints.\n * @param {string | undefined} effort\n * The reasoning effort level: one of \"none\", \"minimal\", \"low\", \"medium\", \"high\"\n * (case-insensitive). If undefined, the function returns undefined.\n * @returns {number | undefined}\n * The corresponding token count for the given effort and model. Returns undefined if\n * the effort is not recognized or not provided.\n *\n * @example\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"medium\") // 8192\n * convertReasoningEffortToReasoningTokens(\"gemini-1.5-pro\", \"high\") // 32768\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"none\") // 0\n * convertReasoningEffortToReasoningTokens(undefined, \"low\") // 1024\n */\nexport function convertReasoningEffortToReasoningTokens(\n modelName?: string,\n effort?: string\n): number | undefined {\n if (effort === undefined) {\n return undefined;\n }\n\n // gemini-2.5-flash and -flash-lite can be disabled. Others can't.\n // https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n const minTokens: number = modelName?.startsWith(\"gemini-2.5-flash\") ? 0 : 128;\n const maxTokens: number = modelName?.startsWith(\"gemini-2.5-flash\")\n ? 24 * 1024\n : 32 * 1024;\n\n const normalizedEffort = effort.toLowerCase();\n switch (normalizedEffort) {\n case \"none\":\n case \"minimal\":\n return minTokens;\n case \"low\":\n // Defined as 1k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 1024;\n case \"medium\":\n // Defined as 8k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 8 * 1024;\n case \"high\":\n // Defined as 24k or 32k (model-dependent) by https://ai.google.dev/gemini-api/docs/openai#thinking\n return maxTokens;\n default:\n return undefined;\n }\n}\n\n/**\n * Converts a reasoning token budget (e.g., 0, 1024, 8192, 32768) to its corresponding\n * reasoning \"effort\" level, as used in the Google Gemini API (\"minimal\", \"low\", \"medium\", \"high\").\n *\n * This is the inverse of {@link convertReasoningEffortToReasoningTokens}. It is useful for\n * translating explicit numeric settings (often surfaced in raw API responses or configs)\n * into user-facing friendly reasoning levels/labels. The mapping relies on Google's\n * documented reasoning levels for different models:\n *\n * - -1 means \"high\" effort (per API documentation: the default, dynamic setting).\n * - 0 means \"minimal\" unless the model is \"gemini-3-pro\", for which it maps to \"low\".\n * - <= 1024 tokens: \"low\"\n * - <= 8192 tokens: \"medium\" (unless model is \"gemini-3-pro\", for which it reverts to \"low\")\n * - > 8192: \"high\"\n *\n * Reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} model\n * The Gemini model name (e.g., \"gemini-3-pro\"). Used for model-specific special cases.\n * @param {number | undefined} reasoningTokens\n * The token budget to convert. If undefined, the function returns undefined.\n * @returns {Lowercase<Gemini.ThinkingLevel> | undefined}\n * The reasoning \"effort\" level (\"minimal\", \"low\", \"medium\", \"high\") or undefined if input is not valid.\n *\n * @example\n * convertReasoningTokensToReasoningEffort(\"gemini-3-pro\", 0) // \"low\"\n * convertReasoningTokensToReasoningEffort(\"gemini-1.5-pro\", 0) // \"minimal\"\n * convertReasoningTokensToReasoningEffort(undefined, 1024) // \"low\"\n * convertReasoningTokensToReasoningEffort(undefined, -1) // \"high\"\n * convertReasoningTokensToReasoningEffort(undefined, 9000) // \"high\"\n */\nexport function convertReasoningTokensToReasoningEffort(\n model?: string,\n reasoningTokens?: number\n): Gemini.ThinkingLevel | undefined {\n if (typeof reasoningTokens === \"undefined\") {\n return undefined;\n } else if (reasoningTokens === -1) {\n // -1 means \"high\"/dynamic (\"default\" per https://ai.google.dev/gemini-api/docs/thinking#thinking-levels)\n return \"high\";\n } else if (reasoningTokens === 0) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"minimal\";\n }\n } else if (reasoningTokens <= 1024) {\n return \"low\";\n } else if (reasoningTokens <= 8192) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"medium\";\n }\n } else {\n return \"high\";\n }\n}\n\n/**\n * Infers and returns the Google platform type from the provided chat model parameters.\n *\n * This function helps determine which Google platform (e.g., Vertex AI on GCP or the public API)\n * should be used for calling Gemini/PaLM endpoints, based on the options provided in\n * {@link BaseChatGoogleParams} (such as model constructors or call options).\n */\nexport function convertParamsToPlatformType(params: {\n platformType?: GooglePlatformType;\n vertexai?: boolean;\n}): GooglePlatformType | undefined {\n if (typeof params === \"undefined\") {\n return undefined;\n }\n if (typeof params.platformType !== \"undefined\") {\n return params.platformType;\n }\n if (params.vertexai === true) {\n return \"gcp\";\n }\n return undefined;\n}\n\n/**\n * Returns the model family from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"gemini\"\n */\nexport function getModelFamily(model: string): string {\n return model.split(\"-\")[0];\n}\n\n/**\n * Returns the model version from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"2.5\"\n */\nexport function getModelVersion(model: string): string {\n return model.split(\"-\")[1];\n}\n\n/**\n * Returns the model level from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"flash\", \"gemini-2.5-flash-lite\" → \"flash-lite\"\n */\nexport function getModelLevel(model: string): string {\n const parts = model.split(\"-\");\n let ret = parts[2];\n if (ret === \"flash\" && parts[3] === \"lite\") {\n ret = \"flash-lite\";\n }\n return ret;\n}\n\n/**\n * Returns the model specialty from a Gemini model name.\n * e.g. \"gemini-2.5-flash-preview-image\" → \"image\",\n * \"gemini-2.5-flash-preview-tts\" → \"tts\",\n * \"gemini-2.5-flash\" → \"\"\n */\nexport function getModelSpecialty(model: string): string {\n if (model.includes(\"image\")) {\n return \"image\";\n } else if (model.includes(\"tts\")) {\n return \"tts\";\n } else {\n return \"\";\n }\n}\n\n/**\n * Builds the `thinkingConfig` object for the Gemini `generationConfig`,\n * translating user-facing reasoning fields into the wire format.\n *\n * @param model The full Gemini model name (e.g. \"gemini-2.5-flash\").\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.ThinkingConfig}, or `{}`\n * when thinking should not be explicitly configured.\n */\nexport function convertFieldsToThinkingConfig(\n model: string,\n fields: ChatGoogleFields\n): Gemini.ThinkingConfig | undefined {\n // Thinking / reasoning\n let includeThoughts = true;\n\n let thinkingBudget =\n fields?.maxReasoningTokens ??\n fields?.thinkingBudget ??\n convertReasoningEffortToReasoningTokens(\n model,\n fields?.reasoningEffort ?? fields?.thinkingLevel\n );\n if (\n thinkingBudget === 0 ||\n (model.includes(\"pro\") && thinkingBudget === 128)\n ) {\n includeThoughts = false;\n }\n if (\n model.startsWith(\"gemini-2.5-pro\") &&\n typeof thinkingBudget !== \"undefined\"\n ) {\n // Can't turn off Gemini 2.5 Pro thinking completely\n if (thinkingBudget >= 0 && thinkingBudget < 128) {\n thinkingBudget = 128;\n }\n }\n\n const thinkingLevelRaw =\n fields?.reasoningEffort ??\n fields?.thinkingLevel ??\n convertReasoningTokensToReasoningEffort(\n model,\n fields?.maxReasoningTokens ?? fields?.thinkingBudget\n );\n let thinkingLevel = thinkingLevelRaw?.toUpperCase();\n if (thinkingLevel === \"MINIMAL\") {\n includeThoughts = false;\n }\n if (model.startsWith(\"gemini-3-pro\")) {\n // Gemini 3 Pro has only low and high.\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n } else if (thinkingLevel === \"MEDIUM\") {\n thinkingLevel = \"HIGH\";\n }\n }\n\n // If we are using a model that doesn't support thinking at all (gemini 2.5 imaging)\n // or we haven't explicitly tried to set a thinking budget/level, then bail out.\n const modelVersion = getModelVersion(model);\n const modelSpecialty = getModelSpecialty(model);\n const thoughtsNotSupported =\n modelVersion === \"2.5\" && modelSpecialty === \"image\";\n if (\n thoughtsNotSupported ||\n typeof thinkingBudget === \"undefined\" ||\n typeof thinkingLevel === \"undefined\"\n ) {\n return undefined;\n }\n\n // If we have gotten this far, then we want to explicitly set if we include thoughts or not.\n const thinkingConfig: Gemini.ThinkingConfig = {\n includeThoughts,\n };\n\n // Explicitly setting the budget/level is only valid (currently) for text models.\n if (modelSpecialty === \"\") {\n if (model.startsWith(\"gemini-2.5\")) {\n thinkingConfig.thinkingBudget = thinkingBudget;\n } else {\n thinkingConfig.thinkingLevel = thinkingLevel as Gemini.ThinkingLevel;\n }\n }\n\n return thinkingConfig;\n}\n\n/**\n * Builds the `speechConfig` object for the Gemini `generationConfig`,\n * normalising the user-friendly simplified forms into the wire format.\n *\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.SpeechConfig}, or `{}`\n * when no speech config was provided.\n */\nexport function convertFieldsToSpeechConfig(\n fields: ChatGoogleFields\n): Gemini.SpeechConfig | undefined {\n const config: Gemini.SpeechConfig | SimplifiedSpeechConfig | undefined =\n fields.speechConfig;\n if (typeof config === \"undefined\") {\n return undefined;\n }\n\n function isSpeechConfig(\n config: Gemini.SpeechConfig | SimplifiedSpeechConfig\n ): config is Gemini.SpeechConfig {\n return (\n typeof config === \"object\" &&\n (Object.hasOwn(config, \"voiceConfig\") ||\n Object.hasOwn(config, \"multiSpeakerVoiceConfig\"))\n );\n }\n\n function hasLanguage(\n config: SimplifiedSpeechConfig\n ): config is SimplifiedSpeechLanguageConfig {\n return typeof config === \"object\" && Object.hasOwn(config, \"languageCode\");\n }\n\n function hasVoice(\n config: SimplifiedSpeechLanguageConfig\n ): config is SpeechVoiceLanguage {\n return Object.hasOwn(config, \"voice\");\n }\n\n // If this is already a SpeechConfig, just return it\n if (isSpeechConfig(config)) {\n return config;\n }\n\n let languageCode: string | undefined;\n let voice: SpeechVoice;\n if (hasLanguage(config)) {\n languageCode = config.languageCode;\n voice = hasVoice(config) ? config.voice : config.voices;\n } else {\n languageCode = undefined;\n voice = config;\n }\n\n let ret: Gemini.SpeechConfig;\n\n if (typeof voice === \"string\") {\n // They just provided the prebuilt voice configuration name. Use it.\n ret = {\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: voice,\n },\n },\n };\n } else {\n // This is multi-speaker, so we have speaker/name pairs\n // If we have just one (why?), turn it into an array for the moment\n const voices: SpeechSpeakerName[] = Array.isArray(voice) ? voice : [voice];\n // Go through all the speaker/name pairs and turn this into the voice config array\n const speakerVoiceConfigs: Gemini.SpeakerVoiceConfig[] = voices.map(\n (v: SpeechSpeakerName): Gemini.SpeakerVoiceConfig => ({\n speaker: v.speaker,\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: v.name,\n },\n },\n })\n );\n // Create the multi-speaker voice configuration\n ret = {\n multiSpeakerVoiceConfig: {\n speakerVoiceConfigs,\n },\n };\n }\n\n if (languageCode) {\n ret.languageCode = languageCode;\n }\n\n return ret;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,wCACd,WACA,QACoB;AACpB,KAAI,WAAW,OACb;CAKF,MAAM,YAAoB,WAAW,WAAW,mBAAmB,GAAG,IAAI;CAC1E,MAAM,YAAoB,WAAW,WAAW,mBAAmB,GAC/D,KAAK,OACL,KAAK;AAGT,SADyB,OAAO,aAAa,EAC7C;EACE,KAAK;EACL,KAAK,UACH,QAAO;EACT,KAAK,MAEH,QAAO;EACT,KAAK,SAEH,QAAO,IAAI;EACb,KAAK,OAEH,QAAO;EACT,QACE;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAqCN,SAAgB,wC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AAM;AAG/C,KADE,iBAAiB,SAAS,mBAAmB,WAG7C,OAAO,mBAAmB,eAC1B,OAAO,kBAAkB,YAEzB;CAIF,MAAM,iBAAwC,EAC5C,iBACD;AAGD,KAAI,mBAAmB,GACrB,KAAI,MAAM,WAAW,aAAa,CAChC,gBAAe,iBAAiB;KAEhC,gBAAe,gBAAgB;AAInC,QAAO;;;;;;;;;;AAWT,SAAgB,4BACd,QACiC;CACjC,MAAM,SACJ,OAAO;AACT,KAAI,OAAO,WAAW,YACpB;CAGF,SAAS,eACP,QAC+B;AAC/B,SACE,OAAO,WAAW,aACjB,OAAO,OAAO,QAAQ,cAAc,IACnC,OAAO,OAAO,QAAQ,0BAA0B;;CAItD,SAAS,YACP,QAC0C;AAC1C,SAAO,OAAO,WAAW,YAAY,OAAO,OAAO,QAAQ,eAAe;;CAG5E,SAAS,SACP,QAC+B;AAC/B,SAAO,OAAO,OAAO,QAAQ,QAAQ;;AAIvC,KAAI,eAAe,OAAO,CACxB,QAAO;CAGT,IAAI;CACJ,IAAI;AACJ,KAAI,YAAY,OAAO,EAAE;AACvB,iBAAe,OAAO;AACtB,UAAQ,SAAS,OAAO,GAAG,OAAO,QAAQ,OAAO;QAC5C;AACL,iBAAe;AACf,UAAQ;;CAGV,IAAI;AAEJ,KAAI,OAAO,UAAU,SAEnB,OAAM,EACJ,aAAa,EACX,qBAAqB,EACnB,WAAW,OACZ,EACF,EACF;KAiBD,OAAM,EACJ,yBAAyB,EACvB,sBAfgC,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAEV,KAC7D,OAAqD;EACpD,SAAS,EAAE;EACX,aAAa,EACX,qBAAqB,EACnB,WAAW,EAAE,MACd,EACF;EACF,EACF,EAKE,EACF;AAGH,KAAI,aACF,KAAI,eAAe;AAGrB,QAAO"}
1
+ {"version":3,"file":"params.js","names":[],"sources":["../../src/converters/params.ts"],"sourcesContent":["import type {\n ChatGoogleFields,\n GooglePlatformType,\n SimplifiedSpeechConfig,\n SimplifiedSpeechLanguageConfig,\n SpeechSpeakerName,\n SpeechVoice,\n SpeechVoiceLanguage,\n} from \"../chat_models/types.js\";\nimport type { Gemini } from \"../chat_models/api-types.js\";\n\n/**\n * Converts a reasoning \"effort\" level value (e.g., \"minimal\", \"low\", \"medium\", \"high\")\n * to a corresponding reasoning token budget, depending on the target Gemini model.\n *\n * This mapping is essential when configuring Google Gemini API calls with reasoning\n * effort/level fields (as in the Gemini OpenAI-compatible API) or when translating\n * user-friendly configuration to explicit token budgets.\n *\n * - The mapping is refined to reflect the constraints documented by Google:\n * - For \"gemini-2.5-flash\" models, token budgets may be set as low as 0 and up to 24k.\n * - For other Gemini models, minimum is 128, maximum is 32k.\n * - Standard effort levels and their token mappings:\n * - \"none\" / \"minimal\": lowest allowed (minTokens)\n * - \"low\": 1k tokens\n * - \"medium\": 8k tokens\n * - \"high\": maximum allowed for the model (maxTokens)\n * - See reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} modelName\n * The Gemini model name (e.g., \"gemini-2.5-flash\"). Used to determine token range constraints.\n * @param {string | undefined} effort\n * The reasoning effort level: one of \"none\", \"minimal\", \"low\", \"medium\", \"high\"\n * (case-insensitive). If undefined, the function returns undefined.\n * @returns {number | undefined}\n * The corresponding token count for the given effort and model. Returns undefined if\n * the effort is not recognized or not provided.\n *\n * @example\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"medium\") // 8192\n * convertReasoningEffortToReasoningTokens(\"gemini-1.5-pro\", \"high\") // 32768\n * convertReasoningEffortToReasoningTokens(\"gemini-2.5-flash\", \"none\") // 0\n * convertReasoningEffortToReasoningTokens(undefined, \"low\") // 1024\n */\nexport function convertReasoningEffortToReasoningTokens(\n modelName?: string,\n effort?: string\n): number | undefined {\n if (effort === undefined) {\n return undefined;\n }\n\n // gemini-2.5-flash and -flash-lite can be disabled. Others can't.\n // https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n const minTokens: number = modelName?.startsWith(\"gemini-2.5-flash\") ? 0 : 128;\n const maxTokens: number = modelName?.startsWith(\"gemini-2.5-flash\")\n ? 24 * 1024\n : 32 * 1024;\n\n const normalizedEffort = effort.toLowerCase();\n switch (normalizedEffort) {\n case \"none\":\n case \"minimal\":\n return minTokens;\n case \"low\":\n // Defined as 1k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 1024;\n case \"medium\":\n // Defined as 8k by https://ai.google.dev/gemini-api/docs/openai#thinking\n return 8 * 1024;\n case \"high\":\n // Defined as 24k or 32k (model-dependent) by https://ai.google.dev/gemini-api/docs/openai#thinking\n return maxTokens;\n default:\n return undefined;\n }\n}\n\n/**\n * Converts a reasoning token budget (e.g., 0, 1024, 8192, 32768) to its corresponding\n * reasoning \"effort\" level, as used in the Google Gemini API (\"minimal\", \"low\", \"medium\", \"high\").\n *\n * This is the inverse of {@link convertReasoningEffortToReasoningTokens}. It is useful for\n * translating explicit numeric settings (often surfaced in raw API responses or configs)\n * into user-facing friendly reasoning levels/labels. The mapping relies on Google's\n * documented reasoning levels for different models:\n *\n * - -1 means \"high\" effort (per API documentation: the default, dynamic setting).\n * - 0 means \"minimal\" unless the model is \"gemini-3-pro\", for which it maps to \"low\".\n * - <= 1024 tokens: \"low\"\n * - <= 8192 tokens: \"medium\" (unless model is \"gemini-3-pro\", for which it reverts to \"low\")\n * - > 8192: \"high\"\n *\n * Reference:\n * https://ai.google.dev/gemini-api/docs/thinking#thinking-levels\n * https://ai.google.dev/gemini-api/docs/openai#thinking\n *\n * @param {string | undefined} model\n * The Gemini model name (e.g., \"gemini-3-pro\"). Used for model-specific special cases.\n * @param {number | undefined} reasoningTokens\n * The token budget to convert. If undefined, the function returns undefined.\n * @returns {Lowercase<Gemini.ThinkingLevel> | undefined}\n * The reasoning \"effort\" level (\"minimal\", \"low\", \"medium\", \"high\") or undefined if input is not valid.\n *\n * @example\n * convertReasoningTokensToReasoningEffort(\"gemini-3-pro\", 0) // \"low\"\n * convertReasoningTokensToReasoningEffort(\"gemini-1.5-pro\", 0) // \"minimal\"\n * convertReasoningTokensToReasoningEffort(undefined, 1024) // \"low\"\n * convertReasoningTokensToReasoningEffort(undefined, -1) // \"high\"\n * convertReasoningTokensToReasoningEffort(undefined, 9000) // \"high\"\n */\nexport function convertReasoningTokensToReasoningEffort(\n model?: string,\n reasoningTokens?: number\n): Gemini.ThinkingLevel | undefined {\n if (typeof reasoningTokens === \"undefined\") {\n return undefined;\n } else if (reasoningTokens === -1) {\n // -1 means \"high\"/dynamic (\"default\" per https://ai.google.dev/gemini-api/docs/thinking#thinking-levels)\n return \"high\";\n } else if (reasoningTokens === 0) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"minimal\";\n }\n } else if (reasoningTokens <= 1024) {\n return \"low\";\n } else if (reasoningTokens <= 8192) {\n if (model?.startsWith(\"gemini-3-pro\")) {\n return \"low\";\n } else {\n return \"medium\";\n }\n } else {\n return \"high\";\n }\n}\n\n/**\n * Infers and returns the Google platform type from the provided chat model parameters.\n *\n * This function helps determine which Google platform (e.g., Vertex AI on GCP or the public API)\n * should be used for calling Gemini/PaLM endpoints, based on the options provided in\n * {@link BaseChatGoogleParams} (such as model constructors or call options).\n */\nexport function convertParamsToPlatformType(params: {\n platformType?: GooglePlatformType;\n vertexai?: boolean;\n}): GooglePlatformType | undefined {\n if (typeof params === \"undefined\") {\n return undefined;\n }\n if (typeof params.platformType !== \"undefined\") {\n return params.platformType;\n }\n if (params.vertexai === true) {\n return \"gcp\";\n }\n return undefined;\n}\n\n/**\n * Returns the model family from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"gemini\"\n */\nexport function getModelFamily(model: string): string {\n return model.split(\"-\")[0];\n}\n\n/**\n * Returns the model version from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"2.5\"\n */\nexport function getModelVersion(model: string): string {\n return model.split(\"-\")[1];\n}\n\n/**\n * Returns the model level from a Gemini model name.\n * e.g. \"gemini-2.5-flash\" → \"flash\", \"gemini-2.5-flash-lite\" → \"flash-lite\"\n */\nexport function getModelLevel(model: string): string {\n const parts = model.split(\"-\");\n let ret = parts[2];\n if (ret === \"flash\" && parts[3] === \"lite\") {\n ret = \"flash-lite\";\n }\n return ret;\n}\n\n/**\n * Returns the model specialty from a Gemini model name.\n * e.g. \"gemini-2.5-flash-preview-image\" → \"image\",\n * \"gemini-2.5-flash-preview-tts\" → \"tts\",\n * \"gemini-2.5-flash\" → \"\"\n */\nexport function getModelSpecialty(model: string): string {\n if (model.includes(\"image\")) {\n return \"image\";\n } else if (model.includes(\"tts\")) {\n return \"tts\";\n } else {\n return \"\";\n }\n}\n\n/**\n * Builds the `thinkingConfig` object for the Gemini `generationConfig`,\n * translating user-facing reasoning fields into the wire format.\n *\n * @param model The full Gemini model name (e.g. \"gemini-2.5-flash\").\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.ThinkingConfig}, or `{}`\n * when thinking should not be explicitly configured.\n */\nexport function convertFieldsToThinkingConfig(\n model: string,\n fields: ChatGoogleFields\n): Gemini.ThinkingConfig | undefined {\n // Thinking / reasoning\n let includeThoughts = true;\n\n let thinkingBudget =\n fields?.maxReasoningTokens ??\n fields?.thinkingBudget ??\n convertReasoningEffortToReasoningTokens(\n model,\n fields?.reasoningEffort ?? fields?.thinkingLevel\n );\n if (\n thinkingBudget === 0 ||\n (model.includes(\"pro\") && thinkingBudget === 128)\n ) {\n includeThoughts = false;\n }\n if (\n model.startsWith(\"gemini-2.5-pro\") &&\n typeof thinkingBudget !== \"undefined\"\n ) {\n // Can't turn off Gemini 2.5 Pro thinking completely\n if (thinkingBudget >= 0 && thinkingBudget < 128) {\n thinkingBudget = 128;\n }\n }\n\n const thinkingLevelRaw =\n fields?.reasoningEffort ??\n fields?.thinkingLevel ??\n convertReasoningTokensToReasoningEffort(\n model,\n fields?.maxReasoningTokens ?? fields?.thinkingBudget\n );\n let thinkingLevel = thinkingLevelRaw?.toUpperCase();\n if (thinkingLevel === \"MINIMAL\") {\n includeThoughts = false;\n }\n if (model.startsWith(\"gemini-3-pro\")) {\n // Gemini 3 Pro has only low and high.\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n } else if (thinkingLevel === \"MEDIUM\") {\n thinkingLevel = \"HIGH\";\n }\n } else if (model.startsWith(\"gemini-3.1-pro\")) {\n // Gemini 3.1 Pro does not have \"minimal\"\n if (thinkingLevel === \"MINIMAL\") {\n thinkingLevel = \"LOW\";\n }\n }\n\n // If we are using a model that doesn't support thinking at all (gemini 2.5 imaging)\n // or we haven't explicitly tried to set a thinking budget/level, then bail out.\n const modelVersion = getModelVersion(model);\n const modelSpecialty = getModelSpecialty(model);\n const thoughtsNotSupported =\n modelVersion === \"2.5\" && modelSpecialty === \"image\";\n if (\n thoughtsNotSupported ||\n typeof thinkingBudget === \"undefined\" ||\n typeof thinkingLevel === \"undefined\"\n ) {\n return undefined;\n }\n\n // If we have gotten this far, then we want to explicitly set if we include thoughts or not.\n const thinkingConfig: Gemini.ThinkingConfig = {\n includeThoughts,\n };\n\n // Explicitly setting the budget/level is only valid (currently) for text models.\n if (modelSpecialty === \"\") {\n if (model.startsWith(\"gemini-2.5\")) {\n thinkingConfig.thinkingBudget = thinkingBudget;\n } else {\n thinkingConfig.thinkingLevel = thinkingLevel as Gemini.ThinkingLevel;\n }\n }\n\n return thinkingConfig;\n}\n\n/**\n * Builds the `speechConfig` object for the Gemini `generationConfig`,\n * normalising the user-friendly simplified forms into the wire format.\n *\n * @param fields The combined user/call-option fields.\n * @returns A populated {@link Gemini.SpeechConfig}, or `{}`\n * when no speech config was provided.\n */\nexport function convertFieldsToSpeechConfig(\n fields: ChatGoogleFields\n): Gemini.SpeechConfig | undefined {\n const config: Gemini.SpeechConfig | SimplifiedSpeechConfig | undefined =\n fields.speechConfig;\n if (typeof config === \"undefined\") {\n return undefined;\n }\n\n function isSpeechConfig(\n config: Gemini.SpeechConfig | SimplifiedSpeechConfig\n ): config is Gemini.SpeechConfig {\n return (\n typeof config === \"object\" &&\n (Object.hasOwn(config, \"voiceConfig\") ||\n Object.hasOwn(config, \"multiSpeakerVoiceConfig\"))\n );\n }\n\n function hasLanguage(\n config: SimplifiedSpeechConfig\n ): config is SimplifiedSpeechLanguageConfig {\n return typeof config === \"object\" && Object.hasOwn(config, \"languageCode\");\n }\n\n function hasVoice(\n config: SimplifiedSpeechLanguageConfig\n ): config is SpeechVoiceLanguage {\n return Object.hasOwn(config, \"voice\");\n }\n\n // If this is already a SpeechConfig, just return it\n if (isSpeechConfig(config)) {\n return config;\n }\n\n let languageCode: string | undefined;\n let voice: SpeechVoice;\n if (hasLanguage(config)) {\n languageCode = config.languageCode;\n voice = hasVoice(config) ? config.voice : config.voices;\n } else {\n languageCode = undefined;\n voice = config;\n }\n\n let ret: Gemini.SpeechConfig;\n\n if (typeof voice === \"string\") {\n // They just provided the prebuilt voice configuration name. Use it.\n ret = {\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: voice,\n },\n },\n };\n } else {\n // This is multi-speaker, so we have speaker/name pairs\n // If we have just one (why?), turn it into an array for the moment\n const voices: SpeechSpeakerName[] = Array.isArray(voice) ? voice : [voice];\n // Go through all the speaker/name pairs and turn this into the voice config array\n const speakerVoiceConfigs: Gemini.SpeakerVoiceConfig[] = voices.map(\n (v: SpeechSpeakerName): Gemini.SpeakerVoiceConfig => ({\n speaker: v.speaker,\n voiceConfig: {\n prebuiltVoiceConfig: {\n voiceName: v.name,\n },\n },\n })\n );\n // Create the multi-speaker voice configuration\n ret = {\n multiSpeakerVoiceConfig: {\n speakerVoiceConfigs,\n },\n };\n }\n\n if (languageCode) {\n ret.languageCode = languageCode;\n }\n\n return ret;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA8CA,SAAgB,wCACd,WACA,QACoB;AACpB,KAAI,WAAW,OACb;CAKF,MAAM,YAAoB,WAAW,WAAW,mBAAmB,GAAG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,EACJ,yBAAyB,EACvB,sBAfgC,MAAM,QAAQ,MAAM,GAAG,QAAQ,CAAC,MAAM,EAEV,KAC7D,OAAqD;EACpD,SAAS,EAAE;EACX,aAAa,EACX,qBAAqB,EACnB,WAAW,EAAE,MACd,EACF;EACF,EACF,EAKE,EACF;AAGH,KAAI,aACF,KAAI,eAAe;AAGrB,QAAO"}
@@ -0,0 +1,57 @@
1
+
2
+ //#region src/utils/metadata.ts
3
+ /**
4
+ * Computes the delta between two token detail objects by subtracting
5
+ * each field in `previous` from the corresponding field in `current`.
6
+ *
7
+ * This is used to convert cumulative token detail counts (as reported
8
+ * by the Gemini streaming API) into per-chunk deltas that can be
9
+ * correctly summed by {@link mergeUsageMetadata} during chunk
10
+ * concatenation.
11
+ *
12
+ * @param current - The cumulative token details from the current chunk.
13
+ * @param previous - The cumulative token details from the previous chunk.
14
+ * @returns The per-chunk delta, or `undefined` if `current` is undefined.
15
+ */
16
+ function subtractTokenDetails(current, previous) {
17
+ if (!current) return void 0;
18
+ if (!previous) return current;
19
+ const result = {};
20
+ for (const key of Object.keys(current)) {
21
+ const cur = current[key];
22
+ const prev = previous[key];
23
+ if (cur !== void 0) result[key] = cur - (prev ?? 0);
24
+ }
25
+ return result;
26
+ }
27
+ /**
28
+ * Computes the delta between two {@link UsageMetadata} objects by
29
+ * subtracting each field in `previous` from the corresponding field in
30
+ * `current`.
31
+ *
32
+ * The Gemini streaming API reports **cumulative** token counts on every
33
+ * SSE chunk, but `@langchain/core`'s `mergeUsageMetadata` combines
34
+ * values via addition. Emitting raw cumulative values on each
35
+ * `AIMessageChunk` would therefore inflate the totals when chunks are
36
+ * concatenated. This function converts cumulative values into per-chunk
37
+ * deltas so that additive merging produces the correct final totals.
38
+ *
39
+ * @param current - The cumulative usage metadata from the current chunk.
40
+ * @param previous - The cumulative usage metadata from the previous
41
+ * chunk, or `undefined` for the first chunk in the stream.
42
+ * @returns The per-chunk delta usage metadata.
43
+ */
44
+ function subtractUsageMetadata(current, previous) {
45
+ if (!previous) return current;
46
+ return {
47
+ input_tokens: current.input_tokens - previous.input_tokens,
48
+ output_tokens: current.output_tokens - previous.output_tokens,
49
+ total_tokens: current.total_tokens - previous.total_tokens,
50
+ input_token_details: subtractTokenDetails(current.input_token_details, previous.input_token_details),
51
+ output_token_details: subtractTokenDetails(current.output_token_details, previous.output_token_details)
52
+ };
53
+ }
54
+
55
+ //#endregion
56
+ exports.subtractUsageMetadata = subtractUsageMetadata;
57
+ //# sourceMappingURL=metadata.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata.cjs","names":[],"sources":["../../src/utils/metadata.ts"],"sourcesContent":["import type {\n UsageMetadata,\n InputTokenDetails,\n OutputTokenDetails,\n} from \"@langchain/core/messages\";\n\n/**\n * Computes the delta between two token detail objects by subtracting\n * each field in `previous` from the corresponding field in `current`.\n *\n * This is used to convert cumulative token detail counts (as reported\n * by the Gemini streaming API) into per-chunk deltas that can be\n * correctly summed by {@link mergeUsageMetadata} during chunk\n * concatenation.\n *\n * @param current - The cumulative token details from the current chunk.\n * @param previous - The cumulative token details from the previous chunk.\n * @returns The per-chunk delta, or `undefined` if `current` is undefined.\n */\nfunction subtractTokenDetails<T extends InputTokenDetails | OutputTokenDetails>(\n current?: T,\n previous?: T\n): T | undefined {\n if (!current) return undefined;\n if (!previous) return current;\n const result = {} as Record<string, number>;\n for (const key of Object.keys(current) as (keyof T)[]) {\n const cur = current[key] as number | undefined;\n const prev = previous[key] as number | undefined;\n if (cur !== undefined) {\n result[key as string] = cur - (prev ?? 0);\n }\n }\n return result as T;\n}\n\n/**\n * Computes the delta between two {@link UsageMetadata} objects by\n * subtracting each field in `previous` from the corresponding field in\n * `current`.\n *\n * The Gemini streaming API reports **cumulative** token counts on every\n * SSE chunk, but `@langchain/core`'s `mergeUsageMetadata` combines\n * values via addition. Emitting raw cumulative values on each\n * `AIMessageChunk` would therefore inflate the totals when chunks are\n * concatenated. This function converts cumulative values into per-chunk\n * deltas so that additive merging produces the correct final totals.\n *\n * @param current - The cumulative usage metadata from the current chunk.\n * @param previous - The cumulative usage metadata from the previous\n * chunk, or `undefined` for the first chunk in the stream.\n * @returns The per-chunk delta usage metadata.\n */\nexport function subtractUsageMetadata(\n current: UsageMetadata,\n previous?: UsageMetadata\n): UsageMetadata {\n if (!previous) return current;\n return {\n input_tokens: current.input_tokens - previous.input_tokens,\n output_tokens: current.output_tokens - previous.output_tokens,\n total_tokens: current.total_tokens - previous.total_tokens,\n input_token_details: subtractTokenDetails(\n current.input_token_details,\n previous.input_token_details\n ),\n output_token_details: subtractTokenDetails(\n current.output_token_details,\n previous.output_token_details\n ),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;AAmBA,SAAS,qBACP,SACA,UACe;AACf,KAAI,CAAC,QAAS,QAAO;AACrB,KAAI,CAAC,SAAU,QAAO;CACtB,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,OAAO,OAAO,KAAK,QAAQ,EAAiB;EACrD,MAAM,MAAM,QAAQ;EACpB,MAAM,OAAO,SAAS;AACtB,MAAI,QAAQ,OACV,QAAO,OAAiB,OAAO,QAAQ;;AAG3C,QAAO;;;;;;;;;;;;;;;;;;;AAoBT,SAAgB,sBACd,SACA,UACe;AACf,KAAI,CAAC,SAAU,QAAO;AACtB,QAAO;EACL,cAAc,QAAQ,eAAe,SAAS;EAC9C,eAAe,QAAQ,gBAAgB,SAAS;EAChD,cAAc,QAAQ,eAAe,SAAS;EAC9C,qBAAqB,qBACnB,QAAQ,qBACR,SAAS,oBACV;EACD,sBAAsB,qBACpB,QAAQ,sBACR,SAAS,qBACV;EACF"}
@@ -0,0 +1,56 @@
1
+ //#region src/utils/metadata.ts
2
+ /**
3
+ * Computes the delta between two token detail objects by subtracting
4
+ * each field in `previous` from the corresponding field in `current`.
5
+ *
6
+ * This is used to convert cumulative token detail counts (as reported
7
+ * by the Gemini streaming API) into per-chunk deltas that can be
8
+ * correctly summed by {@link mergeUsageMetadata} during chunk
9
+ * concatenation.
10
+ *
11
+ * @param current - The cumulative token details from the current chunk.
12
+ * @param previous - The cumulative token details from the previous chunk.
13
+ * @returns The per-chunk delta, or `undefined` if `current` is undefined.
14
+ */
15
+ function subtractTokenDetails(current, previous) {
16
+ if (!current) return void 0;
17
+ if (!previous) return current;
18
+ const result = {};
19
+ for (const key of Object.keys(current)) {
20
+ const cur = current[key];
21
+ const prev = previous[key];
22
+ if (cur !== void 0) result[key] = cur - (prev ?? 0);
23
+ }
24
+ return result;
25
+ }
26
+ /**
27
+ * Computes the delta between two {@link UsageMetadata} objects by
28
+ * subtracting each field in `previous` from the corresponding field in
29
+ * `current`.
30
+ *
31
+ * The Gemini streaming API reports **cumulative** token counts on every
32
+ * SSE chunk, but `@langchain/core`'s `mergeUsageMetadata` combines
33
+ * values via addition. Emitting raw cumulative values on each
34
+ * `AIMessageChunk` would therefore inflate the totals when chunks are
35
+ * concatenated. This function converts cumulative values into per-chunk
36
+ * deltas so that additive merging produces the correct final totals.
37
+ *
38
+ * @param current - The cumulative usage metadata from the current chunk.
39
+ * @param previous - The cumulative usage metadata from the previous
40
+ * chunk, or `undefined` for the first chunk in the stream.
41
+ * @returns The per-chunk delta usage metadata.
42
+ */
43
+ function subtractUsageMetadata(current, previous) {
44
+ if (!previous) return current;
45
+ return {
46
+ input_tokens: current.input_tokens - previous.input_tokens,
47
+ output_tokens: current.output_tokens - previous.output_tokens,
48
+ total_tokens: current.total_tokens - previous.total_tokens,
49
+ input_token_details: subtractTokenDetails(current.input_token_details, previous.input_token_details),
50
+ output_token_details: subtractTokenDetails(current.output_token_details, previous.output_token_details)
51
+ };
52
+ }
53
+
54
+ //#endregion
55
+ export { subtractUsageMetadata };
56
+ //# sourceMappingURL=metadata.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"metadata.js","names":[],"sources":["../../src/utils/metadata.ts"],"sourcesContent":["import type {\n UsageMetadata,\n InputTokenDetails,\n OutputTokenDetails,\n} from \"@langchain/core/messages\";\n\n/**\n * Computes the delta between two token detail objects by subtracting\n * each field in `previous` from the corresponding field in `current`.\n *\n * This is used to convert cumulative token detail counts (as reported\n * by the Gemini streaming API) into per-chunk deltas that can be\n * correctly summed by {@link mergeUsageMetadata} during chunk\n * concatenation.\n *\n * @param current - The cumulative token details from the current chunk.\n * @param previous - The cumulative token details from the previous chunk.\n * @returns The per-chunk delta, or `undefined` if `current` is undefined.\n */\nfunction subtractTokenDetails<T extends InputTokenDetails | OutputTokenDetails>(\n current?: T,\n previous?: T\n): T | undefined {\n if (!current) return undefined;\n if (!previous) return current;\n const result = {} as Record<string, number>;\n for (const key of Object.keys(current) as (keyof T)[]) {\n const cur = current[key] as number | undefined;\n const prev = previous[key] as number | undefined;\n if (cur !== undefined) {\n result[key as string] = cur - (prev ?? 0);\n }\n }\n return result as T;\n}\n\n/**\n * Computes the delta between two {@link UsageMetadata} objects by\n * subtracting each field in `previous` from the corresponding field in\n * `current`.\n *\n * The Gemini streaming API reports **cumulative** token counts on every\n * SSE chunk, but `@langchain/core`'s `mergeUsageMetadata` combines\n * values via addition. Emitting raw cumulative values on each\n * `AIMessageChunk` would therefore inflate the totals when chunks are\n * concatenated. This function converts cumulative values into per-chunk\n * deltas so that additive merging produces the correct final totals.\n *\n * @param current - The cumulative usage metadata from the current chunk.\n * @param previous - The cumulative usage metadata from the previous\n * chunk, or `undefined` for the first chunk in the stream.\n * @returns The per-chunk delta usage metadata.\n */\nexport function subtractUsageMetadata(\n current: UsageMetadata,\n previous?: UsageMetadata\n): UsageMetadata {\n if (!previous) return current;\n return {\n input_tokens: current.input_tokens - previous.input_tokens,\n output_tokens: current.output_tokens - previous.output_tokens,\n total_tokens: current.total_tokens - previous.total_tokens,\n input_token_details: subtractTokenDetails(\n current.input_token_details,\n previous.input_token_details\n ),\n output_token_details: subtractTokenDetails(\n current.output_token_details,\n previous.output_token_details\n ),\n };\n}\n"],"mappings":";;;;;;;;;;;;;;AAmBA,SAAS,qBACP,SACA,UACe;AACf,KAAI,CAAC,QAAS,QAAO;AACrB,KAAI,CAAC,SAAU,QAAO;CACtB,MAAM,SAAS,EAAE;AACjB,MAAK,MAAM,OAAO,OAAO,KAAK,QAAQ,EAAiB;EACrD,MAAM,MAAM,QAAQ;EACpB,MAAM,OAAO,SAAS;AACtB,MAAI,QAAQ,OACV,QAAO,OAAiB,OAAO,QAAQ;;AAG3C,QAAO;;;;;;;;;;;;;;;;;;;AAoBT,SAAgB,sBACd,SACA,UACe;AACf,KAAI,CAAC,SAAU,QAAO;AACtB,QAAO;EACL,cAAc,QAAQ,eAAe,SAAS;EAC9C,eAAe,QAAQ,gBAAgB,SAAS;EAChD,cAAc,QAAQ,eAAe,SAAS;EAC9C,qBAAqB,qBACnB,QAAQ,qBACR,SAAS,oBACV;EACD,sBAAsB,qBACpB,QAAQ,sBACR,SAAS,qBACV;EACF"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@langchain/google",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Google integrations for LangChain.js",
5
5
  "author": "LangChain",
6
6
  "license": "MIT",
@@ -16,7 +16,8 @@
16
16
  "dependencies": {
17
17
  "eventsource-parser": "^3.0.6",
18
18
  "google-auth-library": "^10.5.0",
19
- "jose": "^6.1.3"
19
+ "jose": "^6.1.3",
20
+ "uuid": "^10.0.0"
20
21
  },
21
22
  "peerDependencies": {
22
23
  "@langchain/core": "^1.0.0"
@@ -31,7 +32,7 @@
31
32
  "vitest": "^3.2.4",
32
33
  "zod": "^3.25.76 || ^4",
33
34
  "zod-to-json-schema": "^3.24.6",
34
- "@langchain/core": "^1.1.25",
35
+ "@langchain/core": "^1.1.29",
35
36
  "@langchain/eslint": "0.1.1",
36
37
  "@langchain/standard-tests": "0.0.23"
37
38
  },