Documentation
¶
Overview ¶
Package openrouter implements llm.Client against OpenRouter (https://openrouter.ai). Streaming-first; tool-call assembly; 429/5xx retry at stream-start. The SSE parser is inlined here per ADR-022.
Index ¶
Constants ¶
const DefaultBaseURL = "https://openrouter.ai/api/v1"
DefaultBaseURL is OpenRouter's API root.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is an OpenRouter implementation of llm.Client.
func (*Client) Complete ¶ added in v0.2.4
Complete sends a non-streaming chat-completions request and returns the fully-assembled response in one shot.
Use this for image generation. Image-capable models (gemini-flash-image-preview, gpt-image-1, etc.) emit the entire base64 PNG/JPEG payload in a single SSE chunk. A 1024×1024 PNG is typically 0.8–2 MB raw → 1.07–2.7 MiB base64, which exceeds the SSE scanner's per-line cap and trips ErrServerError on every retry. Complete reads the body as one JSON document and is immune to that failure mode.
Retry behaviour mirrors Stream: 429/5xx at request-start are retried per retryPolicy(); auth and bad-request errors short-circuit. Content-filter aborts surface as ErrContentFiltered (either via a top-level error envelope on the JSON body or via finish_reason="content_filter"). Network-layer errors are mapped onto ErrServerError so CompleteWithRetry can apply the same retry policy as CollectWithRetry.
func (*Client) Stream ¶
Stream sends one chat-completions request and returns a channel of events. EventEnd is always the last event; the channel closes after EventEnd.
Retry happens only at stream-start: if the HTTP response is 429 or 5xx before any SSE bytes have been read, aikido closes the body and retries per the policy returned by retryPolicy(). Mid-stream errors do NOT retry — they propagate as EventError.
type Options ¶
type Options struct {
// APIKey is required.
APIKey string
// BaseURL overrides DefaultBaseURL. Useful for tests via httptest.Server.
BaseURL string
// HTTPClient overrides the default. Default: &http.Client{Timeout: 0}.
// Streams may be long-lived; callers wanting a wall-clock cap pass a
// context with deadline rather than configuring a client-level timeout.
HTTPClient *http.Client
// HTTPReferer is the optional OpenRouter ranking-attribution header.
HTTPReferer string
// XTitle is the optional OpenRouter ranking-attribution header.
XTitle string
// ProviderOrder is the optional `provider.order` routing preference.
// A single entry locks to one provider (allow_fallbacks=false); multiple
// entries form a fallback chain (allow_fallbacks=true). Matches the
// production pattern in asolabs/hub.
ProviderOrder []string
}
Options configure the OpenRouter Client.