xonecas.io
6312
Ga

go-opencode-ai-zen-sdk / files

go-opencode-ai-zen-sdk / parse.go

15.3 KB
1package zen
2
3import (
4	"encoding/json"
5	"fmt"
6	"strings"
7)
8
9// NormalizedDeltaType identifies what kind of content a NormalizedDelta carries.
10type NormalizedDeltaType string
11
12const (
13	// DeltaText is a fragment of the assistant's visible reply.
14	DeltaText NormalizedDeltaType = "text"
15	// DeltaReasoning is a fragment of the model's reasoning / thinking output.
16	DeltaReasoning NormalizedDeltaType = "reasoning"
17	// DeltaToolCallBegin signals the start of a tool call (Name is set, ArgumentsDelta is empty).
18	DeltaToolCallBegin NormalizedDeltaType = "tool_call_begin"
19	// DeltaToolCallArgumentsDelta is an incremental JSON fragment of a tool call's arguments.
20	DeltaToolCallArgumentsDelta NormalizedDeltaType = "tool_call_arguments_delta"
21	// DeltaToolCallDone signals that a tool call is complete (ID, Name, Arguments all set).
22	DeltaToolCallDone NormalizedDeltaType = "tool_call_done"
23	// DeltaDone signals that the stream has finished (no content fields are set).
24	DeltaDone NormalizedDeltaType = "done"
25	// DeltaUsage carries token-count information (InputTokens / OutputTokens).
26	DeltaUsage NormalizedDeltaType = "usage"
27	// DeltaUnknown is emitted for events that carry no recognized content.
28	DeltaUnknown NormalizedDeltaType = "unknown"
29)
30
31// NormalizedDelta is a single parsed increment from a streaming response, endpoint-agnostic.
32type NormalizedDelta struct {
33	Type NormalizedDeltaType
34
35	// Text/Reasoning content (set for DeltaText and DeltaReasoning).
36	Content string
37
38	// Tool call fields.
39	ToolCallIndex     int    // index within this response (0-based)
40	ToolCallID        string // set on DeltaToolCallBegin / DeltaToolCallDone
41	ToolCallName      string // set on DeltaToolCallBegin / DeltaToolCallDone
42	ToolCallSignature string // set on DeltaToolCallBegin / DeltaToolCallDone when provided by provider
43	ArgumentsDelta    string // set on DeltaToolCallArgumentsDelta
44	ArgumentsFull     string // set on DeltaToolCallDone (fully accumulated)
45
46	// Usage fields (set for DeltaUsage).
47	InputTokens  int
48	OutputTokens int
49}
50
51// ParseNormalizedEvent parses a single UnifiedEvent into zero or more NormalizedDelta values.
52// Callers should call this for every event emitted by StreamEvents and accumulate
53// the results. Multiple deltas can be returned from a single event (e.g. Anthropic emits a
54// content_block_start followed by content_block_delta in separate events, but a Gemini chunk
55// may carry both a thought part and a text part).
56func ParseNormalizedEvent(ev UnifiedEvent) []NormalizedDelta {
57	if len(ev.Data) == 0 {
58		return nil
59	}
60
61	switch ev.Endpoint {
62	case EndpointChatCompletions:
63		return parseChatCompletionsDelta(ev)
64	case EndpointResponses:
65		return parseResponsesDelta(ev)
66	case EndpointMessages:
67		return parseMessagesDelta(ev)
68	case EndpointModels:
69		return parseGeminiDelta(ev)
70	default:
71		return nil
72	}
73}
74
75// ---------------------------------------------------------------------------
76// chat/completions
77// ---------------------------------------------------------------------------
78
79// chatCompletionChunk is the minimal shape of a streaming chat completion chunk.
80type chatCompletionChunk struct {
81	Choices []struct {
82		Delta struct {
83			Role             string `json:"role"`
84			Content          string `json:"content"`
85			ReasoningContent string `json:"reasoning_content"`
86			Reasoning        string `json:"reasoning"`
87			ReasoningDetails []struct {
88				Text string `json:"text"`
89			} `json:"reasoning_details"`
90			ToolCalls []struct {
91				Index    int    `json:"index"`
92				ID       string `json:"id"`
93				Type     string `json:"type"`
94				Function struct {
95					Name      string `json:"name"`
96					Arguments string `json:"arguments"`
97				} `json:"function"`
98			} `json:"tool_calls"`
99		} `json:"delta"`
100		FinishReason string `json:"finish_reason"`
101	} `json:"choices"`
102	Usage *struct {
103		PromptTokens     int `json:"prompt_tokens"`
104		CompletionTokens int `json:"completion_tokens"`
105	} `json:"usage"`
106}
107
108func parseChatCompletionsDelta(ev UnifiedEvent) []NormalizedDelta {
109	var chunk chatCompletionChunk
110	if err := json.Unmarshal(ev.Data, &chunk); err != nil {
111		return nil
112	}
113
114	// Some providers (e.g. those using stream_options: {include_usage: true})
115	// send a final chunk with an empty choices array that carries only usage.
116	// Check usage before the choices guard so it is not silently dropped.
117	var out []NormalizedDelta
118	if len(chunk.Choices) == 0 {
119		if chunk.Usage != nil && (chunk.Usage.PromptTokens > 0 || chunk.Usage.CompletionTokens > 0) {
120			return []NormalizedDelta{{
121				Type:         DeltaUsage,
122				InputTokens:  chunk.Usage.PromptTokens,
123				OutputTokens: chunk.Usage.CompletionTokens,
124			}}
125		}
126		return nil
127	}
128
129	delta := chunk.Choices[0].Delta
130
131	if delta.ReasoningContent != "" {
132		out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: delta.ReasoningContent})
133	}
134	if delta.Reasoning != "" {
135		out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: delta.Reasoning})
136	}
137	for _, detail := range delta.ReasoningDetails {
138		if detail.Text != "" {
139			out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: detail.Text})
140		}
141	}
142	if delta.Content != "" {
143		out = append(out, NormalizedDelta{Type: DeltaText, Content: delta.Content})
144	}
145	for _, tc := range delta.ToolCalls {
146		if tc.Function.Name != "" || tc.ID != "" {
147			out = append(out, NormalizedDelta{
148				Type:          DeltaToolCallBegin,
149				ToolCallIndex: tc.Index,
150				ToolCallID:    tc.ID,
151				ToolCallName:  tc.Function.Name,
152			})
153		}
154		if tc.Function.Arguments != "" {
155			out = append(out, NormalizedDelta{
156				Type:           DeltaToolCallArgumentsDelta,
157				ToolCallIndex:  tc.Index,
158				ArgumentsDelta: tc.Function.Arguments,
159			})
160		}
161	}
162
163	if chunk.Choices[0].FinishReason != "" {
164		if chunk.Usage != nil && (chunk.Usage.PromptTokens > 0 || chunk.Usage.CompletionTokens > 0) {
165			out = append(out, NormalizedDelta{
166				Type:         DeltaUsage,
167				InputTokens:  chunk.Usage.PromptTokens,
168				OutputTokens: chunk.Usage.CompletionTokens,
169			})
170		}
171		out = append(out, NormalizedDelta{Type: DeltaDone})
172	}
173
174	return out
175}
176
177// ---------------------------------------------------------------------------
178// responses (OpenAI Responses API)
179// ---------------------------------------------------------------------------
180
181// responsesEvent is the minimal shape of a typed Responses API SSE payload.
182type responsesEvent struct {
183	Type  string `json:"type"`
184	Delta string `json:"delta"`
185	// For tool call events.
186	Item struct {
187		Type   string `json:"type"`
188		ID     string `json:"id"`
189		CallID string `json:"call_id"`
190		Name   string `json:"name"`
191	} `json:"item"`
192	OutputIndex int    `json:"output_index"`
193	Name        string `json:"name"`
194	CallID      string `json:"call_id"`
195	ItemID      string `json:"item_id"`
196	Arguments   string `json:"arguments"`
197	// For response.completed / response.done events.
198	Response *struct {
199		Usage *struct {
200			InputTokens  int `json:"input_tokens"`
201			OutputTokens int `json:"output_tokens"`
202		} `json:"usage"`
203	} `json:"response"`
204}
205
206func parseResponsesDelta(ev UnifiedEvent) []NormalizedDelta {
207	var e responsesEvent
208	if err := json.Unmarshal(ev.Data, &e); err != nil {
209		return nil
210	}
211
212	switch e.Type {
213	case "response.output_text.delta":
214		if e.Delta != "" {
215			return []NormalizedDelta{{Type: DeltaText, Content: e.Delta}}
216		}
217	case "response.reasoning_summary_text.delta", "response.reasoning.delta", "response.reasoning_text.delta":
218		if e.Delta != "" {
219			return []NormalizedDelta{{Type: DeltaReasoning, Content: e.Delta}}
220		}
221	case "response.function_call_arguments_delta", "response.function_call_arguments.delta":
222		if e.Delta != "" {
223			return []NormalizedDelta{{
224				Type:           DeltaToolCallArgumentsDelta,
225				ToolCallIndex:  e.OutputIndex,
226				ArgumentsDelta: e.Delta,
227			}}
228		}
229	case "response.function_call_arguments_done", "response.function_call_arguments.done":
230		callID := e.CallID
231		if callID == "" {
232			callID = e.ItemID
233		}
234		return []NormalizedDelta{{
235			Type:          DeltaToolCallDone,
236			ToolCallIndex: e.OutputIndex,
237			ToolCallID:    callID,
238			ToolCallName:  e.Name,
239			ArgumentsFull: e.Arguments,
240		}}
241	case "response.output_item.added":
242		if e.Item.Type == "function_call" {
243			callID := e.Item.CallID
244			name := e.Item.Name
245			if callID == "" || name == "" {
246				fallbackCallID, fallbackName := extractResponsesFunctionCallItem(ev.Data)
247				if callID == "" {
248					callID = fallbackCallID
249				}
250				if name == "" {
251					name = fallbackName
252				}
253			}
254			if callID == "" {
255				callID = e.Item.ID
256			}
257			if name == "" {
258				name = e.Name
259			}
260			return []NormalizedDelta{{
261				Type:          DeltaToolCallBegin,
262				ToolCallIndex: e.OutputIndex,
263				ToolCallID:    callID,
264				ToolCallName:  name,
265			}}
266		}
267	case "response.completed", "response.done":
268		var out []NormalizedDelta
269		if e.Response != nil && e.Response.Usage != nil {
270			u := e.Response.Usage
271			if u.InputTokens > 0 || u.OutputTokens > 0 {
272				out = append(out, NormalizedDelta{
273					Type:         DeltaUsage,
274					InputTokens:  u.InputTokens,
275					OutputTokens: u.OutputTokens,
276				})
277			}
278		}
279		out = append(out, NormalizedDelta{Type: DeltaDone})
280		return out
281	}
282
283	return nil
284}
285
286func extractResponsesFunctionCallItem(raw json.RawMessage) (string, string) {
287	var root map[string]any
288	if err := json.Unmarshal(raw, &root); err != nil {
289		return "", ""
290	}
291	item, ok := root["item"].(map[string]any)
292	if !ok {
293		return "", ""
294	}
295	if t, ok := item["type"].(string); ok && t != "function_call" {
296		return "", ""
297	}
298
299	var callID string
300	if v, ok := item["call_id"].(string); ok {
301		callID = v
302	} else if v, ok := item["id"].(string); ok {
303		callID = v
304	}
305
306	var name string
307	if v, ok := item["name"].(string); ok {
308		name = v
309	} else if v, ok := item["tool_name"].(string); ok {
310		name = v
311	}
312
313	return callID, name
314}
315
316// ---------------------------------------------------------------------------
317// messages (Anthropic)
318// ---------------------------------------------------------------------------
319
320// anthropicStreamEvent is the minimal shape of an Anthropic SSE payload.
321type anthropicStreamEvent struct {
322	Type  string `json:"type"`
323	Index int    `json:"index"`
324	Delta struct {
325		Type     string `json:"type"`
326		Text     string `json:"text"`
327		Thinking string `json:"thinking"`
328		// For tool use input deltas.
329		PartialJSON string `json:"partial_json"`
330	} `json:"delta"`
331	ContentBlock struct {
332		Type  string `json:"type"`
333		ID    string `json:"id"`
334		Name  string `json:"name"`
335		Input string `json:"input"`
336	} `json:"content_block"`
337	// For message_start usage.
338	Message *struct {
339		Usage *struct {
340			InputTokens int `json:"input_tokens"`
341		} `json:"usage"`
342	} `json:"message"`
343	// For message_delta usage (top-level usage field).
344	Usage *struct {
345		OutputTokens int `json:"output_tokens"`
346	} `json:"usage"`
347}
348
349func parseMessagesDelta(ev UnifiedEvent) []NormalizedDelta {
350	// Anthropic uses the SSE "event:" line for the type, but also includes
351	// "type" in the JSON body. Both are supported.
352	var e anthropicStreamEvent
353	if err := json.Unmarshal(ev.Data, &e); err != nil {
354		return nil
355	}
356
357	// Prefer the JSON body "type" field; fall back to the SSE event name.
358	evType := e.Type
359	if evType == "" {
360		evType = ev.Event
361	}
362
363	switch evType {
364	case "message_start":
365		if e.Message != nil && e.Message.Usage != nil && e.Message.Usage.InputTokens > 0 {
366			return []NormalizedDelta{{
367				Type:        DeltaUsage,
368				InputTokens: e.Message.Usage.InputTokens,
369			}}
370		}
371	case "content_block_start":
372		if e.ContentBlock.Type == "tool_use" {
373			return []NormalizedDelta{{
374				Type:          DeltaToolCallBegin,
375				ToolCallIndex: e.Index,
376				ToolCallID:    e.ContentBlock.ID,
377				ToolCallName:  e.ContentBlock.Name,
378			}}
379		}
380	case "message_delta":
381		if e.Usage != nil && e.Usage.OutputTokens > 0 {
382			return []NormalizedDelta{{
383				Type:         DeltaUsage,
384				OutputTokens: e.Usage.OutputTokens,
385			}}
386		}
387	case "content_block_delta":
388		switch e.Delta.Type {
389		case "text_delta":
390			if e.Delta.Text != "" {
391				return []NormalizedDelta{{Type: DeltaText, Content: e.Delta.Text}}
392			}
393		case "thinking_delta":
394			if e.Delta.Thinking != "" {
395				return []NormalizedDelta{{Type: DeltaReasoning, Content: e.Delta.Thinking}}
396			}
397		case "input_json_delta":
398			if e.Delta.PartialJSON != "" {
399				return []NormalizedDelta{{
400					Type:           DeltaToolCallArgumentsDelta,
401					ToolCallIndex:  e.Index,
402					ArgumentsDelta: e.Delta.PartialJSON,
403				}}
404			}
405		}
406	case "content_block_stop":
407		// No content; signal completion only for tool_use blocks would require
408		// state from content_block_start. Consumers that need DeltaToolCallDone
409		// should accumulate themselves. We emit nothing here.
410	case "message_stop":
411		return []NormalizedDelta{{Type: DeltaDone}}
412	}
413
414	return nil
415}
416
417// ---------------------------------------------------------------------------
418// models (Gemini)
419// ---------------------------------------------------------------------------
420
421// geminiChunk is the minimal shape of a Gemini SSE chunk.
422type geminiChunk struct {
423	Candidates []struct {
424		Content struct {
425			Parts []struct {
426				Text             string    `json:"text"`
427				Thought          bool      `json:"thought"`
428				FunctionCall     *geminiFC `json:"functionCall,omitempty"`
429				ThoughtSignature string    `json:"thoughtSignature"`
430			} `json:"parts"`
431		} `json:"content"`
432		FinishReason string `json:"finishReason"`
433	} `json:"candidates"`
434	UsageMetadata *struct {
435		PromptTokenCount     int `json:"promptTokenCount"`
436		CandidatesTokenCount int `json:"candidatesTokenCount"`
437	} `json:"usageMetadata"`
438}
439
440type geminiFC struct {
441	Name string          `json:"name"`
442	Args json.RawMessage `json:"args"`
443}
444
445func parseGeminiDelta(ev UnifiedEvent) []NormalizedDelta {
446	var chunk geminiChunk
447	if err := json.Unmarshal(ev.Data, &chunk); err != nil {
448		return nil
449	}
450
451	// Emit usage first; some providers send a final candidate-less chunk that
452	// only carries usageMetadata.
453	var out []NormalizedDelta
454	if chunk.UsageMetadata != nil {
455		in := chunk.UsageMetadata.PromptTokenCount
456		outToks := chunk.UsageMetadata.CandidatesTokenCount
457		if in > 0 || outToks > 0 {
458			out = append(out, NormalizedDelta{
459				Type:         DeltaUsage,
460				InputTokens:  in,
461				OutputTokens: outToks,
462			})
463		}
464	}
465
466	if len(chunk.Candidates) == 0 {
467		return out
468	}
469
470	cand := chunk.Candidates[0]
471
472	for i, part := range cand.Content.Parts {
473		if part.FunctionCall != nil {
474			callID := fmt.Sprintf("gemini-%d", i)
475			out = append(out, NormalizedDelta{
476				Type:              DeltaToolCallBegin,
477				ToolCallIndex:     i,
478				ToolCallID:        callID,
479				ToolCallName:      part.FunctionCall.Name,
480				ToolCallSignature: part.ThoughtSignature,
481			})
482			if len(part.FunctionCall.Args) > 0 {
483				args := string(part.FunctionCall.Args)
484				out = append(out, NormalizedDelta{
485					Type:           DeltaToolCallArgumentsDelta,
486					ToolCallIndex:  i,
487					ToolCallID:     callID,
488					ArgumentsDelta: args,
489				})
490				out = append(out, NormalizedDelta{
491					Type:              DeltaToolCallDone,
492					ToolCallIndex:     i,
493					ToolCallID:        callID,
494					ToolCallName:      part.FunctionCall.Name,
495					ToolCallSignature: part.ThoughtSignature,
496					ArgumentsFull:     args,
497				})
498			}
499			continue
500		}
501		text := strings.TrimRight(part.Text, "")
502		if text == "" {
503			continue
504		}
505		if part.Thought {
506			out = append(out, NormalizedDelta{Type: DeltaReasoning, Content: text})
507		} else {
508			out = append(out, NormalizedDelta{Type: DeltaText, Content: text})
509		}
510	}
511
512	if cand.FinishReason != "" && cand.FinishReason != "FINISH_REASON_UNSPECIFIED" {
513		out = append(out, NormalizedDelta{Type: DeltaDone})
514	}
515
516	return out
517}