Repository navigation
Expand file tree
/
Copy pathevents.go
More file actions
134 lines (122 loc) · 5.24 KB
/
Copy pathevents.go
File metadata and controls
134 lines (122 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright (c) 2026 Matthew Winter
//
// This source code is licensed under the MIT license found in the LICENSE file
// in the root directory of this source tree.
package sigma
// EventKind identifies the kind of provider-neutral streaming event.
type EventKind string
const (
// EventKindStart marks the beginning of a stream.
EventKindStart EventKind = "start"
// EventKindTextStart marks the beginning of a text content block.
EventKindTextStart EventKind = "text_start"
// EventKindTextDelta carries text appended to a text content block.
EventKindTextDelta EventKind = "text_delta"
// EventKindTextEnd marks the end of a text content block.
EventKindTextEnd EventKind = "text_end"
// EventKindThinkingStart marks the beginning of a thinking content block.
EventKindThinkingStart EventKind = "thinking_start"
// EventKindThinkingDelta carries text appended to a thinking content block.
EventKindThinkingDelta EventKind = "thinking_delta"
// EventKindThinkingEnd marks the end of a thinking content block.
EventKindThinkingEnd EventKind = "thinking_end"
// EventKindToolCallStart marks the beginning of a tool-call content block.
EventKindToolCallStart EventKind = "toolcall_start"
// EventKindToolCallDelta carries partial tool-call data.
EventKindToolCallDelta EventKind = "toolcall_delta"
// EventKindToolCallEnd marks the end of a tool-call content block.
EventKindToolCallEnd EventKind = "toolcall_end"
// EventKindImageStart marks the beginning of an image content block.
EventKindImageStart EventKind = "image_start"
// EventKindImageDelta carries partial image content.
EventKindImageDelta EventKind = "image_delta"
// EventKindImageEnd marks the end of an image content block.
EventKindImageEnd EventKind = "image_end"
// EventKindDone marks the successful end of a stream.
EventKindDone EventKind = "done"
// EventKindError marks a stream error.
EventKindError EventKind = "error"
)
// PartialToolCall describes an in-progress tool-call update.
// Stream delivery copies JSON-compatible metadata containers so consumer
// mutations cannot change provider state or later events.
type PartialToolCall struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
ArgumentsDelta string `json:"argumentsDelta,omitempty"`
ProviderSignature string `json:"providerSignature,omitempty"`
ProviderMetadata map[string]any `json:"providerMetadata,omitempty"`
}
// Event is a provider-neutral text-generation stream event.
//
// Content block events may be interleaved. Consumers must route text,
// thinking, and tool-call updates by ContentIndex instead of assuming events
// arrive as a single sequential output buffer.
// Incremental ToolCall and PartialToolCall payloads own their JSON-compatible
// arguments and metadata independently of provider state and terminal content.
// Opaque Go objects remain caller-owned.
//
// Typical consumers switch on Kind:
//
// switch event.Kind {
// case sigma.EventKindTextDelta:
// handleTextDelta(event.ContentIndex, event.DeltaText)
// case sigma.EventKindToolCallEnd:
// handleToolCall(event.ContentIndex, event.ToolCall)
// case sigma.EventKindDone, sigma.EventKindError:
// finish(event)
// }
type Event struct {
Kind EventKind `json:"kind"`
ContentIndex *int `json:"contentIndex,omitempty"`
DeltaText string `json:"deltaText,omitempty"`
Text string `json:"text,omitempty"`
Thinking string `json:"thinking,omitempty"`
Image *ContentBlock `json:"image,omitempty"`
PartialImage *ContentBlock `json:"partialImage,omitempty"`
ToolCall *ToolCall `json:"toolCall,omitempty"`
PartialToolCall *PartialToolCall `json:"partialToolCall,omitempty"`
PartialMessage *AssistantMessage `json:"partialMessage,omitempty"`
FinalMessage *AssistantMessage `json:"finalMessage,omitempty"`
Usage *Usage `json:"usage,omitempty"`
StopReason StopReason `json:"stopReason,omitempty"`
Error string `json:"error,omitempty"`
}
// IsTerminal reports whether kind ends a stream.
func (kind EventKind) IsTerminal() bool {
return kind == EventKindDone || kind == EventKindError
}
// IsDelta reports whether kind carries an incremental content update.
func (kind EventKind) IsDelta() bool {
switch kind {
case EventKindTextDelta, EventKindThinkingDelta, EventKindToolCallDelta, EventKindImageDelta:
return true
default:
return false
}
}
// IsStart reports whether kind starts a stream or content block.
func (kind EventKind) IsStart() bool {
switch kind {
case EventKindStart, EventKindTextStart, EventKindThinkingStart, EventKindToolCallStart, EventKindImageStart:
return true
default:
return false
}
}
// String returns the event kind string.
func (kind EventKind) String() string {
return string(kind)
}
// IsTerminal reports whether event ends a stream.
func (event Event) IsTerminal() bool {
return event.Kind.IsTerminal()
}
// IsDelta reports whether event carries an incremental content update.
func (event Event) IsDelta() bool {
return event.Kind.IsDelta()
}
// IsStart reports whether event starts a stream or content block.
func (event Event) IsStart() bool {
return event.Kind.IsStart()
}