Help us shape the future of Go! Take the 2026 Go Developer Survey

provider

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Package provider turns a context of state file records into one LLM request and the streamed response back into records. Five envelopes exist. Same context, same envelope, same bytes: the step record's request hash proves it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Events

func Events(r io.Reader, fn func(event, data string) error) error

Events reads a server-sent event stream and calls fn once per event with its name (may be empty) and joined data. Returns when the stream ends or fn returns an error.

func Split

func Split(ctx []statefile.Record) (system, rest []statefile.Record)

Split separates the leading system records, which are the system prompt, from the rest of the context.

Types

type Cache

type Cache struct {
	TTL   int      `json:"ttl,omitempty"`
	Marks []string `json:"marks"`
}

Cache is the step record's cache header, facts only. Marks are the ids of the records that end a cache breakpoint. Anthropic and bedrock put one after the system prompt and one after the last user block; openai and gemini place their own, so the mark is the last record sent. TTL is seconds from the step's sent time, set only where the request the harness sends fixes it: anthropic and bedrock. The step's usage says what was read and written.

type Call

type Call struct {
	Name      string          `json:"name"`
	Arguments json.RawMessage `json:"arguments"`
}

Call is the body of a call record.

type Envelope

type Envelope interface {
	Name() string
	Accepts(typ string) bool
	Owns(field string) bool
	Compile(req Request) (body []byte, cache json.RawMessage, err error)
	Stream(resp io.Reader, emit func(statefile.Record)) (statefile.Header, error)
}

Envelope is one wire shape. Name tags the thinking and step records it produces; only thinking with its own name is replayed. Compile is deterministic and returns the cache markers it placed, which go on the step record. Stream calls emit once per block as the stream completes it and returns the step header with usage. Accepts is the media types the envelope encodes as media; a record of any other type goes as text. Owns is the body fields Compile builds from the session, which params may not set.

type Provider

type Provider struct {
	URL      string // full endpoint. gemini and bedrock take the model in the path, see Endpoint
	Key      string
	Envelope Envelope
	Headers  func(req *http.Request, key string) // auth and version headers
	Endpoint func(url, model string) string      // nil means URL as is
}

func Anthropic

func Anthropic(key string) Provider

func Bedrock

func Bedrock(region, key string) Provider

Bedrock authenticates with a Bedrock API key (AWS_BEARER_TOKEN_BEDROCK), not SigV4. Model is an inference profile id such as us.anthropic.claude-haiku-4-5-20251001-v1:0.

func Gemini

func Gemini(key string) Provider

func OpenAI

func OpenAI(key string) Provider

func OpenAIChat

func OpenAIChat(base, key string) Provider

OpenAIChat is the Chat Completions envelope for OpenAI-compatible endpoints. Thinking records are never replayed.

func (Provider) Step

func (p Provider) Step(ctx context.Context, req Request, emit func(statefile.Record)) (statefile.Header, error)

Step is one LLM call. Blocks are emitted as they complete; the returned header is the step record minus kind, id, and upto.

type Request

type Request struct {
	Model   string
	Key     string // session id, the routing key for prefix caches
	Tools   []Tool
	Context []statefile.Record // statefile.Build output
	Params  map[string]json.RawMessage
}

Params are the session's vendor fields, see encode.

type Tool

type Tool struct {
	Name        string
	Description string
	Schema      json.RawMessage // JSON Schema for the arguments object
}

type Turn

type Turn struct {
	Assistant bool
	Records   []statefile.Record
	// contains filtered or unexported fields
}

Turns groups the non-system context into alternating turns in API order: user turns hold results, messages, later system records, summaries, and errors; assistant turns hold the blocks one step closed. The step record ends its assistant turn. A step that closed no blocks, a model that said nothing, is no turn at all: every provider rejects an empty assistant turn, and every provider merges the user turns around it.

func Turns

func Turns(rest []statefile.Record) []Turn

Jump to

Keyboard shortcuts

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