illuma-agents 1.0.7 → 1.0.9

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 (237) hide show
  1. package/LICENSE +1 -5
  2. package/dist/cjs/common/enum.cjs +1 -2
  3. package/dist/cjs/common/enum.cjs.map +1 -1
  4. package/dist/cjs/instrumentation.cjs.map +1 -1
  5. package/dist/cjs/llm/anthropic/types.cjs.map +1 -1
  6. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +79 -2
  7. package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
  8. package/dist/cjs/llm/anthropic/utils/tools.cjs.map +1 -1
  9. package/dist/cjs/llm/bedrock/index.cjs +99 -0
  10. package/dist/cjs/llm/bedrock/index.cjs.map +1 -0
  11. package/dist/cjs/llm/fake.cjs.map +1 -1
  12. package/dist/cjs/llm/google/index.cjs +78 -9
  13. package/dist/cjs/llm/google/index.cjs.map +1 -1
  14. package/dist/cjs/llm/google/utils/common.cjs +185 -28
  15. package/dist/cjs/llm/google/utils/common.cjs.map +1 -1
  16. package/dist/cjs/llm/providers.cjs +13 -16
  17. package/dist/cjs/llm/providers.cjs.map +1 -1
  18. package/dist/cjs/llm/text.cjs.map +1 -1
  19. package/dist/cjs/messages/core.cjs +14 -14
  20. package/dist/cjs/messages/core.cjs.map +1 -1
  21. package/dist/cjs/messages/ids.cjs.map +1 -1
  22. package/dist/cjs/messages/prune.cjs.map +1 -1
  23. package/dist/cjs/run.cjs +10 -1
  24. package/dist/cjs/run.cjs.map +1 -1
  25. package/dist/cjs/splitStream.cjs.map +1 -1
  26. package/dist/cjs/stream.cjs +4 -1
  27. package/dist/cjs/stream.cjs.map +1 -1
  28. package/dist/cjs/tools/ToolNode.cjs +163 -55
  29. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  30. package/dist/cjs/tools/handlers.cjs +29 -25
  31. package/dist/cjs/tools/handlers.cjs.map +1 -1
  32. package/dist/cjs/tools/search/anthropic.cjs.map +1 -1
  33. package/dist/cjs/tools/search/content.cjs.map +1 -1
  34. package/dist/cjs/tools/search/firecrawl.cjs.map +1 -1
  35. package/dist/cjs/tools/search/format.cjs.map +1 -1
  36. package/dist/cjs/tools/search/highlights.cjs.map +1 -1
  37. package/dist/cjs/tools/search/rerankers.cjs.map +1 -1
  38. package/dist/cjs/tools/search/schema.cjs +25 -25
  39. package/dist/cjs/tools/search/schema.cjs.map +1 -1
  40. package/dist/cjs/tools/search/search.cjs +6 -1
  41. package/dist/cjs/tools/search/search.cjs.map +1 -1
  42. package/dist/cjs/tools/search/serper-scraper.cjs.map +1 -1
  43. package/dist/cjs/tools/search/tool.cjs +162 -35
  44. package/dist/cjs/tools/search/tool.cjs.map +1 -1
  45. package/dist/cjs/tools/search/utils.cjs.map +1 -1
  46. package/dist/cjs/utils/graph.cjs.map +1 -1
  47. package/dist/cjs/utils/llm.cjs +0 -1
  48. package/dist/cjs/utils/llm.cjs.map +1 -1
  49. package/dist/cjs/utils/misc.cjs.map +1 -1
  50. package/dist/cjs/utils/run.cjs.map +1 -1
  51. package/dist/cjs/utils/title.cjs +7 -7
  52. package/dist/cjs/utils/title.cjs.map +1 -1
  53. package/dist/esm/common/enum.mjs +1 -2
  54. package/dist/esm/common/enum.mjs.map +1 -1
  55. package/dist/esm/instrumentation.mjs.map +1 -1
  56. package/dist/esm/llm/anthropic/types.mjs.map +1 -1
  57. package/dist/esm/llm/anthropic/utils/message_inputs.mjs +79 -2
  58. package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
  59. package/dist/esm/llm/anthropic/utils/tools.mjs.map +1 -1
  60. package/dist/esm/llm/bedrock/index.mjs +97 -0
  61. package/dist/esm/llm/bedrock/index.mjs.map +1 -0
  62. package/dist/esm/llm/fake.mjs.map +1 -1
  63. package/dist/esm/llm/google/index.mjs +79 -10
  64. package/dist/esm/llm/google/index.mjs.map +1 -1
  65. package/dist/esm/llm/google/utils/common.mjs +184 -30
  66. package/dist/esm/llm/google/utils/common.mjs.map +1 -1
  67. package/dist/esm/llm/providers.mjs +2 -5
  68. package/dist/esm/llm/providers.mjs.map +1 -1
  69. package/dist/esm/llm/text.mjs.map +1 -1
  70. package/dist/esm/messages/core.mjs +14 -14
  71. package/dist/esm/messages/core.mjs.map +1 -1
  72. package/dist/esm/messages/ids.mjs.map +1 -1
  73. package/dist/esm/messages/prune.mjs.map +1 -1
  74. package/dist/esm/run.mjs +10 -1
  75. package/dist/esm/run.mjs.map +1 -1
  76. package/dist/esm/splitStream.mjs.map +1 -1
  77. package/dist/esm/stream.mjs +4 -1
  78. package/dist/esm/stream.mjs.map +1 -1
  79. package/dist/esm/tools/ToolNode.mjs +164 -56
  80. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  81. package/dist/esm/tools/handlers.mjs +30 -26
  82. package/dist/esm/tools/handlers.mjs.map +1 -1
  83. package/dist/esm/tools/search/anthropic.mjs.map +1 -1
  84. package/dist/esm/tools/search/content.mjs.map +1 -1
  85. package/dist/esm/tools/search/firecrawl.mjs.map +1 -1
  86. package/dist/esm/tools/search/format.mjs.map +1 -1
  87. package/dist/esm/tools/search/highlights.mjs.map +1 -1
  88. package/dist/esm/tools/search/rerankers.mjs.map +1 -1
  89. package/dist/esm/tools/search/schema.mjs +25 -25
  90. package/dist/esm/tools/search/schema.mjs.map +1 -1
  91. package/dist/esm/tools/search/search.mjs +6 -1
  92. package/dist/esm/tools/search/search.mjs.map +1 -1
  93. package/dist/esm/tools/search/serper-scraper.mjs.map +1 -1
  94. package/dist/esm/tools/search/tool.mjs +162 -35
  95. package/dist/esm/tools/search/tool.mjs.map +1 -1
  96. package/dist/esm/tools/search/utils.mjs.map +1 -1
  97. package/dist/esm/utils/graph.mjs.map +1 -1
  98. package/dist/esm/utils/llm.mjs +0 -1
  99. package/dist/esm/utils/llm.mjs.map +1 -1
  100. package/dist/esm/utils/misc.mjs.map +1 -1
  101. package/dist/esm/utils/run.mjs.map +1 -1
  102. package/dist/esm/utils/title.mjs +7 -7
  103. package/dist/esm/utils/title.mjs.map +1 -1
  104. package/dist/types/common/enum.d.ts +1 -2
  105. package/dist/types/llm/bedrock/index.d.ts +36 -0
  106. package/dist/types/llm/google/index.d.ts +10 -0
  107. package/dist/types/llm/google/types.d.ts +11 -1
  108. package/dist/types/llm/google/utils/common.d.ts +17 -2
  109. package/dist/types/tools/ToolNode.d.ts +9 -1
  110. package/dist/types/tools/search/types.d.ts +2 -0
  111. package/dist/types/types/llm.d.ts +3 -8
  112. package/dist/types/types/tools.d.ts +1 -1
  113. package/package.json +15 -11
  114. package/src/common/enum.ts +1 -2
  115. package/src/common/index.ts +1 -1
  116. package/src/instrumentation.ts +22 -22
  117. package/src/llm/anthropic/llm.spec.ts +1442 -1442
  118. package/src/llm/anthropic/types.ts +140 -140
  119. package/src/llm/anthropic/utils/message_inputs.ts +757 -660
  120. package/src/llm/anthropic/utils/output_parsers.ts +133 -133
  121. package/src/llm/anthropic/utils/tools.ts +29 -29
  122. package/src/llm/bedrock/index.ts +128 -0
  123. package/src/llm/fake.ts +133 -133
  124. package/src/llm/google/data/gettysburg10.wav +0 -0
  125. package/src/llm/google/data/hotdog.jpg +0 -0
  126. package/src/llm/google/index.ts +129 -14
  127. package/src/llm/google/llm.spec.ts +932 -0
  128. package/src/llm/google/types.ts +56 -43
  129. package/src/llm/google/utils/common.ts +873 -660
  130. package/src/llm/google/utils/tools.ts +160 -160
  131. package/src/llm/openai/types.ts +24 -24
  132. package/src/llm/openai/utils/isReasoningModel.test.ts +90 -90
  133. package/src/llm/providers.ts +2 -7
  134. package/src/llm/text.ts +94 -94
  135. package/src/messages/core.ts +463 -463
  136. package/src/messages/formatAgentMessages.tools.test.ts +400 -400
  137. package/src/messages/formatMessage.test.ts +693 -693
  138. package/src/messages/ids.ts +26 -26
  139. package/src/messages/prune.ts +567 -567
  140. package/src/messages/shiftIndexTokenCountMap.test.ts +81 -81
  141. package/src/mockStream.ts +98 -98
  142. package/src/prompts/collab.ts +5 -5
  143. package/src/prompts/index.ts +1 -1
  144. package/src/prompts/taskmanager.ts +61 -61
  145. package/src/run.ts +13 -4
  146. package/src/scripts/ant_web_search_edge_case.ts +162 -0
  147. package/src/scripts/ant_web_search_error_edge_case.ts +148 -0
  148. package/src/scripts/args.ts +48 -48
  149. package/src/scripts/caching.ts +123 -123
  150. package/src/scripts/code_exec_files.ts +193 -193
  151. package/src/scripts/empty_input.ts +137 -137
  152. package/src/scripts/image.ts +178 -178
  153. package/src/scripts/memory.ts +97 -97
  154. package/src/scripts/thinking.ts +149 -149
  155. package/src/specs/anthropic.simple.test.ts +67 -0
  156. package/src/specs/spec.utils.ts +3 -3
  157. package/src/specs/token-distribution-edge-case.test.ts +316 -316
  158. package/src/specs/tool-error.test.ts +193 -193
  159. package/src/splitStream.test.ts +691 -691
  160. package/src/splitStream.ts +234 -234
  161. package/src/stream.test.ts +94 -94
  162. package/src/stream.ts +4 -1
  163. package/src/tools/ToolNode.ts +206 -64
  164. package/src/tools/handlers.ts +32 -28
  165. package/src/tools/search/anthropic.ts +51 -51
  166. package/src/tools/search/content.test.ts +173 -173
  167. package/src/tools/search/content.ts +147 -147
  168. package/src/tools/search/direct-url.test.ts +530 -0
  169. package/src/tools/search/firecrawl.ts +210 -210
  170. package/src/tools/search/format.ts +250 -250
  171. package/src/tools/search/highlights.ts +320 -320
  172. package/src/tools/search/index.ts +2 -2
  173. package/src/tools/search/jina-reranker.test.ts +126 -126
  174. package/src/tools/search/output.md +2775 -2775
  175. package/src/tools/search/rerankers.ts +242 -242
  176. package/src/tools/search/schema.ts +63 -63
  177. package/src/tools/search/search.ts +766 -759
  178. package/src/tools/search/serper-scraper.ts +155 -155
  179. package/src/tools/search/test.html +883 -883
  180. package/src/tools/search/test.md +642 -642
  181. package/src/tools/search/test.ts +159 -159
  182. package/src/tools/search/tool.ts +619 -471
  183. package/src/tools/search/types.ts +689 -687
  184. package/src/tools/search/utils.ts +79 -79
  185. package/src/types/index.ts +6 -6
  186. package/src/types/llm.ts +2 -8
  187. package/src/types/tools.ts +80 -80
  188. package/src/utils/graph.ts +10 -10
  189. package/src/utils/llm.ts +26 -27
  190. package/src/utils/llmConfig.ts +5 -3
  191. package/src/utils/logging.ts +48 -48
  192. package/src/utils/misc.ts +57 -57
  193. package/src/utils/run.ts +100 -100
  194. package/src/utils/title.ts +165 -165
  195. package/dist/cjs/llm/ollama/index.cjs +0 -70
  196. package/dist/cjs/llm/ollama/index.cjs.map +0 -1
  197. package/dist/cjs/llm/ollama/utils.cjs +0 -158
  198. package/dist/cjs/llm/ollama/utils.cjs.map +0 -1
  199. package/dist/esm/llm/ollama/index.mjs +0 -68
  200. package/dist/esm/llm/ollama/index.mjs.map +0 -1
  201. package/dist/esm/llm/ollama/utils.mjs +0 -155
  202. package/dist/esm/llm/ollama/utils.mjs.map +0 -1
  203. package/dist/types/llm/ollama/index.d.ts +0 -8
  204. package/dist/types/llm/ollama/utils.d.ts +0 -7
  205. package/src/llm/ollama/index.ts +0 -92
  206. package/src/llm/ollama/utils.ts +0 -193
  207. package/src/proto/CollabGraph.ts +0 -269
  208. package/src/proto/TaskManager.ts +0 -243
  209. package/src/proto/collab.ts +0 -200
  210. package/src/proto/collab_design.ts +0 -184
  211. package/src/proto/collab_design_v2.ts +0 -224
  212. package/src/proto/collab_design_v3.ts +0 -255
  213. package/src/proto/collab_design_v4.ts +0 -220
  214. package/src/proto/collab_design_v5.ts +0 -251
  215. package/src/proto/collab_graph.ts +0 -181
  216. package/src/proto/collab_original.ts +0 -123
  217. package/src/proto/example.ts +0 -93
  218. package/src/proto/example_new.ts +0 -68
  219. package/src/proto/example_old.ts +0 -201
  220. package/src/proto/example_test.ts +0 -152
  221. package/src/proto/example_test_anthropic.ts +0 -100
  222. package/src/proto/log_stream.ts +0 -202
  223. package/src/proto/main_collab_community_event.ts +0 -133
  224. package/src/proto/main_collab_design_v2.ts +0 -96
  225. package/src/proto/main_collab_design_v4.ts +0 -100
  226. package/src/proto/main_collab_design_v5.ts +0 -135
  227. package/src/proto/main_collab_global_analysis.ts +0 -122
  228. package/src/proto/main_collab_hackathon_event.ts +0 -153
  229. package/src/proto/main_collab_space_mission.ts +0 -153
  230. package/src/proto/main_philosophy.ts +0 -210
  231. package/src/proto/original_script.ts +0 -126
  232. package/src/proto/standard.ts +0 -100
  233. package/src/proto/stream.ts +0 -56
  234. package/src/proto/tasks.ts +0 -118
  235. package/src/proto/tools/global_analysis_tools.ts +0 -86
  236. package/src/proto/tools/space_mission_tools.ts +0 -60
  237. package/src/proto/vertexai.ts +0 -54
@@ -1,660 +1,873 @@
1
- import {
2
- POSSIBLE_ROLES,
3
- type Part,
4
- type Content,
5
- type TextPart,
6
- type FileDataPart,
7
- type InlineDataPart,
8
- type FunctionCallPart,
9
- type GenerateContentCandidate,
10
- type EnhancedGenerateContentResponse,
11
- type FunctionDeclaration as GenerativeAIFunctionDeclaration,
12
- type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
13
- } from '@google/generative-ai';
14
- import {
15
- AIMessageChunk,
16
- BaseMessage,
17
- ChatMessage,
18
- ToolMessage,
19
- ToolMessageChunk,
20
- MessageContent,
21
- MessageContentComplex,
22
- UsageMetadata,
23
- isAIMessage,
24
- isBaseMessage,
25
- isToolMessage,
26
- StandardContentBlockConverter,
27
- parseBase64DataUrl,
28
- convertToProviderContentBlock,
29
- isDataContentBlock,
30
- } from '@langchain/core/messages';
31
- import { ChatGenerationChunk } from '@langchain/core/outputs';
32
- import type { ChatGeneration } from '@langchain/core/outputs';
33
- import { isLangChainTool } from '@langchain/core/utils/function_calling';
34
- import { isOpenAITool } from '@langchain/core/language_models/base';
35
- import { ToolCallChunk } from '@langchain/core/messages/tool';
36
- import { v4 as uuidv4 } from 'uuid';
37
- import {
38
- jsonSchemaToGeminiParameters,
39
- schemaToGenerativeAIParameters,
40
- } from './zod_to_genai_parameters';
41
- import { GoogleGenerativeAIToolType } from '../types';
42
-
43
- export function getMessageAuthor(message: BaseMessage): string {
44
- const type = message._getType();
45
- if (ChatMessage.isInstance(message)) {
46
- return message.role;
47
- }
48
- if (type === 'tool') {
49
- return type;
50
- }
51
- return message.name ?? type;
52
- }
53
-
54
- /**
55
- * Maps a message type to a Google Generative AI chat author.
56
- * @param message The message to map.
57
- * @param model The model to use for mapping.
58
- * @returns The message type mapped to a Google Generative AI chat author.
59
- */
60
- export function convertAuthorToRole(
61
- author: string
62
- ): (typeof POSSIBLE_ROLES)[number] {
63
- switch (author) {
64
- /**
65
- * Note: Gemini currently is not supporting system messages
66
- * we will convert them to human messages and merge with following
67
- * */
68
- case 'supervisor':
69
- case 'ai':
70
- case 'model': // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
71
- return 'model';
72
- case 'system':
73
- return 'system';
74
- case 'human':
75
- return 'user';
76
- case 'tool':
77
- case 'function':
78
- return 'function';
79
- default:
80
- throw new Error(`Unknown / unsupported author: ${author}`);
81
- }
82
- }
83
-
84
- function messageContentMedia(content: MessageContentComplex): Part {
85
- if ('mimeType' in content && 'data' in content) {
86
- return {
87
- inlineData: {
88
- mimeType: content.mimeType,
89
- data: content.data,
90
- },
91
- };
92
- }
93
- if ('mimeType' in content && 'fileUri' in content) {
94
- return {
95
- fileData: {
96
- mimeType: content.mimeType,
97
- fileUri: content.fileUri,
98
- },
99
- };
100
- }
101
-
102
- throw new Error('Invalid media content');
103
- }
104
-
105
- function inferToolNameFromPreviousMessages(
106
- message: ToolMessage | ToolMessageChunk,
107
- previousMessages: BaseMessage[]
108
- ): string | undefined {
109
- return previousMessages
110
- .map((msg) => {
111
- if (isAIMessage(msg)) {
112
- return msg.tool_calls ?? [];
113
- }
114
- return [];
115
- })
116
- .flat()
117
- .find((toolCall) => {
118
- return toolCall.id === message.tool_call_id;
119
- })?.name;
120
- }
121
-
122
- function _getStandardContentBlockConverter(
123
- isMultimodalModel: boolean
124
- ): StandardContentBlockConverter<{
125
- text: TextPart;
126
- image: FileDataPart | InlineDataPart;
127
- audio: FileDataPart | InlineDataPart;
128
- file: FileDataPart | InlineDataPart | TextPart;
129
- }> {
130
- const standardContentBlockConverter: StandardContentBlockConverter<{
131
- text: TextPart;
132
- image: FileDataPart | InlineDataPart;
133
- audio: FileDataPart | InlineDataPart;
134
- file: FileDataPart | InlineDataPart | TextPart;
135
- }> = {
136
- providerName: 'Google Gemini',
137
-
138
- fromStandardTextBlock(block) {
139
- return {
140
- text: block.text,
141
- };
142
- },
143
-
144
- fromStandardImageBlock(block): FileDataPart | InlineDataPart {
145
- if (!isMultimodalModel) {
146
- throw new Error('This model does not support images');
147
- }
148
- if (block.source_type === 'url') {
149
- const data = parseBase64DataUrl({ dataUrl: block.url });
150
- if (data) {
151
- return {
152
- inlineData: {
153
- mimeType: data.mime_type,
154
- data: data.data,
155
- },
156
- };
157
- } else {
158
- return {
159
- fileData: {
160
- mimeType: block.mime_type ?? '',
161
- fileUri: block.url,
162
- },
163
- };
164
- }
165
- }
166
-
167
- if (block.source_type === 'base64') {
168
- return {
169
- inlineData: {
170
- mimeType: block.mime_type ?? '',
171
- data: block.data,
172
- },
173
- };
174
- }
175
-
176
- throw new Error(`Unsupported source type: ${block.source_type}`);
177
- },
178
-
179
- fromStandardAudioBlock(block): FileDataPart | InlineDataPart {
180
- if (!isMultimodalModel) {
181
- throw new Error('This model does not support audio');
182
- }
183
- if (block.source_type === 'url') {
184
- const data = parseBase64DataUrl({ dataUrl: block.url });
185
- if (data) {
186
- return {
187
- inlineData: {
188
- mimeType: data.mime_type,
189
- data: data.data,
190
- },
191
- };
192
- } else {
193
- return {
194
- fileData: {
195
- mimeType: block.mime_type ?? '',
196
- fileUri: block.url,
197
- },
198
- };
199
- }
200
- }
201
-
202
- if (block.source_type === 'base64') {
203
- return {
204
- inlineData: {
205
- mimeType: block.mime_type ?? '',
206
- data: block.data,
207
- },
208
- };
209
- }
210
-
211
- throw new Error(`Unsupported source type: ${block.source_type}`);
212
- },
213
-
214
- fromStandardFileBlock(block): FileDataPart | InlineDataPart | TextPart {
215
- if (!isMultimodalModel) {
216
- throw new Error('This model does not support files');
217
- }
218
- if (block.source_type === 'text') {
219
- return {
220
- text: block.text,
221
- };
222
- }
223
- if (block.source_type === 'url') {
224
- const data = parseBase64DataUrl({ dataUrl: block.url });
225
- if (data) {
226
- return {
227
- inlineData: {
228
- mimeType: data.mime_type,
229
- data: data.data,
230
- },
231
- };
232
- } else {
233
- return {
234
- fileData: {
235
- mimeType: block.mime_type ?? '',
236
- fileUri: block.url,
237
- },
238
- };
239
- }
240
- }
241
-
242
- if (block.source_type === 'base64') {
243
- return {
244
- inlineData: {
245
- mimeType: block.mime_type ?? '',
246
- data: block.data,
247
- },
248
- };
249
- }
250
- throw new Error(`Unsupported source type: ${block.source_type}`);
251
- },
252
- };
253
- return standardContentBlockConverter;
254
- }
255
-
256
- function _convertLangChainContentToPart(
257
- content: MessageContentComplex,
258
- isMultimodalModel: boolean
259
- ): Part | undefined {
260
- if (isDataContentBlock(content)) {
261
- return convertToProviderContentBlock(
262
- content,
263
- _getStandardContentBlockConverter(isMultimodalModel)
264
- );
265
- }
266
-
267
- if (content.type === 'text') {
268
- return { text: content.text };
269
- } else if (content.type === 'executableCode') {
270
- return { executableCode: content.executableCode };
271
- } else if (content.type === 'codeExecutionResult') {
272
- return { codeExecutionResult: content.codeExecutionResult };
273
- } else if (content.type === 'image_url') {
274
- if (!isMultimodalModel) {
275
- throw new Error('This model does not support images');
276
- }
277
- let source: string;
278
- if (typeof content.image_url === 'string') {
279
- source = content.image_url;
280
- } else if (
281
- typeof content.image_url === 'object' &&
282
- 'url' in content.image_url
283
- ) {
284
- source = content.image_url.url;
285
- } else {
286
- throw new Error('Please provide image as base64 encoded data URL');
287
- }
288
- const [dm, data] = source.split(',');
289
- if (!dm.startsWith('data:')) {
290
- throw new Error('Please provide image as base64 encoded data URL');
291
- }
292
-
293
- const [mimeType, encoding] = dm.replace(/^data:/, '').split(';');
294
- if (encoding !== 'base64') {
295
- throw new Error('Please provide image as base64 encoded data URL');
296
- }
297
-
298
- return {
299
- inlineData: {
300
- data,
301
- mimeType,
302
- },
303
- };
304
- } else if (
305
- content.type === 'document' ||
306
- content.type === 'audio' ||
307
- content.type === 'video'
308
- ) {
309
- if (!isMultimodalModel) {
310
- throw new Error(`This model does not support ${content.type}s`);
311
- }
312
- return {
313
- inlineData: {
314
- data: content.data,
315
- mimeType: content.mimeType,
316
- },
317
- };
318
- } else if (content.type === 'media') {
319
- return messageContentMedia(content);
320
- } else if (content.type === 'tool_use') {
321
- return {
322
- functionCall: {
323
- name: content.name,
324
- args: content.input,
325
- },
326
- };
327
- } else if (
328
- content.type?.includes('/') === true &&
329
- // Ensure it's a single slash.
330
- content.type.split('/').length === 2 &&
331
- 'data' in content &&
332
- typeof content.data === 'string'
333
- ) {
334
- return {
335
- inlineData: {
336
- mimeType: content.type,
337
- data: content.data,
338
- },
339
- };
340
- } else if ('functionCall' in content) {
341
- // No action needed here function calls will be added later from message.tool_calls
342
- return undefined;
343
- } else {
344
- if ('type' in content) {
345
- throw new Error(`Unknown content type ${content.type}`);
346
- } else {
347
- throw new Error(`Unknown content ${JSON.stringify(content)}`);
348
- }
349
- }
350
- }
351
-
352
- export function convertMessageContentToParts(
353
- message: BaseMessage,
354
- isMultimodalModel: boolean,
355
- previousMessages: BaseMessage[]
356
- ): Part[] {
357
- if (isToolMessage(message)) {
358
- const messageName =
359
- message.name ??
360
- inferToolNameFromPreviousMessages(message, previousMessages);
361
- if (messageName === undefined) {
362
- throw new Error(
363
- `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.`
364
- );
365
- }
366
-
367
- const result = Array.isArray(message.content)
368
- ? (message.content
369
- .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
370
- .filter((p) => p !== undefined) as Part[])
371
- : message.content;
372
-
373
- if (message.status === 'error') {
374
- return [
375
- {
376
- functionResponse: {
377
- name: messageName,
378
- // The API expects an object with an `error` field if the function call fails.
379
- // `error` must be a valid object (not a string or array), so we wrap `message.content` here
380
- response: { error: { details: result } },
381
- },
382
- },
383
- ];
384
- }
385
-
386
- return [
387
- {
388
- functionResponse: {
389
- name: messageName,
390
- // again, can't have a string or array value for `response`, so we wrap it as an object here
391
- response: { result },
392
- },
393
- },
394
- ];
395
- }
396
-
397
- let functionCalls: FunctionCallPart[] = [];
398
- const messageParts: Part[] = [];
399
-
400
- if (typeof message.content === 'string' && message.content) {
401
- messageParts.push({ text: message.content });
402
- }
403
-
404
- if (Array.isArray(message.content)) {
405
- messageParts.push(
406
- ...(message.content
407
- .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
408
- .filter((p) => p !== undefined) as Part[])
409
- );
410
- }
411
-
412
- if (isAIMessage(message) && message.tool_calls?.length != null) {
413
- functionCalls = message.tool_calls.map((tc) => {
414
- return {
415
- functionCall: {
416
- name: tc.name,
417
- args: tc.args,
418
- },
419
- };
420
- });
421
- }
422
-
423
- return [...messageParts, ...functionCalls];
424
- }
425
-
426
- export function convertBaseMessagesToContent(
427
- messages: BaseMessage[],
428
- isMultimodalModel: boolean,
429
- convertSystemMessageToHumanContent: boolean = false
430
- ): Content[] | undefined {
431
- return messages.reduce<{
432
- content: Content[] | undefined;
433
- mergeWithPreviousContent: boolean;
434
- }>(
435
- (acc, message, index) => {
436
- if (!isBaseMessage(message)) {
437
- throw new Error('Unsupported message input');
438
- }
439
- const author = getMessageAuthor(message);
440
- if (author === 'system' && index !== 0) {
441
- throw new Error('System message should be the first one');
442
- }
443
- const role = convertAuthorToRole(author);
444
-
445
- const prevContent = acc.content?.[acc.content.length];
446
- if (
447
- !acc.mergeWithPreviousContent &&
448
- prevContent &&
449
- prevContent.role === role
450
- ) {
451
- throw new Error(
452
- 'Google Generative AI requires alternate messages between authors'
453
- );
454
- }
455
-
456
- const parts = convertMessageContentToParts(
457
- message,
458
- isMultimodalModel,
459
- messages.slice(0, index)
460
- );
461
-
462
- if (acc.mergeWithPreviousContent) {
463
- const prevContent = acc.content?.[acc.content.length - 1];
464
- if (!prevContent) {
465
- throw new Error(
466
- 'There was a problem parsing your system message. Please try a prompt without one.'
467
- );
468
- }
469
- prevContent.parts.push(...parts);
470
-
471
- return {
472
- mergeWithPreviousContent: false,
473
- content: acc.content,
474
- };
475
- }
476
- let actualRole = role;
477
- if (
478
- actualRole === 'function' ||
479
- (actualRole === 'system' && !convertSystemMessageToHumanContent)
480
- ) {
481
- // GenerativeAI API will throw an error if the role is not "user" or "model."
482
- actualRole = 'user';
483
- }
484
- const content: Content = {
485
- role: actualRole,
486
- parts,
487
- };
488
- return {
489
- mergeWithPreviousContent:
490
- author === 'system' && !convertSystemMessageToHumanContent,
491
- content: [...(acc.content ?? []), content],
492
- };
493
- },
494
- { content: [], mergeWithPreviousContent: false }
495
- ).content;
496
- }
497
-
498
- export function convertResponseContentToChatGenerationChunk(
499
- response: EnhancedGenerateContentResponse,
500
- extra: {
501
- usageMetadata?: UsageMetadata | undefined;
502
- index: number;
503
- }
504
- ): ChatGenerationChunk | null {
505
- if (!response.candidates || response.candidates.length === 0) {
506
- return null;
507
- }
508
- const functionCalls = response.functionCalls();
509
- const [candidate] = response.candidates as [
510
- Partial<GenerateContentCandidate> | undefined,
511
- ];
512
- const { content: candidateContent, ...generationInfo } = candidate ?? {};
513
- let content: MessageContent | undefined;
514
- // Checks if some parts do not have text. If false, it means that the content is a string.
515
- const reasoningParts: string[] = [];
516
- if (
517
- candidateContent != null &&
518
- Array.isArray(candidateContent.parts) &&
519
- candidateContent.parts.every((p) => 'text' in p)
520
- ) {
521
- // content = candidateContent.parts.map((p) => p.text).join('');
522
- const textParts: string[] = [];
523
- for (const part of candidateContent.parts) {
524
- if ('thought' in part && part.thought === true) {
525
- reasoningParts.push(part.text ?? '');
526
- continue;
527
- }
528
- textParts.push(part.text ?? '');
529
- }
530
- content = textParts.join('');
531
- } else if (candidateContent && Array.isArray(candidateContent.parts)) {
532
- content = candidateContent.parts.map((p) => {
533
- if ('text' in p && 'thought' in p && p.thought === true) {
534
- reasoningParts.push(p.text ?? '');
535
- } else if ('text' in p) {
536
- return {
537
- type: 'text',
538
- text: p.text,
539
- };
540
- } else if ('executableCode' in p) {
541
- return {
542
- type: 'executableCode',
543
- executableCode: p.executableCode,
544
- };
545
- } else if ('codeExecutionResult' in p) {
546
- return {
547
- type: 'codeExecutionResult',
548
- codeExecutionResult: p.codeExecutionResult,
549
- };
550
- }
551
- return p;
552
- });
553
- } else {
554
- // no content returned - likely due to abnormal stop reason, e.g. malformed function call
555
- content = [];
556
- }
557
-
558
- let text = '';
559
- if (typeof content === 'string' && content) {
560
- text = content;
561
- } else if (Array.isArray(content)) {
562
- const block = content.find((b) => 'text' in b) as
563
- | { text: string }
564
- | undefined;
565
- text = block?.text ?? '';
566
- }
567
-
568
- const toolCallChunks: ToolCallChunk[] = [];
569
- if (functionCalls) {
570
- toolCallChunks.push(
571
- ...functionCalls.map((fc) => ({
572
- ...fc,
573
- args: JSON.stringify(fc.args),
574
- // Un-commenting this causes LangChain to incorrectly merge tool calls together
575
- // index: extra.index,
576
- type: 'tool_call_chunk' as const,
577
- id: 'id' in fc && typeof fc.id === 'string' ? fc.id : uuidv4(),
578
- }))
579
- );
580
- }
581
-
582
- const additional_kwargs: ChatGeneration['message']['additional_kwargs'] = {};
583
- if (reasoningParts.length > 0) {
584
- additional_kwargs.reasoning = reasoningParts.join('');
585
- }
586
-
587
- if (candidate?.groundingMetadata) {
588
- additional_kwargs.groundingMetadata = candidate.groundingMetadata;
589
- }
590
-
591
- const isFinalChunk =
592
- response.candidates[0]?.finishReason === 'STOP' ||
593
- response.candidates[0]?.finishReason === 'MAX_TOKENS' ||
594
- response.candidates[0]?.finishReason === 'SAFETY';
595
-
596
- return new ChatGenerationChunk({
597
- text,
598
- message: new AIMessageChunk({
599
- content: content,
600
- name: !candidateContent ? undefined : candidateContent.role,
601
- tool_call_chunks: toolCallChunks,
602
- // Each chunk can have unique "generationInfo", and merging strategy is unclear,
603
- // so leave blank for now.
604
- additional_kwargs,
605
- usage_metadata: isFinalChunk ? extra.usageMetadata : undefined,
606
- }),
607
- generationInfo,
608
- });
609
- }
610
-
611
- export function convertToGenerativeAITools(
612
- tools: GoogleGenerativeAIToolType[]
613
- ): GoogleGenerativeAIFunctionDeclarationsTool[] {
614
- if (
615
- tools.every(
616
- (tool) =>
617
- 'functionDeclarations' in tool &&
618
- Array.isArray(tool.functionDeclarations)
619
- )
620
- ) {
621
- return tools as GoogleGenerativeAIFunctionDeclarationsTool[];
622
- }
623
- return [
624
- {
625
- functionDeclarations: tools.map(
626
- (tool): GenerativeAIFunctionDeclaration => {
627
- if (isLangChainTool(tool)) {
628
- const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
629
- if (
630
- jsonSchema.type === 'object' &&
631
- 'properties' in jsonSchema &&
632
- Object.keys(jsonSchema.properties).length === 0
633
- ) {
634
- return {
635
- name: tool.name,
636
- description: tool.description,
637
- };
638
- }
639
- return {
640
- name: tool.name,
641
- description: tool.description,
642
- parameters: jsonSchema,
643
- };
644
- }
645
- if (isOpenAITool(tool)) {
646
- return {
647
- name: tool.function.name,
648
- description:
649
- tool.function.description ?? 'A function available to call.',
650
- parameters: jsonSchemaToGeminiParameters(
651
- tool.function.parameters
652
- ),
653
- };
654
- }
655
- return tool as unknown as GenerativeAIFunctionDeclaration;
656
- }
657
- ),
658
- },
659
- ];
660
- }
1
+ import {
2
+ POSSIBLE_ROLES,
3
+ type Part,
4
+ type Content,
5
+ type TextPart,
6
+ type FileDataPart,
7
+ type InlineDataPart,
8
+ type FunctionCallPart,
9
+ type GenerateContentCandidate,
10
+ type EnhancedGenerateContentResponse,
11
+ type FunctionDeclaration as GenerativeAIFunctionDeclaration,
12
+ type FunctionDeclarationsTool as GoogleGenerativeAIFunctionDeclarationsTool,
13
+ } from '@google/generative-ai';
14
+ import {
15
+ AIMessage,
16
+ AIMessageChunk,
17
+ BaseMessage,
18
+ ChatMessage,
19
+ ToolMessage,
20
+ ToolMessageChunk,
21
+ MessageContent,
22
+ MessageContentComplex,
23
+ UsageMetadata,
24
+ isAIMessage,
25
+ isBaseMessage,
26
+ isToolMessage,
27
+ StandardContentBlockConverter,
28
+ parseBase64DataUrl,
29
+ convertToProviderContentBlock,
30
+ isDataContentBlock,
31
+ } from '@langchain/core/messages';
32
+ import { ChatGenerationChunk } from '@langchain/core/outputs';
33
+ import type { ChatGeneration, ChatResult } from '@langchain/core/outputs';
34
+ import { isLangChainTool } from '@langchain/core/utils/function_calling';
35
+ import { isOpenAITool } from '@langchain/core/language_models/base';
36
+ import { ToolCallChunk } from '@langchain/core/messages/tool';
37
+ import { v4 as uuidv4 } from 'uuid';
38
+ import {
39
+ jsonSchemaToGeminiParameters,
40
+ schemaToGenerativeAIParameters,
41
+ } from './zod_to_genai_parameters';
42
+ import { GoogleGenerativeAIToolType } from '../types';
43
+
44
+ export const _FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY =
45
+ '__gemini_function_call_thought_signatures__';
46
+
47
+ const DUMMY_SIGNATURE =
48
+ 'ErYCCrMCAdHtim9kOoOkrPiCNVsmlpMIKd7ZMxgiFbVQOkgp7nlLcDMzVsZwIzvuT7nQROivoXA72ccC2lSDvR0Gh7dkWaGuj7ctv6t7ZceHnecx0QYa+ix8tYpRfjhyWozQ49lWiws6+YGjCt10KRTyWsZ2h6O7iHTYJwKIRwGUHRKy/qK/6kFxJm5ML00gLq4D8s5Z6DBpp2ZlR+uF4G8jJgeWQgyHWVdx2wGYElaceVAc66tZdPQRdOHpWtgYSI1YdaXgVI8KHY3/EfNc2YqqMIulvkDBAnuMhkAjV9xmBa54Tq+ih3Im4+r3DzqhGqYdsSkhS0kZMwte4Hjs65dZzCw9lANxIqYi1DJ639WNPYihp/DCJCos7o+/EeSPJaio5sgWDyUnMGkY1atsJZ+m7pj7DD5tvQ==';
49
+
50
+ /**
51
+ * Executes a function immediately and returns its result.
52
+ * Functional utility similar to an Immediately Invoked Function Expression (IIFE).
53
+ * @param fn The function to execute.
54
+ * @returns The result of invoking fn.
55
+ */
56
+ export const iife = <T>(fn: () => T): T => fn();
57
+
58
+ export function getMessageAuthor(message: BaseMessage): string {
59
+ const type = message._getType();
60
+ if (ChatMessage.isInstance(message)) {
61
+ return message.role;
62
+ }
63
+ if (type === 'tool') {
64
+ return type;
65
+ }
66
+ return message.name ?? type;
67
+ }
68
+
69
+ /**
70
+ * Maps a message type to a Google Generative AI chat author.
71
+ * @param message The message to map.
72
+ * @param model The model to use for mapping.
73
+ * @returns The message type mapped to a Google Generative AI chat author.
74
+ */
75
+ export function convertAuthorToRole(
76
+ author: string
77
+ ): (typeof POSSIBLE_ROLES)[number] {
78
+ switch (author) {
79
+ /**
80
+ * Note: Gemini currently is not supporting system messages
81
+ * we will convert them to human messages and merge with following
82
+ * */
83
+ case 'supervisor':
84
+ case 'ai':
85
+ case 'model': // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
86
+ return 'model';
87
+ case 'system':
88
+ return 'system';
89
+ case 'human':
90
+ return 'user';
91
+ case 'tool':
92
+ case 'function':
93
+ return 'function';
94
+ default:
95
+ throw new Error(`Unknown / unsupported author: ${author}`);
96
+ }
97
+ }
98
+
99
+ function messageContentMedia(content: MessageContentComplex): Part {
100
+ if ('mimeType' in content && 'data' in content) {
101
+ return {
102
+ inlineData: {
103
+ mimeType: content.mimeType,
104
+ data: content.data,
105
+ },
106
+ };
107
+ }
108
+ if ('mimeType' in content && 'fileUri' in content) {
109
+ return {
110
+ fileData: {
111
+ mimeType: content.mimeType,
112
+ fileUri: content.fileUri,
113
+ },
114
+ };
115
+ }
116
+
117
+ throw new Error('Invalid media content');
118
+ }
119
+
120
+ function inferToolNameFromPreviousMessages(
121
+ message: ToolMessage | ToolMessageChunk,
122
+ previousMessages: BaseMessage[]
123
+ ): string | undefined {
124
+ return previousMessages
125
+ .map((msg) => {
126
+ if (isAIMessage(msg)) {
127
+ return msg.tool_calls ?? [];
128
+ }
129
+ return [];
130
+ })
131
+ .flat()
132
+ .find((toolCall) => {
133
+ return toolCall.id === message.tool_call_id;
134
+ })?.name;
135
+ }
136
+
137
+ function _getStandardContentBlockConverter(
138
+ isMultimodalModel: boolean
139
+ ): StandardContentBlockConverter<{
140
+ text: TextPart;
141
+ image: FileDataPart | InlineDataPart;
142
+ audio: FileDataPart | InlineDataPart;
143
+ file: FileDataPart | InlineDataPart | TextPart;
144
+ }> {
145
+ const standardContentBlockConverter: StandardContentBlockConverter<{
146
+ text: TextPart;
147
+ image: FileDataPart | InlineDataPart;
148
+ audio: FileDataPart | InlineDataPart;
149
+ file: FileDataPart | InlineDataPart | TextPart;
150
+ }> = {
151
+ providerName: 'Google Gemini',
152
+
153
+ fromStandardTextBlock(block) {
154
+ return {
155
+ text: block.text,
156
+ };
157
+ },
158
+
159
+ fromStandardImageBlock(block): FileDataPart | InlineDataPart {
160
+ if (!isMultimodalModel) {
161
+ throw new Error('This model does not support images');
162
+ }
163
+ if (block.source_type === 'url') {
164
+ const data = parseBase64DataUrl({ dataUrl: block.url });
165
+ if (data) {
166
+ return {
167
+ inlineData: {
168
+ mimeType: data.mime_type,
169
+ data: data.data,
170
+ },
171
+ };
172
+ } else {
173
+ return {
174
+ fileData: {
175
+ mimeType: block.mime_type ?? '',
176
+ fileUri: block.url,
177
+ },
178
+ };
179
+ }
180
+ }
181
+
182
+ if (block.source_type === 'base64') {
183
+ return {
184
+ inlineData: {
185
+ mimeType: block.mime_type ?? '',
186
+ data: block.data,
187
+ },
188
+ };
189
+ }
190
+
191
+ throw new Error(`Unsupported source type: ${block.source_type}`);
192
+ },
193
+
194
+ fromStandardAudioBlock(block): FileDataPart | InlineDataPart {
195
+ if (!isMultimodalModel) {
196
+ throw new Error('This model does not support audio');
197
+ }
198
+ if (block.source_type === 'url') {
199
+ const data = parseBase64DataUrl({ dataUrl: block.url });
200
+ if (data) {
201
+ return {
202
+ inlineData: {
203
+ mimeType: data.mime_type,
204
+ data: data.data,
205
+ },
206
+ };
207
+ } else {
208
+ return {
209
+ fileData: {
210
+ mimeType: block.mime_type ?? '',
211
+ fileUri: block.url,
212
+ },
213
+ };
214
+ }
215
+ }
216
+
217
+ if (block.source_type === 'base64') {
218
+ return {
219
+ inlineData: {
220
+ mimeType: block.mime_type ?? '',
221
+ data: block.data,
222
+ },
223
+ };
224
+ }
225
+
226
+ throw new Error(`Unsupported source type: ${block.source_type}`);
227
+ },
228
+
229
+ fromStandardFileBlock(block): FileDataPart | InlineDataPart | TextPart {
230
+ if (!isMultimodalModel) {
231
+ throw new Error('This model does not support files');
232
+ }
233
+ if (block.source_type === 'text') {
234
+ return {
235
+ text: block.text,
236
+ };
237
+ }
238
+ if (block.source_type === 'url') {
239
+ const data = parseBase64DataUrl({ dataUrl: block.url });
240
+ if (data) {
241
+ return {
242
+ inlineData: {
243
+ mimeType: data.mime_type,
244
+ data: data.data,
245
+ },
246
+ };
247
+ } else {
248
+ return {
249
+ fileData: {
250
+ mimeType: block.mime_type ?? '',
251
+ fileUri: block.url,
252
+ },
253
+ };
254
+ }
255
+ }
256
+
257
+ if (block.source_type === 'base64') {
258
+ return {
259
+ inlineData: {
260
+ mimeType: block.mime_type ?? '',
261
+ data: block.data,
262
+ },
263
+ };
264
+ }
265
+ throw new Error(`Unsupported source type: ${block.source_type}`);
266
+ },
267
+ };
268
+ return standardContentBlockConverter;
269
+ }
270
+
271
+ function _convertLangChainContentToPart(
272
+ content: MessageContentComplex,
273
+ isMultimodalModel: boolean
274
+ ): Part | undefined {
275
+ if (isDataContentBlock(content)) {
276
+ return convertToProviderContentBlock(
277
+ content,
278
+ _getStandardContentBlockConverter(isMultimodalModel)
279
+ );
280
+ }
281
+
282
+ if (content.type === 'text') {
283
+ return { text: content.text };
284
+ } else if (content.type === 'executableCode') {
285
+ return { executableCode: content.executableCode };
286
+ } else if (content.type === 'codeExecutionResult') {
287
+ return { codeExecutionResult: content.codeExecutionResult };
288
+ } else if (content.type === 'image_url') {
289
+ if (!isMultimodalModel) {
290
+ throw new Error('This model does not support images');
291
+ }
292
+ let source: string;
293
+ if (typeof content.image_url === 'string') {
294
+ source = content.image_url;
295
+ } else if (
296
+ typeof content.image_url === 'object' &&
297
+ 'url' in content.image_url
298
+ ) {
299
+ source = content.image_url.url;
300
+ } else {
301
+ throw new Error('Please provide image as base64 encoded data URL');
302
+ }
303
+ const [dm, data] = source.split(',');
304
+ if (!dm.startsWith('data:')) {
305
+ throw new Error('Please provide image as base64 encoded data URL');
306
+ }
307
+
308
+ const [mimeType, encoding] = dm.replace(/^data:/, '').split(';');
309
+ if (encoding !== 'base64') {
310
+ throw new Error('Please provide image as base64 encoded data URL');
311
+ }
312
+
313
+ return {
314
+ inlineData: {
315
+ data,
316
+ mimeType,
317
+ },
318
+ };
319
+ } else if (content.type === 'media') {
320
+ return messageContentMedia(content);
321
+ } else if (content.type === 'tool_use') {
322
+ return {
323
+ functionCall: {
324
+ name: content.name,
325
+ args: content.input,
326
+ },
327
+ };
328
+ } else if (
329
+ content.type?.includes('/') === true &&
330
+ // Ensure it's a single slash.
331
+ content.type.split('/').length === 2 &&
332
+ 'data' in content &&
333
+ typeof content.data === 'string'
334
+ ) {
335
+ return {
336
+ inlineData: {
337
+ mimeType: content.type,
338
+ data: content.data,
339
+ },
340
+ };
341
+ } else if ('functionCall' in content) {
342
+ // No action needed here — function calls will be added later from message.tool_calls
343
+ return undefined;
344
+ } else {
345
+ if ('type' in content) {
346
+ throw new Error(`Unknown content type ${content.type}`);
347
+ } else {
348
+ throw new Error(`Unknown content ${JSON.stringify(content)}`);
349
+ }
350
+ }
351
+ }
352
+
353
+ export function convertMessageContentToParts(
354
+ message: BaseMessage,
355
+ isMultimodalModel: boolean,
356
+ previousMessages: BaseMessage[],
357
+ model?: string
358
+ ): Part[] {
359
+ if (isToolMessage(message)) {
360
+ const messageName =
361
+ message.name ??
362
+ inferToolNameFromPreviousMessages(message, previousMessages);
363
+ if (messageName === undefined) {
364
+ throw new Error(
365
+ `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.`
366
+ );
367
+ }
368
+
369
+ const result = Array.isArray(message.content)
370
+ ? (message.content
371
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
372
+ .filter((p) => p !== undefined) as Part[])
373
+ : message.content;
374
+
375
+ if (message.status === 'error') {
376
+ return [
377
+ {
378
+ functionResponse: {
379
+ name: messageName,
380
+ // The API expects an object with an `error` field if the function call fails.
381
+ // `error` must be a valid object (not a string or array), so we wrap `message.content` here
382
+ response: { error: { details: result } },
383
+ },
384
+ },
385
+ ];
386
+ }
387
+
388
+ return [
389
+ {
390
+ functionResponse: {
391
+ name: messageName,
392
+ // again, can't have a string or array value for `response`, so we wrap it as an object here
393
+ response: { result },
394
+ },
395
+ },
396
+ ];
397
+ }
398
+
399
+ let functionCalls: FunctionCallPart[] = [];
400
+ const messageParts: Part[] = [];
401
+
402
+ if (typeof message.content === 'string' && message.content) {
403
+ messageParts.push({ text: message.content });
404
+ }
405
+
406
+ if (Array.isArray(message.content)) {
407
+ messageParts.push(
408
+ ...(message.content
409
+ .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
410
+ .filter((p) => p !== undefined) as Part[])
411
+ );
412
+ }
413
+
414
+ const functionThoughtSignatures = (
415
+ message.additional_kwargs as BaseMessage['additional_kwargs'] | undefined
416
+ )?.[_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY] as
417
+ | Record<string, string>
418
+ | undefined;
419
+
420
+ if (isAIMessage(message) && (message.tool_calls?.length ?? 0) > 0) {
421
+ functionCalls = (message.tool_calls ?? []).map((tc) => {
422
+ const thoughtSignature = iife(() => {
423
+ if (tc.id != null && tc.id !== '') {
424
+ const signature = functionThoughtSignatures?.[tc.id];
425
+ if (signature != null && signature !== '') {
426
+ return signature;
427
+ }
428
+ }
429
+ if (model?.includes('gemini-3') === true) {
430
+ return DUMMY_SIGNATURE;
431
+ }
432
+ return '';
433
+ });
434
+
435
+ return {
436
+ functionCall: {
437
+ name: tc.name,
438
+ args: tc.args,
439
+ },
440
+ ...(thoughtSignature ? { thoughtSignature } : {}),
441
+ };
442
+ });
443
+ }
444
+
445
+ return [...messageParts, ...functionCalls];
446
+ }
447
+
448
+ export function convertBaseMessagesToContent(
449
+ messages: BaseMessage[],
450
+ isMultimodalModel: boolean,
451
+ convertSystemMessageToHumanContent: boolean = false,
452
+
453
+ model?: string
454
+ ): Content[] | undefined {
455
+ return messages.reduce<{
456
+ content: Content[] | undefined;
457
+ mergeWithPreviousContent: boolean;
458
+ }>(
459
+ (acc, message, index) => {
460
+ if (!isBaseMessage(message)) {
461
+ throw new Error('Unsupported message input');
462
+ }
463
+ const author = getMessageAuthor(message);
464
+ if (author === 'system' && index !== 0) {
465
+ throw new Error('System message should be the first one');
466
+ }
467
+ const role = convertAuthorToRole(author);
468
+
469
+ const prevContent = acc.content?.[acc.content.length];
470
+ if (
471
+ !acc.mergeWithPreviousContent &&
472
+ prevContent &&
473
+ prevContent.role === role
474
+ ) {
475
+ throw new Error(
476
+ 'Google Generative AI requires alternate messages between authors'
477
+ );
478
+ }
479
+
480
+ const parts = convertMessageContentToParts(
481
+ message,
482
+ isMultimodalModel,
483
+ messages.slice(0, index),
484
+ model
485
+ );
486
+
487
+ if (acc.mergeWithPreviousContent) {
488
+ const prevContent = acc.content?.[acc.content.length - 1];
489
+ if (!prevContent) {
490
+ throw new Error(
491
+ 'There was a problem parsing your system message. Please try a prompt without one.'
492
+ );
493
+ }
494
+ prevContent.parts.push(...parts);
495
+
496
+ return {
497
+ mergeWithPreviousContent: false,
498
+ content: acc.content,
499
+ };
500
+ }
501
+ let actualRole = role;
502
+ if (
503
+ actualRole === 'function' ||
504
+ (actualRole === 'system' && !convertSystemMessageToHumanContent)
505
+ ) {
506
+ // GenerativeAI API will throw an error if the role is not "user" or "model."
507
+ actualRole = 'user';
508
+ }
509
+ const content: Content = {
510
+ role: actualRole,
511
+ parts,
512
+ };
513
+ return {
514
+ mergeWithPreviousContent:
515
+ author === 'system' && !convertSystemMessageToHumanContent,
516
+ content: [...(acc.content ?? []), content],
517
+ };
518
+ },
519
+ { content: [], mergeWithPreviousContent: false }
520
+ ).content;
521
+ }
522
+
523
+ export function convertResponseContentToChatGenerationChunk(
524
+ response: EnhancedGenerateContentResponse,
525
+ extra: {
526
+ usageMetadata?: UsageMetadata | undefined;
527
+ index: number;
528
+ }
529
+ ): ChatGenerationChunk | null {
530
+ if (!response.candidates || response.candidates.length === 0) {
531
+ return null;
532
+ }
533
+ const [candidate] = response.candidates as [
534
+ Partial<GenerateContentCandidate> | undefined,
535
+ ];
536
+ const { content: candidateContent, ...generationInfo } = candidate ?? {};
537
+
538
+ // Extract function calls directly from parts to preserve thoughtSignature
539
+ const functionCalls =
540
+ (candidateContent?.parts as Part[] | undefined)?.reduce(
541
+ (acc, p) => {
542
+ if ('functionCall' in p && p.functionCall) {
543
+ acc.push({
544
+ ...p,
545
+ id:
546
+ 'id' in p.functionCall && typeof p.functionCall.id === 'string'
547
+ ? p.functionCall.id
548
+ : uuidv4(),
549
+ });
550
+ }
551
+ return acc;
552
+ },
553
+ [] as (
554
+ | undefined
555
+ | (FunctionCallPart & { id: string; thoughtSignature?: string })
556
+ )[]
557
+ ) ?? [];
558
+
559
+ let content: MessageContent | undefined;
560
+ // Checks if some parts do not have text. If false, it means that the content is a string.
561
+ const reasoningParts: string[] = [];
562
+ if (
563
+ candidateContent != null &&
564
+ Array.isArray(candidateContent.parts) &&
565
+ candidateContent.parts.every((p) => 'text' in p)
566
+ ) {
567
+ // content = candidateContent.parts.map((p) => p.text).join('');
568
+ const textParts: string[] = [];
569
+ for (const part of candidateContent.parts) {
570
+ if ('thought' in part && part.thought === true) {
571
+ reasoningParts.push(part.text ?? '');
572
+ continue;
573
+ }
574
+ textParts.push(part.text ?? '');
575
+ }
576
+ content = textParts.join('');
577
+ } else if (candidateContent && Array.isArray(candidateContent.parts)) {
578
+ content = candidateContent.parts
579
+ .map((p) => {
580
+ if ('text' in p && 'thought' in p && p.thought === true) {
581
+ reasoningParts.push(p.text ?? '');
582
+ return undefined;
583
+ } else if ('text' in p) {
584
+ return {
585
+ type: 'text',
586
+ text: p.text,
587
+ };
588
+ } else if ('executableCode' in p) {
589
+ return {
590
+ type: 'executableCode',
591
+ executableCode: p.executableCode,
592
+ };
593
+ } else if ('codeExecutionResult' in p) {
594
+ return {
595
+ type: 'codeExecutionResult',
596
+ codeExecutionResult: p.codeExecutionResult,
597
+ };
598
+ }
599
+ return p;
600
+ })
601
+ .filter((p) => p !== undefined);
602
+ } else {
603
+ // no content returned - likely due to abnormal stop reason, e.g. malformed function call
604
+ content = [];
605
+ }
606
+
607
+ let text = '';
608
+ if (typeof content === 'string' && content) {
609
+ text = content;
610
+ } else if (Array.isArray(content)) {
611
+ const block = content.find((b) => 'text' in b) as
612
+ | { text: string }
613
+ | undefined;
614
+ text = block?.text ?? '';
615
+ }
616
+
617
+ const toolCallChunks: ToolCallChunk[] = [];
618
+ if (functionCalls.length > 0) {
619
+ toolCallChunks.push(
620
+ ...functionCalls.map((fc) => ({
621
+ type: 'tool_call_chunk' as const,
622
+ id: fc?.id,
623
+ name: fc?.functionCall.name,
624
+ args: JSON.stringify(fc?.functionCall.args),
625
+ }))
626
+ );
627
+ }
628
+
629
+ // Extract thought signatures from function calls for Gemini 3+
630
+ const functionThoughtSignatures = functionCalls.reduce(
631
+ (acc, fc) => {
632
+ if (
633
+ fc &&
634
+ 'thoughtSignature' in fc &&
635
+ typeof fc.thoughtSignature === 'string'
636
+ ) {
637
+ acc[fc.id] = fc.thoughtSignature;
638
+ }
639
+ return acc;
640
+ },
641
+ {} as Record<string, string>
642
+ );
643
+
644
+ const additional_kwargs: ChatGeneration['message']['additional_kwargs'] = {
645
+ [_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY]: functionThoughtSignatures,
646
+ };
647
+
648
+ if (reasoningParts.length > 0) {
649
+ additional_kwargs.reasoning = reasoningParts.join('');
650
+ }
651
+
652
+ if (candidate?.groundingMetadata) {
653
+ additional_kwargs.groundingMetadata = candidate.groundingMetadata;
654
+ }
655
+
656
+ const isFinalChunk =
657
+ response.candidates[0]?.finishReason === 'STOP' ||
658
+ response.candidates[0]?.finishReason === 'MAX_TOKENS' ||
659
+ response.candidates[0]?.finishReason === 'SAFETY';
660
+
661
+ return new ChatGenerationChunk({
662
+ text,
663
+ message: new AIMessageChunk({
664
+ content: content,
665
+ name: !candidateContent ? undefined : candidateContent.role,
666
+ tool_call_chunks: toolCallChunks,
667
+ // Each chunk can have unique "generationInfo", and merging strategy is unclear,
668
+ // so leave blank for now.
669
+ additional_kwargs,
670
+ usage_metadata: isFinalChunk ? extra.usageMetadata : undefined,
671
+ }),
672
+ generationInfo,
673
+ });
674
+ }
675
+
676
+ /**
677
+ * Maps a Google GenerateContentResult to a LangChain ChatResult
678
+ */
679
+ export function mapGenerateContentResultToChatResult(
680
+ response: EnhancedGenerateContentResponse,
681
+ extra?: {
682
+ usageMetadata: UsageMetadata | undefined;
683
+ }
684
+ ): ChatResult {
685
+ if (
686
+ !response.candidates ||
687
+ response.candidates.length === 0 ||
688
+ !response.candidates[0]
689
+ ) {
690
+ return {
691
+ generations: [],
692
+ llmOutput: {
693
+ filters: response.promptFeedback,
694
+ },
695
+ };
696
+ }
697
+ const [candidate] = response.candidates as [
698
+ Partial<GenerateContentCandidate> | undefined,
699
+ ];
700
+ const { content: candidateContent, ...generationInfo } = candidate ?? {};
701
+
702
+ // Extract function calls directly from parts to preserve thoughtSignature
703
+ const functionCalls =
704
+ candidateContent?.parts.reduce(
705
+ (acc, p) => {
706
+ if ('functionCall' in p && p.functionCall) {
707
+ acc.push({
708
+ ...p,
709
+ id:
710
+ 'id' in p.functionCall && typeof p.functionCall.id === 'string'
711
+ ? p.functionCall.id
712
+ : uuidv4(),
713
+ });
714
+ }
715
+ return acc;
716
+ },
717
+ [] as (FunctionCallPart & { id: string; thoughtSignature?: string })[]
718
+ ) ?? [];
719
+
720
+ let content: MessageContent | undefined;
721
+ const reasoningParts: string[] = [];
722
+ if (
723
+ Array.isArray(candidateContent?.parts) &&
724
+ candidateContent.parts.length === 1 &&
725
+ candidateContent.parts[0].text &&
726
+ !(
727
+ 'thought' in candidateContent.parts[0] &&
728
+ candidateContent.parts[0].thought === true
729
+ )
730
+ ) {
731
+ content = candidateContent.parts[0].text;
732
+ } else if (
733
+ Array.isArray(candidateContent?.parts) &&
734
+ candidateContent.parts.length > 0
735
+ ) {
736
+ content = candidateContent.parts
737
+ .map((p) => {
738
+ if ('text' in p && 'thought' in p && p.thought === true) {
739
+ reasoningParts.push(p.text ?? '');
740
+ return undefined;
741
+ } else if ('text' in p) {
742
+ return {
743
+ type: 'text',
744
+ text: p.text,
745
+ };
746
+ } else if ('executableCode' in p) {
747
+ return {
748
+ type: 'executableCode',
749
+ executableCode: p.executableCode,
750
+ };
751
+ } else if ('codeExecutionResult' in p) {
752
+ return {
753
+ type: 'codeExecutionResult',
754
+ codeExecutionResult: p.codeExecutionResult,
755
+ };
756
+ }
757
+ return p;
758
+ })
759
+ .filter((p) => p !== undefined);
760
+ } else {
761
+ content = [];
762
+ }
763
+ let text = '';
764
+ if (typeof content === 'string') {
765
+ text = content;
766
+ } else if (Array.isArray(content) && content.length > 0) {
767
+ const block = content.find((b) => 'text' in b) as
768
+ | { text: string }
769
+ | undefined;
770
+ text = block?.text ?? text;
771
+ }
772
+
773
+ const additional_kwargs: ChatGeneration['message']['additional_kwargs'] = {
774
+ ...generationInfo,
775
+ };
776
+ if (reasoningParts.length > 0) {
777
+ additional_kwargs.reasoning = reasoningParts.join('');
778
+ }
779
+
780
+ // Extract thought signatures from function calls for Gemini 3+
781
+ const functionThoughtSignatures = functionCalls.reduce(
782
+ (acc, fc) => {
783
+ if ('thoughtSignature' in fc && typeof fc.thoughtSignature === 'string') {
784
+ acc[fc.id] = fc.thoughtSignature;
785
+ }
786
+ return acc;
787
+ },
788
+ {} as Record<string, string>
789
+ );
790
+
791
+ const tool_calls = functionCalls.map((fc) => ({
792
+ type: 'tool_call' as const,
793
+ id: fc.id,
794
+ name: fc.functionCall.name,
795
+ args: fc.functionCall.args,
796
+ }));
797
+
798
+ // Store thought signatures map for later retrieval
799
+ additional_kwargs[_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY] =
800
+ functionThoughtSignatures;
801
+
802
+ const generation: ChatGeneration = {
803
+ text,
804
+ message: new AIMessage({
805
+ content: content ?? '',
806
+ tool_calls,
807
+ additional_kwargs,
808
+ usage_metadata: extra?.usageMetadata,
809
+ }),
810
+ generationInfo,
811
+ };
812
+ return {
813
+ generations: [generation],
814
+ llmOutput: {
815
+ tokenUsage: {
816
+ promptTokens: extra?.usageMetadata?.input_tokens,
817
+ completionTokens: extra?.usageMetadata?.output_tokens,
818
+ totalTokens: extra?.usageMetadata?.total_tokens,
819
+ },
820
+ },
821
+ };
822
+ }
823
+
824
+ export function convertToGenerativeAITools(
825
+ tools: GoogleGenerativeAIToolType[]
826
+ ): GoogleGenerativeAIFunctionDeclarationsTool[] {
827
+ if (
828
+ tools.every(
829
+ (tool) =>
830
+ 'functionDeclarations' in tool &&
831
+ Array.isArray(tool.functionDeclarations)
832
+ )
833
+ ) {
834
+ return tools as GoogleGenerativeAIFunctionDeclarationsTool[];
835
+ }
836
+ return [
837
+ {
838
+ functionDeclarations: tools.map(
839
+ (tool): GenerativeAIFunctionDeclaration => {
840
+ if (isLangChainTool(tool)) {
841
+ const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
842
+ if (
843
+ jsonSchema.type === 'object' &&
844
+ 'properties' in jsonSchema &&
845
+ Object.keys(jsonSchema.properties).length === 0
846
+ ) {
847
+ return {
848
+ name: tool.name,
849
+ description: tool.description,
850
+ };
851
+ }
852
+ return {
853
+ name: tool.name,
854
+ description: tool.description,
855
+ parameters: jsonSchema,
856
+ };
857
+ }
858
+ if (isOpenAITool(tool)) {
859
+ return {
860
+ name: tool.function.name,
861
+ description:
862
+ tool.function.description ?? 'A function available to call.',
863
+ parameters: jsonSchemaToGeminiParameters(
864
+ tool.function.parameters
865
+ ),
866
+ };
867
+ }
868
+ return tool as unknown as GenerativeAIFunctionDeclaration;
869
+ }
870
+ ),
871
+ },
872
+ ];
873
+ }