provider

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package provider defines the Provider interface and shared types for AI model inference backends (Ollama, Claude, OpenAI).

Index

Constants

This section is empty.

Variables

View Source
var ErrNoHealthyProvider = errors.New("no healthy provider available")

ErrNoHealthyProvider is returned when no provider in the routing chain is healthy.

Functions

This section is empty.

Types

type ChatRequest

type ChatRequest struct {
	Model    string    `json:"model"`
	Messages []Message `json:"messages"`
	Stream   bool      `json:"stream"` // always false for now
}

ChatRequest contains the parameters for a chat completion.

type ChatResponse

type ChatResponse struct {
	Model   string  `json:"model"`
	Message Message `json:"message"`

	// Token usage (if reported by provider).
	PromptTokens     int `json:"prompt_eval_count,omitempty"`
	CompletionTokens int `json:"eval_count,omitempty"`
}

ChatResponse contains the result of a chat completion.

type Message

type Message struct {
	Role    Role   `json:"role"`
	Content string `json:"content"`
}

Message is a single chat message.

type Provider

type Provider interface {
	// Chat sends a chat completion request and returns the response.
	Chat(ctx context.Context, req ChatRequest) (*ChatResponse, error)

	// Healthy returns true if the provider is reachable and ready.
	Healthy(ctx context.Context) bool

	// Name returns the provider identifier (e.g., "ollama", "claude", "openai").
	Name() string
}

Provider is the interface for AI model inference backends. Implementations exist for Ollama (local), Claude, and OpenAI (cloud).

type ProviderConfig

type ProviderConfig struct {
	Type         string `yaml:"type"`          // "claude", "openai", "ollama"
	APIKeyEnv    string `yaml:"api_key_env"`   // env var name for API key
	BaseURL      string `yaml:"base_url"`      // override base URL (for ollama or custom endpoints)
	DefaultModel string `yaml:"default_model"` // default model for this provider
}

ProviderConfig describes a single provider entry in YAML config.

type ProviderFactory

type ProviderFactory func(name string, cfg ProviderConfig) (Provider, error)

ProviderFactory constructs a Provider from a ProviderConfig. It is set by the application's wiring layer (e.g., cmd/samverk) to avoid the registry importing sub-packages directly in tests.

type ProviderInfo

type ProviderInfo struct {
	Name    string
	Type    string
	Model   string
	Healthy bool
}

ProviderInfo describes a registered provider for listing.

type Registry

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

Registry holds constructed providers and routing rules.

func LoadRegistry

func LoadRegistry(path string, factory ProviderFactory) (*Registry, error)

LoadRegistry reads a YAML config file, constructs providers using the given factory, and returns a fully wired Registry.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates an empty provider registry.

func (*Registry) Get

func (r *Registry) Get(ctx context.Context, agentType string) (Provider, string, error)

Get returns the first healthy provider from the routing chain for the given agent type. If the agent type is not configured, it falls back to "default". Returns (provider, model, error).

func (*Registry) List

func (r *Registry) List(ctx context.Context) []ProviderInfo

List returns info about all registered providers with their health status.

func (*Registry) Register

func (r *Registry) Register(name, providerType string, p Provider, model string)

Register adds a named provider to the registry.

func (*Registry) SetRouting

func (r *Registry) SetRouting(routing map[string][]string)

SetRouting sets the routing table that maps agent types to provider chains.

type RegistryConfig

type RegistryConfig struct {
	Providers map[string]ProviderConfig `yaml:"providers"`
	Routing   map[string][]string       `yaml:"routing"` // agent_type -> provider names
}

RegistryConfig is the top-level YAML structure for provider configuration.

func LoadRegistryConfig

func LoadRegistryConfig(path string) (*RegistryConfig, error)

LoadRegistryConfig reads and parses a YAML config file.

type Role

type Role string

Role identifies the sender of a chat message.

const (
	RoleSystem    Role = "system"
	RoleUser      Role = "user"
	RoleAssistant Role = "assistant"
)

Directories

Path Synopsis
Package claude implements the provider.Provider interface using the Anthropic Claude Messages API over raw net/http (no SDK dependency).
Package claude implements the provider.Provider interface using the Anthropic Claude Messages API over raw net/http (no SDK dependency).
Package ollama implements the provider.Provider interface using the Ollama REST API over raw net/http (no SDK dependency).
Package ollama implements the provider.Provider interface using the Ollama REST API over raw net/http (no SDK dependency).
Package openai implements the provider.Provider interface using the OpenAI Chat Completions API over raw net/http (no SDK dependency).
Package openai implements the provider.Provider interface using the OpenAI Chat Completions API over raw net/http (no SDK dependency).

Jump to

Keyboard shortcuts

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