@@ -3,21 +3,77 @@ import { unique } from "remeda" |
3 | 3 | import type { JSONSchema } from "zod/v4/core" |
4 | 4 |
|
5 | 5 | export namespace ProviderTransform { |
6 | | - function normalizeToolCallIds(msgs: ModelMessage[]): ModelMessage[] { |
7 | | - return msgs.map((msg) => { |
8 | | - if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) { |
9 | | - msg.content = msg.content.map((part) => { |
10 | | - if ((part.type === "tool-call" || part.type === "tool-result") && "toolCallId" in part) { |
11 | | - return { |
12 | | - ...part, |
13 | | - toolCallId: part.toolCallId.replace(/[^a-zA-Z0-9_-]/g, "_"), |
| 6 | + function normalizeMessages( |
| 7 | + msgs: ModelMessage[], |
| 8 | + providerID: string, |
| 9 | + modelID: string, |
| 10 | + ): ModelMessage[] { |
| 11 | + if (modelID.includes("claude")) { |
| 12 | + return msgs.map((msg) => { |
| 13 | + if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) { |
| 14 | + msg.content = msg.content.map((part) => { |
| 15 | + if ( |
| 16 | + (part.type === "tool-call" || part.type === "tool-result") && |
| 17 | + "toolCallId" in part |
| 18 | + ) { |
| 19 | + return { |
| 20 | + ...part, |
| 21 | + toolCallId: part.toolCallId.replace(/[^a-zA-Z0-9_-]/g, "_"), |
| 22 | + } |
14 | 23 | } |
15 | | - } |
16 | | - return part |
17 | | - }) |
| 24 | + return part |
| 25 | + }) |
| 26 | + } |
| 27 | + return msg |
| 28 | + }) |
| 29 | + } |
| 30 | + if (providerID === "mistral" || modelID.toLowerCase().includes("mistral")) { |
| 31 | + const result: ModelMessage[] = [] |
| 32 | + for (let i = 0; i < msgs.length; i++) { |
| 33 | + const msg = msgs[i] |
| 34 | + const prevMsg = msgs[i - 1] |
| 35 | + const nextMsg = msgs[i + 1] |
| 36 | + |
| 37 | + if ((msg.role === "assistant" || msg.role === "tool") && Array.isArray(msg.content)) { |
| 38 | + msg.content = msg.content.map((part) => { |
| 39 | + if ( |
| 40 | + (part.type === "tool-call" || part.type === "tool-result") && |
| 41 | + "toolCallId" in part |
| 42 | + ) { |
| 43 | + // Mistral requires alphanumeric tool call IDs with exactly 9 characters |
| 44 | + const normalizedId = part.toolCallId |
| 45 | + .replace(/[^a-zA-Z0-9]/g, "") // Remove non-alphanumeric characters |
| 46 | + .substring(0, 9) // Take first 9 characters |
| 47 | + .padEnd(9, "0") // Pad with zeros if less than 9 characters |
| 48 | + |
| 49 | + return { |
| 50 | + ...part, |
| 51 | + toolCallId: normalizedId, |
| 52 | + } |
| 53 | + } |
| 54 | + return part |
| 55 | + }) |
| 56 | + } |
| 57 | + |
| 58 | + result.push(msg) |
| 59 | + |
| 60 | + // Fix message sequence: tool messages cannot be followed by user messages |
| 61 | + if (msg.role === "tool" && nextMsg?.role === "user") { |
| 62 | + result.push({ |
| 63 | + role: "assistant", |
| 64 | + content: [ |
| 65 | + { |
| 66 | + type: "text", |
| 67 | + text: "Done.", |
| 68 | + }, |
| 69 | + ], |
| 70 | + }) |
| 71 | + } |
18 | 72 | } |
19 | | - return msg |
20 | | - }) |
| 73 | + return result |
| 74 | + } |
| 75 | + |
| 76 | + return msgs |
21 | 77 | } |
22 | 78 |
|
23 | 79 | function applyCaching(msgs: ModelMessage[], providerID: string): ModelMessage[] { |
@@ -64,9 +120,7 @@ export namespace ProviderTransform { |
64 | 120 | } |
65 | 121 |
|
66 | 122 | export function message(msgs: ModelMessage[], providerID: string, modelID: string) { |
67 | | - if (modelID.includes("claude")) { |
68 | | - msgs = normalizeToolCallIds(msgs) |
69 | | - } |
| 123 | + msgs = normalizeMessages(msgs, providerID, modelID) |
70 | 124 | if (providerID === "anthropic" || modelID.includes("anthropic") || modelID.includes("claude")) { |
71 | 125 | msgs = applyCaching(msgs, providerID) |
72 | 126 | } |
|