mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-17 05:26:47 +03:00
fix(ai): drop reasoning details when the model declares another field (#64243)
Co-authored-by: MrMushrooooom <19261047+MrMushrooooom@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
Unverified
parent
e4606463e7
commit
07965844a5
@@ -577,6 +577,9 @@ const lowerAssistantMessage = Effect.fnUntraced(function* (
|
||||
const opaque = reasoningOpaque(reasoning, options.providerMetadataKey)
|
||||
const reasoningText =
|
||||
reasoning.length === 0 || (opaque !== undefined && text === "") ? (requireReasoning ? "" : undefined) : text
|
||||
// A model that declares a different reasoning field reads reasoning there, so structured details and opaque values
|
||||
// from earlier turns (including another model served under the same provider metadata key) must not be replayed.
|
||||
const replaysStructured = configuredField === undefined || configuredField === "reasoning_details"
|
||||
const result = {
|
||||
role: "assistant" as const,
|
||||
content: (() => {
|
||||
@@ -585,10 +588,11 @@ const lowerAssistantMessage = Effect.fnUntraced(function* (
|
||||
return content.map((part) => part.text).join("")
|
||||
})(),
|
||||
...(toolCalls.length > 0 ? { tool_calls: toolCalls } : {}),
|
||||
...(details !== undefined ? { reasoning_details: details } : {}),
|
||||
...(opaque !== undefined ? { reasoning_opaque: opaque } : {}),
|
||||
...(details !== undefined && replaysStructured ? { reasoning_details: details } : {}),
|
||||
...(opaque !== undefined && replaysStructured ? { reasoning_opaque: opaque } : {}),
|
||||
}
|
||||
if (field === undefined || reasoningText === undefined) return result
|
||||
// `reasoning_details` is the structured list itself, so scalar text must not be written over it.
|
||||
if (field === undefined || reasoningText === undefined || field === "reasoning_details") return result
|
||||
return { ...result, [field]: reasoningText }
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { describe, expect } from "bun:test"
|
||||
import { Effect } from "effect"
|
||||
import { ai, LanguageModel, LLMEvent, LLMResponse } from "../../src/effect.js"
|
||||
import { ai, LLMEvent, LLMResponse } from "../../src/effect.js"
|
||||
import * as OpenAICompatible from "../../src/providers/openai-compatible.js"
|
||||
import * as OpenRouter from "../../src/providers/openrouter.js"
|
||||
import { LLMClient } from "../../src/route.js"
|
||||
@@ -11,28 +11,22 @@ import { expectWeatherToolLoop, goldenWeatherToolLoopRequest, runWeatherToolLoop
|
||||
const cases = [
|
||||
{
|
||||
name: "OpenRouter",
|
||||
model: LanguageModel.update(
|
||||
OpenRouter.configure({
|
||||
apiKey: process.env.OPENROUTER_API_KEY ?? "fixture",
|
||||
providerOptions: { reasoning: { max_tokens: 1024 } },
|
||||
}).model("anthropic/claude-sonnet-4.6"),
|
||||
{ compatibility: { reasoningField: "reasoning" } },
|
||||
),
|
||||
model: OpenRouter.configure({
|
||||
apiKey: process.env.OPENROUTER_API_KEY ?? "fixture",
|
||||
providerOptions: { reasoning: { max_tokens: 1024 } },
|
||||
}).model("anthropic/claude-sonnet-4.6"),
|
||||
requires: ["OPENROUTER_API_KEY"],
|
||||
cassette: "openrouter-reasoning",
|
||||
structured: true,
|
||||
},
|
||||
{
|
||||
name: "Vercel AI Gateway",
|
||||
model: LanguageModel.update(
|
||||
OpenAICompatible.configure({
|
||||
provider: "vercel-ai-gateway",
|
||||
baseURL: "https://ai-gateway.vercel.sh/v1",
|
||||
apiKey: process.env.AI_GATEWAY_API_KEY ?? "fixture",
|
||||
http: { body: { reasoning: { enabled: true, max_tokens: 1024 } } },
|
||||
}).model("anthropic/claude-sonnet-4.6"),
|
||||
{ compatibility: { reasoningField: "reasoning" } },
|
||||
),
|
||||
model: OpenAICompatible.configure({
|
||||
provider: "vercel-ai-gateway",
|
||||
baseURL: "https://ai-gateway.vercel.sh/v1",
|
||||
apiKey: process.env.AI_GATEWAY_API_KEY ?? "fixture",
|
||||
http: { body: { reasoning: { enabled: true, max_tokens: 1024 } } },
|
||||
}).model("anthropic/claude-sonnet-4.6"),
|
||||
requires: ["AI_GATEWAY_API_KEY"],
|
||||
cassette: "vercel-ai-gateway-reasoning",
|
||||
structured: true,
|
||||
|
||||
@@ -1247,6 +1247,85 @@ describe("OpenAI Chat route", () => {
|
||||
}),
|
||||
)
|
||||
|
||||
// A mid-session model switch can keep the same provider metadata key while the new model reads reasoning from a
|
||||
// declared field of its own; details replayed from the earlier model are refused by some endpoints.
|
||||
it.effect("does not replay reasoning details when the model declares another reasoning field", () =>
|
||||
Effect.gen(function* () {
|
||||
const details = [
|
||||
{ type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
|
||||
]
|
||||
const replay = yield* compileRequest(
|
||||
ai.request({
|
||||
model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_content" } }),
|
||||
messages: [
|
||||
Message.assistant([
|
||||
{
|
||||
type: "reasoning",
|
||||
text: "thinking",
|
||||
providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
|
||||
},
|
||||
{ type: "text", text: "Hello" },
|
||||
]),
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_content: "thinking" }])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("keeps replayed reasoning details when the model declares reasoning_details", () =>
|
||||
Effect.gen(function* () {
|
||||
const details = [
|
||||
{ type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
|
||||
]
|
||||
const replay = yield* compileRequest(
|
||||
ai.request({
|
||||
model: LanguageModel.update(model, { compatibility: { reasoningField: "reasoning_details" } }),
|
||||
messages: [
|
||||
Message.assistant([
|
||||
{
|
||||
type: "reasoning",
|
||||
text: "thinking",
|
||||
providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
|
||||
},
|
||||
{ type: "text", text: "Hello" },
|
||||
]),
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
expect(replay.body.messages).toEqual([{ role: "assistant", content: "Hello", reasoning_details: details }])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("keeps replayed reasoning details when the model declares no reasoning field", () =>
|
||||
Effect.gen(function* () {
|
||||
const details = [
|
||||
{ type: "reasoning.text", text: "thinking", signature: "signed", format: "anthropic-claude-v1", index: 0 },
|
||||
]
|
||||
const replay = yield* compileRequest(
|
||||
ai.request({
|
||||
model,
|
||||
messages: [
|
||||
Message.assistant([
|
||||
{
|
||||
type: "reasoning",
|
||||
text: "thinking",
|
||||
providerMetadata: { openai: { reasoningField: "reasoning", reasoningDetails: details } },
|
||||
},
|
||||
{ type: "text", text: "Hello" },
|
||||
]),
|
||||
],
|
||||
}),
|
||||
)
|
||||
|
||||
expect(replay.body.messages).toEqual([
|
||||
{ role: "assistant", content: "Hello", reasoning: "thinking", reasoning_details: details },
|
||||
])
|
||||
}),
|
||||
)
|
||||
|
||||
it.effect("reports the cost the endpoint billed", () =>
|
||||
Effect.gen(function* () {
|
||||
const response = yield* LLMClient.generate(request).pipe(
|
||||
|
||||
Reference in New Issue
Block a user