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 ¶
- type AgentConfig
- type Client
- type ClientOption
- type ContentBlock
- type MessageResponse
- type StreamResult
- type ToolDef
- type ToolFunc
- type ToolRunner
- func (tr *ToolRunner) RegisterTool(name string, schema interface{}, fn ToolFunc) error
- func (tr *ToolRunner) Run(ctx context.Context, messages []anthropic.MessageParam) (*MessageResponse, error)
- func (tr *ToolRunner) RunStreaming(ctx context.Context, messages []anthropic.MessageParam) (*StreamResult, error)
- type ToolRunnerOption
- type TriggerConfig
- type Usage
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 ¶
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 ¶
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 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 ¶
TriggerConfig defines an event trigger parsed from agent YAML frontmatter.