agents

package
v0.0.82 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package agents provides named configuration bundles for term-llm. Agents combine system prompts, tool sets, model preferences, and MCP servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CopyAgent

func CopyAgent(src *Agent, destDir, newName string) error

CopyAgent copies an agent to a new location.

func CreateAgentDir

func CreateAgentDir(baseDir, name string) error

CreateAgentDir creates an agent directory with template files.

func DiscoverProjectInstructions added in v0.0.43

func DiscoverProjectInstructions() string

DiscoverProjectInstructions searches for project agent instruction files. First checks the current directory, then walks up to the git root (if any). Returns the content of the first file found, with a header indicating the source. Returns empty string if no files are found. Exported for use by cmd packages when project_instructions is enabled.

func ExpandTemplate

func ExpandTemplate(text string, ctx TemplateContext) string

ExpandTemplate replaces {{variable}} placeholders with values from context.

func ExtractBuiltinResources

func ExtractBuiltinResources(name string) (string, error)

ExtractBuiltinResources extracts additional resource files for a builtin agent to the cache directory. This extracts all .md files except system.md (which is loaded into the agent struct).

func GetBuiltinAgentNames

func GetBuiltinAgentNames() []string

GetBuiltinAgentNames returns the names of all built-in agents.

func GetBuiltinResourceDir

func GetBuiltinResourceDir() (string, error)

GetBuiltinResourceDir returns the cache directory where builtin agent resources are extracted. Uses $XDG_CACHE_HOME if set, otherwise ~/.cache

func GetLocalAgentsDir

func GetLocalAgentsDir() (string, error)

GetLocalAgentsDir returns the path for project-local agents.

func GetUserAgentsDir

func GetUserAgentsDir() (string, error)

GetUserAgentsDir returns the path for user-global agents.

func IsAgentPath added in v0.0.65

func IsAgentPath(value string) bool

IsAgentPath returns true if the value looks like a filesystem path rather than an agent name. It checks for forward slashes, backslashes (for Windows-style paths, including from WSL/mixed-shell usage), and Windows drive-letter prefixes (e.g., C:\).

func IsBuiltinAgent

func IsBuiltinAgent(name string) bool

IsBuiltinAgent checks if an agent name is a built-in.

Types

type Agent

type Agent struct {
	// Metadata
	Name        string `yaml:"name"`
	Description string `yaml:"description"`

	// Model preferences (optional)
	Provider string `yaml:"provider,omitempty"`
	Model    string `yaml:"model,omitempty"`

	// Tool configuration
	Tools ToolsConfig `yaml:"tools,omitempty"`

	// Tool-specific settings
	Shell ShellConfig `yaml:"shell,omitempty"`
	Read  ReadConfig  `yaml:"read,omitempty"`
	Spawn SpawnConfig `yaml:"spawn,omitempty"`

	// Behavior
	MaxTurns int  `yaml:"max_turns,omitempty"`
	Search   bool `yaml:"search,omitempty"` // Enable web search tools

	// DefaultPrompt is used when agent is invoked without a message
	DefaultPrompt string `yaml:"default_prompt,omitempty"`

	// Output specifies where to write agent response (deprecated, use OutputTool + OnComplete)
	// Valid: "" (stdout), "commit_editmsg" (.git/COMMIT_EDITMSG)
	Output string `yaml:"output,omitempty"`

	// OutputTool configures a tool for capturing structured output.
	// When set, a tool with the configured name is dynamically created
	// and added to the agent's enabled tools.
	OutputTool OutputToolConfig `yaml:"output_tool,omitempty"`

	// OnComplete is a shell command to run with captured output piped to stdin.
	// Runs in the git repo root (if in a git repo) or cwd.
	// Replaces the hardcoded "output: commit_editmsg" approach.
	OnComplete string `yaml:"on_complete,omitempty"`

	// Include additional .md files in the system prompt
	// Files are loaded from the agent directory and appended after system.md
	Include []string `yaml:"include,omitempty"`

	// ProjectInstructions controls auto-loading of AGENTS.md, CLAUDE.md, etc.
	// Values: "auto" (default), "true", "false"
	// "auto" includes project instructions if the agent has coding tools (write_file, edit_file, shell)
	ProjectInstructions string `yaml:"project_instructions,omitempty"`

	// MCP servers to auto-connect
	MCP []MCPConfig `yaml:"mcp,omitempty"`

	// GistID for syncing with GitHub Gists (set on export/import)
	GistID string `yaml:"gist_id,omitempty"`

	// System prompt (loaded from system.md + included files)
	SystemPrompt string `yaml:"-"`

	// Source info
	Source     AgentSource `yaml:"-"`
	SourcePath string      `yaml:"-"`
}

Agent represents a named configuration bundle.

func LoadFromDir

func LoadFromDir(dir string, source AgentSource) (*Agent, error)

LoadFromDir loads an agent from a directory containing agent.yaml and optionally system.md.

func LoadFromEmbedded

func LoadFromEmbedded(name string, agentYAML, systemMD []byte) (*Agent, error)

LoadFromEmbedded loads an agent from embedded filesystem data.

func LoadFromPath added in v0.0.65

func LoadFromPath(path string) (*Agent, error)

LoadFromPath loads an agent from an explicit filesystem path. The path is resolved to absolute. Returns an error if the directory doesn't exist or doesn't contain agent.yaml.

func (*Agent) GetEnabledTools

func (a *Agent) GetEnabledTools(allTools []string) []string

GetEnabledTools returns the list of enabled tools. If Enabled is set, returns that list. If Disabled is set, returns all tools except disabled ones. If neither is set, returns nil (use default).

func (*Agent) GetMCPServerNames

func (a *Agent) GetMCPServerNames() []string

GetMCPServerNames returns the names of MCP servers to connect.

func (*Agent) HasDisabledList

func (a *Agent) HasDisabledList() bool

HasDisabledList returns true if the agent uses a disabled list.

func (*Agent) HasEnabledList

func (a *Agent) HasEnabledList() bool

HasEnabledList returns true if the agent uses an explicit enabled list.

func (*Agent) Merge added in v0.0.42

func (a *Agent) Merge(pref config.AgentPreference)

Merge applies preferences on top of the agent's existing configuration. Non-zero/non-nil values in pref override the agent's defaults.

func (*Agent) ShouldLoadProjectInstructions added in v0.0.43

func (a *Agent) ShouldLoadProjectInstructions() bool

ShouldLoadProjectInstructions returns true if this agent should load project instruction files. Uses "auto" logic by default: include if agent has coding tools (write_file, edit_file, shell).

func (*Agent) String

func (a *Agent) String() string

String returns a brief description of the agent.

func (*Agent) Validate

func (a *Agent) Validate() error

Validate checks that the agent configuration is valid.

type AgentSource

type AgentSource int

AgentSource indicates where an agent was loaded from.

const (
	SourceLocal   AgentSource = iota // Project-local (./term-llm-agents/)
	SourceUser                       // User-global (~/.config/term-llm/agents/)
	SourceBuiltin                    // Embedded built-in
)

func (AgentSource) SourceName

func (s AgentSource) SourceName() string

SourceName returns a human-readable name for the agent source.

type MCPConfig

type MCPConfig struct {
	Name    string `yaml:"name"`
	Command string `yaml:"command,omitempty"`
}

MCPConfig specifies an MCP server to connect.

type OutputToolConfig added in v0.0.37

type OutputToolConfig struct {
	Name        string `yaml:"name"`        // Tool name (e.g., "set_commit_message")
	Param       string `yaml:"param"`       // Parameter to capture (default: "content")
	Description string `yaml:"description"` // Tool description
}

OutputToolConfig configures a tool for capturing structured output.

func (*OutputToolConfig) IsConfigured added in v0.0.37

func (c *OutputToolConfig) IsConfigured() bool

IsConfigured returns true if the output tool is configured.

type ReadConfig

type ReadConfig struct {
	Dirs []string `yaml:"dirs,omitempty"`
}

ReadConfig provides read tool settings.

type Registry

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

Registry manages agent discovery and resolution.

func NewRegistry

func NewRegistry(cfg RegistryConfig) (*Registry, error)

NewRegistry creates an agent registry with standard paths.

func (*Registry) Get

func (r *Registry) Get(name string) (*Agent, error)

Get retrieves an agent by name. Resolution order: local > user > search paths > builtin Preferences are applied on top of the loaded agent config.

func (*Registry) List

func (r *Registry) List() ([]*Agent, error)

List returns all available agents. Each agent appears only once, with first-found taking precedence.

func (*Registry) ListBySource

func (r *Registry) ListBySource(source AgentSource) ([]*Agent, error)

ListBySource returns agents from a specific source.

func (*Registry) ListNames added in v0.0.43

func (r *Registry) ListNames() ([]string, error)

ListNames returns just the names of available agents without loading full content. This is optimized for completions where only names are needed.

func (*Registry) SetPreferences added in v0.0.42

func (r *Registry) SetPreferences(prefs map[string]config.AgentPreference)

SetPreferences sets the preference overrides to apply to agents. Call this after creating the registry to configure agent preferences. This also invalidates the cache for any agents that have preferences set, ensuring the new preferences are applied on next Get().

type RegistryConfig

type RegistryConfig struct {
	UseBuiltin  bool
	SearchPaths []string
}

RegistryConfig configures the agent registry.

type ShellConfig

type ShellConfig struct {
	Allow   []string          `yaml:"allow,omitempty"`
	AutoRun bool              `yaml:"auto_run,omitempty"`
	Scripts map[string]string `yaml:"scripts,omitempty"` // Named scripts (auto-approved)
}

ShellConfig provides shell tool settings.

type SpawnConfig added in v0.0.35

type SpawnConfig struct {
	MaxParallel    int      `yaml:"max_parallel,omitempty"`   // Max concurrent sub-agents (default 3)
	MaxDepth       int      `yaml:"max_depth,omitempty"`      // Max nesting level (default 2)
	DefaultTimeout int      `yaml:"timeout,omitempty"`        // Default timeout in seconds (default 300)
	AllowedAgents  []string `yaml:"allowed_agents,omitempty"` // Optional whitelist of allowed agents
}

SpawnConfig configures spawn_agent behavior for this agent.

type TemplateContext

type TemplateContext struct {
	// Time-related
	Date     string // YYYY-MM-DD
	DateTime string // YYYY-MM-DD HH:MM:SS
	Time     string // HH:MM
	Year     string // YYYY

	// Directory info
	Cwd     string // Full working directory
	CwdName string // Directory name only
	Home    string // Home directory
	User    string // Username

	// Git info (empty if not a git repo)
	GitBranch   string // Current branch
	GitRepo     string // Repository name
	GitDiffStat string // Output of git diff --stat (staged + unstaged)

	// File context (from -f flags)
	Files     string // Comma-separated file names
	FileCount string // Number of files

	// System
	OS string // Operating system

	// Agent context
	ResourceDir string // Directory containing agent resources (for builtin agents)

	// Project agent instructions (dynamically discovered)
	// Searches in priority order: AGENTS.md, CLAUDE.md, .github/copilot-instructions.md,
	// .cursor/rules, CONTRIBUTING.md - returns first found
	Agents string
}

TemplateContext holds values for template variable expansion.

func NewTemplateContext

func NewTemplateContext() TemplateContext

NewTemplateContext creates a context with current environment values. Deprecated: Use NewTemplateContextForTemplate instead to avoid expensive operations when template variables are not used.

func NewTemplateContextForTemplate added in v0.0.34

func NewTemplateContextForTemplate(template string) TemplateContext

NewTemplateContextForTemplate creates a context, only computing expensive values (like git_diff_stat, agents) if they are actually used in the template.

func (TemplateContext) WithFiles

func (c TemplateContext) WithFiles(files []string) TemplateContext

WithFiles adds file context to the template context.

func (TemplateContext) WithResourceDir

func (c TemplateContext) WithResourceDir(resourceDir string) TemplateContext

WithResourceDir sets the resource directory for an agent.

type ToolsConfig

type ToolsConfig struct {
	// Enabled is an explicit allow list of tools
	Enabled []string `yaml:"enabled,omitempty"`
	// Disabled is a deny list (all others enabled)
	Disabled []string `yaml:"disabled,omitempty"`
}

ToolsConfig specifies which tools to enable or disable.

Directories

Path Synopsis
Package gist provides GitHub Gist operations for agent sharing.
Package gist provides GitHub Gist operations for agent sharing.

Jump to

Keyboard shortcuts

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