This commit is contained in:
yetone
2026-11-12 12:45:39 +08:00
parent 6cd6b37651
commit f1e6705c52
5 changed files with 50 additions and 5 deletions
+23 -4
View File
@@ -299,15 +299,34 @@ func readAnthropic(body []byte, clientMarks, facts bool) (*Request, error) {
} else if th != nil && (th.Type == "enabled" || th.Type == "adaptive") {
r.Thinking = true
r.Effort = effortOfBudget(th.BudgetTokens)
if oc := a.OutputConfig; oc != nil {
if e := effortOf(oc.Effort); e != "" {
r.Effort = e
}
if oc := a.OutputConfig; oc != nil && effortOf(oc.Effort) != "" {
r.Effort = effortOf(oc.Effort)
} else if e := turnEffort(body); e != "" {
// the level of the turn, kept in a message of its own (Claude
// Desktop's and Claude Code's {role: "system"} message with an
// output_config), is the one picked; the budget beside it is
// only nearest to it (a High asked as 31999 read as Max)
r.Effort = e
}
}
return r, nil
}
// turnEffort is the last effort a message of an Anthropic request carries
// in its own output_config; "" when none does.
func turnEffort(body []byte) string {
if !bytes.Contains(body, []byte(`"output_config"`)) {
return ""
}
last := ""
for _, e := range gjson.GetBytes(body, "messages.#.output_config.effort").Array() {
if v := effortOf(e.String()); v != "" {
last = v
}
}
return last
}
// thinkingOffUnlessAsked says thinking is off when the request doesn't
// mention it. That is what Anthropic's API assumes, but DeepSeek and other
// vendors' Anthropic endpoints think by default, so Claude Code's requests
@@ -62,6 +62,9 @@ done
{"messages budget", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":9000,"thinking":{"type":"enabled","budget_tokens":20000},"messages":[{"role":"user","content":"a1"}]}`, "--effort high"},
{"messages adaptive", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":9000,"thinking":{"type":"adaptive","display":"omitted"},"output_config":{"effort":"xhigh"},"messages":[{"role":"user","content":"a2"}]}`, "--effort xhigh"},
{"messages turn effort", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":9000,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"a3"},{"role":"system","content":[],"output_config":{"effort":"max"}}]}`, "--effort max"},
{"turn effort over budget", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":31999},"messages":[{"role":"user","content":"a4"},{"role":"system","content":[],"output_config":{"effort":"high"}}]}`, "--effort high"},
{"top effort over budget", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":40000,"thinking":{"type":"enabled","budget_tokens":31999},"output_config":{"effort":"high"},"messages":[{"role":"user","content":"a5"}]}`, "--effort high"},
{"thinking summaries on", provider.Anthropic, "/v1/messages", `{"model":"claude-opus-5-5","max_tokens":9000,"thinking":{"type":"adaptive"},"messages":[{"role":"user","content":"a6"}]}`, `"showThinkingSummaries":true`},
{"chat reasoning object", provider.Chat, "/v1/chat/completions", `{"model":"claude-opus-5-5","reasoning":{"effort":"high"},"messages":[{"role":"user","content":"b3"}]}`, "--effort high"},
{"chat output_config", provider.Chat, "/v1/chat/completions", `{"model":"claude-opus-5-5","output_config":{"effort":"max"},"messages":[{"role":"user","content":"b4"}]}`, "--effort max"},
{"chat thinking budget", provider.Chat, "/v1/chat/completions", `{"model":"claude-opus-5-5","thinking":{"type":"enabled","budget_tokens":8000},"messages":[{"role":"user","content":"b5"}]}`, "--effort medium"},
@@ -75,3 +78,16 @@ done
}
}
}
// TestTurnEffortBeatsBudget: a High carried in the turn's own message is the
// level routing reads too, not the budget beside it (DIYgod on X, #5379).
func TestTurnEffortBeatsBudget(t *testing.T) {
body := []byte(`{"model":"m","thinking":{"type":"enabled","budget_tokens":31999},"messages":[{"role":"user","content":"x"},{"role":"system","content":[],"output_config":{"effort":"high"}}]}`)
if got := requestEffort(provider.Anthropic, body); got != "high" {
t.Errorf("requestEffort = %q, want high", got)
}
r, err := parseAnthropicRequest(body, false)
if err != nil || r.Effort != "high" {
t.Errorf("parsed effort = %q (%v), want high", r.Effort, err)
}
}
+1 -1
View File
@@ -3676,7 +3676,7 @@ func claudeCLIArgs(model, mcpConfig, effort string, web bool) []string {
"-p", "--output-format", "stream-json", "--input-format", "stream-json",
"--include-partial-messages", "--verbose", "--model", model,
"--tools", own, "--strict-mcp-config", "--mcp-config", mcpConfig,
"--setting-sources", "", "--settings", disableDeepLinkSettings, "--dangerously-skip-permissions",
"--setting-sources", "", "--settings", claudeRunSettings, "--dangerously-skip-permissions",
// the run's history lives in the process; start keeps it on disk
// where a turn after the run is let go can go on with it
// (savedSession), in the folder all runs share
+7
View File
@@ -506,6 +506,13 @@ func claudeResult(b []byte) (claudeRun, bool) {
// read.
const disableDeepLinkSettings = `{"disableDeepLinkRegistration":"disable"}`
// claudeRunSettings is a run's settings: no deep link, and the thinking
// shown as a summary. -p has --thinking-display, but the interactive mode
// (interactiveArgs drops that flag) shows none unless showThinkingSummaries
// is on, so a client saw no thinking at all (DIYgod on X, #5379). It costs
// no quota.
const claudeRunSettings = `{"disableDeepLinkRegistration":"disable","showThinkingSummaries":true}`
func claudeUsageArgs() []string {
return []string{"-p", "/usage", "--output-format", "json",
// verbose: the array of every message, whose usage_report holds
+3
View File
@@ -751,6 +751,9 @@ func requestEffort(proto provider.Protocol, body []byte) string {
if e := effortOf(v.OutputConfig.Effort); e != "" {
return e
}
if e := turnEffort(body); e != "" {
return e
}
return effortOfBudget(v.Thinking.Budget)
}