protocol

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnthropicErrorType

func AnthropicErrorType(err *chat.Error) string

AnthropicErrorType maps a canonical API error to an Anthropic error type string.

func AsError

func AsError(err error) *chat.Error

AsError normalizes err into a chat.APIError with safe defaults.

func InitialResponse

func InitialResponse(seed chat.Response, parsed *ParsedRequest) chat.Response

InitialResponse assembles the initial response envelope shared by every creation path, applying library defaults for identity and echoing the retrieval fields from the parsed request.

func RequiresReasoningSummary

func RequiresReasoningSummary(event chat.Event) bool

func ValidateOutputEvent

func ValidateOutputEvent(event chat.Event, info chat.Info) error

ValidateOutputEvent checks an agent output event against what the selected agent declares it can provide.

func ValidateRequestedOutput

func ValidateRequestedOutput(event chat.Event, modalities chat.Modality) error

ValidateRequestedOutput checks an agent output event against the modalities the caller actually requested.

func WriteError

func WriteError(w http.ResponseWriter, kind Kind, err error)

WriteError writes the canonical error envelope for the selected protocol.

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, value any)

WriteJSON marshals value as JSON with the given HTTP status.

Types

type Adapter

type Adapter interface {
	// ValidateEvent checks whether event can be encoded for this protocol.
	ValidateEvent(chat.Event) error
	// Response builds a non-streaming response body for result.
	Response(chat.Request, execution.Result, Meta) (any, error)
	// Stream returns an SSE encoder for the given response writer.
	// meta is retained for the stream lifetime so terminal Response
	// updates are visible to Complete.
	Stream(http.ResponseWriter, ParsedRequest, *Meta, chat.Limits) StreamEncoder
}

Adapter is the protocol-specific boundary used by the HTTP handler.

type Kind

type Kind uint8

Kind identifies one of the wire protocols served by the handler.

const (
	// Chat is the OpenAI Chat Completions wire protocol.
	Chat Kind = iota + 1
	// Responses is the OpenAI Responses wire protocol.
	Responses
	// Anthropic is the Anthropic Messages wire protocol.
	Anthropic
)

type Meta

type Meta struct {
	Response chat.Response // identity, status, output, and retrieval fields
	Activity bool          // when true, Responses may encode EventActivity frames
}

Meta contains the response value shared by all protocol encoders.

type ParsedRequest

type ParsedRequest struct {
	Kind         Kind              // wire protocol that parsed the request
	Request      chat.Request      // canonical execution request for Agent.Run
	Turn         []chat.Item       // items submitted in this HTTP request only
	Previous     *string           // prior response ID for continuation
	Store        *bool             // wire store flag; nil when omitted
	Metadata     map[string]string // application metadata for the response
	Retain       bool              // effective retention after StoreDefault
	Stream       bool              // whether the client requested streaming
	IncludeUsage bool              // Chat Completions stream_options.include_usage only
}

ParsedRequest is the protocol-neutral request produced by a wire decoder. Request holds agent execution fields. Turn, Previous, Store, Metadata, and Retain are acceptance/persistence concerns filled by parsers and the handler.

type SSEWriter

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

SSEWriter owns the shared HTTP/SSE mechanics. Protocol codecs only provide event names and values; this type handles headers, event-size limits, flushing, and the OpenAI terminal marker.

func NewSSEWriter

func NewSSEWriter(w http.ResponseWriter, limits chat.Limits) *SSEWriter

NewSSEWriter constructs an SSEWriter for the given response writer and limits.

func (*SSEWriter) Done

func (s *SSEWriter) Done() error

Done writes the OpenAI-compatible [DONE] marker. Anthropic and audio SSE codecs intentionally do not use it because their protocols have typed terminal events.

func (*SSEWriter) Start

func (s *SSEWriter) Start() error

Start writes SSE headers and marks the stream as started.

func (*SSEWriter) Started

func (s *SSEWriter) Started() bool

Started reports whether SSE headers have been written.

func (*SSEWriter) Write

func (s *SSEWriter) Write(event string, value any) error

Write marshals value as one SSE data frame, optionally prefixed with event.

type StreamEncoder

type StreamEncoder interface {
	// Event encodes one canonical agent event into protocol SSE frames.
	Event(chat.Event) error
	// Complete emits terminal stream events and closes the SSE lifecycle.
	Complete(chat.Outcome, []chat.Item) error
	// Fail writes a protocol-specific error response for the stream.
	Fail(error) error
	// Started reports whether SSE output has begun.
	Started() bool
}

StreamEncoder turns canonical events into one protocol's SSE lifecycle.

Jump to

Keyboard shortcuts

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