Files
sub2api/backend/internal/service/claude55_thinking.go
T
Anankke f42dad9b71 fix(composite): adapt thinking to the Claude 5.5 model a route rewrites to
A composite route that rewrites the request model left the client's thinking untouched, so a route from claude-sonnet-5 to claude-opus-5-5 forwarded thinking.type=enabled with budget_tokens and validateClaude55Request answered 400 before the request reached the upstream.

The route middleware now rewrites the model through RewriteCompositeRequestModel, which replaces a thinking mode that validateClaude55Request rejects for the new model with the nearest mode it accepts: enabled becomes adaptive without budget_tokens, and disabled becomes between_tools on Sonnet 5.5.

The mapping is shared with the Bedrock Sonnet 5.5 sanitizer, whose behavior is unchanged.

Modes the target still rejects stay with validateClaude55Request, and a request whose model the route does not rewrite keeps its thinking as sent.
2026-11-16 05:48:26 -08:00

28 lines
884 B
Go

package service
import (
"github.com/Wei-Shaw/sub2api/internal/pkg/claude"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
// adaptThinkingToClaude55 replaces a thinking mode that a Claude 5.5 model
// rejects with the nearest mode it accepts: enabled becomes adaptive without
// budget_tokens, and on Sonnet 5.5 disabled becomes between_tools. Other modes
// are left for validateClaude55Request to judge.
func adaptThinkingToClaude55(body []byte, model string) []byte {
if !isClaude55SignedThinkingModel(model) {
return body
}
switch gjson.GetBytes(body, "thinking.type").String() {
case "enabled":
body, _ = sjson.SetBytes(body, "thinking.type", "adaptive")
body, _ = sjson.DeleteBytes(body, "thinking.budget_tokens")
case "disabled":
if claude.IsSonnet55(model) {
body, _ = sjson.SetBytes(body, "thinking.type", "between_tools")
}
}
return body
}