@nocobase/plugin-ai 2.2.0-beta.8 → 2.2.0-beta.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 (75) hide show
  1. package/dist/ai/docs/nocobase/api/app/env.md +28 -1
  2. package/dist/ai/docs/nocobase/api/cli/config/delete.md +8 -6
  3. package/dist/ai/docs/nocobase/api/cli/config/get.md +8 -6
  4. package/dist/ai/docs/nocobase/api/cli/config/index.md +37 -26
  5. package/dist/ai/docs/nocobase/api/cli/config/set.md +14 -8
  6. package/dist/ai/docs/nocobase/file-manager/field-attachment.md +6 -0
  7. package/dist/ai/docs/nocobase/file-manager/file-preview/index.md +3 -12
  8. package/dist/ai/docs/nocobase/file-manager/storage/aliyun-oss.md +9 -0
  9. package/dist/ai/docs/nocobase/file-manager/storage/amazon-s3.md +9 -0
  10. package/dist/ai/docs/nocobase/file-manager/storage/index.md +23 -0
  11. package/dist/ai/docs/nocobase/file-manager/storage/local.md +9 -0
  12. package/dist/ai/docs/nocobase/file-manager/storage/migrate-to-s3-pro.md +269 -0
  13. package/dist/ai/docs/nocobase/file-manager/storage/tencent-cos.md +9 -0
  14. package/dist/ai/docs/nocobase/get-started/installation/docker-caddy.mdx +175 -0
  15. package/dist/ai/docs/nocobase/get-started/installation/docker-nginx.mdx +176 -0
  16. package/dist/ai/docs/nocobase/get-started/installation/docker.mdx +94 -1
  17. package/dist/ai/docs/nocobase/get-started/installation/env.md +14 -4
  18. package/dist/ai/docs/nocobase/ops-management/version-control/index.md +1 -0
  19. package/dist/client/{428.e9f38da3b0d8b498.js → 428.5eb4f53ddbb6a3b8.js} +1 -1
  20. package/dist/client/559.9f97ebcdb0bd6231.js +10 -0
  21. package/dist/client/index.js +3 -3
  22. package/dist/client/llm-providers/mistral/ModelSettings.d.ts +10 -0
  23. package/dist/client/llm-providers/mistral/index.d.ts +10 -0
  24. package/dist/client/llm-services/utils.d.ts +5 -0
  25. package/dist/client-v2/index.d.ts +0 -1
  26. package/dist/client-v2/index.js +1 -1
  27. package/dist/externalVersion.js +16 -16
  28. package/dist/locale/en-US.json +1 -0
  29. package/dist/locale/zh-CN.json +1 -0
  30. package/dist/node_modules/@langchain/mistralai/LICENSE +21 -0
  31. package/dist/node_modules/@langchain/mistralai/dist/_virtual/rolldown_runtime.cjs +25 -0
  32. package/dist/node_modules/@langchain/mistralai/dist/chat_models.cjs +859 -0
  33. package/dist/node_modules/@langchain/mistralai/dist/chat_models.d.cts +541 -0
  34. package/dist/node_modules/@langchain/mistralai/dist/chat_models.d.ts +541 -0
  35. package/dist/node_modules/@langchain/mistralai/dist/chat_models.js +857 -0
  36. package/dist/node_modules/@langchain/mistralai/dist/embeddings.cjs +140 -0
  37. package/dist/node_modules/@langchain/mistralai/dist/embeddings.d.cts +116 -0
  38. package/dist/node_modules/@langchain/mistralai/dist/embeddings.d.ts +116 -0
  39. package/dist/node_modules/@langchain/mistralai/dist/embeddings.js +139 -0
  40. package/dist/node_modules/@langchain/mistralai/dist/index.cjs +21 -0
  41. package/dist/node_modules/@langchain/mistralai/dist/index.d.cts +4 -0
  42. package/dist/node_modules/@langchain/mistralai/dist/index.d.ts +4 -0
  43. package/dist/node_modules/@langchain/mistralai/dist/index.js +5 -0
  44. package/dist/node_modules/@langchain/mistralai/dist/llms.cjs +275 -0
  45. package/dist/node_modules/@langchain/mistralai/dist/llms.d.cts +147 -0
  46. package/dist/node_modules/@langchain/mistralai/dist/llms.d.ts +147 -0
  47. package/dist/node_modules/@langchain/mistralai/dist/llms.js +274 -0
  48. package/dist/node_modules/@langchain/mistralai/dist/utils.cjs +61 -0
  49. package/dist/node_modules/@langchain/mistralai/dist/utils.js +59 -0
  50. package/dist/node_modules/@langchain/mistralai/package.json +1 -0
  51. package/dist/node_modules/@langchain/xai/dist/index.cjs +2 -2
  52. package/dist/node_modules/@langchain/xai/package.json +1 -1
  53. package/dist/node_modules/fs-extra/package.json +1 -1
  54. package/dist/node_modules/jsonrepair/package.json +1 -1
  55. package/dist/node_modules/just-bash/package.json +1 -1
  56. package/dist/node_modules/nodejs-snowflake/package.json +1 -1
  57. package/dist/node_modules/openai/package.json +1 -1
  58. package/dist/node_modules/zod/package.json +1 -1
  59. package/dist/server/ai-employees/ai-employee.js +7 -5
  60. package/dist/server/ai-employees/checkpoints/cleaner.d.ts +2 -0
  61. package/dist/server/ai-employees/checkpoints/cleaner.js +11 -1
  62. package/dist/server/ai-employees/middleware/conversation.d.ts +2 -0
  63. package/dist/server/ai-employees/middleware/conversation.js +2 -1
  64. package/dist/server/ai-employees/utils.d.ts +3 -1
  65. package/dist/server/ai-employees/utils.js +2 -0
  66. package/dist/server/llm-providers/mistral.d.ts +62 -0
  67. package/dist/server/llm-providers/mistral.js +265 -0
  68. package/dist/server/llm-providers/provider.d.ts +6 -2
  69. package/dist/server/llm-providers/provider.js +27 -14
  70. package/dist/server/plugin.js +2 -0
  71. package/dist/server/workflow/nodes/employee/index.js +92 -46
  72. package/dist/server/workflow/nodes/employee/tools.js +8 -1
  73. package/package.json +3 -2
  74. package/dist/client/559.a0f2f1cc2be3c039.js +0 -10
  75. package/dist/client-v2/ai-employees/flow/models/AIEmployeeButtonModel.d.ts +0 -32
@@ -0,0 +1,859 @@
1
+ const require_rolldown_runtime = require('./_virtual/rolldown_runtime.cjs');
2
+ const require_utils = require('./utils.cjs');
3
+ const uuid = require_rolldown_runtime.__toESM(require("uuid"));
4
+ const __mistralai_mistralai = require_rolldown_runtime.__toESM(require("@mistralai/mistralai"));
5
+ const __mistralai_mistralai_lib_http_js = require_rolldown_runtime.__toESM(require("@mistralai/mistralai/lib/http.js"));
6
+ const __langchain_core_messages = require_rolldown_runtime.__toESM(require("@langchain/core/messages"));
7
+ const __langchain_core_language_models_chat_models = require_rolldown_runtime.__toESM(require("@langchain/core/language_models/chat_models"));
8
+ const __langchain_core_outputs = require_rolldown_runtime.__toESM(require("@langchain/core/outputs"));
9
+ const __langchain_core_utils_async_caller = require_rolldown_runtime.__toESM(require("@langchain/core/utils/async_caller"));
10
+ const __langchain_core_utils_env = require_rolldown_runtime.__toESM(require("@langchain/core/utils/env"));
11
+ const __langchain_core_output_parsers = require_rolldown_runtime.__toESM(require("@langchain/core/output_parsers"));
12
+ const __langchain_core_output_parsers_openai_tools = require_rolldown_runtime.__toESM(require("@langchain/core/output_parsers/openai_tools"));
13
+ const __langchain_core_runnables = require_rolldown_runtime.__toESM(require("@langchain/core/runnables"));
14
+ const __langchain_core_utils_json_schema = require_rolldown_runtime.__toESM(require("@langchain/core/utils/json_schema"));
15
+ const __langchain_core_utils_function_calling = require_rolldown_runtime.__toESM(require("@langchain/core/utils/function_calling"));
16
+ const __langchain_core_utils_types = require_rolldown_runtime.__toESM(require("@langchain/core/utils/types"));
17
+
18
+ //#region src/chat_models.ts
19
+ function convertMessagesToMistralMessages(messages) {
20
+ const getRole = (role) => {
21
+ switch (role) {
22
+ case "human": return "user";
23
+ case "ai": return "assistant";
24
+ case "system": return "system";
25
+ case "tool": return "tool";
26
+ case "function": return "assistant";
27
+ default: throw new Error(`Unknown message type: ${role}`);
28
+ }
29
+ };
30
+ const getContent = (content, type) => {
31
+ const _generateContentChunk = (complex, role) => {
32
+ if (complex.type === "image_url" && (role === "user" || role === "assistant")) return {
33
+ type: complex.type,
34
+ imageUrl: complex?.image_url
35
+ };
36
+ if (complex.type === "text") return {
37
+ type: complex.type,
38
+ text: complex?.text
39
+ };
40
+ throw new Error(`ChatMistralAI only supports messages of "image_url" for roles "user" and "assistant", and "text" for all others.\n\nReceived: ${JSON.stringify(content, null, 2)}`);
41
+ };
42
+ if (typeof content === "string") return content;
43
+ if (Array.isArray(content)) {
44
+ const mistralRole = getRole(type);
45
+ const newContent = [];
46
+ content.forEach((messageContentComplex) => {
47
+ if (messageContentComplex.type === "text" || messageContentComplex.type === "image_url") newContent.push(_generateContentChunk(messageContentComplex, mistralRole));
48
+ else throw new Error(`Mistral only supports types "text" or "image_url" for complex message types.`);
49
+ });
50
+ return newContent;
51
+ }
52
+ throw new Error(`Message content must be a string or an array.\n\nReceived: ${JSON.stringify(content, null, 2)}`);
53
+ };
54
+ const getTools = (message) => {
55
+ if ((0, __langchain_core_messages.isAIMessage)(message) && !!message.tool_calls?.length) return message.tool_calls.map((toolCall) => ({
56
+ ...toolCall,
57
+ id: require_utils._convertToolCallIdToMistralCompatible(toolCall.id ?? "")
58
+ })).map(__langchain_core_output_parsers_openai_tools.convertLangChainToolCallToOpenAI);
59
+ return void 0;
60
+ };
61
+ const toolResponseIds = /* @__PURE__ */ new Set();
62
+ for (const m of messages) if ("tool_call_id" in m && typeof m.tool_call_id === "string") toolResponseIds.add(require_utils._convertToolCallIdToMistralCompatible(m.tool_call_id));
63
+ return messages.flatMap((message) => {
64
+ const toolCalls = getTools(message);
65
+ const content = getContent(message.content, message.getType());
66
+ if ("tool_call_id" in message && typeof message.tool_call_id === "string") return [{
67
+ role: getRole(message.getType()),
68
+ content,
69
+ name: message.name,
70
+ toolCallId: require_utils._convertToolCallIdToMistralCompatible(message.tool_call_id)
71
+ }];
72
+ else if ((0, __langchain_core_messages.isAIMessage)(message)) if (toolCalls === void 0) return [{
73
+ role: getRole(message.getType()),
74
+ content
75
+ }];
76
+ else {
77
+ const filteredToolCalls = toolCalls.filter((tc) => toolResponseIds.has(require_utils._convertToolCallIdToMistralCompatible(tc.id ?? "")));
78
+ if (filteredToolCalls.length === 0) {
79
+ const isEmptyContent = typeof content === "string" && content.trim() === "" || Array.isArray(content) && content.length === 0;
80
+ if (isEmptyContent) return [];
81
+ return [{
82
+ role: getRole(message.getType()),
83
+ content
84
+ }];
85
+ }
86
+ return [{
87
+ role: getRole(message.getType()),
88
+ toolCalls: filteredToolCalls
89
+ }];
90
+ }
91
+ return [{
92
+ role: getRole(message.getType()),
93
+ content
94
+ }];
95
+ });
96
+ }
97
+ function mistralAIResponseToChatMessage(choice, usage) {
98
+ const { message } = choice;
99
+ if (message === void 0) throw new Error("No message found in response");
100
+ let rawToolCalls = [];
101
+ if ("toolCalls" in message && Array.isArray(message.toolCalls)) rawToolCalls = message.toolCalls;
102
+ const content = require_utils._mistralContentChunkToMessageContentComplex(message.content);
103
+ switch (message.role) {
104
+ case "assistant": {
105
+ const toolCalls = [];
106
+ const invalidToolCalls = [];
107
+ for (const rawToolCall of rawToolCalls) try {
108
+ const parsed = (0, __langchain_core_output_parsers_openai_tools.parseToolCall)(rawToolCall, { returnId: true });
109
+ toolCalls.push({
110
+ ...parsed,
111
+ id: parsed.id ?? (0, uuid.v4)().replace(/-/g, "")
112
+ });
113
+ } catch (e) {
114
+ invalidToolCalls.push((0, __langchain_core_output_parsers_openai_tools.makeInvalidToolCall)(rawToolCall, e.message));
115
+ }
116
+ return new __langchain_core_messages.AIMessage({
117
+ content,
118
+ tool_calls: toolCalls,
119
+ invalid_tool_calls: invalidToolCalls,
120
+ additional_kwargs: {},
121
+ usage_metadata: usage ? {
122
+ input_tokens: usage.promptTokens,
123
+ output_tokens: usage.completionTokens,
124
+ total_tokens: usage.totalTokens
125
+ } : void 0
126
+ });
127
+ }
128
+ default: return new __langchain_core_messages.HumanMessage({ content });
129
+ }
130
+ }
131
+ function _convertDeltaToMessageChunk(delta, usage) {
132
+ if (!delta.content && !delta.toolCalls) {
133
+ if (usage) return new __langchain_core_messages.AIMessageChunk({
134
+ content: "",
135
+ usage_metadata: usage ? {
136
+ input_tokens: usage.promptTokens,
137
+ output_tokens: usage.completionTokens,
138
+ total_tokens: usage.totalTokens
139
+ } : void 0
140
+ });
141
+ return null;
142
+ }
143
+ const rawToolCallChunksWithIndex = delta.toolCalls?.length ? delta.toolCalls?.map((toolCall, index) => ({
144
+ ...toolCall,
145
+ index,
146
+ id: toolCall.id ?? (0, uuid.v4)().replace(/-/g, ""),
147
+ type: "function"
148
+ })) : void 0;
149
+ let role = "assistant";
150
+ if (delta.role) role = delta.role;
151
+ const content = require_utils._mistralContentChunkToMessageContentComplex(delta.content);
152
+ let additional_kwargs;
153
+ const toolCallChunks = [];
154
+ if (rawToolCallChunksWithIndex !== void 0) for (const rawToolCallChunk of rawToolCallChunksWithIndex) {
155
+ const rawArgs = rawToolCallChunk.function?.arguments;
156
+ const args = rawArgs === void 0 || typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs);
157
+ toolCallChunks.push({
158
+ name: rawToolCallChunk.function?.name,
159
+ args,
160
+ id: rawToolCallChunk.id,
161
+ index: rawToolCallChunk.index,
162
+ type: "tool_call_chunk"
163
+ });
164
+ }
165
+ else additional_kwargs = {};
166
+ if (role === "user") return new __langchain_core_messages.HumanMessageChunk({ content });
167
+ else if (role === "assistant") return new __langchain_core_messages.AIMessageChunk({
168
+ content,
169
+ tool_call_chunks: toolCallChunks,
170
+ additional_kwargs,
171
+ usage_metadata: usage ? {
172
+ input_tokens: usage.promptTokens,
173
+ output_tokens: usage.completionTokens,
174
+ total_tokens: usage.totalTokens
175
+ } : void 0
176
+ });
177
+ else if (role === "tool") return new __langchain_core_messages.ToolMessageChunk({
178
+ content,
179
+ additional_kwargs,
180
+ tool_call_id: rawToolCallChunksWithIndex?.[0].id ?? ""
181
+ });
182
+ else if (role === "function") return new __langchain_core_messages.FunctionMessageChunk({
183
+ content,
184
+ additional_kwargs
185
+ });
186
+ else return new __langchain_core_messages.ChatMessageChunk({
187
+ content,
188
+ role
189
+ });
190
+ }
191
+ function _convertToolToMistralTool(tools) {
192
+ if (!tools || !tools.length) return void 0;
193
+ return tools.map((tool) => {
194
+ if ("function" in tool) return {
195
+ type: tool.type ?? "function",
196
+ function: tool.function
197
+ };
198
+ if ((0, __langchain_core_utils_function_calling.isLangChainTool)(tool)) {
199
+ const description = tool.description ?? `Tool: ${tool.name}`;
200
+ return {
201
+ type: "function",
202
+ function: {
203
+ name: tool.name,
204
+ description,
205
+ parameters: (0, __langchain_core_utils_types.isInteropZodSchema)(tool.schema) ? (0, __langchain_core_utils_json_schema.toJsonSchema)(tool.schema) : tool.schema
206
+ }
207
+ };
208
+ }
209
+ throw new Error(`Unknown tool type passed to ChatMistral: ${JSON.stringify(tool, null, 2)}`);
210
+ });
211
+ }
212
+ /**
213
+ * Mistral AI chat model integration.
214
+ *
215
+ * Setup:
216
+ * Install `@langchain/mistralai` and set an environment variable named `MISTRAL_API_KEY`.
217
+ *
218
+ * ```bash
219
+ * npm install @langchain/mistralai
220
+ * export MISTRAL_API_KEY="your-api-key"
221
+ * ```
222
+ *
223
+ * ## [Constructor args](https://api.js.langchain.com/classes/_langchain_mistralai.ChatMistralAI.html#constructor)
224
+ *
225
+ * ## [Runtime args](https://api.js.langchain.com/interfaces/_langchain_mistralai.ChatMistralAICallOptions.html)
226
+ *
227
+ * Runtime args can be passed as the second argument to any of the base runnable methods `.invoke`. `.stream`, `.batch`, etc.
228
+ * They can also be passed via `.withConfig`, or the second arg in `.bindTools`, like shown in the examples below:
229
+ *
230
+ * ```typescript
231
+ * // When calling `.withConfig`, call options should be passed via the first argument
232
+ * const llmWithArgsBound = llm.bindTools([...]) // tools array
233
+ * .withConfig({
234
+ * stop: ["\n"], // other call options
235
+ * });
236
+ *
237
+ * // You can also bind tools and call options like this
238
+ * const llmWithTools = llm.bindTools([...], {
239
+ * tool_choice: "auto",
240
+ * });
241
+ * ```
242
+ *
243
+ * ## Examples
244
+ *
245
+ * <details open>
246
+ * <summary><strong>Instantiate</strong></summary>
247
+ *
248
+ * ```typescript
249
+ * import { ChatMistralAI } from '@langchain/mistralai';
250
+ *
251
+ * const llm = new ChatMistralAI({
252
+ * model: "mistral-large-2402",
253
+ * temperature: 0,
254
+ * // other params...
255
+ * });
256
+ * ```
257
+ * </details>
258
+ *
259
+ * <br />
260
+ *
261
+ * <details>
262
+ * <summary><strong>Invoking</strong></summary>
263
+ *
264
+ * ```typescript
265
+ * const input = `Translate "I love programming" into French.`;
266
+ *
267
+ * // Models also accept a list of chat messages or a formatted prompt
268
+ * const result = await llm.invoke(input);
269
+ * console.log(result);
270
+ * ```
271
+ *
272
+ * ```txt
273
+ * AIMessage {
274
+ * "content": "The translation of \"I love programming\" into French is \"J'aime la programmation\". Here's the breakdown:\n\n- \"I\" translates to \"Je\"\n- \"love\" translates to \"aime\"\n- \"programming\" translates to \"la programmation\"\n\nSo, \"J'aime la programmation\" means \"I love programming\" in French.",
275
+ * "additional_kwargs": {},
276
+ * "response_metadata": {
277
+ * "tokenUsage": {
278
+ * "completionTokens": 89,
279
+ * "promptTokens": 13,
280
+ * "totalTokens": 102
281
+ * },
282
+ * "finish_reason": "stop"
283
+ * },
284
+ * "tool_calls": [],
285
+ * "invalid_tool_calls": [],
286
+ * "usage_metadata": {
287
+ * "input_tokens": 13,
288
+ * "output_tokens": 89,
289
+ * "total_tokens": 102
290
+ * }
291
+ * }
292
+ * ```
293
+ * </details>
294
+ *
295
+ * <br />
296
+ *
297
+ * <details>
298
+ * <summary><strong>Streaming Chunks</strong></summary>
299
+ *
300
+ * ```typescript
301
+ * for await (const chunk of await llm.stream(input)) {
302
+ * console.log(chunk);
303
+ * }
304
+ * ```
305
+ *
306
+ * ```txt
307
+ * AIMessageChunk {
308
+ * "content": "The",
309
+ * "additional_kwargs": {},
310
+ * "response_metadata": {
311
+ * "prompt": 0,
312
+ * "completion": 0
313
+ * },
314
+ * "tool_calls": [],
315
+ * "tool_call_chunks": [],
316
+ * "invalid_tool_calls": []
317
+ * }
318
+ * AIMessageChunk {
319
+ * "content": " translation",
320
+ * "additional_kwargs": {},
321
+ * "response_metadata": {
322
+ * "prompt": 0,
323
+ * "completion": 0
324
+ * },
325
+ * "tool_calls": [],
326
+ * "tool_call_chunks": [],
327
+ * "invalid_tool_calls": []
328
+ * }
329
+ * AIMessageChunk {
330
+ * "content": " of",
331
+ * "additional_kwargs": {},
332
+ * "response_metadata": {
333
+ * "prompt": 0,
334
+ * "completion": 0
335
+ * },
336
+ * "tool_calls": [],
337
+ * "tool_call_chunks": [],
338
+ * "invalid_tool_calls": []
339
+ * }
340
+ * AIMessageChunk {
341
+ * "content": " \"",
342
+ * "additional_kwargs": {},
343
+ * "response_metadata": {
344
+ * "prompt": 0,
345
+ * "completion": 0
346
+ * },
347
+ * "tool_calls": [],
348
+ * "tool_call_chunks": [],
349
+ * "invalid_tool_calls": []
350
+ * }
351
+ * AIMessageChunk {
352
+ * "content": "I",
353
+ * "additional_kwargs": {},
354
+ * "response_metadata": {
355
+ * "prompt": 0,
356
+ * "completion": 0
357
+ * },
358
+ * "tool_calls": [],
359
+ * "tool_call_chunks": [],
360
+ * "invalid_tool_calls": []
361
+ * }
362
+ * AIMessageChunk {
363
+ * "content": ".",
364
+ * "additional_kwargs": {},
365
+ * "response_metadata": {
366
+ * "prompt": 0,
367
+ * "completion": 0
368
+ * },
369
+ * "tool_calls": [],
370
+ * "tool_call_chunks": [],
371
+ * "invalid_tool_calls": []
372
+ *}
373
+ *AIMessageChunk {
374
+ * "content": "",
375
+ * "additional_kwargs": {},
376
+ * "response_metadata": {
377
+ * "prompt": 0,
378
+ * "completion": 0
379
+ * },
380
+ * "tool_calls": [],
381
+ * "tool_call_chunks": [],
382
+ * "invalid_tool_calls": [],
383
+ * "usage_metadata": {
384
+ * "input_tokens": 13,
385
+ * "output_tokens": 89,
386
+ * "total_tokens": 102
387
+ * }
388
+ *}
389
+ * ```
390
+ * </details>
391
+ *
392
+ * <br />
393
+ *
394
+ * <details>
395
+ * <summary><strong>Aggregate Streamed Chunks</strong></summary>
396
+ *
397
+ * ```typescript
398
+ * import { AIMessageChunk } from '@langchain/core/messages';
399
+ * import { concat } from '@langchain/core/utils/stream';
400
+ *
401
+ * const stream = await llm.stream(input);
402
+ * let full: AIMessageChunk | undefined;
403
+ * for await (const chunk of stream) {
404
+ * full = !full ? chunk : concat(full, chunk);
405
+ * }
406
+ * console.log(full);
407
+ * ```
408
+ *
409
+ * ```txt
410
+ * AIMessageChunk {
411
+ * "content": "The translation of \"I love programming\" into French is \"J'aime la programmation\". Here's the breakdown:\n\n- \"I\" translates to \"Je\"\n- \"love\" translates to \"aime\"\n- \"programming\" translates to \"la programmation\"\n\nSo, \"J'aime la programmation\" means \"I love programming\" in French.",
412
+ * "additional_kwargs": {},
413
+ * "response_metadata": {
414
+ * "prompt": 0,
415
+ * "completion": 0
416
+ * },
417
+ * "tool_calls": [],
418
+ * "tool_call_chunks": [],
419
+ * "invalid_tool_calls": [],
420
+ * "usage_metadata": {
421
+ * "input_tokens": 13,
422
+ * "output_tokens": 89,
423
+ * "total_tokens": 102
424
+ * }
425
+ * }
426
+ * ```
427
+ * </details>
428
+ *
429
+ * <br />
430
+ *
431
+ * <details>
432
+ * <summary><strong>Bind tools</strong></summary>
433
+ *
434
+ * ```typescript
435
+ * import { z } from 'zod';
436
+ *
437
+ * const GetWeather = {
438
+ * name: "GetWeather",
439
+ * description: "Get the current weather in a given location",
440
+ * schema: z.object({
441
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
442
+ * }),
443
+ * }
444
+ *
445
+ * const GetPopulation = {
446
+ * name: "GetPopulation",
447
+ * description: "Get the current population in a given location",
448
+ * schema: z.object({
449
+ * location: z.string().describe("The city and state, e.g. San Francisco, CA")
450
+ * }),
451
+ * }
452
+ *
453
+ * const llmWithTools = llm.bindTools([GetWeather, GetPopulation]);
454
+ * const aiMsg = await llmWithTools.invoke(
455
+ * "Which city is hotter today and which is bigger: LA or NY?"
456
+ * );
457
+ * console.log(aiMsg.tool_calls);
458
+ * ```
459
+ *
460
+ * ```txt
461
+ * [
462
+ * {
463
+ * name: 'GetWeather',
464
+ * args: { location: 'Los Angeles, CA' },
465
+ * type: 'tool_call',
466
+ * id: '47i216yko'
467
+ * },
468
+ * {
469
+ * name: 'GetWeather',
470
+ * args: { location: 'New York, NY' },
471
+ * type: 'tool_call',
472
+ * id: 'nb3v8Fpcn'
473
+ * },
474
+ * {
475
+ * name: 'GetPopulation',
476
+ * args: { location: 'Los Angeles, CA' },
477
+ * type: 'tool_call',
478
+ * id: 'EedWzByIB'
479
+ * },
480
+ * {
481
+ * name: 'GetPopulation',
482
+ * args: { location: 'New York, NY' },
483
+ * type: 'tool_call',
484
+ * id: 'jLdLia7zC'
485
+ * }
486
+ * ]
487
+ * ```
488
+ * </details>
489
+ *
490
+ * <br />
491
+ *
492
+ * <details>
493
+ * <summary><strong>Structured Output</strong></summary>
494
+ *
495
+ * ```typescript
496
+ * import { z } from 'zod';
497
+ *
498
+ * const Joke = z.object({
499
+ * setup: z.string().describe("The setup of the joke"),
500
+ * punchline: z.string().describe("The punchline to the joke"),
501
+ * rating: z.number().optional().describe("How funny the joke is, from 1 to 10")
502
+ * }).describe('Joke to tell user.');
503
+ *
504
+ * const structuredLlm = llm.withStructuredOutput(Joke, { name: "Joke" });
505
+ * const jokeResult = await structuredLlm.invoke("Tell me a joke about cats");
506
+ * console.log(jokeResult);
507
+ * ```
508
+ *
509
+ * ```txt
510
+ * {
511
+ * setup: "Why don't cats play poker in the jungle?",
512
+ * punchline: 'Too many cheetahs!',
513
+ * rating: 7
514
+ * }
515
+ * ```
516
+ * </details>
517
+ *
518
+ * <br />
519
+ *
520
+ * <details>
521
+ * <summary><strong>Usage Metadata</strong></summary>
522
+ *
523
+ * ```typescript
524
+ * const aiMsgForMetadata = await llm.invoke(input);
525
+ * console.log(aiMsgForMetadata.usage_metadata);
526
+ * ```
527
+ *
528
+ * ```txt
529
+ * { input_tokens: 13, output_tokens: 89, total_tokens: 102 }
530
+ * ```
531
+ * </details>
532
+ *
533
+ * <br />
534
+ */
535
+ var ChatMistralAI = class extends __langchain_core_language_models_chat_models.BaseChatModel {
536
+ static lc_name() {
537
+ return "ChatMistralAI";
538
+ }
539
+ lc_namespace = [
540
+ "langchain",
541
+ "chat_models",
542
+ "mistralai"
543
+ ];
544
+ model = "mistral-small-latest";
545
+ apiKey;
546
+ /**
547
+ * @deprecated use serverURL instead
548
+ */
549
+ endpoint;
550
+ serverURL;
551
+ temperature = .7;
552
+ streaming = false;
553
+ topP = 1;
554
+ maxTokens;
555
+ /**
556
+ * @deprecated use safePrompt instead
557
+ */
558
+ safeMode = false;
559
+ safePrompt = false;
560
+ randomSeed;
561
+ seed;
562
+ maxRetries;
563
+ lc_serializable = true;
564
+ streamUsage = true;
565
+ beforeRequestHooks;
566
+ requestErrorHooks;
567
+ responseHooks;
568
+ httpClient;
569
+ presencePenalty;
570
+ frequencyPenalty;
571
+ numCompletions;
572
+ constructor(fields) {
573
+ super(fields ?? {});
574
+ const apiKey = fields?.apiKey ?? (0, __langchain_core_utils_env.getEnvironmentVariable)("MISTRAL_API_KEY");
575
+ if (!apiKey) throw new Error("API key MISTRAL_API_KEY is missing for MistralAI, but it is required.");
576
+ this.apiKey = apiKey;
577
+ this.streaming = fields?.streaming ?? this.streaming;
578
+ this.serverURL = fields?.serverURL ?? this.serverURL;
579
+ this.temperature = fields?.temperature ?? this.temperature;
580
+ this.topP = fields?.topP ?? this.topP;
581
+ this.maxTokens = fields?.maxTokens ?? this.maxTokens;
582
+ this.safePrompt = fields?.safePrompt ?? this.safePrompt;
583
+ this.randomSeed = fields?.seed ?? fields?.randomSeed ?? this.seed;
584
+ this.seed = this.randomSeed;
585
+ this.maxRetries = fields?.maxRetries;
586
+ this.httpClient = fields?.httpClient;
587
+ this.model = fields?.model ?? fields?.modelName ?? this.model;
588
+ this.streamUsage = fields?.streamUsage ?? this.streamUsage;
589
+ this.beforeRequestHooks = fields?.beforeRequestHooks ?? this.beforeRequestHooks;
590
+ this.requestErrorHooks = fields?.requestErrorHooks ?? this.requestErrorHooks;
591
+ this.responseHooks = fields?.responseHooks ?? this.responseHooks;
592
+ this.presencePenalty = fields?.presencePenalty ?? this.presencePenalty;
593
+ this.frequencyPenalty = fields?.frequencyPenalty ?? this.frequencyPenalty;
594
+ this.numCompletions = fields?.numCompletions ?? this.numCompletions;
595
+ this.addAllHooksToHttpClient();
596
+ }
597
+ get lc_secrets() {
598
+ return { apiKey: "MISTRAL_API_KEY" };
599
+ }
600
+ get lc_aliases() {
601
+ return { apiKey: "mistral_api_key" };
602
+ }
603
+ getLsParams(options) {
604
+ const params = this.invocationParams(options);
605
+ return {
606
+ ls_provider: "mistral",
607
+ ls_model_name: this.model,
608
+ ls_model_type: "chat",
609
+ ls_temperature: params.temperature ?? void 0,
610
+ ls_max_tokens: params.maxTokens ?? void 0
611
+ };
612
+ }
613
+ _llmType() {
614
+ return "mistral_ai";
615
+ }
616
+ /**
617
+ * Get the parameters used to invoke the model
618
+ */
619
+ invocationParams(options) {
620
+ const { response_format, tools, tool_choice } = options ?? {};
621
+ const mistralAITools = tools?.length ? _convertToolToMistralTool(tools) : void 0;
622
+ const params = {
623
+ model: this.model,
624
+ tools: mistralAITools,
625
+ temperature: this.temperature,
626
+ maxTokens: this.maxTokens,
627
+ topP: this.topP,
628
+ randomSeed: this.seed,
629
+ safePrompt: this.safePrompt,
630
+ toolChoice: tool_choice,
631
+ responseFormat: response_format,
632
+ presencePenalty: this.presencePenalty,
633
+ frequencyPenalty: this.frequencyPenalty,
634
+ n: this.numCompletions
635
+ };
636
+ return params;
637
+ }
638
+ bindTools(tools, kwargs) {
639
+ const mistralTools = _convertToolToMistralTool(tools);
640
+ return new __langchain_core_runnables.RunnableBinding({
641
+ bound: this,
642
+ kwargs: {
643
+ ...kwargs ?? {},
644
+ tools: mistralTools
645
+ },
646
+ config: {}
647
+ });
648
+ }
649
+ async completionWithRetry(input, streaming) {
650
+ const caller = new __langchain_core_utils_async_caller.AsyncCaller({ maxRetries: this.maxRetries });
651
+ const client = new __mistralai_mistralai.Mistral({
652
+ apiKey: this.apiKey,
653
+ serverURL: this.serverURL,
654
+ ...this.httpClient ? { httpClient: this.httpClient } : {}
655
+ });
656
+ return caller.call(async () => {
657
+ try {
658
+ let res;
659
+ if (streaming) res = await client.chat.stream(input);
660
+ else res = await client.chat.complete(input);
661
+ return res;
662
+ } catch (e) {
663
+ if (e.message?.includes("status: 400") || e.message?.toLowerCase().includes("status 400") || e.message?.includes("validation failed")) e.status = 400;
664
+ throw e;
665
+ }
666
+ });
667
+ }
668
+ /** @ignore */
669
+ async _generate(messages, options, runManager) {
670
+ const tokenUsage = {};
671
+ const params = this.invocationParams(options);
672
+ const mistralMessages = convertMessagesToMistralMessages(messages);
673
+ const input = {
674
+ ...params,
675
+ messages: mistralMessages
676
+ };
677
+ const shouldStream = options.signal ?? !!options.timeout;
678
+ if (this.streaming || shouldStream) {
679
+ const stream = this._streamResponseChunks(messages, options, runManager);
680
+ const finalChunks = {};
681
+ for await (const chunk of stream) {
682
+ const index = chunk.generationInfo?.completion ?? 0;
683
+ if (finalChunks[index] === void 0) finalChunks[index] = chunk;
684
+ else finalChunks[index] = finalChunks[index].concat(chunk);
685
+ }
686
+ const generations$1 = Object.entries(finalChunks).sort(([aKey], [bKey]) => parseInt(aKey, 10) - parseInt(bKey, 10)).map(([_, value]) => value);
687
+ return {
688
+ generations: generations$1,
689
+ llmOutput: { estimatedTokenUsage: tokenUsage }
690
+ };
691
+ }
692
+ const response = await this.completionWithRetry(input, false);
693
+ const { completionTokens, promptTokens, totalTokens } = response?.usage ?? {};
694
+ if (completionTokens) tokenUsage.completionTokens = (tokenUsage.completionTokens ?? 0) + completionTokens;
695
+ if (promptTokens) tokenUsage.promptTokens = (tokenUsage.promptTokens ?? 0) + promptTokens;
696
+ if (totalTokens) tokenUsage.totalTokens = (tokenUsage.totalTokens ?? 0) + totalTokens;
697
+ const generations = [];
698
+ for (const part of response?.choices ?? []) {
699
+ if ("delta" in part) throw new Error("Delta not supported in non-streaming mode.");
700
+ if (!("message" in part)) throw new Error("No message found in the choice.");
701
+ let text = part.message?.content ?? "";
702
+ if (Array.isArray(text)) text = text[0].type === "text" ? text[0].text : "";
703
+ const generation = {
704
+ text,
705
+ message: mistralAIResponseToChatMessage(part, response?.usage)
706
+ };
707
+ if (part.finishReason) generation.generationInfo = { finishReason: part.finishReason };
708
+ generations.push(generation);
709
+ }
710
+ return {
711
+ generations,
712
+ llmOutput: { tokenUsage }
713
+ };
714
+ }
715
+ async *_streamResponseChunks(messages, options, runManager) {
716
+ const mistralMessages = convertMessagesToMistralMessages(messages);
717
+ const params = this.invocationParams(options);
718
+ const input = {
719
+ ...params,
720
+ messages: mistralMessages
721
+ };
722
+ const streamIterable = await this.completionWithRetry(input, true);
723
+ for await (const { data } of streamIterable) {
724
+ if (options.signal?.aborted) throw new Error("AbortError");
725
+ const choice = data?.choices[0];
726
+ if (!choice || !("delta" in choice)) continue;
727
+ const { delta } = choice;
728
+ if (!delta) continue;
729
+ const newTokenIndices = {
730
+ prompt: 0,
731
+ completion: choice.index ?? 0
732
+ };
733
+ const shouldStreamUsage = this.streamUsage || options.streamUsage;
734
+ const message = _convertDeltaToMessageChunk(delta, shouldStreamUsage ? data.usage : null);
735
+ if (message === null) continue;
736
+ let text = delta.content ?? "";
737
+ if (Array.isArray(text)) text = text[0].type === "text" ? text[0].text : "";
738
+ const generationChunk = new __langchain_core_outputs.ChatGenerationChunk({
739
+ message,
740
+ text,
741
+ generationInfo: newTokenIndices
742
+ });
743
+ yield generationChunk;
744
+ runManager?.handleLLMNewToken(generationChunk.text ?? "", newTokenIndices, void 0, void 0, void 0, { chunk: generationChunk });
745
+ }
746
+ }
747
+ addAllHooksToHttpClient() {
748
+ try {
749
+ this.removeAllHooksFromHttpClient();
750
+ const hasHooks = [
751
+ this.beforeRequestHooks,
752
+ this.requestErrorHooks,
753
+ this.responseHooks
754
+ ].some((hook) => hook && hook.length > 0);
755
+ if (hasHooks && !this.httpClient) this.httpClient = new __mistralai_mistralai_lib_http_js.HTTPClient();
756
+ if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.addHook("beforeRequest", hook);
757
+ if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.addHook("requestError", hook);
758
+ if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.addHook("response", hook);
759
+ } catch {
760
+ throw new Error("Error in adding all hooks");
761
+ }
762
+ }
763
+ removeAllHooksFromHttpClient() {
764
+ try {
765
+ if (this.beforeRequestHooks) for (const hook of this.beforeRequestHooks) this.httpClient?.removeHook("beforeRequest", hook);
766
+ if (this.requestErrorHooks) for (const hook of this.requestErrorHooks) this.httpClient?.removeHook("requestError", hook);
767
+ if (this.responseHooks) for (const hook of this.responseHooks) this.httpClient?.removeHook("response", hook);
768
+ } catch {
769
+ throw new Error("Error in removing hooks");
770
+ }
771
+ }
772
+ removeHookFromHttpClient(hook) {
773
+ try {
774
+ this.httpClient?.removeHook("beforeRequest", hook);
775
+ this.httpClient?.removeHook("requestError", hook);
776
+ this.httpClient?.removeHook("response", hook);
777
+ } catch {
778
+ throw new Error("Error in removing hook");
779
+ }
780
+ }
781
+ /** @ignore */
782
+ _combineLLMOutput() {
783
+ return [];
784
+ }
785
+ withStructuredOutput(outputSchema, config) {
786
+ const schema = outputSchema;
787
+ const name = config?.name;
788
+ const method = config?.method;
789
+ const includeRaw = config?.includeRaw;
790
+ let llm;
791
+ let outputParser;
792
+ if (method === "jsonMode") {
793
+ let outputSchema$1;
794
+ if ((0, __langchain_core_utils_types.isInteropZodSchema)(schema)) {
795
+ outputParser = __langchain_core_output_parsers.StructuredOutputParser.fromZodSchema(schema);
796
+ outputSchema$1 = (0, __langchain_core_utils_json_schema.toJsonSchema)(schema);
797
+ } else outputParser = new __langchain_core_output_parsers.JsonOutputParser();
798
+ llm = this.withConfig({
799
+ response_format: { type: "json_object" },
800
+ ls_structured_output_format: {
801
+ kwargs: { method: "jsonMode" },
802
+ schema: outputSchema$1
803
+ }
804
+ });
805
+ } else {
806
+ let functionName = name ?? "extract";
807
+ if ((0, __langchain_core_utils_types.isInteropZodSchema)(schema)) {
808
+ const asJsonSchema = (0, __langchain_core_utils_json_schema.toJsonSchema)(schema);
809
+ llm = this.bindTools([{
810
+ type: "function",
811
+ function: {
812
+ name: functionName,
813
+ description: asJsonSchema.description,
814
+ parameters: asJsonSchema
815
+ }
816
+ }]).withConfig({
817
+ tool_choice: "any",
818
+ ls_structured_output_format: {
819
+ kwargs: { method: "functionCalling" },
820
+ schema: asJsonSchema
821
+ }
822
+ });
823
+ outputParser = new __langchain_core_output_parsers_openai_tools.JsonOutputKeyToolsParser({
824
+ returnSingle: true,
825
+ keyName: functionName,
826
+ zodSchema: schema
827
+ });
828
+ } else {
829
+ let openAIFunctionDefinition;
830
+ if (typeof schema.name === "string" && typeof schema.parameters === "object" && schema.parameters != null) {
831
+ openAIFunctionDefinition = schema;
832
+ functionName = schema.name;
833
+ } else openAIFunctionDefinition = {
834
+ name: functionName,
835
+ description: schema.description ?? "",
836
+ parameters: schema
837
+ };
838
+ llm = this.bindTools([{
839
+ type: "function",
840
+ function: openAIFunctionDefinition
841
+ }]).withConfig({ tool_choice: "any" });
842
+ outputParser = new __langchain_core_output_parsers_openai_tools.JsonOutputKeyToolsParser({
843
+ returnSingle: true,
844
+ keyName: functionName
845
+ });
846
+ }
847
+ }
848
+ if (!includeRaw) return llm.pipe(outputParser);
849
+ const parserAssign = __langchain_core_runnables.RunnablePassthrough.assign({ parsed: (input, config$1) => outputParser.invoke(input.raw, config$1) });
850
+ const parserNone = __langchain_core_runnables.RunnablePassthrough.assign({ parsed: () => null });
851
+ const parsedWithFallback = parserAssign.withFallbacks({ fallbacks: [parserNone] });
852
+ return __langchain_core_runnables.RunnableSequence.from([{ raw: llm }, parsedWithFallback]);
853
+ }
854
+ };
855
+
856
+ //#endregion
857
+ exports.ChatMistralAI = ChatMistralAI;
858
+ exports.convertMessagesToMistralMessages = convertMessagesToMistralMessages;
859
+ //# sourceMappingURL=chat_models.cjs.map