sse

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: AGPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package sse provides a minimal, purpose-built SSE (Server-Sent Events) tee-reader for use in the tapes proxy. It is designed to parse SSE events from an upstream LLM provider while simultaneously forwarding the raw bytes verbatim to a downstream client in a tee pipe fashion.

This package intentionally does NOT provide SSE writer or server capabilities.

See the SSE specification: https://html.spec.whatwg.org/multipage/server-sent-events.html

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Event

type Event struct {
	// Type is the SSE event type from the "event:" field.
	// An empty string means the default "message" type per the SSE spec.
	Type string

	// Data is the concatenated contents of all "data:" lines for this event,
	// joined with "\n" (per the SSE spec, multiple data fields are joined
	// with a single newline).
	Data string

	// ID is the last event ID from the "id:" field, if present.
	ID string
}

Event represents a single parsed SSE event, delimited by a blank line in the upstream byte stream.

type TeeReader

type TeeReader struct {
	// contains filtered or unexported fields
}

TeeReader reads SSE events from a source io.Reader while simultaneously writing all raw bytes verbatim to a destination io.writer. This effectively enables "tee" shaped reading where TeeReader.Next returns the Event for consumption while writing to a separate destination.

┌──────────────────┐ │ source io.Reader │ └──────────────────┘ │ ▼ ┌──────────────────┐ ┌───────────────────────┐ │ TeeReader.Next() │──▶│ destination io.Writer │ └──────────────────┘ └───────────────────────┘ │ ▼ ┌──────────────────┐ │ Event │ └──────────────────┘

An SSE client io.Writer receives the exact copy of the stream, while the caller can inspect parsed events.

func NewTeeReader

func NewTeeReader(src io.Reader, dest io.Writer) *TeeReader

NewTeeReader returns a Reader that parses SSE events from the src io.Reader and writes all raw bytes through to dest. The dest writer typically backs an io.Pipe connected to the downstream HTTP response.

func (*TeeReader) Next

func (r *TeeReader) Next() (*Event, error)

Next returns the next parsed SSE event from the scanner. It blocks until a complete event is available (terminated by a blank line in the stream). Next returns nil, nil when the source is exhausted.

Next also tees all bytes to the destination writer supplied to NewTeeReader. This ensures the downstream client receivers can consume SSE stream bytes verbatim.

Jump to

Keyboard shortcuts

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