Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ var (
)

const (
// SSETypeDefault is the default type of SSEEvent.
SSETypeDefault = "message"

// SSETypeDone is the type of SSEEvent that indicates the prediction is done. The Data field will contain an empty JSON object.
SSETypeDone = "done"

Expand All @@ -39,7 +42,7 @@ type SSEEvent struct {
}

func (e *SSEEvent) decode(b []byte) error {
data := [][]byte{}
chunks := [][]byte{}
for _, line := range bytes.Split(b, []byte("\n")) {
// Parse field and value from line
parts := bytes.SplitN(line, []byte{':'}, 2)
Expand All @@ -62,17 +65,17 @@ func (e *SSEEvent) decode(b []byte) error {
case "event":
e.Type = string(value)
case "data":
data = append(data, value)
chunks = append(chunks, value)
default:
// ignore
}
}

if !utf8.Valid(bytes.Join(data, []byte("\n"))) {
data := bytes.Join(chunks, []byte("\n"))
if !utf8.Valid(data) {
return ErrInvalidUTF8Data
}

e.Data = string(bytes.Join(data, []byte("\n")))
e.Data = string(data)

return nil
}
Expand Down Expand Up @@ -202,9 +205,11 @@ func (r *Client) streamPrediction(ctx context.Context, prediction *Prediction, l
b := buf.Bytes()
buf.Reset()

event := SSEEvent{}
event := SSEEvent{Type: SSETypeDefault}
if err := event.decode(b); err != nil {
errChan <- err
Comment thread
mattt marked this conversation as resolved.
close(done)
return
}

sseChan <- event
Expand Down