@mariozechner/pi-ai 0.56.0 → 0.56.2
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 +4 -3
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/models.d.ts +1 -1
- package/dist/models.d.ts.map +1 -1
- package/dist/models.generated.d.ts +211 -35
- package/dist/models.generated.d.ts.map +1 -1
- package/dist/models.generated.js +255 -84
- package/dist/models.generated.js.map +1 -1
- package/dist/models.js +2 -2
- package/dist/models.js.map +1 -1
- package/dist/providers/google-gemini-cli.d.ts.map +1 -1
- package/dist/providers/google-gemini-cli.js +21 -10
- package/dist/providers/google-gemini-cli.js.map +1 -1
- package/dist/providers/google-shared.d.ts.map +1 -1
- package/dist/providers/google-shared.js +16 -22
- package/dist/providers/google-shared.js.map +1 -1
- package/dist/providers/mistral.d.ts +22 -0
- package/dist/providers/mistral.d.ts.map +1 -0
- package/dist/providers/mistral.js +495 -0
- package/dist/providers/mistral.js.map +1 -0
- package/dist/providers/openai-codex-responses.d.ts.map +1 -1
- package/dist/providers/openai-codex-responses.js +1 -1
- package/dist/providers/openai-codex-responses.js.map +1 -1
- package/dist/providers/openai-completions.d.ts.map +1 -1
- package/dist/providers/openai-completions.js +8 -33
- package/dist/providers/openai-completions.js.map +1 -1
- package/dist/providers/openai-responses-shared.d.ts.map +1 -1
- package/dist/providers/openai-responses-shared.js +30 -13
- package/dist/providers/openai-responses-shared.js.map +1 -1
- package/dist/providers/register-builtins.d.ts.map +1 -1
- package/dist/providers/register-builtins.js +6 -0
- package/dist/providers/register-builtins.js.map +1 -1
- package/dist/types.d.ts +6 -3
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/overflow.d.ts +1 -1
- package/dist/utils/overflow.d.ts.map +1 -1
- package/dist/utils/overflow.js +4 -3
- package/dist/utils/overflow.js.map +1 -1
- package/package.json +5 -7
- package/oauth.d.ts +0 -1
- package/oauth.js +0 -1
|
@@ -0,0 +1,495 @@
|
|
|
1
|
+
import { Mistral } from "@mistralai/mistralai";
|
|
2
|
+
import { getEnvApiKey } from "../env-api-keys.js";
|
|
3
|
+
import { calculateCost } from "../models.js";
|
|
4
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
|
+
import { shortHash } from "../utils/hash.js";
|
|
6
|
+
import { parseStreamingJson } from "../utils/json-parse.js";
|
|
7
|
+
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
8
|
+
import { buildBaseOptions, clampReasoning } from "./simple-options.js";
|
|
9
|
+
import { transformMessages } from "./transform-messages.js";
|
|
10
|
+
const MISTRAL_TOOL_CALL_ID_LENGTH = 9;
|
|
11
|
+
const MAX_MISTRAL_ERROR_BODY_CHARS = 4000;
|
|
12
|
+
/**
|
|
13
|
+
* Stream responses from Mistral using `chat.stream`.
|
|
14
|
+
*/
|
|
15
|
+
export const streamMistral = (model, context, options) => {
|
|
16
|
+
const stream = new AssistantMessageEventStream();
|
|
17
|
+
(async () => {
|
|
18
|
+
const output = createOutput(model);
|
|
19
|
+
try {
|
|
20
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
21
|
+
if (!apiKey) {
|
|
22
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
23
|
+
}
|
|
24
|
+
// Intentionally per-request: avoids shared SDK mutable state across concurrent consumers.
|
|
25
|
+
const mistral = new Mistral({
|
|
26
|
+
apiKey,
|
|
27
|
+
serverURL: model.baseUrl,
|
|
28
|
+
});
|
|
29
|
+
const normalizeMistralToolCallId = createMistralToolCallIdNormalizer();
|
|
30
|
+
const transformedMessages = transformMessages(context.messages, model, (id) => normalizeMistralToolCallId(id));
|
|
31
|
+
const payload = buildChatPayload(model, context, transformedMessages, options);
|
|
32
|
+
options?.onPayload?.(payload);
|
|
33
|
+
const mistralStream = await mistral.chat.stream(payload, buildRequestOptions(model, options));
|
|
34
|
+
stream.push({ type: "start", partial: output });
|
|
35
|
+
await consumeChatStream(model, output, stream, mistralStream);
|
|
36
|
+
if (options?.signal?.aborted) {
|
|
37
|
+
throw new Error("Request was aborted");
|
|
38
|
+
}
|
|
39
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
40
|
+
throw new Error("An unknown error occurred");
|
|
41
|
+
}
|
|
42
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
43
|
+
stream.end();
|
|
44
|
+
}
|
|
45
|
+
catch (error) {
|
|
46
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
47
|
+
output.errorMessage = formatMistralError(error);
|
|
48
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
49
|
+
stream.end();
|
|
50
|
+
}
|
|
51
|
+
})();
|
|
52
|
+
return stream;
|
|
53
|
+
};
|
|
54
|
+
/**
|
|
55
|
+
* Maps provider-agnostic `SimpleStreamOptions` to Mistral options.
|
|
56
|
+
*/
|
|
57
|
+
export const streamSimpleMistral = (model, context, options) => {
|
|
58
|
+
const apiKey = options?.apiKey || getEnvApiKey(model.provider);
|
|
59
|
+
if (!apiKey) {
|
|
60
|
+
throw new Error(`No API key for provider: ${model.provider}`);
|
|
61
|
+
}
|
|
62
|
+
const base = buildBaseOptions(model, options, apiKey);
|
|
63
|
+
const reasoning = clampReasoning(options?.reasoning);
|
|
64
|
+
return streamMistral(model, context, {
|
|
65
|
+
...base,
|
|
66
|
+
promptMode: model.reasoning && reasoning ? "reasoning" : undefined,
|
|
67
|
+
});
|
|
68
|
+
};
|
|
69
|
+
function createOutput(model) {
|
|
70
|
+
return {
|
|
71
|
+
role: "assistant",
|
|
72
|
+
content: [],
|
|
73
|
+
api: model.api,
|
|
74
|
+
provider: model.provider,
|
|
75
|
+
model: model.id,
|
|
76
|
+
usage: {
|
|
77
|
+
input: 0,
|
|
78
|
+
output: 0,
|
|
79
|
+
cacheRead: 0,
|
|
80
|
+
cacheWrite: 0,
|
|
81
|
+
totalTokens: 0,
|
|
82
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
83
|
+
},
|
|
84
|
+
stopReason: "stop",
|
|
85
|
+
timestamp: Date.now(),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
function createMistralToolCallIdNormalizer() {
|
|
89
|
+
const idMap = new Map();
|
|
90
|
+
const reverseMap = new Map();
|
|
91
|
+
return (id) => {
|
|
92
|
+
const existing = idMap.get(id);
|
|
93
|
+
if (existing)
|
|
94
|
+
return existing;
|
|
95
|
+
let attempt = 0;
|
|
96
|
+
while (true) {
|
|
97
|
+
const candidate = deriveMistralToolCallId(id, attempt);
|
|
98
|
+
const owner = reverseMap.get(candidate);
|
|
99
|
+
if (!owner || owner === id) {
|
|
100
|
+
idMap.set(id, candidate);
|
|
101
|
+
reverseMap.set(candidate, id);
|
|
102
|
+
return candidate;
|
|
103
|
+
}
|
|
104
|
+
attempt++;
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
function deriveMistralToolCallId(id, attempt) {
|
|
109
|
+
const normalized = id.replace(/[^a-zA-Z0-9]/g, "");
|
|
110
|
+
if (attempt === 0 && normalized.length === MISTRAL_TOOL_CALL_ID_LENGTH)
|
|
111
|
+
return normalized;
|
|
112
|
+
const seedBase = normalized || id;
|
|
113
|
+
const seed = attempt === 0 ? seedBase : `${seedBase}:${attempt}`;
|
|
114
|
+
return shortHash(seed)
|
|
115
|
+
.replace(/[^a-zA-Z0-9]/g, "")
|
|
116
|
+
.slice(0, MISTRAL_TOOL_CALL_ID_LENGTH);
|
|
117
|
+
}
|
|
118
|
+
function formatMistralError(error) {
|
|
119
|
+
if (error instanceof Error) {
|
|
120
|
+
const sdkError = error;
|
|
121
|
+
const statusCode = typeof sdkError.statusCode === "number" ? sdkError.statusCode : undefined;
|
|
122
|
+
const bodyText = typeof sdkError.body === "string" ? sdkError.body.trim() : undefined;
|
|
123
|
+
if (statusCode !== undefined && bodyText) {
|
|
124
|
+
return `Mistral API error (${statusCode}): ${truncateErrorText(bodyText, MAX_MISTRAL_ERROR_BODY_CHARS)}`;
|
|
125
|
+
}
|
|
126
|
+
if (statusCode !== undefined)
|
|
127
|
+
return `Mistral API error (${statusCode}): ${error.message}`;
|
|
128
|
+
return error.message;
|
|
129
|
+
}
|
|
130
|
+
return safeJsonStringify(error);
|
|
131
|
+
}
|
|
132
|
+
function truncateErrorText(text, maxChars) {
|
|
133
|
+
if (text.length <= maxChars)
|
|
134
|
+
return text;
|
|
135
|
+
return `${text.slice(0, maxChars)}... [truncated ${text.length - maxChars} chars]`;
|
|
136
|
+
}
|
|
137
|
+
function safeJsonStringify(value) {
|
|
138
|
+
try {
|
|
139
|
+
const serialized = JSON.stringify(value);
|
|
140
|
+
return serialized === undefined ? String(value) : serialized;
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
return String(value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
function buildRequestOptions(model, options) {
|
|
147
|
+
const requestOptions = {};
|
|
148
|
+
if (options?.signal)
|
|
149
|
+
requestOptions.signal = options.signal;
|
|
150
|
+
requestOptions.retries = { strategy: "none" };
|
|
151
|
+
const headers = {};
|
|
152
|
+
if (model.headers)
|
|
153
|
+
Object.assign(headers, model.headers);
|
|
154
|
+
if (options?.headers)
|
|
155
|
+
Object.assign(headers, options.headers);
|
|
156
|
+
// Mistral infrastructure uses `x-affinity` for KV-cache reuse (prefix caching).
|
|
157
|
+
// Respect explicit caller-provided header values.
|
|
158
|
+
if (options?.sessionId && !headers["x-affinity"]) {
|
|
159
|
+
headers["x-affinity"] = options.sessionId;
|
|
160
|
+
}
|
|
161
|
+
if (Object.keys(headers).length > 0) {
|
|
162
|
+
requestOptions.headers = headers;
|
|
163
|
+
}
|
|
164
|
+
return requestOptions;
|
|
165
|
+
}
|
|
166
|
+
function buildChatPayload(model, context, messages, options) {
|
|
167
|
+
const payload = {
|
|
168
|
+
model: model.id,
|
|
169
|
+
stream: true,
|
|
170
|
+
messages: toChatMessages(messages, model.input.includes("image")),
|
|
171
|
+
};
|
|
172
|
+
if (context.tools?.length)
|
|
173
|
+
payload.tools = toFunctionTools(context.tools);
|
|
174
|
+
if (options?.temperature !== undefined)
|
|
175
|
+
payload.temperature = options.temperature;
|
|
176
|
+
if (options?.maxTokens !== undefined)
|
|
177
|
+
payload.maxTokens = options.maxTokens;
|
|
178
|
+
if (options?.toolChoice)
|
|
179
|
+
payload.toolChoice = mapToolChoice(options.toolChoice);
|
|
180
|
+
if (options?.promptMode)
|
|
181
|
+
payload.promptMode = options.promptMode;
|
|
182
|
+
if (context.systemPrompt) {
|
|
183
|
+
payload.messages.unshift({
|
|
184
|
+
role: "system",
|
|
185
|
+
content: sanitizeSurrogates(context.systemPrompt),
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
return payload;
|
|
189
|
+
}
|
|
190
|
+
async function consumeChatStream(model, output, stream, mistralStream) {
|
|
191
|
+
let currentBlock = null;
|
|
192
|
+
const blocks = output.content;
|
|
193
|
+
const blockIndex = () => blocks.length - 1;
|
|
194
|
+
const toolBlocksByKey = new Map();
|
|
195
|
+
const finishCurrentBlock = (block) => {
|
|
196
|
+
if (!block)
|
|
197
|
+
return;
|
|
198
|
+
if (block.type === "text") {
|
|
199
|
+
stream.push({
|
|
200
|
+
type: "text_end",
|
|
201
|
+
contentIndex: blockIndex(),
|
|
202
|
+
content: block.text,
|
|
203
|
+
partial: output,
|
|
204
|
+
});
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
if (block.type === "thinking") {
|
|
208
|
+
stream.push({
|
|
209
|
+
type: "thinking_end",
|
|
210
|
+
contentIndex: blockIndex(),
|
|
211
|
+
content: block.thinking,
|
|
212
|
+
partial: output,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
};
|
|
216
|
+
for await (const event of mistralStream) {
|
|
217
|
+
const chunk = event.data;
|
|
218
|
+
if (chunk.usage) {
|
|
219
|
+
output.usage.input = chunk.usage.promptTokens || 0;
|
|
220
|
+
output.usage.output = chunk.usage.completionTokens || 0;
|
|
221
|
+
output.usage.cacheRead = 0;
|
|
222
|
+
output.usage.cacheWrite = 0;
|
|
223
|
+
output.usage.totalTokens = chunk.usage.totalTokens || output.usage.input + output.usage.output;
|
|
224
|
+
calculateCost(model, output.usage);
|
|
225
|
+
}
|
|
226
|
+
const choice = chunk.choices[0];
|
|
227
|
+
if (!choice)
|
|
228
|
+
continue;
|
|
229
|
+
if (choice.finishReason) {
|
|
230
|
+
output.stopReason = mapChatStopReason(choice.finishReason);
|
|
231
|
+
}
|
|
232
|
+
const delta = choice.delta;
|
|
233
|
+
if (delta.content !== null && delta.content !== undefined) {
|
|
234
|
+
const contentItems = typeof delta.content === "string" ? [delta.content] : delta.content;
|
|
235
|
+
for (const item of contentItems) {
|
|
236
|
+
if (typeof item === "string") {
|
|
237
|
+
const textDelta = sanitizeSurrogates(item);
|
|
238
|
+
if (!currentBlock || currentBlock.type !== "text") {
|
|
239
|
+
finishCurrentBlock(currentBlock);
|
|
240
|
+
currentBlock = { type: "text", text: "" };
|
|
241
|
+
output.content.push(currentBlock);
|
|
242
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
243
|
+
}
|
|
244
|
+
currentBlock.text += textDelta;
|
|
245
|
+
stream.push({
|
|
246
|
+
type: "text_delta",
|
|
247
|
+
contentIndex: blockIndex(),
|
|
248
|
+
delta: textDelta,
|
|
249
|
+
partial: output,
|
|
250
|
+
});
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (item.type === "thinking") {
|
|
254
|
+
const deltaText = item.thinking
|
|
255
|
+
.map((part) => ("text" in part ? part.text : ""))
|
|
256
|
+
.filter((text) => text.length > 0)
|
|
257
|
+
.join("");
|
|
258
|
+
const thinkingDelta = sanitizeSurrogates(deltaText);
|
|
259
|
+
if (!thinkingDelta)
|
|
260
|
+
continue;
|
|
261
|
+
if (!currentBlock || currentBlock.type !== "thinking") {
|
|
262
|
+
finishCurrentBlock(currentBlock);
|
|
263
|
+
currentBlock = { type: "thinking", thinking: "" };
|
|
264
|
+
output.content.push(currentBlock);
|
|
265
|
+
stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
266
|
+
}
|
|
267
|
+
currentBlock.thinking += thinkingDelta;
|
|
268
|
+
stream.push({
|
|
269
|
+
type: "thinking_delta",
|
|
270
|
+
contentIndex: blockIndex(),
|
|
271
|
+
delta: thinkingDelta,
|
|
272
|
+
partial: output,
|
|
273
|
+
});
|
|
274
|
+
continue;
|
|
275
|
+
}
|
|
276
|
+
if (item.type === "text") {
|
|
277
|
+
const textDelta = sanitizeSurrogates(item.text);
|
|
278
|
+
if (!currentBlock || currentBlock.type !== "text") {
|
|
279
|
+
finishCurrentBlock(currentBlock);
|
|
280
|
+
currentBlock = { type: "text", text: "" };
|
|
281
|
+
output.content.push(currentBlock);
|
|
282
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
283
|
+
}
|
|
284
|
+
currentBlock.text += textDelta;
|
|
285
|
+
stream.push({
|
|
286
|
+
type: "text_delta",
|
|
287
|
+
contentIndex: blockIndex(),
|
|
288
|
+
delta: textDelta,
|
|
289
|
+
partial: output,
|
|
290
|
+
});
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
const toolCalls = delta.toolCalls || [];
|
|
295
|
+
for (const toolCall of toolCalls) {
|
|
296
|
+
if (currentBlock) {
|
|
297
|
+
finishCurrentBlock(currentBlock);
|
|
298
|
+
currentBlock = null;
|
|
299
|
+
}
|
|
300
|
+
const callId = toolCall.id && toolCall.id !== "null"
|
|
301
|
+
? toolCall.id
|
|
302
|
+
: deriveMistralToolCallId(`toolcall:${toolCall.index ?? 0}`, 0);
|
|
303
|
+
const key = `${callId}:${toolCall.index || 0}`;
|
|
304
|
+
const existingIndex = toolBlocksByKey.get(key);
|
|
305
|
+
let block;
|
|
306
|
+
if (existingIndex !== undefined) {
|
|
307
|
+
const existing = output.content[existingIndex];
|
|
308
|
+
if (existing?.type === "toolCall") {
|
|
309
|
+
block = existing;
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
if (!block) {
|
|
313
|
+
block = {
|
|
314
|
+
type: "toolCall",
|
|
315
|
+
id: callId,
|
|
316
|
+
name: toolCall.function.name,
|
|
317
|
+
arguments: {},
|
|
318
|
+
partialArgs: "",
|
|
319
|
+
};
|
|
320
|
+
output.content.push(block);
|
|
321
|
+
toolBlocksByKey.set(key, output.content.length - 1);
|
|
322
|
+
stream.push({ type: "toolcall_start", contentIndex: output.content.length - 1, partial: output });
|
|
323
|
+
}
|
|
324
|
+
const argsDelta = typeof toolCall.function.arguments === "string"
|
|
325
|
+
? toolCall.function.arguments
|
|
326
|
+
: JSON.stringify(toolCall.function.arguments || {});
|
|
327
|
+
block.partialArgs = (block.partialArgs || "") + argsDelta;
|
|
328
|
+
block.arguments = parseStreamingJson(block.partialArgs);
|
|
329
|
+
stream.push({
|
|
330
|
+
type: "toolcall_delta",
|
|
331
|
+
contentIndex: toolBlocksByKey.get(key),
|
|
332
|
+
delta: argsDelta,
|
|
333
|
+
partial: output,
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
finishCurrentBlock(currentBlock);
|
|
338
|
+
for (const index of toolBlocksByKey.values()) {
|
|
339
|
+
const block = output.content[index];
|
|
340
|
+
if (block.type !== "toolCall")
|
|
341
|
+
continue;
|
|
342
|
+
const toolBlock = block;
|
|
343
|
+
toolBlock.arguments = parseStreamingJson(toolBlock.partialArgs);
|
|
344
|
+
delete toolBlock.partialArgs;
|
|
345
|
+
stream.push({
|
|
346
|
+
type: "toolcall_end",
|
|
347
|
+
contentIndex: index,
|
|
348
|
+
toolCall: toolBlock,
|
|
349
|
+
partial: output,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
function toFunctionTools(tools) {
|
|
354
|
+
return tools.map((tool) => ({
|
|
355
|
+
type: "function",
|
|
356
|
+
function: {
|
|
357
|
+
name: tool.name,
|
|
358
|
+
description: tool.description,
|
|
359
|
+
parameters: tool.parameters,
|
|
360
|
+
strict: false,
|
|
361
|
+
},
|
|
362
|
+
}));
|
|
363
|
+
}
|
|
364
|
+
function toChatMessages(messages, supportsImages) {
|
|
365
|
+
const result = [];
|
|
366
|
+
for (const msg of messages) {
|
|
367
|
+
if (msg.role === "user") {
|
|
368
|
+
if (typeof msg.content === "string") {
|
|
369
|
+
result.push({ role: "user", content: sanitizeSurrogates(msg.content) });
|
|
370
|
+
continue;
|
|
371
|
+
}
|
|
372
|
+
const hadImages = msg.content.some((item) => item.type === "image");
|
|
373
|
+
const content = msg.content
|
|
374
|
+
.filter((item) => item.type === "text" || supportsImages)
|
|
375
|
+
.map((item) => {
|
|
376
|
+
if (item.type === "text")
|
|
377
|
+
return { type: "text", text: sanitizeSurrogates(item.text) };
|
|
378
|
+
return { type: "image_url", imageUrl: `data:${item.mimeType};base64,${item.data}` };
|
|
379
|
+
});
|
|
380
|
+
if (content.length > 0) {
|
|
381
|
+
result.push({ role: "user", content });
|
|
382
|
+
continue;
|
|
383
|
+
}
|
|
384
|
+
if (hadImages && !supportsImages) {
|
|
385
|
+
result.push({ role: "user", content: "(image omitted: model does not support images)" });
|
|
386
|
+
}
|
|
387
|
+
continue;
|
|
388
|
+
}
|
|
389
|
+
if (msg.role === "assistant") {
|
|
390
|
+
const contentParts = [];
|
|
391
|
+
const toolCalls = [];
|
|
392
|
+
for (const block of msg.content) {
|
|
393
|
+
if (block.type === "text") {
|
|
394
|
+
if (block.text.trim().length > 0) {
|
|
395
|
+
contentParts.push({ type: "text", text: sanitizeSurrogates(block.text) });
|
|
396
|
+
}
|
|
397
|
+
continue;
|
|
398
|
+
}
|
|
399
|
+
if (block.type === "thinking") {
|
|
400
|
+
if (block.thinking.trim().length > 0) {
|
|
401
|
+
contentParts.push({
|
|
402
|
+
type: "thinking",
|
|
403
|
+
thinking: [{ type: "text", text: sanitizeSurrogates(block.thinking) }],
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
continue;
|
|
407
|
+
}
|
|
408
|
+
toolCalls.push({
|
|
409
|
+
id: block.id,
|
|
410
|
+
type: "function",
|
|
411
|
+
function: { name: block.name, arguments: JSON.stringify(block.arguments || {}) },
|
|
412
|
+
});
|
|
413
|
+
}
|
|
414
|
+
const assistantMessage = { role: "assistant" };
|
|
415
|
+
if (contentParts.length > 0)
|
|
416
|
+
assistantMessage.content = contentParts;
|
|
417
|
+
if (toolCalls.length > 0)
|
|
418
|
+
assistantMessage.toolCalls = toolCalls;
|
|
419
|
+
if (contentParts.length > 0 || toolCalls.length > 0)
|
|
420
|
+
result.push(assistantMessage);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
const toolContent = [];
|
|
424
|
+
const textResult = msg.content
|
|
425
|
+
.filter((part) => part.type === "text")
|
|
426
|
+
.map((part) => (part.type === "text" ? sanitizeSurrogates(part.text) : ""))
|
|
427
|
+
.join("\n");
|
|
428
|
+
const hasImages = msg.content.some((part) => part.type === "image");
|
|
429
|
+
const toolText = buildToolResultText(textResult, hasImages, supportsImages, msg.isError);
|
|
430
|
+
toolContent.push({ type: "text", text: toolText });
|
|
431
|
+
for (const part of msg.content) {
|
|
432
|
+
if (!supportsImages)
|
|
433
|
+
continue;
|
|
434
|
+
if (part.type !== "image")
|
|
435
|
+
continue;
|
|
436
|
+
toolContent.push({
|
|
437
|
+
type: "image_url",
|
|
438
|
+
imageUrl: `data:${part.mimeType};base64,${part.data}`,
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
result.push({
|
|
442
|
+
role: "tool",
|
|
443
|
+
toolCallId: msg.toolCallId,
|
|
444
|
+
name: msg.toolName,
|
|
445
|
+
content: toolContent,
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
return result;
|
|
449
|
+
}
|
|
450
|
+
function buildToolResultText(text, hasImages, supportsImages, isError) {
|
|
451
|
+
const trimmed = text.trim();
|
|
452
|
+
const errorPrefix = isError ? "[tool error] " : "";
|
|
453
|
+
if (trimmed.length > 0) {
|
|
454
|
+
const imageSuffix = hasImages && !supportsImages ? "\n[tool image omitted: model does not support images]" : "";
|
|
455
|
+
return `${errorPrefix}${trimmed}${imageSuffix}`;
|
|
456
|
+
}
|
|
457
|
+
if (hasImages) {
|
|
458
|
+
if (supportsImages) {
|
|
459
|
+
return isError ? "[tool error] (see attached image)" : "(see attached image)";
|
|
460
|
+
}
|
|
461
|
+
return isError
|
|
462
|
+
? "[tool error] (image omitted: model does not support images)"
|
|
463
|
+
: "(image omitted: model does not support images)";
|
|
464
|
+
}
|
|
465
|
+
return isError ? "[tool error] (no tool output)" : "(no tool output)";
|
|
466
|
+
}
|
|
467
|
+
function mapToolChoice(choice) {
|
|
468
|
+
if (!choice)
|
|
469
|
+
return undefined;
|
|
470
|
+
if (choice === "auto" || choice === "none" || choice === "any" || choice === "required") {
|
|
471
|
+
return choice;
|
|
472
|
+
}
|
|
473
|
+
return {
|
|
474
|
+
type: "function",
|
|
475
|
+
function: { name: choice.function.name },
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
function mapChatStopReason(reason) {
|
|
479
|
+
if (reason === null)
|
|
480
|
+
return "stop";
|
|
481
|
+
switch (reason) {
|
|
482
|
+
case "stop":
|
|
483
|
+
return "stop";
|
|
484
|
+
case "length":
|
|
485
|
+
case "model_length":
|
|
486
|
+
return "length";
|
|
487
|
+
case "tool_calls":
|
|
488
|
+
return "toolUse";
|
|
489
|
+
case "error":
|
|
490
|
+
return "error";
|
|
491
|
+
default:
|
|
492
|
+
return "stop";
|
|
493
|
+
}
|
|
494
|
+
}
|
|
495
|
+
//# sourceMappingURL=mistral.js.map
|
|
@@ -0,0 +1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O,CAAC,CAAC,CAAC,+BAA+B,CAAC,CAAC,CAAC,kBAAkB,CAAC;AAAA,CACtE;AAED,SAAS,aAAa,CACrB,MAAoC,EACkE;IACtG,IAAI,CAAC,MAAM;QAAE,OAAO,SAAS,CAAC;IAC9B,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,MAAM,IAAI,MAAM,KAAK,KAAK,IAAI,MAAM,KAAK,UAAU,EAAE,CAAC;QACzF,OAAO,MAAa,CAAC;IACtB,CAAC;IACD,OAAO;QACN,IAAI,EAAE,UAAU;QAChB,QAAQ,EAAE,EAAE,IAAI,EAAE,MAAM,CAAC,QAAQ,CAAC,IAAI,EAAE;KACxC,CAAC;AAAA,CACF;AAED,SAAS,iBAAiB,CAAC,MAAqB,EAAc;IAC7D,IAAI,MAAM,KAAK,IAAI;QAAE,OAAO,MAAM,CAAC;IACnC,QAAQ,MAAM,EAAE,CAAC;QAChB,KAAK,MAAM;YACV,OAAO,MAAM,CAAC;QACf,KAAK,QAAQ,CAAC;QACd,KAAK,cAAc;YAClB,OAAO,QAAQ,CAAC;QACjB,KAAK,YAAY;YAChB,OAAO,SAAS,CAAC;QAClB,KAAK,OAAO;YACX,OAAO,OAAO,CAAC;QAChB;YACC,OAAO,MAAM,CAAC;IAChB,CAAC;AAAA,CACD","sourcesContent":["import { Mistral } from \"@mistralai/mistralai\";\nimport type { RequestOptions } from \"@mistralai/mistralai/lib/sdks.js\";\nimport type {\n\tChatCompletionStreamRequest,\n\tChatCompletionStreamRequestMessages,\n\tCompletionEvent,\n\tContentChunk,\n\tFunctionTool,\n} from \"@mistralai/mistralai/models/components/index.js\";\nimport { getEnvApiKey } from \"../env-api-keys.js\";\nimport { calculateCost } from \"../models.js\";\nimport type {\n\tAssistantMessage,\n\tContext,\n\tMessage,\n\tModel,\n\tSimpleStreamOptions,\n\tStopReason,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingContent,\n\tTool,\n\tToolCall,\n} from \"../types.js\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.js\";\nimport { shortHash } from \"../utils/hash.js\";\nimport { parseStreamingJson } from \"../utils/json-parse.js\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.js\";\nimport { buildBaseOptions, clampReasoning } from \"./simple-options.js\";\nimport { transformMessages } from \"./transform-messages.js\";\n\nconst MISTRAL_TOOL_CALL_ID_LENGTH = 9;\nconst MAX_MISTRAL_ERROR_BODY_CHARS = 4000;\n\n/**\n * Provider-specific options for the Mistral API.\n */\nexport interface MistralOptions extends StreamOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\" | \"required\" | { type: \"function\"; function: { name: string } };\n\tpromptMode?: \"reasoning\";\n}\n\n/**\n * Stream responses from Mistral using `chat.stream`.\n */\nexport const streamMistral: StreamFunction<\"mistral-conversations\", MistralOptions> = (\n\tmodel: Model<\"mistral-conversations\">,\n\tcontext: Context,\n\toptions?: MistralOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t(async () => {\n\t\tconst output = createOutput(model);\n\n\t\ttry {\n\t\t\tconst apiKey = options?.apiKey || getEnvApiKey(model.provider);\n\t\t\tif (!apiKey) {\n\t\t\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t\t\t}\n\n\t\t\t// Intentionally per-request: avoids shared SDK mutable state across concurrent consumers.\n\t\t\tconst mistral = new Mistral({\n\t\t\t\tapiKey,\n\t\t\t\tserverURL: model.baseUrl,\n\t\t\t});\n\n\t\t\tconst normalizeMistralToolCallId = createMistralToolCallIdNormalizer();\n\t\t\tconst transformedMessages = transformMessages(context.messages, model, (id) => normalizeMistralToolCallId(id));\n\n\t\t\tconst payload = buildChatPayload(model, context, transformedMessages, options);\n\t\t\toptions?.onPayload?.(payload);\n\t\t\tconst mistralStream = await mistral.chat.stream(payload, buildRequestOptions(model, options));\n\t\t\tstream.push({ type: \"start\", partial: output });\n\t\t\tawait consumeChatStream(model, output, stream, mistralStream);\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatMistralError(error);\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\n/**\n * Maps provider-agnostic `SimpleStreamOptions` to Mistral options.\n */\nexport const streamSimpleMistral: StreamFunction<\"mistral-conversations\", SimpleStreamOptions> = (\n\tmodel: Model<\"mistral-conversations\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst apiKey = options?.apiKey || getEnvApiKey(model.provider);\n\tif (!apiKey) {\n\t\tthrow new Error(`No API key for provider: ${model.provider}`);\n\t}\n\n\tconst base = buildBaseOptions(model, options, apiKey);\n\tconst reasoning = clampReasoning(options?.reasoning);\n\n\treturn streamMistral(model, context, {\n\t\t...base,\n\t\tpromptMode: model.reasoning && reasoning ? \"reasoning\" : undefined,\n\t} satisfies MistralOptions);\n};\n\nfunction createOutput(model: Model<\"mistral-conversations\">): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason: \"stop\",\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction createMistralToolCallIdNormalizer(): (id: string) => string {\n\tconst idMap = new Map<string, string>();\n\tconst reverseMap = new Map<string, string>();\n\n\treturn (id: string): string => {\n\t\tconst existing = idMap.get(id);\n\t\tif (existing) return existing;\n\n\t\tlet attempt = 0;\n\t\twhile (true) {\n\t\t\tconst candidate = deriveMistralToolCallId(id, attempt);\n\t\t\tconst owner = reverseMap.get(candidate);\n\t\t\tif (!owner || owner === id) {\n\t\t\t\tidMap.set(id, candidate);\n\t\t\t\treverseMap.set(candidate, id);\n\t\t\t\treturn candidate;\n\t\t\t}\n\t\t\tattempt++;\n\t\t}\n\t};\n}\n\nfunction deriveMistralToolCallId(id: string, attempt: number): string {\n\tconst normalized = id.replace(/[^a-zA-Z0-9]/g, \"\");\n\tif (attempt === 0 && normalized.length === MISTRAL_TOOL_CALL_ID_LENGTH) return normalized;\n\tconst seedBase = normalized || id;\n\tconst seed = attempt === 0 ? seedBase : `${seedBase}:${attempt}`;\n\treturn shortHash(seed)\n\t\t.replace(/[^a-zA-Z0-9]/g, \"\")\n\t\t.slice(0, MISTRAL_TOOL_CALL_ID_LENGTH);\n}\n\nfunction formatMistralError(error: unknown): string {\n\tif (error instanceof Error) {\n\t\tconst sdkError = error as Error & { statusCode?: unknown; body?: unknown };\n\t\tconst statusCode = typeof sdkError.statusCode === \"number\" ? sdkError.statusCode : undefined;\n\t\tconst bodyText = typeof sdkError.body === \"string\" ? sdkError.body.trim() : undefined;\n\t\tif (statusCode !== undefined && bodyText) {\n\t\t\treturn `Mistral API error (${statusCode}): ${truncateErrorText(bodyText, MAX_MISTRAL_ERROR_BODY_CHARS)}`;\n\t\t}\n\t\tif (statusCode !== undefined) return `Mistral API error (${statusCode}): ${error.message}`;\n\t\treturn error.message;\n\t}\n\treturn safeJsonStringify(error);\n}\n\nfunction truncateErrorText(text: string, maxChars: number): string {\n\tif (text.length <= maxChars) return text;\n\treturn `${text.slice(0, maxChars)}... [truncated ${text.length - maxChars} chars]`;\n}\n\nfunction safeJsonStringify(value: unknown): string {\n\ttry {\n\t\tconst serialized = JSON.stringify(value);\n\t\treturn serialized === undefined ? String(value) : serialized;\n\t} catch {\n\t\treturn String(value);\n\t}\n}\n\nfunction buildRequestOptions(model: Model<\"mistral-conversations\">, options?: MistralOptions): RequestOptions {\n\tconst requestOptions: RequestOptions = {};\n\tif (options?.signal) requestOptions.signal = options.signal;\n\trequestOptions.retries = { strategy: \"none\" };\n\n\tconst headers: Record<string, string> = {};\n\tif (model.headers) Object.assign(headers, model.headers);\n\tif (options?.headers) Object.assign(headers, options.headers);\n\n\t// Mistral infrastructure uses `x-affinity` for KV-cache reuse (prefix caching).\n\t// Respect explicit caller-provided header values.\n\tif (options?.sessionId && !headers[\"x-affinity\"]) {\n\t\theaders[\"x-affinity\"] = options.sessionId;\n\t}\n\n\tif (Object.keys(headers).length > 0) {\n\t\trequestOptions.headers = headers;\n\t}\n\n\treturn requestOptions;\n}\n\nfunction buildChatPayload(\n\tmodel: Model<\"mistral-conversations\">,\n\tcontext: Context,\n\tmessages: Message[],\n\toptions?: MistralOptions,\n): ChatCompletionStreamRequest {\n\tconst payload: ChatCompletionStreamRequest = {\n\t\tmodel: model.id,\n\t\tstream: true,\n\t\tmessages: toChatMessages(messages, model.input.includes(\"image\")),\n\t};\n\n\tif (context.tools?.length) payload.tools = toFunctionTools(context.tools);\n\tif (options?.temperature !== undefined) payload.temperature = options.temperature;\n\tif (options?.maxTokens !== undefined) payload.maxTokens = options.maxTokens;\n\tif (options?.toolChoice) payload.toolChoice = mapToolChoice(options.toolChoice);\n\tif (options?.promptMode) payload.promptMode = options.promptMode as any;\n\n\tif (context.systemPrompt) {\n\t\tpayload.messages.unshift({\n\t\t\trole: \"system\",\n\t\t\tcontent: sanitizeSurrogates(context.systemPrompt),\n\t\t});\n\t}\n\n\treturn payload;\n}\n\nasync function consumeChatStream(\n\tmodel: Model<\"mistral-conversations\">,\n\toutput: AssistantMessage,\n\tstream: AssistantMessageEventStream,\n\tmistralStream: AsyncIterable<CompletionEvent>,\n): Promise<void> {\n\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\tconst blocks = output.content;\n\tconst blockIndex = () => blocks.length - 1;\n\tconst toolBlocksByKey = new Map<string, number>();\n\n\tconst finishCurrentBlock = (block?: typeof currentBlock) => {\n\t\tif (!block) return;\n\t\tif (block.type === \"text\") {\n\t\t\tstream.push({\n\t\t\t\ttype: \"text_end\",\n\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\tcontent: block.text,\n\t\t\t\tpartial: output,\n\t\t\t});\n\t\t\treturn;\n\t\t}\n\t\tif (block.type === \"thinking\") {\n\t\t\tstream.push({\n\t\t\t\ttype: \"thinking_end\",\n\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\tcontent: block.thinking,\n\t\t\t\tpartial: output,\n\t\t\t});\n\t\t}\n\t};\n\n\tfor await (const event of mistralStream) {\n\t\tconst chunk = event.data;\n\n\t\tif (chunk.usage) {\n\t\t\toutput.usage.input = chunk.usage.promptTokens || 0;\n\t\t\toutput.usage.output = chunk.usage.completionTokens || 0;\n\t\t\toutput.usage.cacheRead = 0;\n\t\t\toutput.usage.cacheWrite = 0;\n\t\t\toutput.usage.totalTokens = chunk.usage.totalTokens || output.usage.input + output.usage.output;\n\t\t\tcalculateCost(model, output.usage);\n\t\t}\n\n\t\tconst choice = chunk.choices[0];\n\t\tif (!choice) continue;\n\n\t\tif (choice.finishReason) {\n\t\t\toutput.stopReason = mapChatStopReason(choice.finishReason);\n\t\t}\n\n\t\tconst delta = choice.delta;\n\t\tif (delta.content !== null && delta.content !== undefined) {\n\t\t\tconst contentItems = typeof delta.content === \"string\" ? [delta.content] : delta.content;\n\t\t\tfor (const item of contentItems) {\n\t\t\t\tif (typeof item === \"string\") {\n\t\t\t\t\tconst textDelta = sanitizeSurrogates(item);\n\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"text\") {\n\t\t\t\t\t\tfinishCurrentBlock(currentBlock);\n\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t}\n\t\t\t\t\tcurrentBlock.text += textDelta;\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tdelta: textDelta,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (item.type === \"thinking\") {\n\t\t\t\t\tconst deltaText = item.thinking\n\t\t\t\t\t\t.map((part) => (\"text\" in part ? part.text : \"\"))\n\t\t\t\t\t\t.filter((text) => text.length > 0)\n\t\t\t\t\t\t.join(\"\");\n\t\t\t\t\tconst thinkingDelta = sanitizeSurrogates(deltaText);\n\t\t\t\t\tif (!thinkingDelta) continue;\n\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"thinking\") {\n\t\t\t\t\t\tfinishCurrentBlock(currentBlock);\n\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\" };\n\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\tstream.push({ type: \"thinking_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t}\n\t\t\t\t\tcurrentBlock.thinking += thinkingDelta;\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tdelta: thinkingDelta,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\n\t\t\t\tif (item.type === \"text\") {\n\t\t\t\t\tconst textDelta = sanitizeSurrogates(item.text);\n\t\t\t\t\tif (!currentBlock || currentBlock.type !== \"text\") {\n\t\t\t\t\t\tfinishCurrentBlock(currentBlock);\n\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t}\n\t\t\t\t\tcurrentBlock.text += textDelta;\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tdelta: textDelta,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tconst toolCalls = delta.toolCalls || [];\n\t\tfor (const toolCall of toolCalls) {\n\t\t\tif (currentBlock) {\n\t\t\t\tfinishCurrentBlock(currentBlock);\n\t\t\t\tcurrentBlock = null;\n\t\t\t}\n\t\t\tconst callId =\n\t\t\t\ttoolCall.id && toolCall.id !== \"null\"\n\t\t\t\t\t? toolCall.id\n\t\t\t\t\t: deriveMistralToolCallId(`toolcall:${toolCall.index ?? 0}`, 0);\n\t\t\tconst key = `${callId}:${toolCall.index || 0}`;\n\t\t\tconst existingIndex = toolBlocksByKey.get(key);\n\t\t\tlet block: (ToolCall & { partialArgs?: string }) | undefined;\n\n\t\t\tif (existingIndex !== undefined) {\n\t\t\t\tconst existing = output.content[existingIndex];\n\t\t\t\tif (existing?.type === \"toolCall\") {\n\t\t\t\t\tblock = existing as ToolCall & { partialArgs?: string };\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (!block) {\n\t\t\t\tblock = {\n\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\tid: callId,\n\t\t\t\t\tname: toolCall.function.name,\n\t\t\t\t\targuments: {},\n\t\t\t\t\tpartialArgs: \"\",\n\t\t\t\t};\n\t\t\t\toutput.content.push(block);\n\t\t\t\ttoolBlocksByKey.set(key, output.content.length - 1);\n\t\t\t\tstream.push({ type: \"toolcall_start\", contentIndex: output.content.length - 1, partial: output });\n\t\t\t}\n\n\t\t\tconst argsDelta =\n\t\t\t\ttypeof toolCall.function.arguments === \"string\"\n\t\t\t\t\t? toolCall.function.arguments\n\t\t\t\t\t: JSON.stringify(toolCall.function.arguments || {});\n\t\t\tblock.partialArgs = (block.partialArgs || \"\") + argsDelta;\n\t\t\tblock.arguments = parseStreamingJson<Record<string, unknown>>(block.partialArgs);\n\t\t\tstream.push({\n\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\tcontentIndex: toolBlocksByKey.get(key)!,\n\t\t\t\tdelta: argsDelta,\n\t\t\t\tpartial: output,\n\t\t\t});\n\t\t}\n\t}\n\n\tfinishCurrentBlock(currentBlock);\n\tfor (const index of toolBlocksByKey.values()) {\n\t\tconst block = output.content[index];\n\t\tif (block.type !== \"toolCall\") continue;\n\t\tconst toolBlock = block as ToolCall & { partialArgs?: string };\n\t\ttoolBlock.arguments = parseStreamingJson<Record<string, unknown>>(toolBlock.partialArgs);\n\t\tdelete toolBlock.partialArgs;\n\t\tstream.push({\n\t\t\ttype: \"toolcall_end\",\n\t\t\tcontentIndex: index,\n\t\t\ttoolCall: toolBlock,\n\t\t\tpartial: output,\n\t\t});\n\t}\n}\n\nfunction toFunctionTools(tools: Tool[]): Array<FunctionTool & { type: \"function\" }> {\n\treturn tools.map((tool) => ({\n\t\ttype: \"function\",\n\t\tfunction: {\n\t\t\tname: tool.name,\n\t\t\tdescription: tool.description,\n\t\t\tparameters: tool.parameters as unknown as Record<string, unknown>,\n\t\t\tstrict: false,\n\t\t},\n\t}));\n}\n\nfunction toChatMessages(messages: Message[], supportsImages: boolean): ChatCompletionStreamRequestMessages[] {\n\tconst result: ChatCompletionStreamRequestMessages[] = [];\n\n\tfor (const msg of messages) {\n\t\tif (msg.role === \"user\") {\n\t\t\tif (typeof msg.content === \"string\") {\n\t\t\t\tresult.push({ role: \"user\", content: sanitizeSurrogates(msg.content) });\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst hadImages = msg.content.some((item) => item.type === \"image\");\n\t\t\tconst content: ContentChunk[] = msg.content\n\t\t\t\t.filter((item) => item.type === \"text\" || supportsImages)\n\t\t\t\t.map((item) => {\n\t\t\t\t\tif (item.type === \"text\") return { type: \"text\", text: sanitizeSurrogates(item.text) };\n\t\t\t\t\treturn { type: \"image_url\", imageUrl: `data:${item.mimeType};base64,${item.data}` };\n\t\t\t\t});\n\t\t\tif (content.length > 0) {\n\t\t\t\tresult.push({ role: \"user\", content });\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tif (hadImages && !supportsImages) {\n\t\t\t\tresult.push({ role: \"user\", content: \"(image omitted: model does not support images)\" });\n\t\t\t}\n\t\t\tcontinue;\n\t\t}\n\n\t\tif (msg.role === \"assistant\") {\n\t\t\tconst contentParts: ContentChunk[] = [];\n\t\t\tconst toolCalls: Array<{ id: string; type: \"function\"; function: { name: string; arguments: string } }> = [];\n\n\t\t\tfor (const block of msg.content) {\n\t\t\t\tif (block.type === \"text\") {\n\t\t\t\t\tif (block.text.trim().length > 0) {\n\t\t\t\t\t\tcontentParts.push({ type: \"text\", text: sanitizeSurrogates(block.text) });\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\tif (block.type === \"thinking\") {\n\t\t\t\t\tif (block.thinking.trim().length > 0) {\n\t\t\t\t\t\tcontentParts.push({\n\t\t\t\t\t\t\ttype: \"thinking\",\n\t\t\t\t\t\t\tthinking: [{ type: \"text\", text: sanitizeSurrogates(block.thinking) }],\n\t\t\t\t\t\t});\n\t\t\t\t\t}\n\t\t\t\t\tcontinue;\n\t\t\t\t}\n\t\t\t\ttoolCalls.push({\n\t\t\t\t\tid: block.id,\n\t\t\t\t\ttype: \"function\",\n\t\t\t\t\tfunction: { name: block.name, arguments: JSON.stringify(block.arguments || {}) },\n\t\t\t\t});\n\t\t\t}\n\n\t\t\tconst assistantMessage: ChatCompletionStreamRequestMessages = { role: \"assistant\" };\n\t\t\tif (contentParts.length > 0) assistantMessage.content = contentParts;\n\t\t\tif (toolCalls.length > 0) assistantMessage.toolCalls = toolCalls;\n\t\t\tif (contentParts.length > 0 || toolCalls.length > 0) result.push(assistantMessage);\n\t\t\tcontinue;\n\t\t}\n\n\t\tconst toolContent: ContentChunk[] = [];\n\t\tconst textResult = msg.content\n\t\t\t.filter((part) => part.type === \"text\")\n\t\t\t.map((part) => (part.type === \"text\" ? sanitizeSurrogates(part.text) : \"\"))\n\t\t\t.join(\"\\n\");\n\t\tconst hasImages = msg.content.some((part) => part.type === \"image\");\n\t\tconst toolText = buildToolResultText(textResult, hasImages, supportsImages, msg.isError);\n\t\ttoolContent.push({ type: \"text\", text: toolText });\n\t\tfor (const part of msg.content) {\n\t\t\tif (!supportsImages) continue;\n\t\t\tif (part.type !== \"image\") continue;\n\t\t\ttoolContent.push({\n\t\t\t\ttype: \"image_url\",\n\t\t\t\timageUrl: `data:${part.mimeType};base64,${part.data}`,\n\t\t\t});\n\t\t}\n\t\tresult.push({\n\t\t\trole: \"tool\",\n\t\t\ttoolCallId: msg.toolCallId,\n\t\t\tname: msg.toolName,\n\t\t\tcontent: toolContent,\n\t\t});\n\t}\n\n\treturn result;\n}\n\nfunction buildToolResultText(text: string, hasImages: boolean, supportsImages: boolean, isError: boolean): string {\n\tconst trimmed = text.trim();\n\tconst errorPrefix = isError ? \"[tool error] \" : \"\";\n\n\tif (trimmed.length > 0) {\n\t\tconst imageSuffix = hasImages && !supportsImages ? \"\\n[tool image omitted: model does not support images]\" : \"\";\n\t\treturn `${errorPrefix}${trimmed}${imageSuffix}`;\n\t}\n\n\tif (hasImages) {\n\t\tif (supportsImages) {\n\t\t\treturn isError ? \"[tool error] (see attached image)\" : \"(see attached image)\";\n\t\t}\n\t\treturn isError\n\t\t\t? \"[tool error] (image omitted: model does not support images)\"\n\t\t\t: \"(image omitted: model does not support images)\";\n\t}\n\n\treturn isError ? \"[tool error] (no tool output)\" : \"(no tool output)\";\n}\n\nfunction mapToolChoice(\n\tchoice: MistralOptions[\"toolChoice\"],\n): \"auto\" | \"none\" | \"any\" | \"required\" | { type: \"function\"; function: { name: string } } | undefined {\n\tif (!choice) return undefined;\n\tif (choice === \"auto\" || choice === \"none\" || choice === \"any\" || choice === \"required\") {\n\t\treturn choice as any;\n\t}\n\treturn {\n\t\ttype: \"function\",\n\t\tfunction: { name: choice.function.name },\n\t};\n}\n\nfunction mapChatStopReason(reason: string | null): StopReason {\n\tif (reason === null) return \"stop\";\n\tswitch (reason) {\n\t\tcase \"stop\":\n\t\t\treturn \"stop\";\n\t\tcase \"length\":\n\t\tcase \"model_length\":\n\t\t\treturn \"length\";\n\t\tcase \"tool_calls\":\n\t\t\treturn \"toolUse\";\n\t\tcase \"error\":\n\t\t\treturn \"error\";\n\t\tdefault:\n\t\t\treturn \"stop\";\n\t}\n}\n"]}
|