summaryrefslogtreecommitdiffhomepage
path: root/packages/tui/internal/decoders/decoder.go
blob: efb699202f5345e54f4765a2d7c82b898cd3e2c7 (plain)
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
package decoders

import (
	"bufio"
	"bytes"
	"io"

	"github.com/sst/opencode-sdk-go/packages/ssestream"
)

// UnboundedDecoder is an SSE decoder that uses bufio.Reader instead of bufio.Scanner
// to avoid the 32MB token size limit. This is a workaround for large SSE events until
// the upstream Stainless SDK is fixed.
//
// This decoder handles SSE events of unlimited size by reading line-by-line with
// bufio.Reader.ReadBytes('\n'), which dynamically grows the buffer as needed.
type UnboundedDecoder struct {
	reader *bufio.Reader
	closer io.ReadCloser
	evt    ssestream.Event
	err    error
}

// NewUnboundedDecoder creates a new unbounded SSE decoder with a 1MB initial buffer size
func NewUnboundedDecoder(rc io.ReadCloser) ssestream.Decoder {
	reader := bufio.NewReaderSize(rc, 1024*1024) // 1MB initial buffer
	return &UnboundedDecoder{
		reader: reader,
		closer: rc,
	}
}

// Next reads and decodes the next SSE event from the stream
func (d *UnboundedDecoder) Next() bool {
	if d.err != nil {
		return false
	}

	event := ""
	data := bytes.NewBuffer(nil)

	for {
		line, err := d.reader.ReadBytes('\n')
		if err != nil {
			if err == io.EOF && len(line) == 0 {
				return false
			}
			if err != io.EOF {
				d.err = err
				return false
			}
		}

		// Remove trailing newline characters
		line = bytes.TrimRight(line, "\r\n")

		// Empty line indicates end of event
		if len(line) == 0 {
			if data.Len() > 0 || event != "" {
				d.evt = ssestream.Event{
					Type: event,
					Data: data.Bytes(),
				}
				return true
			}
			continue
		}

		// Skip comments (lines starting with ':')
		if line[0] == ':' {
			continue
		}

		// Parse field
		name, value, found := bytes.Cut(line, []byte(":"))
		if !found {
			// Field with no value
			continue
		}

		// Remove leading space from value
		if len(value) > 0 && value[0] == ' ' {
			value = value[1:]
		}

		switch string(name) {
		case "":
			// An empty line in the form ": something" is a comment and should be ignored
			continue
		case "event":
			event = string(value)
		case "data":
			_, d.err = data.Write(value)
			if d.err != nil {
				return false
			}
			_, d.err = data.WriteRune('\n')
			if d.err != nil {
				return false
			}
		}
	}
}

// Event returns the current event
func (d *UnboundedDecoder) Event() ssestream.Event {
	return d.evt
}

// Close closes the underlying reader
func (d *UnboundedDecoder) Close() error {
	return d.closer.Close()
}

// Err returns any error that occurred during decoding
func (d *UnboundedDecoder) Err() error {
	return d.err
}