api

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package api implements the LLM gateway against a direct Anthropic-compatible HTTP endpoint. It is strictly additive to the subscription gateway: the local authoring loop does not need it, but a server-side worker cannot run against a personal subscription and so requires it.

Index

Constants

This section is empty.

Variables

View Source
var ErrNoAPIKey = errors.New("api: an API key is required")

ErrNoAPIKey is returned when no key is configured.

Functions

This section is empty.

Types

type AuthStyle

type AuthStyle string

AuthStyle selects how the API key is presented to the gateway.

const (
	// AuthStyleAnthropic sends `x-api-key`, the direct Anthropic API's own
	// scheme. This is the default: unset, it is exactly today's behaviour.
	AuthStyleAnthropic AuthStyle = "x-api-key"
	// AuthStyleBearer sends `Authorization: Bearer <key>`, which is what
	// Anthropic-compatible gateways such as OpenRouter
	// (https://openrouter.ai/api/v1/messages) expect.
	//
	// Only the auth header differs. `anthropic-version` is sent either way —
	// see the comment at the header block for why.
	AuthStyleBearer AuthStyle = "bearer"
)

type Gateway

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

Gateway is a direct-API LLM gateway.

func New

func New(o Options) (*Gateway, error)

New returns a gateway. An empty key is an error rather than a warning: a gateway that silently cannot authenticate fails on first use, mid-run.

func (*Gateway) Complete

func (g *Gateway) Complete(ctx context.Context, r llm.Req) (llm.Res, error)

Complete implements llm.Gateway.

func (*Gateway) ModelFor

func (g *Gateway) ModelFor(c llm.ModelClass) string

ModelFor implements llm.Gateway. This gateway always knows its own model: StrongModel and FastModel are configured (or defaulted) in New, so an unrecognized class simply falls back to the strong model, matching modelFor's own convention.

type Options

type Options struct {
	BaseURL     string
	APIKey      string
	StrongModel string
	FastModel   string
	// AuthStyle selects the auth header sent with every request. Empty
	// defaults to AuthStyleAnthropic, today's behaviour.
	AuthStyle  AuthStyle
	Cache      llm.Cache
	HTTPClient *http.Client
	// MaxRetries bounds retries of transient (5xx/429) upstream failures.
	MaxRetries int
	// Sleep is the backoff hook, overridden in tests so they do not wait.
	Sleep func(time.Duration)
}

Options configures the gateway.

Jump to

Keyboard shortcuts

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