agent

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultTokenThreshold is the default threshold at which compaction triggers (80K tokens).
	DefaultTokenThreshold = 80000
	// PruneTurnAge is the number of recent turns to keep intact; older messages get pruned.
	PruneTurnAge = 20
)
View Source
const (
	// DefaultHeartbeatInterval is the default interval between heartbeat checks.
	DefaultHeartbeatInterval = 30 * time.Minute
)

Variables

This section is empty.

Functions

func EstimateTokens

func EstimateTokens(messages []provider.Message) int

EstimateTokens provides a rough token estimate: chars/4.

Types

type Agent

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

Agent is the ReAct agent loop.

func NewAgent

func NewAgent(rc config.ResolvedAgent, prov provider.Provider, mb *bus.MessageBus, homeDir string) *Agent

NewAgent creates a new Agent from a resolved config.

func (*Agent) HandleMessage

func (a *Agent) HandleMessage(ctx context.Context, msg bus.InboundMessage) string

HandleMessage processes an inbound message through the ReAct loop.

func (*Agent) HandleWebChat added in v0.3.0

func (a *Agent) HandleWebChat(ctx context.Context, text string) string

HandleWebChat handles a chat message from the web UI.

func (*Agent) InjectGroupMessage

func (a *Agent) InjectGroupMessage(ctx context.Context, msg bus.InboundMessage)

InjectGroupMessage appends a message from another bot into the session history without triggering an LLM call. This gives the agent awareness of what other bots said in the group chat.

func (*Agent) Name

func (a *Agent) Name() string

Name returns the agent's name.

func (*Agent) SetGroupContext

func (a *Agent) SetGroupContext(gc *GroupContext)

SetGroupContext configures group chat awareness for this agent's system prompt.

type CompactResult

type CompactResult struct {
	Messages []provider.Message
	Pruned   bool
	LogFile  string
}

CompactResult holds the result of a compaction operation.

func CompactMessages

func CompactMessages(messages []provider.Message, workspace string, prov provider.Provider, model string) (*CompactResult, error)

CompactMessages prunes and optionally compresses the message history when it exceeds the token threshold. Step 1 (Pruning): For messages older than PruneTurnAge, strip tool result content. Step 2 (Compression): If still over threshold after pruning, summarize older messages using the LLM and write full history to a log file.

type ContextBuilder

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

ContextBuilder assembles the system prompt and runtime context.

func NewContextBuilder

func NewContextBuilder(workspace string, memory *Memory, skillsSummary string) *ContextBuilder

NewContextBuilder creates a new context builder.

func (*ContextBuilder) BuildRuntimeContext

func (cb *ContextBuilder) BuildRuntimeContext(channel, chatID string) string

BuildRuntimeContext returns the runtime context to inject before the user message.

func (*ContextBuilder) BuildSystemPrompt

func (cb *ContextBuilder) BuildSystemPrompt() string

BuildSystemPrompt assembles the system prompt from identity, bootstrap files, memory, and skills.

func (*ContextBuilder) SetGroupContext

func (cb *ContextBuilder) SetGroupContext(gc *GroupContext)

SetGroupContext sets the group chat context for system prompt generation.

type GroupContext

type GroupContext struct {
	BotUsername string   // this agent's bot username
	Teammates   []string // other agent names in the group
}

GroupContext holds information about the group chat environment for system prompt injection.

type Heartbeat

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

Heartbeat runs periodic checks and triggers agent actions.

func NewHeartbeat

func NewHeartbeat(ag *Agent, mb *bus.MessageBus, interval time.Duration) *Heartbeat

NewHeartbeat creates a new heartbeat for the given agent.

func (*Heartbeat) Start

func (hb *Heartbeat) Start(ctx context.Context)

Start begins the heartbeat goroutine. It blocks until ctx is cancelled.

type HeartbeatConfig

type HeartbeatConfig struct {
	Interval time.Duration
}

HeartbeatConfig holds heartbeat configuration.

type HookContext

type HookContext struct {
	AgentName  string
	Point      HookPoint
	Messages   []provider.Message
	ToolName   string // for tool-related hooks
	ToolArgs   string // for BeforeToolCall
	ToolResult string // for AfterToolCall
	Response   *provider.Response
	Error      error
	StartTime  time.Time // set at BeforeModelCall/BeforeToolCall for timing
}

HookContext carries data available to hooks at each hook point.

type HookFunc

type HookFunc func(ctx context.Context, hc *HookContext)

HookFunc is a function that runs at a hook point. It can inspect and modify the HookContext.

func LoggingHook

func LoggingHook() HookFunc

LoggingHook returns a hook function that logs timing information.

type HookPoint

type HookPoint int

HookPoint identifies where in the agent loop a hook fires.

const (
	BeforeSystemPrompt HookPoint = iota
	AfterSystemPrompt
	BeforeModelCall
	AfterModelCall
	BeforeToolCall
	AfterToolCall
)

type HookRegistry

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

HookRegistry stores registered hooks per hook point.

func NewHookRegistry

func NewHookRegistry() *HookRegistry

NewHookRegistry creates a new hook registry.

func (*HookRegistry) Register

func (hr *HookRegistry) Register(point HookPoint, fn HookFunc)

Register adds a hook function for the given hook point.

func (*HookRegistry) Run

func (hr *HookRegistry) Run(ctx context.Context, hc *HookContext)

Run executes all hooks registered at the given point.

type Manager

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

Manager loads and manages all agent instances.

func NewManager

func NewManager(resolved []config.ResolvedAgent, prov provider.Provider, mb *bus.MessageBus) (*Manager, error)

NewManager creates agents from resolved configs.

func (*Manager) AgentByID

func (m *Manager) AgentByID(id string) *Agent

AgentByID returns an agent by its ID.

func (*Manager) All

func (m *Manager) All() []*Agent

All returns all loaded agents.

func (*Manager) DefaultAgent

func (m *Manager) DefaultAgent() *Agent

DefaultAgent returns the default agent (set when only one agent exists).

func (*Manager) Names

func (m *Manager) Names() []string

Names returns all agent IDs.

type Memory

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

Memory manages the dual-layer memory system (MEMORY.md + HISTORY.md).

func NewMemory

func NewMemory(workspace string) *Memory

NewMemory creates a new memory manager.

func (*Memory) AppendHistory

func (m *Memory) AppendHistory(entry string) error

AppendHistory adds an entry to the history log.

func (*Memory) LoadHistory

func (m *Memory) LoadHistory() string

LoadHistory reads the history log.

func (*Memory) LoadMemory

func (m *Memory) LoadMemory() string

LoadMemory reads the long-term memory file.

func (*Memory) ReviewAndUpdateMemory

func (m *Memory) ReviewAndUpdateMemory(workspace string)

ReviewAndUpdateMemory scans recent history entries and appends new key facts to MEMORY.md. This is called by the heartbeat to keep long-term memory fresh.

func (*Memory) SaveMemory

func (m *Memory) SaveMemory(content string) error

SaveMemory overwrites the long-term memory file.

type Skill

type Skill struct {
	Name    string // directory name
	Layer   string // "global", "team", or "agent"
	Content string // contents of SKILL.md
}

Skill represents a discovered skill.

type SkillsLoader

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

SkillsLoader discovers and merges skills from three layers: global > team > agent. Agent-level skills win on conflict.

func NewSkillsLoader

func NewSkillsLoader(homeDir, agentDir, teamDir string, skillsCfg config.SkillsConfig) *SkillsLoader

NewSkillsLoader creates a new skills loader.

func (*SkillsLoader) BuildSkillsSummary

func (sl *SkillsLoader) BuildSkillsSummary(skills []Skill) string

BuildSkillsSummary returns an XML summary of all skills for the system prompt. Always-load skills get their full content injected.

func (*SkillsLoader) LoadSkills

func (sl *SkillsLoader) LoadSkills() []Skill

LoadSkills discovers skills from all layers and returns them merged (agent wins on conflict).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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