GeezerVison is showing github.com — fix(provider): preserve Mistral reasoning history (#38453) · argszero/opencode@20589d6
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +7-7Lines changed: 7 additions & 7 deletions Collapse file Copy file name to clipboardExpand all lines: package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 149 | 149 | | 150 | 150 | | 151 | 151 | | 152 | | - | | 152 | + | 153 | 153 | | 154 | 154 | | 155 | 155 | | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 72 | 72 | "@ai-sdk/google": "3.0.73", | 73 | 73 | "@ai-sdk/google-vertex": "4.0.128", | 74 | 74 | "@ai-sdk/groq": "3.0.31", | 75 | | - "@ai-sdk/mistral": "3.0.34", | | 75 | + "@ai-sdk/mistral": "3.0.51", | 76 | 76 | "@ai-sdk/openai": "3.0.84", | 77 | 77 | "@ai-sdk/openai-compatible": "2.0.41", | 78 | 78 | "@ai-sdk/perplexity": "3.0.26", | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/test/provider-mistral.test.ts +254Lines changed: 254 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,3 +26,257 @@ test("Mistral sends promptCacheKey as prompt_cache_key", async () => { | 26 | 26 |
| 27 | 27 | expect(body?.prompt_cache_key).toBe("session-123") | 28 | 28 | }) | | 29 | + | | 30 | +test("Mistral round-trips native reasoning in assistant history", async () => { | | 31 | + let body: { messages?: unknown[] } | undefined | | 32 | + const mockFetch = Object.assign( | | 33 | + async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => { | | 34 | + body = JSON.parse(String(init?.body)) | | 35 | + return Response.json({ | | 36 | + id: "response-1", | | 37 | + created: 0, | | 38 | + model: "mistral-small-latest", | | 39 | + object: "chat.completion", | | 40 | + choices: [ | | 41 | + { | | 42 | + index: 0, | | 43 | + message: { | | 44 | + role: "assistant", | | 45 | + content: [ | | 46 | + { | | 47 | + type: "thinking", | | 48 | + thinking: [ | | 49 | + { type: "text", text: "The user is greeting me." }, | | 50 | + { | | 51 | + type: "tool_reference", | | 52 | + tool: "web_search", | | 53 | + title: "Example result", | | 54 | + url: "https://example.com/tool", | | 55 | + favicon: "https://example.com/favicon.ico", | | 56 | + description: "Example description", | | 57 | + }, | | 58 | + { type: "reference", reference_ids: [1, "source-2"] }, | | 59 | + ], | | 60 | + closed: true, | | 61 | + signature: "sig-123", | | 62 | + }, | | 63 | + { type: "text", text: "Hi" }, | | 64 | + ], | | 65 | + }, | | 66 | + finish_reason: "stop", | | 67 | + }, | | 68 | + ], | | 69 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | | 70 | + }) | | 71 | + }, | | 72 | + { preconnect: fetch.preconnect }, | | 73 | + ) | | 74 | + const model = createMistral({ apiKey: "test", fetch: mockFetch })("mistral-small-latest") | | 75 | + | | 76 | + const first = await model.doGenerate({ | | 77 | + prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }], | | 78 | + }) | | 79 | + const reasoning = first.content.find((part) => part.type === "reasoning") | | 80 | + const text = first.content.find((part) => part.type === "text") | | 81 | + if (!reasoning || !text) throw new Error("expected reasoning and text") | | 82 | + | | 83 | + await model.doGenerate({ | | 84 | + prompt: [ | | 85 | + { role: "user", content: [{ type: "text", text: "Hello" }] }, | | 86 | + { | | 87 | + role: "assistant", | | 88 | + content: [{ ...reasoning, providerOptions: reasoning.providerMetadata }, text], | | 89 | + }, | | 90 | + { role: "user", content: [{ type: "text", text: "Hello again" }] }, | | 91 | + ], | | 92 | + }) | | 93 | + | | 94 | + expect(body?.messages?.[1]).toEqual({ | | 95 | + role: "assistant", | | 96 | + content: [ | | 97 | + { | | 98 | + type: "thinking", | | 99 | + thinking: [ | | 100 | + { type: "text", text: "The user is greeting me." }, | | 101 | + { | | 102 | + type: "tool_reference", | | 103 | + tool: "web_search", | | 104 | + title: "Example result", | | 105 | + url: "https://example.com/tool", | | 106 | + favicon: "https://example.com/favicon.ico", | | 107 | + description: "Example description", | | 108 | + }, | | 109 | + { type: "reference", reference_ids: [1, "source-2"] }, | | 110 | + ], | | 111 | + closed: true, | | 112 | + signature: "sig-123", | | 113 | + }, | | 114 | + { type: "text", text: "Hi" }, | | 115 | + ], | | 116 | + }) | | 117 | + | | 118 | + await model.doGenerate({ | | 119 | + prompt: [ | | 120 | + { role: "user", content: [{ type: "text", text: "Hello" }] }, | | 121 | + { | | 122 | + role: "assistant", | | 123 | + content: [ | | 124 | + { type: "reasoning", text: "thinking" }, | | 125 | + { type: "text", text: "Hi" }, | | 126 | + ], | | 127 | + }, | | 128 | + { role: "user", content: [{ type: "text", text: "Hello again" }] }, | | 129 | + ], | | 130 | + }) | | 131 | + expect(body?.messages?.[1]).toEqual({ role: "assistant", content: "thinkingHi" }) | | 132 | +}) | | 133 | + | | 134 | +test("Mistral preserves native reasoning metadata while streaming", async () => { | | 135 | + const chunks = [ | | 136 | + { | | 137 | + id: "response-1", | | 138 | + created: 0, | | 139 | + model: "mistral-small-latest", | | 140 | + choices: [ | | 141 | + { | | 142 | + index: 0, | | 143 | + delta: { | | 144 | + role: "assistant", | | 145 | + content: [ | | 146 | + { | | 147 | + type: "thinking", | | 148 | + thinking: [ | | 149 | + { type: "text", text: "thinking" }, | | 150 | + { | | 151 | + type: "tool_reference", | | 152 | + tool: "web_search", | | 153 | + title: "Example result", | | 154 | + url: "https://example.com/tool", | | 155 | + favicon: "https://example.com/favicon.ico", | | 156 | + description: "Example description", | | 157 | + }, | | 158 | + ], | | 159 | + }, | | 160 | + ], | | 161 | + }, | | 162 | + }, | | 163 | + ], | | 164 | + }, | | 165 | + { | | 166 | + id: "response-1", | | 167 | + created: 0, | | 168 | + model: "mistral-small-latest", | | 169 | + choices: [ | | 170 | + { | | 171 | + index: 0, | | 172 | + delta: { | | 173 | + content: [ | | 174 | + { | | 175 | + type: "thinking", | | 176 | + thinking: [{ type: "reference", reference_ids: [1, "source-2"] }], | | 177 | + closed: true, | | 178 | + signature: "sig-123", | | 179 | + }, | | 180 | + ], | | 181 | + }, | | 182 | + }, | | 183 | + ], | | 184 | + }, | | 185 | + { | | 186 | + id: "response-1", | | 187 | + created: 0, | | 188 | + model: "mistral-small-latest", | | 189 | + choices: [{ index: 0, delta: { content: [{ type: "text", text: "answer" }] } }], | | 190 | + }, | | 191 | + { | | 192 | + id: "response-1", | | 193 | + created: 0, | | 194 | + model: "mistral-small-latest", | | 195 | + choices: [{ index: 0, delta: {}, finish_reason: "stop" }], | | 196 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | | 197 | + }, | | 198 | + ] | | 199 | + const mockFetch = Object.assign( | | 200 | + async () => | | 201 | + new Response(chunks.map((chunk) => `data: ${JSON.stringify(chunk)}\n\n`).join(""), { | | 202 | + headers: { "Content-Type": "text/event-stream" }, | | 203 | + }), | | 204 | + { preconnect: fetch.preconnect }, | | 205 | + ) | | 206 | + const model = createMistral({ apiKey: "test", fetch: mockFetch })("mistral-small-latest") | | 207 | + const result = await model.doStream({ | | 208 | + prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }], | | 209 | + }) | | 210 | + const events = [] | | 211 | + for await (const event of result.stream) events.push(event) | | 212 | + | | 213 | + expect(events.find((event) => event.type === "reasoning-end")?.providerMetadata).toEqual({ | | 214 | + mistral: { | | 215 | + thinking: { | | 216 | + type: "thinking", | | 217 | + thinking: [ | | 218 | + { type: "text", text: "thinking" }, | | 219 | + { | | 220 | + type: "tool_reference", | | 221 | + tool: "web_search", | | 222 | + title: "Example result", | | 223 | + url: "https://example.com/tool", | | 224 | + favicon: "https://example.com/favicon.ico", | | 225 | + description: "Example description", | | 226 | + }, | | 227 | + { type: "reference", reference_ids: [1, "source-2"] }, | | 228 | + ], | | 229 | + closed: true, | | 230 | + signature: "sig-123", | | 231 | + }, | | 232 | + }, | | 233 | + }) | | 234 | + expect( | | 235 | + events | | 236 | + .filter((event) => event.type === "reasoning-start" || event.type === "reasoning-delta") | | 237 | + .every((event) => event.providerMetadata === undefined), | | 238 | + ).toBe(true) | | 239 | +}) | | 240 | + | | 241 | +test("Mistral preserves metadata-only thinking chunks", async () => { | | 242 | + const thinking = { | | 243 | + type: "thinking" as const, | | 244 | + thinking: [ | | 245 | + { | | 246 | + type: "tool_reference", | | 247 | + tool: "web_search", | | 248 | + title: "Example result", | | 249 | + url: "https://example.com/tool", | | 250 | + favicon: "https://example.com/favicon.ico", | | 251 | + description: "Example description", | | 252 | + }, | | 253 | + { type: "reference", reference_ids: [1, "source-2"] }, | | 254 | + ], | | 255 | + closed: true, | | 256 | + signature: "sig-123", | | 257 | + } | | 258 | + const mockFetch = Object.assign( | | 259 | + async () => | | 260 | + Response.json({ | | 261 | + id: "response-1", | | 262 | + created: 0, | | 263 | + model: "mistral-small-latest", | | 264 | + object: "chat.completion", | | 265 | + choices: [{ index: 0, message: { role: "assistant", content: [thinking] }, finish_reason: "stop" }], | | 266 | + usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }, | | 267 | + }), | | 268 | + { preconnect: fetch.preconnect }, | | 269 | + ) | | 270 | + const model = createMistral({ apiKey: "test", fetch: mockFetch })("mistral-small-latest") | | 271 | + const result = await model.doGenerate({ | | 272 | + prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }], | | 273 | + }) | | 274 | + | | 275 | + expect(result.content).toEqual([ | | 276 | + { | | 277 | + type: "reasoning", | | 278 | + text: "", | | 279 | + providerMetadata: { mistral: { thinking } }, | | 280 | + }, | | 281 | + ]) | | 282 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 66 | 66 | "@ai-sdk/google": "3.0.73", | 67 | 67 | "@ai-sdk/google-vertex": "4.0.128", | 68 | 68 | "@ai-sdk/groq": "3.0.31", | 69 | | - "@ai-sdk/mistral": "3.0.34", | | 69 | + "@ai-sdk/mistral": "3.0.51", | 70 | 70 | "@ai-sdk/openai": "3.0.84", | 71 | 71 | "@ai-sdk/openai-compatible": "2.0.41", | 72 | 72 | "@ai-sdk/perplexity": "3.0.26", | |
|