GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: resolve Mistral API compatibility issues (#2440) · Rwanbt/unifia@554b06f

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • packages/opencode/src/provider
    • transform.ts
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎packages/opencode/src/provider/transform.ts‎

Copy file name to clipboardExpand all lines: packages/opencode/src/provider/transform.ts
+70-16Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,77 @@ import { unique } from "remeda"
33
import type { JSONSchema } from "zod/v4/core"
44

55
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+
}
1423
}
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+
}
1872
}
19-
return msg
20-
})
73+
return result
74+
}
75+
76+
return msgs
2177
}
2278

2379
function applyCaching(msgs: ModelMessage[], providerID: string): ModelMessage[] {
@@ -64,9 +120,7 @@ export namespace ProviderTransform {
64120
}
65121

66122
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)
70124
if (providerID === "anthropic" || modelID.includes("anthropic") || modelID.includes("claude")) {
71125
msgs = applyCaching(msgs, providerID)
72126
}

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.