agent

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package agent provides functionality for executing tasks using AI-powered tools within the opsy application.

The agent acts as a bridge between the user's task requests and the available tools, using the Anthropic Claude API to intelligently select and execute appropriate tools based on the task requirements.

Core Components

The package consists of several key components:

  • Agent: The main struct that handles task execution and tool management
  • Communication: Channels for sending messages, commands, and status updates
  • Message: Represents a message from the agent or tool execution
  • Status: Represents the current state of the agent (Running, Finished, etc.)

Agent Configuration

The agent can be configured using functional options:

agent := agent.New(
	agent.WithConfig(cfg),
	agent.WithLogger(logger),
	agent.WithContext(ctx),
	agent.WithCommunication(comm),
)

Available options include:

  • WithConfig: Sets the configuration for the agent
  • WithLogger: Sets the logger for the agent
  • WithContext: Sets the context for the agent
  • WithCommunication: Sets the communication channels

Task Execution

Tasks are executed using the Run method:

outputs, err := agent.Run(&tool.RunOptions{
	Task:   "Clone the repository",
	Tools:  toolManager.GetTools(),
	Prompt: customPrompt, // Optional: Override default system prompt
	Caller: "git",       // Optional: Tool identifier for messages
}, ctx)

The agent will: 1. Parse the task and available tools 2. Use the Anthropic API to determine which tools to use 3. Execute the selected tools with appropriate parameters 4. Return the combined output from all tool executions

The agent supports customizing the system prompt through RunOptions.Prompt, which allows overriding the default behavior when needed.

Communication

The agent uses channels to communicate its progress:

  • Messages: Task progress and tool output messages
  • Commands: Commands executed by tools
  • Status: Current agent status (Running, Finished)

Example usage:

comm := &agent.Communication{
	Commands: make(chan tool.Command),
	Messages: make(chan agent.Message),
	Status:   make(chan agent.Status),
}

go func() {
	for msg := range comm.Messages {
		// Handle message
	}
}()

Tool Integration

Tools are converted to a format compatible with the Anthropic API:

  • Name: Tool identifier
  • Description: Tool purpose and functionality
  • InputSchema: JSON Schema defining valid inputs

The agent ensures proper conversion and validation of tools before use. By default, parallel tool use is disabled to ensure deterministic execution.

Error Handling

The package defines several error types:

  • ErrNoRunOptions: No options provided for Run
  • ErrNoTaskProvided: No task specified in options

All errors are properly logged with contextual information using structured logging. Tool execution errors are captured and reflected in the tool results.

Logging

The agent uses structured logging (slog) to provide detailed execution information:

  • Configuration details on initialization
  • Task execution progress and tool usage
  • Error conditions with context
  • Tool execution results and messages

Logs can be configured through the WithLogger option to capture different levels of detail as needed.

Thread Safety

The agent is designed to be thread-safe and can handle multiple concurrent tasks. Each task execution gets its own context and can be cancelled independently.

Index

Constants

View Source
const (
	// ErrNoRunOptions is the error returned when no run options are provided.
	ErrNoRunOptions = "no run options provided"
	// ErrNoTaskProvided is the error returned when no task is provided.
	ErrNoTaskProvided = "no task provided"

	// StatusReady is the status of the agent when it is ready to run.
	StatusReady = "Ready"
	// StatusRunning is the status of the agent when it is running.
	StatusRunning = "Running"
	// StatusFinished is the status of the agent when it has finished.
	StatusFinished = "Finished"
	// StatusError is the status of the agent when it has encountered an error.
	StatusError = "Error"
)
View Source
const (
	// Name is the name of the agent.
	Name = "Opsy"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Agent

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

Agent is a struct that contains the state of the agent.

func New

func New(opts ...Option) *Agent

New creates a new Agent.

func (*Agent) Run

func (a *Agent) Run(opts *tool.RunOptions, ctx context.Context) ([]tool.Output, error)

Run runs the agent with the given task and tools.

type Communication

type Communication struct {
	Commands chan tool.Command
	Messages chan Message
	Status   chan Status
}

Communication is a struct that contains the communication channels for the agent.

type Message

type Message struct {
	// Tool is the name of the tool that sent the message.
	Tool string
	// Message is the message from the tool.
	Message string
	// Timestamp is the timestamp when the message was sent.
	Timestamp time.Time
}

Message is a struct that contains a message from the agent.

type Option

type Option func(*Agent)

Option is a function that configures the Agent.

func WithClient

func WithClient(client *anthropic.Client) Option

WithClient sets the client for the agent.

func WithCommunication

func WithCommunication(communication *Communication) Option

WithCommunication sets the communication channels for the agent.

func WithConfig

func WithConfig(cfg config.Configuration) Option

WithConfig sets the configuration for the agent.

func WithContext

func WithContext(ctx context.Context) Option

WithContext sets the context for the agent.

func WithLogger

func WithLogger(logger *slog.Logger) Option

WithLogger sets the logger for the agent.

type Status

type Status string

Status is the status of the agent.

Jump to

Keyboard shortcuts

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