reflection

package
v0.4.4 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package reflection provides self-evaluation and improvement capabilities for agents

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Evaluation

type Evaluation struct {
	Strengths     []string               `json:"strengths"`
	Weaknesses    []string               `json:"weaknesses"`
	Opportunities []string               `json:"opportunities"`
	Threats       []string               `json:"threats"`
	Score         float64                `json:"score"` // 0-1
	Details       map[string]interface{} `json:"details,omitempty"`
}

Evaluation represents an evaluation of performance

type Experience

type Experience struct {
	ID          string                 `json:"id"`
	Description string                 `json:"description"`
	Input       interface{}            `json:"input"`
	Output      interface{}            `json:"output"`
	Success     bool                   `json:"success"`
	Duration    time.Duration          `json:"duration"`
	Error       string                 `json:"error,omitempty"`
	Context     map[string]interface{} `json:"context,omitempty"`
	Timestamp   time.Time              `json:"timestamp"`
}

Experience represents an experience to learn from

type Improvement

type Improvement struct {
	Description   string                 `json:"description"`
	Priority      int                    `json:"priority"` // 1-5
	Impact        float64                `json:"impact"`   // Expected impact 0-1
	Effort        float64                `json:"effort"`   // Required effort 0-1
	ActionItems   []string               `json:"action_items"`
	Prerequisites []string               `json:"prerequisites,omitempty"`
	Metadata      map[string]interface{} `json:"metadata,omitempty"`
}

Improvement represents a suggested improvement

type Insight

type Insight struct {
	Description string    `json:"description"`
	Category    string    `json:"category"`
	Importance  float64   `json:"importance"` // 0-1
	Evidence    []string  `json:"evidence,omitempty"`
	Timestamp   time.Time `json:"timestamp"`
}

Insight represents a key insight from reflection

type LearningModel

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

LearningModel manages and applies learned knowledge

func NewLearningModel

func NewLearningModel() *LearningModel

NewLearningModel creates a new learning model

func (*LearningModel) AddLearning

func (m *LearningModel) AddLearning(learning LearningPoint)

AddLearning adds a new learning to the model

func (*LearningModel) GetLearningsByCategory

func (m *LearningModel) GetLearningsByCategory(category string) []LearningPoint

GetLearningsByCategory retrieves learnings by category

func (*LearningModel) GetRelevantLearnings

func (m *LearningModel) GetRelevantLearnings(context interface{}) []LearningPoint

GetRelevantLearnings retrieves learnings relevant to the given context

func (*LearningModel) GetStatistics

func (m *LearningModel) GetStatistics() map[string]interface{}

GetStatistics returns statistics about the learning model

func (*LearningModel) PruneLowEffectiveness

func (m *LearningModel) PruneLowEffectiveness(threshold float64) int

PruneLowEffectiveness removes learnings with consistently low effectiveness

func (*LearningModel) UpdateEffectiveness

func (m *LearningModel) UpdateEffectiveness(learningID string, success bool)

UpdateEffectiveness updates the effectiveness of a learning based on outcomes

func (*LearningModel) UpdateWithLearnings

func (m *LearningModel) UpdateWithLearnings(learnings []LearningPoint)

UpdateWithLearnings updates the model with multiple learnings

type LearningPoint

type LearningPoint struct {
	Lesson        string    `json:"lesson"`
	Context       string    `json:"context"`
	Category      string    `json:"category"`
	Applicability float64   `json:"applicability"` // How widely applicable 0-1
	Confidence    float64   `json:"confidence"`    // Confidence in the learning 0-1
	Examples      []string  `json:"examples,omitempty"`
	Timestamp     time.Time `json:"timestamp"`
}

LearningPoint represents something learned from experience

type PerformanceMetrics

type PerformanceMetrics struct {
	SuccessRate     float64                `json:"success_rate"`
	AverageTime     time.Duration          `json:"average_time"`
	ErrorRate       float64                `json:"error_rate"`
	TotalExecutions int                    `json:"total_executions"`
	ResourceUsage   map[string]float64     `json:"resource_usage"`
	CustomMetrics   map[string]interface{} `json:"custom_metrics,omitempty"`
}

PerformanceMetrics contains performance metrics for evaluation

type ReflectionMetrics

type ReflectionMetrics struct {
	TotalReflections       int                      `json:"total_reflections"`
	AverageScore           float64                  `json:"average_score"`
	ImprovementRate        float64                  `json:"improvement_rate"`
	LearningsGenerated     int                      `json:"learnings_generated"`
	LearningsApplied       int                      `json:"learnings_applied"`
	SuccessfulApplications int                      `json:"successful_applications"`
	CategoryPerformance    map[string]float64       `json:"category_performance"`
	TimeMetrics            map[string]time.Duration `json:"time_metrics"`
	// contains filtered or unexported fields
}

ReflectionMetrics tracks metrics for reflection performance

func NewReflectionMetrics

func NewReflectionMetrics() *ReflectionMetrics

NewReflectionMetrics creates new reflection metrics

func (*ReflectionMetrics) GetImprovementRate

func (m *ReflectionMetrics) GetImprovementRate() float64

GetImprovementRate calculates the improvement rate

func (*ReflectionMetrics) GetSummary

func (m *ReflectionMetrics) GetSummary() map[string]interface{}

GetSummary returns a summary of reflection metrics

func (*ReflectionMetrics) RecordLearningApplication

func (m *ReflectionMetrics) RecordLearningApplication(success bool)

RecordLearningApplication records when a learning is applied

func (*ReflectionMetrics) RecordReflection

func (m *ReflectionMetrics) RecordReflection(result *ReflectionResult)

RecordReflection records a reflection event

type ReflectionOption

type ReflectionOption func(*SelfReflectiveAgent)

ReflectionOption configures the reflective agent

func WithLearningThreshold

func WithLearningThreshold(threshold float64) ReflectionOption

WithLearningThreshold sets the learning threshold

func WithReflectionInterval

func WithReflectionInterval(interval time.Duration) ReflectionOption

WithReflectionInterval sets the reflection interval

type ReflectionResult

type ReflectionResult struct {
	ID               string                 `json:"id"`
	Type             ReflectionType         `json:"type"`
	Subject          string                 `json:"subject"`
	Timestamp        time.Time              `json:"timestamp"`
	Evaluation       *Evaluation            `json:"evaluation"`
	Insights         []Insight              `json:"insights"`
	Improvements     []Improvement          `json:"improvements"`
	LearningPoints   []LearningPoint        `json:"learning_points"`
	PerformanceScore float64                `json:"performance_score"`
	Confidence       float64                `json:"confidence"`
	Metadata         map[string]interface{} `json:"metadata,omitempty"`
}

ReflectionResult represents the result of a reflection process

type ReflectionType

type ReflectionType string

ReflectionType represents the type of reflection

const (
	ReflectionTypeSelfEvaluation     ReflectionType = "self_evaluation"
	ReflectionTypePerformanceReview  ReflectionType = "performance_review"
	ReflectionTypeStrategyAnalysis   ReflectionType = "strategy_analysis"
	ReflectionTypeLearningExtraction ReflectionType = "learning_extraction"
	ReflectionTypeErrorAnalysis      ReflectionType = "error_analysis"
)

type ReflectiveAgent

type ReflectiveAgent interface {
	core.Agent

	// Reflect performs self-reflection
	Reflect(ctx context.Context, subject interface{}) (*ReflectionResult, error)

	// EvaluatePerformance evaluates recent performance
	EvaluatePerformance(ctx context.Context, metrics PerformanceMetrics) (*Evaluation, error)

	// ExtractLearnings extracts learnings from experiences
	ExtractLearnings(ctx context.Context, experiences []Experience) ([]LearningPoint, error)

	// GenerateImprovements generates improvement suggestions
	GenerateImprovements(ctx context.Context, evaluation *Evaluation) ([]Improvement, error)

	// ApplyLearnings applies learnings to improve behavior
	ApplyLearnings(ctx context.Context, learnings []LearningPoint) error
}

ReflectiveAgent interface for agents with reflection capabilities

type SelfReflectiveAgent

type SelfReflectiveAgent struct {
	*core.BaseAgent
	// contains filtered or unexported fields
}

SelfReflectiveAgent implements reflection capabilities

func NewSelfReflectiveAgentWithContext

func NewSelfReflectiveAgentWithContext(parentCtx context.Context, llmClient llm.Client, mem memory.EnhancedMemory, opts ...ReflectionOption) *SelfReflectiveAgent

NewSelfReflectiveAgentWithContext creates a new self-reflective agent with a parent context

func (*SelfReflectiveAgent) ApplyLearnings

func (a *SelfReflectiveAgent) ApplyLearnings(ctx context.Context, learnings []LearningPoint) error

ApplyLearnings applies learnings to improve behavior

func (*SelfReflectiveAgent) EvaluatePerformance

func (a *SelfReflectiveAgent) EvaluatePerformance(ctx context.Context, metrics PerformanceMetrics) (*Evaluation, error)

EvaluatePerformance evaluates recent performance

func (*SelfReflectiveAgent) Execute

Execute implements the Agent interface with reflection

func (*SelfReflectiveAgent) ExtractLearnings

func (a *SelfReflectiveAgent) ExtractLearnings(ctx context.Context, experiences []Experience) ([]LearningPoint, error)

ExtractLearnings extracts learnings from experiences

func (*SelfReflectiveAgent) GenerateImprovements

func (a *SelfReflectiveAgent) GenerateImprovements(ctx context.Context, evaluation *Evaluation) ([]Improvement, error)

GenerateImprovements generates improvement suggestions

func (*SelfReflectiveAgent) Reflect

func (a *SelfReflectiveAgent) Reflect(ctx context.Context, subject interface{}) (*ReflectionResult, error)

Reflect performs self-reflection on a subject

func (*SelfReflectiveAgent) Shutdown

func (a *SelfReflectiveAgent) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the agent

Jump to

Keyboard shortcuts

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