-
Notifications
You must be signed in to change notification settings - Fork 3
/
message_sse_decoder.go
261 lines (232 loc) · 6.75 KB
/
message_sse_decoder.go
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package anthrogo
import (
"bufio"
"encoding/json"
"fmt"
"io"
"strings"
)
// MessageEventPayload is the decoded event from anthropic
type MessageEventPayload struct {
Event string
Data EventData
}
// EventData contains content which will be whatever the model output
// and Data which is the full data from the event
type EventData struct {
Content string
Data any
}
// MessageStart is one of the data types for events and it represents the start of a
// a stream of messages. It contains metadata about the request.
type MessageStart struct {
Type string `json:"type"`
Message struct {
ID string `json:"id"`
Type string `json:"type"`
Role string `json:"role"`
Content []string `json:"content"`
Model string `json:"model"`
StopReason string `json:"stop_reason"`
StopSequence string `json:"stop_sequence"`
Usage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
} `json:"message"`
}
// ContentBlockStart marks the start of a new content block in the message stream.
type ContentBlockStart struct {
Type string `json:"type"`
Index int `json:"index"`
ContentBlock struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content_block"`
}
// PingData is a ping event
type PingData struct {
Type string `json:"type"`
}
// ContentBlockDelta carries new content for a content block in the message stream.
type ContentBlockDelta struct {
Type string `json:"type"`
Index int `json:"index"`
Delta struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"delta"`
}
// ContentBlockStop marks the end of a content block in the message stream.
type ContentBlockStop struct {
Type string `json:"type"`
Index int `json:"index"`
}
// MessageDelta events indicate top-level changes to the final message.
type MessageDelta struct {
Type string `json:"type"`
Delta interface{} `json:"delta"`
Usage struct {
OutputTokens int `json:"output_tokens"`
} `json:"usage"`
}
// MessageStopData is the final event in a message stream.
type MessageStopData struct {
Type string `json:"type"`
}
// ErrorData is the event type for errors.
type ErrorData struct {
Type string `json:"type"`
Error struct {
Type string `json:"type"`
Message string `json:"message"`
} `json:"error"`
}
// MessageEvent is the event type for messages. It contains the message payload
// and an error if one occurred.
type MessageEvent struct {
Message *MessageEventPayload
Err *error
}
// MessageSSEDecoder is a decoder for the SSE stream from the message endpoint.
type MessageSSEDecoder struct {
reader *bufio.Reader
content []string
}
// DecodeOptions are options for decoding the SSE stream.
type DecodeOptions struct {
ContentOnly bool
}
// NewMessageSSEDecoder creates a new MessageSSEDecoder.
func NewMessageSSEDecoder(reader io.Reader) *MessageSSEDecoder {
return &MessageSSEDecoder{
reader: bufio.NewReader(reader),
content: make([]string, 0),
}
}
// Decode reads the next event from the SSE stream.
func (d *MessageSSEDecoder) Decode(opts ...DecodeOptions) (*MessageEventPayload, error) {
var options DecodeOptions
if len(opts) > 1 {
return nil, fmt.Errorf("too many options provided, expected at most one")
} else if len(opts) == 1 {
options = opts[0]
}
line, err := d.reader.ReadString('\n')
if err != nil {
if err == io.EOF {
return nil, nil
}
return nil, err
}
line = strings.TrimSpace(line)
if line == "" {
// Recursively call Decode to read the next event
return d.Decode(opts...)
}
parts := strings.SplitN(line, ":", 2)
if len(parts) != 2 {
return nil, fmt.Errorf("invalid SSE format")
}
field := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
if field == "event" {
data, err := d.decodeData(value)
if err != nil {
return nil, err
}
if data.Content != "" || !options.ContentOnly || value == "message_stop" {
return &MessageEventPayload{
Event: value,
Data: data,
}, nil
}
}
// Recursively call Decode to read the next event if we didn't have one here
return d.Decode(opts...)
}
func (d *MessageSSEDecoder) decodeData(event string) (EventData, error) {
var eventData EventData
for {
line, err := d.reader.ReadString('\n')
if err != nil {
break
}
line = strings.TrimSpace(line)
if line == "" {
break
}
if strings.HasPrefix(line, "data:") {
jsonData := strings.TrimSpace(line[5:])
switch event {
case "message_start":
var messageStartData MessageStart
err := json.Unmarshal([]byte(jsonData), &messageStartData)
if err != nil {
return eventData, err
}
eventData.Data = messageStartData
case "content_block_start":
var contentBlockStartData ContentBlockStart
err := json.Unmarshal([]byte(jsonData), &contentBlockStartData)
if err != nil {
return eventData, err
}
eventData.Data = contentBlockStartData
eventData.Content = contentBlockStartData.ContentBlock.Text
d.updateContent(contentBlockStartData.Index, contentBlockStartData.ContentBlock.Text)
case "ping":
var pingData PingData
err := json.Unmarshal([]byte(jsonData), &pingData)
if err != nil {
return eventData, err
}
eventData.Data = pingData
case "content_block_delta":
var contentBlockDeltaData ContentBlockDelta
err := json.Unmarshal([]byte(jsonData), &contentBlockDeltaData)
if err != nil {
return eventData, err
}
eventData.Data = contentBlockDeltaData
eventData.Content = contentBlockDeltaData.Delta.Text
d.updateContent(contentBlockDeltaData.Index, contentBlockDeltaData.Delta.Text)
case "content_block_stop":
var contentBlockStopData ContentBlockStop
err := json.Unmarshal([]byte(jsonData), &contentBlockStopData)
if err != nil {
return eventData, err
}
eventData.Data = contentBlockStopData
case "message_delta":
var messageDeltaData MessageDelta
err := json.Unmarshal([]byte(jsonData), &messageDeltaData)
if err != nil {
return eventData, err
}
eventData.Data = messageDeltaData
case "message_stop":
var messageStopData MessageStopData
err := json.Unmarshal([]byte(jsonData), &messageStopData)
if err != nil {
return eventData, err
}
eventData.Data = messageStopData
case "error":
var errorData ErrorData
err := json.Unmarshal([]byte(jsonData), &errorData)
if err != nil {
return eventData, err
}
return eventData, fmt.Errorf("error(%s) - %s", errorData.Error.Type, errorData.Error.Message)
}
}
}
return eventData, nil
}
func (d *MessageSSEDecoder) updateContent(index int, content string) {
if index >= len(d.content) {
d.content = append(d.content, make([]string, index-len(d.content)+1)...)
}
d.content[index] += content
}