provider

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package provider defines the model-neutral message types and the streaming interface every model backend implements. The agent loop only ever sees these types; the Anthropic and OpenAI wire dialects stay inside their own files and translate at the edge.

Index

Constants

View Source
const (
	StopEndTurn   = "end_turn"
	StopToolUse   = "tool_use"
	StopMaxTokens = "max_tokens"
)

Stop reasons, normalized across dialects.

Variables

This section is empty.

Functions

This section is empty.

Types

type Anthropic

type Anthropic struct {
	APIKey  string
	BaseURL string // default https://api.anthropic.com
	Client  *http.Client
}

Anthropic speaks the Messages API with SSE streaming.

func (*Anthropic) Stream

func (a *Anthropic) Stream(ctx context.Context, req Request, emit func(Event)) (*Response, error)

Stream implements Provider.

type Block

type Block struct {
	Type BlockType `json:"type"`

	// BlockText.
	Text string `json:"text,omitempty"`

	// BlockImage: base64 payload plus its MIME type.
	MediaType string `json:"media_type,omitempty"`
	Data      string `json:"data,omitempty"`

	// BlockToolUse.
	ID    string          `json:"id,omitempty"`
	Name  string          `json:"name,omitempty"`
	Input json.RawMessage `json:"input,omitempty"`

	// BlockToolResult.
	ToolID  string `json:"tool_id,omitempty"`
	Content string `json:"content,omitempty"`
	IsError bool   `json:"is_error,omitempty"`
}

Block is one piece of message content, a tagged union: Type says which fields mean anything. A plain struct beats an interface here because blocks round-trip through JSON for the session ledger.

func Text

func Text(s string) Block

Text builds a text block.

type BlockType

type BlockType string

BlockType enumerates the kinds of content a message can carry.

const (
	BlockText       BlockType = "text"
	BlockImage      BlockType = "image"
	BlockToolUse    BlockType = "tool_use"
	BlockToolResult BlockType = "tool_result"
)

type Event

type Event struct {
	Type EventType
	Text string
	Name string
}

Event is a streaming callback payload.

type EventType

type EventType int

EventType tags a streaming event.

const (
	// EventText carries a text delta.
	EventText EventType = iota
	// EventToolUse fires once per tool call, as soon as its name is known.
	EventToolUse
)

type Message

type Message struct {
	Role   Role    `json:"role"`
	Blocks []Block `json:"blocks"`
}

Message is one conversation entry.

func UserText

func UserText(s string) Message

UserText builds the common case, a user message holding one text block.

type OpenAI

type OpenAI struct {
	APIKey  string
	BaseURL string // e.g. https://api.openai.com/v1 or http://gamingpc:8000/v1
	Client  *http.Client
}

OpenAI speaks the chat completions dialect, which is the lingua franca of local and hosted inference alike: llama.cpp, ollama, vllm, and most gateways all serve it. BaseURL points at the /v1 root.

func (*OpenAI) Stream

func (o *OpenAI) Stream(ctx context.Context, req Request, emit func(Event)) (*Response, error)

Stream implements Provider.

type Provider

type Provider interface {
	Stream(ctx context.Context, req Request, emit func(Event)) (*Response, error)
}

Provider streams one model response. emit may be nil when the caller does not care about deltas; Stream always returns the complete response.

func Build

func Build(pc config.Provider) (Provider, error)

Build turns a config entry into a live Provider.

type Request

type Request struct {
	Model     string
	System    string
	Messages  []Message
	Tools     []Tool
	MaxTokens int
}

Request is one model call.

type Response

type Response struct {
	Blocks     []Block
	StopReason string
	Usage      Usage
}

Response is the fully assembled reply to one call.

type Role

type Role string

Role is who a message is from. Tool results ride on user messages, the way both wire dialects expect.

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

type Tool

type Tool struct {
	Name        string
	Description string
	Schema      json.RawMessage
}

Tool describes a callable tool the way the model sees it. Schema is a JSON schema for the input object.

type Usage

type Usage struct {
	InputTokens  int `json:"input_tokens"`
	OutputTokens int `json:"output_tokens"`
}

Usage counts tokens for one call.

Jump to

Keyboard shortcuts

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