Files
CLIProxyAPI/internal/runtime/executor/helps/claude_tool_choice.go
Luis Pater 6e6afab99a refactor: migrate gjson, sjson, and base64 to MoeForge/moego
- Replace tidwall/gjson and tidwall/sjson with github.com/MoeForge/moego/ojson
  and encoding/base64 with github.com/MoeForge/moego/base64 across the main
  module and examples
- Introduce cross-method *ojson.Doc request rewriting pipelines across
  thinking, signature, client/codex, runtime/executor/helps, pluginhost, and
  all provider executors with FinalizePayloadDoc as the final semantic barrier
- Convert all consecutive JSON mutations (>= 2 Set/SetRaw/Delete operations)
  to batch edits on ojson.Doc
- Upgrade read-only byte paths to ojson.GetBytesNoCopy,
  ojson.GetManyBytesNoCopy, and util.ParseGJSONBytesNoCopy with explicit
  strings.Clone ownership protection across streaming and persistent state
- Use base64.StdEncoding.ValidateString for zero-allocation signature checks
  and base64.EncodeDataURL for single-allocation data URL construction
2026-11-03 03:05:04 +08:00

44 lines
1.4 KiB
Go

package helps
import (
"strings"
"github.com/MoeForge/moego/ojson"
"github.com/router-for-me/CLIProxyAPI/v8/internal/thinking"
"github.com/router-for-me/CLIProxyAPI/v8/internal/util"
)
// NormalizeClaudeToolChoice downgrades forced tool use on Claude 5.5 to auto.
// Use the routed upstream model, not the payload model, and run before thinking
// preparation so forced-mode cleanup cannot discard thinking or signed history.
// Delegated providers keep their own tool-choice contract. User payload rules
// must run afterwards and remain authoritative.
func NormalizeClaudeToolChoice(provider, model string, payload []byte) []byte {
if len(payload) == 0 || provider != "claude" {
return payload
}
doc := ojson.NewDoc(payload)
if !NormalizeClaudeToolChoiceDoc(provider, model, doc) {
return payload
}
return doc.Bytes()
}
// NormalizeClaudeToolChoiceDoc downgrades forced tool use on Claude 5.5 to auto in place on doc.
func NormalizeClaudeToolChoiceDoc(provider, model string, doc *ojson.Doc) bool {
if doc == nil || doc.Len() == 0 || provider != "claude" {
return false
}
model = strings.ToLower(thinking.ParseSuffix(strings.TrimSpace(model)).ModelName)
if !claude55Model.MatchString(model) {
return false
}
switch doc.Get("tool_choice.type").String() {
case "any", "tool":
_ = doc.Set("tool_choice.type", "auto")
_ = util.DeleteJSONDocIfExists(doc, "tool_choice.name")
return true
}
return false
}