api

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2026 License: MIT Imports: 12 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 api provides the OpenRouter API client.

Index

Constants

View Source
const (
	// DefaultBaseURL is the default OpenRouter API base URL.
	DefaultBaseURL = "https://openrouter.ai/api/v1"

	// DefaultTimeout is the default HTTP timeout for non-streaming requests.
	DefaultTimeout = 5 * time.Minute

	// DefaultStreamTimeout is the default timeout for streaming requests.
	DefaultStreamTimeout = 5 * time.Minute

	// DefaultImageTimeout is the default timeout for image generation requests.
	DefaultImageTimeout = 5 * time.Minute

	// DefaultMaxRetries is the default maximum number of retries.
	DefaultMaxRetries = 3

	// DefaultInitialBackoff is the default initial backoff duration.
	DefaultInitialBackoff = 500 * time.Millisecond

	// DefaultMaxBackoff is the default maximum backoff duration.
	DefaultMaxBackoff = 5 * time.Second
)
View Source
const (
	SSEDataPrefix    = "data: "
	SSECommentPrefix = ":"
	StreamEndSignal  = "[DONE]"
)

SSE stream constants.

Variables

View Source
var (
	ErrUnauthorized       = errors.New("unauthorized")
	ErrRateLimited        = errors.New("rate limited")
	ErrServiceUnavailable = errors.New("service unavailable")
	ErrStreamClosed       = errors.New("stream closed")
)

Sentinel errors for common API error conditions.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	StatusCode int
	Message    string
	Body       string
}

APIError represents an error from the OpenRouter API.

func (*APIError) Error

func (e *APIError) Error() string

func (*APIError) Unwrap

func (e *APIError) Unwrap() error

Unwrap returns the underlying sentinel error based on status code.

type ChatCall

type ChatCall struct {
	Ctx context.Context
	Req *ChatRequest
}

ChatCall records a call to Chat.

type ChatRequest

type ChatRequest struct {
	Model       string       `json:"model"`
	Messages    []Message    `json:"messages"`
	Stream      bool         `json:"stream"`
	Modalities  []string     `json:"modalities,omitempty"`
	ImageConfig *ImageConfig `json:"image_config,omitempty"`
}

ChatRequest represents a request to the chat completions API.

type ChatResponse

type ChatResponse struct {
	Choices []Choice `json:"choices"`
	Error   *struct {
		Message string `json:"message"`
	} `json:"error"`
}

ChatResponse represents the response from the chat completions API.

type ChatStreamCall

type ChatStreamCall struct {
	Ctx context.Context
	Req *ChatRequest
}

ChatStreamCall records a call to ChatStream.

type Choice

type Choice struct {
	Delta struct {
		Content string `json:"content"`
	} `json:"delta"`
	Message struct {
		Content string         `json:"content"`
		Images  []ImageContent `json:"images,omitempty"`
	} `json:"message"`
	FinishReason *string `json:"finish_reason"`
}

Choice represents a completion choice in the response.

type Client

type Client interface {
	// Chat sends a non-streaming chat request.
	Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)

	// ChatStream sends a streaming chat request and returns a StreamReader.
	ChatStream(ctx context.Context, req *ChatRequest) (*StreamReader, error)

	// ListModels retrieves available models.
	ListModels(ctx context.Context, opts *ListModelsOptions) ([]Model, error)
}

Client is the interface for interacting with the OpenRouter API.

func DefaultClient

func DefaultClient(apiKey string, timeout time.Duration) Client

DefaultClient creates a new client with default configuration.

func ImageClient

func ImageClient(apiKey string, timeout time.Duration) Client

ImageClient creates a new client configured for image generation.

func NewClient

func NewClient(cfg ClientConfig) Client

NewClient creates a new API client with the given configuration.

type ClientConfig

type ClientConfig struct {
	// APIKey is the OpenRouter API key.
	APIKey string

	// BaseURL is the API base URL. Defaults to DefaultBaseURL.
	BaseURL string

	// Timeout is the HTTP timeout for non-streaming requests.
	// Defaults to DefaultTimeout.
	Timeout time.Duration

	// StreamTimeout is the timeout for streaming requests.
	// Defaults to DefaultStreamTimeout.
	StreamTimeout time.Duration

	// HTTPClient is an optional custom HTTP client.
	// If nil, a new client will be created.
	HTTPClient *http.Client

	// Referer is the HTTP-Referer header value.
	Referer string

	// Title is the X-Title header value.
	Title string

	// Retry configures retry behavior. If nil, retries are disabled.
	Retry *RetryConfig
}

ClientConfig contains configuration for the API client.

type ContentPart added in v0.2.0

type ContentPart struct {
	Type     string    `json:"type"`
	Text     string    `json:"text,omitempty"`
	ImageURL *ImageURL `json:"image_url,omitempty"`
}

ContentPart represents a single part of a multipart message content.

type ImageConfig

type ImageConfig struct {
	AspectRatio string `json:"aspect_ratio,omitempty"` // e.g., "1:1", "16:9"
	Size        string `json:"size,omitempty"`         // e.g., "1K", "2K", "4K"
}

ImageConfig represents configuration for image generation.

type ImageContent

type ImageContent struct {
	Type     string   `json:"type"` // "image_url"
	ImageURL ImageURL `json:"image_url"`
}

ImageContent represents image content in the response.

type ImageURL

type ImageURL struct {
	URL string `json:"url"` // data:image/png;base64,...
}

ImageURL represents an image URL in the response.

type ListModelsCall

type ListModelsCall struct {
	Ctx  context.Context
	Opts *ListModelsOptions
}

ListModelsCall records a call to ListModels.

type ListModelsOptions

type ListModelsOptions struct {
	Category            string
	SupportedParameters string
}

ListModelsOptions represents options for listing models.

type Message

type Message struct {
	Role         string        `json:"role"`
	Content      string        `json:"-"`
	ContentParts []ContentPart `json:"-"`
}

Message represents a chat message. Content is used for simple string messages. ContentParts is used for multipart messages (e.g., text + images). When both are set, ContentParts takes precedence during marshaling.

func (Message) MarshalJSON added in v0.2.0

func (m Message) MarshalJSON() ([]byte, error)

MarshalJSON implements custom JSON marshaling for Message. When ContentParts is populated, content is serialized as an array. Otherwise, content is serialized as a string.

func (*Message) UnmarshalJSON added in v0.2.0

func (m *Message) UnmarshalJSON(data []byte) error

UnmarshalJSON implements custom JSON unmarshaling for Message. It detects whether content is a string or array and populates fields accordingly.

type MockClient

type MockClient struct {
	// ChatFunc is called when Chat is invoked.
	ChatFunc func(ctx context.Context, req *ChatRequest) (*ChatResponse, error)

	// ChatStreamFunc is called when ChatStream is invoked.
	ChatStreamFunc func(ctx context.Context, req *ChatRequest) (*StreamReader, error)

	// ListModelsFunc is called when ListModels is invoked.
	ListModelsFunc func(ctx context.Context, opts *ListModelsOptions) ([]Model, error)

	// ChatCalls records all calls to Chat.
	ChatCalls []ChatCall

	// ChatStreamCalls records all calls to ChatStream.
	ChatStreamCalls []ChatStreamCall

	// ListModelsCalls records all calls to ListModels.
	ListModelsCalls []ListModelsCall
}

MockClient is a mock implementation of the Client interface for testing.

func NewMockClient

func NewMockClient() *MockClient

NewMockClient creates a new MockClient with default implementations.

func (*MockClient) Chat

func (m *MockClient) Chat(ctx context.Context, req *ChatRequest) (*ChatResponse, error)

Chat implements Client.Chat.

func (*MockClient) ChatStream

func (m *MockClient) ChatStream(ctx context.Context, req *ChatRequest) (*StreamReader, error)

ChatStream implements Client.ChatStream.

func (*MockClient) ListModels

func (m *MockClient) ListModels(ctx context.Context, opts *ListModelsOptions) ([]Model, error)

ListModels implements Client.ListModels.

func (*MockClient) Reset

func (m *MockClient) Reset()

Reset clears all recorded calls.

type Model

type Model struct {
	ID                  string            `json:"id"`
	Name                string            `json:"name"`
	Created             int64             `json:"created"`
	Description         string            `json:"description"`
	ContextLength       *int              `json:"context_length"`
	Pricing             ModelPricing      `json:"pricing"`
	Architecture        ModelArchitecture `json:"architecture"`
	TopProvider         TopProviderInfo   `json:"top_provider"`
	PerRequestLimits    *PerRequestLimits `json:"per_request_limits"`
	SupportedParameters []string          `json:"supported_parameters"`
}

Model represents an OpenRouter model.

func (*Model) IsImageModel

func (m *Model) IsImageModel() bool

IsImageModel returns true if the model supports image output.

func (*Model) IsTextOnlyModel

func (m *Model) IsTextOnlyModel() bool

IsTextOnlyModel returns true if the model supports text output but not image output.

func (*Model) SupportsImageInput added in v0.2.0

func (m *Model) SupportsImageInput() bool

SupportsImageInput returns true if the model accepts image input.

type ModelArchitecture

type ModelArchitecture struct {
	Tokenizer        string   `json:"tokenizer"`
	InstructType     *string  `json:"instruct_type"`
	InputModalities  []string `json:"input_modalities"`
	OutputModalities []string `json:"output_modalities"`
}

ModelArchitecture represents the architecture of a model.

type ModelPricing

type ModelPricing struct {
	Prompt     string `json:"prompt"`
	Completion string `json:"completion"`
	Request    string `json:"request"`
	Image      string `json:"image"`
	Web        string `json:"web_search,omitempty"`
	Audio      string `json:"input_audio,omitempty"`
}

ModelPricing represents pricing information for a model.

type ModelsResponse

type ModelsResponse struct {
	Data []Model `json:"data"`
}

ModelsResponse represents the response from the models API.

type PerRequestLimits

type PerRequestLimits struct {
	PromptTokens     *int `json:"prompt_tokens"`
	CompletionTokens *int `json:"completion_tokens"`
}

PerRequestLimits represents per-request token limits.

type RetryConfig

type RetryConfig struct {
	// MaxRetries is the maximum number of retry attempts.
	MaxRetries int

	// InitialBackoff is the initial backoff duration.
	InitialBackoff time.Duration

	// MaxBackoff is the maximum backoff duration.
	MaxBackoff time.Duration
}

RetryConfig configures retry behavior.

func DefaultRetryConfig

func DefaultRetryConfig() RetryConfig

DefaultRetryConfig returns the default retry configuration.

type StreamChunk

type StreamChunk struct {
	Content      string
	Done         bool
	FinishReason *string
}

StreamChunk represents a chunk of streamed content.

type StreamError

type StreamError struct {
	Message string
	Cause   error
}

StreamError represents an error that occurred during streaming.

func (*StreamError) Error

func (e *StreamError) Error() string

func (*StreamError) Unwrap

func (e *StreamError) Unwrap() error

type StreamReader

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

StreamReader reads SSE events from a stream.

func NewStreamReader

func NewStreamReader(body io.ReadCloser) *StreamReader

NewStreamReader creates a new StreamReader from an io.ReadCloser.

func (*StreamReader) Close

func (r *StreamReader) Close() error

Close closes the underlying stream.

func (*StreamReader) Next

func (r *StreamReader) Next() (*StreamChunk, error)

Next reads the next chunk from the stream. Returns nil, nil when the stream is complete. Returns nil, error on stream errors.

func (*StreamReader) ReadAll

func (r *StreamReader) ReadAll() (string, error)

ReadAll reads all content from the stream and returns it as a string. This is a convenience method for non-TUI usage.

type TopProviderInfo

type TopProviderInfo struct {
	ContextLength       *int `json:"context_length"`
	MaxCompletionTokens *int `json:"max_completion_tokens"`
	IsModerated         bool `json:"is_moderated"`
}

TopProviderInfo represents information about the top provider.

Jump to

Keyboard shortcuts

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