@ai-sdk/mistral 4.0.53 → 4.0.55

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.
package/dist/index.js CHANGED
@@ -1,1395 +1,1102 @@
1
- // src/mistral-provider.ts
2
- import {
3
- NoSuchModelError
4
- } from "@ai-sdk/provider";
5
- import {
6
- loadApiKey,
7
- withoutTrailingSlash,
8
- withUserAgentSuffix
9
- } from "@ai-sdk/provider-utils";
10
-
11
- // src/mistral-chat-language-model.ts
12
- import {
13
- combineHeaders,
14
- createEventSourceResponseHandler,
15
- createJsonResponseHandler,
16
- generateId,
17
- injectJsonInstructionIntoMessages,
18
- isCustomReasoning,
19
- mapReasoningToProviderEffort,
20
- parseProviderOptions,
21
- postJsonToApi,
22
- serializeModelOptions,
23
- StreamingToolCallTracker,
24
- WORKFLOW_SERIALIZE,
25
- WORKFLOW_DESERIALIZE
26
- } from "@ai-sdk/provider-utils";
27
- import { z as z4 } from "zod/v4";
28
-
29
- // src/convert-mistral-usage.ts
30
- import { createNullLanguageModelUsage } from "@ai-sdk/provider-utils";
1
+ import { InvalidArgumentError, NoSuchModelError, TooManyEmbeddingValuesForCallError, UnsupportedFunctionalityError } from "@ai-sdk/provider";
2
+ import { StreamingToolCallTracker, WORKFLOW_DESERIALIZE, WORKFLOW_SERIALIZE, combineHeaders, convertBase64ToUint8Array, convertToBase64, createEventSourceResponseHandler, createJsonErrorResponseHandler, createJsonResponseHandler, createLanguageModelResponseMetadata as getResponseMetadata, createNullLanguageModelUsage, generateId, getTopLevelMediaType, injectJsonInstructionIntoMessages, isCustomReasoning, lazySchema, loadApiKey, mapReasoningToProviderEffort, mediaTypeToExtension, parseProviderOptions, postFormDataToApi, postJsonToApi, postToApi, resolveFullMediaType, serializeModelOptions, withUserAgentSuffix, withoutTrailingSlash, zodSchema } from "@ai-sdk/provider-utils";
31
3
  import { z } from "zod/v4";
32
- var mistralPromptTokensDetailsSchema = z.object({
33
- messages: z.array(z.json()).nullish(),
34
- cached_tokens: z.number().nullish(),
35
- audio_tokens: z.number().nullish()
4
+ //#region src/convert-mistral-usage.ts
5
+ const mistralPromptTokensDetailsSchema = z.object({
6
+ messages: z.array(z.json()).nullish(),
7
+ cached_tokens: z.number().nullish(),
8
+ audio_tokens: z.number().nullish()
36
9
  }).catchall(z.json());
37
- var mistralCompletionTokensDetailsSchema = z.object({
38
- reasoning_tokens: z.number().nullish()
39
- }).catchall(z.json());
40
- var mistralUsageSchema = z.object({
41
- prompt_tokens: z.number(),
42
- completion_tokens: z.number(),
43
- total_tokens: z.number(),
44
- prompt_audio_seconds: z.number().nullish(),
45
- request_count: z.number().nullish(),
46
- service_tier: z.string().nullish(),
47
- num_cached_tokens: z.number().nullish(),
48
- prompt_tokens_details: mistralPromptTokensDetailsSchema.nullish(),
49
- prompt_token_details: mistralPromptTokensDetailsSchema.nullish(),
50
- completion_tokens_details: mistralCompletionTokensDetailsSchema.nullish()
10
+ const mistralCompletionTokensDetailsSchema = z.object({ reasoning_tokens: z.number().nullish() }).catchall(z.json());
11
+ const mistralUsageSchema = z.object({
12
+ prompt_tokens: z.number(),
13
+ completion_tokens: z.number(),
14
+ total_tokens: z.number(),
15
+ prompt_audio_seconds: z.number().nullish(),
16
+ request_count: z.number().nullish(),
17
+ service_tier: z.string().nullish(),
18
+ num_cached_tokens: z.number().nullish(),
19
+ prompt_tokens_details: mistralPromptTokensDetailsSchema.nullish(),
20
+ prompt_token_details: mistralPromptTokensDetailsSchema.nullish(),
21
+ completion_tokens_details: mistralCompletionTokensDetailsSchema.nullish()
51
22
  }).catchall(z.json());
52
23
  function convertMistralUsage(usage) {
53
- if (usage == null) {
54
- return createNullLanguageModelUsage();
55
- }
56
- const promptTokens = usage.prompt_tokens;
57
- const completionTokens = usage.completion_tokens;
58
- const cacheReadTokens = usage.num_cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? usage.prompt_token_details?.cached_tokens ?? 0;
59
- return {
60
- inputTokens: {
61
- total: promptTokens,
62
- noCache: promptTokens - cacheReadTokens,
63
- cacheRead: cacheReadTokens || void 0,
64
- cacheWrite: void 0
65
- },
66
- outputTokens: {
67
- total: completionTokens,
68
- text: completionTokens,
69
- reasoning: void 0
70
- },
71
- raw: usage
72
- };
24
+ if (usage == null) return createNullLanguageModelUsage();
25
+ const promptTokens = usage.prompt_tokens;
26
+ const completionTokens = usage.completion_tokens;
27
+ const cacheReadTokens = usage.num_cached_tokens ?? usage.prompt_tokens_details?.cached_tokens ?? usage.prompt_token_details?.cached_tokens ?? 0;
28
+ return {
29
+ inputTokens: {
30
+ total: promptTokens,
31
+ noCache: promptTokens - cacheReadTokens,
32
+ cacheRead: cacheReadTokens || void 0,
33
+ cacheWrite: void 0
34
+ },
35
+ outputTokens: {
36
+ total: completionTokens,
37
+ text: completionTokens,
38
+ reasoning: void 0
39
+ },
40
+ raw: usage
41
+ };
73
42
  }
74
-
75
- // src/convert-to-mistral-chat-messages.ts
76
- import {
77
- UnsupportedFunctionalityError
78
- } from "@ai-sdk/provider";
79
- import {
80
- convertToBase64,
81
- getTopLevelMediaType,
82
- resolveFullMediaType
83
- } from "@ai-sdk/provider-utils";
43
+ //#endregion
44
+ //#region src/convert-to-mistral-chat-messages.ts
84
45
  function formatFileUrl({ part }) {
85
- if (part.data.type === "url") {
86
- return part.data.url.toString();
87
- }
88
- if (part.data.type === "data") {
89
- return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
90
- }
91
- throw new UnsupportedFunctionalityError({
92
- functionality: `file part data type ${part.data.type}`
93
- });
46
+ if (part.data.type === "url") return part.data.url.toString();
47
+ if (part.data.type === "data") return `data:${resolveFullMediaType({ part })};base64,${convertToBase64(part.data.data)}`;
48
+ throw new UnsupportedFunctionalityError({ functionality: `file part data type ${part.data.type}` });
94
49
  }
95
50
  function convertToMistralChatMessages(prompt) {
96
- const messages = [];
97
- for (let i = 0; i < prompt.length; i++) {
98
- const { role, content } = prompt[i];
99
- const isLastMessage = i === prompt.length - 1;
100
- switch (role) {
101
- case "system": {
102
- messages.push({ role: "system", content });
103
- break;
104
- }
105
- case "user": {
106
- messages.push({
107
- role: "user",
108
- content: content.map((part) => {
109
- switch (part.type) {
110
- case "text": {
111
- return { type: "text", text: part.text };
112
- }
113
- case "file": {
114
- switch (part.data.type) {
115
- case "reference": {
116
- throw new UnsupportedFunctionalityError({
117
- functionality: "file parts with provider references"
118
- });
119
- }
120
- case "text": {
121
- throw new UnsupportedFunctionalityError({
122
- functionality: "text file parts"
123
- });
124
- }
125
- case "url":
126
- case "data": {
127
- const topLevel = getTopLevelMediaType(part.mediaType);
128
- if (topLevel === "image") {
129
- return {
130
- type: "image_url",
131
- image_url: formatFileUrl({ part })
132
- };
133
- } else {
134
- if (part.data.type === "data") {
135
- const fullMediaType = resolveFullMediaType({ part });
136
- if (fullMediaType !== "application/pdf") {
137
- throw new UnsupportedFunctionalityError({
138
- functionality: "Only images and PDF file parts are supported"
139
- });
140
- }
141
- } else if (part.mediaType !== "application/pdf") {
142
- throw new UnsupportedFunctionalityError({
143
- functionality: "Only images and PDF file parts are supported"
144
- });
145
- }
146
- return {
147
- type: "document_url",
148
- document_url: formatFileUrl({ part })
149
- };
150
- }
151
- }
152
- }
153
- }
154
- }
155
- })
156
- });
157
- break;
158
- }
159
- case "assistant": {
160
- let text = "";
161
- let hasReasoning = false;
162
- const contentParts = [];
163
- const toolCalls = [];
164
- for (const part of content) {
165
- switch (part.type) {
166
- case "text": {
167
- text += part.text;
168
- contentParts.push({ type: "text", text: part.text });
169
- break;
170
- }
171
- case "tool-call": {
172
- toolCalls.push({
173
- id: part.toolCallId,
174
- type: "function",
175
- function: {
176
- name: part.toolName,
177
- arguments: JSON.stringify(part.input)
178
- }
179
- });
180
- break;
181
- }
182
- case "reasoning": {
183
- hasReasoning = true;
184
- contentParts.push({
185
- type: "thinking",
186
- thinking: [{ type: "text", text: part.text }],
187
- closed: true
188
- });
189
- break;
190
- }
191
- default: {
192
- throw new Error(
193
- `Unsupported content type in assistant message: ${part.type}`
194
- );
195
- }
196
- }
197
- }
198
- messages.push({
199
- role: "assistant",
200
- content: hasReasoning ? contentParts : text,
201
- prefix: isLastMessage ? true : void 0,
202
- tool_calls: toolCalls.length > 0 ? toolCalls : void 0
203
- });
204
- break;
205
- }
206
- case "tool": {
207
- for (const toolResponse of content) {
208
- if (toolResponse.type === "tool-approval-response") {
209
- continue;
210
- }
211
- const output = toolResponse.output;
212
- let contentValue;
213
- switch (output.type) {
214
- case "text":
215
- case "error-text":
216
- contentValue = output.value;
217
- break;
218
- case "execution-denied":
219
- contentValue = output.reason ?? "Tool call execution denied.";
220
- break;
221
- case "content":
222
- case "json":
223
- case "error-json":
224
- contentValue = JSON.stringify(output.value);
225
- break;
226
- }
227
- messages.push({
228
- role: "tool",
229
- name: toolResponse.toolName,
230
- tool_call_id: toolResponse.toolCallId,
231
- content: contentValue
232
- });
233
- }
234
- break;
235
- }
236
- default: {
237
- const _exhaustiveCheck = role;
238
- throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
239
- }
240
- }
241
- }
242
- return messages;
51
+ const messages = [];
52
+ for (let i = 0; i < prompt.length; i++) {
53
+ const { role, content } = prompt[i];
54
+ const isLastMessage = i === prompt.length - 1;
55
+ switch (role) {
56
+ case "system":
57
+ messages.push({
58
+ role: "system",
59
+ content
60
+ });
61
+ break;
62
+ case "user":
63
+ messages.push({
64
+ role: "user",
65
+ content: content.map((part) => {
66
+ switch (part.type) {
67
+ case "text": return {
68
+ type: "text",
69
+ text: part.text
70
+ };
71
+ case "file": switch (part.data.type) {
72
+ case "reference": throw new UnsupportedFunctionalityError({ functionality: "file parts with provider references" });
73
+ case "text": throw new UnsupportedFunctionalityError({ functionality: "text file parts" });
74
+ case "url":
75
+ case "data": if (getTopLevelMediaType(part.mediaType) === "image") return {
76
+ type: "image_url",
77
+ image_url: formatFileUrl({ part })
78
+ };
79
+ else {
80
+ if (part.data.type === "data") {
81
+ if (resolveFullMediaType({ part }) !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: "Only images and PDF file parts are supported" });
82
+ } else if (part.mediaType !== "application/pdf") throw new UnsupportedFunctionalityError({ functionality: "Only images and PDF file parts are supported" });
83
+ return {
84
+ type: "document_url",
85
+ document_url: formatFileUrl({ part })
86
+ };
87
+ }
88
+ }
89
+ }
90
+ })
91
+ });
92
+ break;
93
+ case "assistant": {
94
+ let text = "";
95
+ let hasReasoning = false;
96
+ const contentParts = [];
97
+ const toolCalls = [];
98
+ for (const part of content) switch (part.type) {
99
+ case "text":
100
+ text += part.text;
101
+ contentParts.push({
102
+ type: "text",
103
+ text: part.text
104
+ });
105
+ break;
106
+ case "tool-call":
107
+ toolCalls.push({
108
+ id: part.toolCallId,
109
+ type: "function",
110
+ function: {
111
+ name: part.toolName,
112
+ arguments: JSON.stringify(part.input)
113
+ }
114
+ });
115
+ break;
116
+ case "reasoning":
117
+ hasReasoning = true;
118
+ contentParts.push({
119
+ type: "thinking",
120
+ thinking: [{
121
+ type: "text",
122
+ text: part.text
123
+ }],
124
+ closed: true
125
+ });
126
+ break;
127
+ default: throw new Error(`Unsupported content type in assistant message: ${part.type}`);
128
+ }
129
+ messages.push({
130
+ role: "assistant",
131
+ content: hasReasoning ? contentParts : text,
132
+ prefix: isLastMessage ? true : void 0,
133
+ tool_calls: toolCalls.length > 0 ? toolCalls : void 0
134
+ });
135
+ break;
136
+ }
137
+ case "tool":
138
+ for (const toolResponse of content) {
139
+ if (toolResponse.type === "tool-approval-response") continue;
140
+ const output = toolResponse.output;
141
+ let contentValue;
142
+ switch (output.type) {
143
+ case "text":
144
+ case "error-text":
145
+ contentValue = output.value;
146
+ break;
147
+ case "execution-denied":
148
+ contentValue = output.reason ?? "Tool call execution denied.";
149
+ break;
150
+ case "content":
151
+ case "json":
152
+ case "error-json": contentValue = JSON.stringify(output.value);
153
+ }
154
+ messages.push({
155
+ role: "tool",
156
+ name: toolResponse.toolName,
157
+ tool_call_id: toolResponse.toolCallId,
158
+ content: contentValue
159
+ });
160
+ }
161
+ break;
162
+ default: throw new Error(`Unsupported role: ${role}`);
163
+ }
164
+ }
165
+ return messages;
243
166
  }
244
-
245
- // src/get-response-metadata.ts
246
- import { createLanguageModelResponseMetadata } from "@ai-sdk/provider-utils";
247
-
248
- // src/map-mistral-finish-reason.ts
167
+ //#endregion
168
+ //#region src/map-mistral-finish-reason.ts
249
169
  function mapMistralFinishReason(finishReason) {
250
- switch (finishReason) {
251
- case "stop":
252
- return "stop";
253
- case "length":
254
- case "model_length":
255
- return "length";
256
- case "tool_calls":
257
- return "tool-calls";
258
- default:
259
- return "other";
260
- }
170
+ switch (finishReason) {
171
+ case "stop": return "stop";
172
+ case "length":
173
+ case "model_length": return "length";
174
+ case "tool_calls": return "tool-calls";
175
+ case "error": return "error";
176
+ default: return "other";
177
+ }
261
178
  }
262
-
263
- // src/mistral-chat-language-model-options.ts
264
- import { z as z2 } from "zod/v4";
265
- var mistralLanguageModelChatOptions = z2.object({
266
- /**
267
- * Whether to inject a safety prompt before all conversations.
268
- *
269
- * Defaults to `false`.
270
- */
271
- safePrompt: z2.boolean().optional(),
272
- documentImageLimit: z2.number().optional(),
273
- documentPageLimit: z2.number().optional(),
274
- /**
275
- * Whether to use structured outputs.
276
- *
277
- * @default true
278
- */
279
- structuredOutputs: z2.boolean().optional(),
280
- /**
281
- * Whether to use strict JSON schema validation.
282
- *
283
- * @default false
284
- */
285
- strictJsonSchema: z2.boolean().optional(),
286
- /**
287
- * Whether to enable parallel function calling during tool use.
288
- * When set to false, the model will use at most one tool per response.
289
- *
290
- * @default true
291
- */
292
- parallelToolCalls: z2.boolean().optional(),
293
- /**
294
- * A stable identifier used to route requests with shared prompt prefixes to
295
- * the same cache.
296
- */
297
- promptCacheKey: z2.string().optional(),
298
- /**
299
- * Controls the reasoning effort for models that support adjustable reasoning.
300
- *
301
- * - `'high'`: Enable reasoning
302
- * - `'none'`: Disable reasoning
303
- */
304
- reasoningEffort: z2.enum(["high", "none"]).optional()
179
+ //#endregion
180
+ //#region src/mistral-chat-language-model-options.ts
181
+ const mistralLanguageModelChatOptions = z.object({
182
+ /**
183
+ * Whether to inject a safety prompt before all conversations.
184
+ *
185
+ * Defaults to `false`.
186
+ */
187
+ safePrompt: z.boolean().optional(),
188
+ documentImageLimit: z.number().optional(),
189
+ documentPageLimit: z.number().optional(),
190
+ /**
191
+ * Whether to use structured outputs.
192
+ *
193
+ * @default true
194
+ */
195
+ structuredOutputs: z.boolean().optional(),
196
+ /**
197
+ * Whether to use strict JSON schema validation.
198
+ *
199
+ * @default false
200
+ */
201
+ strictJsonSchema: z.boolean().optional(),
202
+ /**
203
+ * Whether to enable parallel function calling during tool use.
204
+ * When set to false, the model will use at most one tool per response.
205
+ *
206
+ * @default true
207
+ */
208
+ parallelToolCalls: z.boolean().optional(),
209
+ /**
210
+ * A stable identifier used to route requests with shared prompt prefixes to
211
+ * the same cache.
212
+ */
213
+ promptCacheKey: z.string().optional(),
214
+ /**
215
+ * Controls the reasoning effort for models that support adjustable reasoning.
216
+ *
217
+ * - `'high'`: Enable reasoning
218
+ * - `'none'`: Disable reasoning
219
+ */
220
+ reasoningEffort: z.enum(["high", "none"]).optional()
305
221
  });
306
-
307
- // src/mistral-error.ts
308
- import { createJsonErrorResponseHandler } from "@ai-sdk/provider-utils";
309
- import { z as z3 } from "zod/v4";
310
- var mistralErrorDataSchema = z3.object({
311
- object: z3.literal("error"),
312
- message: z3.string(),
313
- type: z3.string(),
314
- param: z3.string().nullable(),
315
- code: z3.string().nullable()
222
+ //#endregion
223
+ //#region src/mistral-error.ts
224
+ const mistralErrorDataSchema = z.object({
225
+ object: z.literal("error"),
226
+ message: z.string(),
227
+ type: z.string(),
228
+ param: z.string().nullable(),
229
+ code: z.string().nullable()
316
230
  });
317
- var mistralFailedResponseHandler = createJsonErrorResponseHandler({
318
- errorSchema: mistralErrorDataSchema,
319
- errorToMessage: (data) => data.message
231
+ const mistralFailedResponseHandler = createJsonErrorResponseHandler({
232
+ errorSchema: mistralErrorDataSchema,
233
+ errorToMessage: (data) => data.message
320
234
  });
321
-
322
- // src/mistral-prepare-tools.ts
323
- import {
324
- UnsupportedFunctionalityError as UnsupportedFunctionalityError2
325
- } from "@ai-sdk/provider";
326
- function prepareTools({
327
- tools,
328
- toolChoice
329
- }) {
330
- tools = tools?.length ? tools : void 0;
331
- const toolWarnings = [];
332
- if (tools == null) {
333
- return { tools: void 0, toolChoice: void 0, toolWarnings };
334
- }
335
- const mistralTools = [];
336
- for (const tool of tools) {
337
- if (tool.type === "provider") {
338
- toolWarnings.push({
339
- type: "unsupported",
340
- feature: `provider-defined tool ${tool.id}`
341
- });
342
- } else {
343
- mistralTools.push({
344
- type: "function",
345
- function: {
346
- name: tool.name,
347
- description: tool.description,
348
- parameters: tool.inputSchema,
349
- ...tool.strict != null ? { strict: tool.strict } : {}
350
- }
351
- });
352
- }
353
- }
354
- if (toolChoice == null) {
355
- return { tools: mistralTools, toolChoice: void 0, toolWarnings };
356
- }
357
- const type = toolChoice.type;
358
- switch (type) {
359
- case "auto":
360
- case "none":
361
- return { tools: mistralTools, toolChoice: type, toolWarnings };
362
- case "required":
363
- return { tools: mistralTools, toolChoice: "any", toolWarnings };
364
- // mistral does not support tool mode directly,
365
- // so we filter the tools and force the tool choice through 'any'
366
- case "tool":
367
- return {
368
- tools: mistralTools.filter(
369
- (tool) => tool.function.name === toolChoice.toolName
370
- ),
371
- toolChoice: "any",
372
- toolWarnings
373
- };
374
- default: {
375
- const _exhaustiveCheck = type;
376
- throw new UnsupportedFunctionalityError2({
377
- functionality: `tool choice type: ${_exhaustiveCheck}`
378
- });
379
- }
380
- }
235
+ //#endregion
236
+ //#region src/mistral-prepare-tools.ts
237
+ function prepareTools({ tools, toolChoice }) {
238
+ tools = tools?.length ? tools : void 0;
239
+ const toolWarnings = [];
240
+ if (tools == null) return {
241
+ tools: void 0,
242
+ toolChoice: void 0,
243
+ toolWarnings
244
+ };
245
+ const mistralTools = [];
246
+ for (const tool of tools) if (tool.type === "provider") toolWarnings.push({
247
+ type: "unsupported",
248
+ feature: `provider-defined tool ${tool.id}`
249
+ });
250
+ else mistralTools.push({
251
+ type: "function",
252
+ function: {
253
+ name: tool.name,
254
+ description: tool.description,
255
+ parameters: tool.inputSchema,
256
+ ...tool.strict != null ? { strict: tool.strict } : {}
257
+ }
258
+ });
259
+ if (toolChoice == null) return {
260
+ tools: mistralTools,
261
+ toolChoice: void 0,
262
+ toolWarnings
263
+ };
264
+ const type = toolChoice.type;
265
+ switch (type) {
266
+ case "auto":
267
+ case "none": return {
268
+ tools: mistralTools,
269
+ toolChoice: type,
270
+ toolWarnings
271
+ };
272
+ case "required": return {
273
+ tools: mistralTools,
274
+ toolChoice: "any",
275
+ toolWarnings
276
+ };
277
+ case "tool": return {
278
+ tools: mistralTools.filter((tool) => tool.function.name === toolChoice.toolName),
279
+ toolChoice: "any",
280
+ toolWarnings
281
+ };
282
+ default: throw new UnsupportedFunctionalityError({ functionality: `tool choice type: ${type}` });
283
+ }
381
284
  }
382
-
383
- // src/mistral-chat-language-model.ts
384
- var reasoningEffortModelIds = /* @__PURE__ */ new Set([
385
- "glm-5-2",
386
- "labs-leanstral-1-5",
387
- "labs-leanstral-1-5-1",
388
- "magistral-medium-latest",
389
- "magistral-small-latest",
390
- "mistral-medium",
391
- "mistral-medium-2604",
392
- "mistral-medium-3",
393
- "mistral-medium-3-5",
394
- "mistral-medium-3.5",
395
- "mistral-medium-latest",
396
- "mistral-small-2603",
397
- "mistral-small-latest",
398
- "mistral-vibe-cli-fast",
399
- "mistral-vibe-cli-latest",
400
- "mistral-vibe-cli-with-tools",
401
- "zai-glm-5-2"
285
+ //#endregion
286
+ //#region src/mistral-chat-language-model.ts
287
+ const reasoningEffortModelIds = /* @__PURE__ */ new Set([
288
+ "glm-5-2",
289
+ "labs-leanstral-1-5",
290
+ "labs-leanstral-1-5-1",
291
+ "magistral-medium-latest",
292
+ "magistral-small-latest",
293
+ "mistral-medium",
294
+ "mistral-medium-2604",
295
+ "mistral-medium-3",
296
+ "mistral-medium-3-5",
297
+ "mistral-medium-3.5",
298
+ "mistral-medium-latest",
299
+ "mistral-small-2603",
300
+ "mistral-small-latest",
301
+ "mistral-vibe-cli-fast",
302
+ "mistral-vibe-cli-latest",
303
+ "mistral-vibe-cli-with-tools",
304
+ "zai-glm-5-2"
402
305
  ]);
403
- var MistralChatLanguageModel = class _MistralChatLanguageModel {
404
- constructor(modelId, config) {
405
- this.specificationVersion = "v4";
406
- this.supportedUrls = {
407
- "application/pdf": [/^https:\/\/.*$/]
408
- };
409
- this.modelId = modelId;
410
- this.config = config;
411
- this.generateId = config.generateId ?? generateId;
412
- }
413
- static [WORKFLOW_SERIALIZE](model) {
414
- return serializeModelOptions({
415
- modelId: model.modelId,
416
- config: model.config
417
- });
418
- }
419
- static [WORKFLOW_DESERIALIZE](options) {
420
- return new _MistralChatLanguageModel(options.modelId, options.config);
421
- }
422
- get provider() {
423
- return this.config.provider;
424
- }
425
- async getArgs({
426
- prompt,
427
- maxOutputTokens,
428
- temperature,
429
- topP,
430
- topK,
431
- frequencyPenalty,
432
- presencePenalty,
433
- reasoning,
434
- stopSequences,
435
- responseFormat,
436
- seed,
437
- providerOptions,
438
- tools,
439
- toolChoice
440
- }) {
441
- const warnings = [];
442
- const options = await parseProviderOptions({
443
- provider: "mistral",
444
- providerOptions,
445
- schema: mistralLanguageModelChatOptions
446
- }) ?? {};
447
- if (topK != null) {
448
- warnings.push({ type: "unsupported", feature: "topK" });
449
- }
450
- const supportsReasoningEffort = reasoningEffortModelIds.has(this.modelId);
451
- let resolvedReasoningEffort;
452
- if (supportsReasoningEffort) {
453
- resolvedReasoningEffort = options.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
454
- reasoning,
455
- effortMap: {
456
- minimal: "high",
457
- low: "high",
458
- medium: "high",
459
- high: "high",
460
- xhigh: "high"
461
- },
462
- warnings
463
- }) : void 0);
464
- } else if (isCustomReasoning(reasoning)) {
465
- warnings.push({
466
- type: "unsupported",
467
- feature: "reasoning",
468
- details: "This model does not support reasoning configuration."
469
- });
470
- }
471
- const structuredOutputs = options.structuredOutputs ?? true;
472
- const strictJsonSchema = options.strictJsonSchema ?? false;
473
- if (responseFormat?.type === "json" && !responseFormat?.schema) {
474
- prompt = injectJsonInstructionIntoMessages({
475
- messages: prompt,
476
- schema: responseFormat.schema
477
- });
478
- }
479
- const baseArgs = {
480
- // model id:
481
- model: this.modelId,
482
- // model specific settings:
483
- safe_prompt: options.safePrompt,
484
- // standardized settings:
485
- max_tokens: maxOutputTokens,
486
- temperature,
487
- top_p: topP,
488
- ...frequencyPenalty != null ? { frequency_penalty: frequencyPenalty } : {},
489
- ...presencePenalty != null ? { presence_penalty: presencePenalty } : {},
490
- stop: stopSequences,
491
- random_seed: seed,
492
- reasoning_effort: resolvedReasoningEffort,
493
- // response format:
494
- response_format: responseFormat?.type === "json" ? structuredOutputs && responseFormat?.schema != null ? {
495
- type: "json_schema",
496
- json_schema: {
497
- schema: responseFormat.schema,
498
- strict: strictJsonSchema,
499
- name: responseFormat.name ?? "response",
500
- description: responseFormat.description
501
- }
502
- } : { type: "json_object" } : void 0,
503
- // mistral-specific provider options:
504
- document_image_limit: options.documentImageLimit,
505
- document_page_limit: options.documentPageLimit,
506
- ...options.promptCacheKey !== void 0 ? { prompt_cache_key: options.promptCacheKey } : {},
507
- // messages:
508
- messages: convertToMistralChatMessages(prompt)
509
- };
510
- const {
511
- tools: mistralTools,
512
- toolChoice: mistralToolChoice,
513
- toolWarnings
514
- } = prepareTools({
515
- tools,
516
- toolChoice
517
- });
518
- return {
519
- args: {
520
- ...baseArgs,
521
- tools: mistralTools,
522
- tool_choice: mistralToolChoice,
523
- ...mistralTools != null && options.parallelToolCalls !== void 0 ? { parallel_tool_calls: options.parallelToolCalls } : {}
524
- },
525
- warnings: [...warnings, ...toolWarnings]
526
- };
527
- }
528
- async doGenerate(options) {
529
- const { args: body, warnings } = await this.getArgs(options);
530
- const {
531
- responseHeaders,
532
- value: response,
533
- rawValue: rawResponse
534
- } = await postJsonToApi({
535
- url: `${this.config.baseURL}/chat/completions`,
536
- headers: combineHeaders(this.config.headers?.(), options.headers),
537
- body,
538
- failedResponseHandler: mistralFailedResponseHandler,
539
- successfulResponseHandler: createJsonResponseHandler(
540
- mistralChatResponseSchema
541
- ),
542
- abortSignal: options.abortSignal,
543
- fetch: this.config.fetch
544
- });
545
- const choice = response.choices[0];
546
- const content = [];
547
- if (choice.message.content != null && Array.isArray(choice.message.content)) {
548
- for (const part of choice.message.content) {
549
- if (part.type === "thinking") {
550
- const reasoningText = extractReasoningContent(part.thinking);
551
- if (reasoningText.length > 0) {
552
- content.push({ type: "reasoning", text: reasoningText });
553
- }
554
- } else if (part.type === "text") {
555
- if (part.text.length > 0) {
556
- content.push({ type: "text", text: part.text });
557
- }
558
- }
559
- }
560
- } else {
561
- const text = extractTextContent(choice.message.content);
562
- if (text != null && text.length > 0) {
563
- content.push({ type: "text", text });
564
- }
565
- }
566
- if (choice.message.tool_calls != null) {
567
- for (const toolCall of choice.message.tool_calls) {
568
- content.push({
569
- type: "tool-call",
570
- toolCallId: toolCall.id,
571
- toolName: toolCall.function.name,
572
- input: toolCall.function.arguments
573
- });
574
- }
575
- }
576
- return {
577
- content,
578
- finishReason: {
579
- unified: mapMistralFinishReason(choice.finish_reason),
580
- raw: choice.finish_reason ?? void 0
581
- },
582
- usage: convertMistralUsage(response.usage),
583
- request: { body },
584
- response: {
585
- ...createLanguageModelResponseMetadata(response),
586
- headers: responseHeaders,
587
- body: rawResponse
588
- },
589
- warnings
590
- };
591
- }
592
- async doStream(options) {
593
- const { args, warnings } = await this.getArgs(options);
594
- const body = { ...args, stream: true };
595
- const { responseHeaders, value: response } = await postJsonToApi({
596
- url: `${this.config.baseURL}/chat/completions`,
597
- headers: combineHeaders(this.config.headers?.(), options.headers),
598
- body,
599
- failedResponseHandler: mistralFailedResponseHandler,
600
- successfulResponseHandler: createEventSourceResponseHandler(
601
- mistralChatChunkSchema
602
- ),
603
- abortSignal: options.abortSignal,
604
- fetch: this.config.fetch
605
- });
606
- let finishReason = {
607
- unified: "other",
608
- raw: void 0
609
- };
610
- let usage = void 0;
611
- let isFirstChunk = true;
612
- let activeText = false;
613
- let activeReasoningId = null;
614
- let toolCallTracker;
615
- const generateId2 = this.generateId;
616
- return {
617
- stream: response.pipeThrough(
618
- new TransformStream({
619
- start(controller) {
620
- toolCallTracker = new StreamingToolCallTracker(controller, {
621
- generateId: generateId2
622
- });
623
- controller.enqueue({ type: "stream-start", warnings });
624
- },
625
- transform(chunk, controller) {
626
- if (options.includeRawChunks) {
627
- controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
628
- }
629
- if (!chunk.success) {
630
- controller.enqueue({ type: "error", error: chunk.error });
631
- return;
632
- }
633
- const value = chunk.value;
634
- if (isFirstChunk) {
635
- isFirstChunk = false;
636
- controller.enqueue({
637
- type: "response-metadata",
638
- ...createLanguageModelResponseMetadata(value)
639
- });
640
- }
641
- if (value.usage != null) {
642
- usage = value.usage;
643
- }
644
- const choice = value.choices[0];
645
- const delta = choice.delta;
646
- const textContent = extractTextContent(delta.content);
647
- if (delta.content != null && Array.isArray(delta.content)) {
648
- for (const part of delta.content) {
649
- if (part.type === "thinking") {
650
- const reasoningDelta = extractReasoningContent(part.thinking);
651
- if (reasoningDelta.length > 0) {
652
- if (activeReasoningId == null) {
653
- if (activeText) {
654
- controller.enqueue({ type: "text-end", id: "0" });
655
- activeText = false;
656
- }
657
- activeReasoningId = generateId2();
658
- controller.enqueue({
659
- type: "reasoning-start",
660
- id: activeReasoningId
661
- });
662
- }
663
- controller.enqueue({
664
- type: "reasoning-delta",
665
- id: activeReasoningId,
666
- delta: reasoningDelta
667
- });
668
- }
669
- }
670
- }
671
- }
672
- if (textContent != null && textContent.length > 0) {
673
- if (!activeText) {
674
- if (activeReasoningId != null) {
675
- controller.enqueue({
676
- type: "reasoning-end",
677
- id: activeReasoningId
678
- });
679
- activeReasoningId = null;
680
- }
681
- controller.enqueue({ type: "text-start", id: "0" });
682
- activeText = true;
683
- }
684
- controller.enqueue({
685
- type: "text-delta",
686
- id: "0",
687
- delta: textContent
688
- });
689
- }
690
- if (delta?.tool_calls != null) {
691
- for (const toolCall of delta.tool_calls) {
692
- toolCallTracker.processDelta(toolCall);
693
- }
694
- }
695
- if (choice.finish_reason != null) {
696
- finishReason = {
697
- unified: mapMistralFinishReason(choice.finish_reason),
698
- raw: choice.finish_reason
699
- };
700
- }
701
- },
702
- flush(controller) {
703
- if (activeReasoningId != null) {
704
- controller.enqueue({
705
- type: "reasoning-end",
706
- id: activeReasoningId
707
- });
708
- }
709
- if (activeText) {
710
- controller.enqueue({ type: "text-end", id: "0" });
711
- }
712
- toolCallTracker.flush();
713
- controller.enqueue({
714
- type: "finish",
715
- finishReason,
716
- usage: convertMistralUsage(usage)
717
- });
718
- }
719
- })
720
- ),
721
- request: { body },
722
- response: { headers: responseHeaders }
723
- };
724
- }
306
+ var MistralChatLanguageModel = class MistralChatLanguageModel {
307
+ static [WORKFLOW_SERIALIZE](model) {
308
+ return serializeModelOptions({
309
+ modelId: model.modelId,
310
+ config: model.config
311
+ });
312
+ }
313
+ static [WORKFLOW_DESERIALIZE](options) {
314
+ return new MistralChatLanguageModel(options.modelId, options.config);
315
+ }
316
+ constructor(modelId, config) {
317
+ this.specificationVersion = "v4";
318
+ this.supportedUrls = { "application/pdf": [/^https:\/\/.*$/] };
319
+ this.modelId = modelId;
320
+ this.config = config;
321
+ this.generateId = config.generateId ?? generateId;
322
+ }
323
+ get provider() {
324
+ return this.config.provider;
325
+ }
326
+ async getArgs({ prompt, maxOutputTokens, temperature, topP, topK, frequencyPenalty, presencePenalty, reasoning, stopSequences, responseFormat, seed, providerOptions, tools, toolChoice }) {
327
+ const warnings = [];
328
+ const options = await parseProviderOptions({
329
+ provider: "mistral",
330
+ providerOptions,
331
+ schema: mistralLanguageModelChatOptions
332
+ }) ?? {};
333
+ if (topK != null) warnings.push({
334
+ type: "unsupported",
335
+ feature: "topK"
336
+ });
337
+ const supportsReasoningEffort = reasoningEffortModelIds.has(this.modelId);
338
+ let resolvedReasoningEffort;
339
+ if (supportsReasoningEffort) resolvedReasoningEffort = options.reasoningEffort ?? (isCustomReasoning(reasoning) ? reasoning === "none" ? "none" : mapReasoningToProviderEffort({
340
+ reasoning,
341
+ effortMap: {
342
+ minimal: "high",
343
+ low: "high",
344
+ medium: "high",
345
+ high: "high",
346
+ xhigh: "high"
347
+ },
348
+ warnings
349
+ }) : void 0);
350
+ else if (isCustomReasoning(reasoning)) warnings.push({
351
+ type: "unsupported",
352
+ feature: "reasoning",
353
+ details: "This model does not support reasoning configuration."
354
+ });
355
+ const structuredOutputs = options.structuredOutputs ?? true;
356
+ const strictJsonSchema = options.strictJsonSchema ?? false;
357
+ if (responseFormat?.type === "json" && (!structuredOutputs || responseFormat.schema == null)) prompt = injectJsonInstructionIntoMessages({
358
+ messages: prompt,
359
+ schema: responseFormat.schema
360
+ });
361
+ const baseArgs = {
362
+ model: this.modelId,
363
+ safe_prompt: options.safePrompt,
364
+ max_tokens: maxOutputTokens,
365
+ temperature,
366
+ top_p: topP,
367
+ ...frequencyPenalty != null ? { frequency_penalty: frequencyPenalty } : {},
368
+ ...presencePenalty != null ? { presence_penalty: presencePenalty } : {},
369
+ stop: stopSequences,
370
+ random_seed: seed,
371
+ reasoning_effort: resolvedReasoningEffort,
372
+ response_format: responseFormat?.type === "json" ? structuredOutputs && responseFormat?.schema != null ? {
373
+ type: "json_schema",
374
+ json_schema: {
375
+ schema: responseFormat.schema,
376
+ strict: strictJsonSchema,
377
+ name: responseFormat.name ?? "response",
378
+ description: responseFormat.description
379
+ }
380
+ } : { type: "json_object" } : void 0,
381
+ document_image_limit: options.documentImageLimit,
382
+ document_page_limit: options.documentPageLimit,
383
+ ...options.promptCacheKey !== void 0 ? { prompt_cache_key: options.promptCacheKey } : {},
384
+ messages: convertToMistralChatMessages(prompt)
385
+ };
386
+ const { tools: mistralTools, toolChoice: mistralToolChoice, toolWarnings } = prepareTools({
387
+ tools,
388
+ toolChoice
389
+ });
390
+ return {
391
+ args: {
392
+ ...baseArgs,
393
+ tools: mistralTools,
394
+ tool_choice: mistralToolChoice,
395
+ ...mistralTools != null && options.parallelToolCalls !== void 0 ? { parallel_tool_calls: options.parallelToolCalls } : {}
396
+ },
397
+ warnings: [...warnings, ...toolWarnings]
398
+ };
399
+ }
400
+ async doGenerate(options) {
401
+ const { args: body, warnings } = await this.getArgs(options);
402
+ const { responseHeaders, value: response, rawValue: rawResponse } = await postJsonToApi({
403
+ url: `${this.config.baseURL}/chat/completions`,
404
+ headers: combineHeaders(this.config.headers?.(), options.headers),
405
+ body,
406
+ failedResponseHandler: mistralFailedResponseHandler,
407
+ successfulResponseHandler: createJsonResponseHandler(mistralChatResponseSchema),
408
+ abortSignal: options.abortSignal,
409
+ fetch: this.config.fetch
410
+ });
411
+ const choice = response.choices[0];
412
+ const content = [];
413
+ if (choice.message.content != null && Array.isArray(choice.message.content)) {
414
+ for (const part of choice.message.content) if (part.type === "thinking") {
415
+ const reasoningText = extractReasoningContent(part.thinking);
416
+ if (reasoningText.length > 0) content.push({
417
+ type: "reasoning",
418
+ text: reasoningText
419
+ });
420
+ } else if (part.type === "text") {
421
+ if (part.text.length > 0) content.push({
422
+ type: "text",
423
+ text: part.text
424
+ });
425
+ }
426
+ } else {
427
+ const text = extractTextContent(choice.message.content);
428
+ if (text != null && text.length > 0) content.push({
429
+ type: "text",
430
+ text
431
+ });
432
+ }
433
+ if (choice.message.tool_calls != null) for (const toolCall of choice.message.tool_calls) content.push({
434
+ type: "tool-call",
435
+ toolCallId: toolCall.id,
436
+ toolName: toolCall.function.name,
437
+ input: toolCall.function.arguments
438
+ });
439
+ return {
440
+ content,
441
+ finishReason: {
442
+ unified: mapMistralFinishReason(choice.finish_reason),
443
+ raw: choice.finish_reason ?? void 0
444
+ },
445
+ usage: convertMistralUsage(response.usage),
446
+ request: { body },
447
+ response: {
448
+ ...getResponseMetadata(response),
449
+ headers: responseHeaders,
450
+ body: rawResponse
451
+ },
452
+ warnings
453
+ };
454
+ }
455
+ async doStream(options) {
456
+ const { args, warnings } = await this.getArgs(options);
457
+ const body = {
458
+ ...args,
459
+ stream: true
460
+ };
461
+ const { responseHeaders, value: response } = await postJsonToApi({
462
+ url: `${this.config.baseURL}/chat/completions`,
463
+ headers: combineHeaders(this.config.headers?.(), options.headers),
464
+ body,
465
+ failedResponseHandler: mistralFailedResponseHandler,
466
+ successfulResponseHandler: createEventSourceResponseHandler(mistralChatChunkSchema),
467
+ abortSignal: options.abortSignal,
468
+ fetch: this.config.fetch
469
+ });
470
+ let finishReason = {
471
+ unified: "other",
472
+ raw: void 0
473
+ };
474
+ let usage = void 0;
475
+ let isFirstChunk = true;
476
+ let activeText = false;
477
+ let activeReasoningId = null;
478
+ let toolCallTracker;
479
+ const generateId = this.generateId;
480
+ return {
481
+ stream: response.pipeThrough(new TransformStream({
482
+ start(controller) {
483
+ toolCallTracker = new StreamingToolCallTracker(controller, { generateId });
484
+ controller.enqueue({
485
+ type: "stream-start",
486
+ warnings
487
+ });
488
+ },
489
+ transform(chunk, controller) {
490
+ if (options.includeRawChunks) controller.enqueue({
491
+ type: "raw",
492
+ rawValue: chunk.rawValue
493
+ });
494
+ if (!chunk.success) {
495
+ controller.enqueue({
496
+ type: "error",
497
+ error: chunk.error
498
+ });
499
+ return;
500
+ }
501
+ const value = chunk.value;
502
+ if (isFirstChunk) {
503
+ isFirstChunk = false;
504
+ controller.enqueue({
505
+ type: "response-metadata",
506
+ ...getResponseMetadata(value)
507
+ });
508
+ }
509
+ if (value.usage != null) usage = value.usage;
510
+ const choice = value.choices[0];
511
+ const delta = choice.delta;
512
+ const textContent = extractTextContent(delta.content);
513
+ if (delta.content != null && Array.isArray(delta.content)) {
514
+ for (const part of delta.content) if (part.type === "thinking") {
515
+ const reasoningDelta = extractReasoningContent(part.thinking);
516
+ if (reasoningDelta.length > 0) {
517
+ if (activeReasoningId == null) {
518
+ if (activeText) {
519
+ controller.enqueue({
520
+ type: "text-end",
521
+ id: "0"
522
+ });
523
+ activeText = false;
524
+ }
525
+ activeReasoningId = generateId();
526
+ controller.enqueue({
527
+ type: "reasoning-start",
528
+ id: activeReasoningId
529
+ });
530
+ }
531
+ controller.enqueue({
532
+ type: "reasoning-delta",
533
+ id: activeReasoningId,
534
+ delta: reasoningDelta
535
+ });
536
+ }
537
+ }
538
+ }
539
+ if (textContent != null && textContent.length > 0) {
540
+ if (!activeText) {
541
+ if (activeReasoningId != null) {
542
+ controller.enqueue({
543
+ type: "reasoning-end",
544
+ id: activeReasoningId
545
+ });
546
+ activeReasoningId = null;
547
+ }
548
+ controller.enqueue({
549
+ type: "text-start",
550
+ id: "0"
551
+ });
552
+ activeText = true;
553
+ }
554
+ controller.enqueue({
555
+ type: "text-delta",
556
+ id: "0",
557
+ delta: textContent
558
+ });
559
+ }
560
+ if (delta?.tool_calls != null) for (const toolCall of delta.tool_calls) toolCallTracker.processDelta(toolCall);
561
+ if (choice.finish_reason != null) finishReason = {
562
+ unified: mapMistralFinishReason(choice.finish_reason),
563
+ raw: choice.finish_reason
564
+ };
565
+ },
566
+ flush(controller) {
567
+ if (activeReasoningId != null) controller.enqueue({
568
+ type: "reasoning-end",
569
+ id: activeReasoningId
570
+ });
571
+ if (activeText) controller.enqueue({
572
+ type: "text-end",
573
+ id: "0"
574
+ });
575
+ toolCallTracker.flush();
576
+ controller.enqueue({
577
+ type: "finish",
578
+ finishReason,
579
+ usage: convertMistralUsage(usage)
580
+ });
581
+ }
582
+ })),
583
+ request: { body },
584
+ response: { headers: responseHeaders }
585
+ };
586
+ }
725
587
  };
726
588
  function extractReasoningContent(thinking) {
727
- return thinking.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text).join("");
589
+ return thinking.filter((chunk) => chunk.type === "text").map((chunk) => chunk.text).join("");
728
590
  }
729
591
  function extractTextContent(content) {
730
- if (typeof content === "string") {
731
- return content;
732
- }
733
- if (content == null) {
734
- return void 0;
735
- }
736
- const textContent = [];
737
- for (const chunk of content) {
738
- const { type } = chunk;
739
- switch (type) {
740
- case "text":
741
- textContent.push(chunk.text);
742
- break;
743
- case "thinking":
744
- case "image_url":
745
- case "reference":
746
- break;
747
- default: {
748
- const _exhaustiveCheck = type;
749
- throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
750
- }
751
- }
752
- }
753
- return textContent.length ? textContent.join("") : void 0;
592
+ if (typeof content === "string") return content;
593
+ if (content == null) return;
594
+ const textContent = [];
595
+ for (const chunk of content) {
596
+ const { type } = chunk;
597
+ switch (type) {
598
+ case "text":
599
+ textContent.push(chunk.text);
600
+ break;
601
+ case "thinking":
602
+ case "image_url":
603
+ case "reference": break;
604
+ default: throw new Error(`Unsupported type: ${type}`);
605
+ }
606
+ }
607
+ return textContent.length ? textContent.join("") : void 0;
754
608
  }
755
- var mistralContentSchema = z4.union([
756
- z4.string(),
757
- z4.array(
758
- z4.discriminatedUnion("type", [
759
- z4.object({
760
- type: z4.literal("text"),
761
- text: z4.string()
762
- }),
763
- z4.object({
764
- type: z4.literal("image_url"),
765
- image_url: z4.union([
766
- z4.string(),
767
- z4.object({
768
- url: z4.string(),
769
- detail: z4.string().nullable()
770
- })
771
- ])
772
- }),
773
- z4.object({
774
- type: z4.literal("reference"),
775
- reference_ids: z4.array(z4.union([z4.string(), z4.number()]))
776
- }),
777
- z4.object({
778
- type: z4.literal("thinking"),
779
- thinking: z4.array(
780
- z4.object({
781
- type: z4.literal("text"),
782
- text: z4.string()
783
- })
784
- )
785
- })
786
- ])
787
- )
788
- ]).nullish();
789
- var mistralChatResponseSchema = z4.object({
790
- id: z4.string().nullish(),
791
- created: z4.number().nullish(),
792
- model: z4.string().nullish(),
793
- choices: z4.array(
794
- z4.object({
795
- message: z4.object({
796
- role: z4.literal("assistant"),
797
- content: mistralContentSchema,
798
- tool_calls: z4.array(
799
- z4.object({
800
- id: z4.string(),
801
- function: z4.object({ name: z4.string(), arguments: z4.string() })
802
- })
803
- ).nullish()
804
- }),
805
- index: z4.number(),
806
- finish_reason: z4.string().nullish()
807
- })
808
- ),
809
- object: z4.literal("chat.completion"),
810
- usage: mistralUsageSchema
609
+ const mistralContentSchema = z.union([z.string(), z.array(z.discriminatedUnion("type", [
610
+ z.object({
611
+ type: z.literal("text"),
612
+ text: z.string()
613
+ }),
614
+ z.object({
615
+ type: z.literal("image_url"),
616
+ image_url: z.union([z.string(), z.object({
617
+ url: z.string(),
618
+ detail: z.string().nullable()
619
+ })])
620
+ }),
621
+ z.object({
622
+ type: z.literal("reference"),
623
+ reference_ids: z.array(z.union([z.string(), z.number()]))
624
+ }),
625
+ z.object({
626
+ type: z.literal("thinking"),
627
+ thinking: z.array(z.object({
628
+ type: z.literal("text"),
629
+ text: z.string()
630
+ }))
631
+ })
632
+ ]))]).nullish();
633
+ const mistralChatResponseSchema = z.object({
634
+ id: z.string().nullish(),
635
+ created: z.number().nullish(),
636
+ model: z.string().nullish(),
637
+ choices: z.array(z.object({
638
+ message: z.object({
639
+ role: z.literal("assistant"),
640
+ content: mistralContentSchema,
641
+ tool_calls: z.array(z.object({
642
+ id: z.string(),
643
+ function: z.object({
644
+ name: z.string(),
645
+ arguments: z.string()
646
+ })
647
+ })).nullish()
648
+ }),
649
+ index: z.number(),
650
+ finish_reason: z.string().nullish()
651
+ })),
652
+ object: z.literal("chat.completion"),
653
+ usage: mistralUsageSchema
811
654
  });
812
- var mistralChatChunkSchema = z4.object({
813
- id: z4.string().nullish(),
814
- created: z4.number().nullish(),
815
- model: z4.string().nullish(),
816
- choices: z4.array(
817
- z4.object({
818
- delta: z4.object({
819
- role: z4.enum(["assistant"]).optional(),
820
- content: mistralContentSchema,
821
- tool_calls: z4.array(
822
- z4.object({
823
- index: z4.number().nullish(),
824
- id: z4.string().nullish(),
825
- function: z4.object({
826
- name: z4.string().nullish(),
827
- arguments: z4.string().nullish()
828
- })
829
- })
830
- ).nullish()
831
- }),
832
- finish_reason: z4.string().nullish(),
833
- index: z4.number()
834
- })
835
- ),
836
- usage: mistralUsageSchema.nullish()
655
+ const mistralChatChunkSchema = z.object({
656
+ id: z.string().nullish(),
657
+ created: z.number().nullish(),
658
+ model: z.string().nullish(),
659
+ choices: z.array(z.object({
660
+ delta: z.object({
661
+ role: z.enum(["assistant"]).optional(),
662
+ content: mistralContentSchema,
663
+ tool_calls: z.array(z.object({
664
+ index: z.number().nullish(),
665
+ id: z.string().nullish(),
666
+ function: z.object({
667
+ name: z.string().nullish(),
668
+ arguments: z.string().nullish()
669
+ })
670
+ })).nullish()
671
+ }),
672
+ finish_reason: z.string().nullish(),
673
+ index: z.number()
674
+ })),
675
+ usage: mistralUsageSchema.nullish()
837
676
  });
838
-
839
- // src/mistral-embedding-model.ts
840
- import {
841
- TooManyEmbeddingValuesForCallError
842
- } from "@ai-sdk/provider";
843
- import {
844
- combineHeaders as combineHeaders2,
845
- createJsonResponseHandler as createJsonResponseHandler2,
846
- parseProviderOptions as parseProviderOptions2,
847
- postJsonToApi as postJsonToApi2,
848
- serializeModelOptions as serializeModelOptions2,
849
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE2,
850
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE2
851
- } from "@ai-sdk/provider-utils";
852
- import { z as z6 } from "zod/v4";
853
-
854
- // src/mistral-embedding-model-options.ts
855
- import { z as z5 } from "zod/v4";
856
- var mistralEmbeddingModelOptions = z5.object({
857
- /**
858
- * Additional metadata to attach to the embedding request.
859
- */
860
- metadata: z5.record(z5.string(), z5.any()).optional(),
861
- /**
862
- * The dimension of the output embeddings when supported by the model.
863
- */
864
- outputDimension: z5.number().int().positive().optional(),
865
- /**
866
- * The data type of the output embeddings when supported by the model.
867
- */
868
- outputDtype: z5.enum(["float", "int8", "uint8", "binary", "ubinary"]).optional()
677
+ //#endregion
678
+ //#region src/mistral-embedding-model-options.ts
679
+ const mistralEmbeddingModelOptions = z.object({
680
+ /**
681
+ * Additional metadata to attach to the embedding request.
682
+ */
683
+ metadata: z.record(z.string(), z.any()).optional(),
684
+ /**
685
+ * The dimension of the output embeddings when supported by the model.
686
+ */
687
+ outputDimension: z.number().int().positive().optional(),
688
+ /**
689
+ * The data type of the output embeddings when supported by the model.
690
+ */
691
+ outputDtype: z.enum([
692
+ "float",
693
+ "int8",
694
+ "uint8",
695
+ "binary",
696
+ "ubinary"
697
+ ]).optional()
869
698
  });
870
-
871
- // src/mistral-embedding-model.ts
872
- var MistralEmbeddingModel = class _MistralEmbeddingModel {
873
- constructor(modelId, config) {
874
- this.specificationVersion = "v4";
875
- this.maxEmbeddingsPerCall = 32;
876
- this.supportsParallelCalls = false;
877
- this.modelId = modelId;
878
- this.config = config;
879
- }
880
- get provider() {
881
- return this.config.provider;
882
- }
883
- static [WORKFLOW_SERIALIZE2](model) {
884
- return serializeModelOptions2({
885
- modelId: model.modelId,
886
- config: model.config
887
- });
888
- }
889
- static [WORKFLOW_DESERIALIZE2](options) {
890
- return new _MistralEmbeddingModel(options.modelId, options.config);
891
- }
892
- async doEmbed({
893
- values,
894
- abortSignal,
895
- headers,
896
- providerOptions
897
- }) {
898
- if (values.length > this.maxEmbeddingsPerCall) {
899
- throw new TooManyEmbeddingValuesForCallError({
900
- provider: this.provider,
901
- modelId: this.modelId,
902
- maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
903
- values
904
- });
905
- }
906
- const mistralOptions = await parseProviderOptions2({
907
- provider: "mistral",
908
- providerOptions,
909
- schema: mistralEmbeddingModelOptions
910
- }) ?? {};
911
- const {
912
- responseHeaders,
913
- value: response,
914
- rawValue
915
- } = await postJsonToApi2({
916
- url: `${this.config.baseURL}/embeddings`,
917
- headers: combineHeaders2(this.config.headers?.(), headers),
918
- body: {
919
- model: this.modelId,
920
- input: values,
921
- metadata: mistralOptions.metadata,
922
- output_dimension: mistralOptions.outputDimension,
923
- output_dtype: mistralOptions.outputDtype,
924
- encoding_format: "float"
925
- },
926
- failedResponseHandler: mistralFailedResponseHandler,
927
- successfulResponseHandler: createJsonResponseHandler2(
928
- MistralTextEmbeddingResponseSchema
929
- ),
930
- abortSignal,
931
- fetch: this.config.fetch
932
- });
933
- return {
934
- warnings: [],
935
- embeddings: response.data.map((item) => item.embedding),
936
- usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
937
- response: { headers: responseHeaders, body: rawValue }
938
- };
939
- }
699
+ //#endregion
700
+ //#region src/mistral-embedding-model.ts
701
+ var MistralEmbeddingModel = class MistralEmbeddingModel {
702
+ get provider() {
703
+ return this.config.provider;
704
+ }
705
+ static [WORKFLOW_SERIALIZE](model) {
706
+ return serializeModelOptions({
707
+ modelId: model.modelId,
708
+ config: model.config
709
+ });
710
+ }
711
+ static [WORKFLOW_DESERIALIZE](options) {
712
+ return new MistralEmbeddingModel(options.modelId, options.config);
713
+ }
714
+ constructor(modelId, config) {
715
+ this.specificationVersion = "v4";
716
+ this.maxEmbeddingsPerCall = 32;
717
+ this.supportsParallelCalls = false;
718
+ this.modelId = modelId;
719
+ this.config = config;
720
+ }
721
+ async doEmbed({ values, abortSignal, headers, providerOptions }) {
722
+ if (values.length > this.maxEmbeddingsPerCall) throw new TooManyEmbeddingValuesForCallError({
723
+ provider: this.provider,
724
+ modelId: this.modelId,
725
+ maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
726
+ values
727
+ });
728
+ const mistralOptions = await parseProviderOptions({
729
+ provider: "mistral",
730
+ providerOptions,
731
+ schema: mistralEmbeddingModelOptions
732
+ }) ?? {};
733
+ const { responseHeaders, value: response, rawValue } = await postJsonToApi({
734
+ url: `${this.config.baseURL}/embeddings`,
735
+ headers: combineHeaders(this.config.headers?.(), headers),
736
+ body: {
737
+ model: this.modelId,
738
+ input: values,
739
+ metadata: mistralOptions.metadata,
740
+ output_dimension: mistralOptions.outputDimension,
741
+ output_dtype: mistralOptions.outputDtype,
742
+ encoding_format: "float"
743
+ },
744
+ failedResponseHandler: mistralFailedResponseHandler,
745
+ successfulResponseHandler: createJsonResponseHandler(MistralTextEmbeddingResponseSchema),
746
+ abortSignal,
747
+ fetch: this.config.fetch
748
+ });
749
+ return {
750
+ warnings: [],
751
+ embeddings: response.data.map((item) => item.embedding),
752
+ usage: response.usage ? { tokens: response.usage.prompt_tokens } : void 0,
753
+ response: {
754
+ headers: responseHeaders,
755
+ body: rawValue
756
+ }
757
+ };
758
+ }
940
759
  };
941
- var MistralTextEmbeddingResponseSchema = z6.object({
942
- data: z6.array(z6.object({ embedding: z6.array(z6.number()) })),
943
- usage: z6.object({ prompt_tokens: z6.number() }).nullish()
760
+ const MistralTextEmbeddingResponseSchema = z.object({
761
+ data: z.array(z.object({ embedding: z.array(z.number()) })),
762
+ usage: z.object({ prompt_tokens: z.number() }).nullish()
944
763
  });
945
-
946
- // src/mistral-speech-model.ts
947
- import {
948
- combineHeaders as combineHeaders3,
949
- createJsonResponseHandler as createJsonResponseHandler3,
950
- parseProviderOptions as parseProviderOptions3,
951
- postToApi,
952
- serializeModelOptions as serializeModelOptions3,
953
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE3,
954
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE3
955
- } from "@ai-sdk/provider-utils";
956
- import { z as z8 } from "zod/v4";
957
-
958
- // src/mistral-speech-model-options.ts
959
- import { z as z7 } from "zod/v4";
960
- var mistralSpeechModelOptions = z7.object({
961
- /**
962
- * Base64-encoded reference audio for one-off voice cloning.
963
- *
964
- * When provided, this takes precedence over the standard `voice` option.
965
- */
966
- refAudio: z7.string().min(1).optional()
967
- });
968
-
969
- // src/mistral-speech-model.ts
970
- var MistralSpeechModel = class _MistralSpeechModel {
971
- constructor(modelId, config) {
972
- this.modelId = modelId;
973
- this.config = config;
974
- this.specificationVersion = "v4";
975
- }
976
- static [WORKFLOW_SERIALIZE3](model) {
977
- return serializeModelOptions3({
978
- modelId: model.modelId,
979
- config: model.config
980
- });
981
- }
982
- static [WORKFLOW_DESERIALIZE3](options) {
983
- return new _MistralSpeechModel(options.modelId, options.config);
984
- }
985
- get provider() {
986
- return this.config.provider;
987
- }
988
- async getArgs({
989
- text,
990
- voice,
991
- outputFormat = "mp3",
992
- instructions,
993
- speed,
994
- language,
995
- providerOptions
996
- }) {
997
- const warnings = [];
998
- const mistralOptions = await parseProviderOptions3({
999
- provider: "mistral",
1000
- providerOptions,
1001
- schema: mistralSpeechModelOptions
1002
- });
1003
- let responseFormat = "mp3";
1004
- if (["pcm", "wav", "mp3", "flac", "opus"].includes(outputFormat)) {
1005
- responseFormat = outputFormat;
1006
- } else {
1007
- warnings.push({
1008
- type: "unsupported",
1009
- feature: "outputFormat",
1010
- details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
1011
- });
1012
- }
1013
- if (instructions != null) {
1014
- warnings.push({
1015
- type: "unsupported",
1016
- feature: "instructions",
1017
- details: "Mistral speech models do not support the `instructions` option. Use a reference audio clip to guide delivery."
1018
- });
1019
- }
1020
- if (speed != null) {
1021
- warnings.push({
1022
- type: "unsupported",
1023
- feature: "speed",
1024
- details: "Mistral speech models do not support the `speed` option. It was ignored."
1025
- });
1026
- }
1027
- if (language != null) {
1028
- warnings.push({
1029
- type: "unsupported",
1030
- feature: "language",
1031
- details: "Mistral speech models do not support the `language` option. Language is inferred from the input text and voice."
1032
- });
1033
- }
1034
- const refAudio = mistralOptions?.refAudio;
1035
- const requestBody = {
1036
- model: this.modelId,
1037
- input: text,
1038
- voice_id: refAudio == null ? voice : void 0,
1039
- ref_audio: refAudio,
1040
- response_format: responseFormat,
1041
- stream: false
1042
- };
1043
- const requestBodyValues = {
1044
- ...requestBody,
1045
- ref_audio: refAudio == null ? void 0 : "[redacted]"
1046
- };
1047
- return { requestBody, requestBodyValues, warnings };
1048
- }
1049
- async doGenerate(options) {
1050
- const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
1051
- const { requestBody, requestBodyValues, warnings } = await this.getArgs(options);
1052
- const {
1053
- value: response,
1054
- responseHeaders,
1055
- rawValue: rawResponse
1056
- } = await postToApi({
1057
- url: `${this.config.baseURL}/audio/speech`,
1058
- headers: combineHeaders3(
1059
- { "Content-Type": "application/json" },
1060
- this.config.headers?.(),
1061
- options.headers
1062
- ),
1063
- body: {
1064
- content: JSON.stringify(requestBody),
1065
- values: requestBodyValues
1066
- },
1067
- failedResponseHandler: mistralFailedResponseHandler,
1068
- successfulResponseHandler: createJsonResponseHandler3(
1069
- mistralSpeechResponseSchema
1070
- ),
1071
- abortSignal: options.abortSignal,
1072
- fetch: this.config.fetch
1073
- });
1074
- return {
1075
- audio: response.audio_data,
1076
- warnings,
1077
- request: {
1078
- body: JSON.stringify(requestBodyValues)
1079
- },
1080
- response: {
1081
- timestamp: currentDate,
1082
- modelId: this.modelId,
1083
- headers: responseHeaders,
1084
- body: rawResponse
1085
- }
1086
- };
1087
- }
764
+ //#endregion
765
+ //#region src/mistral-speech-model-options.ts
766
+ const mistralSpeechModelOptions = z.object({
767
+ /**
768
+ * Base64-encoded reference audio for one-off voice cloning.
769
+ *
770
+ * When provided, this takes precedence over the standard `voice` option.
771
+ */
772
+ refAudio: z.string().min(1).optional() });
773
+ //#endregion
774
+ //#region src/mistral-speech-model.ts
775
+ var MistralSpeechModel = class MistralSpeechModel {
776
+ static [WORKFLOW_SERIALIZE](model) {
777
+ return serializeModelOptions({
778
+ modelId: model.modelId,
779
+ config: model.config
780
+ });
781
+ }
782
+ static [WORKFLOW_DESERIALIZE](options) {
783
+ return new MistralSpeechModel(options.modelId, options.config);
784
+ }
785
+ get provider() {
786
+ return this.config.provider;
787
+ }
788
+ constructor(modelId, config) {
789
+ this.modelId = modelId;
790
+ this.config = config;
791
+ this.specificationVersion = "v4";
792
+ }
793
+ async getArgs({ text, voice, outputFormat = "mp3", instructions, speed, language, providerOptions }) {
794
+ const warnings = [];
795
+ const mistralOptions = await parseProviderOptions({
796
+ provider: "mistral",
797
+ providerOptions,
798
+ schema: mistralSpeechModelOptions
799
+ });
800
+ let responseFormat = "mp3";
801
+ if ([
802
+ "pcm",
803
+ "wav",
804
+ "mp3",
805
+ "flac",
806
+ "opus"
807
+ ].includes(outputFormat)) responseFormat = outputFormat;
808
+ else warnings.push({
809
+ type: "unsupported",
810
+ feature: "outputFormat",
811
+ details: `Unsupported output format: ${outputFormat}. Using mp3 instead.`
812
+ });
813
+ if (instructions != null) warnings.push({
814
+ type: "unsupported",
815
+ feature: "instructions",
816
+ details: "Mistral speech models do not support the `instructions` option. Use a reference audio clip to guide delivery."
817
+ });
818
+ if (speed != null) warnings.push({
819
+ type: "unsupported",
820
+ feature: "speed",
821
+ details: "Mistral speech models do not support the `speed` option. It was ignored."
822
+ });
823
+ if (language != null) warnings.push({
824
+ type: "unsupported",
825
+ feature: "language",
826
+ details: "Mistral speech models do not support the `language` option. Language is inferred from the input text and voice."
827
+ });
828
+ const refAudio = mistralOptions?.refAudio;
829
+ const requestBody = {
830
+ model: this.modelId,
831
+ input: text,
832
+ voice_id: refAudio == null ? voice : void 0,
833
+ ref_audio: refAudio,
834
+ response_format: responseFormat,
835
+ stream: false
836
+ };
837
+ return {
838
+ requestBody,
839
+ requestBodyValues: {
840
+ ...requestBody,
841
+ ref_audio: refAudio == null ? void 0 : "[redacted]"
842
+ },
843
+ warnings
844
+ };
845
+ }
846
+ async doGenerate(options) {
847
+ const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
848
+ const { requestBody, requestBodyValues, warnings } = await this.getArgs(options);
849
+ const { value: response, responseHeaders, rawValue: rawResponse } = await postToApi({
850
+ url: `${this.config.baseURL}/audio/speech`,
851
+ headers: combineHeaders({ "Content-Type": "application/json" }, this.config.headers?.(), options.headers),
852
+ body: {
853
+ content: JSON.stringify(requestBody),
854
+ values: requestBodyValues
855
+ },
856
+ failedResponseHandler: mistralFailedResponseHandler,
857
+ successfulResponseHandler: createJsonResponseHandler(mistralSpeechResponseSchema),
858
+ abortSignal: options.abortSignal,
859
+ fetch: this.config.fetch
860
+ });
861
+ return {
862
+ audio: response.audio_data,
863
+ warnings,
864
+ request: { body: JSON.stringify(requestBodyValues) },
865
+ response: {
866
+ timestamp: currentDate,
867
+ modelId: this.modelId,
868
+ headers: responseHeaders,
869
+ body: rawResponse
870
+ }
871
+ };
872
+ }
1088
873
  };
1089
- var mistralSpeechResponseSchema = z8.object({
1090
- audio_data: z8.string()
1091
- });
1092
-
1093
- // src/mistral-transcription-model.ts
1094
- import {
1095
- InvalidArgumentError
1096
- } from "@ai-sdk/provider";
1097
- import {
1098
- combineHeaders as combineHeaders4,
1099
- convertBase64ToUint8Array,
1100
- createJsonResponseHandler as createJsonResponseHandler4,
1101
- mediaTypeToExtension,
1102
- parseProviderOptions as parseProviderOptions4,
1103
- postFormDataToApi,
1104
- serializeModelOptions as serializeModelOptions4,
1105
- WORKFLOW_DESERIALIZE as WORKFLOW_DESERIALIZE4,
1106
- WORKFLOW_SERIALIZE as WORKFLOW_SERIALIZE4
1107
- } from "@ai-sdk/provider-utils";
1108
- import { z as z10 } from "zod/v4";
1109
-
1110
- // src/mistral-transcription-model-options.ts
1111
- import {
1112
- lazySchema,
1113
- zodSchema
1114
- } from "@ai-sdk/provider-utils";
1115
- import { z as z9 } from "zod/v4";
1116
- var mistralTranscriptionModelOptions = lazySchema(
1117
- () => zodSchema(
1118
- z9.object({
1119
- /**
1120
- * The language of the audio, e.g. "en". Providing the language can boost
1121
- * accuracy.
1122
- */
1123
- language: z9.string().min(1).optional(),
1124
- /**
1125
- * The sampling temperature.
1126
- */
1127
- temperature: z9.number().optional(),
1128
- /**
1129
- * The timestamp granularities to include in the transcription response.
1130
- */
1131
- timestampGranularities: z9.array(z9.enum(["segment", "word"])).min(1).optional(),
1132
- /**
1133
- * Whether to identify speakers in the transcription.
1134
- */
1135
- diarize: z9.boolean().optional(),
1136
- /**
1137
- * Words or phrases to guide the model toward correct spellings of names,
1138
- * technical terms, or domain-specific vocabulary.
1139
- *
1140
- * Mistral requires each item to omit commas and whitespace. Use
1141
- * underscores to represent multi-word phrases.
1142
- */
1143
- contextBias: z9.array(
1144
- z9.string().min(1).regex(
1145
- /^[^\s,]+$/,
1146
- "Context bias items must not contain commas or whitespace."
1147
- )
1148
- ).max(100).optional()
1149
- })
1150
- )
1151
- );
1152
-
1153
- // src/mistral-transcription-model.ts
1154
- var MistralTranscriptionModel = class _MistralTranscriptionModel {
1155
- constructor(modelId, config) {
1156
- this.modelId = modelId;
1157
- this.config = config;
1158
- this.specificationVersion = "v4";
1159
- }
1160
- static [WORKFLOW_SERIALIZE4](model) {
1161
- return serializeModelOptions4({
1162
- modelId: model.modelId,
1163
- config: model.config
1164
- });
1165
- }
1166
- static [WORKFLOW_DESERIALIZE4](options) {
1167
- return new _MistralTranscriptionModel(options.modelId, options.config);
1168
- }
1169
- get provider() {
1170
- return this.config.provider;
1171
- }
1172
- async getArgs({
1173
- audio,
1174
- mediaType,
1175
- providerOptions
1176
- }) {
1177
- const warnings = [];
1178
- const mistralOptions = await parseProviderOptions4({
1179
- provider: "mistral",
1180
- providerOptions,
1181
- schema: mistralTranscriptionModelOptions
1182
- });
1183
- if (mistralOptions?.language != null && mistralOptions.timestampGranularities != null) {
1184
- throw new InvalidArgumentError({
1185
- argument: "providerOptions",
1186
- message: "providerOptions.mistral.language cannot be combined with providerOptions.mistral.timestampGranularities"
1187
- });
1188
- }
1189
- const formData = new FormData();
1190
- const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
1191
- formData.append("model", this.modelId);
1192
- formData.append(
1193
- "file",
1194
- new File([blob], "audio", { type: mediaType }),
1195
- `audio.${mediaTypeToExtension(mediaType)}`
1196
- );
1197
- if (mistralOptions != null) {
1198
- appendFormValue(formData, "language", mistralOptions.language);
1199
- appendFormValue(formData, "temperature", mistralOptions.temperature);
1200
- appendFormValue(
1201
- formData,
1202
- "timestamp_granularities",
1203
- mistralOptions.timestampGranularities
1204
- );
1205
- appendFormValue(formData, "diarize", mistralOptions.diarize);
1206
- appendFormValue(formData, "context_bias", mistralOptions.contextBias);
1207
- }
1208
- return { formData, warnings };
1209
- }
1210
- async doGenerate(options) {
1211
- const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
1212
- const { formData, warnings } = await this.getArgs(options);
1213
- const {
1214
- value: response,
1215
- responseHeaders,
1216
- rawValue: rawResponse
1217
- } = await postFormDataToApi({
1218
- url: `${this.config.baseURL}/audio/transcriptions`,
1219
- headers: combineHeaders4(this.config.headers?.(), options.headers),
1220
- formData,
1221
- failedResponseHandler: mistralFailedResponseHandler,
1222
- successfulResponseHandler: createJsonResponseHandler4(
1223
- mistralTranscriptionResponseSchema
1224
- ),
1225
- abortSignal: options.abortSignal,
1226
- fetch: this.config.fetch
1227
- });
1228
- const segments = response.segments?.map((segment) => ({
1229
- text: segment.text,
1230
- startSecond: segment.start,
1231
- endSecond: segment.end
1232
- })) ?? [];
1233
- const mistralMetadata = {};
1234
- if (response.usage != null) {
1235
- mistralMetadata.usage = {
1236
- ...response.usage.prompt_tokens != null && {
1237
- promptTokens: response.usage.prompt_tokens
1238
- },
1239
- ...response.usage.completion_tokens != null && {
1240
- completionTokens: response.usage.completion_tokens
1241
- },
1242
- ...response.usage.total_tokens != null && {
1243
- totalTokens: response.usage.total_tokens
1244
- },
1245
- ...response.usage.prompt_audio_seconds != null && {
1246
- promptAudioSeconds: response.usage.prompt_audio_seconds
1247
- },
1248
- ...response.usage.request_count != null && {
1249
- requestCount: response.usage.request_count
1250
- }
1251
- };
1252
- }
1253
- const providerSegments = response.segments?.filter(
1254
- (segment) => segment.type != null || segment.score != null || segment.speaker_id != null
1255
- ).map((segment) => ({
1256
- text: segment.text,
1257
- startSecond: segment.start,
1258
- endSecond: segment.end,
1259
- ...segment.type != null && { type: segment.type },
1260
- ...segment.score != null && { score: segment.score },
1261
- ...segment.speaker_id != null && {
1262
- speakerId: segment.speaker_id
1263
- }
1264
- }));
1265
- if (providerSegments != null && providerSegments.length > 0) {
1266
- mistralMetadata.segments = providerSegments;
1267
- }
1268
- return {
1269
- text: response.text,
1270
- segments,
1271
- language: response.language ?? void 0,
1272
- durationInSeconds: response.usage?.prompt_audio_seconds ?? segments.at(-1)?.endSecond ?? void 0,
1273
- warnings,
1274
- response: {
1275
- timestamp: currentDate,
1276
- modelId: response.model,
1277
- headers: responseHeaders,
1278
- body: rawResponse
1279
- },
1280
- ...Object.keys(mistralMetadata).length > 0 && {
1281
- providerMetadata: {
1282
- mistral: mistralMetadata
1283
- }
1284
- }
1285
- };
1286
- }
874
+ const mistralSpeechResponseSchema = z.object({ audio_data: z.string() });
875
+ //#endregion
876
+ //#region src/mistral-transcription-model-options.ts
877
+ const mistralTranscriptionModelOptions = lazySchema(() => zodSchema(z.object({
878
+ /**
879
+ * The language of the audio, e.g. "en". Providing the language can boost
880
+ * accuracy.
881
+ */
882
+ language: z.string().min(1).optional(),
883
+ /**
884
+ * The sampling temperature.
885
+ */
886
+ temperature: z.number().optional(),
887
+ /**
888
+ * The timestamp granularities to include in the transcription response.
889
+ */
890
+ timestampGranularities: z.array(z.enum(["segment", "word"])).min(1).optional(),
891
+ /**
892
+ * Whether to identify speakers in the transcription.
893
+ */
894
+ diarize: z.boolean().optional(),
895
+ /**
896
+ * Words or phrases to guide the model toward correct spellings of names,
897
+ * technical terms, or domain-specific vocabulary.
898
+ *
899
+ * Mistral requires each item to omit commas and whitespace. Use
900
+ * underscores to represent multi-word phrases.
901
+ */
902
+ contextBias: z.array(z.string().min(1).regex(/^[^\s,]+$/, "Context bias items must not contain commas or whitespace.")).max(100).optional()
903
+ })));
904
+ //#endregion
905
+ //#region src/mistral-transcription-model.ts
906
+ var MistralTranscriptionModel = class MistralTranscriptionModel {
907
+ static [WORKFLOW_SERIALIZE](model) {
908
+ return serializeModelOptions({
909
+ modelId: model.modelId,
910
+ config: model.config
911
+ });
912
+ }
913
+ static [WORKFLOW_DESERIALIZE](options) {
914
+ return new MistralTranscriptionModel(options.modelId, options.config);
915
+ }
916
+ get provider() {
917
+ return this.config.provider;
918
+ }
919
+ constructor(modelId, config) {
920
+ this.modelId = modelId;
921
+ this.config = config;
922
+ this.specificationVersion = "v4";
923
+ }
924
+ async getArgs({ audio, mediaType, providerOptions }) {
925
+ const warnings = [];
926
+ const mistralOptions = await parseProviderOptions({
927
+ provider: "mistral",
928
+ providerOptions,
929
+ schema: mistralTranscriptionModelOptions
930
+ });
931
+ if (mistralOptions?.language != null && mistralOptions.timestampGranularities != null) throw new InvalidArgumentError({
932
+ argument: "providerOptions",
933
+ message: "providerOptions.mistral.language cannot be combined with providerOptions.mistral.timestampGranularities"
934
+ });
935
+ const formData = new FormData();
936
+ const blob = audio instanceof Uint8Array ? new Blob([audio]) : new Blob([convertBase64ToUint8Array(audio)]);
937
+ formData.append("model", this.modelId);
938
+ formData.append("file", new File([blob], "audio", { type: mediaType }), `audio.${mediaTypeToExtension(mediaType)}`);
939
+ if (mistralOptions != null) {
940
+ appendFormValue(formData, "language", mistralOptions.language);
941
+ appendFormValue(formData, "temperature", mistralOptions.temperature);
942
+ appendFormValue(formData, "timestamp_granularities", mistralOptions.timestampGranularities);
943
+ appendFormValue(formData, "diarize", mistralOptions.diarize);
944
+ appendFormValue(formData, "context_bias", mistralOptions.contextBias);
945
+ }
946
+ return {
947
+ formData,
948
+ warnings
949
+ };
950
+ }
951
+ async doGenerate(options) {
952
+ const currentDate = this.config._internal?.currentDate?.() ?? /* @__PURE__ */ new Date();
953
+ const { formData, warnings } = await this.getArgs(options);
954
+ const { value: response, responseHeaders, rawValue: rawResponse } = await postFormDataToApi({
955
+ url: `${this.config.baseURL}/audio/transcriptions`,
956
+ headers: combineHeaders(this.config.headers?.(), options.headers),
957
+ formData,
958
+ failedResponseHandler: mistralFailedResponseHandler,
959
+ successfulResponseHandler: createJsonResponseHandler(mistralTranscriptionResponseSchema),
960
+ abortSignal: options.abortSignal,
961
+ fetch: this.config.fetch
962
+ });
963
+ const segments = response.segments?.map((segment) => ({
964
+ text: segment.text,
965
+ startSecond: segment.start,
966
+ endSecond: segment.end
967
+ })) ?? [];
968
+ const mistralMetadata = {};
969
+ if (response.usage != null) mistralMetadata.usage = {
970
+ ...response.usage.prompt_tokens != null && { promptTokens: response.usage.prompt_tokens },
971
+ ...response.usage.completion_tokens != null && { completionTokens: response.usage.completion_tokens },
972
+ ...response.usage.total_tokens != null && { totalTokens: response.usage.total_tokens },
973
+ ...response.usage.prompt_audio_seconds != null && { promptAudioSeconds: response.usage.prompt_audio_seconds },
974
+ ...response.usage.request_count != null && { requestCount: response.usage.request_count }
975
+ };
976
+ const providerSegments = response.segments?.filter((segment) => segment.type != null || segment.score != null || segment.speaker_id != null).map((segment) => ({
977
+ text: segment.text,
978
+ startSecond: segment.start,
979
+ endSecond: segment.end,
980
+ ...segment.type != null && { type: segment.type },
981
+ ...segment.score != null && { score: segment.score },
982
+ ...segment.speaker_id != null && { speakerId: segment.speaker_id }
983
+ }));
984
+ if (providerSegments != null && providerSegments.length > 0) mistralMetadata.segments = providerSegments;
985
+ return {
986
+ text: response.text,
987
+ segments,
988
+ language: response.language ?? void 0,
989
+ durationInSeconds: response.usage?.prompt_audio_seconds ?? segments.at(-1)?.endSecond ?? void 0,
990
+ warnings,
991
+ response: {
992
+ timestamp: currentDate,
993
+ modelId: response.model,
994
+ headers: responseHeaders,
995
+ body: rawResponse
996
+ },
997
+ ...Object.keys(mistralMetadata).length > 0 && { providerMetadata: { mistral: mistralMetadata } }
998
+ };
999
+ }
1287
1000
  };
1288
1001
  function appendFormValue(formData, key, value) {
1289
- if (value == null) {
1290
- return;
1291
- }
1292
- if (Array.isArray(value)) {
1293
- for (const item of value) {
1294
- formData.append(key, item);
1295
- }
1296
- return;
1297
- }
1298
- formData.append(key, String(value));
1002
+ if (value == null) return;
1003
+ if (Array.isArray(value)) {
1004
+ for (const item of value) formData.append(key, item);
1005
+ return;
1006
+ }
1007
+ formData.append(key, String(value));
1299
1008
  }
1300
- var mistralTranscriptionResponseSchema = z10.object({
1301
- model: z10.string(),
1302
- text: z10.string(),
1303
- language: z10.string().nullish(),
1304
- segments: z10.array(
1305
- z10.object({
1306
- type: z10.literal("transcription_segment").nullish(),
1307
- text: z10.string(),
1308
- start: z10.number(),
1309
- end: z10.number(),
1310
- score: z10.number().nullish(),
1311
- speaker_id: z10.string().nullish()
1312
- })
1313
- ).nullish(),
1314
- usage: z10.object({
1315
- prompt_tokens: z10.number().nullish(),
1316
- completion_tokens: z10.number().nullish(),
1317
- total_tokens: z10.number().nullish(),
1318
- prompt_audio_seconds: z10.number().nullish(),
1319
- request_count: z10.number().nullish()
1320
- }).nullish()
1009
+ const mistralTranscriptionResponseSchema = z.object({
1010
+ model: z.string(),
1011
+ text: z.string(),
1012
+ language: z.string().nullish(),
1013
+ segments: z.array(z.object({
1014
+ type: z.literal("transcription_segment").nullish(),
1015
+ text: z.string(),
1016
+ start: z.number(),
1017
+ end: z.number(),
1018
+ score: z.number().nullish(),
1019
+ speaker_id: z.string().nullish()
1020
+ })).nullish(),
1021
+ usage: z.object({
1022
+ prompt_tokens: z.number().nullish(),
1023
+ completion_tokens: z.number().nullish(),
1024
+ total_tokens: z.number().nullish(),
1025
+ prompt_audio_seconds: z.number().nullish(),
1026
+ request_count: z.number().nullish()
1027
+ }).nullish()
1321
1028
  });
1322
-
1323
- // src/version.ts
1324
- var VERSION = true ? "4.0.53" : "0.0.0-test";
1325
-
1326
- // src/mistral-provider.ts
1029
+ //#endregion
1030
+ //#region src/version.ts
1031
+ const VERSION = "4.0.55";
1032
+ //#endregion
1033
+ //#region src/mistral-provider.ts
1034
+ /**
1035
+ * Create a Mistral AI provider instance.
1036
+ */
1327
1037
  function createMistral(options = {}) {
1328
- const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.mistral.ai/v1";
1329
- const getHeaders = () => withUserAgentSuffix(
1330
- {
1331
- Authorization: `Bearer ${loadApiKey({
1332
- apiKey: options.apiKey,
1333
- environmentVariableName: "MISTRAL_API_KEY",
1334
- description: "Mistral"
1335
- })}`,
1336
- ...options.headers
1337
- },
1338
- `ai-sdk/mistral/${VERSION}`
1339
- );
1340
- const createChatModel = (modelId) => new MistralChatLanguageModel(modelId, {
1341
- provider: "mistral.chat",
1342
- baseURL,
1343
- headers: getHeaders,
1344
- fetch: options.fetch,
1345
- generateId: options.generateId
1346
- });
1347
- const createEmbeddingModel = (modelId) => new MistralEmbeddingModel(modelId, {
1348
- provider: "mistral.embedding",
1349
- baseURL,
1350
- headers: getHeaders,
1351
- fetch: options.fetch
1352
- });
1353
- const createSpeechModel = (modelId) => new MistralSpeechModel(modelId, {
1354
- provider: "mistral.speech",
1355
- baseURL,
1356
- headers: getHeaders,
1357
- fetch: options.fetch
1358
- });
1359
- const createTranscriptionModel = (modelId) => new MistralTranscriptionModel(modelId, {
1360
- provider: "mistral.transcription",
1361
- baseURL,
1362
- headers: getHeaders,
1363
- fetch: options.fetch
1364
- });
1365
- const provider = function(modelId) {
1366
- if (new.target) {
1367
- throw new Error(
1368
- "The Mistral model function cannot be called with the new keyword."
1369
- );
1370
- }
1371
- return createChatModel(modelId);
1372
- };
1373
- provider.specificationVersion = "v4";
1374
- provider.languageModel = createChatModel;
1375
- provider.chat = createChatModel;
1376
- provider.embedding = createEmbeddingModel;
1377
- provider.embeddingModel = createEmbeddingModel;
1378
- provider.textEmbedding = createEmbeddingModel;
1379
- provider.textEmbeddingModel = createEmbeddingModel;
1380
- provider.speech = createSpeechModel;
1381
- provider.speechModel = createSpeechModel;
1382
- provider.transcription = createTranscriptionModel;
1383
- provider.transcriptionModel = createTranscriptionModel;
1384
- provider.imageModel = (modelId) => {
1385
- throw new NoSuchModelError({ modelId, modelType: "imageModel" });
1386
- };
1387
- return provider;
1038
+ const baseURL = withoutTrailingSlash(options.baseURL) ?? "https://api.mistral.ai/v1";
1039
+ const getHeaders = () => withUserAgentSuffix({
1040
+ Authorization: `Bearer ${loadApiKey({
1041
+ apiKey: options.apiKey,
1042
+ environmentVariableName: "MISTRAL_API_KEY",
1043
+ description: "Mistral"
1044
+ })}`,
1045
+ ...options.headers
1046
+ }, `ai-sdk-mistral/${VERSION}`);
1047
+ const createChatModel = (modelId) => new MistralChatLanguageModel(modelId, {
1048
+ provider: "mistral.chat",
1049
+ baseURL,
1050
+ headers: getHeaders,
1051
+ fetch: options.fetch,
1052
+ generateId: options.generateId
1053
+ });
1054
+ const createEmbeddingModel = (modelId) => new MistralEmbeddingModel(modelId, {
1055
+ provider: "mistral.embedding",
1056
+ baseURL,
1057
+ headers: getHeaders,
1058
+ fetch: options.fetch
1059
+ });
1060
+ const createSpeechModel = (modelId) => new MistralSpeechModel(modelId, {
1061
+ provider: "mistral.speech",
1062
+ baseURL,
1063
+ headers: getHeaders,
1064
+ fetch: options.fetch
1065
+ });
1066
+ const createTranscriptionModel = (modelId) => new MistralTranscriptionModel(modelId, {
1067
+ provider: "mistral.transcription",
1068
+ baseURL,
1069
+ headers: getHeaders,
1070
+ fetch: options.fetch
1071
+ });
1072
+ const provider = function(modelId) {
1073
+ if (new.target) throw new Error("The Mistral model function cannot be called with the new keyword.");
1074
+ return createChatModel(modelId);
1075
+ };
1076
+ provider.specificationVersion = "v4";
1077
+ provider.languageModel = createChatModel;
1078
+ provider.chat = createChatModel;
1079
+ provider.embedding = createEmbeddingModel;
1080
+ provider.embeddingModel = createEmbeddingModel;
1081
+ provider.textEmbedding = createEmbeddingModel;
1082
+ provider.textEmbeddingModel = createEmbeddingModel;
1083
+ provider.speech = createSpeechModel;
1084
+ provider.speechModel = createSpeechModel;
1085
+ provider.transcription = createTranscriptionModel;
1086
+ provider.transcriptionModel = createTranscriptionModel;
1087
+ provider.imageModel = (modelId) => {
1088
+ throw new NoSuchModelError({
1089
+ modelId,
1090
+ modelType: "imageModel"
1091
+ });
1092
+ };
1093
+ return provider;
1388
1094
  }
1389
- var mistral = createMistral();
1390
- export {
1391
- VERSION,
1392
- createMistral,
1393
- mistral
1394
- };
1095
+ /**
1096
+ * Default Mistral provider instance.
1097
+ */
1098
+ const mistral = createMistral();
1099
+ //#endregion
1100
+ export { VERSION, createMistral, mistral };
1101
+
1395
1102
  //# sourceMappingURL=index.js.map