@@ -2,6 +2,7 @@ package zen
22
33 import (
44 "encoding/json"
5+ "fmt"
56 "strings"
67 )
78
@@ -33,11 +34,12 @@ type NormalizedDelta struct {
3334 Content string
3435
3536 // Tool call fields.
36- ToolCallIndex int // index within this response (0-based)
37- ToolCallID string // set on DeltaToolCallBegin / DeltaToolCallDone
38- ToolCallName string // set on DeltaToolCallBegin / DeltaToolCallDone
39- ArgumentsDelta string // set on DeltaToolCallArgumentsDelta
40- ArgumentsFull string // set on DeltaToolCallDone (fully accumulated)
37+ ToolCallIndex int // index within this response (0-based)
38+ ToolCallID string // set on DeltaToolCallBegin / DeltaToolCallDone
39+ ToolCallName string // set on DeltaToolCallBegin / DeltaToolCallDone
40+ ToolCallSignature string // set on DeltaToolCallBegin / DeltaToolCallDone when provided by provider
41+ ArgumentsDelta string // set on DeltaToolCallArgumentsDelta
42+ ArgumentsFull string // set on DeltaToolCallDone (fully accumulated)
4143 }
4244
4345 // ParseNormalizedEvent parses a single UnifiedEvent into zero or more NormalizedDelta values.
@@ -75,7 +77,11 @@ type chatCompletionChunk struct {
7577 Role string `json:"role"`
7678 Content string `json:"content"`
7779 ReasoningContent string `json:"reasoning_content"`
78- ToolCalls []struct {
80+ Reasoning string `json:"reasoning"`
81+ ReasoningDetails []struct {
82+ Text string `json:"text"`
83+ } `json:"reasoning_details"`
84+ ToolCalls []struct {
7985 Index int `json:"index"`
8086 ID string `json:"id"`
8187 Type string `json:"type"`
@@ -104,6 +110,14 @@ func parseChatCompletionsDelta(ev UnifiedEvent) []NormalizedDelta {
104110 if delta.ReasoningContent != "" {
105111 out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: delta.ReasoningContent})
106112 }
113+ if delta.Reasoning != "" {
114+ out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: delta.Reasoning})
115+ }
116+ for _, detail := range delta.ReasoningDetails {
117+ if detail.Text != "" {
118+ out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: detail.Text})
119+ }
120+ }
107121 if delta.Content != "" {
108122 out = append(out, NormalizedDelta{Type: DeltaText, Content: delta.Content})
109123 }
@@ -142,13 +156,15 @@ type responsesEvent struct {
142156 Delta string `json:"delta"`
143157 // For tool call events.
144158 Item struct {
145- Type string `json:"type"`
146- ID string `json:"id"`
147- Name string `json:"name"`
159+ Type string `json:"type"`
160+ ID string `json:"id"`
161+ CallID string `json:"call_id"`
162+ Name string `json:"name"`
148163 } `json:"item"`
149164 OutputIndex int `json:"output_index"`
150165 Name string `json:"name"`
151166 CallID string `json:"call_id"`
167+ ItemID string `json:"item_id"`
152168 Arguments string `json:"arguments"`
153169 }
154170
@@ -167,7 +183,7 @@ func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
167183 if e.Delta != "" {
168184 return []NormalizedDelta{{Type: DeltaReasoning, Content: e.Delta}}
169185 }
170- case "response.function_call_arguments_delta":
186+ case "response.function_call_arguments_delta", "response.function_call_arguments.delta":
171187 if e.Delta != "" {
172188 return []NormalizedDelta{{
173189 Type: DeltaToolCallArgumentsDelta,
@@ -175,21 +191,42 @@ func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
175191 ArgumentsDelta: e.Delta,
176192 }}
177193 }
178- case "response.function_call_arguments_done":
194+ case "response.function_call_arguments_done", "response.function_call_arguments.done":
195+ callID := e.CallID
196+ if callID == "" {
197+ callID = e.ItemID
198+ }
179199 return []NormalizedDelta{{
180200 Type: DeltaToolCallDone,
181201 ToolCallIndex: e.OutputIndex,
182- ToolCallID: e.CallID,
202+ ToolCallID: callID,
183203 ToolCallName: e.Name,
184204 ArgumentsFull: e.Arguments,
185205 }}
186206 case "response.output_item.added":
187207 if e.Item.Type == "function_call" {
208+ callID := e.Item.CallID
209+ name := e.Item.Name
210+ if callID == "" || name == "" {
211+ fallbackCallID, fallbackName := extractResponsesFunctionCallItem(ev.Data)
212+ if callID == "" {
213+ callID = fallbackCallID
214+ }
215+ if name == "" {
216+ name = fallbackName
217+ }
218+ }
219+ if callID == "" {
220+ callID = e.Item.ID
221+ }
222+ if name == "" {
223+ name = e.Name
224+ }
188225 return []NormalizedDelta{{
189226 Type: DeltaToolCallBegin,
190227 ToolCallIndex: e.OutputIndex,
191- ToolCallID: e.Item.ID,
192- ToolCallName: e.Item.Name,
228+ ToolCallID: callID,
229+ ToolCallName: name,
193230 }}
194231 }
195232 case "response.completed", "response.done":
@@ -199,6 +236,36 @@ func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
199236 return nil
200237 }
201238
239+func extractResponsesFunctionCallItem(raw json.RawMessage) (string, string) {
240+ var root map[string]any
241+ if err := json.Unmarshal(raw, &root); err != nil {
242+ return "", ""
243+ }
244+ item, ok := root["item"].(map[string]any)
245+ if !ok {
246+ return "", ""
247+ }
248+ if t, ok := item["type"].(string); ok && t != "function_call" {
249+ return "", ""
250+ }
251+
252+ var callID string
253+ if v, ok := item["call_id"].(string); ok {
254+ callID = v
255+ } else if v, ok := item["id"].(string); ok {
256+ callID = v
257+ }
258+
259+ var name string
260+ if v, ok := item["name"].(string); ok {
261+ name = v
262+ } else if v, ok := item["tool_name"].(string); ok {
263+ name = v
264+ }
265+
266+ return callID, name
267+}
268+
202269 // ---------------------------------------------------------------------------
203270 // messages (Anthropic)
204271 // ---------------------------------------------------------------------------
@@ -285,9 +352,10 @@ type geminiChunk struct {
285352 Candidates []struct {
286353 Content struct {
287354 Parts []struct {
288- Text string `json:"text"`
289- Thought bool `json:"thought"`
290- FunctionCall *geminiFC `json:"functionCall,omitempty"`
355+ Text string `json:"text"`
356+ Thought bool `json:"thought"`
357+ FunctionCall *geminiFC `json:"functionCall,omitempty"`
358+ ThoughtSignature string `json:"thoughtSignature"`
291359 } `json:"parts"`
292360 } `json:"content"`
293361 FinishReason string `json:"finishReason"`
@@ -313,16 +381,30 @@ func parseGeminiDelta(ev UnifiedEvent) []NormalizedDelta {
313381
314382 for i, part := range cand.Content.Parts {
315383 if part.FunctionCall != nil {
384+ callID := fmt.Sprintf("gemini-%d", i)
385+ callID = encodeGeminiToolCallID(callID, part.ThoughtSignature)
316386 out = append(out, NormalizedDelta{
317- Type: DeltaToolCallBegin,
318- ToolCallIndex: i,
319- ToolCallName: part.FunctionCall.Name,
387+ Type: DeltaToolCallBegin,
388+ ToolCallIndex: i,
389+ ToolCallID: callID,
390+ ToolCallName: part.FunctionCall.Name,
391+ ToolCallSignature: part.ThoughtSignature,
320392 })
321393 if len(part.FunctionCall.Args) > 0 {
394+ args := string(part.FunctionCall.Args)
322395 out = append(out, NormalizedDelta{
323396 Type: DeltaToolCallArgumentsDelta,
324397 ToolCallIndex: i,
325- ArgumentsDelta: string(part.FunctionCall.Args),
398+ ToolCallID: callID,
399+ ArgumentsDelta: args,
400+ })
401+ out = append(out, NormalizedDelta{
402+ Type: DeltaToolCallDone,
403+ ToolCallIndex: i,
404+ ToolCallID: callID,
405+ ToolCallName: part.FunctionCall.Name,
406+ ToolCallSignature: part.ThoughtSignature,
407+ ArgumentsFull: args,
326408 })
327409 }
328410 continue