sse

package
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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 NewWriter

func NewWriter(w http.ResponseWriter) *Writer

NewWriter wraps an HTTP response.

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

func (s *Writer) Comment(text string) error

Comment writes an SSE comment: ignored by every client, which makes it the cheapest possible keepalive.

func (*Writer) Event

func (s *Writer) Event(name string, payload any) error

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.

func (*Writer) Raw

func (s *Writer) Raw(data string) error

Raw writes a data frame verbatim, for sentinels like OpenAI's "[DONE]".

func (*Writer) Wrote

func (s *Writer) Wrote() bool

Wrote reports whether any frame has been sent, i.e. whether the response is already committed to being an event stream.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL