@librechat/agents 3.0.24 → 3.0.26

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,7 +1,16 @@
1
1
  import { POSSIBLE_ROLES, type Part, type Content, type EnhancedGenerateContentResponse, type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool } from '@google/generative-ai';
2
2
  import { BaseMessage, UsageMetadata } from '@langchain/core/messages';
3
3
  import { ChatGenerationChunk } from '@langchain/core/outputs';
4
+ import type { ChatResult } from '@langchain/core/outputs';
4
5
  import { GoogleGenerativeAIToolType } from '../types';
6
+ export declare const _FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY = "__gemini_function_call_thought_signatures__";
7
+ /**
8
+ * Executes a function immediately and returns its result.
9
+ * Functional utility similar to an Immediately Invoked Function Expression (IIFE).
10
+ * @param fn The function to execute.
11
+ * @returns The result of invoking fn.
12
+ */
13
+ export declare const iife: <T>(fn: () => T) => T;
5
14
  export declare function getMessageAuthor(message: BaseMessage): string;
6
15
  /**
7
16
  * Maps a message type to a Google Generative AI chat author.
@@ -10,10 +19,16 @@ export declare function getMessageAuthor(message: BaseMessage): string;
10
19
  * @returns The message type mapped to a Google Generative AI chat author.
11
20
  */
12
21
  export declare function convertAuthorToRole(author: string): (typeof POSSIBLE_ROLES)[number];
13
- export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean, previousMessages: BaseMessage[]): Part[];
14
- export declare function convertBaseMessagesToContent(messages: BaseMessage[], isMultimodalModel: boolean, convertSystemMessageToHumanContent?: boolean): Content[] | undefined;
22
+ export declare function convertMessageContentToParts(message: BaseMessage, isMultimodalModel: boolean, previousMessages: BaseMessage[], model?: string): Part[];
23
+ export declare function convertBaseMessagesToContent(messages: BaseMessage[], isMultimodalModel: boolean, convertSystemMessageToHumanContent?: boolean, model?: string): Content[] | undefined;
15
24
  export declare function convertResponseContentToChatGenerationChunk(response: EnhancedGenerateContentResponse, extra: {
16
25
  usageMetadata?: UsageMetadata | undefined;
17
26
  index: number;
18
27
  }): ChatGenerationChunk | null;
28
+ /**
29
+ * Maps a Google GenerateContentResult to a LangChain ChatResult
30
+ */
31
+ export declare function mapGenerateContentResultToChatResult(response: EnhancedGenerateContentResponse, extra?: {
32
+ usageMetadata: UsageMetadata | undefined;
33
+ }): ChatResult;
19
34
  export declare function convertToGenerativeAITools(tools: GoogleGenerativeAIToolType[]): GoogleGenerativeAIFunctionDeclarationsTool[];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "3.0.24",
3
+ "version": "3.0.26",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
Binary file
@@ -11,15 +11,30 @@ import type {
11
11
  import type { CallbackManagerForLLMRun } from '@langchain/core/callbacks/manager';
12
12
  import type { BaseMessage, UsageMetadata } from '@langchain/core/messages';
13
13
  import type { GeminiGenerationConfig } from '@langchain/google-common';
14
- import type { GeminiApiUsageMetadata } from './types';
14
+ import type { GeminiApiUsageMetadata, InputTokenDetails } from './types';
15
15
  import type { GoogleClientOptions } from '@/types';
16
16
  import {
17
17
  convertResponseContentToChatGenerationChunk,
18
18
  convertBaseMessagesToContent,
19
+ mapGenerateContentResultToChatResult,
19
20
  } from './utils/common';
20
21
 
21
22
  export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
22
23
  thinkingConfig?: GeminiGenerationConfig['thinkingConfig'];
24
+
25
+ /**
26
+ * Override to add gemini-3 model support for multimodal and function calling thought signatures
27
+ */
28
+ get _isMultimodalModel(): boolean {
29
+ return (
30
+ this.model.startsWith('gemini-1.5') ||
31
+ this.model.startsWith('gemini-2') ||
32
+ (this.model.startsWith('gemma-3-') &&
33
+ !this.model.startsWith('gemma-3-1b')) ||
34
+ this.model.startsWith('gemini-3')
35
+ );
36
+ }
37
+
23
38
  constructor(fields: GoogleClientOptions) {
24
39
  super(fields);
25
40
 
@@ -111,6 +126,59 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
111
126
  return 'LibreChatGoogleGenerativeAI';
112
127
  }
113
128
 
129
+ /**
130
+ * Helper function to convert Gemini API usage metadata to LangChain format
131
+ * Includes support for cached tokens and tier-based tracking for gemini-3-pro-preview
132
+ */
133
+ private _convertToUsageMetadata(
134
+ usageMetadata: GeminiApiUsageMetadata | undefined,
135
+ model: string
136
+ ): UsageMetadata | undefined {
137
+ if (!usageMetadata) {
138
+ return undefined;
139
+ }
140
+
141
+ const output: UsageMetadata = {
142
+ input_tokens: usageMetadata.promptTokenCount ?? 0,
143
+ output_tokens:
144
+ (usageMetadata.candidatesTokenCount ?? 0) +
145
+ (usageMetadata.thoughtsTokenCount ?? 0),
146
+ total_tokens: usageMetadata.totalTokenCount ?? 0,
147
+ };
148
+
149
+ if (usageMetadata.cachedContentTokenCount) {
150
+ output.input_token_details ??= {};
151
+ output.input_token_details.cache_read =
152
+ usageMetadata.cachedContentTokenCount;
153
+ }
154
+
155
+ // gemini-3-pro-preview has bracket based tracking of tokens per request
156
+ if (model === 'gemini-3-pro-preview') {
157
+ const over200k = Math.max(
158
+ 0,
159
+ (usageMetadata.promptTokenCount ?? 0) - 200000
160
+ );
161
+ const cachedOver200k = Math.max(
162
+ 0,
163
+ (usageMetadata.cachedContentTokenCount ?? 0) - 200000
164
+ );
165
+ if (over200k) {
166
+ output.input_token_details = {
167
+ ...output.input_token_details,
168
+ over_200k: over200k,
169
+ } as InputTokenDetails;
170
+ }
171
+ if (cachedOver200k) {
172
+ output.input_token_details = {
173
+ ...output.input_token_details,
174
+ cache_read_over_200k: cachedOver200k,
175
+ } as InputTokenDetails;
176
+ }
177
+ }
178
+
179
+ return output;
180
+ }
181
+
114
182
  invocationParams(
115
183
  options?: this['ParsedCallOptions']
116
184
  ): Omit<GenerateContentRequest, 'contents'> {
@@ -127,6 +195,60 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
127
195
  return params;
128
196
  }
129
197
 
198
+ async _generate(
199
+ messages: BaseMessage[],
200
+ options: this['ParsedCallOptions'],
201
+ runManager?: CallbackManagerForLLMRun
202
+ ): Promise<import('@langchain/core/outputs').ChatResult> {
203
+ const prompt = convertBaseMessagesToContent(
204
+ messages,
205
+ this._isMultimodalModel,
206
+ this.useSystemInstruction,
207
+ this.model
208
+ );
209
+ let actualPrompt = prompt;
210
+ if (prompt?.[0].role === 'system') {
211
+ const [systemInstruction] = prompt;
212
+ /** @ts-ignore */
213
+ this.client.systemInstruction = systemInstruction;
214
+ actualPrompt = prompt.slice(1);
215
+ }
216
+ const parameters = this.invocationParams(options);
217
+ const request = {
218
+ ...parameters,
219
+ contents: actualPrompt,
220
+ };
221
+
222
+ const res = await this.caller.callWithOptions(
223
+ { signal: options.signal },
224
+ async () =>
225
+ /** @ts-ignore */
226
+ this.client.generateContent(request)
227
+ );
228
+
229
+ const response = res.response;
230
+ const usageMetadata = this._convertToUsageMetadata(
231
+ /** @ts-ignore */
232
+ response.usageMetadata,
233
+ this.model
234
+ );
235
+
236
+ /** @ts-ignore */
237
+ const generationResult = mapGenerateContentResultToChatResult(response, {
238
+ usageMetadata,
239
+ });
240
+
241
+ await runManager?.handleLLMNewToken(
242
+ generationResult.generations[0].text || '',
243
+ undefined,
244
+ undefined,
245
+ undefined,
246
+ undefined,
247
+ undefined
248
+ );
249
+ return generationResult;
250
+ }
251
+
130
252
  async *_streamResponseChunks(
131
253
  messages: BaseMessage[],
132
254
  options: this['ParsedCallOptions'],
@@ -135,7 +257,8 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
135
257
  const prompt = convertBaseMessagesToContent(
136
258
  messages,
137
259
  this._isMultimodalModel,
138
- this.useSystemInstruction
260
+ this.useSystemInstruction,
261
+ this.model
139
262
  );
140
263
  let actualPrompt = prompt;
141
264
  if (prompt?.[0].role === 'system') {
@@ -166,18 +289,10 @@ export class CustomChatGoogleGenerativeAI extends ChatGoogleGenerativeAI {
166
289
  this.streamUsage !== false &&
167
290
  options.streamUsage !== false
168
291
  ) {
169
- const genAIUsageMetadata = response.usageMetadata as
170
- | GeminiApiUsageMetadata
171
- | undefined;
172
-
173
- const output_tokens =
174
- (genAIUsageMetadata?.candidatesTokenCount ?? 0) +
175
- (genAIUsageMetadata?.thoughtsTokenCount ?? 0);
176
- lastUsageMetadata = {
177
- input_tokens: genAIUsageMetadata?.promptTokenCount ?? 0,
178
- output_tokens,
179
- total_tokens: genAIUsageMetadata?.totalTokenCount ?? 0,
180
- };
292
+ lastUsageMetadata = this._convertToUsageMetadata(
293
+ response.usageMetadata as GeminiApiUsageMetadata | undefined,
294
+ this.model
295
+ );
181
296
  }
182
297
 
183
298
  const chunk = convertResponseContentToChatGenerationChunk(response, {