xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / commit 1d803d2

fix: preserve gemini thought signatures across tool calls

xonecas2026-02-19 1d803d2b212f960ec7ba342860cba9efcff9a7d9 parent a03b10e

4 files changed

README.md+3−149
@@ -8,156 +8,9 @@ Minimal Go SDK for OpenCode Zen.
88 go get github.com/sacenox/go-opencode-ai-zen-sdk
99 ```
1010
11-## Agent Loop Example (tools + streaming)
11+## Examples
1212
13-```go
14-package main
15-
16-import (
17- "context"
18- "encoding/json"
19- "fmt"
20- "strings"
21- "time"
22-
23- zen "github.com/sacenox/go-opencode-ai-zen-sdk"
24-)
25-
26-type toolCall struct {
27- ID string
28- Name string
29- Args strings.Builder
30- Full string
31-}
32-
33-func main() {
34- client, err := zen.NewClient(zen.Config{APIKey: "your-key"})
35- if err != nil {
36- panic(err)
37- }
38-
39- tools := map[string]func(context.Context, json.RawMessage) (string, error){
40- "add": func(_ context.Context, args json.RawMessage) (string, error) {
41- var input struct {
42- A float64 `json:"a"`
43- B float64 `json:"b"`
44- }
45- if err := json.Unmarshal(args, &input); err != nil {
46- return "", err
47- }
48- return fmt.Sprintf("%.0f", input.A+input.B), nil
49- },
50- }
51-
52- messages := []zen.NormalizedMessage{{
53- Role: "user",
54- Content: "What is 3 + 4, then double it?",
55- }}
56-
57- maxSteps := 6
58- for step := 0; step < maxSteps; step++ {
59- req := zen.NormalizedRequest{
60- Model: "gpt-5.1",
61- System: "You are a precise assistant. Use tools when needed.",
62- Messages: messages,
63- Reasoning: &zen.NormalizedReasoning{Effort: "low"},
64- Tools: []zen.NormalizedTool{
65- {
66- Name: "add",
67- Description: "Adds two numbers",
68- Parameters: json.RawMessage(`{"type":"object","properties":{"a":{"type":"number"},"b":{"type":"number"}},"required":["a","b"]}`),
69- },
70- },
71- ToolChoice: &zen.NormalizedToolChoice{Type: zen.ToolChoiceAuto},
72- }
73-
74- ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
75- deltas, errs, err := client.Stream(ctx, req)
76- if err != nil {
77- cancel()
78- panic(err)
79- }
80-
81- var text strings.Builder
82- toolCalls := map[int]*toolCall{}
83- var ordered []*toolCall
84-
85- for d := range deltas {
86- switch d.Type {
87- case zen.DeltaText:
88- text.WriteString(d.Content)
89- case zen.DeltaToolCallBegin:
90- call := &toolCall{ID: d.ToolCallID, Name: d.ToolCallName}
91- toolCalls[d.ToolCallIndex] = call
92- ordered = append(ordered, call)
93- case zen.DeltaToolCallArgumentsDelta:
94- if call := toolCalls[d.ToolCallIndex]; call != nil {
95- call.Args.WriteString(d.ArgumentsDelta)
96- }
97- case zen.DeltaToolCallDone:
98- call := toolCalls[d.ToolCallIndex]
99- if call == nil {
100- call = &toolCall{}
101- toolCalls[d.ToolCallIndex] = call
102- ordered = append(ordered, call)
103- }
104- call.ID = d.ToolCallID
105- call.Name = d.ToolCallName
106- if d.ArgumentsFull != "" {
107- call.Full = d.ArgumentsFull
108- } else {
109- call.Full = call.Args.String()
110- }
111- }
112- }
113- if err := <-errs; err != nil {
114- cancel()
115- panic(err)
116- }
117- cancel()
118-
119- if len(ordered) == 0 {
120- messages = append(messages, zen.NormalizedMessage{
121- Role: "assistant",
122- Content: text.String(),
123- })
124- fmt.Println(text.String())
125- break
126- }
127-
128- assistant := zen.NormalizedMessage{Role: "assistant"}
129- for _, call := range ordered {
130- assistant.ToolCalls = append(assistant.ToolCalls, zen.NormalizedToolCall{
131- ID: call.ID,
132- Name: call.Name,
133- Arguments: json.RawMessage(call.Full),
134- })
135- }
136- messages = append(messages, assistant)
137-
138- for _, call := range ordered {
139- tool := tools[call.Name]
140- if tool == nil {
141- messages = append(messages, zen.NormalizedMessage{
142- Role: "tool",
143- ToolCallID: call.ID,
144- Content: fmt.Sprintf("unknown tool: %s", call.Name),
145- })
146- continue
147- }
148- result, err := tool(context.Background(), json.RawMessage(call.Full))
149- if err != nil {
150- result = "error: " + err.Error()
151- }
152- messages = append(messages, zen.NormalizedMessage{
153- Role: "tool",
154- ToolCallID: call.ID,
155- Content: result,
156- })
157- }
158- }
159-}
160-```
13+See `examples/` for runnable examples (agent loop, tools, streaming).
16114
16215 ## List Models
16316
@@ -194,3 +47,4 @@ This project is not affiliated with opencode.ai.
19447 that fires while the SSE body is still being read, causing spurious
19548 `context deadline exceeded (Client.Timeout ...)` errors. Use the request
19649 `context.Context` to enforce deadlines instead.
50+- **Gemini tool calls:** preserve `ThoughtSignature` across tool call → tool result turns.
examples/agent_loop/main.go+4−12
@@ -127,9 +127,10 @@ func runAgentLoop(client *zen.Client, model string, debugSSE bool, tools map[str
127127 calls[i].Name = toolName
128128 }
129129 assistant.ToolCalls = append(assistant.ToolCalls, zen.NormalizedToolCall{
130- ID: calls[i].ID,
131- Name: calls[i].Name,
132- Arguments: calls[i].Arguments,
130+ ID: calls[i].ID,
131+ Name: calls[i].Name,
132+ Arguments: calls[i].Arguments,
133+ ThoughtSignature: calls[i].ThoughtSignature,
133134 })
134135 }
135136 messages = append(messages, assistant)
@@ -244,14 +245,5 @@ func normalizeModelAlias(value string) string {
244245 model = strings.ReplaceAll(model, " ", "-")
245246 model = strings.ReplaceAll(model, "_", "-")
246247
247- switch model {
248- case "haiku", "claude-haiku", "claude-haiku-3-5", "claude-3-5-haiku", "claude-haiku-4-5":
249- return "claude-3-5-haiku"
250- case "gemini-3-pro", "gemini-3-pro-preview", "gemini-3.0-pro", "gemini-3-pro-preview-raw":
251- return "gemini-3-pro"
252- case "minimax2.5", "minimax-2.5", "minimax-m2.5":
253- return "minimax-m2.5"
254- }
255-
256248 return model
257249 }
parse.go+0−1
@@ -472,7 +472,6 @@ func parseGeminiDelta(ev UnifiedEvent) []NormalizedDelta {
472472 for i, part := range cand.Content.Parts {
473473 if part.FunctionCall != nil {
474474 callID := fmt.Sprintf("gemini-%d", i)
475- callID = encodeGeminiToolCallID(callID, part.ThoughtSignature)
476475 out = append(out, NormalizedDelta{
477476 Type: DeltaToolCallBegin,
478477 ToolCallIndex: i,
tool_calls.go+15−6
@@ -8,9 +8,10 @@ import (
88
99 // StreamToolCall is a fully assembled tool call extracted from a stream.
1010 type StreamToolCall struct {
11- ID string
12- Name string
13- Arguments json.RawMessage
11+ ID string
12+ Name string
13+ Arguments json.RawMessage
14+ ThoughtSignature string
1415 }
1516
1617 // ToolCallAccumulator stitches streaming tool call deltas into complete calls.
@@ -24,6 +25,7 @@ type toolCallState struct {
2425 index int
2526 id string
2627 name string
28+ sig string
2729 args strings.Builder
2830 full string
2931 }
@@ -51,6 +53,9 @@ func (a *ToolCallAccumulator) Apply(delta NormalizedDelta) bool {
5153 if call.name == "" {
5254 call.name = delta.ToolCallName
5355 }
56+ if call.sig == "" {
57+ call.sig = delta.ToolCallSignature
58+ }
5459 case DeltaToolCallArgumentsDelta:
5560 call.args.WriteString(delta.ArgumentsDelta)
5661 case DeltaToolCallDone:
@@ -60,6 +65,9 @@ func (a *ToolCallAccumulator) Apply(delta NormalizedDelta) bool {
6065 if call.name == "" {
6166 call.name = delta.ToolCallName
6267 }
68+ if call.sig == "" {
69+ call.sig = delta.ToolCallSignature
70+ }
6371 if delta.ArgumentsFull != "" {
6472 call.full = delta.ArgumentsFull
6573 }
@@ -89,9 +97,10 @@ func (a *ToolCallAccumulator) CompleteCalls() []StreamToolCall {
8997 full = call.args.String()
9098 }
9199 out = append(out, StreamToolCall{
92- ID: id,
93- Name: call.name,
94- Arguments: json.RawMessage(full),
100+ ID: id,
101+ Name: call.name,
102+ Arguments: json.RawMessage(full),
103+ ThoughtSignature: call.sig,
95104 })
96105 }
97106 return out