protocol

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: AGPL-3.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 protocol maps HTTP surfaces and wire events onto relaykit DTOs.

relaykit deliberately stops at the DTO boundary: it neither parses nor emits SSE. Everything below the DTO — framing, event names, the `data: [DONE]` sentinel — is protocol plumbing and therefore lives here.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientModel

func ClientModel(format types.RelayFormat, request any, pathModel string) string

ClientModel reads the model name a client asked for. For Gemini the caller must supply the name parsed from the path.

func DecodeRequest

func DecodeRequest(format types.RelayFormat, body []byte) (any, error)

DecodeRequest parses a client body into the relaykit DTO for its protocol.

func DecodeStreamEvent

func DecodeStreamEvent(format types.RelayFormat, event Event) (value any, done bool, err error)

DecodeStreamEvent parses one upstream SSE frame into a relaykit DTO.

done reports the end of the stream; the caller stops reading once it is true. Frames the converter cannot represent are skipped (done=false, value=nil, err=nil) rather than failing the whole stream, because upstreams routinely emit keep-alives and vendor-specific event types.

func EncodeResponse

func EncodeResponse(value any) ([]byte, error)

EncodeResponse serialises a converted DTO for the client.

func IsStreamRequest

func IsStreamRequest(format types.RelayFormat, request any, path string) bool

IsStreamRequest reports whether the decoded request asked for streaming. Each protocol carries the flag differently: a body field for OpenAI and Claude, the URL method for Gemini.

func MatchInbound

func MatchInbound(path string) (types.RelayFormat, string, bool)

MatchInbound resolves the client protocol from a request path. The second return is the model name extracted from the path, which only Gemini uses.

func SetModel

func SetModel(request any, model string)

SetModel rewrites the model name carried by a request DTO. Gemini has no model field — its model travels in the URL — so the caller keeps that name separately.

Types

type Event

type Event struct {
	Name string
	Data string
}

Event is one decoded SSE frame.

func EncodeStreamEvent

func EncodeStreamEvent(format types.RelayFormat, value any) ([]Event, error)

EncodeStreamEvent renders a converted DTO as downstream SSE frame(s).

Claude Messages and OpenAI Responses frame every event with a matching `event:` line; OpenAI Chat and Gemini send bare data frames.

type Reader

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

Reader decodes an SSE byte stream into frames.

func NewReader

func NewReader(r io.Reader) *Reader

func (*Reader) Next

func (r *Reader) Next() (Event, bool, error)

Next returns the next frame. ok is false at end of stream.

Per the SSE spec a frame ends at a blank line; `data:` lines accumulate and are joined with "\n". Comment lines (": ping") are skipped because relaykit has no representation for them.

type Writer

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

Writer emits SSE frames.

func NewWriter

func NewWriter(w io.Writer) *Writer

func (*Writer) WriteComment

func (w *Writer) WriteComment(text string) error

WriteComment emits an SSE comment, used as a keep-alive ping.

func (*Writer) WriteData

func (w *Writer) WriteData(data string) error

WriteData emits a data-only frame, used by the OpenAI Chat and Gemini surfaces.

func (*Writer) WriteDone

func (w *Writer) WriteDone() error

WriteDone emits the OpenAI Chat end-of-stream sentinel.

func (*Writer) WriteEvent

func (w *Writer) WriteEvent(name string, data string) error

WriteEvent emits a named frame, used by the Claude Messages and OpenAI Responses surfaces.

Jump to

Keyboard shortcuts

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