xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / commit 9af6c03

fix: reasoning message issues

xonecas2026-02-19 9af6c03957e53f4cad881f10c125be3eacdf83b5 parent c28731c

2 files changed

cmd/test_models/main.go+33−1
@@ -12,7 +12,7 @@ import (
1212 )
1313
1414 // One representative model per endpoint type.
15-var probeModels = []string{"gpt-5.1", "claude-sonnet-4-6", "gemini-3-flash", "glm-5"}
15+var probeModels = []string{"gpt-5.1", "claude-sonnet-4-6", "gemini-3-flash", "kimi-k2-thinking"}
1616
1717 type testResult struct {
1818 Model string
@@ -55,6 +55,8 @@ func main() {
5555 {"UnifiedStreamNormalized", testUnifiedStreamNormalized},
5656 {"ToolHistory (normalized)", testToolHistoryNormalized},
5757 {"ToolHistory (stream)", testToolHistoryNormalizedStream},
58+ {"Reasoning (normalized)", testReasoningNormalized},
59+ {"Reasoning (stream)", testReasoningNormalizedStream},
5860 }
5961
6062 for _, s := range sections {
@@ -301,6 +303,36 @@ func testToolHistoryNormalizedStream(ctx context.Context, client *zen.Client, mo
301303 return drainUnifiedStream(modelID, "auto", string(reqBody), eventCh, errCh, err, start)
302304 }
303305
306+func testReasoningNormalized(ctx context.Context, client *zen.Client, modelID string) testResult {
307+ req := zen.NormalizedRequest{
308+ Model: modelID,
309+ Messages: []zen.NormalizedMessage{{Role: "user", Content: "What is 2 + 2? Think step by step."}},
310+ Reasoning: &zen.NormalizedReasoning{Effort: "low"},
311+ }
312+ start := time.Now()
313+ reqBody, _ := json.Marshal(req)
314+
315+ resp, err := client.UnifiedCreateNormalized(ctx, req)
316+ if err != nil {
317+ return makeResult(modelID, "auto", false, string(reqBody), "", err, time.Since(start))
318+ }
319+ return makeResult(modelID, string(resp.Endpoint), false, string(reqBody), truncate(string(resp.Body), 100), nil, time.Since(start))
320+}
321+
322+func testReasoningNormalizedStream(ctx context.Context, client *zen.Client, modelID string) testResult {
323+ req := zen.NormalizedRequest{
324+ Model: modelID,
325+ Messages: []zen.NormalizedMessage{{Role: "user", Content: "What is 2 + 2? Think step by step."}},
326+ Reasoning: &zen.NormalizedReasoning{Effort: "low"},
327+ Stream: true,
328+ }
329+ start := time.Now()
330+ reqBody, _ := json.Marshal(req)
331+
332+ eventCh, errCh, err := client.UnifiedStreamNormalized(ctx, req)
333+ return drainUnifiedStream(modelID, "auto", string(reqBody), eventCh, errCh, err, start)
334+}
335+
304336 // drainStream consumes a StreamEvent channel and builds a testResult.
305337 func drainStream(modelID, endpoint, reqBody string, eventCh <-chan zen.StreamEvent, errCh <-chan error, initErr error, start time.Time) testResult {
306338 if initErr != nil {
normalized.go+19−8
@@ -193,8 +193,25 @@ func (r NormalizedRequest) ToMessagesRequest() (*MessagesRequest, error) {
193193 // Anthropic's messages API requires max_tokens; apply a default when the
194194 // caller did not specify one so the normalized path works out of the box.
195195 maxTokens := r.MaxTokens
196+
197+ // Resolve the thinking budget early so we can ensure max_tokens > budget_tokens,
198+ // which is required by Anthropic's API.
199+ var thinkingBudget int
200+ if r.Reasoning != nil {
201+ thinkingBudget = r.Reasoning.BudgetTokens
202+ if thinkingBudget == 0 && r.Reasoning.Effort != "" {
203+ thinkingBudget = mapEffortToBudget(r.Reasoning.Effort)
204+ }
205+ }
206+
196207 if maxTokens == nil {
197208 defaultMax := 1024
209+ // When reasoning is active, max_tokens must be strictly greater than
210+ // budget_tokens. Default to twice the budget so there is room for the
211+ // actual response text.
212+ if thinkingBudget > 0 && thinkingBudget >= defaultMax {
213+ defaultMax = thinkingBudget * 2
214+ }
198215 maxTokens = &defaultMax
199216 }
200217
@@ -208,14 +225,8 @@ func (r NormalizedRequest) ToMessagesRequest() (*MessagesRequest, error) {
208225 Extra: r.Extra,
209226 }
210227
211- if r.Reasoning != nil {
212- budget := r.Reasoning.BudgetTokens
213- if budget == 0 && r.Reasoning.Effort != "" {
214- budget = mapEffortToBudget(r.Reasoning.Effort)
215- }
216- if budget > 0 {
217- req.Thinking = &AnthropicThinking{Type: "enabled", BudgetTokens: budget}
218- }
228+ if thinkingBudget > 0 {
229+ req.Thinking = &AnthropicThinking{Type: "enabled", BudgetTokens: thinkingBudget}
219230 }
220231
221232 if r.ToolChoice != nil && r.ToolChoice.Type == ToolChoiceNone {