llm

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 5, 2026 License: MIT Imports: 11 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package llm provides a client abstraction for LLM providers, starting with Anthropic SDK integration for colony worker interactions.

The package offers three main capabilities:

  • Client: wraps the Anthropic SDK for sending messages and receiving responses
  • Streaming: accumulates SSE stream events into complete text responses
  • Tools: implements an agentic tool-use loop with registered tool functions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AgentConfig

type AgentConfig struct {
	Name        string          `yaml:"name"`
	Description string          `yaml:"description"`
	Tools       []string        `yaml:"tools"`
	Color       string          `yaml:"color"`
	Model       string          `yaml:"model"`
	Triggers    []TriggerConfig `yaml:"triggers"`
}

AgentConfig holds the parsed agent specification from YAML frontmatter.

func ParseAgentSpec

func ParseAgentSpec(content []byte) (*AgentConfig, error)

ParseAgentSpec parses YAML frontmatter from raw agent definition content. The content must start with "---" and contain a second "---" delimiter. Returns an error for missing delimiters, unclosed frontmatter, invalid YAML, or an empty Name field.

func ParseAgentSpecFile

func ParseAgentSpecFile(path string) (*AgentConfig, error)

ParseAgentSpecFile reads a file and parses its YAML frontmatter into an AgentConfig.

type Client

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

Client wraps an Anthropic SDK client with configured model and token limits.

func NewClient

func NewClient(opts ...ClientOption) (*Client, error)

NewClient creates a new LLM client with the given options. If no model is specified, defaults to claude-sonnet-4-20250514. If no max tokens is specified, defaults to 4096. Returns an error if no API key is available (neither explicit nor env var).

func (*Client) SendMessage

func (c *Client) SendMessage(ctx context.Context, messages []anthropic.MessageParam) (*MessageResponse, error)

SendMessage sends messages to the LLM and returns the response.

type ClientOption

type ClientOption func(*Client)

ClientOption configures a Client during construction.

func WithAPIKey

func WithAPIKey(key string) ClientOption

WithAPIKey sets the API key explicitly, overriding the ANTHROPIC_API_KEY env var.

func WithMaxTokens

func WithMaxTokens(n int64) ClientOption

WithMaxTokens sets the maximum number of tokens for API responses.

func WithModel

func WithModel(model string) ClientOption

WithModel sets the model to use for API calls.

type ContentBlock

type ContentBlock struct {
	Type string
	Text string
}

ContentBlock represents a single content block in a response.

type MessageResponse

type MessageResponse struct {
	ID         string
	Role       string
	Content    []ContentBlock
	StopReason string
	Model      string
	Usage      Usage
}

MessageResponse holds the parsed response from an LLM call.

type StreamResult

type StreamResult struct {
	Text       string
	Role       string
	Model      string
	StopReason string
	Usage      Usage
}

StreamResult holds the accumulated result from an SSE stream.

func AccumulateStream

func AccumulateStream(stream *ssestream.Stream[anthropic.MessageStreamEventUnion]) (*StreamResult, error)

AccumulateStream consumes all events from an SSE stream and accumulates text content into a StreamResult. Returns an error if the stream fails.

type ToolDef

type ToolDef struct {
	Name        string
	Description string
	InputSchema interface{}
}

ToolDef describes a tool that can be registered with a ToolRunner.

type ToolFunc

type ToolFunc func(ctx context.Context, input map[string]interface{}) (string, error)

ToolFunc is the signature for a tool execution function.

type ToolRunner

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

ToolRunner executes an agentic tool-use loop against the Anthropic API. It sends messages, detects tool_use blocks in responses, executes the corresponding tool functions, returns results, and repeats until the model responds with text only or max iterations are exceeded.

func NewToolRunner

func NewToolRunner(client *Client, funcs map[string]ToolFunc, opts ...ToolRunnerOption) *ToolRunner

NewToolRunner creates a new ToolRunner with the given client and tool functions. Tool definitions are automatically created from the function names. Defaults to a maximum of 10 iterations.

func (*ToolRunner) RegisterTool

func (tr *ToolRunner) RegisterTool(name string, schema interface{}, fn ToolFunc) error

RegisterTool adds a tool to the runner with an explicit schema and function.

func (*ToolRunner) Run

func (tr *ToolRunner) Run(ctx context.Context, messages []anthropic.MessageParam) (*MessageResponse, error)

Run executes the tool-use loop using non-streaming API calls. It sends messages, checks for tool_use blocks, executes tools, and loops until the model responds with text-only content or max iterations are exceeded.

func (*ToolRunner) RunStreaming

func (tr *ToolRunner) RunStreaming(ctx context.Context, messages []anthropic.MessageParam) (*StreamResult, error)

RunStreaming executes the tool-use loop with streaming for the final response. Tool execution uses non-streaming calls; only the last response is streamed for text accumulation via AccumulateStream.

type ToolRunnerOption

type ToolRunnerOption func(*ToolRunner)

ToolRunnerOption configures a ToolRunner.

func WithToolRunnerMaxIterations

func WithToolRunnerMaxIterations(n int) ToolRunnerOption

WithToolRunnerMaxIterations sets the maximum number of tool-use loop iterations.

type TriggerConfig

type TriggerConfig struct {
	Topic  string         `yaml:"topic"`
	Filter map[string]any `yaml:"filter"`
}

TriggerConfig defines an event trigger parsed from agent YAML frontmatter.

type Usage

type Usage struct {
	InputTokens  int64
	OutputTokens int64
}

Usage holds token usage information from an API response.

Jump to

Keyboard shortcuts

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