persona

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2025 License: AGPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package persona provides functionality for defining and managing different LLM personas.

Overview

The Persona system provides a way to define different roles or personalities for the LLM agent, each with its own set of tools and system prompts. A persona defines:

  1. A set of tools available to the LLM
  2. A system prompt that guides the LLM's behavior
  3. A unique identifier and descriptive name

Usage

Selecting a Persona:

You can select a persona when starting the chat application using the --persona flag:

aigent chat --persona software_engineer

Available Personas:

Currently, the following personas are available:

  • Software Engineer (software_engineer): A persona focused on software engineering tasks, providing tools for code analysis, development, and debugging.

Creating a New Persona

To create a new persona:

  1. Implement the Persona interface defined in persona.go
  2. Register the persona in the persona registry

Example implementation:

// MyNewPersona represents a custom persona for the LLM agent.
type MyNewPersona struct {
    tools     []llm.Tool
    executors map[string]llm.ToolExecutor
    logger    *slog.Logger
}

// NewMyNewPersona creates a new instance of the custom persona.
func NewMyNewPersona(logger *slog.Logger) *MyNewPersona {
    // Initialize with tools and executors
    // ...
    return persona
}

// Implement the Persona interface methods
// ...

// AddMCPTools adds tools from MCP servers to this persona.
func (p *MyNewPersona) AddMCPTools(mcpManager *mcp.Manager) {
    // Add MCP tools as needed
    // ...
}

Then register it in the persona registry:

// SetupPersonaRegistry creates and configures a persona registry with available personas.
func SetupPersonaRegistry(logger *slog.Logger) *persona.Registry {
    registry := persona.NewRegistry(
        persona.WithRegistryLogger(logger),
        persona.WithPersona(persona.NewSoftwareEngineer(logger)),
        persona.WithPersona(persona.NewMyNewPersona(logger)), // Add your new persona here
    )
    return registry
}

MCP Tools Support

Personas can include tools from MCP (Machine Cognition Protocol) servers. The system works as follows:

  1. All MCP tools defined in aigent.yaml are automatically added to every persona
  2. Additional hard-coded MCP tools can be specified per persona using the GetMCPToolSpecs() method
  3. These hard-coded tools would typically be from servers not defined in aigent.yaml

Example of defining additional MCP tools for a persona:

// GetMCPToolSpecs returns the MCP tool specifications for this persona.
func (p *MyPersona) GetMCPToolSpecs() []MCPToolSpec {
    return []MCPToolSpec{
        {
            // Additional server not in aigent.yaml
            ServerName: "specialized-tools",
        },
        {
            // Another additional server with specific tools
            ServerName: "advanced-tools",
            ToolNames:  []string{"special-tool-1", "special-tool-2"},
        },
    }
}

Architecture

The persona system consists of:

  • Persona interface: Defines the contract for all personas
  • Registry: Manages available personas and provides access to them
  • Integration with the chat system to use the selected persona's tools and system prompt

The Software Engineer persona is provided as the default implementation, which includes:

  • A hardcoded "buildin" MCP server with tools like read_file and run_command
  • Support for automatically adding all tools from MCP servers defined in aigent.yaml
  • A system prompt tailored for software engineering tasks

Package persona provides functionality for defining and managing different LLM personas.

Index

Constants

This section is empty.

Variables

View Source
var DefaultToolRiskAnalyzer = NewToolRiskAnalyzer(nil)

DefaultToolRiskAnalyzer is a global instance for convenience

Functions

func AnalyzeAndRegisterMCPTool added in v0.0.2

func AnalyzeAndRegisterMCPTool(serverName string, mcpTool mcplib.ToolRetType) toolapproval.RiskLevel

AnalyzeAndRegisterMCPTool is a convenience function that uses the default analyzer

func AnalyzeMCPToolRisk added in v0.0.2

func AnalyzeMCPToolRisk(serverName string, mcpTool mcplib.ToolRetType, logger *slog.Logger) toolapproval.RiskLevel

AnalyzeMCPToolRisk is a convenience function that uses the default analyzer

func MCPToolToLLMTool

func MCPToolToLLMTool(serverName string, mcpTool mcplib.ToolRetType) llm.Tool

MCPToolToLLMTool converts an MCP tool to an LLM tool

Types

type MCPToolSpec

type MCPToolSpec struct {
	// ServerName is the name of the MCP server
	ServerName string
	// ToolNames is a list of tool names to include from this server
	// If empty, all tools from the server will be included
	ToolNames []string
}

MCPToolSpec specifies which MCP tools a persona wants to use

type Persona

type Persona interface {
	// ID returns the unique identifier for this persona
	ID() string

	// Name returns a human-readable name for this persona
	Name() string

	// Description returns a description of this persona
	Description() string

	// GetTools returns the tools available to this persona
	GetTools() []llm.Tool

	// GetToolExecutors returns the tool executors for this persona's tools
	GetToolExecutors() map[string]llm.ToolExecutor

	// GetSystemPrompt returns the system prompt for this persona
	GetSystemPrompt() string

	// AddMCPTools adds tools from MCP servers to this persona
	AddMCPTools(mcpManager mcp.Manager)

	// GetMCPToolSpecs returns the MCP tool specifications for this persona
	// These specify which MCP servers and tools the persona wants to use
	GetMCPToolSpecs() []MCPToolSpec

	// Cleanup releases resources used by this persona
	Cleanup()
}

Persona represents a specific role or personality for the LLM agent, which defines available tools and prompts.

type Registry

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

Registry maintains a collection of available personas

func NewRegistry

func NewRegistry(opts ...RegistryOption) *Registry

NewRegistry creates a new persona registry with optional configuration

func (*Registry) CleanupAllPersonas

func (r *Registry) CleanupAllPersonas()

CleanupAllPersonas calls the Cleanup method on all registered personas

func (*Registry) CleanupPersona

func (r *Registry) CleanupPersona(personaID string) error

CleanupPersona calls the Cleanup method on a persona to release its resources

func (*Registry) CreateToolRegistry

func (r *Registry) CreateToolRegistry(personaID string) (*llm.ToolRegistry, error)

CreateToolRegistry creates a tool registry from a persona

func (*Registry) GetPersona

func (r *Registry) GetPersona(personaID string) (Persona, error)

GetPersona returns a persona by its ID

func (*Registry) ListPersonas

func (r *Registry) ListPersonas() []Persona

ListPersonas returns all registered personas

func (*Registry) RegisterPersona

func (r *Registry) RegisterPersona(persona Persona)

RegisterPersona adds a persona to the registry

type RegistryOption

type RegistryOption func(*Registry)

RegistryOption defines functional options for configuring Registry

func WithPersona

func WithPersona(persona Persona) RegistryOption

WithPersona registers a persona during initialization

func WithRegistryLogger

func WithRegistryLogger(logger *slog.Logger) RegistryOption

WithRegistryLogger sets the logger for the Registry

type SoftwareEngineer

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

SoftwareEngineer represents a software engineer persona for the LLM agent.

func NewSoftwareEngineer

func NewSoftwareEngineer(logger *slog.Logger) *SoftwareEngineer

NewSoftwareEngineer creates a new software engineer persona with default tools.

func (*SoftwareEngineer) AddMCPTools

func (se *SoftwareEngineer) AddMCPTools(mcpManager mcp.Manager)

AddMCPTools adds tools from MCP servers to this persona. For the "buildin" server, it starts it directly. For other servers, it would use the mcpManager (not fully implemented here yet for others).

func (*SoftwareEngineer) Cleanup

func (se *SoftwareEngineer) Cleanup()

Cleanup releases resources used by this persona, such as hardcoded servers.

func (*SoftwareEngineer) Description

func (se *SoftwareEngineer) Description() string

Description returns a description of this persona.

func (*SoftwareEngineer) GetMCPToolSpecs

func (se *SoftwareEngineer) GetMCPToolSpecs() []MCPToolSpec

GetMCPToolSpecs returns the MCP tool specifications for this persona.

func (*SoftwareEngineer) GetSystemPrompt

func (se *SoftwareEngineer) GetSystemPrompt() string

GetSystemPrompt returns the system prompt for this persona.

func (*SoftwareEngineer) GetToolExecutors

func (se *SoftwareEngineer) GetToolExecutors() map[string]llm.ToolExecutor

GetToolExecutors returns the tool executors for this persona's tools.

func (*SoftwareEngineer) GetTools

func (se *SoftwareEngineer) GetTools() []llm.Tool

GetTools returns the tools available to this persona.

func (*SoftwareEngineer) ID

func (se *SoftwareEngineer) ID() string

ID returns the unique identifier for this persona.

func (*SoftwareEngineer) Name

func (se *SoftwareEngineer) Name() string

Name returns a human-readable name for this persona.

type ToolRiskAnalyzer added in v0.0.2

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

ToolRiskAnalyzer is responsible for analyzing and registering risk levels for tools as they are added to personas

func NewToolRiskAnalyzer added in v0.0.2

func NewToolRiskAnalyzer(logger *slog.Logger) *ToolRiskAnalyzer

NewToolRiskAnalyzer creates a new tool risk analyzer

func (*ToolRiskAnalyzer) AnalyzeAndRegisterToolRisk added in v0.0.2

func (a *ToolRiskAnalyzer) AnalyzeAndRegisterToolRisk(
	serverName string,
	mcpTool mcplib.ToolRetType,
) toolapproval.RiskLevel

AnalyzeAndRegisterToolRisk analyzes a tool's risk level and registers it in the default tool risk registry

Jump to

Keyboard shortcuts

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