mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix(protocol): preserve explicit reasoning disable (#150)
This commit is contained in:
1 parent
56a789d560
commit
394acb5de3
4 files changed
+62
-23
No files matched your search
@@ -292,9 +292,9 @@ surfaces, so a forced level reaches a rung that the budget alone cannot express.
|
||||
}
|
||||
```
|
||||
|
||||
A level a client states explicitly always wins, so this only replaces a level
|
||||
nobody asked for: an absent one, or one derived from `thinking.budget_tokens`.
|
||||
Use `"none"` to remove reasoning from requests that do not ask for it.
|
||||
Explicit client disables remain disabled across protocol conversion: Anthropic
|
||||
`thinking.type="disabled"` and Chat/Responses effort `"none"` cannot be re-enabled
|
||||
by a configured default.
|
||||
|
||||
Levels are reported faithfully in both directions. An explicit
|
||||
`output_config.effort` survives conversion instead of being shadowed by a
|
||||
|
||||
+2
-3
@@ -290,9 +290,8 @@ Responses 近似表达,而强制指定的强度可以到达单独靠预算表
|
||||
}
|
||||
```
|
||||
|
||||
客户端显式指定的强度始终优先,因此它只替换无人指定的强度:完全未设置,
|
||||
或由 `thinking.budget_tokens` 推断出来的强度。设为 `"none"` 可为未请求思考的
|
||||
请求移除思考配置。
|
||||
客户端显式关闭始终优先。Anthropic 原生 `thinking.type="disabled"` 和 Chat/Responses 的 effort
|
||||
`"none"` 都会在协议转换后保持关闭,不会被配置的默认强度重新开启。
|
||||
|
||||
两个方向的强度都会如实保留:显式的 `output_config.effort` 不会再被 `thinking`
|
||||
块遮蔽,`budget_tokens` 保持其档位(`8192` 为 `high`,`32000` 为 `xhigh`,
|
||||
|
||||
+18
-10
@@ -243,22 +243,23 @@ func responsesTextFormat(format any) any {
|
||||
// output_config.effort threw the explicit effort away and reported the budget
|
||||
// bucket instead, so a client asking for "max" was served as "high".
|
||||
//
|
||||
// Without a stated level, an object of type "disabled" yields nil, one of type
|
||||
// "enabled"/"adaptive" the effortForThinkingBudget rung of its budget_tokens,
|
||||
// and any other object its "type" value as the level (nil when it has none). A
|
||||
// non-object value passes through unchanged, except that the strings "none"
|
||||
// and "disabled" yield nil.
|
||||
// Explicitly disabled reasoning remains distinct from an unset value so later
|
||||
// defaults cannot turn it back on.
|
||||
func reasoningEffort(value any) any {
|
||||
if object, ok := value.(map[string]any); ok {
|
||||
if jsonutil.StringAt(object, "type") == "disabled" {
|
||||
return "none"
|
||||
}
|
||||
if effort, ok := explicitEffort(object); ok {
|
||||
return effort
|
||||
}
|
||||
switch jsonutil.StringAt(object, "type") {
|
||||
case "disabled":
|
||||
return nil
|
||||
case "enabled", "adaptive":
|
||||
return effortForThinkingBudget(jsonutil.IntAt(object, "budget_tokens"))
|
||||
default:
|
||||
if effort, ok := normalizeEffortValue(object["type"]); ok {
|
||||
return effort
|
||||
}
|
||||
return object["type"]
|
||||
}
|
||||
}
|
||||
@@ -268,6 +269,12 @@ func reasoningEffort(value any) any {
|
||||
return value
|
||||
}
|
||||
|
||||
// reasoningDisabled reports whether a reasoning value explicitly disables it.
|
||||
func reasoningDisabled(value any) bool {
|
||||
effort, ok := reasoningEffort(value).(string)
|
||||
return ok && effort == "none"
|
||||
}
|
||||
|
||||
// explicitEffort reports the effort a client stated verbatim in a reasoning
|
||||
// object and whether the object carried one at all. It looks at "effort" first
|
||||
// and at "reasoning_effort" when "effort" states nothing; a key holding a blank
|
||||
@@ -302,9 +309,7 @@ func normalizeEffortValue(value any) (any, bool) {
|
||||
}
|
||||
switch strings.ToLower(strings.TrimSpace(text)) {
|
||||
case "none", "disabled":
|
||||
// Explicitly no reasoning: clear any inferred level instead of
|
||||
// resurrecting one from a budget.
|
||||
return nil, true
|
||||
return "none", true
|
||||
case "":
|
||||
return nil, false
|
||||
}
|
||||
@@ -330,6 +335,9 @@ func anthropicThinking(value any) (any, map[string]any) {
|
||||
if value == nil {
|
||||
return nil, nil
|
||||
}
|
||||
if reasoningDisabled(value) {
|
||||
return map[string]any{"type": "disabled"}, nil
|
||||
}
|
||||
budget := 0
|
||||
if object, ok := value.(map[string]any); ok {
|
||||
kind := jsonutil.StringAt(object, "type")
|
||||
|
||||
@@ -48,6 +48,9 @@ func ForcedEffort(protocol Protocol, body map[string]any, effort string) {
|
||||
if body == nil {
|
||||
return
|
||||
}
|
||||
if requestDisablesReasoning(protocol, body) {
|
||||
return
|
||||
}
|
||||
effort = strings.ToLower(strings.TrimSpace(effort))
|
||||
if !validForcedEffort(effort) {
|
||||
return
|
||||
@@ -85,6 +88,24 @@ func ForcedEffort(protocol Protocol, body map[string]any, effort string) {
|
||||
}
|
||||
}
|
||||
|
||||
func requestDisablesReasoning(protocol Protocol, body map[string]any) bool {
|
||||
switch protocol {
|
||||
case Chat:
|
||||
return reasoningDisabled(body["reasoning_effort"]) ||
|
||||
reasoningDisabled(body["reasoning"]) ||
|
||||
reasoningDisabled(body["thinking"])
|
||||
case Responses:
|
||||
return reasoningDisabled(jsonutil.AnyAt(body, "reasoning", "effort")) ||
|
||||
reasoningDisabled(body["reasoning_effort"])
|
||||
case Anthropic:
|
||||
return reasoningDisabled(body["thinking"]) ||
|
||||
reasoningDisabled(jsonutil.AnyAt(body, "output_config", "effort")) ||
|
||||
reasoningDisabled(body["effort"])
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// clientEffortExplicit reports whether an upstream body already carries a
|
||||
// level in its protocol's native slot, which a forced level must not replace.
|
||||
//
|
||||
@@ -355,12 +376,13 @@ func anthropicContentHasType(content []any, kind string) bool {
|
||||
// "none"/"disabled" decodes as no reasoning at all, even next to a thinking
|
||||
// block.
|
||||
func anthropicReasoning(input map[string]any) any {
|
||||
if reasoningDisabled(input["thinking"]) {
|
||||
return "none"
|
||||
}
|
||||
stated := jsonutil.FirstAny(jsonutil.AnyAt(input, "output_config", "effort"), input["effort"])
|
||||
if effort, ok := normalizeEffortValue(stated); ok {
|
||||
// The explicit effort survives next to the thinking block's budget so
|
||||
// nothing the client said is lost on the way to the bridge.
|
||||
if effort == nil {
|
||||
return nil
|
||||
if effort == "none" {
|
||||
return "none"
|
||||
}
|
||||
if thinking, ok := input["thinking"].(map[string]any); ok {
|
||||
merged := make(map[string]any, len(thinking)+1)
|
||||
@@ -396,6 +418,9 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
request.MaxTokens = jsonutil.FirstAny(input["max_completion_tokens"], input["max_tokens"])
|
||||
request.Stop = input["stop"]
|
||||
request.Reasoning = jsonutil.FirstAny(input["reasoning_effort"], input["reasoning"])
|
||||
if reasoningDisabled(input["thinking"]) {
|
||||
request.Reasoning = "none"
|
||||
}
|
||||
for i, raw := range jsonutil.SliceAt(input, "messages") {
|
||||
message, ok := raw.(map[string]any)
|
||||
if !ok {
|
||||
@@ -612,6 +637,9 @@ func decodeBridgeRequest(protocol Protocol, input map[string]any) (bridgeRequest
|
||||
default:
|
||||
return request, fmt.Errorf("unsupported input protocol %q", protocol)
|
||||
}
|
||||
if reasoningDisabled(request.Reasoning) {
|
||||
request.Reasoning = "none"
|
||||
}
|
||||
for i := range request.Messages {
|
||||
for j := range request.Messages[i].Blocks {
|
||||
block := &request.Messages[i].Blocks[j]
|
||||
@@ -722,7 +750,11 @@ func encodeChatRequest(request bridgeRequest) (map[string]any, error) {
|
||||
jsonutil.Put(output, "max_tokens", request.MaxTokens)
|
||||
jsonutil.Put(output, "stop", request.Stop)
|
||||
if effort := reasoningEffort(request.Reasoning); effort != nil {
|
||||
output["reasoning_effort"] = effort
|
||||
if effort == "none" {
|
||||
output["reasoning_effort"] = "none"
|
||||
} else {
|
||||
output["reasoning_effort"] = effort
|
||||
}
|
||||
}
|
||||
jsonutil.Put(output, "response_format", chatResponseFormat(request.ResponseFormat))
|
||||
jsonutil.Put(output, "parallel_tool_calls", request.ParallelToolCalls)
|
||||
@@ -961,8 +993,8 @@ func missingToolIDs(order []string, pending map[string]bool) []string {
|
||||
// level is forwarded unchanged, and a bare "none"/"disabled" string yields nil.
|
||||
func responsesReasoning(value any) any {
|
||||
if object, ok := value.(map[string]any); ok {
|
||||
if jsonutil.StringAt(object, "type") == "disabled" {
|
||||
return nil
|
||||
if reasoningDisabled(object) {
|
||||
return map[string]any{"effort": "none"}
|
||||
}
|
||||
// The object is reduced to the level reasoningEffort reads from it:
|
||||
// an "effort" or "reasoning_effort" the client stated, else the rung
|
||||
|
||||
Reference in new issue
Block a user