@@ -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.