stage

package
v1.261002.1 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MPL-2.0 Imports: 5 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 stage defines transport-independent protocol endpoints, ordered same-protocol stages, and the bidirectional bridges between protocols.

Provider endpoints, Guardrails and the server tool loop become composable levels, and protocol changes happen only in an explicit Bridge. Nothing in the server imports this package yet. Terms and design decisions are in .design/protocol-stage.md.

Anthropic V1 is not a chain protocol. V1 is a subset of Anthropic Beta on the wire, so V1 requests are upgraded to Beta at the client edge and downgraded only where a provider needs the V1 path; every Endpoint, Stage and Bridge in a chain speaks anthropic_beta for Anthropic. The contracts enforce this.

Terminal provider endpoints live in stage/upstream; the cross-protocol bridges from Anthropic Beta to the OpenAI protocols live in stage/anthropicbridge.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HasCommittedSideEffects

func HasCommittedSideEffects(err error) bool

HasCommittedSideEffects reports whether err was raised after a stage committed side effects.

func WrapCommitted

func WrapCommitted(err error, committed bool) error

WrapCommitted marks err as raised after side effects when committed is true, and returns it unchanged otherwise, so retry classification before the first side effect is unaffected.

Types

type Bridge

type Bridge interface {
	Source() protocol.APIType
	Target() protocol.APIType
	Open(ctx context.Context, call Call, operation Operation) (BridgeSession, error)
}

Bridge describes an immutable, concurrency-safe bidirectional protocol adapter. Open converts one source Call to the target protocol and creates all mutable response/stream correlation state for that call.

type BridgeSession

type BridgeSession interface {
	TargetCall() Call
	ConvertComplete(ctx context.Context, response *Response) (*Response, error)
	ConvertStream(ctx context.Context, stream EventStream) (EventStream, error)
}

BridgeSession is the per-call reverse path created while converting a request inward. A session is used for exactly one Complete or Stream invocation. Errors from the target endpoint are returned unchanged: they carry the provider's status, which the client edge maps.

ConvertStream must return a source-protocol stream that owns the target stream: it closes the target stream from Close.

type Call

type Call struct {
	Request any
	State   ProtocolState
}

Call is one invocation of an Endpoint in that endpoint's native protocol. Request remains protocol-native; a Bridge is responsible for changing its concrete type before it crosses a protocol boundary.

type CommittedError

type CommittedError struct {
	Err error
}

CommittedError marks an error raised after a stage committed side effects, such as executing a server-owned tool. Retrying the attempt would repeat them, so failover must not retry an error for which HasCommittedSideEffects reports true.

func (*CommittedError) Error

func (e *CommittedError) Error() string

func (*CommittedError) Unwrap

func (e *CommittedError) Unwrap() error

type Endpoint

type Endpoint interface {
	Protocol() protocol.APIType
	Complete(ctx context.Context, call Call) (*Response, error)
	Stream(ctx context.Context, call Call) (EventStream, error)
}

Endpoint is a complete non-streaming and streaming implementation of one concrete protocol. It does not own HTTP parsing, response headers, or SSE framing.

func Adapt

func Adapt(next Endpoint, bridge Bridge) (Endpoint, error)

Adapt exposes next in bridge.Source() while calling next in bridge.Target(). It validates the protocol boundary without executing a request.

func Compose

func Compose(terminal Endpoint, stages ...Stage) (Endpoint, error)

Compose wraps terminal with stages written in request order, from outermost to innermost. For example:

Compose(provider, guardrails, tools)

produces guardrails(tools(provider)). Responses and stream events naturally return through tools and then guardrails.

Compose performs structural validation only. It never invokes Complete or Stream and it never inserts an implicit protocol conversion.

type Event

type Event struct {
	Value any
}

Event is one native-protocol streaming event.

type EventStream

type EventStream interface {
	Next(ctx context.Context) (Event, error)
	Close() error
	Result() StreamResult
}

EventStream is a pull-based stream in one concrete protocol.

Next returns io.EOF on normal completion and must honor ctx cancellation. The caller must call Close exactly once for every successfully returned EventStream. Result returns the latest terminal summary and must not advance the stream.

type Heartbeat

type Heartbeat struct{}

Heartbeat is an Event value a stage emits while it works without producing output (for example while a server tool runs), so the client edge can keep an idle connection alive. It carries no protocol payload: client adapters consume it and never forward it as a protocol event, and a Bridge above the stage that emits it must pass it through unchanged.

type Operation

type Operation uint8

Operation identifies which endpoint operation the Bridge session is being opened for. Request conversion may legitimately differ between complete and streaming calls (for example, stream flags and usage options).

const (
	OperationComplete Operation = iota + 1
	OperationStream
)

func (Operation) String

func (o Operation) String() string

type ProtocolState

type ProtocolState struct {
	// OpenAIChat is populated when a request is converted to OpenAI Chat. It
	// preserves reasoning/thinking facts used by provider-specific transforms.
	OpenAIChat *protocol.OpenAIConfig
}

ProtocolState carries typed, request-derived facts that a later endpoint or stage still needs after the request changes protocol. It deliberately avoids an open-ended property bag: every carried value needs an explicit contract.

Values are per-call and may be mutated by inner request transforms. A Bridge must never retain them on the shared Bridge instance.

type Response

type Response struct {
	Value any
	Usage *protocol.TokenUsage
}

Response is the complete result returned by an Endpoint. Value is expressed in the endpoint's native protocol; Usage is the protocol-neutral token usage of the call, which outer levels must preserve across protocol changes.

type Stage

type Stage interface {
	Name() string
	Protocol() protocol.APIType
	Wrap(next Endpoint) Endpoint
}

Stage is a named full-duplex wrapper implemented in one concrete protocol. Wrap must not execute next. Compose validates the protocol reported by both the Stage and the wrapped Endpoint.

type StreamResult

type StreamResult struct {
	Usage *protocol.TokenUsage
}

StreamResult is the latest protocol-neutral summary of an EventStream. It is valid before completion, but usage may only become final after Next returns io.EOF.

Directories

Path Synopsis
Package anthropicbridge adapts Anthropic Beta calls to OpenAI Chat and OpenAI Responses while keeping the outward response in Anthropic Beta.
Package anthropicbridge adapts Anthropic Beta calls to OpenAI Chat and OpenAI Responses while keeping the outward response in Anthropic Beta.
Package openaibridge adapts OpenAI calls to a target provider protocol while keeping the outward response in the exact OpenAI source protocol.
Package openaibridge adapts OpenAI calls to a target provider protocol while keeping the outward response in the exact OpenAI source protocol.
Package responsesbridge adapts OpenAI Responses calls to provider protocols while keeping the outward response in the Responses source protocol.
Package responsesbridge adapts OpenAI Responses calls to provider protocols while keeping the outward response in the Responses source protocol.
Package sdkstream presents a Protocol Stage EventStream as the SDK stream types the existing client writers consume, so the client-facing output of a Stage pipeline goes through exactly the code that writes it today.
Package sdkstream presents a Protocol Stage EventStream as the SDK stream types the existing client writers consume, so the client-facing output of a Stage pipeline goes through exactly the code that writes it today.
Package toolround is the Tool Round Stage: the single decision point for every tool call a model makes, on the Anthropic Beta protocol.
Package toolround is the Tool Round Stage: the single decision point for every tool call a model makes, on the Anthropic Beta protocol.
Package upstream provides the terminal Protocol Stage endpoints: one per chain protocol, each forwarding a native request to the selected provider through the existing internal/forwarding functions.
Package upstream provides the terminal Protocol Stage endpoints: one per chain protocol, each forwarding a native request to the selected provider through the existing internal/forwarding functions.

Jump to

Keyboard shortcuts

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