agent

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAgentNotFound = errors.New("agent not found")

ErrAgentNotFound is returned when an agent is not found in the runtime

View Source
var ErrRuntimeNotFound = errors.New("runtime not found in context")

ErrRuntimeNotFound is returned when runtime is not found in context

Functions

func ContextWithRuntime

func ContextWithRuntime(ctx context.Context, rt Runtime) context.Context

ContextWithRuntime creates a context with the given runtime

func Register

func Register(role string, factory FactoryFunc)

Register registers a factory with the default registry

Types

type Agent

type Agent interface {
	// Name returns the unique identifier for this agent instance
	Name() string

	// Role returns the agent type/role (e.g., "react", "classifier", "planner")
	Role() string

	// Start runs the agent asynchronously (e.g., listening on inputs)
	// Returns when context is canceled or agent encounters fatal error
	Start(ctx context.Context) error

	// Execute performs synchronous request-response execution
	// Used by orchestration patterns for direct invocation
	Execute(ctx context.Context, input *Message) (*Message, error)

	// Stop gracefully shuts down the agent
	Stop(ctx context.Context) error

	// Ready returns true if the agent is ready to accept requests
	Ready() bool
}

Agent interface supports both synchronous (Execute) and asynchronous (Start) execution. Agents can implement one or both methods depending on their use case.

func CreateAgent

func CreateAgent(def AgentDef, rt Runtime) (Agent, error)

CreateAgent creates an agent using the default registry

func CreateAgentWithRegistry

func CreateAgentWithRegistry(def AgentDef, rt Runtime, registry Registry) (Agent, error)

CreateAgentWithRegistry creates an agent using a custom registry (useful for testing)

type AgentDef

type AgentDef struct {
	Name       string         `yaml:"name"`
	Role       string         `yaml:"role"`
	Interval   Duration       `yaml:"interval,omitempty"`
	Listen     string         `yaml:"listen,omitempty"`
	Inputs     []Input        `yaml:"inputs,omitempty"`
	Outputs    []Output       `yaml:"outputs,omitempty"`
	DependsOn  []string       `yaml:"depends_on,omitempty"` // Startup dependencies
	Model      string         `yaml:"model,omitempty"`
	Prompt     string         `yaml:"prompt,omitempty"`
	Tools      []Tool         `yaml:"tools,omitempty"`       // Deprecated: use MCPServers
	MCPServers []string       `yaml:"mcp_servers,omitempty"` // MCP server names
	Extra      map[string]any `yaml:",inline"`
}

func TestAgentDef

func TestAgentDef(name, role string) AgentDef

TestAgentDef creates a test AgentDef with sensible defaults

func (*AgentDef) GetString

func (d *AgentDef) GetString(key, def string) string

func (*AgentDef) UnmarshalKey

func (d *AgentDef) UnmarshalKey(key string, v any) error

type DefaultRegistry

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

DefaultRegistry is the global registry implementation

func NewRegistry

func NewRegistry() *DefaultRegistry

NewRegistry creates a new registry instance (useful for testing)

func (*DefaultRegistry) GetFactory

func (r *DefaultRegistry) GetFactory(role string) (FactoryFunc, bool)

func (*DefaultRegistry) Register

func (r *DefaultRegistry) Register(role string, factory FactoryFunc)

type Duration

type Duration struct{ time.Duration }

func (*Duration) UnmarshalText

func (d *Duration) UnmarshalText(text []byte) error

type FactoryFunc

type FactoryFunc func(AgentDef, Runtime) (Agent, error)

Registry for agent factory functions

func GetFactory

func GetFactory(role string) (FactoryFunc, bool)

GetFactory retrieves a factory from the default registry

type Input

type Input struct {
	Source string `yaml:"source"`
}

type Message

type Message struct{ *pb.Message }

type MockRuntime

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

MockRuntime is a mock implementation of the Runtime interface for testing

func NewMockRuntime

func NewMockRuntime() *MockRuntime

NewMockRuntime creates a new mock runtime

func (*MockRuntime) Close

func (m *MockRuntime) Close()

Close closes all channels

func (*MockRuntime) GetRecvCalls

func (m *MockRuntime) GetRecvCalls() []string

GetRecvCalls returns all recorded Recv calls

func (*MockRuntime) GetSendCalls

func (m *MockRuntime) GetSendCalls() []SendCall

GetSendCalls returns all recorded Send calls

func (*MockRuntime) Recv

func (m *MockRuntime) Recv(source string) (<-chan *Message, error)

Recv implements Runtime.Recv

func (*MockRuntime) Send

func (m *MockRuntime) Send(target string, msg *Message) error

Send implements Runtime.Send

func (*MockRuntime) SendMessage

func (m *MockRuntime) SendMessage(source string, msg *Message)

SendMessage sends a message to a channel (for testing)

func (*MockRuntime) SetRecvError

func (m *MockRuntime) SetRecvError(err error)

SetRecvError sets an error to return from Recv

func (*MockRuntime) SetSendError

func (m *MockRuntime) SetSendError(err error)

SetSendError sets an error to return from Send

type NotImplementedError

type NotImplementedError struct {
	AgentName string
	Method    string
}

NotImplementedError is returned when a method is not implemented by an agent

func (*NotImplementedError) Error

func (e *NotImplementedError) Error() string

type Output

type Output struct {
	Target string `yaml:"target"`
	Addr   string `yaml:"addr,omitempty"`
}

type Registry

type Registry interface {
	Register(role string, factory FactoryFunc)
	GetFactory(role string) (FactoryFunc, bool)
}

Registry interface allows for testable registry implementations

type Runtime

type Runtime interface {
	// Send sends a message to a target agent asynchronously
	Send(target string, msg *Message) error

	// Recv returns a channel to receive messages from a source agent
	Recv(source string) (<-chan *Message, error)

	// Call invokes an agent synchronously and waits for response
	// Used by orchestration patterns for request-response execution
	Call(ctx context.Context, target string, input *Message) (*Message, error)

	// CallParallel invokes multiple agents concurrently and returns all results
	// Execution continues even if some agents fail (partial results returned)
	CallParallel(ctx context.Context, targets []string, input *Message) (map[string]*Message, map[string]error)

	// Broadcast sends a message to all registered agents asynchronously
	Broadcast(msg *Message) error

	// Register registers an agent instance with the runtime
	Register(agent Agent) error

	// Unregister removes an agent from the runtime
	Unregister(name string) error

	// Get retrieves a registered agent by name
	Get(name string) (Agent, error)

	// List returns all registered agent names
	List() []string

	// Start starts the runtime (e.g., gRPC server for distributed mode)
	Start(ctx context.Context) error

	// Stop gracefully shuts down the runtime
	Stop(ctx context.Context) error
}

Runtime interface provides agent execution and message passing capabilities. Supports both local (single binary) and distributed (gRPC) deployment modes.

func MustRuntimeFromContext

func MustRuntimeFromContext(ctx context.Context) Runtime

MustRuntimeFromContext extracts the Runtime from context and panics if not found. Deprecated: Use RuntimeFromContext instead and handle the error appropriately. This function is maintained for backward compatibility but should be avoided in new code as panics can cause unexpected application crashes.

func RuntimeFromContext

func RuntimeFromContext(ctx context.Context) (Runtime, error)

RuntimeFromContext safely extracts the Runtime from context. Returns the runtime and nil error if found, or nil and ErrRuntimeNotFound if not found.

type RuntimeKey

type RuntimeKey struct{}

type SendCall

type SendCall struct {
	Target  string
	Message *Message
}

SendCall records a call to Send

type Tool

type Tool struct {
	Name        string         `yaml:"name"`
	Description string         `yaml:"description"`
	InputSchema map[string]any `yaml:"input_schema"`
}

Jump to

Keyboard shortcuts

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