-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
158 lines (127 loc) · 4.1 KB
/
decoder.go
File metadata and controls
158 lines (127 loc) · 4.1 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package prompts
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/katallaxie/pkg/conv"
"github.com/katallaxie/pkg/slices"
)
const maxBufferSize = 512 * 1 * 1000
// ResponseDecoder decodes http responses into struct values.
type ResponseDecoder interface {
// Decode decodes the response into the value pointed to by v.
Decode(resp *http.Response, v any) error
}
// jsonDecoder decodes http response JSON into a JSON-tagged struct value.
type jsonDecoder struct{}
// NewJSONDecoder returns a ResponseDecoder that decodes JSON responses into struct values.
func NewJSONDecoder() ResponseDecoder {
return jsonDecoder{}
}
// Decode decodes the Response Body into the value pointed to by v.
// Caller must provide a non-nil v and close the resp.Body.
func (d jsonDecoder) Decode(resp *http.Response, v any) error {
return json.NewDecoder(resp.Body).Decode(v)
}
type byteStreamer struct{}
// NewByteStreamer returns a ResponseDecoder that copies the response body into an [io.Writer] instance.
func NewByteStreamer() ResponseDecoder {
return byteStreamer{}
}
// Decode simply tries to copy response data into v assuming its an [io.Writer] instance. Assuming so little about v
// gives consumers a lot of choice about consuming response data. They can wait for all data to be dumped into some
// buffer then act on it or they can read as soon as data gets written.
func (d byteStreamer) Decode(resp *http.Response, v any) error {
w, ok := v.(io.Writer)
if !ok {
return fmt.Errorf("expected type: %T; got: %T", w, v)
}
_, err := io.Copy(w, resp.Body)
if err != nil {
return fmt.Errorf("failed copying response data to v: %w", err)
}
return nil
}
// SSEvent is an event emitted by the server.
type SSEvent struct {
// ID is the unique identifier for this event.
ID []byte
// Event is the name of the event.
Event []byte
// Data is the content of this message.
Data []byte
// Retry is the reconnection time (milliseconds).
Retry []byte
}
var (
idField = []byte(`id`)
eventField = []byte(`event`)
dataField = []byte(`data`)
retryField = []byte(`retry`)
)
// ErrInvalidSequence is returned when the event sequence is invalid.
var ErrInvalidSequence = fmt.Errorf("invalid event sequence")
type completionEventDecoder struct{}
// NewCompletionEventDecoder returns a new SSE decoder.
func NewCompletionEventDecoder() ResponseDecoder {
return &completionEventDecoder{}
}
// Decode simply tries to copy response data into v assuming its an [io.Writer] instance. Assuming so little about v
// gives consumers a lot of choice about consuming response data. They can wait for all data to be dumped into some
// buffer then act on it or they can read as soon as data gets written.
func (d completionEventDecoder) Decode(resp *http.Response, v any) error {
s, ok := v.(CompletionEventStream)
if !ok {
return fmt.Errorf("expected type: %T; got: %T", s, v)
}
scanner := bufio.NewScanner(resp.Body)
buf := make([]byte, 0, maxBufferSize)
scanner.Buffer(buf, maxBufferSize)
defer close(s)
for scanner.Scan() {
sseEvent := SSEvent{}
payload := scanner.Bytes()
if len(payload) == 0 {
continue
}
// parse line
del := bytes.IndexByte(payload, ':')
if del == 0 || del < 0 {
continue // skip comment
}
field, content := payload[:del], bytes.TrimSpace(payload[del+1:])
switch {
case bytes.EqualFold(field, idField):
sseEvent.ID = content
case bytes.EqualFold(field, eventField):
sseEvent.Event = content
case bytes.EqualFold(field, dataField):
if sseEvent.Data == nil {
sseEvent.Data = make([]byte, 0)
} else {
sseEvent.Data = append(sseEvent.Data, '\n')
}
sseEvent.Data = append(sseEvent.Data, content...)
case bytes.EqualFold(field, retryField):
sseEvent.Retry = content
}
event := CompletionEvent{Raw: sseEvent}
event.Type = conv.String(sseEvent.Event)
if !slices.GreaterThen(0, sseEvent.Data...) {
s <- event
continue
}
err := json.Unmarshal(sseEvent.Data, &event.Response)
if err != nil {
return err
}
s <- event
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("failed to read response body: %w", err)
}
return nil
}