Documentation
¶
Overview ¶
Package ir defines the protocol-neutral internal representation that sits between API frontends (OpenAI, Anthropic) and agent CLI adapters.
Frontends decode wire requests into an Request; adapters translate CLI output into a stream of [Event]s; frontends encode those events back into their own dialect. Neither side knows about the other.
Index ¶
Constants ¶
const DefaultRateLimitRetry = 10 * time.Second
DefaultRateLimitRetry is the retry hint attached to an upstream rate limit. The CLIs do not say how long to wait, and both vendor SDKs back off on their own, but a hint keeps a client from hammering a limit it has just hit.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Code ¶
type Code string
Code is a protocol-neutral error class. Frontends translate it into their own dialect's error shape; the HTTP status comes from Code.HTTPStatus.
const ( // CodeInvalidRequest — the caller sent something we cannot serve (400). CodeInvalidRequest Code = "invalid_request" // CodeModelNotFound — no adapter serves the requested model (404). CodeModelNotFound Code = "model_not_found" CodeUnauthorized Code = "unauthorized" // CodeOverloaded — no concurrency slot became free in time (429). CodeOverloaded Code = "overloaded" CodeUpstreamUnavailable Code = "upstream_unavailable" // CodeUpstreamError — the CLI ran but failed (502). CodeUpstreamError Code = "upstream_error" // CodeTimeout — the CLI exceeded a request or idle deadline (504). CodeTimeout Code = "timeout" // CodeCanceled — the client went away before the response was complete. CodeCanceled Code = "canceled" // CodeInternal — a bug in agent2api (500). CodeInternal Code = "internal" )
func (Code) HTTPStatus ¶
HTTPStatus maps a code to the status frontends should return.
type Completion ¶
type Completion struct {
Text string
Thinking string
Usage *Usage
StopReason StopReason
Model string
}
Completion is a fully collected response, as a non-streaming frontend needs it. Adapters always stream internally; "non-streaming" is just this.
type Error ¶
type Error struct {
Code Code
Message string
// Param names the offending request field, when there is one.
Param string
// Detail is extra diagnostic context, e.g. the tail of the CLI's stderr.
Detail string
// RetryAfter, when set, is rendered as the Retry-After header. Both vendor
// SDKs honour it when deciding how long to back off.
RetryAfter time.Duration
// Err is the wrapped cause, if any.
Err error
}
Error is an agent2api error carrying enough context for a frontend to render an actionable message. Detail holds operator-facing context (a stderr tail, for instance) and is included in the response body: this is a local, single-user gateway, and hiding the CLI's own complaint helps nobody.
func AsError ¶
AsError extracts an *Error from err, wrapping anything unrecognized as an internal error so callers always have something to render.
func IncompleteRun ¶
func IncompleteRun() *Error
IncompleteRun is the error for a run that ended without a terminal event: a backend that stopped mid-answer. It must never be reported as a short answer, because the caller has no way to tell the difference.
func InvalidRequest ¶
InvalidRequest builds a 400 tied to a specific request field.
type Event ¶
type Event struct {
Type EventType
// Text is set for EventTextDelta and EventThinkingDelta.
Text string
// Usage is set for EventUsage and may also be set on EventDone.
Usage *Usage
// StopReason may be set on EventDone.
StopReason StopReason
// Model is the concrete backend model the CLI reported, when it does. Set
// on EventStart or EventDone.
Model string
// Err is set for EventError.
Err *Error
}
Event is one unit of adapter output. Exactly one of Text, Usage or Err is meaningful, depending on Type.
type EventType ¶
type EventType int
EventType discriminates the events an adapter streams while a CLI runs.
const ( // EventStart is emitted once, when the backend has accepted the request. EventStart EventType = iota // EventTextDelta carries a chunk of assistant-visible text. EventTextDelta // EventThinkingDelta carries a chunk of reasoning text, when the CLI // exposes one. Frontends may drop it. EventThinkingDelta // EventUsage carries token accounting; it may arrive more than once, in // which case later events supersede earlier ones. EventUsage // EventDone is the final event of a successful run. EventDone // EventError is the final event of a failed run. EventError )
type Message ¶
Message is a single turn of the conversation. Content is plain text; the frontends flatten richer wire formats (content part arrays) down to text and reject anything that cannot be represented, such as images.
type Request ¶
type Request struct {
// Model is the resolved, namespaced backend model, e.g. "claude-code:opus".
Model string
// Adapter is the adapter id the request was routed to, e.g. "claude-code".
Adapter string
// Variant is the adapter-specific model name, e.g. "opus".
Variant string
Messages []Message
Stream bool
// MaxTokens and Temperature are best-effort: most CLIs cannot enforce them
// and adapters are free to ignore them.
MaxTokens *int
Temperature *float64
// Metadata carries non-essential request annotations (e.g. the caller's
// "user" field). Never used for routing.
Metadata map[string]string
}
Request is a protocol-neutral completion request. It carries the entire conversation: agent2api keeps no server-side state, so every request must be self-contained.
type StopReason ¶
type StopReason string
StopReason mirrors the Anthropic vocabulary; frontends map it into their own.
const ( StopEndTurn StopReason = "end_turn" StopMaxTokens StopReason = "max_tokens" StopStopSeq StopReason = "stop_sequence" )