@tanstack/ai-mistral 0.2.2 → 0.2.4
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/esm/adapters/text.js +695 -731
- package/dist/esm/adapters/text.js.map +1 -1
- package/dist/esm/index.js +1 -7
- package/dist/esm/model-meta.js +233 -49
- package/dist/esm/model-meta.js.map +1 -1
- package/dist/esm/tools/function-tool.js +33 -23
- package/dist/esm/tools/function-tool.js.map +1 -1
- package/dist/esm/tools/tool-converter.js +11 -7
- package/dist/esm/tools/tool-converter.js.map +1 -1
- package/dist/esm/utils/client.js +36 -37
- package/dist/esm/utils/client.js.map +1 -1
- package/dist/esm/utils/schema-converter.js +73 -89
- package/dist/esm/utils/schema-converter.js.map +1 -1
- package/package.json +6 -6
- package/src/adapters/text.ts +1 -1
- package/src/utils/schema-converter.ts +2 -2
- package/dist/esm/index.js.map +0 -1
|
@@ -1,741 +1,705 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { makeMistralStructuredOutputCompatible, transformNullsToUndefined } from "../utils/schema-converter.js";
|
|
2
2
|
import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
|
|
3
3
|
import { createMistralClient, generateId, getMistralApiKeyFromEnv } from "../utils/client.js";
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
);
|
|
4
|
+
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
5
|
+
//#region src/adapters/text.ts
|
|
6
|
+
/** Cast an event object to StreamChunk. Adapters construct events with string
|
|
7
|
+
* literal types which are structurally compatible with the EventType enum. */
|
|
8
|
+
var asChunk = (chunk) => chunk;
|
|
9
|
+
/**
|
|
10
|
+
* Parse the accumulated streaming arguments for a tool call. Throws a clear
|
|
11
|
+
* error if the JSON is malformed — silently substituting `{}` would let a
|
|
12
|
+
* tool fire with empty inputs, masking truncated streams or mis-shaped output.
|
|
13
|
+
*/
|
|
9
14
|
function parseToolCallInput(toolCall) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
{ cause }
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
class MistralTextAdapter extends BaseTextAdapter {
|
|
23
|
-
name = "mistral";
|
|
24
|
-
client;
|
|
25
|
-
rawConfig;
|
|
26
|
-
constructor(config, model) {
|
|
27
|
-
super(config, model);
|
|
28
|
-
this.client = createMistralClient(config);
|
|
29
|
-
this.rawConfig = config;
|
|
30
|
-
}
|
|
31
|
-
async *chatStream(options) {
|
|
32
|
-
const requestParams = this.mapTextOptionsToMistral(options);
|
|
33
|
-
const timestamp = Date.now();
|
|
34
|
-
const aguiState = {
|
|
35
|
-
runId: options.runId ?? generateId(this.name),
|
|
36
|
-
threadId: options.threadId ?? generateId(this.name),
|
|
37
|
-
messageId: generateId(this.name),
|
|
38
|
-
timestamp,
|
|
39
|
-
hasEmittedRunStarted: false
|
|
40
|
-
};
|
|
41
|
-
try {
|
|
42
|
-
const stream = this.fetchRawMistralStream(requestParams, this.rawConfig);
|
|
43
|
-
yield* this.processMistralStreamChunks(stream, options, aguiState);
|
|
44
|
-
} catch (error) {
|
|
45
|
-
const err = error;
|
|
46
|
-
if (!aguiState.hasEmittedRunStarted) {
|
|
47
|
-
aguiState.hasEmittedRunStarted = true;
|
|
48
|
-
yield asChunk({
|
|
49
|
-
type: "RUN_STARTED",
|
|
50
|
-
runId: aguiState.runId,
|
|
51
|
-
threadId: aguiState.threadId,
|
|
52
|
-
model: options.model,
|
|
53
|
-
timestamp
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
yield asChunk({
|
|
57
|
-
type: "RUN_ERROR",
|
|
58
|
-
runId: aguiState.runId,
|
|
59
|
-
model: options.model,
|
|
60
|
-
timestamp,
|
|
61
|
-
message: err.message || "Unknown error",
|
|
62
|
-
code: err.code,
|
|
63
|
-
error: {
|
|
64
|
-
message: err.message || "Unknown error",
|
|
65
|
-
code: err.code
|
|
66
|
-
}
|
|
67
|
-
});
|
|
68
|
-
throw err;
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Generate structured output using Mistral's JSON Schema response format.
|
|
73
|
-
*/
|
|
74
|
-
async structuredOutput(options) {
|
|
75
|
-
const { chatOptions, outputSchema } = options;
|
|
76
|
-
const { stream: _stream, ...nonStreamParams } = this.mapTextOptionsToMistral(chatOptions);
|
|
77
|
-
const jsonSchema = makeMistralStructuredOutputCompatible(
|
|
78
|
-
outputSchema,
|
|
79
|
-
outputSchema.required || []
|
|
80
|
-
);
|
|
81
|
-
const response = await this.client.chat.complete({
|
|
82
|
-
...nonStreamParams,
|
|
83
|
-
responseFormat: {
|
|
84
|
-
type: "json_schema",
|
|
85
|
-
jsonSchema: {
|
|
86
|
-
name: "structured_output",
|
|
87
|
-
schemaDefinition: jsonSchema,
|
|
88
|
-
strict: true
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
const rawText = response.choices[0]?.message?.content;
|
|
93
|
-
const textContent = typeof rawText === "string" ? rawText : "";
|
|
94
|
-
let parsed;
|
|
95
|
-
try {
|
|
96
|
-
parsed = JSON.parse(textContent);
|
|
97
|
-
} catch {
|
|
98
|
-
throw new Error(
|
|
99
|
-
`Failed to parse structured output as JSON. Content: ${textContent.slice(0, 200)}${textContent.length > 200 ? "..." : ""}`
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
data: transformNullsToUndefined(parsed),
|
|
104
|
-
rawText: textContent
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Processes streaming chunks from the Mistral API and yields AG-UI stream events.
|
|
109
|
-
*/
|
|
110
|
-
async *processMistralStreamChunks(stream, options, aguiState) {
|
|
111
|
-
let accumulatedContent = "";
|
|
112
|
-
const timestamp = aguiState.timestamp;
|
|
113
|
-
let hasEmittedTextMessageStart = false;
|
|
114
|
-
let hasEmittedTextMessageEnd = false;
|
|
115
|
-
let hasEmittedToolCall = false;
|
|
116
|
-
let hasEmittedRunFinished = false;
|
|
117
|
-
let lastChunkModel = options.model;
|
|
118
|
-
let reasoningMessageId = null;
|
|
119
|
-
let hasClosedReasoning = false;
|
|
120
|
-
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
121
|
-
try {
|
|
122
|
-
for await (const chunk of stream) {
|
|
123
|
-
lastChunkModel = chunk.model || options.model;
|
|
124
|
-
const choice = chunk.choices?.[0];
|
|
125
|
-
if (!choice) continue;
|
|
126
|
-
const chunkModel = chunk.model || options.model;
|
|
127
|
-
if (!aguiState.hasEmittedRunStarted) {
|
|
128
|
-
aguiState.hasEmittedRunStarted = true;
|
|
129
|
-
yield asChunk({
|
|
130
|
-
type: "RUN_STARTED",
|
|
131
|
-
runId: aguiState.runId,
|
|
132
|
-
threadId: aguiState.threadId,
|
|
133
|
-
model: chunkModel,
|
|
134
|
-
timestamp
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
const delta = choice.delta;
|
|
138
|
-
const { text: deltaContent, thinking: deltaThinkingFromContent } = this.extractDeltaParts(delta?.content);
|
|
139
|
-
const deltaThinking = deltaThinkingFromContent + (typeof delta?.reasoning_content === "string" ? delta.reasoning_content : "");
|
|
140
|
-
const deltaToolCalls = delta?.tool_calls;
|
|
141
|
-
if (deltaThinking) {
|
|
142
|
-
if (reasoningMessageId === null) {
|
|
143
|
-
reasoningMessageId = generateId(this.name);
|
|
144
|
-
yield asChunk({
|
|
145
|
-
type: "REASONING_START",
|
|
146
|
-
messageId: reasoningMessageId,
|
|
147
|
-
model: chunkModel,
|
|
148
|
-
timestamp
|
|
149
|
-
});
|
|
150
|
-
yield asChunk({
|
|
151
|
-
type: "REASONING_MESSAGE_START",
|
|
152
|
-
messageId: reasoningMessageId,
|
|
153
|
-
role: "reasoning",
|
|
154
|
-
model: chunkModel,
|
|
155
|
-
timestamp
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
yield asChunk({
|
|
159
|
-
type: "REASONING_MESSAGE_CONTENT",
|
|
160
|
-
messageId: reasoningMessageId,
|
|
161
|
-
model: chunkModel,
|
|
162
|
-
timestamp,
|
|
163
|
-
delta: deltaThinking
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
const aboutToEmitOutput = !!deltaContent || !!deltaToolCalls && deltaToolCalls.length > 0;
|
|
167
|
-
if (reasoningMessageId !== null && !hasClosedReasoning && aboutToEmitOutput) {
|
|
168
|
-
hasClosedReasoning = true;
|
|
169
|
-
yield asChunk({
|
|
170
|
-
type: "REASONING_MESSAGE_END",
|
|
171
|
-
messageId: reasoningMessageId,
|
|
172
|
-
model: chunkModel,
|
|
173
|
-
timestamp
|
|
174
|
-
});
|
|
175
|
-
yield asChunk({
|
|
176
|
-
type: "REASONING_END",
|
|
177
|
-
messageId: reasoningMessageId,
|
|
178
|
-
model: chunkModel,
|
|
179
|
-
timestamp
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
if (deltaContent) {
|
|
183
|
-
if (!hasEmittedTextMessageStart) {
|
|
184
|
-
hasEmittedTextMessageStart = true;
|
|
185
|
-
yield asChunk({
|
|
186
|
-
type: "TEXT_MESSAGE_START",
|
|
187
|
-
messageId: aguiState.messageId,
|
|
188
|
-
model: chunkModel,
|
|
189
|
-
timestamp,
|
|
190
|
-
role: "assistant"
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
accumulatedContent += deltaContent;
|
|
194
|
-
yield asChunk({
|
|
195
|
-
type: "TEXT_MESSAGE_CONTENT",
|
|
196
|
-
messageId: aguiState.messageId,
|
|
197
|
-
model: chunkModel,
|
|
198
|
-
timestamp,
|
|
199
|
-
delta: deltaContent,
|
|
200
|
-
content: accumulatedContent
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
if (deltaToolCalls) {
|
|
204
|
-
for (const [i, toolCallDelta] of deltaToolCalls.entries()) {
|
|
205
|
-
const index = toolCallDelta.index ?? i;
|
|
206
|
-
let toolCall = toolCallsInProgress.get(index);
|
|
207
|
-
if (!toolCall) {
|
|
208
|
-
toolCall = {
|
|
209
|
-
id: toolCallDelta.id || "",
|
|
210
|
-
name: toolCallDelta.function?.name || "",
|
|
211
|
-
arguments: "",
|
|
212
|
-
started: false,
|
|
213
|
-
ended: false
|
|
214
|
-
};
|
|
215
|
-
toolCallsInProgress.set(index, toolCall);
|
|
216
|
-
}
|
|
217
|
-
if (toolCallDelta.id) toolCall.id = toolCallDelta.id;
|
|
218
|
-
if (toolCallDelta.function?.name) {
|
|
219
|
-
toolCall.name = toolCallDelta.function.name;
|
|
220
|
-
}
|
|
221
|
-
const rawArgs = toolCallDelta.function?.arguments;
|
|
222
|
-
const argsDelta = rawArgs === void 0 ? void 0 : typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs);
|
|
223
|
-
if (argsDelta !== void 0) {
|
|
224
|
-
toolCall.arguments += argsDelta;
|
|
225
|
-
}
|
|
226
|
-
const justStarted = !!toolCall.id && !!toolCall.name && !toolCall.started;
|
|
227
|
-
if (justStarted) {
|
|
228
|
-
toolCall.started = true;
|
|
229
|
-
yield asChunk({
|
|
230
|
-
type: "TOOL_CALL_START",
|
|
231
|
-
toolCallId: toolCall.id,
|
|
232
|
-
toolCallName: toolCall.name,
|
|
233
|
-
toolName: toolCall.name,
|
|
234
|
-
model: chunkModel,
|
|
235
|
-
timestamp,
|
|
236
|
-
index
|
|
237
|
-
});
|
|
238
|
-
if (toolCall.arguments.length > 0) {
|
|
239
|
-
yield asChunk({
|
|
240
|
-
type: "TOOL_CALL_ARGS",
|
|
241
|
-
toolCallId: toolCall.id,
|
|
242
|
-
model: chunkModel,
|
|
243
|
-
timestamp,
|
|
244
|
-
delta: toolCall.arguments
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
} else if (argsDelta !== void 0 && toolCall.started) {
|
|
248
|
-
yield asChunk({
|
|
249
|
-
type: "TOOL_CALL_ARGS",
|
|
250
|
-
toolCallId: toolCall.id,
|
|
251
|
-
model: chunkModel,
|
|
252
|
-
timestamp,
|
|
253
|
-
delta: argsDelta
|
|
254
|
-
});
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
const finishReason = choice.finish_reason;
|
|
259
|
-
if (finishReason) {
|
|
260
|
-
if (finishReason === "tool_calls" || toolCallsInProgress.size > 0) {
|
|
261
|
-
for (const [, toolCall] of toolCallsInProgress) {
|
|
262
|
-
if (!toolCall.started || !toolCall.id || !toolCall.name || toolCall.ended) {
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
const parsedInput = parseToolCallInput(toolCall);
|
|
266
|
-
toolCall.ended = true;
|
|
267
|
-
hasEmittedToolCall = true;
|
|
268
|
-
yield asChunk({
|
|
269
|
-
type: "TOOL_CALL_END",
|
|
270
|
-
toolCallId: toolCall.id,
|
|
271
|
-
toolCallName: toolCall.name,
|
|
272
|
-
toolName: toolCall.name,
|
|
273
|
-
model: chunkModel,
|
|
274
|
-
timestamp,
|
|
275
|
-
input: parsedInput
|
|
276
|
-
});
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
const computedFinishReason = finishReason === "tool_calls" || hasEmittedToolCall ? "tool_calls" : finishReason === "length" ? "length" : "stop";
|
|
280
|
-
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
281
|
-
hasClosedReasoning = true;
|
|
282
|
-
yield asChunk({
|
|
283
|
-
type: "REASONING_MESSAGE_END",
|
|
284
|
-
messageId: reasoningMessageId,
|
|
285
|
-
model: chunkModel,
|
|
286
|
-
timestamp
|
|
287
|
-
});
|
|
288
|
-
yield asChunk({
|
|
289
|
-
type: "REASONING_END",
|
|
290
|
-
messageId: reasoningMessageId,
|
|
291
|
-
model: chunkModel,
|
|
292
|
-
timestamp
|
|
293
|
-
});
|
|
294
|
-
}
|
|
295
|
-
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
296
|
-
hasEmittedTextMessageEnd = true;
|
|
297
|
-
yield asChunk({
|
|
298
|
-
type: "TEXT_MESSAGE_END",
|
|
299
|
-
messageId: aguiState.messageId,
|
|
300
|
-
model: chunkModel,
|
|
301
|
-
timestamp
|
|
302
|
-
});
|
|
303
|
-
}
|
|
304
|
-
const usage = chunk.usage;
|
|
305
|
-
hasEmittedRunFinished = true;
|
|
306
|
-
yield asChunk({
|
|
307
|
-
type: "RUN_FINISHED",
|
|
308
|
-
runId: aguiState.runId,
|
|
309
|
-
threadId: aguiState.threadId,
|
|
310
|
-
model: chunkModel,
|
|
311
|
-
timestamp,
|
|
312
|
-
usage: usage ? {
|
|
313
|
-
promptTokens: usage.prompt_tokens || 0,
|
|
314
|
-
completionTokens: usage.completion_tokens || 0,
|
|
315
|
-
totalTokens: usage.total_tokens || 0
|
|
316
|
-
} : void 0,
|
|
317
|
-
finishReason: computedFinishReason
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
}
|
|
321
|
-
if (!hasEmittedRunFinished) {
|
|
322
|
-
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
323
|
-
hasClosedReasoning = true;
|
|
324
|
-
yield asChunk({
|
|
325
|
-
type: "REASONING_MESSAGE_END",
|
|
326
|
-
messageId: reasoningMessageId,
|
|
327
|
-
model: lastChunkModel,
|
|
328
|
-
timestamp
|
|
329
|
-
});
|
|
330
|
-
yield asChunk({
|
|
331
|
-
type: "REASONING_END",
|
|
332
|
-
messageId: reasoningMessageId,
|
|
333
|
-
model: lastChunkModel,
|
|
334
|
-
timestamp
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
for (const [, toolCall] of toolCallsInProgress) {
|
|
338
|
-
if (toolCall.started && !toolCall.ended) {
|
|
339
|
-
toolCall.ended = true;
|
|
340
|
-
hasEmittedToolCall = true;
|
|
341
|
-
yield asChunk({
|
|
342
|
-
type: "TOOL_CALL_END",
|
|
343
|
-
toolCallId: toolCall.id,
|
|
344
|
-
toolCallName: toolCall.name,
|
|
345
|
-
toolName: toolCall.name,
|
|
346
|
-
model: lastChunkModel,
|
|
347
|
-
timestamp,
|
|
348
|
-
input: parseToolCallInput(toolCall)
|
|
349
|
-
});
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
353
|
-
hasEmittedTextMessageEnd = true;
|
|
354
|
-
yield asChunk({
|
|
355
|
-
type: "TEXT_MESSAGE_END",
|
|
356
|
-
messageId: aguiState.messageId,
|
|
357
|
-
model: lastChunkModel,
|
|
358
|
-
timestamp
|
|
359
|
-
});
|
|
360
|
-
}
|
|
361
|
-
hasEmittedRunFinished = true;
|
|
362
|
-
yield asChunk({
|
|
363
|
-
type: "RUN_FINISHED",
|
|
364
|
-
runId: aguiState.runId,
|
|
365
|
-
threadId: aguiState.threadId,
|
|
366
|
-
model: lastChunkModel,
|
|
367
|
-
timestamp,
|
|
368
|
-
usage: void 0,
|
|
369
|
-
finishReason: hasEmittedToolCall ? "tool_calls" : "stop"
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
} catch (error) {
|
|
373
|
-
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
374
|
-
hasClosedReasoning = true;
|
|
375
|
-
yield asChunk({
|
|
376
|
-
type: "REASONING_MESSAGE_END",
|
|
377
|
-
messageId: reasoningMessageId,
|
|
378
|
-
model: lastChunkModel,
|
|
379
|
-
timestamp
|
|
380
|
-
});
|
|
381
|
-
yield asChunk({
|
|
382
|
-
type: "REASONING_END",
|
|
383
|
-
messageId: reasoningMessageId,
|
|
384
|
-
model: lastChunkModel,
|
|
385
|
-
timestamp
|
|
386
|
-
});
|
|
387
|
-
}
|
|
388
|
-
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
389
|
-
hasEmittedTextMessageEnd = true;
|
|
390
|
-
yield asChunk({
|
|
391
|
-
type: "TEXT_MESSAGE_END",
|
|
392
|
-
messageId: aguiState.messageId,
|
|
393
|
-
model: lastChunkModel,
|
|
394
|
-
timestamp
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
for (const [, toolCall] of toolCallsInProgress) {
|
|
398
|
-
if (toolCall.started && !toolCall.ended) {
|
|
399
|
-
toolCall.ended = true;
|
|
400
|
-
let partialInput = {};
|
|
401
|
-
try {
|
|
402
|
-
partialInput = toolCall.arguments ? transformNullsToUndefined(JSON.parse(toolCall.arguments)) : {};
|
|
403
|
-
} catch {
|
|
404
|
-
partialInput = {};
|
|
405
|
-
}
|
|
406
|
-
yield asChunk({
|
|
407
|
-
type: "TOOL_CALL_END",
|
|
408
|
-
toolCallId: toolCall.id,
|
|
409
|
-
toolCallName: toolCall.name,
|
|
410
|
-
toolName: toolCall.name,
|
|
411
|
-
model: lastChunkModel,
|
|
412
|
-
timestamp,
|
|
413
|
-
input: partialInput
|
|
414
|
-
});
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
throw error;
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
/**
|
|
421
|
-
* Makes a raw fetch request to the Mistral chat completions endpoint and
|
|
422
|
-
* parses the SSE stream manually, bypassing the SDK's Zod validation which
|
|
423
|
-
* rejects streaming tool call chunks that omit `name` in argument deltas.
|
|
424
|
-
*/
|
|
425
|
-
async *fetchRawMistralStream(params, config) {
|
|
426
|
-
const serverURL = (config.serverURL ?? "https://api.mistral.ai").replace(/\/+$/, "").replace(/\/v1$/, "");
|
|
427
|
-
const url = `${serverURL}/v1/chat/completions`;
|
|
428
|
-
const body = this.toWireBody(params);
|
|
429
|
-
const headers = {
|
|
430
|
-
"Content-Type": "application/json",
|
|
431
|
-
Authorization: `Bearer ${config.apiKey}`,
|
|
432
|
-
...config.defaultHeaders
|
|
433
|
-
};
|
|
434
|
-
const response = await fetch(url, {
|
|
435
|
-
method: "POST",
|
|
436
|
-
headers,
|
|
437
|
-
body: JSON.stringify(body)
|
|
438
|
-
});
|
|
439
|
-
if (!response.ok) {
|
|
440
|
-
const errorText = await response.text();
|
|
441
|
-
throw new Error(`Mistral API error ${response.status}: ${errorText}`);
|
|
442
|
-
}
|
|
443
|
-
if (!response.body) {
|
|
444
|
-
throw new Error(
|
|
445
|
-
"Mistral API returned a response with no body. This may indicate a proxy or runtime that does not support streaming."
|
|
446
|
-
);
|
|
447
|
-
}
|
|
448
|
-
const reader = response.body.getReader();
|
|
449
|
-
const decoder = new TextDecoder();
|
|
450
|
-
let buffer = "";
|
|
451
|
-
try {
|
|
452
|
-
for (; ; ) {
|
|
453
|
-
const { done, value } = await reader.read();
|
|
454
|
-
if (done) break;
|
|
455
|
-
buffer += decoder.decode(value, { stream: true });
|
|
456
|
-
const lines = buffer.split("\n");
|
|
457
|
-
buffer = lines.pop() ?? "";
|
|
458
|
-
for (const line of lines) {
|
|
459
|
-
const trimmed = line.trim();
|
|
460
|
-
if (!trimmed.startsWith("data:")) continue;
|
|
461
|
-
const data = trimmed.slice(5).trimStart();
|
|
462
|
-
if (data === "[DONE]") return;
|
|
463
|
-
let parsed;
|
|
464
|
-
try {
|
|
465
|
-
parsed = JSON.parse(data);
|
|
466
|
-
} catch (e) {
|
|
467
|
-
if (e instanceof SyntaxError) {
|
|
468
|
-
console.warn(
|
|
469
|
-
`[mistral] skipped unparseable SSE chunk: ${data.slice(0, 200)}`
|
|
470
|
-
);
|
|
471
|
-
continue;
|
|
472
|
-
}
|
|
473
|
-
throw e;
|
|
474
|
-
}
|
|
475
|
-
if (parsed && typeof parsed === "object" && "error" in parsed && !("choices" in parsed)) {
|
|
476
|
-
const errPayload = parsed.error;
|
|
477
|
-
const message = typeof errPayload === "string" ? errPayload : errPayload && typeof errPayload === "object" && "message" in errPayload ? String(errPayload.message) : JSON.stringify(errPayload);
|
|
478
|
-
throw new Error(`Mistral stream error: ${message}`);
|
|
479
|
-
}
|
|
480
|
-
yield parsed;
|
|
481
|
-
}
|
|
482
|
-
}
|
|
483
|
-
} finally {
|
|
484
|
-
await reader.cancel().catch(() => {
|
|
485
|
-
});
|
|
486
|
-
reader.releaseLock();
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
/**
|
|
490
|
-
* Converts the SDK's camelCase `ChatCompletionStreamRequest` into the
|
|
491
|
-
* snake_case wire body, including converting messages.
|
|
492
|
-
*/
|
|
493
|
-
toWireBody(params) {
|
|
494
|
-
const {
|
|
495
|
-
messages,
|
|
496
|
-
maxTokens,
|
|
497
|
-
topP,
|
|
498
|
-
randomSeed,
|
|
499
|
-
responseFormat,
|
|
500
|
-
toolChoice,
|
|
501
|
-
parallelToolCalls,
|
|
502
|
-
frequencyPenalty,
|
|
503
|
-
presencePenalty,
|
|
504
|
-
safePrompt,
|
|
505
|
-
stream: _stream,
|
|
506
|
-
...rest
|
|
507
|
-
} = params;
|
|
508
|
-
return {
|
|
509
|
-
...rest,
|
|
510
|
-
messages: messages.map(messageToWire),
|
|
511
|
-
stream: true,
|
|
512
|
-
// Opt in to usage on the final streaming chunk.
|
|
513
|
-
stream_options: { include_usage: true },
|
|
514
|
-
...maxTokens != null && { max_tokens: maxTokens },
|
|
515
|
-
...topP != null && { top_p: topP },
|
|
516
|
-
...randomSeed != null && { random_seed: randomSeed },
|
|
517
|
-
...responseFormat != null && { response_format: responseFormat },
|
|
518
|
-
...toolChoice != null && { tool_choice: toolChoice },
|
|
519
|
-
...parallelToolCalls != null && {
|
|
520
|
-
parallel_tool_calls: parallelToolCalls
|
|
521
|
-
},
|
|
522
|
-
...frequencyPenalty != null && { frequency_penalty: frequencyPenalty },
|
|
523
|
-
...presencePenalty != null && { presence_penalty: presencePenalty },
|
|
524
|
-
...safePrompt != null && { safe_prompt: safePrompt }
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
/**
|
|
528
|
-
* Splits a Mistral delta content payload into text and reasoning deltas.
|
|
529
|
-
* Mistral reasoning models (magistral-*) stream reasoning content as
|
|
530
|
-
* `{ type: 'thinking', thinking: [{ type: 'text', text }, ...] }` content
|
|
531
|
-
* parts. A single delta may contain text only, thinking only, or — rarely —
|
|
532
|
-
* both (when a step transitions); both fields are returned so the caller
|
|
533
|
-
* can sequence REASONING and TEXT lifecycle events in order.
|
|
534
|
-
*/
|
|
535
|
-
extractDeltaParts(content) {
|
|
536
|
-
if (!content) return { text: "", thinking: "" };
|
|
537
|
-
if (typeof content === "string") return { text: content, thinking: "" };
|
|
538
|
-
let text = "";
|
|
539
|
-
let thinking = "";
|
|
540
|
-
for (const part of content) {
|
|
541
|
-
if (part.type === "text" && typeof part.text === "string") {
|
|
542
|
-
text += part.text;
|
|
543
|
-
} else if (part.type === "thinking" && Array.isArray(part.thinking)) {
|
|
544
|
-
for (const inner of part.thinking) {
|
|
545
|
-
if (inner.type === "text" && typeof inner.text === "string") {
|
|
546
|
-
thinking += inner.text;
|
|
547
|
-
}
|
|
548
|
-
}
|
|
549
|
-
}
|
|
550
|
-
}
|
|
551
|
-
return { text, thinking };
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* Maps common TextOptions to Mistral Chat Completions request parameters.
|
|
555
|
-
*/
|
|
556
|
-
mapTextOptionsToMistral(options) {
|
|
557
|
-
const modelOptions = options.modelOptions;
|
|
558
|
-
const tools = options.tools ? convertToolsToProviderFormat(options.tools) : void 0;
|
|
559
|
-
const messages = [];
|
|
560
|
-
if (options.systemPrompts && options.systemPrompts.length > 0) {
|
|
561
|
-
messages.push({
|
|
562
|
-
role: "system",
|
|
563
|
-
content: options.systemPrompts.join("\n")
|
|
564
|
-
});
|
|
565
|
-
}
|
|
566
|
-
for (const message of options.messages) {
|
|
567
|
-
messages.push(this.convertMessageToMistral(message));
|
|
568
|
-
}
|
|
569
|
-
return {
|
|
570
|
-
model: options.model,
|
|
571
|
-
messages,
|
|
572
|
-
temperature: modelOptions?.temperature ?? void 0,
|
|
573
|
-
maxTokens: modelOptions?.max_tokens ?? void 0,
|
|
574
|
-
topP: modelOptions?.top_p ?? void 0,
|
|
575
|
-
tools,
|
|
576
|
-
stream: true,
|
|
577
|
-
...modelOptions && {
|
|
578
|
-
...modelOptions.stop != null && { stop: modelOptions.stop },
|
|
579
|
-
...modelOptions.random_seed != null && {
|
|
580
|
-
randomSeed: modelOptions.random_seed
|
|
581
|
-
},
|
|
582
|
-
...modelOptions.response_format != null && {
|
|
583
|
-
responseFormat: modelOptions.response_format
|
|
584
|
-
},
|
|
585
|
-
...modelOptions.tool_choice != null && {
|
|
586
|
-
toolChoice: modelOptions.tool_choice
|
|
587
|
-
},
|
|
588
|
-
...modelOptions.parallel_tool_calls != null && {
|
|
589
|
-
parallelToolCalls: modelOptions.parallel_tool_calls
|
|
590
|
-
},
|
|
591
|
-
...modelOptions.frequency_penalty != null && {
|
|
592
|
-
frequencyPenalty: modelOptions.frequency_penalty
|
|
593
|
-
},
|
|
594
|
-
...modelOptions.presence_penalty != null && {
|
|
595
|
-
presencePenalty: modelOptions.presence_penalty
|
|
596
|
-
},
|
|
597
|
-
...modelOptions.n != null && { n: modelOptions.n },
|
|
598
|
-
...modelOptions.prediction != null && {
|
|
599
|
-
prediction: modelOptions.prediction
|
|
600
|
-
},
|
|
601
|
-
...modelOptions.safe_prompt != null && {
|
|
602
|
-
safePrompt: modelOptions.safe_prompt
|
|
603
|
-
}
|
|
604
|
-
}
|
|
605
|
-
};
|
|
606
|
-
}
|
|
607
|
-
/**
|
|
608
|
-
* Converts a TanStack AI ModelMessage to a Mistral ChatCompletionMessageParam.
|
|
609
|
-
*/
|
|
610
|
-
convertMessageToMistral(message) {
|
|
611
|
-
if (message.role === "tool") {
|
|
612
|
-
if (!message.toolCallId) {
|
|
613
|
-
throw new Error("Missing toolCallId for tool message");
|
|
614
|
-
}
|
|
615
|
-
return {
|
|
616
|
-
role: "tool",
|
|
617
|
-
toolCallId: message.toolCallId,
|
|
618
|
-
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
|
|
619
|
-
};
|
|
620
|
-
}
|
|
621
|
-
if (message.role === "assistant") {
|
|
622
|
-
const toolCalls = message.toolCalls?.map((tc) => ({
|
|
623
|
-
id: tc.id,
|
|
624
|
-
type: "function",
|
|
625
|
-
function: {
|
|
626
|
-
name: tc.function.name,
|
|
627
|
-
arguments: typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments)
|
|
628
|
-
}
|
|
629
|
-
}));
|
|
630
|
-
return {
|
|
631
|
-
role: "assistant",
|
|
632
|
-
content: this.extractTextContent(message.content),
|
|
633
|
-
...toolCalls && toolCalls.length > 0 ? { toolCalls } : {}
|
|
634
|
-
};
|
|
635
|
-
}
|
|
636
|
-
const contentParts = this.normalizeContent(message.content);
|
|
637
|
-
if (contentParts.length === 1 && contentParts[0]?.type === "text") {
|
|
638
|
-
return {
|
|
639
|
-
role: "user",
|
|
640
|
-
content: contentParts[0].content
|
|
641
|
-
};
|
|
642
|
-
}
|
|
643
|
-
const parts = contentParts.map(
|
|
644
|
-
(part) => this.convertContentPartToMistral(part)
|
|
645
|
-
);
|
|
646
|
-
return {
|
|
647
|
-
role: "user",
|
|
648
|
-
content: parts.length > 0 ? parts : ""
|
|
649
|
-
};
|
|
650
|
-
}
|
|
651
|
-
/**
|
|
652
|
-
* Converts a ContentPart to a Mistral content part. Returns undefined for
|
|
653
|
-
* unsupported part types.
|
|
654
|
-
*/
|
|
655
|
-
convertContentPartToMistral(part) {
|
|
656
|
-
if (part.type === "text") {
|
|
657
|
-
return { type: "text", text: part.content };
|
|
658
|
-
}
|
|
659
|
-
if (part.type === "image") {
|
|
660
|
-
const imageMetadata = part.metadata;
|
|
661
|
-
const imageValue = part.source.value;
|
|
662
|
-
const imageUrl = part.source.type === "data" && !imageValue.startsWith("data:") ? `data:${part.source.mimeType};base64,${imageValue}` : imageValue;
|
|
663
|
-
return {
|
|
664
|
-
type: "image_url",
|
|
665
|
-
imageUrl: imageMetadata?.detail ? { url: imageUrl, detail: imageMetadata.detail } : imageUrl
|
|
666
|
-
};
|
|
667
|
-
}
|
|
668
|
-
throw new Error(
|
|
669
|
-
`Mistral text adapter does not support content part of type '${part.type}'. Supported types: text, image. Use a vision-capable model (pixtral-large-latest, pixtral-12b-2409, mistral-medium-latest, or mistral-small-latest) for images.`
|
|
670
|
-
);
|
|
671
|
-
}
|
|
672
|
-
/**
|
|
673
|
-
* Normalizes message content to an array of ContentPart.
|
|
674
|
-
*/
|
|
675
|
-
normalizeContent(content) {
|
|
676
|
-
if (content === null) return [];
|
|
677
|
-
if (typeof content === "string") return [{ type: "text", content }];
|
|
678
|
-
return content;
|
|
679
|
-
}
|
|
680
|
-
/**
|
|
681
|
-
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
682
|
-
*/
|
|
683
|
-
extractTextContent(content) {
|
|
684
|
-
if (content === null) return "";
|
|
685
|
-
if (typeof content === "string") return content;
|
|
686
|
-
return content.filter((p) => p.type === "text").map((p) => p.content).join("");
|
|
687
|
-
}
|
|
15
|
+
if (!toolCall.arguments) return {};
|
|
16
|
+
try {
|
|
17
|
+
return transformNullsToUndefined(JSON.parse(toolCall.arguments));
|
|
18
|
+
} catch (cause) {
|
|
19
|
+
const preview = toolCall.arguments.slice(0, 200);
|
|
20
|
+
const ellipsis = toolCall.arguments.length > 200 ? "..." : "";
|
|
21
|
+
throw new Error(`Failed to parse tool call arguments for tool '${toolCall.name}' (id: ${toolCall.id}). Arguments: ${preview}${ellipsis}`, { cause });
|
|
22
|
+
}
|
|
688
23
|
}
|
|
24
|
+
/**
|
|
25
|
+
* Mistral Text (Chat) Adapter.
|
|
26
|
+
*
|
|
27
|
+
* Tree-shakeable adapter for Mistral chat/text completion functionality.
|
|
28
|
+
*/
|
|
29
|
+
var MistralTextAdapter = class extends BaseTextAdapter {
|
|
30
|
+
name = "mistral";
|
|
31
|
+
client;
|
|
32
|
+
rawConfig;
|
|
33
|
+
constructor(config, model) {
|
|
34
|
+
super(config, model);
|
|
35
|
+
this.client = createMistralClient(config);
|
|
36
|
+
this.rawConfig = config;
|
|
37
|
+
}
|
|
38
|
+
async *chatStream(options) {
|
|
39
|
+
const requestParams = this.mapTextOptionsToMistral(options);
|
|
40
|
+
const timestamp = Date.now();
|
|
41
|
+
const aguiState = {
|
|
42
|
+
runId: options.runId ?? generateId(this.name),
|
|
43
|
+
threadId: options.threadId ?? generateId(this.name),
|
|
44
|
+
messageId: generateId(this.name),
|
|
45
|
+
timestamp,
|
|
46
|
+
hasEmittedRunStarted: false
|
|
47
|
+
};
|
|
48
|
+
try {
|
|
49
|
+
const stream = this.fetchRawMistralStream(requestParams, this.rawConfig);
|
|
50
|
+
yield* this.processMistralStreamChunks(stream, options, aguiState);
|
|
51
|
+
} catch (error) {
|
|
52
|
+
const err = error;
|
|
53
|
+
if (!aguiState.hasEmittedRunStarted) {
|
|
54
|
+
aguiState.hasEmittedRunStarted = true;
|
|
55
|
+
yield asChunk({
|
|
56
|
+
type: "RUN_STARTED",
|
|
57
|
+
runId: aguiState.runId,
|
|
58
|
+
threadId: aguiState.threadId,
|
|
59
|
+
model: options.model,
|
|
60
|
+
timestamp
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
yield asChunk({
|
|
64
|
+
type: "RUN_ERROR",
|
|
65
|
+
runId: aguiState.runId,
|
|
66
|
+
model: options.model,
|
|
67
|
+
timestamp,
|
|
68
|
+
message: err.message || "Unknown error",
|
|
69
|
+
code: err.code,
|
|
70
|
+
error: {
|
|
71
|
+
message: err.message || "Unknown error",
|
|
72
|
+
code: err.code
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
throw err;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Generate structured output using Mistral's JSON Schema response format.
|
|
80
|
+
*/
|
|
81
|
+
async structuredOutput(options) {
|
|
82
|
+
const { chatOptions, outputSchema } = options;
|
|
83
|
+
const { stream: _stream, ...nonStreamParams } = this.mapTextOptionsToMistral(chatOptions);
|
|
84
|
+
const jsonSchema = makeMistralStructuredOutputCompatible(outputSchema, outputSchema.required || []);
|
|
85
|
+
const rawText = (await this.client.chat.complete({
|
|
86
|
+
...nonStreamParams,
|
|
87
|
+
responseFormat: {
|
|
88
|
+
type: "json_schema",
|
|
89
|
+
jsonSchema: {
|
|
90
|
+
name: "structured_output",
|
|
91
|
+
schemaDefinition: jsonSchema,
|
|
92
|
+
strict: true
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
})).choices[0]?.message?.content;
|
|
96
|
+
const textContent = typeof rawText === "string" ? rawText : "";
|
|
97
|
+
let parsed;
|
|
98
|
+
try {
|
|
99
|
+
parsed = JSON.parse(textContent);
|
|
100
|
+
} catch {
|
|
101
|
+
throw new Error(`Failed to parse structured output as JSON. Content: ${textContent.slice(0, 200)}${textContent.length > 200 ? "..." : ""}`);
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
data: transformNullsToUndefined(parsed),
|
|
105
|
+
rawText: textContent
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Processes streaming chunks from the Mistral API and yields AG-UI stream events.
|
|
110
|
+
*/
|
|
111
|
+
async *processMistralStreamChunks(stream, options, aguiState) {
|
|
112
|
+
let accumulatedContent = "";
|
|
113
|
+
const timestamp = aguiState.timestamp;
|
|
114
|
+
let hasEmittedTextMessageStart = false;
|
|
115
|
+
let hasEmittedTextMessageEnd = false;
|
|
116
|
+
let hasEmittedToolCall = false;
|
|
117
|
+
let hasEmittedRunFinished = false;
|
|
118
|
+
let lastChunkModel = options.model;
|
|
119
|
+
let reasoningMessageId = null;
|
|
120
|
+
let hasClosedReasoning = false;
|
|
121
|
+
const toolCallsInProgress = /* @__PURE__ */ new Map();
|
|
122
|
+
try {
|
|
123
|
+
for await (const chunk of stream) {
|
|
124
|
+
lastChunkModel = chunk.model || options.model;
|
|
125
|
+
const choice = chunk.choices?.[0];
|
|
126
|
+
if (!choice) continue;
|
|
127
|
+
const chunkModel = chunk.model || options.model;
|
|
128
|
+
if (!aguiState.hasEmittedRunStarted) {
|
|
129
|
+
aguiState.hasEmittedRunStarted = true;
|
|
130
|
+
yield asChunk({
|
|
131
|
+
type: "RUN_STARTED",
|
|
132
|
+
runId: aguiState.runId,
|
|
133
|
+
threadId: aguiState.threadId,
|
|
134
|
+
model: chunkModel,
|
|
135
|
+
timestamp
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
const delta = choice.delta;
|
|
139
|
+
const { text: deltaContent, thinking: deltaThinkingFromContent } = this.extractDeltaParts(delta?.content);
|
|
140
|
+
const deltaThinking = deltaThinkingFromContent + (typeof delta?.reasoning_content === "string" ? delta.reasoning_content : "");
|
|
141
|
+
const deltaToolCalls = delta?.tool_calls;
|
|
142
|
+
if (deltaThinking) {
|
|
143
|
+
if (reasoningMessageId === null) {
|
|
144
|
+
reasoningMessageId = generateId(this.name);
|
|
145
|
+
yield asChunk({
|
|
146
|
+
type: "REASONING_START",
|
|
147
|
+
messageId: reasoningMessageId,
|
|
148
|
+
model: chunkModel,
|
|
149
|
+
timestamp
|
|
150
|
+
});
|
|
151
|
+
yield asChunk({
|
|
152
|
+
type: "REASONING_MESSAGE_START",
|
|
153
|
+
messageId: reasoningMessageId,
|
|
154
|
+
role: "reasoning",
|
|
155
|
+
model: chunkModel,
|
|
156
|
+
timestamp
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
yield asChunk({
|
|
160
|
+
type: "REASONING_MESSAGE_CONTENT",
|
|
161
|
+
messageId: reasoningMessageId,
|
|
162
|
+
model: chunkModel,
|
|
163
|
+
timestamp,
|
|
164
|
+
delta: deltaThinking
|
|
165
|
+
});
|
|
166
|
+
}
|
|
167
|
+
const aboutToEmitOutput = !!deltaContent || !!deltaToolCalls && deltaToolCalls.length > 0;
|
|
168
|
+
if (reasoningMessageId !== null && !hasClosedReasoning && aboutToEmitOutput) {
|
|
169
|
+
hasClosedReasoning = true;
|
|
170
|
+
yield asChunk({
|
|
171
|
+
type: "REASONING_MESSAGE_END",
|
|
172
|
+
messageId: reasoningMessageId,
|
|
173
|
+
model: chunkModel,
|
|
174
|
+
timestamp
|
|
175
|
+
});
|
|
176
|
+
yield asChunk({
|
|
177
|
+
type: "REASONING_END",
|
|
178
|
+
messageId: reasoningMessageId,
|
|
179
|
+
model: chunkModel,
|
|
180
|
+
timestamp
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
if (deltaContent) {
|
|
184
|
+
if (!hasEmittedTextMessageStart) {
|
|
185
|
+
hasEmittedTextMessageStart = true;
|
|
186
|
+
yield asChunk({
|
|
187
|
+
type: "TEXT_MESSAGE_START",
|
|
188
|
+
messageId: aguiState.messageId,
|
|
189
|
+
model: chunkModel,
|
|
190
|
+
timestamp,
|
|
191
|
+
role: "assistant"
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
accumulatedContent += deltaContent;
|
|
195
|
+
yield asChunk({
|
|
196
|
+
type: "TEXT_MESSAGE_CONTENT",
|
|
197
|
+
messageId: aguiState.messageId,
|
|
198
|
+
model: chunkModel,
|
|
199
|
+
timestamp,
|
|
200
|
+
delta: deltaContent,
|
|
201
|
+
content: accumulatedContent
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
if (deltaToolCalls) for (const [i, toolCallDelta] of deltaToolCalls.entries()) {
|
|
205
|
+
const index = toolCallDelta.index ?? i;
|
|
206
|
+
let toolCall = toolCallsInProgress.get(index);
|
|
207
|
+
if (!toolCall) {
|
|
208
|
+
toolCall = {
|
|
209
|
+
id: toolCallDelta.id || "",
|
|
210
|
+
name: toolCallDelta.function?.name || "",
|
|
211
|
+
arguments: "",
|
|
212
|
+
started: false,
|
|
213
|
+
ended: false
|
|
214
|
+
};
|
|
215
|
+
toolCallsInProgress.set(index, toolCall);
|
|
216
|
+
}
|
|
217
|
+
if (toolCallDelta.id) toolCall.id = toolCallDelta.id;
|
|
218
|
+
if (toolCallDelta.function?.name) toolCall.name = toolCallDelta.function.name;
|
|
219
|
+
const rawArgs = toolCallDelta.function?.arguments;
|
|
220
|
+
const argsDelta = rawArgs === void 0 ? void 0 : typeof rawArgs === "string" ? rawArgs : JSON.stringify(rawArgs);
|
|
221
|
+
if (argsDelta !== void 0) toolCall.arguments += argsDelta;
|
|
222
|
+
if (!!toolCall.id && !!toolCall.name && !toolCall.started) {
|
|
223
|
+
toolCall.started = true;
|
|
224
|
+
yield asChunk({
|
|
225
|
+
type: "TOOL_CALL_START",
|
|
226
|
+
toolCallId: toolCall.id,
|
|
227
|
+
toolCallName: toolCall.name,
|
|
228
|
+
toolName: toolCall.name,
|
|
229
|
+
model: chunkModel,
|
|
230
|
+
timestamp,
|
|
231
|
+
index
|
|
232
|
+
});
|
|
233
|
+
if (toolCall.arguments.length > 0) yield asChunk({
|
|
234
|
+
type: "TOOL_CALL_ARGS",
|
|
235
|
+
toolCallId: toolCall.id,
|
|
236
|
+
model: chunkModel,
|
|
237
|
+
timestamp,
|
|
238
|
+
delta: toolCall.arguments
|
|
239
|
+
});
|
|
240
|
+
} else if (argsDelta !== void 0 && toolCall.started) yield asChunk({
|
|
241
|
+
type: "TOOL_CALL_ARGS",
|
|
242
|
+
toolCallId: toolCall.id,
|
|
243
|
+
model: chunkModel,
|
|
244
|
+
timestamp,
|
|
245
|
+
delta: argsDelta
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
const finishReason = choice.finish_reason;
|
|
249
|
+
if (finishReason) {
|
|
250
|
+
if (finishReason === "tool_calls" || toolCallsInProgress.size > 0) for (const [, toolCall] of toolCallsInProgress) {
|
|
251
|
+
if (!toolCall.started || !toolCall.id || !toolCall.name || toolCall.ended) continue;
|
|
252
|
+
const parsedInput = parseToolCallInput(toolCall);
|
|
253
|
+
toolCall.ended = true;
|
|
254
|
+
hasEmittedToolCall = true;
|
|
255
|
+
yield asChunk({
|
|
256
|
+
type: "TOOL_CALL_END",
|
|
257
|
+
toolCallId: toolCall.id,
|
|
258
|
+
toolCallName: toolCall.name,
|
|
259
|
+
toolName: toolCall.name,
|
|
260
|
+
model: chunkModel,
|
|
261
|
+
timestamp,
|
|
262
|
+
input: parsedInput
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
const computedFinishReason = finishReason === "tool_calls" || hasEmittedToolCall ? "tool_calls" : finishReason === "length" ? "length" : "stop";
|
|
266
|
+
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
267
|
+
hasClosedReasoning = true;
|
|
268
|
+
yield asChunk({
|
|
269
|
+
type: "REASONING_MESSAGE_END",
|
|
270
|
+
messageId: reasoningMessageId,
|
|
271
|
+
model: chunkModel,
|
|
272
|
+
timestamp
|
|
273
|
+
});
|
|
274
|
+
yield asChunk({
|
|
275
|
+
type: "REASONING_END",
|
|
276
|
+
messageId: reasoningMessageId,
|
|
277
|
+
model: chunkModel,
|
|
278
|
+
timestamp
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
282
|
+
hasEmittedTextMessageEnd = true;
|
|
283
|
+
yield asChunk({
|
|
284
|
+
type: "TEXT_MESSAGE_END",
|
|
285
|
+
messageId: aguiState.messageId,
|
|
286
|
+
model: chunkModel,
|
|
287
|
+
timestamp
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
const usage = chunk.usage;
|
|
291
|
+
hasEmittedRunFinished = true;
|
|
292
|
+
yield asChunk({
|
|
293
|
+
type: "RUN_FINISHED",
|
|
294
|
+
runId: aguiState.runId,
|
|
295
|
+
threadId: aguiState.threadId,
|
|
296
|
+
model: chunkModel,
|
|
297
|
+
timestamp,
|
|
298
|
+
usage: usage ? {
|
|
299
|
+
promptTokens: usage.prompt_tokens || 0,
|
|
300
|
+
completionTokens: usage.completion_tokens || 0,
|
|
301
|
+
totalTokens: usage.total_tokens || 0
|
|
302
|
+
} : void 0,
|
|
303
|
+
finishReason: computedFinishReason
|
|
304
|
+
});
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
if (!hasEmittedRunFinished) {
|
|
308
|
+
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
309
|
+
hasClosedReasoning = true;
|
|
310
|
+
yield asChunk({
|
|
311
|
+
type: "REASONING_MESSAGE_END",
|
|
312
|
+
messageId: reasoningMessageId,
|
|
313
|
+
model: lastChunkModel,
|
|
314
|
+
timestamp
|
|
315
|
+
});
|
|
316
|
+
yield asChunk({
|
|
317
|
+
type: "REASONING_END",
|
|
318
|
+
messageId: reasoningMessageId,
|
|
319
|
+
model: lastChunkModel,
|
|
320
|
+
timestamp
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
for (const [, toolCall] of toolCallsInProgress) if (toolCall.started && !toolCall.ended) {
|
|
324
|
+
toolCall.ended = true;
|
|
325
|
+
hasEmittedToolCall = true;
|
|
326
|
+
yield asChunk({
|
|
327
|
+
type: "TOOL_CALL_END",
|
|
328
|
+
toolCallId: toolCall.id,
|
|
329
|
+
toolCallName: toolCall.name,
|
|
330
|
+
toolName: toolCall.name,
|
|
331
|
+
model: lastChunkModel,
|
|
332
|
+
timestamp,
|
|
333
|
+
input: parseToolCallInput(toolCall)
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
337
|
+
hasEmittedTextMessageEnd = true;
|
|
338
|
+
yield asChunk({
|
|
339
|
+
type: "TEXT_MESSAGE_END",
|
|
340
|
+
messageId: aguiState.messageId,
|
|
341
|
+
model: lastChunkModel,
|
|
342
|
+
timestamp
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
hasEmittedRunFinished = true;
|
|
346
|
+
yield asChunk({
|
|
347
|
+
type: "RUN_FINISHED",
|
|
348
|
+
runId: aguiState.runId,
|
|
349
|
+
threadId: aguiState.threadId,
|
|
350
|
+
model: lastChunkModel,
|
|
351
|
+
timestamp,
|
|
352
|
+
usage: void 0,
|
|
353
|
+
finishReason: hasEmittedToolCall ? "tool_calls" : "stop"
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
} catch (error) {
|
|
357
|
+
if (reasoningMessageId !== null && !hasClosedReasoning) {
|
|
358
|
+
hasClosedReasoning = true;
|
|
359
|
+
yield asChunk({
|
|
360
|
+
type: "REASONING_MESSAGE_END",
|
|
361
|
+
messageId: reasoningMessageId,
|
|
362
|
+
model: lastChunkModel,
|
|
363
|
+
timestamp
|
|
364
|
+
});
|
|
365
|
+
yield asChunk({
|
|
366
|
+
type: "REASONING_END",
|
|
367
|
+
messageId: reasoningMessageId,
|
|
368
|
+
model: lastChunkModel,
|
|
369
|
+
timestamp
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
if (hasEmittedTextMessageStart && !hasEmittedTextMessageEnd) {
|
|
373
|
+
hasEmittedTextMessageEnd = true;
|
|
374
|
+
yield asChunk({
|
|
375
|
+
type: "TEXT_MESSAGE_END",
|
|
376
|
+
messageId: aguiState.messageId,
|
|
377
|
+
model: lastChunkModel,
|
|
378
|
+
timestamp
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
for (const [, toolCall] of toolCallsInProgress) if (toolCall.started && !toolCall.ended) {
|
|
382
|
+
toolCall.ended = true;
|
|
383
|
+
let partialInput = {};
|
|
384
|
+
try {
|
|
385
|
+
partialInput = toolCall.arguments ? transformNullsToUndefined(JSON.parse(toolCall.arguments)) : {};
|
|
386
|
+
} catch {
|
|
387
|
+
partialInput = {};
|
|
388
|
+
}
|
|
389
|
+
yield asChunk({
|
|
390
|
+
type: "TOOL_CALL_END",
|
|
391
|
+
toolCallId: toolCall.id,
|
|
392
|
+
toolCallName: toolCall.name,
|
|
393
|
+
toolName: toolCall.name,
|
|
394
|
+
model: lastChunkModel,
|
|
395
|
+
timestamp,
|
|
396
|
+
input: partialInput
|
|
397
|
+
});
|
|
398
|
+
}
|
|
399
|
+
throw error;
|
|
400
|
+
}
|
|
401
|
+
}
|
|
402
|
+
/**
|
|
403
|
+
* Makes a raw fetch request to the Mistral chat completions endpoint and
|
|
404
|
+
* parses the SSE stream manually, bypassing the SDK's Zod validation which
|
|
405
|
+
* rejects streaming tool call chunks that omit `name` in argument deltas.
|
|
406
|
+
*/
|
|
407
|
+
async *fetchRawMistralStream(params, config) {
|
|
408
|
+
const url = `${(config.serverURL ?? "https://api.mistral.ai").replace(/\/+$/, "").replace(/\/v1$/, "")}/v1/chat/completions`;
|
|
409
|
+
const body = this.toWireBody(params);
|
|
410
|
+
const headers = {
|
|
411
|
+
"Content-Type": "application/json",
|
|
412
|
+
Authorization: `Bearer ${config.apiKey}`,
|
|
413
|
+
...config.defaultHeaders
|
|
414
|
+
};
|
|
415
|
+
const response = await fetch(url, {
|
|
416
|
+
method: "POST",
|
|
417
|
+
headers,
|
|
418
|
+
body: JSON.stringify(body)
|
|
419
|
+
});
|
|
420
|
+
if (!response.ok) {
|
|
421
|
+
const errorText = await response.text();
|
|
422
|
+
throw new Error(`Mistral API error ${response.status}: ${errorText}`);
|
|
423
|
+
}
|
|
424
|
+
if (!response.body) throw new Error("Mistral API returned a response with no body. This may indicate a proxy or runtime that does not support streaming.");
|
|
425
|
+
const reader = response.body.getReader();
|
|
426
|
+
const decoder = new TextDecoder();
|
|
427
|
+
let buffer = "";
|
|
428
|
+
try {
|
|
429
|
+
for (;;) {
|
|
430
|
+
const { done, value } = await reader.read();
|
|
431
|
+
if (done) break;
|
|
432
|
+
buffer += decoder.decode(value, { stream: true });
|
|
433
|
+
const lines = buffer.split("\n");
|
|
434
|
+
buffer = lines.pop() ?? "";
|
|
435
|
+
for (const line of lines) {
|
|
436
|
+
const trimmed = line.trim();
|
|
437
|
+
if (!trimmed.startsWith("data:")) continue;
|
|
438
|
+
const data = trimmed.slice(5).trimStart();
|
|
439
|
+
if (data === "[DONE]") return;
|
|
440
|
+
let parsed;
|
|
441
|
+
try {
|
|
442
|
+
parsed = JSON.parse(data);
|
|
443
|
+
} catch (e) {
|
|
444
|
+
if (e instanceof SyntaxError) {
|
|
445
|
+
console.warn(`[mistral] skipped unparseable SSE chunk: ${data.slice(0, 200)}`);
|
|
446
|
+
continue;
|
|
447
|
+
}
|
|
448
|
+
throw e;
|
|
449
|
+
}
|
|
450
|
+
if (parsed && typeof parsed === "object" && "error" in parsed && !("choices" in parsed)) {
|
|
451
|
+
const errPayload = parsed.error;
|
|
452
|
+
const message = typeof errPayload === "string" ? errPayload : errPayload && typeof errPayload === "object" && "message" in errPayload ? String(errPayload.message) : JSON.stringify(errPayload);
|
|
453
|
+
throw new Error(`Mistral stream error: ${message}`);
|
|
454
|
+
}
|
|
455
|
+
yield parsed;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
} finally {
|
|
459
|
+
await reader.cancel().catch(() => {});
|
|
460
|
+
reader.releaseLock();
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
/**
|
|
464
|
+
* Converts the SDK's camelCase `ChatCompletionStreamRequest` into the
|
|
465
|
+
* snake_case wire body, including converting messages.
|
|
466
|
+
*/
|
|
467
|
+
toWireBody(params) {
|
|
468
|
+
const { messages, maxTokens, topP, randomSeed, responseFormat, toolChoice, parallelToolCalls, frequencyPenalty, presencePenalty, safePrompt, stream: _stream, ...rest } = params;
|
|
469
|
+
return {
|
|
470
|
+
...rest,
|
|
471
|
+
messages: messages.map(messageToWire),
|
|
472
|
+
stream: true,
|
|
473
|
+
stream_options: { include_usage: true },
|
|
474
|
+
...maxTokens != null && { max_tokens: maxTokens },
|
|
475
|
+
...topP != null && { top_p: topP },
|
|
476
|
+
...randomSeed != null && { random_seed: randomSeed },
|
|
477
|
+
...responseFormat != null && { response_format: responseFormat },
|
|
478
|
+
...toolChoice != null && { tool_choice: toolChoice },
|
|
479
|
+
...parallelToolCalls != null && { parallel_tool_calls: parallelToolCalls },
|
|
480
|
+
...frequencyPenalty != null && { frequency_penalty: frequencyPenalty },
|
|
481
|
+
...presencePenalty != null && { presence_penalty: presencePenalty },
|
|
482
|
+
...safePrompt != null && { safe_prompt: safePrompt }
|
|
483
|
+
};
|
|
484
|
+
}
|
|
485
|
+
/**
|
|
486
|
+
* Splits a Mistral delta content payload into text and reasoning deltas.
|
|
487
|
+
* Mistral reasoning models (magistral-*) stream reasoning content as
|
|
488
|
+
* `{ type: 'thinking', thinking: [{ type: 'text', text }, ...] }` content
|
|
489
|
+
* parts. A single delta may contain text only, thinking only, or — rarely —
|
|
490
|
+
* both (when a step transitions); both fields are returned so the caller
|
|
491
|
+
* can sequence REASONING and TEXT lifecycle events in order.
|
|
492
|
+
*/
|
|
493
|
+
extractDeltaParts(content) {
|
|
494
|
+
if (!content) return {
|
|
495
|
+
text: "",
|
|
496
|
+
thinking: ""
|
|
497
|
+
};
|
|
498
|
+
if (typeof content === "string") return {
|
|
499
|
+
text: content,
|
|
500
|
+
thinking: ""
|
|
501
|
+
};
|
|
502
|
+
let text = "";
|
|
503
|
+
let thinking = "";
|
|
504
|
+
for (const part of content) if (part.type === "text" && typeof part.text === "string") text += part.text;
|
|
505
|
+
else if (part.type === "thinking" && Array.isArray(part.thinking)) {
|
|
506
|
+
for (const inner of part.thinking) if (inner.type === "text" && typeof inner.text === "string") thinking += inner.text;
|
|
507
|
+
}
|
|
508
|
+
return {
|
|
509
|
+
text,
|
|
510
|
+
thinking
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
/**
|
|
514
|
+
* Maps common TextOptions to Mistral Chat Completions request parameters.
|
|
515
|
+
*/
|
|
516
|
+
mapTextOptionsToMistral(options) {
|
|
517
|
+
const modelOptions = options.modelOptions;
|
|
518
|
+
const tools = options.tools ? convertToolsToProviderFormat(options.tools) : void 0;
|
|
519
|
+
const messages = [];
|
|
520
|
+
if (options.systemPrompts && options.systemPrompts.length > 0) messages.push({
|
|
521
|
+
role: "system",
|
|
522
|
+
content: options.systemPrompts.join("\n")
|
|
523
|
+
});
|
|
524
|
+
for (const message of options.messages) messages.push(this.convertMessageToMistral(message));
|
|
525
|
+
return {
|
|
526
|
+
model: options.model,
|
|
527
|
+
messages,
|
|
528
|
+
temperature: modelOptions?.temperature ?? void 0,
|
|
529
|
+
maxTokens: modelOptions?.max_tokens ?? void 0,
|
|
530
|
+
topP: modelOptions?.top_p ?? void 0,
|
|
531
|
+
tools,
|
|
532
|
+
stream: true,
|
|
533
|
+
...modelOptions && {
|
|
534
|
+
...modelOptions.stop != null && { stop: modelOptions.stop },
|
|
535
|
+
...modelOptions.random_seed != null && { randomSeed: modelOptions.random_seed },
|
|
536
|
+
...modelOptions.response_format != null && { responseFormat: modelOptions.response_format },
|
|
537
|
+
...modelOptions.tool_choice != null && { toolChoice: modelOptions.tool_choice },
|
|
538
|
+
...modelOptions.parallel_tool_calls != null && { parallelToolCalls: modelOptions.parallel_tool_calls },
|
|
539
|
+
...modelOptions.frequency_penalty != null && { frequencyPenalty: modelOptions.frequency_penalty },
|
|
540
|
+
...modelOptions.presence_penalty != null && { presencePenalty: modelOptions.presence_penalty },
|
|
541
|
+
...modelOptions.n != null && { n: modelOptions.n },
|
|
542
|
+
...modelOptions.prediction != null && { prediction: modelOptions.prediction },
|
|
543
|
+
...modelOptions.safe_prompt != null && { safePrompt: modelOptions.safe_prompt }
|
|
544
|
+
}
|
|
545
|
+
};
|
|
546
|
+
}
|
|
547
|
+
/**
|
|
548
|
+
* Converts a TanStack AI ModelMessage to a Mistral ChatCompletionMessageParam.
|
|
549
|
+
*/
|
|
550
|
+
convertMessageToMistral(message) {
|
|
551
|
+
if (message.role === "tool") {
|
|
552
|
+
if (!message.toolCallId) throw new Error("Missing toolCallId for tool message");
|
|
553
|
+
return {
|
|
554
|
+
role: "tool",
|
|
555
|
+
toolCallId: message.toolCallId,
|
|
556
|
+
content: typeof message.content === "string" ? message.content : JSON.stringify(message.content)
|
|
557
|
+
};
|
|
558
|
+
}
|
|
559
|
+
if (message.role === "assistant") {
|
|
560
|
+
const toolCalls = message.toolCalls?.map((tc) => ({
|
|
561
|
+
id: tc.id,
|
|
562
|
+
type: "function",
|
|
563
|
+
function: {
|
|
564
|
+
name: tc.function.name,
|
|
565
|
+
arguments: typeof tc.function.arguments === "string" ? tc.function.arguments : JSON.stringify(tc.function.arguments)
|
|
566
|
+
}
|
|
567
|
+
}));
|
|
568
|
+
return {
|
|
569
|
+
role: "assistant",
|
|
570
|
+
content: this.extractTextContent(message.content),
|
|
571
|
+
...toolCalls && toolCalls.length > 0 ? { toolCalls } : {}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
const contentParts = this.normalizeContent(message.content);
|
|
575
|
+
if (contentParts.length === 1 && contentParts[0]?.type === "text") return {
|
|
576
|
+
role: "user",
|
|
577
|
+
content: contentParts[0].content
|
|
578
|
+
};
|
|
579
|
+
const parts = contentParts.map((part) => this.convertContentPartToMistral(part));
|
|
580
|
+
return {
|
|
581
|
+
role: "user",
|
|
582
|
+
content: parts.length > 0 ? parts : ""
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* Converts a ContentPart to a Mistral content part. Returns undefined for
|
|
587
|
+
* unsupported part types.
|
|
588
|
+
*/
|
|
589
|
+
convertContentPartToMistral(part) {
|
|
590
|
+
if (part.type === "text") return {
|
|
591
|
+
type: "text",
|
|
592
|
+
text: part.content
|
|
593
|
+
};
|
|
594
|
+
if (part.type === "image") {
|
|
595
|
+
const imageMetadata = part.metadata;
|
|
596
|
+
const imageValue = part.source.value;
|
|
597
|
+
const imageUrl = part.source.type === "data" && !imageValue.startsWith("data:") ? `data:${part.source.mimeType};base64,${imageValue}` : imageValue;
|
|
598
|
+
return {
|
|
599
|
+
type: "image_url",
|
|
600
|
+
imageUrl: imageMetadata?.detail ? {
|
|
601
|
+
url: imageUrl,
|
|
602
|
+
detail: imageMetadata.detail
|
|
603
|
+
} : imageUrl
|
|
604
|
+
};
|
|
605
|
+
}
|
|
606
|
+
throw new Error(`Mistral text adapter does not support content part of type '${part.type}'. Supported types: text, image. Use a vision-capable model (pixtral-large-latest, pixtral-12b-2409, mistral-medium-latest, or mistral-small-latest) for images.`);
|
|
607
|
+
}
|
|
608
|
+
/**
|
|
609
|
+
* Normalizes message content to an array of ContentPart.
|
|
610
|
+
*/
|
|
611
|
+
normalizeContent(content) {
|
|
612
|
+
if (content === null) return [];
|
|
613
|
+
if (typeof content === "string") return [{
|
|
614
|
+
type: "text",
|
|
615
|
+
content
|
|
616
|
+
}];
|
|
617
|
+
return content;
|
|
618
|
+
}
|
|
619
|
+
/**
|
|
620
|
+
* Extracts text content from a content value that may be string, null, or ContentPart array.
|
|
621
|
+
*/
|
|
622
|
+
extractTextContent(content) {
|
|
623
|
+
if (content === null) return "";
|
|
624
|
+
if (typeof content === "string") return content;
|
|
625
|
+
return content.filter((p) => p.type === "text").map((p) => p.content).join("");
|
|
626
|
+
}
|
|
627
|
+
};
|
|
628
|
+
/**
|
|
629
|
+
* Snake-cases a Mistral SDK message into the wire format expected by the API.
|
|
630
|
+
*/
|
|
689
631
|
function messageToWire(msg) {
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
})
|
|
725
|
-
};
|
|
726
|
-
}
|
|
727
|
-
return msg;
|
|
632
|
+
if (msg.role === "tool") return {
|
|
633
|
+
role: "tool",
|
|
634
|
+
tool_call_id: msg.toolCallId,
|
|
635
|
+
content: msg.content,
|
|
636
|
+
...msg.name !== void 0 ? { name: msg.name } : {}
|
|
637
|
+
};
|
|
638
|
+
if (msg.role === "assistant") {
|
|
639
|
+
const base = {
|
|
640
|
+
role: "assistant",
|
|
641
|
+
content: msg.content ?? null
|
|
642
|
+
};
|
|
643
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) base.tool_calls = msg.toolCalls.map((tc) => ({
|
|
644
|
+
id: tc.id,
|
|
645
|
+
type: tc.type ?? "function",
|
|
646
|
+
function: tc.function
|
|
647
|
+
}));
|
|
648
|
+
if (msg.prefix !== void 0) base.prefix = msg.prefix;
|
|
649
|
+
return base;
|
|
650
|
+
}
|
|
651
|
+
if (msg.role === "user" && Array.isArray(msg.content)) return {
|
|
652
|
+
role: "user",
|
|
653
|
+
content: msg.content.map((part) => {
|
|
654
|
+
if (part.type === "image_url") return {
|
|
655
|
+
type: "image_url",
|
|
656
|
+
image_url: part.imageUrl
|
|
657
|
+
};
|
|
658
|
+
if (part.type === "document_url") return {
|
|
659
|
+
type: "document_url",
|
|
660
|
+
document_url: part.documentUrl
|
|
661
|
+
};
|
|
662
|
+
return part;
|
|
663
|
+
})
|
|
664
|
+
};
|
|
665
|
+
return msg;
|
|
728
666
|
}
|
|
667
|
+
/**
|
|
668
|
+
* Creates a Mistral text adapter with explicit API key.
|
|
669
|
+
*
|
|
670
|
+
* @param model - The model name (e.g., 'mistral-large-latest')
|
|
671
|
+
* @param apiKey - Your Mistral API key
|
|
672
|
+
* @param config - Optional additional configuration
|
|
673
|
+
* @returns Configured Mistral text adapter instance
|
|
674
|
+
*
|
|
675
|
+
* @example
|
|
676
|
+
* ```typescript
|
|
677
|
+
* const adapter = createMistralText('mistral-large-latest', 'api_key');
|
|
678
|
+
* ```
|
|
679
|
+
*/
|
|
729
680
|
function createMistralText(model, apiKey, config) {
|
|
730
|
-
|
|
681
|
+
return new MistralTextAdapter({
|
|
682
|
+
apiKey,
|
|
683
|
+
...config
|
|
684
|
+
}, model);
|
|
731
685
|
}
|
|
686
|
+
/**
|
|
687
|
+
* Creates a Mistral text adapter using the `MISTRAL_API_KEY` environment variable.
|
|
688
|
+
*
|
|
689
|
+
* @param model - The model name (e.g., 'mistral-large-latest')
|
|
690
|
+
* @param config - Optional configuration (excluding apiKey)
|
|
691
|
+
* @returns Configured Mistral text adapter instance
|
|
692
|
+
* @throws Error if MISTRAL_API_KEY is not found in environment
|
|
693
|
+
*
|
|
694
|
+
* @example
|
|
695
|
+
* ```typescript
|
|
696
|
+
* const adapter = mistralText('mistral-large-latest');
|
|
697
|
+
* ```
|
|
698
|
+
*/
|
|
732
699
|
function mistralText(model, config) {
|
|
733
|
-
|
|
734
|
-
return createMistralText(model, apiKey, config);
|
|
700
|
+
return createMistralText(model, getMistralApiKeyFromEnv(), config);
|
|
735
701
|
}
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
};
|
|
741
|
-
//# sourceMappingURL=text.js.map
|
|
702
|
+
//#endregion
|
|
703
|
+
export { MistralTextAdapter, createMistralText, mistralText };
|
|
704
|
+
|
|
705
|
+
//# sourceMappingURL=text.js.map
|