@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,463 @@
1
+ import { isBaseMessage, AIMessageChunk, ChatMessage, isToolMessage, isAIMessage, isDataContentBlock, convertToProviderContentBlock, parseBase64DataUrl } from '@langchain/core/messages';
2
+ import { ChatGenerationChunk } from '@langchain/core/outputs';
3
+ import '@langchain/core/utils/function_calling';
4
+ import '@langchain/core/language_models/base';
5
+ import { v4 } from 'uuid';
6
+ import '@langchain/core/utils/types';
7
+ import '@langchain/core/utils/json_schema';
8
+
9
+ function getMessageAuthor(message) {
10
+ const type = message._getType();
11
+ if (ChatMessage.isInstance(message)) {
12
+ return message.role;
13
+ }
14
+ if (type === 'tool') {
15
+ return type;
16
+ }
17
+ return message.name ?? type;
18
+ }
19
+ /**
20
+ * Maps a message type to a Google Generative AI chat author.
21
+ * @param message The message to map.
22
+ * @param model The model to use for mapping.
23
+ * @returns The message type mapped to a Google Generative AI chat author.
24
+ */
25
+ function convertAuthorToRole(author) {
26
+ switch (author) {
27
+ /**
28
+ * Note: Gemini currently is not supporting system messages
29
+ * we will convert them to human messages and merge with following
30
+ * */
31
+ case 'supervisor':
32
+ case 'ai':
33
+ case 'model': // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
34
+ return 'model';
35
+ case 'system':
36
+ return 'system';
37
+ case 'human':
38
+ return 'user';
39
+ case 'tool':
40
+ case 'function':
41
+ return 'function';
42
+ default:
43
+ throw new Error(`Unknown / unsupported author: ${author}`);
44
+ }
45
+ }
46
+ function messageContentMedia(content) {
47
+ if ('mimeType' in content && 'data' in content) {
48
+ return {
49
+ inlineData: {
50
+ mimeType: content.mimeType,
51
+ data: content.data,
52
+ },
53
+ };
54
+ }
55
+ if ('mimeType' in content && 'fileUri' in content) {
56
+ return {
57
+ fileData: {
58
+ mimeType: content.mimeType,
59
+ fileUri: content.fileUri,
60
+ },
61
+ };
62
+ }
63
+ throw new Error('Invalid media content');
64
+ }
65
+ function inferToolNameFromPreviousMessages(message, previousMessages) {
66
+ return previousMessages
67
+ .map((msg) => {
68
+ if (isAIMessage(msg)) {
69
+ return msg.tool_calls ?? [];
70
+ }
71
+ return [];
72
+ })
73
+ .flat()
74
+ .find((toolCall) => {
75
+ return toolCall.id === message.tool_call_id;
76
+ })?.name;
77
+ }
78
+ function _getStandardContentBlockConverter(isMultimodalModel) {
79
+ const standardContentBlockConverter = {
80
+ providerName: 'Google Gemini',
81
+ fromStandardTextBlock(block) {
82
+ return {
83
+ text: block.text,
84
+ };
85
+ },
86
+ fromStandardImageBlock(block) {
87
+ if (!isMultimodalModel) {
88
+ throw new Error('This model does not support images');
89
+ }
90
+ if (block.source_type === 'url') {
91
+ const data = parseBase64DataUrl({ dataUrl: block.url });
92
+ if (data) {
93
+ return {
94
+ inlineData: {
95
+ mimeType: data.mime_type,
96
+ data: data.data,
97
+ },
98
+ };
99
+ }
100
+ else {
101
+ return {
102
+ fileData: {
103
+ mimeType: block.mime_type ?? '',
104
+ fileUri: block.url,
105
+ },
106
+ };
107
+ }
108
+ }
109
+ if (block.source_type === 'base64') {
110
+ return {
111
+ inlineData: {
112
+ mimeType: block.mime_type ?? '',
113
+ data: block.data,
114
+ },
115
+ };
116
+ }
117
+ throw new Error(`Unsupported source type: ${block.source_type}`);
118
+ },
119
+ fromStandardAudioBlock(block) {
120
+ if (!isMultimodalModel) {
121
+ throw new Error('This model does not support audio');
122
+ }
123
+ if (block.source_type === 'url') {
124
+ const data = parseBase64DataUrl({ dataUrl: block.url });
125
+ if (data) {
126
+ return {
127
+ inlineData: {
128
+ mimeType: data.mime_type,
129
+ data: data.data,
130
+ },
131
+ };
132
+ }
133
+ else {
134
+ return {
135
+ fileData: {
136
+ mimeType: block.mime_type ?? '',
137
+ fileUri: block.url,
138
+ },
139
+ };
140
+ }
141
+ }
142
+ if (block.source_type === 'base64') {
143
+ return {
144
+ inlineData: {
145
+ mimeType: block.mime_type ?? '',
146
+ data: block.data,
147
+ },
148
+ };
149
+ }
150
+ throw new Error(`Unsupported source type: ${block.source_type}`);
151
+ },
152
+ fromStandardFileBlock(block) {
153
+ if (!isMultimodalModel) {
154
+ throw new Error('This model does not support files');
155
+ }
156
+ if (block.source_type === 'text') {
157
+ return {
158
+ text: block.text,
159
+ };
160
+ }
161
+ if (block.source_type === 'url') {
162
+ const data = parseBase64DataUrl({ dataUrl: block.url });
163
+ if (data) {
164
+ return {
165
+ inlineData: {
166
+ mimeType: data.mime_type,
167
+ data: data.data,
168
+ },
169
+ };
170
+ }
171
+ else {
172
+ return {
173
+ fileData: {
174
+ mimeType: block.mime_type ?? '',
175
+ fileUri: block.url,
176
+ },
177
+ };
178
+ }
179
+ }
180
+ if (block.source_type === 'base64') {
181
+ return {
182
+ inlineData: {
183
+ mimeType: block.mime_type ?? '',
184
+ data: block.data,
185
+ },
186
+ };
187
+ }
188
+ throw new Error(`Unsupported source type: ${block.source_type}`);
189
+ },
190
+ };
191
+ return standardContentBlockConverter;
192
+ }
193
+ function _convertLangChainContentToPart(content, isMultimodalModel) {
194
+ if (isDataContentBlock(content)) {
195
+ return convertToProviderContentBlock(content, _getStandardContentBlockConverter(isMultimodalModel));
196
+ }
197
+ if (content.type === 'text') {
198
+ return { text: content.text };
199
+ }
200
+ else if (content.type === 'executableCode') {
201
+ return { executableCode: content.executableCode };
202
+ }
203
+ else if (content.type === 'codeExecutionResult') {
204
+ return { codeExecutionResult: content.codeExecutionResult };
205
+ }
206
+ else if (content.type === 'image_url') {
207
+ if (!isMultimodalModel) {
208
+ throw new Error('This model does not support images');
209
+ }
210
+ let source;
211
+ if (typeof content.image_url === 'string') {
212
+ source = content.image_url;
213
+ }
214
+ else if (typeof content.image_url === 'object' &&
215
+ 'url' in content.image_url) {
216
+ source = content.image_url.url;
217
+ }
218
+ else {
219
+ throw new Error('Please provide image as base64 encoded data URL');
220
+ }
221
+ const [dm, data] = source.split(',');
222
+ if (!dm.startsWith('data:')) {
223
+ throw new Error('Please provide image as base64 encoded data URL');
224
+ }
225
+ const [mimeType, encoding] = dm.replace(/^data:/, '').split(';');
226
+ if (encoding !== 'base64') {
227
+ throw new Error('Please provide image as base64 encoded data URL');
228
+ }
229
+ return {
230
+ inlineData: {
231
+ data,
232
+ mimeType,
233
+ },
234
+ };
235
+ }
236
+ else if (content.type === 'media') {
237
+ return messageContentMedia(content);
238
+ }
239
+ else if (content.type === 'tool_use') {
240
+ return {
241
+ functionCall: {
242
+ name: content.name,
243
+ args: content.input,
244
+ },
245
+ };
246
+ }
247
+ else if (content.type?.includes('/') === true &&
248
+ // Ensure it's a single slash.
249
+ content.type.split('/').length === 2 &&
250
+ 'data' in content &&
251
+ typeof content.data === 'string') {
252
+ return {
253
+ inlineData: {
254
+ mimeType: content.type,
255
+ data: content.data,
256
+ },
257
+ };
258
+ }
259
+ else if ('functionCall' in content) {
260
+ // No action needed here — function calls will be added later from message.tool_calls
261
+ return undefined;
262
+ }
263
+ else {
264
+ if ('type' in content) {
265
+ throw new Error(`Unknown content type ${content.type}`);
266
+ }
267
+ else {
268
+ throw new Error(`Unknown content ${JSON.stringify(content)}`);
269
+ }
270
+ }
271
+ }
272
+ function convertMessageContentToParts(message, isMultimodalModel, previousMessages) {
273
+ if (isToolMessage(message)) {
274
+ const messageName = message.name ??
275
+ inferToolNameFromPreviousMessages(message, previousMessages);
276
+ if (messageName === undefined) {
277
+ throw new Error(`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.`);
278
+ }
279
+ const result = Array.isArray(message.content)
280
+ ? message.content
281
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
282
+ .filter((p) => p !== undefined)
283
+ : message.content;
284
+ if (message.status === 'error') {
285
+ return [
286
+ {
287
+ functionResponse: {
288
+ name: messageName,
289
+ // The API expects an object with an `error` field if the function call fails.
290
+ // `error` must be a valid object (not a string or array), so we wrap `message.content` here
291
+ response: { error: { details: result } },
292
+ },
293
+ },
294
+ ];
295
+ }
296
+ return [
297
+ {
298
+ functionResponse: {
299
+ name: messageName,
300
+ // again, can't have a string or array value for `response`, so we wrap it as an object here
301
+ response: { result },
302
+ },
303
+ },
304
+ ];
305
+ }
306
+ let functionCalls = [];
307
+ const messageParts = [];
308
+ if (typeof message.content === 'string' && message.content) {
309
+ messageParts.push({ text: message.content });
310
+ }
311
+ if (Array.isArray(message.content)) {
312
+ messageParts.push(...message.content
313
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
314
+ .filter((p) => p !== undefined));
315
+ }
316
+ if (isAIMessage(message) && message.tool_calls?.length != null) {
317
+ functionCalls = message.tool_calls.map((tc) => {
318
+ return {
319
+ functionCall: {
320
+ name: tc.name,
321
+ args: tc.args,
322
+ },
323
+ };
324
+ });
325
+ }
326
+ return [...messageParts, ...functionCalls];
327
+ }
328
+ function convertBaseMessagesToContent(messages, isMultimodalModel, convertSystemMessageToHumanContent = false) {
329
+ return messages.reduce((acc, message, index) => {
330
+ if (!isBaseMessage(message)) {
331
+ throw new Error('Unsupported message input');
332
+ }
333
+ const author = getMessageAuthor(message);
334
+ if (author === 'system' && index !== 0) {
335
+ throw new Error('System message should be the first one');
336
+ }
337
+ const role = convertAuthorToRole(author);
338
+ const prevContent = acc.content[acc.content.length];
339
+ if (!acc.mergeWithPreviousContent &&
340
+ prevContent &&
341
+ prevContent.role === role) {
342
+ throw new Error('Google Generative AI requires alternate messages between authors');
343
+ }
344
+ const parts = convertMessageContentToParts(message, isMultimodalModel, messages.slice(0, index));
345
+ if (acc.mergeWithPreviousContent) {
346
+ const prevContent = acc.content[acc.content.length - 1];
347
+ if (!prevContent) {
348
+ throw new Error('There was a problem parsing your system message. Please try a prompt without one.');
349
+ }
350
+ prevContent.parts.push(...parts);
351
+ return {
352
+ mergeWithPreviousContent: false,
353
+ content: acc.content,
354
+ };
355
+ }
356
+ let actualRole = role;
357
+ if (actualRole === 'function' ||
358
+ (actualRole === 'system' && !convertSystemMessageToHumanContent)) {
359
+ // GenerativeAI API will throw an error if the role is not "user" or "model."
360
+ actualRole = 'user';
361
+ }
362
+ const content = {
363
+ role: actualRole,
364
+ parts,
365
+ };
366
+ return {
367
+ mergeWithPreviousContent: author === 'system' && !convertSystemMessageToHumanContent,
368
+ content: [...acc.content, content],
369
+ };
370
+ }, { content: [], mergeWithPreviousContent: false }).content;
371
+ }
372
+ function convertResponseContentToChatGenerationChunk(response, extra) {
373
+ if (!response.candidates || response.candidates.length === 0) {
374
+ return null;
375
+ }
376
+ const functionCalls = response.functionCalls();
377
+ const [candidate] = response.candidates;
378
+ const { content: candidateContent, ...generationInfo } = candidate;
379
+ let content;
380
+ // Checks if some parts do not have text. If false, it means that the content is a string.
381
+ const reasoningParts = [];
382
+ if (Array.isArray(candidateContent.parts) &&
383
+ candidateContent.parts.every((p) => 'text' in p)) {
384
+ // content = candidateContent.parts.map((p) => p.text).join('');
385
+ const textParts = [];
386
+ for (const part of candidateContent.parts) {
387
+ if ('thought' in part && part.thought === true) {
388
+ reasoningParts.push(part.text ?? '');
389
+ continue;
390
+ }
391
+ textParts.push(part.text ?? '');
392
+ }
393
+ content = textParts.join('');
394
+ }
395
+ else if (Array.isArray(candidateContent.parts)) {
396
+ content = candidateContent.parts.map((p) => {
397
+ if ('text' in p && 'thought' in p && p.thought === true) {
398
+ reasoningParts.push(p.text ?? '');
399
+ }
400
+ else if ('text' in p) {
401
+ return {
402
+ type: 'text',
403
+ text: p.text,
404
+ };
405
+ }
406
+ else if ('executableCode' in p) {
407
+ return {
408
+ type: 'executableCode',
409
+ executableCode: p.executableCode,
410
+ };
411
+ }
412
+ else if ('codeExecutionResult' in p) {
413
+ return {
414
+ type: 'codeExecutionResult',
415
+ codeExecutionResult: p.codeExecutionResult,
416
+ };
417
+ }
418
+ return p;
419
+ });
420
+ }
421
+ else {
422
+ // no content returned - likely due to abnormal stop reason, e.g. malformed function call
423
+ content = [];
424
+ }
425
+ let text = '';
426
+ if (typeof content === 'string' && content) {
427
+ text = content;
428
+ }
429
+ else if (Array.isArray(content)) {
430
+ const block = content.find((b) => 'text' in b);
431
+ text = block?.text ?? '';
432
+ }
433
+ const toolCallChunks = [];
434
+ if (functionCalls) {
435
+ toolCallChunks.push(...functionCalls.map((fc) => ({
436
+ ...fc,
437
+ args: JSON.stringify(fc.args),
438
+ index: extra.index,
439
+ type: 'tool_call_chunk',
440
+ id: 'id' in fc && typeof fc.id === 'string' ? fc.id : v4(),
441
+ })));
442
+ }
443
+ const additional_kwargs = {};
444
+ if (reasoningParts.length > 0) {
445
+ additional_kwargs.reasoning = reasoningParts.join('');
446
+ }
447
+ return new ChatGenerationChunk({
448
+ text,
449
+ message: new AIMessageChunk({
450
+ content: content || '',
451
+ name: !candidateContent ? undefined : candidateContent.role,
452
+ tool_call_chunks: toolCallChunks,
453
+ // Each chunk can have unique "generationInfo", and merging strategy is unclear,
454
+ // so leave blank for now.
455
+ additional_kwargs,
456
+ usage_metadata: extra.usageMetadata,
457
+ }),
458
+ generationInfo,
459
+ });
460
+ }
461
+
462
+ export { convertAuthorToRole, convertBaseMessagesToContent, convertMessageContentToParts, convertResponseContentToChatGenerationChunk, getMessageAuthor };
463
+ //# sourceMappingURL=common.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.mjs","sources":["../../../../../src/llm/google/utils/common.ts"],"sourcesContent":["import {\n EnhancedGenerateContentResponse,\n Content,\n Part,\n type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,\n type FunctionDeclaration as GenerativeAIFunctionDeclaration,\n POSSIBLE_ROLES,\n FunctionCallPart,\n TextPart,\n FileDataPart,\n InlineDataPart,\n} from '@google/generative-ai';\nimport {\n AIMessageChunk,\n BaseMessage,\n ChatMessage,\n ToolMessage,\n ToolMessageChunk,\n MessageContent,\n MessageContentComplex,\n UsageMetadata,\n isAIMessage,\n isBaseMessage,\n isToolMessage,\n StandardContentBlockConverter,\n parseBase64DataUrl,\n convertToProviderContentBlock,\n isDataContentBlock,\n} from '@langchain/core/messages';\nimport { ChatGenerationChunk } from '@langchain/core/outputs';\nimport type { ChatGeneration } from '@langchain/core/outputs';\nimport { isLangChainTool } from '@langchain/core/utils/function_calling';\nimport { isOpenAITool } from '@langchain/core/language_models/base';\nimport { ToolCallChunk } from '@langchain/core/messages/tool';\nimport { v4 as uuidv4 } from 'uuid';\nimport {\n jsonSchemaToGeminiParameters,\n schemaToGenerativeAIParameters,\n} from './zod_to_genai_parameters';\nimport { GoogleGenerativeAIToolType } from '../types';\n\nexport function getMessageAuthor(message: BaseMessage): string {\n const type = message._getType();\n if (ChatMessage.isInstance(message)) {\n return message.role;\n }\n if (type === 'tool') {\n return type;\n }\n return message.name ?? type;\n}\n\n/**\n * Maps a message type to a Google Generative AI chat author.\n * @param message The message to map.\n * @param model The model to use for mapping.\n * @returns The message type mapped to a Google Generative AI chat author.\n */\nexport function convertAuthorToRole(\n author: string\n): (typeof POSSIBLE_ROLES)[number] {\n switch (author) {\n /**\n * Note: Gemini currently is not supporting system messages\n * we will convert them to human messages and merge with following\n * */\n case 'supervisor':\n case 'ai':\n case 'model': // getMessageAuthor returns message.name. code ex.: return message.name ?? type;\n return 'model';\n case 'system':\n return 'system';\n case 'human':\n return 'user';\n case 'tool':\n case 'function':\n return 'function';\n default:\n throw new Error(`Unknown / unsupported author: ${author}`);\n }\n}\n\nfunction messageContentMedia(content: MessageContentComplex): Part {\n if ('mimeType' in content && 'data' in content) {\n return {\n inlineData: {\n mimeType: content.mimeType,\n data: content.data,\n },\n };\n }\n if ('mimeType' in content && 'fileUri' in content) {\n return {\n fileData: {\n mimeType: content.mimeType,\n fileUri: content.fileUri,\n },\n };\n }\n\n throw new Error('Invalid media content');\n}\n\nfunction inferToolNameFromPreviousMessages(\n message: ToolMessage | ToolMessageChunk,\n previousMessages: BaseMessage[]\n): string | undefined {\n return previousMessages\n .map((msg) => {\n if (isAIMessage(msg)) {\n return msg.tool_calls ?? [];\n }\n return [];\n })\n .flat()\n .find((toolCall) => {\n return toolCall.id === message.tool_call_id;\n })?.name;\n}\n\nfunction _getStandardContentBlockConverter(\n isMultimodalModel: boolean\n): StandardContentBlockConverter<{\n text: TextPart;\n image: FileDataPart | InlineDataPart;\n audio: FileDataPart | InlineDataPart;\n file: FileDataPart | InlineDataPart | TextPart;\n}> {\n const standardContentBlockConverter: StandardContentBlockConverter<{\n text: TextPart;\n image: FileDataPart | InlineDataPart;\n audio: FileDataPart | InlineDataPart;\n file: FileDataPart | InlineDataPart | TextPart;\n }> = {\n providerName: 'Google Gemini',\n\n fromStandardTextBlock(block) {\n return {\n text: block.text,\n };\n },\n\n fromStandardImageBlock(block): FileDataPart | InlineDataPart {\n if (!isMultimodalModel) {\n throw new Error('This model does not support images');\n }\n if (block.source_type === 'url') {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (data) {\n return {\n inlineData: {\n mimeType: data.mime_type,\n data: data.data,\n },\n };\n } else {\n return {\n fileData: {\n mimeType: block.mime_type ?? '',\n fileUri: block.url,\n },\n };\n }\n }\n\n if (block.source_type === 'base64') {\n return {\n inlineData: {\n mimeType: block.mime_type ?? '',\n data: block.data,\n },\n };\n }\n\n throw new Error(`Unsupported source type: ${block.source_type}`);\n },\n\n fromStandardAudioBlock(block): FileDataPart | InlineDataPart {\n if (!isMultimodalModel) {\n throw new Error('This model does not support audio');\n }\n if (block.source_type === 'url') {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (data) {\n return {\n inlineData: {\n mimeType: data.mime_type,\n data: data.data,\n },\n };\n } else {\n return {\n fileData: {\n mimeType: block.mime_type ?? '',\n fileUri: block.url,\n },\n };\n }\n }\n\n if (block.source_type === 'base64') {\n return {\n inlineData: {\n mimeType: block.mime_type ?? '',\n data: block.data,\n },\n };\n }\n\n throw new Error(`Unsupported source type: ${block.source_type}`);\n },\n\n fromStandardFileBlock(block): FileDataPart | InlineDataPart | TextPart {\n if (!isMultimodalModel) {\n throw new Error('This model does not support files');\n }\n if (block.source_type === 'text') {\n return {\n text: block.text,\n };\n }\n if (block.source_type === 'url') {\n const data = parseBase64DataUrl({ dataUrl: block.url });\n if (data) {\n return {\n inlineData: {\n mimeType: data.mime_type,\n data: data.data,\n },\n };\n } else {\n return {\n fileData: {\n mimeType: block.mime_type ?? '',\n fileUri: block.url,\n },\n };\n }\n }\n\n if (block.source_type === 'base64') {\n return {\n inlineData: {\n mimeType: block.mime_type ?? '',\n data: block.data,\n },\n };\n }\n throw new Error(`Unsupported source type: ${block.source_type}`);\n },\n };\n return standardContentBlockConverter;\n}\n\nfunction _convertLangChainContentToPart(\n content: MessageContentComplex,\n isMultimodalModel: boolean\n): Part | undefined {\n if (isDataContentBlock(content)) {\n return convertToProviderContentBlock(\n content,\n _getStandardContentBlockConverter(isMultimodalModel)\n );\n }\n\n if (content.type === 'text') {\n return { text: content.text };\n } else if (content.type === 'executableCode') {\n return { executableCode: content.executableCode };\n } else if (content.type === 'codeExecutionResult') {\n return { codeExecutionResult: content.codeExecutionResult };\n } else if (content.type === 'image_url') {\n if (!isMultimodalModel) {\n throw new Error('This model does not support images');\n }\n let source: string;\n if (typeof content.image_url === 'string') {\n source = content.image_url;\n } else if (\n typeof content.image_url === 'object' &&\n 'url' in content.image_url\n ) {\n source = content.image_url.url;\n } else {\n throw new Error('Please provide image as base64 encoded data URL');\n }\n const [dm, data] = source.split(',');\n if (!dm.startsWith('data:')) {\n throw new Error('Please provide image as base64 encoded data URL');\n }\n\n const [mimeType, encoding] = dm.replace(/^data:/, '').split(';');\n if (encoding !== 'base64') {\n throw new Error('Please provide image as base64 encoded data URL');\n }\n\n return {\n inlineData: {\n data,\n mimeType,\n },\n };\n } else if (content.type === 'media') {\n return messageContentMedia(content);\n } else if (content.type === 'tool_use') {\n return {\n functionCall: {\n name: content.name,\n args: content.input,\n },\n };\n } else if (\n content.type?.includes('/') === true &&\n // Ensure it's a single slash.\n content.type.split('/').length === 2 &&\n 'data' in content &&\n typeof content.data === 'string'\n ) {\n return {\n inlineData: {\n mimeType: content.type,\n data: content.data,\n },\n };\n } else if ('functionCall' in content) {\n // No action needed here — function calls will be added later from message.tool_calls\n return undefined;\n } else {\n if ('type' in content) {\n throw new Error(`Unknown content type ${content.type}`);\n } else {\n throw new Error(`Unknown content ${JSON.stringify(content)}`);\n }\n }\n}\n\nexport function convertMessageContentToParts(\n message: BaseMessage,\n isMultimodalModel: boolean,\n previousMessages: BaseMessage[]\n): Part[] {\n if (isToolMessage(message)) {\n const messageName =\n message.name ??\n inferToolNameFromPreviousMessages(message, previousMessages);\n if (messageName === undefined) {\n throw new Error(\n `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.`\n );\n }\n\n const result = Array.isArray(message.content)\n ? (message.content\n .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))\n .filter((p) => p !== undefined) as Part[])\n : message.content;\n\n if (message.status === 'error') {\n return [\n {\n functionResponse: {\n name: messageName,\n // The API expects an object with an `error` field if the function call fails.\n // `error` must be a valid object (not a string or array), so we wrap `message.content` here\n response: { error: { details: result } },\n },\n },\n ];\n }\n\n return [\n {\n functionResponse: {\n name: messageName,\n // again, can't have a string or array value for `response`, so we wrap it as an object here\n response: { result },\n },\n },\n ];\n }\n\n let functionCalls: FunctionCallPart[] = [];\n const messageParts: Part[] = [];\n\n if (typeof message.content === 'string' && message.content) {\n messageParts.push({ text: message.content });\n }\n\n if (Array.isArray(message.content)) {\n messageParts.push(\n ...(message.content\n .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))\n .filter((p) => p !== undefined) as Part[])\n );\n }\n\n if (isAIMessage(message) && message.tool_calls?.length != null) {\n functionCalls = message.tool_calls.map((tc) => {\n return {\n functionCall: {\n name: tc.name,\n args: tc.args,\n },\n };\n });\n }\n\n return [...messageParts, ...functionCalls];\n}\n\nexport function convertBaseMessagesToContent(\n messages: BaseMessage[],\n isMultimodalModel: boolean,\n convertSystemMessageToHumanContent: boolean = false\n): Content[] {\n return messages.reduce<{\n content: Content[];\n mergeWithPreviousContent: boolean;\n }>(\n (acc, message, index) => {\n if (!isBaseMessage(message)) {\n throw new Error('Unsupported message input');\n }\n const author = getMessageAuthor(message);\n if (author === 'system' && index !== 0) {\n throw new Error('System message should be the first one');\n }\n const role = convertAuthorToRole(author);\n\n const prevContent = acc.content[acc.content.length];\n if (\n !acc.mergeWithPreviousContent &&\n prevContent &&\n prevContent.role === role\n ) {\n throw new Error(\n 'Google Generative AI requires alternate messages between authors'\n );\n }\n\n const parts = convertMessageContentToParts(\n message,\n isMultimodalModel,\n messages.slice(0, index)\n );\n\n if (acc.mergeWithPreviousContent) {\n const prevContent = acc.content[acc.content.length - 1];\n if (!prevContent) {\n throw new Error(\n 'There was a problem parsing your system message. Please try a prompt without one.'\n );\n }\n prevContent.parts.push(...parts);\n\n return {\n mergeWithPreviousContent: false,\n content: acc.content,\n };\n }\n let actualRole = role;\n if (\n actualRole === 'function' ||\n (actualRole === 'system' && !convertSystemMessageToHumanContent)\n ) {\n // GenerativeAI API will throw an error if the role is not \"user\" or \"model.\"\n actualRole = 'user';\n }\n const content: Content = {\n role: actualRole,\n parts,\n };\n return {\n mergeWithPreviousContent:\n author === 'system' && !convertSystemMessageToHumanContent,\n content: [...acc.content, content],\n };\n },\n { content: [], mergeWithPreviousContent: false }\n ).content;\n}\n\nexport function convertResponseContentToChatGenerationChunk(\n response: EnhancedGenerateContentResponse,\n extra: {\n usageMetadata?: UsageMetadata | undefined;\n index: number;\n }\n): ChatGenerationChunk | null {\n if (!response.candidates || response.candidates.length === 0) {\n return null;\n }\n const functionCalls = response.functionCalls();\n const [candidate] = response.candidates;\n const { content: candidateContent, ...generationInfo } = candidate;\n let content: MessageContent | undefined;\n // Checks if some parts do not have text. If false, it means that the content is a string.\n const reasoningParts: string[] = [];\n if (\n Array.isArray(candidateContent.parts) &&\n candidateContent.parts.every((p) => 'text' in p)\n ) {\n // content = candidateContent.parts.map((p) => p.text).join('');\n const textParts: string[] = [];\n for (const part of candidateContent.parts) {\n if ('thought' in part && part.thought === true) {\n reasoningParts.push(part.text ?? '');\n continue;\n }\n textParts.push(part.text ?? '');\n }\n content = textParts.join('');\n } else if (Array.isArray(candidateContent.parts)) {\n content = candidateContent.parts.map((p) => {\n if ('text' in p && 'thought' in p && p.thought === true) {\n reasoningParts.push(p.text ?? '');\n } else if ('text' in p) {\n return {\n type: 'text',\n text: p.text,\n };\n } else if ('executableCode' in p) {\n return {\n type: 'executableCode',\n executableCode: p.executableCode,\n };\n } else if ('codeExecutionResult' in p) {\n return {\n type: 'codeExecutionResult',\n codeExecutionResult: p.codeExecutionResult,\n };\n }\n return p;\n });\n } else {\n // no content returned - likely due to abnormal stop reason, e.g. malformed function call\n content = [];\n }\n\n let text = '';\n if (typeof content === 'string' && content) {\n text = content;\n } else if (Array.isArray(content)) {\n const block = content.find((b) => 'text' in b) as\n | { text: string }\n | undefined;\n text = block?.text ?? '';\n }\n\n const toolCallChunks: ToolCallChunk[] = [];\n if (functionCalls) {\n toolCallChunks.push(\n ...functionCalls.map((fc) => ({\n ...fc,\n args: JSON.stringify(fc.args),\n index: extra.index,\n type: 'tool_call_chunk' as const,\n id: 'id' in fc && typeof fc.id === 'string' ? fc.id : uuidv4(),\n }))\n );\n }\n\n const additional_kwargs: ChatGeneration['message']['additional_kwargs'] = {};\n if (reasoningParts.length > 0) {\n additional_kwargs.reasoning = reasoningParts.join('');\n }\n\n return new ChatGenerationChunk({\n text,\n message: new AIMessageChunk({\n content: content || '',\n name: !candidateContent ? undefined : candidateContent.role,\n tool_call_chunks: toolCallChunks,\n // Each chunk can have unique \"generationInfo\", and merging strategy is unclear,\n // so leave blank for now.\n additional_kwargs,\n usage_metadata: extra.usageMetadata,\n }),\n generationInfo,\n });\n}\n\nexport function convertToGenerativeAITools(\n tools: GoogleGenerativeAIToolType[]\n): GoogleGenerativeAIFunctionDeclarationsTool[] {\n if (\n tools.every(\n (tool) =>\n 'functionDeclarations' in tool &&\n Array.isArray(tool.functionDeclarations)\n )\n ) {\n return tools as GoogleGenerativeAIFunctionDeclarationsTool[];\n }\n return [\n {\n functionDeclarations: tools.map(\n (tool): GenerativeAIFunctionDeclaration => {\n if (isLangChainTool(tool)) {\n const jsonSchema = schemaToGenerativeAIParameters(tool.schema);\n if (\n jsonSchema.type === 'object' &&\n 'properties' in jsonSchema &&\n Object.keys(jsonSchema.properties).length === 0\n ) {\n return {\n name: tool.name,\n description: tool.description,\n };\n }\n return {\n name: tool.name,\n description: tool.description,\n parameters: jsonSchema,\n };\n }\n if (isOpenAITool(tool)) {\n return {\n name: tool.function.name,\n description:\n tool.function.description ?? 'A function available to call.',\n parameters: jsonSchemaToGeminiParameters(\n tool.function.parameters\n ),\n };\n }\n return tool as unknown as GenerativeAIFunctionDeclaration;\n }\n ),\n },\n ];\n}\n"],"names":["uuidv4"],"mappings":";;;;;;;;AAyCM,SAAU,gBAAgB,CAAC,OAAoB,EAAA;AACnD,IAAA,MAAM,IAAI,GAAG,OAAO,CAAC,QAAQ,EAAE;AAC/B,IAAA,IAAI,WAAW,CAAC,UAAU,CAAC,OAAO,CAAC,EAAE;QACnC,OAAO,OAAO,CAAC,IAAI;;AAErB,IAAA,IAAI,IAAI,KAAK,MAAM,EAAE;AACnB,QAAA,OAAO,IAAI;;AAEb,IAAA,OAAO,OAAO,CAAC,IAAI,IAAI,IAAI;AAC7B;AAEA;;;;;AAKG;AACG,SAAU,mBAAmB,CACjC,MAAc,EAAA;IAEd,QAAQ,MAAM;AACd;;;AAGO;AACP,QAAA,KAAK,YAAY;AACjB,QAAA,KAAK,IAAI;QACT,KAAK,OAAO;AACV,YAAA,OAAO,OAAO;AAChB,QAAA,KAAK,QAAQ;AACX,YAAA,OAAO,QAAQ;AACjB,QAAA,KAAK,OAAO;AACV,YAAA,OAAO,MAAM;AACf,QAAA,KAAK,MAAM;AACX,QAAA,KAAK,UAAU;AACb,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AAC,CAAC,EAAE,MAAM;AAC5B,YAAA,GAAG,EAAE;YACL,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC,IAAI,CAAC;YAC7B,KAAK,EAAE,KAAK,CAAC,KAAK;AAClB,YAAA,IAAI,EAAE,iBAA0B;YAChC,EAAE,EAAE,IAAI,IAAI,EAAE,IAAI,OAAO,EAAE,CAAC,EAAE,KAAK,QAAQ,GAAG,EAAE,CAAC,EAAE,GAAGA,EAAM,EAAE;SAC/D,CAAC,CAAC,CACJ;;IAGH,MAAM,iBAAiB,GAAmD,EAAE;AAC5E,IAAA,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC,EAAE;QAC7B,iBAAiB,CAAC,SAAS,GAAG,cAAc,CAAC,IAAI,CAAC,EAAE,CAAC;;IAGvD,OAAO,IAAI,mBAAmB,CAAC;QAC7B,IAAI;QACJ,OAAO,EAAE,IAAI,cAAc,CAAC;YAC1B,OAAO,EAAE,OAAO,IAAI,EAAE;AACtB,YAAA,IAAI,EAAE,CAAC,gBAAgB,GAAG,SAAS,GAAG,gBAAgB,CAAC,IAAI;AAC3D,YAAA,gBAAgB,EAAE,cAAc;;;YAGhC,iBAAiB;YACjB,cAAc,EAAE,KAAK,CAAC,aAAa;SACpC,CAAC;QACF,cAAc;AACf,KAAA,CAAC;AACJ;;;;"}
@@ -207,11 +207,13 @@ hasToolCallChunks: ${hasToolCallChunks}
207
207
  });
208
208
  }
209
209
  else if (content.every((c) => (c.type?.startsWith(ContentTypes.THINKING) ?? false) ||
210
+ (c.type?.startsWith(ContentTypes.REASONING) ?? false) ||
210
211
  (c.type?.startsWith(ContentTypes.REASONING_CONTENT) ?? false))) {
211
212
  graph.dispatchReasoningDelta(stepId, {
212
213
  content: content.map((c) => ({
213
214
  type: ContentTypes.THINK,
214
215
  think: c.thinking ??
216
+ c.reasoning ??
215
217
  c.reasoningText?.text ??
216
218
  '',
217
219
  })),
@@ -221,8 +223,9 @@ hasToolCallChunks: ${hasToolCallChunks}
221
223
  handleReasoning(chunk, graph) {
222
224
  let reasoning_content = chunk.additional_kwargs?.[graph.reasoningKey];
223
225
  if (Array.isArray(chunk.content) &&
224
- (chunk.content[0]?.type === 'thinking' ||
225
- chunk.content[0]?.type === 'reasoning_content')) {
226
+ (chunk.content[0]?.type === ContentTypes.THINKING ||
227
+ chunk.content[0]?.type === ContentTypes.REASONING ||
228
+ chunk.content[0]?.type === ContentTypes.REASONING_CONTENT)) {
226
229
  reasoning_content = 'valid';
227
230
  }
228
231
  if (reasoning_content != null &&