Documentation
¶
Overview ¶
Package api handles LLM API communication via the Anthropic Go SDK.
Package api defines data structures for API communication
Index ¶
- Constants
- func WithPerAttemptTimeout(ctx context.Context, d time.Duration) context.Context
- func WithRetryNotifier(ctx context.Context, fn func(attempt int, err error)) context.Context
- type Client
- func (c *Client) GetModel() string
- func (c *Client) SendMessage(ctx context.Context, prompt string) (string, error)
- func (c *Client) SendMessageWithHistory(ctx context.Context, messages []Message) (string, error)
- func (c *Client) SendMessageWithTools(ctx context.Context, messages []Message, tools []Tool, executor ToolExecutor, ...) (string, []Message, Usage, error)
- func (c *Client) SendMessageWithToolsStreaming(ctx context.Context, messages []Message, tools []Tool, executor ToolExecutor, ...) (string, []Message, Usage, error)
- func (c *Client) SetModel(model string)
- type ClientOption
- type ContentBlock
- type InputSchema
- type Message
- type MultiExecutor
- type PropertyDef
- type StatusError
- type Tool
- type ToolCallResult
- type ToolExecutor
- type Usage
Constants ¶
const ( DefaultMaxTokens = 8192 MessagesEndpoint = "/v1/messages" AnthropicVersion = "2023-06-01" )
Constants for API configuration.
Variables ¶
This section is empty.
Functions ¶
func WithPerAttemptTimeout ¶
WithPerAttemptTimeout returns a context that will apply the given timeout to each individual request attempt. The SDK client extracts this value and passes it as option.WithRequestTimeout, so each retry gets a fresh timeout.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client handles communication with the Anthropic API using the official SDK.
func NewClient ¶
func NewClient(baseURL, apiKey string, opts ...ClientOption) *Client
NewClient creates a new API client backed by the Anthropic Go SDK.
func (*Client) SendMessage ¶
SendMessage sends a single user message and returns the response text.
func (*Client) SendMessageWithHistory ¶
SendMessageWithHistory sends a conversation history and returns the response text.
func (*Client) SendMessageWithTools ¶
func (c *Client) SendMessageWithTools(ctx context.Context, messages []Message, tools []Tool, executor ToolExecutor, onCall ...func(ToolCallResult)) (string, []Message, Usage, error)
SendMessageWithTools sends a conversation with tool definitions, executing any tool calls the model makes and continuing the loop until the model returns a final text response. It returns the final text response, the full accumulated message history, the total token usage across all API calls in the loop, and any error. The optional onCall callback is invoked after each tool execution with the result.
func (*Client) SendMessageWithToolsStreaming ¶
func (c *Client) SendMessageWithToolsStreaming(ctx context.Context, messages []Message, tools []Tool, executor ToolExecutor, onToken func(string), onCall ...func(ToolCallResult)) (string, []Message, Usage, error)
SendMessageWithToolsStreaming is like SendMessageWithTools but streams text tokens via the SSE API. onToken is called for each text token as it arrives; it may be nil. Tool-calling turns are handled the same way as in SendMessageWithTools.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption is a function that configures a Client.
func WithHTTPClient ¶
func WithHTTPClient(h *http.Client) ClientOption
WithHTTPClient sets a custom HTTP client.
func WithMaxRetries ¶
func WithMaxRetries(n int) ClientOption
WithMaxRetries sets the maximum number of retry attempts after a retryable error.
func WithMaxTokens ¶
func WithMaxTokens(n int) ClientOption
WithMaxTokens sets the maximum number of tokens in API responses.
func WithModel ¶
func WithModel(model string) ClientOption
WithModel sets the model to use for API requests.
func WithSystemPrompt ¶
func WithSystemPrompt(s string) ClientOption
WithSystemPrompt sets the system prompt sent with every request.
type ContentBlock ¶
type ContentBlock struct {
Type string `json:"type"`
// text blocks
Text string `json:"text,omitempty"`
// tool_use blocks (assistant → us)
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Input map[string]any `json:"input,omitempty"`
// tool_result blocks (us → assistant)
ToolUseID string `json:"tool_use_id,omitempty"`
Content string `json:"content,omitempty"`
}
ContentBlock represents a single content block in a message or response. Type is one of "text", "tool_use", or "tool_result".
type InputSchema ¶
type InputSchema struct {
Type string `json:"type"`
Properties map[string]PropertyDef `json:"properties"`
Required []string `json:"required,omitempty"`
}
InputSchema is the JSON Schema for a tool's input object.
type Message ¶
Message represents a single message in the conversation. Content may be a plain string or a []ContentBlock (for tool_use/tool_result).
type MultiExecutor ¶
type MultiExecutor struct {
// contains filtered or unexported fields
}
MultiExecutor dispatches ExecuteTool calls to the registered executor for each tool name.
func NewMultiExecutor ¶
func NewMultiExecutor() *MultiExecutor
NewMultiExecutor returns an empty MultiExecutor.
func (*MultiExecutor) ExecuteTool ¶
func (m *MultiExecutor) ExecuteTool(ctx context.Context, name string, input map[string]any) (string, error)
ExecuteTool dispatches to the executor registered for name. Returns an error if no executor is registered for name.
func (*MultiExecutor) Register ¶
func (m *MultiExecutor) Register(name string, exec ToolExecutor)
Register associates name with the given executor.
type PropertyDef ¶
PropertyDef describes a single property in an InputSchema.
type StatusError ¶
StatusError is returned when the API responds with a non-2xx status code. It wraps the SDK's *anthropic.Error to preserve the FriendlyMessage API used by tui/api.go without requiring changes there.
func (*StatusError) Error ¶
func (e *StatusError) Error() string
func (*StatusError) FriendlyMessage ¶ added in v0.5.0
func (e *StatusError) FriendlyMessage() string
FriendlyMessage returns a short, human-readable error message by parsing the structured JSON body. Falls back to a generic status-code message.
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema InputSchema `json:"input_schema"`
}
Tool describes a tool the model may call.
type ToolCallResult ¶
ToolCallResult records a single tool execution that occurred during a request.