@librechat/agents 2.4.42 → 2.4.43

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.
Files changed (40) hide show
  1. package/dist/cjs/common/enum.cjs +4 -2
  2. package/dist/cjs/common/enum.cjs.map +1 -1
  3. package/dist/cjs/graphs/Graph.cjs +2 -2
  4. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  5. package/dist/cjs/llm/google/index.cjs +73 -1
  6. package/dist/cjs/llm/google/index.cjs.map +1 -1
  7. package/dist/cjs/llm/google/utils/common.cjs +469 -0
  8. package/dist/cjs/llm/google/utils/common.cjs.map +1 -0
  9. package/dist/cjs/stream.cjs +5 -2
  10. package/dist/cjs/stream.cjs.map +1 -1
  11. package/dist/esm/common/enum.mjs +4 -2
  12. package/dist/esm/common/enum.mjs.map +1 -1
  13. package/dist/esm/graphs/Graph.mjs +2 -2
  14. package/dist/esm/graphs/Graph.mjs.map +1 -1
  15. package/dist/esm/llm/google/index.mjs +73 -1
  16. package/dist/esm/llm/google/index.mjs.map +1 -1
  17. package/dist/esm/llm/google/utils/common.mjs +463 -0
  18. package/dist/esm/llm/google/utils/common.mjs.map +1 -0
  19. package/dist/esm/stream.mjs +5 -2
  20. package/dist/esm/stream.mjs.map +1 -1
  21. package/dist/types/common/enum.d.ts +5 -3
  22. package/dist/types/llm/google/index.d.ts +10 -5
  23. package/dist/types/llm/google/types.d.ts +32 -0
  24. package/dist/types/llm/google/utils/common.d.ts +19 -0
  25. package/dist/types/llm/google/utils/tools.d.ts +10 -0
  26. package/dist/types/llm/google/utils/zod_to_genai_parameters.d.ts +14 -0
  27. package/dist/types/types/llm.d.ts +2 -0
  28. package/dist/types/types/stream.d.ts +5 -0
  29. package/package.json +1 -1
  30. package/src/common/enum.ts +4 -2
  31. package/src/graphs/Graph.ts +10 -6
  32. package/src/llm/google/index.ts +118 -8
  33. package/src/llm/google/types.ts +43 -0
  34. package/src/llm/google/utils/common.ts +632 -0
  35. package/src/llm/google/utils/tools.ts +160 -0
  36. package/src/llm/google/utils/zod_to_genai_parameters.ts +88 -0
  37. package/src/stream.ts +5 -2
  38. package/src/types/llm.ts +2 -0
  39. package/src/types/stream.ts +6 -0
  40. package/src/utils/llmConfig.ts +2 -2
@@ -0,0 +1,632 @@
1
+ import {
2
+ EnhancedGenerateContentResponse,
3
+ Content,
4
+ Part,
5
+ type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
6
+ type FunctionDeclaration as GenerativeAIFunctionDeclaration,
7
+ POSSIBLE_ROLES,
8
+ FunctionCallPart,
9
+ TextPart,
10
+ FileDataPart,
11
+ InlineDataPart,
12
+ } from '@google/generative-ai';
13
+ import {
14
+ AIMessageChunk,
15
+ BaseMessage,
16
+ ChatMessage,
17
+ ToolMessage,
18
+ ToolMessageChunk,
19
+ MessageContent,
20
+ MessageContentComplex,
21
+ UsageMetadata,
22
+ isAIMessage,
23
+ isBaseMessage,
24
+ isToolMessage,
25
+ StandardContentBlockConverter,
26
+ parseBase64DataUrl,
27
+ convertToProviderContentBlock,
28
+ isDataContentBlock,
29
+ } from '@langchain/core/messages';
30
+ import { ChatGenerationChunk } from '@langchain/core/outputs';
31
+ import type { ChatGeneration } from '@langchain/core/outputs';
32
+ import { isLangChainTool } from '@langchain/core/utils/function_calling';
33
+ import { isOpenAITool } from '@langchain/core/language_models/base';
34
+ import { ToolCallChunk } from '@langchain/core/messages/tool';
35
+ import { v4 as uuidv4 } from 'uuid';
36
+ import {
37
+ jsonSchemaToGeminiParameters,
38
+ schemaToGenerativeAIParameters,
39
+ } from './zod_to_genai_parameters';
40
+ import { GoogleGenerativeAIToolType } from '../types';
41
+
42
+ export function getMessageAuthor(message: BaseMessage): string {
43
+ const type = message._getType();
44
+ if (ChatMessage.isInstance(message)) {
45
+ return message.role;
46
+ }
47
+ if (type === 'tool') {
48
+ return type;
49
+ }
50
+ return message.name ?? type;
51
+ }
52
+
53
+ /**
54
+ * Maps a message type to a Google Generative AI chat author.
55
+ * @param message The message to map.
56
+ * @param model The model to use for mapping.
57
+ * @returns The message type mapped to a Google Generative AI chat author.
58
+ */
59
+ export function convertAuthorToRole(
60
+ author: string
61
+ ): (typeof POSSIBLE_ROLES)[number] {
62
+ switch (author) {
63
+ /**
64
+ * Note: Gemini currently is not supporting system messages
65
+ * we will convert them to human messages and merge with following
66
+ * */
67
+ case 'supervisor':
68
+ case 'ai':
69
+ case 'model': // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
70
+ return 'model';
71
+ case 'system':
72
+ return 'system';
73
+ case 'human':
74
+ return 'user';
75
+ case 'tool':
76
+ case 'function':
77
+ return 'function';
78
+ default:
79
+ throw new Error(`Unknown / unsupported author: ${author}`);
80
+ }
81
+ }
82
+
83
+ function messageContentMedia(content: MessageContentComplex): Part {
84
+ if ('mimeType' in content && 'data' in content) {
85
+ return {
86
+ inlineData: {
87
+ mimeType: content.mimeType,
88
+ data: content.data,
89
+ },
90
+ };
91
+ }
92
+ if ('mimeType' in content && 'fileUri' in content) {
93
+ return {
94
+ fileData: {
95
+ mimeType: content.mimeType,
96
+ fileUri: content.fileUri,
97
+ },
98
+ };
99
+ }
100
+
101
+ throw new Error('Invalid media content');
102
+ }
103
+
104
+ function inferToolNameFromPreviousMessages(
105
+ message: ToolMessage | ToolMessageChunk,
106
+ previousMessages: BaseMessage[]
107
+ ): string | undefined {
108
+ return previousMessages
109
+ .map((msg) => {
110
+ if (isAIMessage(msg)) {
111
+ return msg.tool_calls ?? [];
112
+ }
113
+ return [];
114
+ })
115
+ .flat()
116
+ .find((toolCall) => {
117
+ return toolCall.id === message.tool_call_id;
118
+ })?.name;
119
+ }
120
+
121
+ function _getStandardContentBlockConverter(
122
+ isMultimodalModel: boolean
123
+ ): StandardContentBlockConverter<{
124
+ text: TextPart;
125
+ image: FileDataPart | InlineDataPart;
126
+ audio: FileDataPart | InlineDataPart;
127
+ file: FileDataPart | InlineDataPart | TextPart;
128
+ }> {
129
+ const standardContentBlockConverter: StandardContentBlockConverter<{
130
+ text: TextPart;
131
+ image: FileDataPart | InlineDataPart;
132
+ audio: FileDataPart | InlineDataPart;
133
+ file: FileDataPart | InlineDataPart | TextPart;
134
+ }> = {
135
+ providerName: 'Google Gemini',
136
+
137
+ fromStandardTextBlock(block) {
138
+ return {
139
+ text: block.text,
140
+ };
141
+ },
142
+
143
+ fromStandardImageBlock(block): FileDataPart | InlineDataPart {
144
+ if (!isMultimodalModel) {
145
+ throw new Error('This model does not support images');
146
+ }
147
+ if (block.source_type === 'url') {
148
+ const data = parseBase64DataUrl({ dataUrl: block.url });
149
+ if (data) {
150
+ return {
151
+ inlineData: {
152
+ mimeType: data.mime_type,
153
+ data: data.data,
154
+ },
155
+ };
156
+ } else {
157
+ return {
158
+ fileData: {
159
+ mimeType: block.mime_type ?? '',
160
+ fileUri: block.url,
161
+ },
162
+ };
163
+ }
164
+ }
165
+
166
+ if (block.source_type === 'base64') {
167
+ return {
168
+ inlineData: {
169
+ mimeType: block.mime_type ?? '',
170
+ data: block.data,
171
+ },
172
+ };
173
+ }
174
+
175
+ throw new Error(`Unsupported source type: ${block.source_type}`);
176
+ },
177
+
178
+ fromStandardAudioBlock(block): FileDataPart | InlineDataPart {
179
+ if (!isMultimodalModel) {
180
+ throw new Error('This model does not support audio');
181
+ }
182
+ if (block.source_type === 'url') {
183
+ const data = parseBase64DataUrl({ dataUrl: block.url });
184
+ if (data) {
185
+ return {
186
+ inlineData: {
187
+ mimeType: data.mime_type,
188
+ data: data.data,
189
+ },
190
+ };
191
+ } else {
192
+ return {
193
+ fileData: {
194
+ mimeType: block.mime_type ?? '',
195
+ fileUri: block.url,
196
+ },
197
+ };
198
+ }
199
+ }
200
+
201
+ if (block.source_type === 'base64') {
202
+ return {
203
+ inlineData: {
204
+ mimeType: block.mime_type ?? '',
205
+ data: block.data,
206
+ },
207
+ };
208
+ }
209
+
210
+ throw new Error(`Unsupported source type: ${block.source_type}`);
211
+ },
212
+
213
+ fromStandardFileBlock(block): FileDataPart | InlineDataPart | TextPart {
214
+ if (!isMultimodalModel) {
215
+ throw new Error('This model does not support files');
216
+ }
217
+ if (block.source_type === 'text') {
218
+ return {
219
+ text: block.text,
220
+ };
221
+ }
222
+ if (block.source_type === 'url') {
223
+ const data = parseBase64DataUrl({ dataUrl: block.url });
224
+ if (data) {
225
+ return {
226
+ inlineData: {
227
+ mimeType: data.mime_type,
228
+ data: data.data,
229
+ },
230
+ };
231
+ } else {
232
+ return {
233
+ fileData: {
234
+ mimeType: block.mime_type ?? '',
235
+ fileUri: block.url,
236
+ },
237
+ };
238
+ }
239
+ }
240
+
241
+ if (block.source_type === 'base64') {
242
+ return {
243
+ inlineData: {
244
+ mimeType: block.mime_type ?? '',
245
+ data: block.data,
246
+ },
247
+ };
248
+ }
249
+ throw new Error(`Unsupported source type: ${block.source_type}`);
250
+ },
251
+ };
252
+ return standardContentBlockConverter;
253
+ }
254
+
255
+ function _convertLangChainContentToPart(
256
+ content: MessageContentComplex,
257
+ isMultimodalModel: boolean
258
+ ): Part | undefined {
259
+ if (isDataContentBlock(content)) {
260
+ return convertToProviderContentBlock(
261
+ content,
262
+ _getStandardContentBlockConverter(isMultimodalModel)
263
+ );
264
+ }
265
+
266
+ if (content.type === 'text') {
267
+ return { text: content.text };
268
+ } else if (content.type === 'executableCode') {
269
+ return { executableCode: content.executableCode };
270
+ } else if (content.type === 'codeExecutionResult') {
271
+ return { codeExecutionResult: content.codeExecutionResult };
272
+ } else if (content.type === 'image_url') {
273
+ if (!isMultimodalModel) {
274
+ throw new Error('This model does not support images');
275
+ }
276
+ let source: string;
277
+ if (typeof content.image_url === 'string') {
278
+ source = content.image_url;
279
+ } else if (
280
+ typeof content.image_url === 'object' &&
281
+ 'url' in content.image_url
282
+ ) {
283
+ source = content.image_url.url;
284
+ } else {
285
+ throw new Error('Please provide image as base64 encoded data URL');
286
+ }
287
+ const [dm, data] = source.split(',');
288
+ if (!dm.startsWith('data:')) {
289
+ throw new Error('Please provide image as base64 encoded data URL');
290
+ }
291
+
292
+ const [mimeType, encoding] = dm.replace(/^data:/, '').split(';');
293
+ if (encoding !== 'base64') {
294
+ throw new Error('Please provide image as base64 encoded data URL');
295
+ }
296
+
297
+ return {
298
+ inlineData: {
299
+ data,
300
+ mimeType,
301
+ },
302
+ };
303
+ } else if (content.type === 'media') {
304
+ return messageContentMedia(content);
305
+ } else if (content.type === 'tool_use') {
306
+ return {
307
+ functionCall: {
308
+ name: content.name,
309
+ args: content.input,
310
+ },
311
+ };
312
+ } else if (
313
+ content.type?.includes('/') === true &&
314
+ // Ensure it's a single slash.
315
+ content.type.split('/').length === 2 &&
316
+ 'data' in content &&
317
+ typeof content.data === 'string'
318
+ ) {
319
+ return {
320
+ inlineData: {
321
+ mimeType: content.type,
322
+ data: content.data,
323
+ },
324
+ };
325
+ } else if ('functionCall' in content) {
326
+ // No action needed here — function calls will be added later from message.tool_calls
327
+ return undefined;
328
+ } else {
329
+ if ('type' in content) {
330
+ throw new Error(`Unknown content type ${content.type}`);
331
+ } else {
332
+ throw new Error(`Unknown content ${JSON.stringify(content)}`);
333
+ }
334
+ }
335
+ }
336
+
337
+ export function convertMessageContentToParts(
338
+ message: BaseMessage,
339
+ isMultimodalModel: boolean,
340
+ previousMessages: BaseMessage[]
341
+ ): Part[] {
342
+ if (isToolMessage(message)) {
343
+ const messageName =
344
+ message.name ??
345
+ inferToolNameFromPreviousMessages(message, previousMessages);
346
+ if (messageName === undefined) {
347
+ throw new Error(
348
+ `Google requires a tool name for each tool call response, and we could not infer a called tool name for ToolMessage "${message.id}" from your passed messages. Please populate a "name" field on that ToolMessage explicitly.`
349
+ );
350
+ }
351
+
352
+ const result = Array.isArray(message.content)
353
+ ? (message.content
354
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
355
+ .filter((p) => p !== undefined) as Part[])
356
+ : message.content;
357
+
358
+ if (message.status === 'error') {
359
+ return [
360
+ {
361
+ functionResponse: {
362
+ name: messageName,
363
+ // The API expects an object with an `error` field if the function call fails.
364
+ // `error` must be a valid object (not a string or array), so we wrap `message.content` here
365
+ response: { error: { details: result } },
366
+ },
367
+ },
368
+ ];
369
+ }
370
+
371
+ return [
372
+ {
373
+ functionResponse: {
374
+ name: messageName,
375
+ // again, can't have a string or array value for `response`, so we wrap it as an object here
376
+ response: { result },
377
+ },
378
+ },
379
+ ];
380
+ }
381
+
382
+ let functionCalls: FunctionCallPart[] = [];
383
+ const messageParts: Part[] = [];
384
+
385
+ if (typeof message.content === 'string' && message.content) {
386
+ messageParts.push({ text: message.content });
387
+ }
388
+
389
+ if (Array.isArray(message.content)) {
390
+ messageParts.push(
391
+ ...(message.content
392
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
393
+ .filter((p) => p !== undefined) as Part[])
394
+ );
395
+ }
396
+
397
+ if (isAIMessage(message) && message.tool_calls?.length != null) {
398
+ functionCalls = message.tool_calls.map((tc) => {
399
+ return {
400
+ functionCall: {
401
+ name: tc.name,
402
+ args: tc.args,
403
+ },
404
+ };
405
+ });
406
+ }
407
+
408
+ return [...messageParts, ...functionCalls];
409
+ }
410
+
411
+ export function convertBaseMessagesToContent(
412
+ messages: BaseMessage[],
413
+ isMultimodalModel: boolean,
414
+ convertSystemMessageToHumanContent: boolean = false
415
+ ): Content[] {
416
+ return messages.reduce<{
417
+ content: Content[];
418
+ mergeWithPreviousContent: boolean;
419
+ }>(
420
+ (acc, message, index) => {
421
+ if (!isBaseMessage(message)) {
422
+ throw new Error('Unsupported message input');
423
+ }
424
+ const author = getMessageAuthor(message);
425
+ if (author === 'system' && index !== 0) {
426
+ throw new Error('System message should be the first one');
427
+ }
428
+ const role = convertAuthorToRole(author);
429
+
430
+ const prevContent = acc.content[acc.content.length];
431
+ if (
432
+ !acc.mergeWithPreviousContent &&
433
+ prevContent &&
434
+ prevContent.role === role
435
+ ) {
436
+ throw new Error(
437
+ 'Google Generative AI requires alternate messages between authors'
438
+ );
439
+ }
440
+
441
+ const parts = convertMessageContentToParts(
442
+ message,
443
+ isMultimodalModel,
444
+ messages.slice(0, index)
445
+ );
446
+
447
+ if (acc.mergeWithPreviousContent) {
448
+ const prevContent = acc.content[acc.content.length - 1];
449
+ if (!prevContent) {
450
+ throw new Error(
451
+ 'There was a problem parsing your system message. Please try a prompt without one.'
452
+ );
453
+ }
454
+ prevContent.parts.push(...parts);
455
+
456
+ return {
457
+ mergeWithPreviousContent: false,
458
+ content: acc.content,
459
+ };
460
+ }
461
+ let actualRole = role;
462
+ if (
463
+ actualRole === 'function' ||
464
+ (actualRole === 'system' && !convertSystemMessageToHumanContent)
465
+ ) {
466
+ // GenerativeAI API will throw an error if the role is not "user" or "model."
467
+ actualRole = 'user';
468
+ }
469
+ const content: Content = {
470
+ role: actualRole,
471
+ parts,
472
+ };
473
+ return {
474
+ mergeWithPreviousContent:
475
+ author === 'system' && !convertSystemMessageToHumanContent,
476
+ content: [...acc.content, content],
477
+ };
478
+ },
479
+ { content: [], mergeWithPreviousContent: false }
480
+ ).content;
481
+ }
482
+
483
+ export function convertResponseContentToChatGenerationChunk(
484
+ response: EnhancedGenerateContentResponse,
485
+ extra: {
486
+ usageMetadata?: UsageMetadata | undefined;
487
+ index: number;
488
+ }
489
+ ): ChatGenerationChunk | null {
490
+ if (!response.candidates || response.candidates.length === 0) {
491
+ return null;
492
+ }
493
+ const functionCalls = response.functionCalls();
494
+ const [candidate] = response.candidates;
495
+ const { content: candidateContent, ...generationInfo } = candidate;
496
+ let content: MessageContent | undefined;
497
+ // Checks if some parts do not have text. If false, it means that the content is a string.
498
+ const reasoningParts: string[] = [];
499
+ if (
500
+ Array.isArray(candidateContent.parts) &&
501
+ candidateContent.parts.every((p) => 'text' in p)
502
+ ) {
503
+ // content = candidateContent.parts.map((p) => p.text).join('');
504
+ const textParts: string[] = [];
505
+ for (const part of candidateContent.parts) {
506
+ if ('thought' in part && part.thought === true) {
507
+ reasoningParts.push(part.text ?? '');
508
+ continue;
509
+ }
510
+ textParts.push(part.text ?? '');
511
+ }
512
+ content = textParts.join('');
513
+ } else if (Array.isArray(candidateContent.parts)) {
514
+ content = candidateContent.parts.map((p) => {
515
+ if ('text' in p && 'thought' in p && p.thought === true) {
516
+ reasoningParts.push(p.text ?? '');
517
+ } else if ('text' in p) {
518
+ return {
519
+ type: 'text',
520
+ text: p.text,
521
+ };
522
+ } else if ('executableCode' in p) {
523
+ return {
524
+ type: 'executableCode',
525
+ executableCode: p.executableCode,
526
+ };
527
+ } else if ('codeExecutionResult' in p) {
528
+ return {
529
+ type: 'codeExecutionResult',
530
+ codeExecutionResult: p.codeExecutionResult,
531
+ };
532
+ }
533
+ return p;
534
+ });
535
+ } else {
536
+ // no content returned - likely due to abnormal stop reason, e.g. malformed function call
537
+ content = [];
538
+ }
539
+
540
+ let text = '';
541
+ if (typeof content === 'string' && content) {
542
+ text = content;
543
+ } else if (Array.isArray(content)) {
544
+ const block = content.find((b) => 'text' in b) as
545
+ | { text: string }
546
+ | undefined;
547
+ text = block?.text ?? '';
548
+ }
549
+
550
+ const toolCallChunks: ToolCallChunk[] = [];
551
+ if (functionCalls) {
552
+ toolCallChunks.push(
553
+ ...functionCalls.map((fc) => ({
554
+ ...fc,
555
+ args: JSON.stringify(fc.args),
556
+ index: extra.index,
557
+ type: 'tool_call_chunk' as const,
558
+ id: 'id' in fc && typeof fc.id === 'string' ? fc.id : uuidv4(),
559
+ }))
560
+ );
561
+ }
562
+
563
+ const additional_kwargs: ChatGeneration['message']['additional_kwargs'] = {};
564
+ if (reasoningParts.length > 0) {
565
+ additional_kwargs.reasoning = reasoningParts.join('');
566
+ }
567
+
568
+ return new ChatGenerationChunk({
569
+ text,
570
+ message: new AIMessageChunk({
571
+ content: content || '',
572
+ name: !candidateContent ? undefined : candidateContent.role,
573
+ tool_call_chunks: toolCallChunks,
574
+ // Each chunk can have unique "generationInfo", and merging strategy is unclear,
575
+ // so leave blank for now.
576
+ additional_kwargs,
577
+ usage_metadata: extra.usageMetadata,
578
+ }),
579
+ generationInfo,
580
+ });
581
+ }
582
+
583
+ export function convertToGenerativeAITools(
584
+ tools: GoogleGenerativeAIToolType[]
585
+ ): GoogleGenerativeAIFunctionDeclarationsTool[] {
586
+ if (
587
+ tools.every(
588
+ (tool) =>
589
+ 'functionDeclarations' in tool &&
590
+ Array.isArray(tool.functionDeclarations)
591
+ )
592
+ ) {
593
+ return tools as GoogleGenerativeAIFunctionDeclarationsTool[];
594
+ }
595
+ return [
596
+ {
597
+ functionDeclarations: tools.map(
598
+ (tool): GenerativeAIFunctionDeclaration => {
599
+ if (isLangChainTool(tool)) {
600
+ const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
601
+ if (
602
+ jsonSchema.type === 'object' &&
603
+ 'properties' in jsonSchema &&
604
+ Object.keys(jsonSchema.properties).length === 0
605
+ ) {
606
+ return {
607
+ name: tool.name,
608
+ description: tool.description,
609
+ };
610
+ }
611
+ return {
612
+ name: tool.name,
613
+ description: tool.description,
614
+ parameters: jsonSchema,
615
+ };
616
+ }
617
+ if (isOpenAITool(tool)) {
618
+ return {
619
+ name: tool.function.name,
620
+ description:
621
+ tool.function.description ?? 'A function available to call.',
622
+ parameters: jsonSchemaToGeminiParameters(
623
+ tool.function.parameters
624
+ ),
625
+ };
626
+ }
627
+ return tool as unknown as GenerativeAIFunctionDeclaration;
628
+ }
629
+ ),
630
+ },
631
+ ];
632
+ }