provider

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package provider abstracts model backends behind Anthropic Messages semantics. A provider instance is constructed purely from configuration (protocol / model / base_url / api_key — CLAUDE.md principle 4): the anthropic protocol adapter works against ANY endpoint speaking Anthropic Messages — a gateway, a proxy, or a self-hosted model — never just api.anthropic.com. The registry resolves an agent's model string to a configured provider.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chunk

type Chunk struct {
	Kind  ChunkKind
	Index int64  // content block index (text/thinking deltas)
	Text  string // text/thinking fragment

	ToolUse *ToolUse // KindToolUse only

	StopReason string             // KindDone only: end_turn | tool_use | max_tokens | …
	Usage      *domain.ModelUsage // KindDone only
}

Chunk is one streaming increment.

type ChunkKind

type ChunkKind string

ChunkKind discriminates streaming increments of a turn.

const (
	// KindTextDelta appends text to the content block at Index.
	KindTextDelta ChunkKind = "text_delta"
	// KindThinkingDelta appends thinking text to the block at Index.
	KindThinkingDelta ChunkKind = "thinking_delta"
	// KindToolUse is one complete tool invocation (input fully accumulated).
	KindToolUse ChunkKind = "tool_use"
	// KindDone closes the turn with stop reason and usage.
	KindDone ChunkKind = "done"
)

type Config

type Config struct {
	Protocol string            // "anthropic" | "openai"
	Model    string            // model id sent to the upstream endpoint
	BaseURL  string            // endpoint base URL; required (no implicit default host)
	APIKey   string            // credential for the endpoint
	Headers  map[string]string // optional extra headers (gateway routing etc.)
}

Config constructs one provider instance.

type Factory

type Factory func(Config) (Provider, error)

Factory constructs a Provider for one protocol.

type Message

type Message struct {
	Role    string          // "user" | "assistant"
	Content json.RawMessage // Anthropic content: a string or an array of blocks
}

Message is one conversational turn.

type Provider

type Provider interface {
	Generate(ctx context.Context, req Request) (Stream, error)
}

Provider runs single model turns against one configured backend.

type Registry

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

Registry resolves agent model strings to constructed providers. Routing is exact-match with an optional "*" default, so an enterprise maps its model names onto endpoints purely in configuration. Constructed providers are cached per model string — one client per backend, not one per turn.

func NewRegistry

func NewRegistry(routes []Route, factories map[string]Factory) (*Registry, error)

func (*Registry) Provider

func (r *Registry) Provider(model string) (Provider, error)

Provider resolves the provider for an agent's model string. When the route's config has no upstream model set, the agent's own model string passes through (a gateway that understands the platform's model names).

type Request

type Request struct {
	System    string
	Messages  []Message
	Tools     []json.RawMessage // Anthropic tool definitions, verbatim
	MaxTokens int64
}

Request is one model turn in Anthropic Messages semantics. Content and tool definitions stay as raw Anthropic wire JSON so the anthropic-protocol adapter is near-zero-conversion; lossy mappings are confined to the non-Anthropic adapters (see provider/openai).

type Route

type Route struct {
	Model  string
	Config Config
}

Route binds an agent-facing model string to a provider config. Model "*" is the default route.

func LoadRoutes

func LoadRoutes(path string) ([]Route, error)

LoadRoutes reads a model_providers JSON file into registry routes. Structural validation happens here (unknown keys are config typos, not extensions); route-level validation stays in NewRegistry.

type Stream

type Stream interface {
	Next() bool
	Chunk() Chunk
	Err() error
	Close() error
}

Stream yields one turn's chunks in order.

for stream.Next() { chunk := stream.Chunk(); … }
if err := stream.Err(); err != nil { … }

type ToolUse

type ToolUse struct {
	ID    string
	Name  string
	Input json.RawMessage
}

ToolUse is a complete tool call emitted by the model.

Directories

Path Synopsis
Package anthropic adapts any endpoint speaking the Anthropic Messages protocol to the provider interface, via the official SDK with a configurable base URL — an enterprise gateway, a proxy, or a self-hosted model are all just configuration.
Package anthropic adapts any endpoint speaking the Anthropic Messages protocol to the provider interface, via the official SDK with a configurable base URL — an enterprise gateway, a proxy, or a self-hosted model are all just configuration.
Package openai adapts an OpenAI Chat Completions endpoint (OpenAI itself, a vLLM server, or an internal OpenAI-compatible gateway) to the provider interface.
Package openai adapts an OpenAI Chat Completions endpoint (OpenAI itself, a vLLM server, or an internal OpenAI-compatible gateway) to the provider interface.

Jump to

Keyboard shortcuts

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