Documentation
¶
Overview ¶
Package sse encodes Server-Sent Events and pumps adapter events into them.
Both API dialects stream over SSE, so framing, flushing and keepalives live here; the frontends only decide what each frame contains.
Index ¶
Constants ¶
const ( // DefaultHeartbeat is the keepalive cadence used when none is configured. DefaultHeartbeat = 15 * time.Second // DefaultWriteTimeout bounds one frame write, so a client that stops reading // cannot pin a goroutine and its CLI subprocess for the whole request // timeout: a failed write ends the stream, cancelling the request. DefaultWriteTimeout = 30 * time.Second )
Variables ¶
This section is empty.
Functions ¶
func Relay ¶
func Relay(ctx context.Context, events <-chan ir.Event, interval time.Duration, on func(ir.Event) error, ping func() error) error
Relay forwards adapter events to on until the run ends, calling ping whenever the backend has produced nothing for interval.
The idle timer restarts on every event, so a ping means the backend really has gone quiet.
It returns nil once the adapter closes its channel, the error from on or ping, or a canceled error if ctx ended first. An early return does not drain the channel: the request context ending is what releases the adapter goroutine and its subprocess.
Types ¶
type Writer ¶
type Writer struct {
// WriteTimeout bounds a single frame write; zero means
// [DefaultWriteTimeout], negative disables the deadline.
WriteTimeout time.Duration
// contains filtered or unexported fields
}
Writer writes SSE frames to an HTTP response, flushing each one so the client sees it as it happens.
Headers go out with the first frame, not at construction: while nothing has been sent the response is uncommitted, so a run that fails before producing output can still be reported as an HTTP status rather than as an error buried in a stream that already claimed 200 OK.
func (*Writer) ClearDeadline ¶
func (s *Writer) ClearDeadline()
ClearDeadline removes the write deadline this writer installed. Callers must do this when the stream ends: the deadline lives on the connection, and a keep-alive connection reused for the next request must not inherit it.
func (*Writer) Comment ¶
Comment writes an SSE comment: ignored by every client, which makes it the cheapest possible keepalive.
func (*Writer) Event ¶
Event writes one named event carrying the JSON encoding of payload. An empty name writes a data-only frame, which is what the OpenAI dialect uses.