@nocobase/plugin-ai 2.0.0-beta.6 → 2.0.0-beta.7

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 (65) hide show
  1. package/dist/client/6a4c1ee12f864e38.js +10 -0
  2. package/dist/client/index.js +1 -1
  3. package/dist/collections/ai-employees.d.ts +1 -0
  4. package/dist/collections/ai-employees.js +4 -0
  5. package/dist/externalVersion.js +11 -11
  6. package/dist/locale/en-US.json +2 -0
  7. package/dist/locale/zh-CN.json +2 -0
  8. package/dist/node_modules/@langchain/anthropic/package.json +1 -1
  9. package/dist/node_modules/@langchain/core/package.json +1 -1
  10. package/dist/node_modules/@langchain/deepseek/package.json +1 -1
  11. package/dist/node_modules/@langchain/google-genai/LICENSE +6 -6
  12. package/dist/node_modules/@langchain/google-genai/dist/_virtual/rolldown_runtime.cjs +25 -0
  13. package/dist/node_modules/@langchain/google-genai/dist/chat_models.cjs +680 -842
  14. package/dist/node_modules/@langchain/google-genai/dist/chat_models.d.cts +581 -0
  15. package/dist/node_modules/@langchain/google-genai/dist/chat_models.d.ts +196 -157
  16. package/dist/node_modules/@langchain/google-genai/dist/chat_models.js +678 -837
  17. package/dist/node_modules/@langchain/google-genai/dist/embeddings.cjs +97 -151
  18. package/dist/node_modules/@langchain/google-genai/dist/embeddings.d.cts +104 -0
  19. package/dist/node_modules/@langchain/google-genai/dist/embeddings.d.ts +76 -70
  20. package/dist/node_modules/@langchain/google-genai/dist/embeddings.js +93 -144
  21. package/dist/node_modules/@langchain/google-genai/dist/index.cjs +252 -18
  22. package/dist/node_modules/@langchain/google-genai/dist/index.d.cts +3 -0
  23. package/dist/node_modules/@langchain/google-genai/dist/index.d.ts +3 -2
  24. package/dist/node_modules/@langchain/google-genai/dist/index.js +4 -2
  25. package/dist/node_modules/@langchain/google-genai/dist/output_parsers.cjs +47 -75
  26. package/dist/node_modules/@langchain/google-genai/dist/output_parsers.js +47 -72
  27. package/dist/node_modules/@langchain/google-genai/dist/profiles.cjs +345 -0
  28. package/dist/node_modules/@langchain/google-genai/dist/profiles.js +344 -0
  29. package/dist/node_modules/@langchain/google-genai/dist/types.d.cts +17 -0
  30. package/dist/node_modules/@langchain/google-genai/dist/types.d.ts +16 -2
  31. package/dist/node_modules/@langchain/google-genai/dist/utils/common.cjs +419 -551
  32. package/dist/node_modules/@langchain/google-genai/dist/utils/common.js +417 -545
  33. package/dist/node_modules/@langchain/google-genai/dist/utils/tools.cjs +65 -102
  34. package/dist/node_modules/@langchain/google-genai/dist/utils/tools.js +64 -99
  35. package/dist/node_modules/@langchain/google-genai/dist/utils/zod_to_genai_parameters.cjs +31 -49
  36. package/dist/node_modules/@langchain/google-genai/dist/utils/zod_to_genai_parameters.js +29 -45
  37. package/dist/node_modules/@langchain/google-genai/package.json +1 -1
  38. package/dist/node_modules/@langchain/ollama/package.json +1 -1
  39. package/dist/node_modules/@langchain/openai/package.json +1 -1
  40. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  41. package/dist/node_modules/zod/package.json +1 -1
  42. package/dist/node_modules/zod-to-json-schema/package.json +1 -1
  43. package/dist/server/ai-employees/ai-employee.js +5 -2
  44. package/dist/server/llm-providers/anthropic.js +4 -5
  45. package/dist/server/llm-providers/provider.js +5 -1
  46. package/dist/server/manager/ai-chat-conversation.js +3 -2
  47. package/dist/server/manager/built-in-manager.js +3 -3
  48. package/dist/server/migrations/20260107000000-reset-builtin-about.d.ts +14 -0
  49. package/dist/server/migrations/20260107000000-reset-builtin-about.js +48 -0
  50. package/dist/server/resource/ai.js +8 -2
  51. package/dist/server/resource/aiEmployees.d.ts +0 -1
  52. package/dist/server/resource/aiEmployees.js +0 -15
  53. package/dist/server/types/ai-message.type.d.ts +2 -0
  54. package/package.json +3 -3
  55. package/dist/client/a7f0550514128d80.js +0 -10
  56. package/dist/node_modules/@langchain/google-genai/dist/output_parsers.d.ts +0 -20
  57. package/dist/node_modules/@langchain/google-genai/dist/types.cjs +0 -2
  58. package/dist/node_modules/@langchain/google-genai/dist/types.js +0 -1
  59. package/dist/node_modules/@langchain/google-genai/dist/utils/common.d.ts +0 -22
  60. package/dist/node_modules/@langchain/google-genai/dist/utils/tools.d.ts +0 -10
  61. package/dist/node_modules/@langchain/google-genai/dist/utils/zod_to_genai_parameters.d.ts +0 -14
  62. package/dist/node_modules/@langchain/google-genai/index.cjs +0 -252
  63. package/dist/node_modules/@langchain/google-genai/index.d.cts +0 -1
  64. package/dist/node_modules/@langchain/google-genai/index.d.ts +0 -1
  65. package/dist/node_modules/@langchain/google-genai/index.js +0 -1
@@ -1,563 +1,435 @@
1
- import { AIMessage, AIMessageChunk, ChatMessage, isAIMessage, isBaseMessage, isToolMessage, parseBase64DataUrl, convertToProviderContentBlock, isDataContentBlock, } from "@langchain/core/messages";
2
- import { ChatGenerationChunk, } from "@langchain/core/outputs";
1
+ import { jsonSchemaToGeminiParameters, schemaToGenerativeAIParameters } from "./zod_to_genai_parameters.js";
2
+ import { AIMessage, AIMessageChunk, ChatMessage, convertToProviderContentBlock, isAIMessage, isBaseMessage, isDataContentBlock, isToolMessage, parseBase64DataUrl } from "@langchain/core/messages";
3
+ import { ChatGenerationChunk } from "@langchain/core/outputs";
3
4
  import { isLangChainTool } from "@langchain/core/utils/function_calling";
4
5
  import { isOpenAITool } from "@langchain/core/language_models/base";
5
- import { v4 as uuidv4 } from "uuid";
6
- import { jsonSchemaToGeminiParameters, schemaToGenerativeAIParameters, } from "./zod_to_genai_parameters.js";
7
- export function getMessageAuthor(message) {
8
- const type = message._getType();
9
- if (ChatMessage.isInstance(message)) {
10
- return message.role;
11
- }
12
- if (type === "tool") {
13
- return type;
14
- }
15
- return message.name ?? type;
6
+ import { v4 } from "uuid";
7
+
8
+ //#region src/utils/common.ts
9
+ const _FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY = "__gemini_function_call_thought_signatures__";
10
+ const DUMMY_SIGNATURE = "ErYCCrMCAdHtim9kOoOkrPiCNVsmlpMIKd7ZMxgiFbVQOkgp7nlLcDMzVsZwIzvuT7nQROivoXA72ccC2lSDvR0Gh7dkWaGuj7ctv6t7ZceHnecx0QYa+ix8tYpRfjhyWozQ49lWiws6+YGjCt10KRTyWsZ2h6O7iHTYJwKIRwGUHRKy/qK/6kFxJm5ML00gLq4D8s5Z6DBpp2ZlR+uF4G8jJgeWQgyHWVdx2wGYElaceVAc66tZdPQRdOHpWtgYSI1YdaXgVI8KHY3/EfNc2YqqMIulvkDBAnuMhkAjV9xmBa54Tq+ih3Im4+r3DzqhGqYdsSkhS0kZMwte4Hjs65dZzCw9lANxIqYi1DJ639WNPYihp/DCJCos7o+/EeSPJaio5sgWDyUnMGkY1atsJZ+m7pj7DD5tvQ==";
11
+ const iife = (fn) => fn();
12
+ function getMessageAuthor(message) {
13
+ const type = message._getType();
14
+ if (ChatMessage.isInstance(message)) return message.role;
15
+ if (type === "tool") return type;
16
+ return message.name ?? type;
16
17
  }
17
18
  /**
18
- * Maps a message type to a Google Generative AI chat author.
19
- * @param message The message to map.
20
- * @param model The model to use for mapping.
21
- * @returns The message type mapped to a Google Generative AI chat author.
22
- */
23
- export function convertAuthorToRole(author) {
24
- switch (author) {
25
- /**
26
- * Note: Gemini currently is not supporting system messages
27
- * we will convert them to human messages and merge with following
28
- * */
29
- case "supervisor":
30
- case "ai":
31
- case "model": // getMessageAuthor returns message.name. code ex.: return message.name ?? type;
32
- return "model";
33
- case "system":
34
- return "system";
35
- case "human":
36
- return "user";
37
- case "tool":
38
- case "function":
39
- return "function";
40
- default:
41
- throw new Error(`Unknown / unsupported author: ${author}`);
42
- }
19
+ * Maps a message type to a Google Generative AI chat author.
20
+ * @param message The message to map.
21
+ * @param model The model to use for mapping.
22
+ * @returns The message type mapped to a Google Generative AI chat author.
23
+ */
24
+ function convertAuthorToRole(author) {
25
+ switch (author) {
26
+ case "supervisor":
27
+ case "ai":
28
+ case "model": return "model";
29
+ case "system": return "system";
30
+ case "human": return "user";
31
+ case "tool":
32
+ case "function": return "function";
33
+ default: throw new Error(`Unknown / unsupported author: ${author}`);
34
+ }
43
35
  }
44
36
  function messageContentMedia(content) {
45
- if ("mimeType" in content && "data" in content) {
46
- return {
47
- inlineData: {
48
- mimeType: content.mimeType,
49
- data: content.data,
50
- },
51
- };
52
- }
53
- if ("mimeType" in content && "fileUri" in content) {
54
- return {
55
- fileData: {
56
- mimeType: content.mimeType,
57
- fileUri: content.fileUri,
58
- },
59
- };
60
- }
61
- throw new Error("Invalid media content");
37
+ if ("mimeType" in content && "data" in content) return { inlineData: {
38
+ mimeType: content.mimeType,
39
+ data: content.data
40
+ } };
41
+ if ("mimeType" in content && "fileUri" in content) return { fileData: {
42
+ mimeType: content.mimeType,
43
+ fileUri: content.fileUri
44
+ } };
45
+ throw new Error("Invalid media content");
62
46
  }
63
47
  function inferToolNameFromPreviousMessages(message, previousMessages) {
64
- return previousMessages
65
- .map((msg) => {
66
- if (isAIMessage(msg)) {
67
- return msg.tool_calls ?? [];
68
- }
69
- return [];
70
- })
71
- .flat()
72
- .find((toolCall) => {
73
- return toolCall.id === message.tool_call_id;
74
- })?.name;
48
+ return previousMessages.map((msg) => {
49
+ if (isAIMessage(msg)) return msg.tool_calls ?? [];
50
+ return [];
51
+ }).flat().find((toolCall) => {
52
+ return toolCall.id === message.tool_call_id;
53
+ })?.name;
75
54
  }
76
55
  function _getStandardContentBlockConverter(isMultimodalModel) {
77
- const standardContentBlockConverter = {
78
- providerName: "Google Gemini",
79
- fromStandardTextBlock(block) {
80
- return {
81
- text: block.text,
82
- };
83
- },
84
- fromStandardImageBlock(block) {
85
- if (!isMultimodalModel) {
86
- throw new Error("This model does not support images");
87
- }
88
- if (block.source_type === "url") {
89
- const data = parseBase64DataUrl({ dataUrl: block.url });
90
- if (data) {
91
- return {
92
- inlineData: {
93
- mimeType: data.mime_type,
94
- data: data.data,
95
- },
96
- };
97
- }
98
- else {
99
- return {
100
- fileData: {
101
- mimeType: block.mime_type ?? "",
102
- fileUri: block.url,
103
- },
104
- };
105
- }
106
- }
107
- if (block.source_type === "base64") {
108
- return {
109
- inlineData: {
110
- mimeType: block.mime_type ?? "",
111
- data: block.data,
112
- },
113
- };
114
- }
115
- throw new Error(`Unsupported source type: ${block.source_type}`);
116
- },
117
- fromStandardAudioBlock(block) {
118
- if (!isMultimodalModel) {
119
- throw new Error("This model does not support audio");
120
- }
121
- if (block.source_type === "url") {
122
- const data = parseBase64DataUrl({ dataUrl: block.url });
123
- if (data) {
124
- return {
125
- inlineData: {
126
- mimeType: data.mime_type,
127
- data: data.data,
128
- },
129
- };
130
- }
131
- else {
132
- return {
133
- fileData: {
134
- mimeType: block.mime_type ?? "",
135
- fileUri: block.url,
136
- },
137
- };
138
- }
139
- }
140
- if (block.source_type === "base64") {
141
- return {
142
- inlineData: {
143
- mimeType: block.mime_type ?? "",
144
- data: block.data,
145
- },
146
- };
147
- }
148
- throw new Error(`Unsupported source type: ${block.source_type}`);
149
- },
150
- fromStandardFileBlock(block) {
151
- if (!isMultimodalModel) {
152
- throw new Error("This model does not support files");
153
- }
154
- if (block.source_type === "text") {
155
- return {
156
- text: block.text,
157
- };
158
- }
159
- if (block.source_type === "url") {
160
- const data = parseBase64DataUrl({ dataUrl: block.url });
161
- if (data) {
162
- return {
163
- inlineData: {
164
- mimeType: data.mime_type,
165
- data: data.data,
166
- },
167
- };
168
- }
169
- else {
170
- return {
171
- fileData: {
172
- mimeType: block.mime_type ?? "",
173
- fileUri: block.url,
174
- },
175
- };
176
- }
177
- }
178
- if (block.source_type === "base64") {
179
- return {
180
- inlineData: {
181
- mimeType: block.mime_type ?? "",
182
- data: block.data,
183
- },
184
- };
185
- }
186
- throw new Error(`Unsupported source type: ${block.source_type}`);
187
- },
188
- };
189
- return standardContentBlockConverter;
56
+ const standardContentBlockConverter = {
57
+ providerName: "Google Gemini",
58
+ fromStandardTextBlock(block) {
59
+ return { text: block.text };
60
+ },
61
+ fromStandardImageBlock(block) {
62
+ if (!isMultimodalModel) throw new Error("This model does not support images");
63
+ if (block.source_type === "url") {
64
+ const data = parseBase64DataUrl({ dataUrl: block.url });
65
+ if (data) return { inlineData: {
66
+ mimeType: data.mime_type,
67
+ data: data.data
68
+ } };
69
+ else return { fileData: {
70
+ mimeType: block.mime_type ?? "",
71
+ fileUri: block.url
72
+ } };
73
+ }
74
+ if (block.source_type === "base64") return { inlineData: {
75
+ mimeType: block.mime_type ?? "",
76
+ data: block.data
77
+ } };
78
+ throw new Error(`Unsupported source type: ${block.source_type}`);
79
+ },
80
+ fromStandardAudioBlock(block) {
81
+ if (!isMultimodalModel) throw new Error("This model does not support audio");
82
+ if (block.source_type === "url") {
83
+ const data = parseBase64DataUrl({ dataUrl: block.url });
84
+ if (data) return { inlineData: {
85
+ mimeType: data.mime_type,
86
+ data: data.data
87
+ } };
88
+ else return { fileData: {
89
+ mimeType: block.mime_type ?? "",
90
+ fileUri: block.url
91
+ } };
92
+ }
93
+ if (block.source_type === "base64") return { inlineData: {
94
+ mimeType: block.mime_type ?? "",
95
+ data: block.data
96
+ } };
97
+ throw new Error(`Unsupported source type: ${block.source_type}`);
98
+ },
99
+ fromStandardFileBlock(block) {
100
+ if (!isMultimodalModel) throw new Error("This model does not support files");
101
+ if (block.source_type === "text") return { text: block.text };
102
+ if (block.source_type === "url") {
103
+ const data = parseBase64DataUrl({ dataUrl: block.url });
104
+ if (data) return { inlineData: {
105
+ mimeType: data.mime_type,
106
+ data: data.data
107
+ } };
108
+ else return { fileData: {
109
+ mimeType: block.mime_type ?? "",
110
+ fileUri: block.url
111
+ } };
112
+ }
113
+ if (block.source_type === "base64") return { inlineData: {
114
+ mimeType: block.mime_type ?? "",
115
+ data: block.data
116
+ } };
117
+ throw new Error(`Unsupported source type: ${block.source_type}`);
118
+ }
119
+ };
120
+ return standardContentBlockConverter;
190
121
  }
191
122
  function _convertLangChainContentToPart(content, isMultimodalModel) {
192
- if (isDataContentBlock(content)) {
193
- return convertToProviderContentBlock(content, _getStandardContentBlockConverter(isMultimodalModel));
194
- }
195
- if (content.type === "text") {
196
- return { text: content.text };
197
- }
198
- else if (content.type === "executableCode") {
199
- return { executableCode: content.executableCode };
200
- }
201
- else if (content.type === "codeExecutionResult") {
202
- return { codeExecutionResult: content.codeExecutionResult };
203
- }
204
- else if (content.type === "image_url") {
205
- if (!isMultimodalModel) {
206
- throw new Error(`This model does not support images`);
207
- }
208
- let source;
209
- if (typeof content.image_url === "string") {
210
- source = content.image_url;
211
- }
212
- else if (typeof content.image_url === "object" &&
213
- "url" in content.image_url) {
214
- source = content.image_url.url;
215
- }
216
- else {
217
- throw new Error("Please provide image as base64 encoded data URL");
218
- }
219
- const [dm, data] = source.split(",");
220
- if (!dm.startsWith("data:")) {
221
- throw new Error("Please provide image as base64 encoded data URL");
222
- }
223
- const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
224
- if (encoding !== "base64") {
225
- throw new Error("Please provide image as base64 encoded data URL");
226
- }
227
- return {
228
- inlineData: {
229
- data,
230
- mimeType,
231
- },
232
- };
233
- }
234
- else if (content.type === "media") {
235
- return messageContentMedia(content);
236
- }
237
- else if (content.type === "tool_use") {
238
- return {
239
- functionCall: {
240
- name: content.name,
241
- args: content.input,
242
- },
243
- };
244
- }
245
- else if (content.type?.includes("/") &&
246
- // Ensure it's a single slash.
247
- content.type.split("/").length === 2 &&
248
- "data" in content &&
249
- typeof content.data === "string") {
250
- return {
251
- inlineData: {
252
- mimeType: content.type,
253
- data: content.data,
254
- },
255
- };
256
- }
257
- else if ("functionCall" in content) {
258
- // No action needed here — function calls will be added later from message.tool_calls
259
- return undefined;
260
- }
261
- else {
262
- if ("type" in content) {
263
- throw new Error(`Unknown content type ${content.type}`);
264
- }
265
- else {
266
- throw new Error(`Unknown content ${JSON.stringify(content)}`);
267
- }
268
- }
123
+ if (isDataContentBlock(content)) return convertToProviderContentBlock(content, _getStandardContentBlockConverter(isMultimodalModel));
124
+ if (content.type === "text") return { text: content.text };
125
+ else if (content.type === "executableCode") return { executableCode: content.executableCode };
126
+ else if (content.type === "codeExecutionResult") return { codeExecutionResult: content.codeExecutionResult };
127
+ else if (content.type === "image_url") {
128
+ if (!isMultimodalModel) throw new Error(`This model does not support images`);
129
+ let source;
130
+ if (typeof content.image_url === "string") source = content.image_url;
131
+ else if (typeof content.image_url === "object" && "url" in content.image_url) source = content.image_url.url;
132
+ else throw new Error("Please provide image as base64 encoded data URL");
133
+ const [dm, data] = source.split(",");
134
+ if (!dm.startsWith("data:")) throw new Error("Please provide image as base64 encoded data URL");
135
+ const [mimeType, encoding] = dm.replace(/^data:/, "").split(";");
136
+ if (encoding !== "base64") throw new Error("Please provide image as base64 encoded data URL");
137
+ return { inlineData: {
138
+ data,
139
+ mimeType
140
+ } };
141
+ } else if (content.type === "media") return messageContentMedia(content);
142
+ else if (content.type === "tool_use") return { functionCall: {
143
+ name: content.name,
144
+ args: content.input
145
+ } };
146
+ else if (content.type?.includes("/") && content.type.split("/").length === 2 && "data" in content && typeof content.data === "string") return { inlineData: {
147
+ mimeType: content.type,
148
+ data: content.data
149
+ } };
150
+ else if ("functionCall" in content) return void 0;
151
+ else if ("type" in content) throw new Error(`Unknown content type ${content.type}`);
152
+ else throw new Error(`Unknown content ${JSON.stringify(content)}`);
269
153
  }
270
- export function convertMessageContentToParts(message, isMultimodalModel, previousMessages) {
271
- if (isToolMessage(message)) {
272
- const messageName = message.name ??
273
- inferToolNameFromPreviousMessages(message, previousMessages);
274
- if (messageName === undefined) {
275
- 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.`);
276
- }
277
- const result = Array.isArray(message.content)
278
- ? message.content
279
- .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
280
- .filter((p) => p !== undefined)
281
- : message.content;
282
- if (message.status === "error") {
283
- return [
284
- {
285
- functionResponse: {
286
- name: messageName,
287
- // The API expects an object with an `error` field if the function call fails.
288
- // `error` must be a valid object (not a string or array), so we wrap `message.content` here
289
- response: { error: { details: result } },
290
- },
291
- },
292
- ];
293
- }
294
- return [
295
- {
296
- functionResponse: {
297
- name: messageName,
298
- // again, can't have a string or array value for `response`, so we wrap it as an object here
299
- response: { result },
300
- },
301
- },
302
- ];
303
- }
304
- let functionCalls = [];
305
- const messageParts = [];
306
- if (typeof message.content === "string" && message.content) {
307
- messageParts.push({ text: message.content });
308
- }
309
- if (Array.isArray(message.content)) {
310
- messageParts.push(...message.content
311
- .map((c) => _convertLangChainContentToPart(c, isMultimodalModel))
312
- .filter((p) => p !== undefined));
313
- }
314
- if (isAIMessage(message) && message.tool_calls?.length) {
315
- functionCalls = message.tool_calls.map((tc) => {
316
- return {
317
- functionCall: {
318
- name: tc.name,
319
- args: tc.args,
320
- },
321
- };
322
- });
323
- }
324
- return [...messageParts, ...functionCalls];
154
+ function convertMessageContentToParts(message, isMultimodalModel, previousMessages, model) {
155
+ if (isToolMessage(message)) {
156
+ const messageName = message.name ?? inferToolNameFromPreviousMessages(message, previousMessages);
157
+ if (messageName === void 0) 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.`);
158
+ const result = Array.isArray(message.content) ? message.content.map((c) => _convertLangChainContentToPart(c, isMultimodalModel)).filter((p) => p !== void 0) : message.content;
159
+ if (message.status === "error") return [{ functionResponse: {
160
+ name: messageName,
161
+ response: { error: { details: result } }
162
+ } }];
163
+ return [{ functionResponse: {
164
+ name: messageName,
165
+ response: { result }
166
+ } }];
167
+ }
168
+ let functionCalls = [];
169
+ const messageParts = [];
170
+ if (typeof message.content === "string" && message.content) messageParts.push({ text: message.content });
171
+ if (Array.isArray(message.content)) messageParts.push(...message.content.map((c) => _convertLangChainContentToPart(c, isMultimodalModel)).filter((p) => p !== void 0));
172
+ const functionThoughtSignatures = message.additional_kwargs?.[_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY];
173
+ if (isAIMessage(message) && message.tool_calls?.length) functionCalls = message.tool_calls.map((tc) => {
174
+ const thoughtSignature = iife(() => {
175
+ if (tc.id) {
176
+ const signature = functionThoughtSignatures?.[tc.id];
177
+ if (signature) return signature;
178
+ }
179
+ if (model?.includes("gemini-3")) return DUMMY_SIGNATURE;
180
+ return "";
181
+ });
182
+ return {
183
+ functionCall: {
184
+ name: tc.name,
185
+ args: tc.args
186
+ },
187
+ ...thoughtSignature ? { thoughtSignature } : {}
188
+ };
189
+ });
190
+ return [...messageParts, ...functionCalls];
325
191
  }
326
- export function convertBaseMessagesToContent(messages, isMultimodalModel, convertSystemMessageToHumanContent = false) {
327
- return messages.reduce((acc, message, index) => {
328
- if (!isBaseMessage(message)) {
329
- throw new Error("Unsupported message input");
330
- }
331
- const author = getMessageAuthor(message);
332
- if (author === "system" && index !== 0) {
333
- throw new Error("System message should be the first one");
334
- }
335
- const role = convertAuthorToRole(author);
336
- const prevContent = acc.content[acc.content.length];
337
- if (!acc.mergeWithPreviousContent &&
338
- prevContent &&
339
- prevContent.role === role) {
340
- throw new Error("Google Generative AI requires alternate messages between authors");
341
- }
342
- const parts = convertMessageContentToParts(message, isMultimodalModel, messages.slice(0, index));
343
- if (acc.mergeWithPreviousContent) {
344
- const prevContent = acc.content[acc.content.length - 1];
345
- if (!prevContent) {
346
- throw new Error("There was a problem parsing your system message. Please try a prompt without one.");
347
- }
348
- prevContent.parts.push(...parts);
349
- return {
350
- mergeWithPreviousContent: false,
351
- content: acc.content,
352
- };
353
- }
354
- let actualRole = role;
355
- if (actualRole === "function" ||
356
- (actualRole === "system" && !convertSystemMessageToHumanContent)) {
357
- // GenerativeAI API will throw an error if the role is not "user" or "model."
358
- actualRole = "user";
359
- }
360
- const content = {
361
- role: actualRole,
362
- parts,
363
- };
364
- return {
365
- mergeWithPreviousContent: author === "system" && !convertSystemMessageToHumanContent,
366
- content: [...acc.content, content],
367
- };
368
- }, { content: [], mergeWithPreviousContent: false }).content;
192
+ function convertBaseMessagesToContent(messages, isMultimodalModel, convertSystemMessageToHumanContent = false, model) {
193
+ return messages.reduce((acc, message, index) => {
194
+ if (!isBaseMessage(message)) throw new Error("Unsupported message input");
195
+ const author = getMessageAuthor(message);
196
+ if (author === "system" && index !== 0) throw new Error("System message should be the first one");
197
+ const role = convertAuthorToRole(author);
198
+ const prevContent = acc.content[acc.content.length];
199
+ if (!acc.mergeWithPreviousContent && prevContent && prevContent.role === role) throw new Error("Google Generative AI requires alternate messages between authors");
200
+ const parts = convertMessageContentToParts(message, isMultimodalModel, messages.slice(0, index), model);
201
+ if (acc.mergeWithPreviousContent) {
202
+ const prevContent$1 = acc.content[acc.content.length - 1];
203
+ if (!prevContent$1) throw new Error("There was a problem parsing your system message. Please try a prompt without one.");
204
+ prevContent$1.parts.push(...parts);
205
+ return {
206
+ mergeWithPreviousContent: false,
207
+ content: acc.content
208
+ };
209
+ }
210
+ let actualRole = role;
211
+ if (actualRole === "function" || actualRole === "system" && !convertSystemMessageToHumanContent) actualRole = "user";
212
+ const content = {
213
+ role: actualRole,
214
+ parts
215
+ };
216
+ return {
217
+ mergeWithPreviousContent: author === "system" && !convertSystemMessageToHumanContent,
218
+ content: [...acc.content, content]
219
+ };
220
+ }, {
221
+ content: [],
222
+ mergeWithPreviousContent: false
223
+ }).content;
369
224
  }
370
- export function mapGenerateContentResultToChatResult(response, extra) {
371
- // if rejected or error, return empty generations with reason in filters
372
- if (!response.candidates ||
373
- response.candidates.length === 0 ||
374
- !response.candidates[0]) {
375
- return {
376
- generations: [],
377
- llmOutput: {
378
- filters: response.promptFeedback,
379
- },
380
- };
381
- }
382
- const functionCalls = response.functionCalls();
383
- const [candidate] = response.candidates;
384
- const { content: candidateContent, ...generationInfo } = candidate;
385
- let content;
386
- if (Array.isArray(candidateContent?.parts) &&
387
- candidateContent.parts.length === 1 &&
388
- candidateContent.parts[0].text) {
389
- content = candidateContent.parts[0].text;
390
- }
391
- else if (Array.isArray(candidateContent?.parts) &&
392
- candidateContent.parts.length > 0) {
393
- content = candidateContent.parts.map((p) => {
394
- if ("text" in p) {
395
- return {
396
- type: "text",
397
- text: p.text,
398
- };
399
- }
400
- else if ("executableCode" in p) {
401
- return {
402
- type: "executableCode",
403
- executableCode: p.executableCode,
404
- };
405
- }
406
- else if ("codeExecutionResult" in p) {
407
- return {
408
- type: "codeExecutionResult",
409
- codeExecutionResult: p.codeExecutionResult,
410
- };
411
- }
412
- return p;
413
- });
414
- }
415
- else {
416
- // no content returned - likely due to abnormal stop reason, e.g. malformed function call
417
- content = [];
418
- }
419
- let text = "";
420
- if (typeof content === "string") {
421
- text = content;
422
- }
423
- else if (Array.isArray(content) && content.length > 0) {
424
- const block = content.find((b) => "text" in b);
425
- text = block?.text ?? text;
426
- }
427
- const generation = {
428
- text,
429
- message: new AIMessage({
430
- content: content ?? "",
431
- tool_calls: functionCalls?.map((fc) => {
432
- return {
433
- ...fc,
434
- type: "tool_call",
435
- id: "id" in fc && typeof fc.id === "string" ? fc.id : uuidv4(),
436
- };
437
- }),
438
- additional_kwargs: {
439
- ...generationInfo,
440
- },
441
- usage_metadata: extra?.usageMetadata,
442
- }),
443
- generationInfo,
444
- };
445
- return {
446
- generations: [generation],
447
- llmOutput: {
448
- tokenUsage: {
449
- promptTokens: extra?.usageMetadata?.input_tokens,
450
- completionTokens: extra?.usageMetadata?.output_tokens,
451
- totalTokens: extra?.usageMetadata?.total_tokens,
452
- },
453
- },
454
- };
225
+ function mapGenerateContentResultToChatResult(response, extra) {
226
+ if (!response.candidates || response.candidates.length === 0 || !response.candidates[0]) return {
227
+ generations: [],
228
+ llmOutput: { filters: response.promptFeedback }
229
+ };
230
+ const [candidate] = response.candidates;
231
+ const { content: candidateContent,...generationInfo } = candidate;
232
+ const functionCalls = candidateContent.parts.reduce((acc, p) => {
233
+ if ("functionCall" in p && p.functionCall) acc.push({
234
+ ...p,
235
+ id: "id" in p.functionCall && typeof p.functionCall.id === "string" ? p.functionCall.id : v4()
236
+ });
237
+ return acc;
238
+ }, []);
239
+ let content;
240
+ if (Array.isArray(candidateContent?.parts) && candidateContent.parts.length === 1 && candidateContent.parts[0].text) content = candidateContent.parts[0].text;
241
+ else if (Array.isArray(candidateContent?.parts) && candidateContent.parts.length > 0) content = candidateContent.parts.map((p) => {
242
+ if ("text" in p) return {
243
+ type: "text",
244
+ text: p.text
245
+ };
246
+ else if ("inlineData" in p) return {
247
+ type: "inlineData",
248
+ inlineData: p.inlineData
249
+ };
250
+ else if ("functionCall" in p) return {
251
+ type: "functionCall",
252
+ functionCall: p.functionCall
253
+ };
254
+ else if ("functionResponse" in p) return {
255
+ type: "functionResponse",
256
+ functionResponse: p.functionResponse
257
+ };
258
+ else if ("fileData" in p) return {
259
+ type: "fileData",
260
+ fileData: p.fileData
261
+ };
262
+ else if ("executableCode" in p) return {
263
+ type: "executableCode",
264
+ executableCode: p.executableCode
265
+ };
266
+ else if ("codeExecutionResult" in p) return {
267
+ type: "codeExecutionResult",
268
+ codeExecutionResult: p.codeExecutionResult
269
+ };
270
+ return p;
271
+ });
272
+ else content = [];
273
+ const functionThoughtSignatures = functionCalls?.reduce((acc, fc) => {
274
+ if ("thoughtSignature" in fc && typeof fc.thoughtSignature === "string") acc[fc.id] = fc.thoughtSignature;
275
+ return acc;
276
+ }, {});
277
+ let text = "";
278
+ if (typeof content === "string") text = content;
279
+ else if (Array.isArray(content) && content.length > 0) {
280
+ const block = content.find((b) => "text" in b);
281
+ text = block?.text ?? text;
282
+ }
283
+ const generation = {
284
+ text,
285
+ message: new AIMessage({
286
+ content: content ?? "",
287
+ tool_calls: functionCalls?.map((fc) => ({
288
+ type: "tool_call",
289
+ id: fc.id,
290
+ name: fc.functionCall.name,
291
+ args: fc.functionCall.args
292
+ })),
293
+ additional_kwargs: {
294
+ ...generationInfo,
295
+ [_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY]: functionThoughtSignatures
296
+ },
297
+ usage_metadata: extra?.usageMetadata
298
+ }),
299
+ generationInfo
300
+ };
301
+ return {
302
+ generations: [generation],
303
+ llmOutput: { tokenUsage: {
304
+ promptTokens: extra?.usageMetadata?.input_tokens,
305
+ completionTokens: extra?.usageMetadata?.output_tokens,
306
+ totalTokens: extra?.usageMetadata?.total_tokens
307
+ } }
308
+ };
455
309
  }
456
- export function convertResponseContentToChatGenerationChunk(response, extra) {
457
- if (!response.candidates || response.candidates.length === 0) {
458
- return null;
459
- }
460
- const functionCalls = response.functionCalls();
461
- const [candidate] = response.candidates;
462
- const { content: candidateContent, ...generationInfo } = candidate;
463
- let content;
464
- // Checks if some parts do not have text. If false, it means that the content is a string.
465
- if (Array.isArray(candidateContent?.parts) &&
466
- candidateContent.parts.every((p) => "text" in p)) {
467
- content = candidateContent.parts.map((p) => p.text).join("");
468
- }
469
- else if (Array.isArray(candidateContent?.parts)) {
470
- content = candidateContent.parts.map((p) => {
471
- if ("text" in p) {
472
- return {
473
- type: "text",
474
- text: p.text,
475
- };
476
- }
477
- else if ("executableCode" in p) {
478
- return {
479
- type: "executableCode",
480
- executableCode: p.executableCode,
481
- };
482
- }
483
- else if ("codeExecutionResult" in p) {
484
- return {
485
- type: "codeExecutionResult",
486
- codeExecutionResult: p.codeExecutionResult,
487
- };
488
- }
489
- return p;
490
- });
491
- }
492
- else {
493
- // no content returned - likely due to abnormal stop reason, e.g. malformed function call
494
- content = [];
495
- }
496
- let text = "";
497
- if (content && typeof content === "string") {
498
- text = content;
499
- }
500
- else if (Array.isArray(content)) {
501
- const block = content.find((b) => "text" in b);
502
- text = block?.text ?? "";
503
- }
504
- const toolCallChunks = [];
505
- if (functionCalls) {
506
- toolCallChunks.push(...functionCalls.map((fc) => ({
507
- ...fc,
508
- args: JSON.stringify(fc.args),
509
- index: extra.index,
510
- type: "tool_call_chunk",
511
- id: "id" in fc && typeof fc.id === "string" ? fc.id : uuidv4(),
512
- })));
513
- }
514
- return new ChatGenerationChunk({
515
- text,
516
- message: new AIMessageChunk({
517
- content: content || "",
518
- name: !candidateContent ? undefined : candidateContent.role,
519
- tool_call_chunks: toolCallChunks,
520
- // Each chunk can have unique "generationInfo", and merging strategy is unclear,
521
- // so leave blank for now.
522
- additional_kwargs: {},
523
- usage_metadata: extra.usageMetadata,
524
- }),
525
- generationInfo,
526
- });
310
+ function convertResponseContentToChatGenerationChunk(response, extra) {
311
+ if (!response.candidates || response.candidates.length === 0) return null;
312
+ const [candidate] = response.candidates;
313
+ const { content: candidateContent,...generationInfo } = candidate;
314
+ const functionCalls = candidateContent.parts.reduce((acc, p) => {
315
+ if ("functionCall" in p && p.functionCall) acc.push({
316
+ ...p,
317
+ id: "id" in p.functionCall && typeof p.functionCall.id === "string" ? p.functionCall.id : v4()
318
+ });
319
+ return acc;
320
+ }, []);
321
+ let content;
322
+ if (Array.isArray(candidateContent?.parts) && candidateContent.parts.every((p) => "text" in p)) content = candidateContent.parts.map((p) => p.text).join("");
323
+ else if (Array.isArray(candidateContent?.parts)) content = candidateContent.parts.map((p) => {
324
+ if ("text" in p) return {
325
+ type: "text",
326
+ text: p.text
327
+ };
328
+ else if ("inlineData" in p) return {
329
+ type: "inlineData",
330
+ inlineData: p.inlineData
331
+ };
332
+ else if ("functionCall" in p) return {
333
+ type: "functionCall",
334
+ functionCall: p.functionCall
335
+ };
336
+ else if ("functionResponse" in p) return {
337
+ type: "functionResponse",
338
+ functionResponse: p.functionResponse
339
+ };
340
+ else if ("fileData" in p) return {
341
+ type: "fileData",
342
+ fileData: p.fileData
343
+ };
344
+ else if ("executableCode" in p) return {
345
+ type: "executableCode",
346
+ executableCode: p.executableCode
347
+ };
348
+ else if ("codeExecutionResult" in p) return {
349
+ type: "codeExecutionResult",
350
+ codeExecutionResult: p.codeExecutionResult
351
+ };
352
+ return p;
353
+ });
354
+ else content = [];
355
+ let text = "";
356
+ if (content && typeof content === "string") text = content;
357
+ else if (Array.isArray(content)) {
358
+ const block = content.find((b) => "text" in b);
359
+ text = block?.text ?? "";
360
+ }
361
+ const toolCallChunks = [];
362
+ if (functionCalls) toolCallChunks.push(...functionCalls.map((fc) => ({
363
+ type: "tool_call_chunk",
364
+ id: fc.id,
365
+ name: fc.functionCall.name,
366
+ args: JSON.stringify(fc.functionCall.args)
367
+ })));
368
+ const functionThoughtSignatures = functionCalls?.reduce((acc, fc) => {
369
+ if ("thoughtSignature" in fc && typeof fc.thoughtSignature === "string") acc[fc.id] = fc.thoughtSignature;
370
+ return acc;
371
+ }, {});
372
+ return new ChatGenerationChunk({
373
+ text,
374
+ message: new AIMessageChunk({
375
+ content: content || "",
376
+ name: !candidateContent ? void 0 : candidateContent.role,
377
+ tool_call_chunks: toolCallChunks,
378
+ additional_kwargs: { [_FUNCTION_CALL_THOUGHT_SIGNATURES_MAP_KEY]: functionThoughtSignatures },
379
+ response_metadata: { model_provider: "google-genai" },
380
+ usage_metadata: extra.usageMetadata
381
+ }),
382
+ generationInfo
383
+ });
527
384
  }
528
- export function convertToGenerativeAITools(tools) {
529
- if (tools.every((tool) => "functionDeclarations" in tool &&
530
- Array.isArray(tool.functionDeclarations))) {
531
- return tools;
532
- }
533
- return [
534
- {
535
- functionDeclarations: tools.map((tool) => {
536
- if (isLangChainTool(tool)) {
537
- const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
538
- if (jsonSchema.type === "object" &&
539
- "properties" in jsonSchema &&
540
- Object.keys(jsonSchema.properties).length === 0) {
541
- return {
542
- name: tool.name,
543
- description: tool.description,
544
- };
545
- }
546
- return {
547
- name: tool.name,
548
- description: tool.description,
549
- parameters: jsonSchema,
550
- };
551
- }
552
- if (isOpenAITool(tool)) {
553
- return {
554
- name: tool.function.name,
555
- description: tool.function.description ?? `A function available to call.`,
556
- parameters: jsonSchemaToGeminiParameters(tool.function.parameters),
557
- };
558
- }
559
- return tool;
560
- }),
561
- },
562
- ];
385
+ function convertToGenerativeAITools(tools) {
386
+ if (tools.every((tool) => "functionDeclarations" in tool && Array.isArray(tool.functionDeclarations))) return tools;
387
+ return [{ functionDeclarations: tools.map((tool) => {
388
+ if (isLangChainTool(tool)) {
389
+ const jsonSchema = schemaToGenerativeAIParameters(tool.schema);
390
+ if (jsonSchema.type === "object" && "properties" in jsonSchema && Object.keys(jsonSchema.properties).length === 0) return {
391
+ name: tool.name,
392
+ description: tool.description
393
+ };
394
+ return {
395
+ name: tool.name,
396
+ description: tool.description,
397
+ parameters: jsonSchema
398
+ };
399
+ }
400
+ if (isOpenAITool(tool)) return {
401
+ name: tool.function.name,
402
+ description: tool.function.description ?? `A function available to call.`,
403
+ parameters: jsonSchemaToGeminiParameters(tool.function.parameters)
404
+ };
405
+ return tool;
406
+ }) }];
563
407
  }
408
+ function convertUsageMetadata(usageMetadata, model) {
409
+ const output = {
410
+ input_tokens: usageMetadata?.promptTokenCount ?? 0,
411
+ output_tokens: usageMetadata?.candidatesTokenCount ?? 0,
412
+ total_tokens: usageMetadata?.totalTokenCount ?? 0
413
+ };
414
+ if (usageMetadata?.cachedContentTokenCount) {
415
+ output.input_token_details ??= {};
416
+ output.input_token_details.cache_read = usageMetadata.cachedContentTokenCount;
417
+ }
418
+ if (model === "gemini-3-pro-preview") {
419
+ const over200k = Math.max(0, usageMetadata?.promptTokenCount ?? -2e5);
420
+ const cachedOver200k = Math.max(0, usageMetadata?.cachedContentTokenCount ?? -2e5);
421
+ if (over200k) output.input_token_details = {
422
+ ...output.input_token_details,
423
+ over_200k: over200k
424
+ };
425
+ if (cachedOver200k) output.input_token_details = {
426
+ ...output.input_token_details,
427
+ cache_read_over_200k: cachedOver200k
428
+ };
429
+ }
430
+ return output;
431
+ }
432
+
433
+ //#endregion
434
+ export { convertBaseMessagesToContent, convertResponseContentToChatGenerationChunk, convertToGenerativeAITools, convertUsageMetadata, mapGenerateContentResultToChatResult };
435
+ //# sourceMappingURL=common.js.map