xonecas.io
62159
Sy

symb / commit 2a58a39

fix: reasoning messages

xonecas2026-02-19 2a58a39d434805a6b104179a43894aa94194bd5a parent 1005b7d

5 files changed

go.mod+1−1
@@ -12,7 +12,7 @@ require (
1212 github.com/charmbracelet/x/exp/golden v0.0.0-20260209194814-eeb2896ac759
1313 github.com/charmbracelet/x/powernap v0.0.0-20260209132835-6b065b8ba62c
1414 github.com/rs/zerolog v1.34.0
15- github.com/sacenox/go-opencode-ai-zen-sdk v0.0.4-0.20260219161442-9af6c03957e5
15+ github.com/sacenox/go-opencode-ai-zen-sdk v0.0.6
1616 github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82
1717 modernc.org/sqlite v1.45.0
1818 mvdan.cc/sh/v3 v3.12.0
go.sum+2−2
@@ -92,8 +92,8 @@ github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7
9292 github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
9393 github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY=
9494 github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ=
95-github.com/sacenox/go-opencode-ai-zen-sdk v0.0.4-0.20260219161442-9af6c03957e5 h1:43b4qYr26VazOOP3Gnp8U2Ka747l7pFZGLo1qPeavZo=
96-github.com/sacenox/go-opencode-ai-zen-sdk v0.0.4-0.20260219161442-9af6c03957e5/go.mod h1:MK7Zno/U0BRk1/YDHKisc3ZXoQhcTyGnU9SbEuQ/c34=
95+github.com/sacenox/go-opencode-ai-zen-sdk v0.0.6 h1:ZBDAAHWeyBx8rtC8T3/xpLnPHPNnsL9fjKy4ujxeT78=
96+github.com/sacenox/go-opencode-ai-zen-sdk v0.0.6/go.mod h1:MK7Zno/U0BRk1/YDHKisc3ZXoQhcTyGnU9SbEuQ/c34=
9797 github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82 h1:6C8qej6f1bStuePVkLSFxoU22XBS165D3klxlzRg8F4=
9898 github.com/smacker/go-tree-sitter v0.0.0-20240827094217-dd81d9e9be82/go.mod h1:xe4pgH49k4SsmkQq5OT8abwhWmnzkhpgnXeekbx2efw=
9999 github.com/sourcegraph/jsonrpc2 v0.2.1 h1:2GtljixMQYUYCmIg7W9aF2dFmniq/mOr2T9tFRh6zSQ=
internal/llm/loop.go+15−12
@@ -307,10 +307,11 @@ func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provide
307307 if err != nil {
308308 // Add error result to history
309309 toolMsg := provider.Message{
310- Role: "tool",
311- Content: fmt.Sprintf("Error: %v", err),
312- ToolCallID: toolCall.ID,
313- CreatedAt: time.Now(),
310+ Role: "tool",
311+ Content: fmt.Sprintf("Error: %v", err),
312+ ToolCallID: toolCall.ID,
313+ FunctionName: toolCall.Name,
314+ CreatedAt: time.Now(),
314315 }
315316 if onMessage != nil {
316317 onMessage(toolMsg)
@@ -325,10 +326,11 @@ func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provide
325326
326327 // Add error result to history
327328 toolMsg := provider.Message{
328- Role: "tool",
329- Content: errText,
330- ToolCallID: toolCall.ID,
331- CreatedAt: time.Now(),
329+ Role: "tool",
330+ Content: errText,
331+ ToolCallID: toolCall.ID,
332+ FunctionName: toolCall.Name,
333+ CreatedAt: time.Now(),
332334 }
333335 if onMessage != nil {
334336 onMessage(toolMsg)
@@ -342,10 +344,11 @@ func executeToolCalls(ctx context.Context, proxy *mcp.Proxy, toolCalls []provide
342344
343345 // Add tool result to history
344346 toolMsg := provider.Message{
345- Role: "tool",
346- Content: resultText,
347- ToolCallID: toolCall.ID,
348- CreatedAt: time.Now(),
347+ Role: "tool",
348+ Content: resultText,
349+ ToolCallID: toolCall.ID,
350+ FunctionName: toolCall.Name,
351+ CreatedAt: time.Now(),
349352 }
350353 if onMessage != nil {
351354 onMessage(toolMsg)
internal/provider/provider.go+1−0
@@ -20,6 +20,7 @@ type Message struct {
2020 Reasoning string // Model reasoning/thinking content (optional)
2121 ToolCalls []ToolCall // For assistant messages with tool calls
2222 ToolCallID string // For tool result messages
23+ FunctionName string // For tool result messages: name of the called function (required by Gemini)
2324 CreatedAt time.Time // Message timestamp
2425 InputTokens int // Token usage for this LLM call (assistant messages only)
2526 OutputTokens int // Token usage for this LLM call (assistant messages only)
internal/provider/zen.go+92−356
@@ -41,358 +41,94 @@ func (p *ZenProvider) Name() string {
4141
4242 func (p *ZenProvider) ChatStream(ctx context.Context, messages []Message, tools []Tool) (<-chan StreamEvent, error) {
4343 system, rest := splitSystem(messages)
44+
4445 req := zen.NormalizedRequest{
45- Model: p.model,
46- System: system,
47- Messages: toZenMessages(rest),
48- Tools: toZenTools(tools),
49- Stream: true,
46+ Model: p.model,
47+ System: system,
48+ Messages: toZenMessages(rest),
49+ Tools: toZenTools(tools),
50+ Reasoning: &zen.NormalizedReasoning{Effort: "low"},
51+ ToolChoice: &zen.NormalizedToolChoice{Type: zen.ToolChoiceAuto},
5052 }
5153
5254 if p.temperature > 0 {
5355 req.Temperature = &p.temperature
5456 }
5557
56- maxTokens := 16000
57- req.MaxTokens = &maxTokens
58-
59- events, errs, err := p.client.UnifiedStreamNormalized(ctx, req)
58+ deltas, errs, err := p.client.Stream(ctx, req)
6059 if err != nil {
60+ var apiErr *zen.APIError
61+ if errors.As(err, &apiErr) {
62+ log.Error().
63+ Int("status", apiErr.StatusCode).
64+ Str("body", string(apiErr.Body)).
65+ Str("model", p.model).
66+ Msg("zen: Stream failed")
67+ }
6168 return nil, err
6269 }
6370
6471 ch := make(chan StreamEvent)
6572 go func() {
6673 defer close(ch)
67- for {
68- select {
69- case ev, ok := <-events:
70- if !ok {
71- return
72- }
73- if !p.emitEvent(ctx, ch, ev) {
74- return
75- }
76- case err, ok := <-errs:
77- if ok && err != nil {
78- var apiErr *zen.APIError
79- if errors.As(err, &apiErr) {
80- log.Error().
81- Int("status", apiErr.StatusCode).
82- Str("body", string(apiErr.Body)).
83- Msg("zen: stream API error")
84- }
85- trySend(ctx, ch, StreamEvent{Type: EventError, Err: err})
86- }
87- return
88- case <-ctx.Done():
89- return
90- }
91- }
92- }()
93-
94- return ch, nil
95-}
96-
97-func (p *ZenProvider) emitEvent(ctx context.Context, ch chan<- StreamEvent, ev zen.UnifiedEvent) bool {
98- data := ev.Data
99- if len(data) == 0 || string(data) == "[DONE]" {
100- return trySend(ctx, ch, StreamEvent{Type: EventDone})
101- }
102-
103- switch ev.Endpoint {
104- case zen.EndpointMessages:
105- return p.emitAnthropicEvent(ctx, ch, ev.Event, data)
106- case zen.EndpointModels:
107- return p.emitGeminiEvent(ctx, ch, data)
108- case zen.EndpointResponses:
109- return p.emitResponsesEvent(ctx, ch, ev.Event, data)
110- default:
111- return p.emitChatCompletionsEvent(ctx, ch, data)
112- }
113-}
114-
115-// emitChatCompletionsEvent handles OpenAI chat completions SSE chunks.
116-func (p *ZenProvider) emitChatCompletionsEvent(ctx context.Context, ch chan<- StreamEvent, data json.RawMessage) bool {
117- var chunk map[string]any
118- if err := json.Unmarshal(data, &chunk); err != nil {
119- return true
120- }
121-
122- if usage, ok := chunk["usage"].(map[string]any); ok {
123- if !trySend(ctx, ch, StreamEvent{
124- Type: EventUsage,
125- InputTokens: getIntOrZero(usage, "prompt_tokens"),
126- OutputTokens: getIntOrZero(usage, "completion_tokens"),
127- }) {
128- return false
129- }
130- }
131-
132- choices, _ := chunk["choices"].([]any)
133- if len(choices) == 0 {
134- delta, _ := chunk["delta"].(map[string]any)
135- if delta != nil {
136- return p.emitDelta(ctx, ch, delta)
137- }
138- return true
139- }
140-
141- choice, _ := choices[0].(map[string]any)
142- delta, _ := choice["delta"].(map[string]any)
143- if delta == nil {
144- return true
145- }
146-
147- return p.emitDelta(ctx, ch, delta)
148-}
149-
150-// emitAnthropicEvent handles Anthropic Messages SSE chunks.
151-// Relevant event types:
152-// - content_block_start: carries tool_use id/name
153-// - content_block_delta: text_delta or input_json_delta
154-// - message_delta: usage
155-func (p *ZenProvider) emitAnthropicEvent(ctx context.Context, ch chan<- StreamEvent, event string, data json.RawMessage) bool {
156- var chunk map[string]any
157- if err := json.Unmarshal(data, &chunk); err != nil {
158- return true
159- }
160-
161- switch event {
162- case "content_block_start":
163- cb, _ := chunk["content_block"].(map[string]any)
164- if getStringOrEmpty(cb, "type") == "tool_use" {
165- idx := getIntOrZero(chunk, "index")
166- if !trySend(ctx, ch, StreamEvent{
167- Type: EventToolCallBegin,
168- ToolCallIndex: idx,
169- ToolCallID: getStringOrEmpty(cb, "id"),
170- ToolCallName: getStringOrEmpty(cb, "name"),
171- }) {
172- return false
173- }
174- }
175-
176- case "content_block_delta":
177- idx := getIntOrZero(chunk, "index")
178- delta, _ := chunk["delta"].(map[string]any)
179- switch getStringOrEmpty(delta, "type") {
180- case "text_delta":
181- if text := getStringOrEmpty(delta, "text"); text != "" {
182- if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: text}) {
183- return false
184- }
185- }
186- case "thinking_delta":
187- if thinking := getStringOrEmpty(delta, "thinking"); thinking != "" {
188- if !trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: thinking}) {
189- return false
74+ for d := range deltas {
75+ var evt StreamEvent
76+ switch d.Type {
77+ case zen.DeltaText:
78+ evt = StreamEvent{Type: EventContentDelta, Content: d.Content}
79+ case zen.DeltaReasoning:
80+ evt = StreamEvent{Type: EventReasoningDelta, Content: d.Content}
81+ case zen.DeltaToolCallBegin:
82+ evt = StreamEvent{
83+ Type: EventToolCallBegin,
84+ ToolCallIndex: d.ToolCallIndex,
85+ ToolCallID: d.ToolCallID,
86+ ToolCallName: d.ToolCallName,
19087 }
191- }
192- case "input_json_delta":
193- if args := getStringOrEmpty(delta, "partial_json"); args != "" {
194- if !trySend(ctx, ch, StreamEvent{
88+ case zen.DeltaToolCallArgumentsDelta:
89+ evt = StreamEvent{
19590 Type: EventToolCallDelta,
196- ToolCallIndex: idx,
197- ToolCallArgs: args,
198- }) {
199- return false
91+ ToolCallIndex: d.ToolCallIndex,
92+ ToolCallArgs: d.ArgumentsDelta,
20093 }
201- }
202- }
203-
204- case "message_delta":
205- if usage, ok := chunk["usage"].(map[string]any); ok {
206- in := getIntOrZero(usage, "input_tokens")
207- out := getIntOrZero(usage, "output_tokens")
208- if in > 0 || out > 0 {
209- if !trySend(ctx, ch, StreamEvent{
94+ case zen.DeltaToolCallDone:
95+ // The accumulator in the caller handles final args; skip.
96+ continue
97+ case zen.DeltaUsage:
98+ evt = StreamEvent{
21099 Type: EventUsage,
211- InputTokens: in,
212- OutputTokens: out,
213- }) {
214- return false
100+ InputTokens: d.InputTokens,
101+ OutputTokens: d.OutputTokens,
215102 }
103+ case zen.DeltaDone:
104+ evt = StreamEvent{Type: EventDone}
105+ default:
106+ continue
216107 }
217- }
218- }
219-
220- return true
221-}
222-
223-// emitGeminiEvent handles Gemini SSE chunks.
224-// Each chunk has candidates[0].content.parts[].{text,functionCall}.
225-func (p *ZenProvider) emitGeminiEvent(ctx context.Context, ch chan<- StreamEvent, data json.RawMessage) bool {
226- var chunk map[string]any
227- if err := json.Unmarshal(data, &chunk); err != nil {
228- return true
229- }
230-
231- candidates, _ := chunk["candidates"].([]any)
232- if len(candidates) == 0 {
233- return true
234- }
235- candidate, _ := candidates[0].(map[string]any)
236- content, _ := candidate["content"].(map[string]any)
237- parts, _ := content["parts"].([]any)
238-
239- for idx, p2 := range parts {
240- part, _ := p2.(map[string]any)
241- if text := getStringOrEmpty(part, "text"); text != "" {
242- if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: text}) {
243- return false
244- }
245- }
246- if fc, ok := part["functionCall"].(map[string]any); ok {
247- name := getStringOrEmpty(fc, "name")
248- if name != "" {
249- if !trySend(ctx, ch, StreamEvent{
250- Type: EventToolCallBegin,
251- ToolCallIndex: idx,
252- ToolCallName: name,
253- }) {
254- return false
255- }
256- }
257- if args, ok := fc["args"]; ok {
258- argsJSON, err := json.Marshal(args)
259- if err == nil {
260- if !trySend(ctx, ch, StreamEvent{
261- Type: EventToolCallDelta,
262- ToolCallIndex: idx,
263- ToolCallArgs: string(argsJSON),
264- }) {
265- return false
108+ if !trySend(ctx, ch, evt) {
109+ // Context cancelled: drain the delta channel so SDK goroutines
110+ // can exit, then drain the error channel before returning.
111+ go func() {
112+ for range deltas {
266113 }
267- }
268- }
269- }
270- }
271-
272- if meta, ok := chunk["usageMetadata"].(map[string]any); ok {
273- in := getIntOrZero(meta, "promptTokenCount")
274- out := getIntOrZero(meta, "candidatesTokenCount")
275- if in > 0 || out > 0 {
276- if !trySend(ctx, ch, StreamEvent{
277- Type: EventUsage,
278- InputTokens: in,
279- OutputTokens: out,
280- }) {
281- return false
282- }
283- }
284- }
285-
286- return true
287-}
288-
289-// emitResponsesEvent handles OpenAI Responses API SSE chunks.
290-// Relevant event types: response.output_text.delta, response.function_call_arguments.delta,
291-// response.output_item.added (carries tool call id/name), response.completed (usage).
292-func (p *ZenProvider) emitResponsesEvent(ctx context.Context, ch chan<- StreamEvent, event string, data json.RawMessage) bool {
293- var chunk map[string]any
294- if err := json.Unmarshal(data, &chunk); err != nil {
295- return true
296- }
297-
298- switch event {
299- case "response.output_text.delta":
300- if delta := getStringOrEmpty(chunk, "delta"); delta != "" {
301- if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: delta}) {
302- return false
303- }
304- }
305-
306- case "response.output_item.added":
307- item, _ := chunk["item"].(map[string]any)
308- if getStringOrEmpty(item, "type") == "function_call" {
309- idx := getIntOrZero(chunk, "output_index")
310- if !trySend(ctx, ch, StreamEvent{
311- Type: EventToolCallBegin,
312- ToolCallIndex: idx,
313- ToolCallID: getStringOrEmpty(item, "call_id"),
314- ToolCallName: getStringOrEmpty(item, "name"),
315- }) {
316- return false
317- }
318- }
319-
320- case "response.function_call_arguments.delta":
321- idx := getIntOrZero(chunk, "output_index")
322- if delta := getStringOrEmpty(chunk, "delta"); delta != "" {
323- if !trySend(ctx, ch, StreamEvent{
324- Type: EventToolCallDelta,
325- ToolCallIndex: idx,
326- ToolCallArgs: delta,
327- }) {
328- return false
329- }
330- }
331-
332- case "response.completed":
333- resp, _ := chunk["response"].(map[string]any)
334- if usage, ok := resp["usage"].(map[string]any); ok {
335- if !trySend(ctx, ch, StreamEvent{
336- Type: EventUsage,
337- InputTokens: getIntOrZero(usage, "input_tokens"),
338- OutputTokens: getIntOrZero(usage, "output_tokens"),
339- }) {
340- return false
341- }
342- }
343- }
344-
345- return true
346-}
347-
348-func (p *ZenProvider) emitDelta(ctx context.Context, ch chan<- StreamEvent, delta map[string]any) bool {
349- if reasoning := getStringOrEmpty(delta, "reasoning"); reasoning != "" {
350- if !trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: reasoning}) {
351- return false
352- }
353- }
354- if reasoning := getStringOrEmpty(delta, "reasoning_content"); reasoning != "" {
355- if !trySend(ctx, ch, StreamEvent{Type: EventReasoningDelta, Content: reasoning}) {
356- return false
357- }
358- }
359- if content := getStringOrEmpty(delta, "content"); content != "" {
360- if !trySend(ctx, ch, StreamEvent{Type: EventContentDelta, Content: content}) {
361- return false
362- }
363- }
364-
365- toolCalls, _ := delta["tool_calls"].([]any)
366- for _, tc := range toolCalls {
367- toolCall, _ := tc.(map[string]any)
368- idx := getIntOrZero(toolCall, "index")
369- id := getStringOrEmpty(toolCall, "id")
370- fn, _ := toolCall["function"].(map[string]any)
371- name := getStringOrEmpty(fn, "name")
372- args := getStringOrEmpty(fn, "arguments")
373-
374- if name != "" {
375- if !trySend(ctx, ch, StreamEvent{
376- Type: EventToolCallBegin,
377- ToolCallIndex: idx,
378- ToolCallID: id,
379- ToolCallName: name,
380- }) {
381- return false
114+ <-errs
115+ }()
116+ return
382117 }
383118 }
384- if args != "" {
385- if !trySend(ctx, ch, StreamEvent{
386- Type: EventToolCallDelta,
387- ToolCallIndex: idx,
388- ToolCallArgs: args,
389- }) {
390- return false
119+ if err := <-errs; err != nil {
120+ var apiErr *zen.APIError
121+ if errors.As(err, &apiErr) {
122+ log.Error().
123+ Int("status", apiErr.StatusCode).
124+ Str("body", string(apiErr.Body)).
125+ Msg("zen: stream API error")
391126 }
127+ trySend(ctx, ch, StreamEvent{Type: EventError, Err: err})
392128 }
393- }
129+ }()
394130
395- return true
131+ return ch, nil
396132 }
397133
398134 func (p *ZenProvider) ListModels(ctx context.Context) ([]Model, error) {
@@ -404,9 +140,7 @@ func (p *ZenProvider) ListModels(ctx context.Context) ([]Model, error) {
404140
405141 models := make([]Model, len(resp.Data))
406142 for i, m := range resp.Data {
407- models[i] = Model{
408- Name: m.ID,
409- }
143+ models[i] = Model{Name: m.ID}
410144 }
411145 log.Debug().Int("count", len(models)).Msg("ListModels success")
412146 return models, nil
@@ -416,6 +150,8 @@ func (p *ZenProvider) Close() error {
416150 return nil
417151 }
418152
153+// splitSystem separates system/developer messages from the rest.
154+// The Zen SDK takes system content as a top-level field on NormalizedRequest.
419155 func splitSystem(messages []Message) (system string, rest []Message) {
420156 var parts []string
421157 for _, m := range messages {
@@ -434,9 +170,10 @@ func toZenMessages(messages []Message) []zen.NormalizedMessage {
434170 result := make([]zen.NormalizedMessage, len(messages))
435171 for i, m := range messages {
436172 nm := zen.NormalizedMessage{
437- Role: m.Role,
438- Content: m.Content,
439- ToolCallID: m.ToolCallID,
173+ Role: m.Role,
174+ Content: m.Content,
175+ ToolCallID: m.ToolCallID,
176+ FunctionName: m.FunctionName,
440177 }
441178 if len(m.ToolCalls) > 0 {
442179 nm.ToolCalls = make([]zen.NormalizedToolCall, len(m.ToolCalls))
@@ -461,7 +198,9 @@ func toZenTools(tools []Tool) []zen.NormalizedTool {
461198 for i, t := range tools {
462199 params := t.Parameters
463200 if len(params) == 0 {
464- params = json.RawMessage(`{"type":"object","properties":{}}`)
201+ params = []byte(`{"type":"object","properties":{}}`)
202+ } else {
203+ params = stripSchemaKeys(params)
465204 }
466205 result[i] = zen.NormalizedTool{
467206 Name: t.Name,
@@ -472,31 +211,29 @@ func toZenTools(tools []Tool) []zen.NormalizedTool {
472211 return result
473212 }
474213
475-func getStringOrEmpty(m map[string]any, key string) string {
476- if v, ok := m[key]; ok {
477- if s, ok := v.(string); ok {
478- return s
214+// stripSchemaKeys removes JSON Schema meta-keys that Gemini rejects as unknown
215+// fields ($schema, $id, $defs, definitions, additionalProperties).
216+// It operates on the top-level object only, which is where MCP tools place them.
217+func stripSchemaKeys(raw []byte) []byte {
218+ var m map[string]json.RawMessage
219+ if err := json.Unmarshal(raw, &m); err != nil {
220+ return raw // not a JSON object, return as-is
221+ }
222+ changed := false
223+ for _, key := range []string{"$schema", "$id", "$defs", "definitions", "additionalProperties"} {
224+ if _, ok := m[key]; ok {
225+ delete(m, key)
226+ changed = true
479227 }
480228 }
481- return ""
482-}
483-
484-func getIntOrZero(m map[string]any, key string) int {
485- if v, ok := m[key]; ok {
486- switch n := v.(type) {
487- case int:
488- return n
489- case int64:
490- return int(n)
491- case float64:
492- return int(n)
493- case json.Number:
494- if i, err := n.Int64(); err == nil {
495- return int(i)
496- }
497- }
229+ if !changed {
230+ return raw
231+ }
232+ out, err := json.Marshal(m)
233+ if err != nil {
234+ return raw
498235 }
499- return 0
236+ return out
500237 }
501238
502239 type ZenFactory struct {
@@ -526,7 +263,6 @@ func (f *ZenFactory) Create(model string, opts Options) Provider {
526263 Str("factory", f.name).
527264 Str("model", model).
528265 Bool("has_api_key", f.apiKey != "").
529- Int("api_key_len", len(f.apiKey)).
530266 Str("base_url", baseURL).
531267 Msg("ZenFactory.Create")