Skip to content

Commit 3f39a32

Browse files
authored
feat(opencode): tolerate Anthropic thinking block binding (anomalyco#46653)
1 parent dc8753f commit 3f39a32

9 files changed

Lines changed: 934 additions & 24 deletions

File tree

‎bun.lock‎

Lines changed: 11 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
"electron"
138138
],
139139
"overrides": {
140+
"@ai-sdk/anthropic": "3.0.111",
140141
"@opentui/core": "catalog:",
141142
"@opentui/keymap": "catalog:",
142143
"@opentui/solid": "catalog:",
@@ -160,6 +161,8 @@
160161
161162
"@tanstack/[email protected]": "patches/@tanstack%[email protected]",
162163
"@ai-sdk/[email protected]": "patches/@ai-sdk%[email protected]",
163-
"@ai-sdk/[email protected]": "patches/@ai-sdk%[email protected]"
164+
"@ai-sdk/[email protected]": "patches/@ai-sdk%[email protected]",
165+
"@ai-sdk/[email protected]": "patches/@ai-sdk%[email protected]",
166+
"@ai-sdk/[email protected]": "patches/@ai-sdk%[email protected]"
164167
}
165168
}

‎packages/core/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"dependencies": {
6464
"@ai-sdk/alibaba": "1.0.17",
6565
"@ai-sdk/amazon-bedrock": "4.0.166",
66-
"@ai-sdk/anthropic": "3.0.82",
66+
"@ai-sdk/anthropic": "3.0.111",
6767
"@ai-sdk/azure": "3.0.88",
6868
"@ai-sdk/cerebras": "2.0.41",
6969
"@ai-sdk/cohere": "3.0.27",

‎packages/opencode/package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
"@agentclientprotocol/sdk": "0.21.0",
5858
"@ai-sdk/alibaba": "1.0.17",
5959
"@ai-sdk/amazon-bedrock": "4.0.166",
60-
"@ai-sdk/anthropic": "3.0.82",
60+
"@ai-sdk/anthropic": "3.0.111",
6161
"@ai-sdk/azure": "3.0.88",
6262
"@ai-sdk/cerebras": "2.0.60",
6363
"@ai-sdk/cohere": "3.0.27",

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,6 +684,41 @@ function anthropicOmitsThinking(apiId: string) {
684684
return anthropicUsesModernAdaptiveThinking(apiId)
685685
}
686686

687+
// Opus 5, Sonnet 5, Fable 5.x, and Mythos 5.x think without a `thinking` parameter.
688+
function anthropicThinksByDefault(apiId: string) {
689+
const version = /claude-(?:[a-z]+-)?(\d+)(?:[.-](\d{1,2}))?(?:[.@-]|$)/i.exec(apiId)
690+
if (!version) return false
691+
return Number(version[1]) >= 5
692+
}
693+
694+
// Fable 5.1 binds each thinking signature to the system prompt, tool list, and
695+
// messages above it, and rejects the request when any of that changes. opencode
696+
// re-renders parts of that prefix between turns (system prompt, tools, compaction),
697+
// so ask the API to drop the affected blocks instead of failing the request.
698+
// Models that do not run the check accept the field, so it is safe on every Claude.
699+
// The patched AI SDK adds the thinking-binding-controls beta whenever it is set.
700+
const ANTHROPIC_BLOCK_BINDING = { prefixMismatchBehavior: "drop_block" }
701+
702+
function anthropicBlockBinding(model: Provider.Model, options: { [x: string]: any }) {
703+
if (!model.api.id.toLowerCase().includes("claude")) return options
704+
const byDefault = anthropicThinksByDefault(model.api.id)
705+
switch (model.api.npm) {
706+
case "@ai-sdk/anthropic":
707+
case "@ai-sdk/google-vertex/anthropic": {
708+
const thinking = options.thinking ?? (byDefault ? { type: "adaptive" } : undefined)
709+
if (!thinking || (thinking.type !== "adaptive" && thinking.type !== "enabled")) return options
710+
return { ...options, thinking: { ...thinking, blockBinding: ANTHROPIC_BLOCK_BINDING } }
711+
}
712+
case "@ai-sdk/amazon-bedrock": {
713+
const reasoningConfig = options.reasoningConfig ?? (byDefault ? { type: "adaptive" } : undefined)
714+
if (!reasoningConfig || (reasoningConfig.type !== "adaptive" && reasoningConfig.type !== "enabled"))
715+
return options
716+
return { ...options, reasoningConfig: { ...reasoningConfig, blockBinding: ANTHROPIC_BLOCK_BINDING } }
717+
}
718+
}
719+
return options
720+
}
721+
687722
function googleThinkingLevelEfforts(apiId: string) {
688723
const id = apiId.toLowerCase()
689724
if (!id.includes("gemini-3")) return ["low", "high"]
@@ -1363,7 +1398,7 @@ export function providerOptions(model: Provider.Model, options: { [x: string]: a
13631398
usesOpenAIReasoningGate &&
13641399
(model.capabilities.reasoning || options.reasoningEffort !== undefined || options.reasoningSummary !== undefined)
13651400
? { ...options, forceReasoning: true }
1366-
: options
1401+
: anthropicBlockBinding(model, options)
13671402

13681403
if (model.api.npm === "@ai-sdk/gateway") {
13691404
// Gateway providerOptions are split across two namespaces:

‎packages/opencode/src/session/processor.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,20 @@ const layer = Layer.effect(
435435
case "step-finish": {
436436
const completedSnapshot = yield* snapshot.track()
437437
yield* Effect.forEach(Object.keys(ctx.reasoningMap), finishReasoning)
438+
// Anthropic reports thinking blocks it removed before the model saw the
439+
// prompt. Prefix mismatches mean opencode changed history behind a signed
440+
// block; log them so the churn can be tracked down.
441+
const dropped = isRecord(value.providerMetadata?.anthropic)
442+
? value.providerMetadata.anthropic.inputTransformations
443+
: undefined
444+
if (Array.isArray(dropped) && dropped.length > 0) {
445+
yield* Effect.logWarning("thinking blocks dropped by provider", {
446+
sessionID: ctx.sessionID,
447+
messageID: ctx.assistantMessage.id,
448+
model: ctx.model.id,
449+
transformations: JSON.stringify(dropped),
450+
})
451+
}
438452
const usage = Session.getUsage({
439453
model: ctx.model,
440454
usage: value.usage ?? new Usage({}),

0 commit comments

Comments
 (0)