@tanstack/ai-mistral 0.1.0
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/README.md +93 -0
- package/dist/esm/adapters/text.d.ts +106 -0
- package/dist/esm/adapters/text.js +741 -0
- package/dist/esm/adapters/text.js.map +1 -0
- package/dist/esm/index.d.ts +10 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/message-types.d.ts +155 -0
- package/dist/esm/model-meta.d.ts +263 -0
- package/dist/esm/model-meta.js +50 -0
- package/dist/esm/model-meta.js.map +1 -0
- package/dist/esm/text/text-provider-options.d.ts +80 -0
- package/dist/esm/tools/function-tool.d.ts +13 -0
- package/dist/esm/tools/function-tool.js +26 -0
- package/dist/esm/tools/function-tool.js.map +1 -0
- package/dist/esm/tools/index.d.ts +2 -0
- package/dist/esm/tools/tool-converter.d.ts +6 -0
- package/dist/esm/tools/tool-converter.js +10 -0
- package/dist/esm/tools/tool-converter.js.map +1 -0
- package/dist/esm/utils/client.d.ts +24 -0
- package/dist/esm/utils/client.js +44 -0
- package/dist/esm/utils/client.js.map +1 -0
- package/dist/esm/utils/index.d.ts +2 -0
- package/dist/esm/utils/schema-converter.d.ts +18 -0
- package/dist/esm/utils/schema-converter.js +93 -0
- package/dist/esm/utils/schema-converter.js.map +1 -0
- package/package.json +60 -0
- package/src/adapters/text.ts +1141 -0
- package/src/index.ts +33 -0
- package/src/message-types.ts +194 -0
- package/src/model-meta.ts +285 -0
- package/src/text/text-provider-options.ts +103 -0
- package/src/tools/function-tool.ts +42 -0
- package/src/tools/index.ts +5 -0
- package/src/tools/tool-converter.ts +14 -0
- package/src/utils/client.ts +70 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/schema-converter.ts +136 -0
|
@@ -0,0 +1,741 @@
|
|
|
1
|
+
import { BaseTextAdapter } from "@tanstack/ai/adapters";
|
|
2
|
+
import { makeMistralStructuredOutputCompatible, transformNullsToUndefined } from "../utils/schema-converter.js";
|
|
3
|
+
import { convertToolsToProviderFormat } from "../tools/tool-converter.js";
|
|
4
|
+
import { createMistralClient, generateId, getMistralApiKeyFromEnv } from "../utils/client.js";
|
|
5
|
+
const asChunk = (chunk) => (
|
|
6
|
+
// eslint-disable-next-line no-restricted-syntax -- Record<string, unknown> doesn't structurally overlap the StreamChunk discriminated union; events are built with literal `type` fields the union accepts at runtime
|
|
7
|
+
chunk
|
|
8
|
+
);
|
|
9
|
+
function parseToolCallInput(toolCall) {
|
|
10
|
+
if (!toolCall.arguments) return {};
|
|
11
|
+
try {
|
|
12
|
+
return transformNullsToUndefined(JSON.parse(toolCall.arguments));
|
|
13
|
+
} catch (cause) {
|
|
14
|
+
const preview = toolCall.arguments.slice(0, 200);
|
|
15
|
+
const ellipsis = toolCall.arguments.length > 200 ? "..." : "";
|
|
16
|
+
throw new Error(
|
|
17
|
+
`Failed to parse tool call arguments for tool '${toolCall.name}' (id: ${toolCall.id}). Arguments: ${preview}${ellipsis}`,
|
|
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
|
+
}
|
|
688
|
+
}
|
|
689
|
+
function messageToWire(msg) {
|
|
690
|
+
if (msg.role === "tool") {
|
|
691
|
+
return {
|
|
692
|
+
role: "tool",
|
|
693
|
+
tool_call_id: msg.toolCallId,
|
|
694
|
+
content: msg.content,
|
|
695
|
+
...msg.name !== void 0 ? { name: msg.name } : {}
|
|
696
|
+
};
|
|
697
|
+
}
|
|
698
|
+
if (msg.role === "assistant") {
|
|
699
|
+
const base = {
|
|
700
|
+
role: "assistant",
|
|
701
|
+
content: msg.content ?? null
|
|
702
|
+
};
|
|
703
|
+
if (msg.toolCalls && msg.toolCalls.length > 0) {
|
|
704
|
+
base.tool_calls = msg.toolCalls.map((tc) => ({
|
|
705
|
+
id: tc.id,
|
|
706
|
+
type: tc.type ?? "function",
|
|
707
|
+
function: tc.function
|
|
708
|
+
}));
|
|
709
|
+
}
|
|
710
|
+
if (msg.prefix !== void 0) base.prefix = msg.prefix;
|
|
711
|
+
return base;
|
|
712
|
+
}
|
|
713
|
+
if (msg.role === "user" && Array.isArray(msg.content)) {
|
|
714
|
+
return {
|
|
715
|
+
role: "user",
|
|
716
|
+
content: msg.content.map((part) => {
|
|
717
|
+
if (part.type === "image_url") {
|
|
718
|
+
return { type: "image_url", image_url: part.imageUrl };
|
|
719
|
+
}
|
|
720
|
+
if (part.type === "document_url") {
|
|
721
|
+
return { type: "document_url", document_url: part.documentUrl };
|
|
722
|
+
}
|
|
723
|
+
return part;
|
|
724
|
+
})
|
|
725
|
+
};
|
|
726
|
+
}
|
|
727
|
+
return msg;
|
|
728
|
+
}
|
|
729
|
+
function createMistralText(model, apiKey, config) {
|
|
730
|
+
return new MistralTextAdapter({ apiKey, ...config }, model);
|
|
731
|
+
}
|
|
732
|
+
function mistralText(model, config) {
|
|
733
|
+
const apiKey = getMistralApiKeyFromEnv();
|
|
734
|
+
return createMistralText(model, apiKey, config);
|
|
735
|
+
}
|
|
736
|
+
export {
|
|
737
|
+
MistralTextAdapter,
|
|
738
|
+
createMistralText,
|
|
739
|
+
mistralText
|
|
740
|
+
};
|
|
741
|
+
//# sourceMappingURL=text.js.map
|