@librechat/agents 3.1.80 → 3.1.81
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.
- package/dist/cjs/llm/vertexai/index.cjs +15 -15
- package/dist/cjs/llm/vertexai/index.cjs.map +1 -1
- package/dist/esm/llm/vertexai/index.mjs +15 -16
- package/dist/esm/llm/vertexai/index.mjs.map +1 -1
- package/dist/types/llm/vertexai/index.d.ts +18 -1
- package/package.json +1 -1
- package/src/llm/vertexai/fixThoughtSignatures.test.ts +154 -0
- package/src/llm/vertexai/index.ts +16 -22
|
@@ -50,34 +50,33 @@ function repairStreamUsageMetadata(current, generationInfoUsage) {
|
|
|
50
50
|
* - The signature for a functionCall part is an empty string
|
|
51
51
|
*
|
|
52
52
|
* This function correlates each "model" content block in the formatted request
|
|
53
|
-
* back to its originating AI message
|
|
54
|
-
* that the library failed to apply.
|
|
53
|
+
* back to its originating AI message by *position*, then re-attaches non-empty
|
|
54
|
+
* signatures that the library failed to apply. AI messages without signatures
|
|
55
|
+
* still consume their slot — filtering them out shifted later messages onto
|
|
56
|
+
* the wrong content block and dropped real signatures on the floor.
|
|
55
57
|
*/
|
|
56
58
|
function fixThoughtSignatures(contents, input) {
|
|
57
|
-
//
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
msg.additional_kwargs.signatures.length > 0);
|
|
61
|
-
// Collect "model" content blocks from the formatted request, in order
|
|
59
|
+
// All AI messages, in order — non-signature ones still consume positional
|
|
60
|
+
// slots so later messages line up with their model content blocks.
|
|
61
|
+
const aiMessages = input.filter(messages.isAIMessage);
|
|
62
62
|
const modelContents = contents.filter((c) => c.role === 'model');
|
|
63
|
-
// They should correspond 1:1 in order (both derived from the same input sequence)
|
|
64
63
|
const count = Math.min(aiMessages.length, modelContents.length);
|
|
65
64
|
for (let i = 0; i < count; i++) {
|
|
66
|
-
const
|
|
65
|
+
const signatures = aiMessages[i].additional_kwargs
|
|
66
|
+
?.signatures;
|
|
67
|
+
if (!Array.isArray(signatures) || signatures.length === 0)
|
|
68
|
+
continue;
|
|
67
69
|
const content = modelContents[i];
|
|
68
|
-
const signatures = msg.additional_kwargs?.signatures;
|
|
69
|
-
// Collect non-empty signatures that aren't already attached to any part
|
|
70
70
|
const attachedSignatures = new Set(content.parts
|
|
71
71
|
.map((p) => p.thoughtSignature)
|
|
72
72
|
.filter((s) => s != null && s !== ''));
|
|
73
|
-
const availableSignatures = signatures
|
|
74
|
-
// Assign available signatures to functionCall parts missing one, in order
|
|
73
|
+
const availableSignatures = signatures.filter((s) => s != null && s !== '' && !attachedSignatures.has(s));
|
|
75
74
|
let sigIdx = 0;
|
|
76
75
|
for (const part of content.parts) {
|
|
77
76
|
if ('functionCall' in part &&
|
|
78
77
|
(part.thoughtSignature == null || part.thoughtSignature === '') &&
|
|
79
|
-
sigIdx <
|
|
80
|
-
part.thoughtSignature = availableSignatures
|
|
78
|
+
sigIdx < availableSignatures.length) {
|
|
79
|
+
part.thoughtSignature = availableSignatures[sigIdx];
|
|
81
80
|
sigIdx++;
|
|
82
81
|
}
|
|
83
82
|
}
|
|
@@ -457,5 +456,6 @@ class ChatVertexAI extends googleGauth.ChatGoogle {
|
|
|
457
456
|
}
|
|
458
457
|
|
|
459
458
|
exports.ChatVertexAI = ChatVertexAI;
|
|
459
|
+
exports.fixThoughtSignatures = fixThoughtSignatures;
|
|
460
460
|
exports.repairStreamUsageMetadata = repairStreamUsageMetadata;
|
|
461
461
|
//# sourceMappingURL=index.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sources":["../../../../src/llm/vertexai/index.ts"],"sourcesContent":["import { ChatGoogle } from '@langchain/google-gauth';\nimport { ChatConnection } from '@langchain/google-common';\nimport type {\n GeminiContent,\n GeminiRequest,\n GoogleAIModelRequestParams,\n GoogleAbstractedClient,\n} from '@langchain/google-common';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport { AIMessageChunk, isAIMessage } from '@langchain/core/messages';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';\n\n/**\n * `@langchain/google-common`'s `_streamResponseChunks` emits usage on TWO\n * different paths within the same stream:\n *\n * - Streaming chunks set `chunk.generationInfo.usage_metadata` via\n * `responseToUsageMetadata`, which correctly sums\n * `candidatesTokenCount + thoughtsTokenCount` and includes\n * `output_token_details.reasoning`.\n * - The trailing fallback chunk (emitted after the API stream exhausts)\n * attaches its own `chunk.message.usage_metadata` built inline as\n * `output_tokens = candidatesTokenCount` only — dropping\n * `thoughtsTokenCount` and `output_token_details` entirely.\n *\n * After `AIMessageChunk.concat`, only `message.usage_metadata` survives —\n * which is the buggy fallback value. This breaks the documented\n * `total_tokens === input_tokens + output_tokens` invariant and silently\n * undercharges thinking models for reasoning tokens.\n *\n * The repair: track the last `generationInfo.usage_metadata` we see, and\n * when the fallback chunk arrives with its buggy `message.usage_metadata`,\n * replace it with the tracked good value. `CustomChatGoogleGenerativeAI`\n * solves the same problem for the Google API path differently — by\n * overriding `_convertToUsageMetadata`.\n */\nexport function repairStreamUsageMetadata(\n current: UsageMetadata | undefined,\n generationInfoUsage: UsageMetadata | undefined\n): UsageMetadata | undefined {\n if (!current) return current;\n if (!generationInfoUsage) return current;\n if (generationInfoUsage.total_tokens !== current.total_tokens) return current;\n if (generationInfoUsage.output_tokens <= current.output_tokens)\n return current;\n return generationInfoUsage;\n}\n\ntype AdditionalKwargs =\n | undefined\n | (BaseMessage['additional_kwargs'] & {\n signatures?: Array<string | undefined>;\n });\n\n/**\n * Fixes thought signatures on functionCall parts in the formatted Gemini request.\n *\n * `@langchain/google-common` stores signatures as a flat array in\n * `additional_kwargs.signatures` (one per response part) and re-attaches them\n * by index only when `signatures.length === parts.length`. This fails when:\n * - The API omits a signature (length mismatch)\n * - Streaming chunks merge with different part counts\n * - The signature for a functionCall part is an empty string\n *\n * This function correlates each \"model\" content block in the formatted request\n * back to its originating AI message, then re-attaches non-empty signatures\n * that the library failed to apply.\n */\nfunction fixThoughtSignatures(\n contents: GeminiContent[],\n input: BaseMessage[]\n): void {\n // Collect AI messages that have signatures, in order\n const aiMessages = input.filter(\n (msg) =>\n isAIMessage(msg) &&\n Array.isArray((msg.additional_kwargs as AdditionalKwargs)?.signatures) &&\n (msg.additional_kwargs.signatures as string[]).length > 0\n );\n\n // Collect \"model\" content blocks from the formatted request, in order\n const modelContents = contents.filter((c) => c.role === 'model');\n\n // They should correspond 1:1 in order (both derived from the same input sequence)\n const count = Math.min(aiMessages.length, modelContents.length);\n for (let i = 0; i < count; i++) {\n const msg = aiMessages[i];\n const content = modelContents[i];\n const signatures = (msg.additional_kwargs as AdditionalKwargs)?.signatures;\n\n // Collect non-empty signatures that aren't already attached to any part\n const attachedSignatures = new Set(\n content.parts\n .map((p) => p.thoughtSignature)\n .filter((s): s is string => s != null && s !== '')\n );\n const availableSignatures = signatures?.filter(\n (s) => s != null && s !== '' && !attachedSignatures.has(s)\n );\n\n // Assign available signatures to functionCall parts missing one, in order\n let sigIdx = 0;\n for (const part of content.parts) {\n if (\n 'functionCall' in part &&\n (part.thoughtSignature == null || part.thoughtSignature === '') &&\n sigIdx < (availableSignatures?.length ?? 0)\n ) {\n part.thoughtSignature = availableSignatures?.[sigIdx];\n sigIdx++;\n }\n }\n }\n}\n\nclass CustomChatConnection extends ChatConnection<VertexAIClientOptions> {\n thinkingConfig?: GoogleThinkingConfig;\n\n async formatData(\n input: BaseMessage[],\n parameters: GoogleAIModelRequestParams\n ): Promise<unknown> {\n const formattedData = (await super.formatData(\n input,\n parameters\n )) as GeminiRequest;\n if (formattedData.generationConfig?.thinkingConfig?.thinkingBudget === -1) {\n // -1 means \"let the model decide\" - delete the property so the API doesn't receive an invalid value\n if (\n formattedData.generationConfig.thinkingConfig.includeThoughts === false\n ) {\n formattedData.generationConfig.thinkingConfig.includeThoughts = true;\n }\n delete formattedData.generationConfig.thinkingConfig.thinkingBudget;\n }\n if (this.thinkingConfig?.thinkingLevel != null) {\n formattedData.generationConfig ??= {};\n // thinkingLevel and thinkingBudget cannot coexist — the API rejects the request.\n // Remove thinkingBudget when thinkingLevel is set.\n const { thinkingBudget: _, ...existingThinkingConfig } =\n (formattedData.generationConfig.thinkingConfig as\n | Record<string, unknown>\n | undefined) ?? {};\n (\n formattedData.generationConfig as Record<string, unknown>\n ).thinkingConfig = {\n ...existingThinkingConfig,\n thinkingLevel: this.thinkingConfig.thinkingLevel,\n ...(this.thinkingConfig.includeThoughts != null && {\n includeThoughts: this.thinkingConfig.includeThoughts,\n }),\n };\n }\n if (formattedData.contents) {\n fixThoughtSignatures(formattedData.contents, input);\n // gemini-3.1+ models reject role=\"function\"; convert to role=\"user\"\n for (const content of formattedData.contents) {\n if (content.role === 'function') {\n (content as { role: string }).role = 'user';\n }\n }\n }\n return formattedData;\n }\n}\n\n/**\n * Integration with Google Vertex AI chat models.\n *\n * Setup:\n * Install `@langchain/google-vertexai` and set your stringified\n * Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.\n *\n * ```bash\n * npm install @langchain/google-vertexai\n * export GOOGLE_APPLICATION_CREDENTIALS=\"path/to/credentials\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatVertexAI } from '@langchain/google-vertexai';\n *\n * const llm = new ChatVertexAI({\n * model: \"gemini-1.5-pro\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\\nHere's why this is the best translation:\\n\\n* **J'adore** means \\\"I love\\\" and conveys a strong passion.\\n* **Programmer** is the French verb for \\\"to program.\\\"\\n\\nThis translation is natural and idiomatic in French. \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 63,\n * \"total_tokens\": 72\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York City, NY' },\n * id: '33c1c1f47e2f492799c77d2800a43912',\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: 'What do you call a cat that loves to bowl?',\n * punchline: 'An alley cat!'\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Stream Usage Metadata</strong></summary>\n *\n * ```typescript\n * const streamForMetadata = await llm.stream(\n * input,\n * {\n * streamUsage: true\n * }\n * );\n * let fullForMetadata: AIMessageChunk | undefined;\n * for await (const chunk of streamForMetadata) {\n * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);\n * }\n * console.log(fullForMetadata?.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatVertexAI extends ChatGoogle {\n lc_namespace = ['langchain', 'chat_models', 'vertexai'];\n dynamicThinkingBudget = false;\n thinkingConfig?: GoogleThinkingConfig;\n\n static lc_name(): 'LibreChatVertexAI' {\n return 'LibreChatVertexAI';\n }\n\n constructor(model: string, fields?: Omit<VertexAIClientOptions, 'model'>);\n constructor(fields?: VertexAIClientOptions);\n constructor(\n modelOrFields?: string | VertexAIClientOptions,\n params?: Omit<VertexAIClientOptions, 'model'>\n ) {\n const fields =\n typeof modelOrFields === 'string'\n ? { ...(params ?? {}), model: modelOrFields }\n : modelOrFields;\n const dynamicThinkingBudget = fields?.thinkingBudget === -1;\n super({\n ...fields,\n platformType: 'gcp',\n });\n this.dynamicThinkingBudget = dynamicThinkingBudget;\n this.thinkingConfig = fields?.thinkingConfig;\n }\n invocationParams(\n options?: this['ParsedCallOptions'] | undefined\n ): GoogleAIModelRequestParams {\n const params = super.invocationParams(options);\n if (this.dynamicThinkingBudget) {\n params.maxReasoningTokens = -1;\n }\n return params;\n }\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n let lastGoodUsage: UsageMetadata | undefined;\n for await (const chunk of super._streamResponseChunks(\n messages,\n options,\n runManager\n )) {\n const genUsage = (\n chunk.generationInfo as { usage_metadata?: UsageMetadata } | undefined\n )?.usage_metadata;\n if (genUsage) {\n lastGoodUsage = genUsage;\n }\n if (chunk.message instanceof AIMessageChunk) {\n const repaired = repairStreamUsageMetadata(\n chunk.message.usage_metadata,\n lastGoodUsage\n );\n if (repaired !== chunk.message.usage_metadata) {\n chunk.message.usage_metadata = repaired;\n }\n }\n yield chunk;\n }\n }\n buildConnection(\n fields: VertexAIClientOptions | undefined,\n client: GoogleAbstractedClient\n ): void {\n // Note: buildConnection is called from super() BEFORE this.thinkingConfig is set,\n // so we must read thinkingConfig from `fields` directly.\n const thinkingConfig = fields?.thinkingConfig ?? this.thinkingConfig;\n\n const connection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n connection.thinkingConfig = thinkingConfig;\n this.connection = connection;\n\n const streamedConnection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n streamedConnection.thinkingConfig = thinkingConfig;\n this.streamedConnection = streamedConnection;\n }\n}\n"],"names":["isAIMessage","ChatConnection","ChatGoogle","messages","AIMessageChunk"],"mappings":";;;;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,SAAU,yBAAyB,CACvC,OAAkC,EAClC,mBAA8C,EAAA;AAE9C,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,OAAO,OAAO;AAC5B,IAAA,IAAI,CAAC,mBAAmB;AAAE,QAAA,OAAO,OAAO;AACxC,IAAA,IAAI,mBAAmB,CAAC,YAAY,KAAK,OAAO,CAAC,YAAY;AAAE,QAAA,OAAO,OAAO;AAC7E,IAAA,IAAI,mBAAmB,CAAC,aAAa,IAAI,OAAO,CAAC,aAAa;AAC5D,QAAA,OAAO,OAAO;AAChB,IAAA,OAAO,mBAAmB;AAC5B;AAQA;;;;;;;;;;;;;AAaG;AACH,SAAS,oBAAoB,CAC3B,QAAyB,EACzB,KAAoB,EAAA;;AAGpB,IAAA,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAC7B,CAAC,GAAG,KACFA,oBAAW,CAAC,GAAG,CAAC;QAChB,KAAK,CAAC,OAAO,CAAE,GAAG,CAAC,iBAAsC,EAAE,UAAU,CAAC;QACrE,GAAG,CAAC,iBAAiB,CAAC,UAAuB,CAAC,MAAM,GAAG,CAAC,CAC5D;;AAGD,IAAA,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC;;AAGhE,IAAA,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC;AAC/D,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE;AAC9B,QAAA,MAAM,GAAG,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AAc;YACjB,IAAI,QAAQ,EAAE;gBACZ,aAAa,GAAG,QAAQ;YAC1B;AACA,YAAA,IAAI,KAAK,CAAC,OAAO,YAAYC,uBAAc,EAAE;AAC3C,gBAAA,MAAM,QAAQ,GAAG,yBAAyB,CACxC,KAAK,CAAC,OAAO,CAAC,cAAc,EAC5B,aAAa,CACd;gBACD,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE;AAC7C,oBAAA,KAAK,CAAC,OAAO,CAAC,cAAc,GAAG,QAAQ;gBACzC;YACF;AACA,YAAA,MAAM,KAAK;QACb;IACF;IACA,eAAe,CACb,MAAyC,EACzC,MAA8B,EAAA;;;QAI9B,MAAM,cAAc,GAAG,MAAM,EAAE,cAAc,IAAI,IAAI,CAAC,cAAc;QAEpE,MAAM,UAAU,GAAG,IAAI,oBAAoB,CACzC,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,KAAK,CACN;AACD,QAAA,UAAU,CAAC,cAAc,GAAG,cAAc;AAC1C,QAAA,IAAI,CAAC,UAAU,GAAG,UAAU;QAE5B,MAAM,kBAAkB,GAAG,IAAI,oBAAoB,CACjD,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,IAAI,CACL;AACD,QAAA,kBAAkB,CAAC,cAAc,GAAG,cAAc;AAClD,QAAA,IAAI,CAAC,kBAAkB,GAAG,kBAAkB;IAC9C;AACD;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs","sources":["../../../../src/llm/vertexai/index.ts"],"sourcesContent":["import { ChatGoogle } from '@langchain/google-gauth';\nimport { ChatConnection } from '@langchain/google-common';\nimport type {\n GeminiContent,\n GeminiRequest,\n GoogleAIModelRequestParams,\n GoogleAbstractedClient,\n} from '@langchain/google-common';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport { AIMessageChunk, isAIMessage } from '@langchain/core/messages';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';\n\n/**\n * `@langchain/google-common`'s `_streamResponseChunks` emits usage on TWO\n * different paths within the same stream:\n *\n * - Streaming chunks set `chunk.generationInfo.usage_metadata` via\n * `responseToUsageMetadata`, which correctly sums\n * `candidatesTokenCount + thoughtsTokenCount` and includes\n * `output_token_details.reasoning`.\n * - The trailing fallback chunk (emitted after the API stream exhausts)\n * attaches its own `chunk.message.usage_metadata` built inline as\n * `output_tokens = candidatesTokenCount` only — dropping\n * `thoughtsTokenCount` and `output_token_details` entirely.\n *\n * After `AIMessageChunk.concat`, only `message.usage_metadata` survives —\n * which is the buggy fallback value. This breaks the documented\n * `total_tokens === input_tokens + output_tokens` invariant and silently\n * undercharges thinking models for reasoning tokens.\n *\n * The repair: track the last `generationInfo.usage_metadata` we see, and\n * when the fallback chunk arrives with its buggy `message.usage_metadata`,\n * replace it with the tracked good value. `CustomChatGoogleGenerativeAI`\n * solves the same problem for the Google API path differently — by\n * overriding `_convertToUsageMetadata`.\n */\nexport function repairStreamUsageMetadata(\n current: UsageMetadata | undefined,\n generationInfoUsage: UsageMetadata | undefined\n): UsageMetadata | undefined {\n if (!current) return current;\n if (!generationInfoUsage) return current;\n if (generationInfoUsage.total_tokens !== current.total_tokens) return current;\n if (generationInfoUsage.output_tokens <= current.output_tokens)\n return current;\n return generationInfoUsage;\n}\n\ntype AdditionalKwargs =\n | undefined\n | (BaseMessage['additional_kwargs'] & {\n signatures?: Array<string | undefined>;\n });\n\n/**\n * Fixes thought signatures on functionCall parts in the formatted Gemini request.\n *\n * `@langchain/google-common` stores signatures as a flat array in\n * `additional_kwargs.signatures` (one per response part) and re-attaches them\n * by index only when `signatures.length === parts.length`. This fails when:\n * - The API omits a signature (length mismatch)\n * - Streaming chunks merge with different part counts\n * - The signature for a functionCall part is an empty string\n *\n * This function correlates each \"model\" content block in the formatted request\n * back to its originating AI message by *position*, then re-attaches non-empty\n * signatures that the library failed to apply. AI messages without signatures\n * still consume their slot — filtering them out shifted later messages onto\n * the wrong content block and dropped real signatures on the floor.\n */\nexport function fixThoughtSignatures(\n contents: GeminiContent[],\n input: BaseMessage[]\n): void {\n // All AI messages, in order — non-signature ones still consume positional\n // slots so later messages line up with their model content blocks.\n const aiMessages = input.filter(isAIMessage);\n const modelContents = contents.filter((c) => c.role === 'model');\n\n const count = Math.min(aiMessages.length, modelContents.length);\n for (let i = 0; i < count; i++) {\n const signatures = (aiMessages[i].additional_kwargs as AdditionalKwargs)\n ?.signatures;\n if (!Array.isArray(signatures) || signatures.length === 0) continue;\n\n const content = modelContents[i];\n const attachedSignatures = new Set(\n content.parts\n .map((p) => p.thoughtSignature)\n .filter((s): s is string => s != null && s !== '')\n );\n const availableSignatures = signatures.filter(\n (s): s is string => s != null && s !== '' && !attachedSignatures.has(s)\n );\n\n let sigIdx = 0;\n for (const part of content.parts) {\n if (\n 'functionCall' in part &&\n (part.thoughtSignature == null || part.thoughtSignature === '') &&\n sigIdx < availableSignatures.length\n ) {\n part.thoughtSignature = availableSignatures[sigIdx];\n sigIdx++;\n }\n }\n }\n}\n\nclass CustomChatConnection extends ChatConnection<VertexAIClientOptions> {\n thinkingConfig?: GoogleThinkingConfig;\n\n async formatData(\n input: BaseMessage[],\n parameters: GoogleAIModelRequestParams\n ): Promise<unknown> {\n const formattedData = (await super.formatData(\n input,\n parameters\n )) as GeminiRequest;\n if (formattedData.generationConfig?.thinkingConfig?.thinkingBudget === -1) {\n // -1 means \"let the model decide\" - delete the property so the API doesn't receive an invalid value\n if (\n formattedData.generationConfig.thinkingConfig.includeThoughts === false\n ) {\n formattedData.generationConfig.thinkingConfig.includeThoughts = true;\n }\n delete formattedData.generationConfig.thinkingConfig.thinkingBudget;\n }\n if (this.thinkingConfig?.thinkingLevel != null) {\n formattedData.generationConfig ??= {};\n // thinkingLevel and thinkingBudget cannot coexist — the API rejects the request.\n // Remove thinkingBudget when thinkingLevel is set.\n const { thinkingBudget: _, ...existingThinkingConfig } =\n (formattedData.generationConfig.thinkingConfig as\n | Record<string, unknown>\n | undefined) ?? {};\n (\n formattedData.generationConfig as Record<string, unknown>\n ).thinkingConfig = {\n ...existingThinkingConfig,\n thinkingLevel: this.thinkingConfig.thinkingLevel,\n ...(this.thinkingConfig.includeThoughts != null && {\n includeThoughts: this.thinkingConfig.includeThoughts,\n }),\n };\n }\n if (formattedData.contents) {\n fixThoughtSignatures(formattedData.contents, input);\n // gemini-3.1+ models reject role=\"function\"; convert to role=\"user\"\n for (const content of formattedData.contents) {\n if (content.role === 'function') {\n (content as { role: string }).role = 'user';\n }\n }\n }\n return formattedData;\n }\n}\n\n/**\n * Integration with Google Vertex AI chat models.\n *\n * Setup:\n * Install `@langchain/google-vertexai` and set your stringified\n * Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.\n *\n * ```bash\n * npm install @langchain/google-vertexai\n * export GOOGLE_APPLICATION_CREDENTIALS=\"path/to/credentials\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatVertexAI } from '@langchain/google-vertexai';\n *\n * const llm = new ChatVertexAI({\n * model: \"gemini-1.5-pro\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\\nHere's why this is the best translation:\\n\\n* **J'adore** means \\\"I love\\\" and conveys a strong passion.\\n* **Programmer** is the French verb for \\\"to program.\\\"\\n\\nThis translation is natural and idiomatic in French. \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 63,\n * \"total_tokens\": 72\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York City, NY' },\n * id: '33c1c1f47e2f492799c77d2800a43912',\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: 'What do you call a cat that loves to bowl?',\n * punchline: 'An alley cat!'\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Stream Usage Metadata</strong></summary>\n *\n * ```typescript\n * const streamForMetadata = await llm.stream(\n * input,\n * {\n * streamUsage: true\n * }\n * );\n * let fullForMetadata: AIMessageChunk | undefined;\n * for await (const chunk of streamForMetadata) {\n * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);\n * }\n * console.log(fullForMetadata?.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatVertexAI extends ChatGoogle {\n lc_namespace = ['langchain', 'chat_models', 'vertexai'];\n dynamicThinkingBudget = false;\n thinkingConfig?: GoogleThinkingConfig;\n\n static lc_name(): 'LibreChatVertexAI' {\n return 'LibreChatVertexAI';\n }\n\n constructor(model: string, fields?: Omit<VertexAIClientOptions, 'model'>);\n constructor(fields?: VertexAIClientOptions);\n constructor(\n modelOrFields?: string | VertexAIClientOptions,\n params?: Omit<VertexAIClientOptions, 'model'>\n ) {\n const fields =\n typeof modelOrFields === 'string'\n ? { ...(params ?? {}), model: modelOrFields }\n : modelOrFields;\n const dynamicThinkingBudget = fields?.thinkingBudget === -1;\n super({\n ...fields,\n platformType: 'gcp',\n });\n this.dynamicThinkingBudget = dynamicThinkingBudget;\n this.thinkingConfig = fields?.thinkingConfig;\n }\n invocationParams(\n options?: this['ParsedCallOptions'] | undefined\n ): GoogleAIModelRequestParams {\n const params = super.invocationParams(options);\n if (this.dynamicThinkingBudget) {\n params.maxReasoningTokens = -1;\n }\n return params;\n }\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n let lastGoodUsage: UsageMetadata | undefined;\n for await (const chunk of super._streamResponseChunks(\n messages,\n options,\n runManager\n )) {\n const genUsage = (\n chunk.generationInfo as { usage_metadata?: UsageMetadata } | undefined\n )?.usage_metadata;\n if (genUsage) {\n lastGoodUsage = genUsage;\n }\n if (chunk.message instanceof AIMessageChunk) {\n const repaired = repairStreamUsageMetadata(\n chunk.message.usage_metadata,\n lastGoodUsage\n );\n if (repaired !== chunk.message.usage_metadata) {\n chunk.message.usage_metadata = repaired;\n }\n }\n yield chunk;\n }\n }\n buildConnection(\n fields: VertexAIClientOptions | undefined,\n client: GoogleAbstractedClient\n ): void {\n // Note: buildConnection is called from super() BEFORE this.thinkingConfig is set,\n // so we must read thinkingConfig from `fields` directly.\n const thinkingConfig = fields?.thinkingConfig ?? this.thinkingConfig;\n\n const connection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n connection.thinkingConfig = thinkingConfig;\n this.connection = connection;\n\n const streamedConnection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n streamedConnection.thinkingConfig = thinkingConfig;\n this.streamedConnection = stre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,CACd;gBACD,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE;AAC7C,oBAAA,KAAK,CAAC,OAAO,CAAC,cAAc,GAAG,QAAQ;gBACzC;YACF;AACA,YAAA,MAAM,KAAK;QACb;IACF;IACA,eAAe,CACb,MAAyC,EACzC,MAA8B,EAAA;;;QAI9B,MAAM,cAAc,GAAG,MAAM,EAAE,cAAc,IAAI,IAAI,CAAC,cAAc;QAEpE,MAAM,UAAU,GAAG,IAAI,oBAAoB,CACzC,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,KAAK,CACN;AACD,QAAA,UAAU,CAAC,cAAc,GAAG,cAAc;AAC1C,QAAA,IAAI,CAAC,UAAU,GAAG,UAAU;QAE5B,MAAM,kBAAkB,GAAG,IAAI,oBAAoB,CACjD,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,IAAI,CACL;AACD,QAAA,kBAAkB,CAAC,cAAc,GAAG,cAAc;AAClD,QAAA,IAAI,CAAC,kBAAkB,GAAG,kBAAkB;IAC9C;AACD;;;;;;"}
|
|
@@ -48,34 +48,33 @@ function repairStreamUsageMetadata(current, generationInfoUsage) {
|
|
|
48
48
|
* - The signature for a functionCall part is an empty string
|
|
49
49
|
*
|
|
50
50
|
* This function correlates each "model" content block in the formatted request
|
|
51
|
-
* back to its originating AI message
|
|
52
|
-
* that the library failed to apply.
|
|
51
|
+
* back to its originating AI message by *position*, then re-attaches non-empty
|
|
52
|
+
* signatures that the library failed to apply. AI messages without signatures
|
|
53
|
+
* still consume their slot — filtering them out shifted later messages onto
|
|
54
|
+
* the wrong content block and dropped real signatures on the floor.
|
|
53
55
|
*/
|
|
54
56
|
function fixThoughtSignatures(contents, input) {
|
|
55
|
-
//
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
msg.additional_kwargs.signatures.length > 0);
|
|
59
|
-
// Collect "model" content blocks from the formatted request, in order
|
|
57
|
+
// All AI messages, in order — non-signature ones still consume positional
|
|
58
|
+
// slots so later messages line up with their model content blocks.
|
|
59
|
+
const aiMessages = input.filter(isAIMessage);
|
|
60
60
|
const modelContents = contents.filter((c) => c.role === 'model');
|
|
61
|
-
// They should correspond 1:1 in order (both derived from the same input sequence)
|
|
62
61
|
const count = Math.min(aiMessages.length, modelContents.length);
|
|
63
62
|
for (let i = 0; i < count; i++) {
|
|
64
|
-
const
|
|
63
|
+
const signatures = aiMessages[i].additional_kwargs
|
|
64
|
+
?.signatures;
|
|
65
|
+
if (!Array.isArray(signatures) || signatures.length === 0)
|
|
66
|
+
continue;
|
|
65
67
|
const content = modelContents[i];
|
|
66
|
-
const signatures = msg.additional_kwargs?.signatures;
|
|
67
|
-
// Collect non-empty signatures that aren't already attached to any part
|
|
68
68
|
const attachedSignatures = new Set(content.parts
|
|
69
69
|
.map((p) => p.thoughtSignature)
|
|
70
70
|
.filter((s) => s != null && s !== ''));
|
|
71
|
-
const availableSignatures = signatures
|
|
72
|
-
// Assign available signatures to functionCall parts missing one, in order
|
|
71
|
+
const availableSignatures = signatures.filter((s) => s != null && s !== '' && !attachedSignatures.has(s));
|
|
73
72
|
let sigIdx = 0;
|
|
74
73
|
for (const part of content.parts) {
|
|
75
74
|
if ('functionCall' in part &&
|
|
76
75
|
(part.thoughtSignature == null || part.thoughtSignature === '') &&
|
|
77
|
-
sigIdx <
|
|
78
|
-
part.thoughtSignature = availableSignatures
|
|
76
|
+
sigIdx < availableSignatures.length) {
|
|
77
|
+
part.thoughtSignature = availableSignatures[sigIdx];
|
|
79
78
|
sigIdx++;
|
|
80
79
|
}
|
|
81
80
|
}
|
|
@@ -454,5 +453,5 @@ class ChatVertexAI extends ChatGoogle {
|
|
|
454
453
|
}
|
|
455
454
|
}
|
|
456
455
|
|
|
457
|
-
export { ChatVertexAI, repairStreamUsageMetadata };
|
|
456
|
+
export { ChatVertexAI, fixThoughtSignatures, repairStreamUsageMetadata };
|
|
458
457
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../../src/llm/vertexai/index.ts"],"sourcesContent":["import { ChatGoogle } from '@langchain/google-gauth';\nimport { ChatConnection } from '@langchain/google-common';\nimport type {\n GeminiContent,\n GeminiRequest,\n GoogleAIModelRequestParams,\n GoogleAbstractedClient,\n} from '@langchain/google-common';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport { AIMessageChunk, isAIMessage } from '@langchain/core/messages';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';\n\n/**\n * `@langchain/google-common`'s `_streamResponseChunks` emits usage on TWO\n * different paths within the same stream:\n *\n * - Streaming chunks set `chunk.generationInfo.usage_metadata` via\n * `responseToUsageMetadata`, which correctly sums\n * `candidatesTokenCount + thoughtsTokenCount` and includes\n * `output_token_details.reasoning`.\n * - The trailing fallback chunk (emitted after the API stream exhausts)\n * attaches its own `chunk.message.usage_metadata` built inline as\n * `output_tokens = candidatesTokenCount` only — dropping\n * `thoughtsTokenCount` and `output_token_details` entirely.\n *\n * After `AIMessageChunk.concat`, only `message.usage_metadata` survives —\n * which is the buggy fallback value. This breaks the documented\n * `total_tokens === input_tokens + output_tokens` invariant and silently\n * undercharges thinking models for reasoning tokens.\n *\n * The repair: track the last `generationInfo.usage_metadata` we see, and\n * when the fallback chunk arrives with its buggy `message.usage_metadata`,\n * replace it with the tracked good value. `CustomChatGoogleGenerativeAI`\n * solves the same problem for the Google API path differently — by\n * overriding `_convertToUsageMetadata`.\n */\nexport function repairStreamUsageMetadata(\n current: UsageMetadata | undefined,\n generationInfoUsage: UsageMetadata | undefined\n): UsageMetadata | undefined {\n if (!current) return current;\n if (!generationInfoUsage) return current;\n if (generationInfoUsage.total_tokens !== current.total_tokens) return current;\n if (generationInfoUsage.output_tokens <= current.output_tokens)\n return current;\n return generationInfoUsage;\n}\n\ntype AdditionalKwargs =\n | undefined\n | (BaseMessage['additional_kwargs'] & {\n signatures?: Array<string | undefined>;\n });\n\n/**\n * Fixes thought signatures on functionCall parts in the formatted Gemini request.\n *\n * `@langchain/google-common` stores signatures as a flat array in\n * `additional_kwargs.signatures` (one per response part) and re-attaches them\n * by index only when `signatures.length === parts.length`. This fails when:\n * - The API omits a signature (length mismatch)\n * - Streaming chunks merge with different part counts\n * - The signature for a functionCall part is an empty string\n *\n * This function correlates each \"model\" content block in the formatted request\n * back to its originating AI message, then re-attaches non-empty signatures\n * that the library failed to apply.\n */\nfunction fixThoughtSignatures(\n contents: GeminiContent[],\n input: BaseMessage[]\n): void {\n // Collect AI messages that have signatures, in order\n const aiMessages = input.filter(\n (msg) =>\n isAIMessage(msg) &&\n Array.isArray((msg.additional_kwargs as AdditionalKwargs)?.signatures) &&\n (msg.additional_kwargs.signatures as string[]).length > 0\n );\n\n // Collect \"model\" content blocks from the formatted request, in order\n const modelContents = contents.filter((c) => c.role === 'model');\n\n // They should correspond 1:1 in order (both derived from the same input sequence)\n const count = Math.min(aiMessages.length, modelContents.length);\n for (let i = 0; i < count; i++) {\n const msg = aiMessages[i];\n const content = modelContents[i];\n const signatures = (msg.additional_kwargs as AdditionalKwargs)?.signatures;\n\n // Collect non-empty signatures that aren't already attached to any part\n const attachedSignatures = new Set(\n content.parts\n .map((p) => p.thoughtSignature)\n .filter((s): s is string => s != null && s !== '')\n );\n const availableSignatures = signatures?.filter(\n (s) => s != null && s !== '' && !attachedSignatures.has(s)\n );\n\n // Assign available signatures to functionCall parts missing one, in order\n let sigIdx = 0;\n for (const part of content.parts) {\n if (\n 'functionCall' in part &&\n (part.thoughtSignature == null || part.thoughtSignature === '') &&\n sigIdx < (availableSignatures?.length ?? 0)\n ) {\n part.thoughtSignature = availableSignatures?.[sigIdx];\n sigIdx++;\n }\n }\n }\n}\n\nclass CustomChatConnection extends ChatConnection<VertexAIClientOptions> {\n thinkingConfig?: GoogleThinkingConfig;\n\n async formatData(\n input: BaseMessage[],\n parameters: GoogleAIModelRequestParams\n ): Promise<unknown> {\n const formattedData = (await super.formatData(\n input,\n parameters\n )) as GeminiRequest;\n if (formattedData.generationConfig?.thinkingConfig?.thinkingBudget === -1) {\n // -1 means \"let the model decide\" - delete the property so the API doesn't receive an invalid value\n if (\n formattedData.generationConfig.thinkingConfig.includeThoughts === false\n ) {\n formattedData.generationConfig.thinkingConfig.includeThoughts = true;\n }\n delete formattedData.generationConfig.thinkingConfig.thinkingBudget;\n }\n if (this.thinkingConfig?.thinkingLevel != null) {\n formattedData.generationConfig ??= {};\n // thinkingLevel and thinkingBudget cannot coexist — the API rejects the request.\n // Remove thinkingBudget when thinkingLevel is set.\n const { thinkingBudget: _, ...existingThinkingConfig } =\n (formattedData.generationConfig.thinkingConfig as\n | Record<string, unknown>\n | undefined) ?? {};\n (\n formattedData.generationConfig as Record<string, unknown>\n ).thinkingConfig = {\n ...existingThinkingConfig,\n thinkingLevel: this.thinkingConfig.thinkingLevel,\n ...(this.thinkingConfig.includeThoughts != null && {\n includeThoughts: this.thinkingConfig.includeThoughts,\n }),\n };\n }\n if (formattedData.contents) {\n fixThoughtSignatures(formattedData.contents, input);\n // gemini-3.1+ models reject role=\"function\"; convert to role=\"user\"\n for (const content of formattedData.contents) {\n if (content.role === 'function') {\n (content as { role: string }).role = 'user';\n }\n }\n }\n return formattedData;\n }\n}\n\n/**\n * Integration with Google Vertex AI chat models.\n *\n * Setup:\n * Install `@langchain/google-vertexai` and set your stringified\n * Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.\n *\n * ```bash\n * npm install @langchain/google-vertexai\n * export GOOGLE_APPLICATION_CREDENTIALS=\"path/to/credentials\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatVertexAI } from '@langchain/google-vertexai';\n *\n * const llm = new ChatVertexAI({\n * model: \"gemini-1.5-pro\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\\nHere's why this is the best translation:\\n\\n* **J'adore** means \\\"I love\\\" and conveys a strong passion.\\n* **Programmer** is the French verb for \\\"to program.\\\"\\n\\nThis translation is natural and idiomatic in French. \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 63,\n * \"total_tokens\": 72\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York City, NY' },\n * id: '33c1c1f47e2f492799c77d2800a43912',\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: 'What do you call a cat that loves to bowl?',\n * punchline: 'An alley cat!'\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Stream Usage Metadata</strong></summary>\n *\n * ```typescript\n * const streamForMetadata = await llm.stream(\n * input,\n * {\n * streamUsage: true\n * }\n * );\n * let fullForMetadata: AIMessageChunk | undefined;\n * for await (const chunk of streamForMetadata) {\n * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);\n * }\n * console.log(fullForMetadata?.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatVertexAI extends ChatGoogle {\n lc_namespace = ['langchain', 'chat_models', 'vertexai'];\n dynamicThinkingBudget = false;\n thinkingConfig?: GoogleThinkingConfig;\n\n static lc_name(): 'LibreChatVertexAI' {\n return 'LibreChatVertexAI';\n }\n\n constructor(model: string, fields?: Omit<VertexAIClientOptions, 'model'>);\n constructor(fields?: VertexAIClientOptions);\n constructor(\n modelOrFields?: string | VertexAIClientOptions,\n params?: Omit<VertexAIClientOptions, 'model'>\n ) {\n const fields =\n typeof modelOrFields === 'string'\n ? { ...(params ?? {}), model: modelOrFields }\n : modelOrFields;\n const dynamicThinkingBudget = fields?.thinkingBudget === -1;\n super({\n ...fields,\n platformType: 'gcp',\n });\n this.dynamicThinkingBudget = dynamicThinkingBudget;\n this.thinkingConfig = fields?.thinkingConfig;\n }\n invocationParams(\n options?: this['ParsedCallOptions'] | undefined\n ): GoogleAIModelRequestParams {\n const params = super.invocationParams(options);\n if (this.dynamicThinkingBudget) {\n params.maxReasoningTokens = -1;\n }\n return params;\n }\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n let lastGoodUsage: UsageMetadata | undefined;\n for await (const chunk of super._streamResponseChunks(\n messages,\n options,\n runManager\n )) {\n const genUsage = (\n chunk.generationInfo as { usage_metadata?: UsageMetadata } | undefined\n )?.usage_metadata;\n if (genUsage) {\n lastGoodUsage = genUsage;\n }\n if (chunk.message instanceof AIMessageChunk) {\n const repaired = repairStreamUsageMetadata(\n chunk.message.usage_metadata,\n lastGoodUsage\n );\n if (repaired !== chunk.message.usage_metadata) {\n chunk.message.usage_metadata = repaired;\n }\n }\n yield chunk;\n }\n }\n buildConnection(\n fields: VertexAIClientOptions | undefined,\n client: GoogleAbstractedClient\n ): void {\n // Note: buildConnection is called from super() BEFORE this.thinkingConfig is set,\n // so we must read thinkingConfig from `fields` directly.\n const thinkingConfig = fields?.thinkingConfig ?? this.thinkingConfig;\n\n const connection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n connection.thinkingConfig = thinkingConfig;\n this.connection = connection;\n\n const streamedConnection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n streamedConnection.thinkingConfig = thinkingConfig;\n this.streamedConnection = streamedConnection;\n }\n}\n"],"names":[],"mappings":";;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,SAAU,yBAAyB,CACvC,OAAkC,EAClC,mBAA8C,EAAA;AAE9C,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,OAAO,OAAO;AAC5B,IAAA,IAAI,CAAC,mBAAmB;AAAE,QAAA,OAAO,OAAO;AACxC,IAAA,IAAI,mBAAmB,CAAC,YAAY,KAAK,OAAO,CAAC,YAAY;AAAE,QAAA,OAAO,OAAO;AAC7E,IAAA,IAAI,mBAAmB,CAAC,aAAa,IAAI,OAAO,CAAC,aAAa;AAC5D,QAAA,OAAO,OAAO;AAChB,IAAA,OAAO,mBAAmB;AAC5B;AAQA;;;;;;;;;;;;;AAaG;AACH,SAAS,oBAAoB,CAC3B,QAAyB,EACzB,KAAoB,EAAA;;AAGpB,IAAA,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAC7B,CAAC,GAAG,KACF,WAAW,CAAC,GAAG,CAAC;QAChB,KAAK,CAAC,OAAO,CAAE,GAAG,CAAC,iBAAsC,EAAE,UAAU,CAAC;QACrE,GAAG,CAAC,iBAAiB,CAAC,UAAuB,CAAC,MAAM,GAAG,CAAC,CAC5D;;AAGD,IAAA,MA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BAAkB,GAAG,kBAAkB;IAC9C;AACD;;;;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../../src/llm/vertexai/index.ts"],"sourcesContent":["import { ChatGoogle } from '@langchain/google-gauth';\nimport { ChatConnection } from '@langchain/google-common';\nimport type {\n GeminiContent,\n GeminiRequest,\n GoogleAIModelRequestParams,\n GoogleAbstractedClient,\n} from '@langchain/google-common';\nimport type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';\nimport type { BaseMessage, UsageMetadata } from '@langchain/core/messages';\nimport { AIMessageChunk, isAIMessage } from '@langchain/core/messages';\nimport type { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';\n\n/**\n * `@langchain/google-common`'s `_streamResponseChunks` emits usage on TWO\n * different paths within the same stream:\n *\n * - Streaming chunks set `chunk.generationInfo.usage_metadata` via\n * `responseToUsageMetadata`, which correctly sums\n * `candidatesTokenCount + thoughtsTokenCount` and includes\n * `output_token_details.reasoning`.\n * - The trailing fallback chunk (emitted after the API stream exhausts)\n * attaches its own `chunk.message.usage_metadata` built inline as\n * `output_tokens = candidatesTokenCount` only — dropping\n * `thoughtsTokenCount` and `output_token_details` entirely.\n *\n * After `AIMessageChunk.concat`, only `message.usage_metadata` survives —\n * which is the buggy fallback value. This breaks the documented\n * `total_tokens === input_tokens + output_tokens` invariant and silently\n * undercharges thinking models for reasoning tokens.\n *\n * The repair: track the last `generationInfo.usage_metadata` we see, and\n * when the fallback chunk arrives with its buggy `message.usage_metadata`,\n * replace it with the tracked good value. `CustomChatGoogleGenerativeAI`\n * solves the same problem for the Google API path differently — by\n * overriding `_convertToUsageMetadata`.\n */\nexport function repairStreamUsageMetadata(\n current: UsageMetadata | undefined,\n generationInfoUsage: UsageMetadata | undefined\n): UsageMetadata | undefined {\n if (!current) return current;\n if (!generationInfoUsage) return current;\n if (generationInfoUsage.total_tokens !== current.total_tokens) return current;\n if (generationInfoUsage.output_tokens <= current.output_tokens)\n return current;\n return generationInfoUsage;\n}\n\ntype AdditionalKwargs =\n | undefined\n | (BaseMessage['additional_kwargs'] & {\n signatures?: Array<string | undefined>;\n });\n\n/**\n * Fixes thought signatures on functionCall parts in the formatted Gemini request.\n *\n * `@langchain/google-common` stores signatures as a flat array in\n * `additional_kwargs.signatures` (one per response part) and re-attaches them\n * by index only when `signatures.length === parts.length`. This fails when:\n * - The API omits a signature (length mismatch)\n * - Streaming chunks merge with different part counts\n * - The signature for a functionCall part is an empty string\n *\n * This function correlates each \"model\" content block in the formatted request\n * back to its originating AI message by *position*, then re-attaches non-empty\n * signatures that the library failed to apply. AI messages without signatures\n * still consume their slot — filtering them out shifted later messages onto\n * the wrong content block and dropped real signatures on the floor.\n */\nexport function fixThoughtSignatures(\n contents: GeminiContent[],\n input: BaseMessage[]\n): void {\n // All AI messages, in order — non-signature ones still consume positional\n // slots so later messages line up with their model content blocks.\n const aiMessages = input.filter(isAIMessage);\n const modelContents = contents.filter((c) => c.role === 'model');\n\n const count = Math.min(aiMessages.length, modelContents.length);\n for (let i = 0; i < count; i++) {\n const signatures = (aiMessages[i].additional_kwargs as AdditionalKwargs)\n ?.signatures;\n if (!Array.isArray(signatures) || signatures.length === 0) continue;\n\n const content = modelContents[i];\n const attachedSignatures = new Set(\n content.parts\n .map((p) => p.thoughtSignature)\n .filter((s): s is string => s != null && s !== '')\n );\n const availableSignatures = signatures.filter(\n (s): s is string => s != null && s !== '' && !attachedSignatures.has(s)\n );\n\n let sigIdx = 0;\n for (const part of content.parts) {\n if (\n 'functionCall' in part &&\n (part.thoughtSignature == null || part.thoughtSignature === '') &&\n sigIdx < availableSignatures.length\n ) {\n part.thoughtSignature = availableSignatures[sigIdx];\n sigIdx++;\n }\n }\n }\n}\n\nclass CustomChatConnection extends ChatConnection<VertexAIClientOptions> {\n thinkingConfig?: GoogleThinkingConfig;\n\n async formatData(\n input: BaseMessage[],\n parameters: GoogleAIModelRequestParams\n ): Promise<unknown> {\n const formattedData = (await super.formatData(\n input,\n parameters\n )) as GeminiRequest;\n if (formattedData.generationConfig?.thinkingConfig?.thinkingBudget === -1) {\n // -1 means \"let the model decide\" - delete the property so the API doesn't receive an invalid value\n if (\n formattedData.generationConfig.thinkingConfig.includeThoughts === false\n ) {\n formattedData.generationConfig.thinkingConfig.includeThoughts = true;\n }\n delete formattedData.generationConfig.thinkingConfig.thinkingBudget;\n }\n if (this.thinkingConfig?.thinkingLevel != null) {\n formattedData.generationConfig ??= {};\n // thinkingLevel and thinkingBudget cannot coexist — the API rejects the request.\n // Remove thinkingBudget when thinkingLevel is set.\n const { thinkingBudget: _, ...existingThinkingConfig } =\n (formattedData.generationConfig.thinkingConfig as\n | Record<string, unknown>\n | undefined) ?? {};\n (\n formattedData.generationConfig as Record<string, unknown>\n ).thinkingConfig = {\n ...existingThinkingConfig,\n thinkingLevel: this.thinkingConfig.thinkingLevel,\n ...(this.thinkingConfig.includeThoughts != null && {\n includeThoughts: this.thinkingConfig.includeThoughts,\n }),\n };\n }\n if (formattedData.contents) {\n fixThoughtSignatures(formattedData.contents, input);\n // gemini-3.1+ models reject role=\"function\"; convert to role=\"user\"\n for (const content of formattedData.contents) {\n if (content.role === 'function') {\n (content as { role: string }).role = 'user';\n }\n }\n }\n return formattedData;\n }\n}\n\n/**\n * Integration with Google Vertex AI chat models.\n *\n * Setup:\n * Install `@langchain/google-vertexai` and set your stringified\n * Vertex AI credentials as an environment variable named `GOOGLE_APPLICATION_CREDENTIALS`.\n *\n * ```bash\n * npm install @langchain/google-vertexai\n * export GOOGLE_APPLICATION_CREDENTIALS=\"path/to/credentials\"\n * ```\n *\n * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_google_vertexai.index.ChatVertexAI.html#constructor.new_ChatVertexAI)\n *\n * ## [Runtime args](https://api.js.langchain.com/interfaces/langchain_google_common_types.GoogleAIBaseLanguageModelCallOptions.html)\n *\n * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.\n * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:\n *\n * ```typescript\n * // When calling `.withConfig`, call options should be passed via the first argument\n * const llmWithArgsBound = llm.withConfig({\n * stop: [\"\\n\"],\n * tools: [...],\n * });\n *\n * // When calling `.bindTools`, call options should be passed via the second argument\n * const llmWithTools = llm.bindTools(\n * [...],\n * {\n * tool_choice: \"auto\",\n * }\n * );\n * ```\n *\n * ## Examples\n *\n * <details open>\n * <summary><strong>Instantiate</strong></summary>\n *\n * ```typescript\n * import { ChatVertexAI } from '@langchain/google-vertexai';\n *\n * const llm = new ChatVertexAI({\n * model: \"gemini-1.5-pro\",\n * temperature: 0,\n * // other params...\n * });\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Invoking</strong></summary>\n *\n * ```typescript\n * const input = `Translate \"I love programming\" into French.`;\n *\n * // Models also accept a list of chat messages or a formatted prompt\n * const result = await llm.invoke(input);\n * console.log(result);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\\nHere's why this is the best translation:\\n\\n* **J'adore** means \\\"I love\\\" and conveys a strong passion.\\n* **Programmer** is the French verb for \\\"to program.\\\"\\n\\nThis translation is natural and idiomatic in French. \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 63,\n * \"total_tokens\": 72\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Streaming Chunks</strong></summary>\n *\n * ```typescript\n * for await (const chunk of await llm.stream(input)) {\n * console.log(chunk);\n * }\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {},\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": []\n * }\n * AIMessageChunk {\n * \"content\": \"\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Aggregate Streamed Chunks</strong></summary>\n *\n * ```typescript\n * import { AIMessageChunk } from '@langchain/core/messages';\n * import { concat } from '@langchain/core/utils/stream';\n *\n * const stream = await llm.stream(input);\n * let full: AIMessageChunk | undefined;\n * for await (const chunk of stream) {\n * full = !full ? chunk : concat(full, chunk);\n * }\n * console.log(full);\n * ```\n *\n * ```txt\n * AIMessageChunk {\n * \"content\": \"\\\"J'adore programmer\\\" \\n\",\n * \"additional_kwargs\": {},\n * \"response_metadata\": {\n * \"finishReason\": \"stop\"\n * },\n * \"tool_calls\": [],\n * \"tool_call_chunks\": [],\n * \"invalid_tool_calls\": [],\n * \"usage_metadata\": {\n * \"input_tokens\": 9,\n * \"output_tokens\": 8,\n * \"total_tokens\": 17\n * }\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Bind tools</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const GetWeather = {\n * name: \"GetWeather\",\n * description: \"Get the current weather in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const GetPopulation = {\n * name: \"GetPopulation\",\n * description: \"Get the current population in a given location\",\n * schema: z.object({\n * location: z.string().describe(\"The city and state, e.g. San Francisco, CA\")\n * }),\n * }\n *\n * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);\n * const aiMsg = await llmWithTools.invoke(\n * \"Which city is hotter today and which is bigger: LA or NY?\"\n * );\n * console.log(aiMsg.tool_calls);\n * ```\n *\n * ```txt\n * [\n * {\n * name: 'GetPopulation',\n * args: { location: 'New York City, NY' },\n * id: '33c1c1f47e2f492799c77d2800a43912',\n * type: 'tool_call'\n * }\n * ]\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Structured Output</strong></summary>\n *\n * ```typescript\n * import { z } from 'zod';\n *\n * const Joke = z.object({\n * setup: z.string().describe(\"The setup of the joke\"),\n * punchline: z.string().describe(\"The punchline to the joke\"),\n * rating: z.number().optional().describe(\"How funny the joke is, from 1 to 10\")\n * }).describe('Joke to tell user.');\n *\n * const structuredLlm = llm.withStructuredOutput(Joke, { name: \"Joke\" });\n * const jokeResult = await structuredLlm.invoke(\"Tell me a joke about cats\");\n * console.log(jokeResult);\n * ```\n *\n * ```txt\n * {\n * setup: 'What do you call a cat that loves to bowl?',\n * punchline: 'An alley cat!'\n * }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Usage Metadata</strong></summary>\n *\n * ```typescript\n * const aiMsgForMetadata = await llm.invoke(input);\n * console.log(aiMsgForMetadata.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n *\n * <details>\n * <summary><strong>Stream Usage Metadata</strong></summary>\n *\n * ```typescript\n * const streamForMetadata = await llm.stream(\n * input,\n * {\n * streamUsage: true\n * }\n * );\n * let fullForMetadata: AIMessageChunk | undefined;\n * for await (const chunk of streamForMetadata) {\n * fullForMetadata = !fullForMetadata ? chunk : concat(fullForMetadata, chunk);\n * }\n * console.log(fullForMetadata?.usage_metadata);\n * ```\n *\n * ```txt\n * { input_tokens: 9, output_tokens: 8, total_tokens: 17 }\n * ```\n * </details>\n *\n * <br />\n */\nexport class ChatVertexAI extends ChatGoogle {\n lc_namespace = ['langchain', 'chat_models', 'vertexai'];\n dynamicThinkingBudget = false;\n thinkingConfig?: GoogleThinkingConfig;\n\n static lc_name(): 'LibreChatVertexAI' {\n return 'LibreChatVertexAI';\n }\n\n constructor(model: string, fields?: Omit<VertexAIClientOptions, 'model'>);\n constructor(fields?: VertexAIClientOptions);\n constructor(\n modelOrFields?: string | VertexAIClientOptions,\n params?: Omit<VertexAIClientOptions, 'model'>\n ) {\n const fields =\n typeof modelOrFields === 'string'\n ? { ...(params ?? {}), model: modelOrFields }\n : modelOrFields;\n const dynamicThinkingBudget = fields?.thinkingBudget === -1;\n super({\n ...fields,\n platformType: 'gcp',\n });\n this.dynamicThinkingBudget = dynamicThinkingBudget;\n this.thinkingConfig = fields?.thinkingConfig;\n }\n invocationParams(\n options?: this['ParsedCallOptions'] | undefined\n ): GoogleAIModelRequestParams {\n const params = super.invocationParams(options);\n if (this.dynamicThinkingBudget) {\n params.maxReasoningTokens = -1;\n }\n return params;\n }\n async *_streamResponseChunks(\n messages: BaseMessage[],\n options: this['ParsedCallOptions'],\n runManager?: CallbackManagerForLLMRun\n ): AsyncGenerator<ChatGenerationChunk> {\n let lastGoodUsage: UsageMetadata | undefined;\n for await (const chunk of super._streamResponseChunks(\n messages,\n options,\n runManager\n )) {\n const genUsage = (\n chunk.generationInfo as { usage_metadata?: UsageMetadata } | undefined\n )?.usage_metadata;\n if (genUsage) {\n lastGoodUsage = genUsage;\n }\n if (chunk.message instanceof AIMessageChunk) {\n const repaired = repairStreamUsageMetadata(\n chunk.message.usage_metadata,\n lastGoodUsage\n );\n if (repaired !== chunk.message.usage_metadata) {\n chunk.message.usage_metadata = repaired;\n }\n }\n yield chunk;\n }\n }\n buildConnection(\n fields: VertexAIClientOptions | undefined,\n client: GoogleAbstractedClient\n ): void {\n // Note: buildConnection is called from super() BEFORE this.thinkingConfig is set,\n // so we must read thinkingConfig from `fields` directly.\n const thinkingConfig = fields?.thinkingConfig ?? this.thinkingConfig;\n\n const connection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n false\n );\n connection.thinkingConfig = thinkingConfig;\n this.connection = connection;\n\n const streamedConnection = new CustomChatConnection(\n { ...fields, ...this },\n this.caller,\n client,\n true\n );\n streamedConnection.thinkingConfig = thinkingConfig;\n this.streamedConnection = streamedConnection;\n }\n}\n"],"names":[],"mappings":";;;;AAcA;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,SAAU,yBAAyB,CACvC,OAAkC,EAClC,mBAA8C,EAAA;AAE9C,IAAA,IAAI,CAAC,OAAO;AAAE,QAAA,OAAO,OAAO;AAC5B,IAAA,IAAI,CAAC,mBAAmB;AAAE,QAAA,OAAO,OAAO;AACxC,IAAA,IAAI,mBAAmB,CAAC,YAAY,KAAK,OAAO,CAAC,YAAY;AAAE,QAAA,OAAO,OAAO;AAC7E,IAAA,IAAI,mBAAmB,CAAC,aAAa,IAAI,OAAO,CAAC,aAAa;AAC5D,QAAA,OAAO,OAAO;AAChB,IAAA,OAAO,mBAAmB;AAC5B;AAQA;;;;;;;;;;;;;;;AAeG;AACG,SAAU,oBAAoB,CAClC,QAAyB,EACzB,KAAoB,EAAA;;;IAIpB,MAAM,UAAU,GAAG,KAAK,CAAC,MAAM,CAAC,WAAW,CAAC;AAC5C,IAAA,MAAM,aAAa,GAAG,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,IAAI,KAAK,OAAO,CAAC;AAEhE,IAAA,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,EAAE,aAAa,CAAC,MAAM,CAAC;AAC/D,IAAA,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,KAAK,EAAE,CAAC,EAAE,EAAE;AAC9B,QAAA,MAAM,UAA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YAAA,MAAM,QAAQ,GACZ,KAAK,CAAC,cACP,EAAE,cAAc;YACjB,IAAI,QAAQ,EAAE;gBACZ,aAAa,GAAG,QAAQ;YAC1B;AACA,YAAA,IAAI,KAAK,CAAC,OAAO,YAAY,cAAc,EAAE;AAC3C,gBAAA,MAAM,QAAQ,GAAG,yBAAyB,CACxC,KAAK,CAAC,OAAO,CAAC,cAAc,EAC5B,aAAa,CACd;gBACD,IAAI,QAAQ,KAAK,KAAK,CAAC,OAAO,CAAC,cAAc,EAAE;AAC7C,oBAAA,KAAK,CAAC,OAAO,CAAC,cAAc,GAAG,QAAQ;gBACzC;YACF;AACA,YAAA,MAAM,KAAK;QACb;IACF;IACA,eAAe,CACb,MAAyC,EACzC,MAA8B,EAAA;;;QAI9B,MAAM,cAAc,GAAG,MAAM,EAAE,cAAc,IAAI,IAAI,CAAC,cAAc;QAEpE,MAAM,UAAU,GAAG,IAAI,oBAAoB,CACzC,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,KAAK,CACN;AACD,QAAA,UAAU,CAAC,cAAc,GAAG,cAAc;AAC1C,QAAA,IAAI,CAAC,UAAU,GAAG,UAAU;QAE5B,MAAM,kBAAkB,GAAG,IAAI,oBAAoB,CACjD,EAAE,GAAG,MAAM,EAAE,GAAG,IAAI,EAAE,EACtB,IAAI,CAAC,MAAM,EACX,MAAM,EACN,IAAI,CACL;AACD,QAAA,kBAAkB,CAAC,cAAc,GAAG,cAAc;AAClD,QAAA,IAAI,CAAC,kBAAkB,GAAG,kBAAkB;IAC9C;AACD;;;;"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ChatGoogle } from '@langchain/google-gauth';
|
|
2
|
-
import type { GoogleAIModelRequestParams, GoogleAbstractedClient } from '@langchain/google-common';
|
|
2
|
+
import type { GeminiContent, GoogleAIModelRequestParams, GoogleAbstractedClient } from '@langchain/google-common';
|
|
3
3
|
import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
|
|
4
4
|
import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
|
|
5
5
|
import type { ChatGenerationChunk } from '@langchain/core/outputs';
|
|
@@ -29,6 +29,23 @@ import type { GoogleThinkingConfig, VertexAIClientOptions } from '@/types';
|
|
|
29
29
|
* overriding `_convertToUsageMetadata`.
|
|
30
30
|
*/
|
|
31
31
|
export declare function repairStreamUsageMetadata(current: UsageMetadata | undefined, generationInfoUsage: UsageMetadata | undefined): UsageMetadata | undefined;
|
|
32
|
+
/**
|
|
33
|
+
* Fixes thought signatures on functionCall parts in the formatted Gemini request.
|
|
34
|
+
*
|
|
35
|
+
* `@langchain/google-common` stores signatures as a flat array in
|
|
36
|
+
* `additional_kwargs.signatures` (one per response part) and re-attaches them
|
|
37
|
+
* by index only when `signatures.length === parts.length`. This fails when:
|
|
38
|
+
* - The API omits a signature (length mismatch)
|
|
39
|
+
* - Streaming chunks merge with different part counts
|
|
40
|
+
* - The signature for a functionCall part is an empty string
|
|
41
|
+
*
|
|
42
|
+
* This function correlates each "model" content block in the formatted request
|
|
43
|
+
* back to its originating AI message by *position*, then re-attaches non-empty
|
|
44
|
+
* signatures that the library failed to apply. AI messages without signatures
|
|
45
|
+
* still consume their slot — filtering them out shifted later messages onto
|
|
46
|
+
* the wrong content block and dropped real signatures on the floor.
|
|
47
|
+
*/
|
|
48
|
+
export declare function fixThoughtSignatures(contents: GeminiContent[], input: BaseMessage[]): void;
|
|
32
49
|
/**
|
|
33
50
|
* Integration with Google Vertex AI chat models.
|
|
34
51
|
*
|
package/package.json
CHANGED
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { expect, test, describe } from '@jest/globals';
|
|
2
|
+
import type { GeminiContent } from '@langchain/google-common';
|
|
3
|
+
import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages';
|
|
4
|
+
import { fixThoughtSignatures } from './index';
|
|
5
|
+
|
|
6
|
+
const SIG_A = 'AY89a1/sigA==';
|
|
7
|
+
const SIG_B = 'AY89a1/sigB==';
|
|
8
|
+
|
|
9
|
+
const buildContents = (
|
|
10
|
+
blocks: Array<['user' | 'model' | 'function', GeminiContent['parts']]>
|
|
11
|
+
): GeminiContent[] =>
|
|
12
|
+
blocks.map(([role, parts]) => ({ role, parts }) as GeminiContent);
|
|
13
|
+
|
|
14
|
+
describe('fixThoughtSignatures', () => {
|
|
15
|
+
test('attaches signature to functionCall part when prior turn is a plain-text AI message (issue LibreChat#13006-followup)', () => {
|
|
16
|
+
// Reproduces the live failure from the issue: a Gemini 3 conversation
|
|
17
|
+
// where turn 1 was plain text ("Hello!") and turn 2 emitted a tool call
|
|
18
|
+
// with a thought signature. The plain-text AI message has no signatures,
|
|
19
|
+
// so the old position-by-filter code matched the toolcall AIMessage with
|
|
20
|
+
// the WRONG model content.
|
|
21
|
+
const helloAi = new AIMessage('Hello! How can I help you today?');
|
|
22
|
+
const toolcallAi = new AIMessage({
|
|
23
|
+
content: '',
|
|
24
|
+
tool_calls: [
|
|
25
|
+
{ name: 'bash_tool', args: { command: 'echo hi' }, id: 'tc1' },
|
|
26
|
+
],
|
|
27
|
+
additional_kwargs: { signatures: [SIG_A, ''] },
|
|
28
|
+
});
|
|
29
|
+
const input = [
|
|
30
|
+
new HumanMessage('hi there'),
|
|
31
|
+
helloAi,
|
|
32
|
+
new HumanMessage('run something'),
|
|
33
|
+
toolcallAi,
|
|
34
|
+
new ToolMessage({ content: 'ok', tool_call_id: 'tc1' }),
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const contents = buildContents([
|
|
38
|
+
['user', [{ text: 'hi there' }]],
|
|
39
|
+
['model', [{ text: 'Hello! How can I help you today?' }]],
|
|
40
|
+
['user', [{ text: 'run something' }]],
|
|
41
|
+
[
|
|
42
|
+
'model',
|
|
43
|
+
[{ functionCall: { name: 'bash_tool', args: { command: 'echo hi' } } }],
|
|
44
|
+
],
|
|
45
|
+
[
|
|
46
|
+
'user',
|
|
47
|
+
[
|
|
48
|
+
{
|
|
49
|
+
functionResponse: {
|
|
50
|
+
name: 'bash_tool',
|
|
51
|
+
response: { content: 'ok' },
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
],
|
|
56
|
+
]);
|
|
57
|
+
|
|
58
|
+
fixThoughtSignatures(contents, input);
|
|
59
|
+
|
|
60
|
+
expect(contents[1].parts[0].thoughtSignature).toBeUndefined();
|
|
61
|
+
expect(contents[3].parts[0]).toMatchObject({
|
|
62
|
+
functionCall: { name: 'bash_tool' },
|
|
63
|
+
thoughtSignature: SIG_A,
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
test('attaches signatures across multiple tool-call turns by position', () => {
|
|
68
|
+
const turn1 = new AIMessage({
|
|
69
|
+
content: '',
|
|
70
|
+
tool_calls: [{ name: 'a', args: {}, id: 't1' }],
|
|
71
|
+
additional_kwargs: { signatures: [SIG_A, ''] },
|
|
72
|
+
});
|
|
73
|
+
const turn2 = new AIMessage({
|
|
74
|
+
content: '',
|
|
75
|
+
tool_calls: [{ name: 'b', args: {}, id: 't2' }],
|
|
76
|
+
additional_kwargs: { signatures: [SIG_B, ''] },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const input = [
|
|
80
|
+
new HumanMessage('q1'),
|
|
81
|
+
turn1,
|
|
82
|
+
new ToolMessage({ content: '1', tool_call_id: 't1' }),
|
|
83
|
+
new HumanMessage('q2'),
|
|
84
|
+
turn2,
|
|
85
|
+
new ToolMessage({ content: '2', tool_call_id: 't2' }),
|
|
86
|
+
];
|
|
87
|
+
const contents = buildContents([
|
|
88
|
+
['user', [{ text: 'q1' }]],
|
|
89
|
+
['model', [{ functionCall: { name: 'a', args: {} } }]],
|
|
90
|
+
['user', [{ functionResponse: { name: 'a', response: {} } }]],
|
|
91
|
+
['user', [{ text: 'q2' }]],
|
|
92
|
+
['model', [{ functionCall: { name: 'b', args: {} } }]],
|
|
93
|
+
['user', [{ functionResponse: { name: 'b', response: {} } }]],
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
fixThoughtSignatures(contents, input);
|
|
97
|
+
|
|
98
|
+
expect(contents[1].parts[0].thoughtSignature).toBe(SIG_A);
|
|
99
|
+
expect(contents[4].parts[0].thoughtSignature).toBe(SIG_B);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test('does not overwrite signatures already attached by the library', () => {
|
|
103
|
+
const ai = new AIMessage({
|
|
104
|
+
content: '',
|
|
105
|
+
tool_calls: [{ name: 'a', args: {}, id: 't1' }],
|
|
106
|
+
additional_kwargs: { signatures: [SIG_A] },
|
|
107
|
+
});
|
|
108
|
+
const input = [new HumanMessage('q'), ai];
|
|
109
|
+
const contents = buildContents([
|
|
110
|
+
['user', [{ text: 'q' }]],
|
|
111
|
+
[
|
|
112
|
+
'model',
|
|
113
|
+
[{ functionCall: { name: 'a', args: {} }, thoughtSignature: SIG_B }],
|
|
114
|
+
],
|
|
115
|
+
]);
|
|
116
|
+
|
|
117
|
+
fixThoughtSignatures(contents, input);
|
|
118
|
+
|
|
119
|
+
expect(contents[1].parts[0].thoughtSignature).toBe(SIG_B);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('no-op when AI message has no signatures', () => {
|
|
123
|
+
const ai = new AIMessage({
|
|
124
|
+
content: '',
|
|
125
|
+
tool_calls: [{ name: 'a', args: {}, id: 't1' }],
|
|
126
|
+
});
|
|
127
|
+
const input = [new HumanMessage('q'), ai];
|
|
128
|
+
const contents = buildContents([
|
|
129
|
+
['user', [{ text: 'q' }]],
|
|
130
|
+
['model', [{ functionCall: { name: 'a', args: {} } }]],
|
|
131
|
+
]);
|
|
132
|
+
|
|
133
|
+
fixThoughtSignatures(contents, input);
|
|
134
|
+
|
|
135
|
+
expect(contents[1].parts[0].thoughtSignature).toBeUndefined();
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
test('skips empty-string signatures', () => {
|
|
139
|
+
const ai = new AIMessage({
|
|
140
|
+
content: '',
|
|
141
|
+
tool_calls: [{ name: 'a', args: {}, id: 't1' }],
|
|
142
|
+
additional_kwargs: { signatures: ['', '', ''] },
|
|
143
|
+
});
|
|
144
|
+
const input = [new HumanMessage('q'), ai];
|
|
145
|
+
const contents = buildContents([
|
|
146
|
+
['user', [{ text: 'q' }]],
|
|
147
|
+
['model', [{ functionCall: { name: 'a', args: {} } }]],
|
|
148
|
+
]);
|
|
149
|
+
|
|
150
|
+
fixThoughtSignatures(contents, input);
|
|
151
|
+
|
|
152
|
+
expect(contents[1].parts[0].thoughtSignature).toBeUndefined();
|
|
153
|
+
});
|
|
154
|
+
});
|
|
@@ -65,50 +65,44 @@ type AdditionalKwargs =
|
|
|
65
65
|
* - The signature for a functionCall part is an empty string
|
|
66
66
|
*
|
|
67
67
|
* This function correlates each "model" content block in the formatted request
|
|
68
|
-
* back to its originating AI message
|
|
69
|
-
* that the library failed to apply.
|
|
68
|
+
* back to its originating AI message by *position*, then re-attaches non-empty
|
|
69
|
+
* signatures that the library failed to apply. AI messages without signatures
|
|
70
|
+
* still consume their slot — filtering them out shifted later messages onto
|
|
71
|
+
* the wrong content block and dropped real signatures on the floor.
|
|
70
72
|
*/
|
|
71
|
-
function fixThoughtSignatures(
|
|
73
|
+
export function fixThoughtSignatures(
|
|
72
74
|
contents: GeminiContent[],
|
|
73
75
|
input: BaseMessage[]
|
|
74
76
|
): void {
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
isAIMessage(msg) &&
|
|
79
|
-
Array.isArray((msg.additional_kwargs as AdditionalKwargs)?.signatures) &&
|
|
80
|
-
(msg.additional_kwargs.signatures as string[]).length > 0
|
|
81
|
-
);
|
|
82
|
-
|
|
83
|
-
// Collect "model" content blocks from the formatted request, in order
|
|
77
|
+
// All AI messages, in order — non-signature ones still consume positional
|
|
78
|
+
// slots so later messages line up with their model content blocks.
|
|
79
|
+
const aiMessages = input.filter(isAIMessage);
|
|
84
80
|
const modelContents = contents.filter((c) => c.role === 'model');
|
|
85
81
|
|
|
86
|
-
// They should correspond 1:1 in order (both derived from the same input sequence)
|
|
87
82
|
const count = Math.min(aiMessages.length, modelContents.length);
|
|
88
83
|
for (let i = 0; i < count; i++) {
|
|
89
|
-
const
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
const signatures = (aiMessages[i].additional_kwargs as AdditionalKwargs)
|
|
85
|
+
?.signatures;
|
|
86
|
+
if (!Array.isArray(signatures) || signatures.length === 0) continue;
|
|
92
87
|
|
|
93
|
-
|
|
88
|
+
const content = modelContents[i];
|
|
94
89
|
const attachedSignatures = new Set(
|
|
95
90
|
content.parts
|
|
96
91
|
.map((p) => p.thoughtSignature)
|
|
97
92
|
.filter((s): s is string => s != null && s !== '')
|
|
98
93
|
);
|
|
99
|
-
const availableSignatures = signatures
|
|
100
|
-
(s) => s != null && s !== '' && !attachedSignatures.has(s)
|
|
94
|
+
const availableSignatures = signatures.filter(
|
|
95
|
+
(s): s is string => s != null && s !== '' && !attachedSignatures.has(s)
|
|
101
96
|
);
|
|
102
97
|
|
|
103
|
-
// Assign available signatures to functionCall parts missing one, in order
|
|
104
98
|
let sigIdx = 0;
|
|
105
99
|
for (const part of content.parts) {
|
|
106
100
|
if (
|
|
107
101
|
'functionCall' in part &&
|
|
108
102
|
(part.thoughtSignature == null || part.thoughtSignature === '') &&
|
|
109
|
-
sigIdx <
|
|
103
|
+
sigIdx < availableSignatures.length
|
|
110
104
|
) {
|
|
111
|
-
part.thoughtSignature = availableSignatures
|
|
105
|
+
part.thoughtSignature = availableSignatures[sigIdx];
|
|
112
106
|
sigIdx++;
|
|
113
107
|
}
|
|
114
108
|
}
|