Documentation
ΒΆ
Overview ΒΆ
Package brain provides intelligent orchestration capabilities for HotPlex. This file (config.go) defines configuration structures loaded from environment variables.
Configuration Hierarchy ΒΆ
Config (root) βββ Model (LLM backend settings) βββ Cache (response caching) βββ Retry (retry policy) βββ Metrics (observability) βββ Cost (cost tracking) βββ RateLimit (throttling) βββ Router (model routing) βββ CircuitBreaker (fault tolerance) βββ Failover (provider failover) βββ Budget (budget limits) βββ Priority (request prioritization) βββ IntentRouter (message classification) βββ Memory (context compression) βββ Guard (safety guardrails)
Environment Variables ΒΆ
All config is loaded from environment variables with prefix HOTPLEX_BRAIN_. See LoadConfigFromEnv() for the full list of variables.
Package brain provides intelligent orchestration capabilities for HotPlex. It includes safety guardrails, intent routing, and context compression.
The SafetyGuard component (this file) provides multi-layer security:
- Input validation: Pattern-based blocking + AI-assisted threat detection
- Output sanitization: Redacts API keys, credentials, internal IPs
- Chat2Config: Natural language configuration commands (disabled by default)
Architecture ΒΆ
SafetyGuard βββ CheckInput() β Pattern scan β Brain analysis β allow/block βββ CheckOutput() β Pattern match β sanitize β allow βββ ParseConfigIntent() β Brain NLU β ExecuteConfigIntent()
Threat Detection Flow ΒΆ
- Fast path: Regex patterns catch obvious attacks (prompt injection, jailbreak)
- Deep analysis: Brain AI classifies subtle threats with confidence scores
- Action: allow (safe), block (threat), or sanitize (redact sensitive data)
Package brain provides intelligent orchestration capabilities for HotPlex.
The ContextCompressor component (this file) manages conversation context to prevent context window overflow while preserving important information.
Problem ΒΆ
Long conversations exceed LLM context limits, causing:
- API errors (context length exceeded)
- Loss of earlier conversation context
- Increased token costs
Solution ΒΆ
Context compression reduces context size while preserving key information:
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Session History (before: 8000+ tokens) β
β βββββββ βββββββ βββββββ βββββββ βββββββ βββββββ β
β βTurn1β βTurn2β β ... β βTurn8β βTurn9β βTurn10β β
β βββββββ βββββββ βββββββ βββββββ βββββββ βββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β
βΌ Compression (threshold reached)
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Compressed Session (after: ~2000 tokens) β
β βββββββββββββββββββ βββββββ βββββββ βββββββ β
β β Summary of 1-7 β βTurn8β βTurn9β βTurn10β β
β β (~500 tokens) β βββββββ βββββββ βββββββ β
β βββββββββββββββββββ β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
Compression Algorithm ΒΆ
- Wait until token count exceeds TokenThreshold (default: 8000)
- Keep last PreserveTurns (default: 5) turns intact
- Summarize older turns using Brain AI (max MaxSummaryTokens)
- Replace old turns with summary, update total token count
MemoryManager ΒΆ
Extends ContextCompressor with user preference tracking:
- Stores user preferences across sessions (programming language, style)
- Extracts preferences from conversations using Brain AI
- Injects preferences into Engine prompts for personalization
Package brain provides intelligent orchestration capabilities for HotPlex.
The IntentRouter component (this file) classifies incoming messages to determine the optimal processing path:
βββββββββββββββ ββββββββββββββββ
β User Message ββββββΆβ IntentRouter β
βββββββββββββββ ββββββββ¬ββββββββ
β
ββββββββββββββββΌβββββββββββββββ
βΌ βΌ βΌ
[chat] [command] [task]
Brain handles Brain handles Engine handles
casual chat status/config code, debugging
Intent Types ΒΆ
- chat: Casual conversation, greetings, small talk β Brain generates response
- command: Status queries, config commands β Brain generates response
- task: Code operations, debugging, analysis β Forward to Engine Provider
- unknown: Ambiguous intent β Default to Engine for safety
Optimization ΒΆ
Fast-path detection handles obvious cases without Brain API calls:
- Greetings ("hi", "hello") β chat
- Status commands ("ping", "status") β command
- Code keywords ("function", "debug") β task
Cache reduces repeated Brain API calls for similar messages.
Index ΒΆ
- Constants
- func DefaultBanPatterns() []string
- func GetRateLimiter() *llm.RateLimiter
- func GetRouter() *llm.Router
- func GetSessionSummary(sessionID string) string
- func Init(logger *slog.Logger) error
- func InitGuard(config GuardConfig, logger *slog.Logger) error
- func InitIntentRouter(config IntentRouterConfig, logger *slog.Logger)
- func InitMemory(config CompressionConfig, logger *slog.Logger)
- func IsRelevant(ctx context.Context, msg string, botMentioned bool) bool
- func QuickResponse(ctx context.Context, msg string, result *IntentResult) (string, error)
- func RecordTurn(sessionID, role, content string, tokenCount int)
- func SanitizeOutputString(output string) string
- func SetGlobal(b Brain)
- type Brain
- type BudgetConfig
- type BudgetControlledBrain
- type CacheConfig
- type CircuitBreakerConfig
- type CompressionConfig
- type Config
- type ConfigIntent
- type ContextCompressor
- func (c *ContextCompressor) CheckAndCompress(ctx context.Context, sessionID string) (*SessionHistory, error)
- func (c *ContextCompressor) ClearExpired() int
- func (c *ContextCompressor) ClearSession(sessionID string)
- func (c *ContextCompressor) ForceCompress(ctx context.Context, sessionID string) (*SessionHistory, error)
- func (c *ContextCompressor) GetContextForEngine(sessionID string) string
- func (c *ContextCompressor) GetHistory(sessionID string) *SessionHistory
- func (c *ContextCompressor) GetSessionSummary(sessionID string) string
- func (c *ContextCompressor) RecordTurn(sessionID, role, content string, tokenCount int)
- func (c *ContextCompressor) SetEnabled(enabled bool)
- func (c *ContextCompressor) Stats() map[string]interface{}
- func (c *ContextCompressor) Stop()
- func (c *ContextCompressor) UpdateConfig(config CompressionConfig)
- type CostConfig
- type FailoverConfig
- type GuardConfig
- type GuardResult
- type HealthStatus
- type IntentResult
- type IntentRouter
- func (r *IntentRouter) ClearCache()
- func (r *IntentRouter) GenerateResponse(ctx context.Context, msg string, intent *IntentResult) (string, error)
- func (r *IntentRouter) GetEnabled() bool
- func (r *IntentRouter) IsRelevant(ctx context.Context, msg string, botMentioned bool) bool
- func (r *IntentRouter) Route(ctx context.Context, msg string) *IntentResult
- func (r *IntentRouter) RouteWithHistory(ctx context.Context, msg string, history []string) *IntentResult
- func (r *IntentRouter) SetEnabled(enabled bool)
- func (r *IntentRouter) ShouldUseEngine(result *IntentResult) bool
- func (r *IntentRouter) Stats() map[string]interface{}
- type IntentRouterConfig
- type IntentRouterFeatureConfig
- type IntentType
- type MemoryCompressionConfig
- type MemoryManager
- func (m *MemoryManager) ExtractPreferences(ctx context.Context, userID, conversation string) error
- func (m *MemoryManager) GenerateUserContext(userID string) string
- func (m *MemoryManager) GetAllUserPreferences(userID string) map[string]string
- func (m *MemoryManager) GetCompressor() *ContextCompressor
- func (m *MemoryManager) GetUserPreference(userID, key string) (string, bool)
- func (m *MemoryManager) RecordUserPreference(userID, key, value string)
- type MetricsConfig
- type ModelConfig
- type ObservableBrain
- type PriorityBrain
- type PriorityConfig
- type RateLimitConfig
- type ResilientBrain
- type RetryConfig
- type RoutableBrain
- type RouterConfig
- type SafetyGuard
- func (g *SafetyGuard) AddBanPattern(pattern string) error
- func (g *SafetyGuard) AnalyzeDangerEvent(ctx context.Context, event map[string]interface{}) (string, error)
- func (g *SafetyGuard) CheckInput(ctx context.Context, input string) *GuardResult
- func (g *SafetyGuard) CheckInputWithUser(ctx context.Context, input string, userID string) *GuardResult
- func (g *SafetyGuard) CheckOutput(output string) *GuardResult
- func (g *SafetyGuard) DiagnoseError(ctx context.Context, err error, eventContext map[string]interface{}) (string, error)
- func (g *SafetyGuard) ExecuteConfigIntent(ctx context.Context, intent *ConfigIntent) (string, error)
- func (g *SafetyGuard) IsAdmin(userID, channelID string) bool
- func (g *SafetyGuard) ParseConfigIntent(ctx context.Context, msg string) (*ConfigIntent, error)
- func (g *SafetyGuard) ReloadPatterns() error
- func (g *SafetyGuard) SanitizeOutput(output string) string
- func (g *SafetyGuard) SetEnabled(enabled bool)
- func (g *SafetyGuard) Stats() map[string]interface{}
- func (g *SafetyGuard) UpdateBanPatterns(patterns []string) error
- type SafetyGuardFeatureConfig
- type SessionHistory
- type StreamingBrain
- type ThreatLevel
- type Turn
Constants ΒΆ
const ( // MinMessageLength is the minimum message length for fast-path detection. // Messages shorter than this are classified as chat. MinMessageLength = 3 // MaxQuickMessageLength is the maximum length for quick classification. // Messages longer than this require deeper analysis. MaxQuickMessageLength = 50 // MaxThankMessageLength is the max length for gratitude detection. MaxThankMessageLength = 30 // MaxContextHistory is the maximum number of history items to include. MaxContextHistory = 5 )
Intent detection thresholds and limits.
const ( // MaxTurnsForSummary is the maximum number of turns to include in summary generation. // Prevents excessive token usage when building summary prompts. MaxTurnsForSummary = 20 )
Context compression limits.
Variables ΒΆ
This section is empty.
Functions ΒΆ
func DefaultBanPatterns ΒΆ added in v0.22.0
func DefaultBanPatterns() []string
DefaultBanPatterns returns default patterns to block.
func GetRateLimiter ΒΆ added in v0.18.0
func GetRateLimiter() *llm.RateLimiter
GetRateLimiter returns the global rate limiter if available.
func GetRouter ΒΆ added in v0.18.0
GetRouter returns the global router if the brain supports routing.
func GetSessionSummary ΒΆ added in v0.22.0
GetSessionSummary is a convenience function to get session summary.
func Init ΒΆ
Init initializes the global Brain from environmental variables. It detects the provider and sets the Global Brain instance.
IMPORTANT: This function MUST be called before using any Brain-dependent features:
- GlobalIntentRouter() requires Global() to be non-nil
- GlobalCompressor() requires Global() to be non-nil
- GlobalGuard() requires Global() to be non-nil
If HOTPLEX_BRAIN_API_KEY is not set, Brain is disabled and features gracefully degrade.
func InitGuard ΒΆ added in v0.22.0
func InitGuard(config GuardConfig, logger *slog.Logger) error
InitGuard initializes the global SafetyGuard.
func InitIntentRouter ΒΆ added in v0.22.0
func InitIntentRouter(config IntentRouterConfig, logger *slog.Logger)
InitIntentRouter initializes the global intent router.
func InitMemory ΒΆ added in v0.22.0
func InitMemory(config CompressionConfig, logger *slog.Logger)
InitMemory initializes the global memory management system.
func IsRelevant ΒΆ added in v0.22.0
IsRelevant is a convenience function for group chat filtering.
func QuickResponse ΒΆ added in v0.22.0
QuickResponse generates a quick response for non-task intents.
func RecordTurn ΒΆ added in v0.22.0
RecordTurn is a convenience function to record a turn.
func SanitizeOutputString ΒΆ added in v0.22.0
SanitizeOutputString is a convenience function for output sanitization.
Types ΒΆ
type Brain ΒΆ
type Brain interface {
// Chat generates a plain text response for a given prompt.
// Best used for simple questions, greetings, or summarization.
Chat(ctx context.Context, prompt string) (string, error)
// Analyze performs structured analysis and returns the result in the target struct.
// The target must be a pointer to a struct that can be unmarshaled from JSON.
// Useful for intent routing, safety checks, and complex data extraction.
Analyze(ctx context.Context, prompt string, target any) error
}
Brain represents the core "System 1" intelligence for HotPlex. It provides fast, structured, and low-cost reasoning capabilities.
type BudgetConfig ΒΆ added in v0.18.0
type BudgetConfig struct {
Enabled bool // Enable budget tracking
Period string // Budget period: "daily", "weekly", "monthly"
Limit float64 // Budget limit in USD
EnableHardLimit bool // Block requests when budget exceeded
AlertThresholds []float64 // Alert at these percentages (e.g., [0.5, 0.8, 0.9])
}
BudgetConfig configures budget limits for LLM costs.
type BudgetControlledBrain ΒΆ added in v0.18.0
type BudgetControlledBrain interface {
Brain
// GetBudgetTracker returns the budget tracker for a session.
GetBudgetTracker(sessionID string) *llm.BudgetTracker
// GetBudgetManager returns the budget manager.
GetBudgetManager() *llm.BudgetManager
}
BudgetControlledBrain extends Brain with budget control capabilities.
type CacheConfig ΒΆ added in v0.18.0
type CacheConfig struct {
Enabled bool // Enable response caching
Size int // Maximum cache entries
}
CacheConfig configures response caching for repeated queries.
type CircuitBreakerConfig ΒΆ added in v0.18.0
type CircuitBreakerConfig struct {
Enabled bool // Enable circuit breaker
MaxFailures int // Failures before opening circuit
Timeout time.Duration // Time before attempting to close circuit
Interval time.Duration // Interval for resetting failure count
}
CircuitBreakerConfig configures circuit breaker for fault tolerance.
type CompressionConfig ΒΆ added in v0.22.0
type CompressionConfig struct {
Enabled bool `json:"enabled"` // Master switch for compression
TokenThreshold int `json:"token_threshold"` // Trigger compression at this token count (default: 8000)
TargetTokenCount int `json:"target_token_count"` // Target tokens after compression (default: 2000)
PreserveTurns int `json:"preserve_turns"` // Number of recent turns to keep intact (default: 5)
MaxSummaryTokens int `json:"max_summary_tokens"` // Max tokens for summary (default: 500)
CompressionRatio float64 `json:"compression_ratio"` // Target compression ratio 0.0-1.0 (default: 0.25)
SessionTTL time.Duration `json:"session_ttl"` // Session memory TTL (default: 24h)
CleanupInterval time.Duration `json:"cleanup_interval"` // Background cleanup interval (default: 1h)
}
CompressionConfig holds configuration for context compression.
func DefaultCompressionConfig ΒΆ added in v0.22.0
func DefaultCompressionConfig() CompressionConfig
DefaultCompressionConfig returns default compression configuration.
type Config ΒΆ
type Config struct {
// Enabled is automatically determined based on APIKey presence.
Enabled bool
// Model is the model configuration.
Model ModelConfig
// Cache is the cache configuration.
Cache CacheConfig
// Retry is the retry configuration.
Retry RetryConfig
// Metrics is the metrics configuration.
Metrics MetricsConfig
// Cost is the cost configuration.
Cost CostConfig
// RateLimit is the rate limit configuration.
RateLimit RateLimitConfig
// Router is the router configuration.
Router RouterConfig
// CircuitBreaker is the circuit breaker configuration.
CircuitBreaker CircuitBreakerConfig
// Failover is the failover configuration.
Failover FailoverConfig
// Budget is the budget configuration.
Budget BudgetConfig
// Priority is the priority configuration.
Priority PriorityConfig
// IntentRouter is the intent router feature configuration.
IntentRouter IntentRouterFeatureConfig
// Memory is the memory compression feature configuration.
Memory MemoryCompressionConfig
// Guard is the safety guard feature configuration.
Guard SafetyGuardFeatureConfig
}
Config holds the configuration for the Global Brain. It aggregates all sub-configurations for the Brain system.
Auto-Enable Logic ΒΆ
Config.Enabled is automatically set based on APIKey presence:
- HOTPLEX_BRAIN_API_KEY present β Enabled = true
- HOTPLEX_BRAIN_API_KEY absent β Enabled = false
This allows graceful degradation when Brain is not configured.
func LoadConfigFromEnv ΒΆ
func LoadConfigFromEnv() Config
LoadConfigFromEnv loads the brain configuration from environment variables.
type ConfigIntent ΒΆ added in v0.22.0
type ConfigIntent struct {
Action string `json:"action"` // "get", "set", "list"
Target string `json:"target"` // "model", "provider", "limit", etc.
Value string `json:"value"` // New value for "set" actions
Extra map[string]interface{} `json:"extra"` // Additional context
Confidence float64 `json:"confidence"`
}
ConfigIntent represents a configuration change intent.
type ContextCompressor ΒΆ added in v0.22.0
type ContextCompressor struct {
// contains filtered or unexported fields
}
ContextCompressor manages context compression for sessions. It tracks conversation turns per session and compresses when threshold is reached.
Thread Safety: All public methods are safe for concurrent use. The mu mutex protects sessions map and metrics.
Lifecycle:
- Create with NewContextCompressor()
- Record turns with RecordTurn()
- Check compression with CheckAndCompress()
- Stop background cleanup with Stop()
func GlobalCompressor ΒΆ added in v0.22.0
func GlobalCompressor() *ContextCompressor
GlobalCompressor returns the global ContextCompressor instance.
func NewContextCompressor ΒΆ added in v0.22.0
func NewContextCompressor(brain Brain, config CompressionConfig, logger *slog.Logger) *ContextCompressor
NewContextCompressor creates a new ContextCompressor.
func (*ContextCompressor) CheckAndCompress ΒΆ added in v0.22.0
func (c *ContextCompressor) CheckAndCompress(ctx context.Context, sessionID string) (*SessionHistory, error)
CheckAndCompress checks if compression is needed and performs it. Returns the compressed history or nil if no compression occurred.
Compression triggers when:
- Total tokens >= TokenThreshold
- Not already compressed recently (prevents repeated compression)
Returns:
- (*SessionHistory, nil): Compression performed successfully
- (nil, nil): No compression needed (below threshold)
- (nil, error): Compression failed
func (*ContextCompressor) ClearExpired ΒΆ added in v0.22.0
func (c *ContextCompressor) ClearExpired() int
ClearExpired clears expired sessions based on TTL.
func (*ContextCompressor) ClearSession ΒΆ added in v0.22.0
func (c *ContextCompressor) ClearSession(sessionID string)
ClearSession clears history for a specific session.
func (*ContextCompressor) ForceCompress ΒΆ added in v0.22.0
func (c *ContextCompressor) ForceCompress(ctx context.Context, sessionID string) (*SessionHistory, error)
ForceCompress forces compression of a session regardless of threshold.
func (*ContextCompressor) GetContextForEngine ΒΆ added in v0.22.0
func (c *ContextCompressor) GetContextForEngine(sessionID string) string
GetContextForEngine returns the context to inject into Engine prompts. This includes any compressed summary and recent turns.
func (*ContextCompressor) GetHistory ΒΆ added in v0.22.0
func (c *ContextCompressor) GetHistory(sessionID string) *SessionHistory
GetHistory returns the session history for a given session.
func (*ContextCompressor) GetSessionSummary ΒΆ added in v0.22.0
func (c *ContextCompressor) GetSessionSummary(sessionID string) string
GetSessionSummary returns the summary for a session, if compressed.
func (*ContextCompressor) RecordTurn ΒΆ added in v0.22.0
func (c *ContextCompressor) RecordTurn(sessionID, role, content string, tokenCount int)
RecordTurn records a conversation turn in session history.
func (*ContextCompressor) SetEnabled ΒΆ added in v0.22.0
func (c *ContextCompressor) SetEnabled(enabled bool)
SetEnabled enables or disables compression at runtime.
func (*ContextCompressor) Stats ΒΆ added in v0.22.0
func (c *ContextCompressor) Stats() map[string]interface{}
Stats returns compressor statistics.
func (*ContextCompressor) Stop ΒΆ added in v0.22.0
func (c *ContextCompressor) Stop()
Stop stops the background cleanup daemon.
func (*ContextCompressor) UpdateConfig ΒΆ added in v0.22.0
func (c *ContextCompressor) UpdateConfig(config CompressionConfig)
UpdateConfig updates the compression configuration.
type CostConfig ΒΆ added in v0.18.0
type CostConfig struct {
Enabled bool // Enable cost tracking
EnableBudget bool // Enable budget enforcement
}
CostConfig configures cost tracking for LLM API calls.
type FailoverConfig ΒΆ added in v0.18.0
type FailoverConfig struct {
Enabled bool // Enable failover
Providers []llm.ProviderConfig // Backup providers
EnableAuto bool // Enable automatic failover
EnableFailback bool // Enable automatic failback when primary recovers
Cooldown time.Duration // Cooldown period before failback
}
FailoverConfig configures provider failover behavior.
type GuardConfig ΒΆ added in v0.22.0
type GuardConfig struct {
Enabled bool `json:"enabled"` // Master switch for all guard features
InputGuardEnabled bool `json:"input_guard_enabled"` // Enable input validation (pattern + AI)
OutputGuardEnabled bool `json:"output_guard_enabled"` // Enable output sanitization (redact secrets)
Chat2ConfigEnabled bool `json:"chat2config_enabled"` // Allow config changes via natural language (security risk)
MaxInputLength int `json:"max_input_length"` // Reject inputs exceeding this length (DoS protection)
ScanDepth int `json:"scan_depth"` // Reserved for nested context scanning
Sensitivity string `json:"sensitivity"` // Detection sensitivity: "low" (pattern-only), "medium", "high" (aggressive AI)
BanPatterns []string `json:"ban_patterns"` // Regex patterns for prompt injection, jailbreak, etc.
AdminUsers []string `json:"admin_users"` // User IDs with elevated privileges
AdminChannels []string `json:"admin_channels"` // Channel IDs with elevated privileges
ResponseTimeout time.Duration `json:"response_timeout"` // Timeout for Brain API calls during analysis
// Rate limiting for CheckInput calls (per-user)
RateLimitRPS float64 `json:"rate_limit_rps"` // Requests per second per user (0 = disabled)
RateLimitBurst int `json:"rate_limit_burst"` // Burst capacity per user
}
GuardConfig holds configuration for SafetyGuard.
func DefaultGuardConfig ΒΆ added in v0.22.0
func DefaultGuardConfig() GuardConfig
DefaultGuardConfig returns default guard configuration.
type GuardResult ΒΆ added in v0.22.0
type GuardResult struct {
Safe bool `json:"safe"` // true if no threat detected or successfully sanitized
ThreatLevel ThreatLevel `json:"threat_level"` // Severity classification
ThreatType string `json:"threat_type,omitempty"` // e.g., "prompt_injection", "sensitive_data_detected"
Reason string `json:"reason,omitempty"` // Human-readable explanation
MatchedPattern string `json:"matched_pattern,omitempty"` // The regex that matched (for debugging)
Action string `json:"action,omitempty"` // "allow", "block", or "sanitize"
SanitizedInput string `json:"sanitized_input,omitempty"` // Redacted version when Action == "sanitize"
}
GuardResult represents the result of a guard check. Action determines the next step:
- "allow": Pass through unchanged
- "block": Reject the input/output entirely
- "sanitize": Pass through with sensitive data redacted (see SanitizedInput)
func CheckInputSafe ΒΆ added in v0.22.0
func CheckInputSafe(ctx context.Context, input string) *GuardResult
CheckInputSafe is a convenience function for input checking.
func CheckOutputSafe ΒΆ added in v0.22.0
func CheckOutputSafe(output string) *GuardResult
CheckOutputSafe is a convenience function for output checking.
type HealthStatus ΒΆ added in v0.18.0
type HealthStatus = llm.HealthStatus
HealthStatus represents the health status of the Brain service. Re-exported from llm package for convenience.
type IntentResult ΒΆ added in v0.22.0
type IntentResult struct {
Type IntentType `json:"type"`
Confidence float64 `json:"confidence"`
Response string `json:"response,omitempty"`
Reason string `json:"reason,omitempty"`
}
IntentResult represents the result of intent detection.
type IntentRouter ΒΆ added in v0.22.0
type IntentRouter struct {
// contains filtered or unexported fields
}
IntentRouter performs intent detection for incoming messages. It determines whether a message should be handled by the Brain (for chat/commands) or forwarded to the Engine Provider (for tasks).
Thread Safety: All public methods are safe for concurrent use. The cache is protected by a dedicated mutex.
func GlobalIntentRouter ΒΆ added in v0.22.0
func GlobalIntentRouter() *IntentRouter
GlobalIntentRouter returns the global IntentRouter instance. It uses the global Brain if available.
func NewIntentRouter ΒΆ added in v0.22.0
func NewIntentRouter(brain Brain, config IntentRouterConfig, logger *slog.Logger) *IntentRouter
NewIntentRouter creates a new IntentRouter instance.
func (*IntentRouter) ClearCache ΒΆ added in v0.22.0
func (r *IntentRouter) ClearCache()
ClearCache clears the intent cache.
func (*IntentRouter) GenerateResponse ΒΆ added in v0.22.0
func (r *IntentRouter) GenerateResponse(ctx context.Context, msg string, intent *IntentResult) (string, error)
GenerateResponse generates a quick response for chat/command intents.
func (*IntentRouter) GetEnabled ΒΆ added in v0.22.0
func (r *IntentRouter) GetEnabled() bool
GetEnabled returns whether the router is enabled.
func (*IntentRouter) IsRelevant ΒΆ added in v0.22.0
IsRelevant checks if a message in a group chat is relevant to the bot. This is used for noise filtering in group channels.
func (*IntentRouter) Route ΒΆ added in v0.22.0
func (r *IntentRouter) Route(ctx context.Context, msg string) *IntentResult
Route determines the intent of a message and returns the routing decision. It performs a two-stage classification:
Fast path: Rule-based detection for obvious cases (greetings, status commands) β Returns immediately without Brain API call
Brain analysis: Sends prompt to Brain for intelligent classification β Returns intent type with confidence score
If the Brain is disabled or unavailable, returns IntentTypeTask to ensure the message is processed by the Engine (safe default).
func (*IntentRouter) RouteWithHistory ΒΆ added in v0.22.0
func (r *IntentRouter) RouteWithHistory(ctx context.Context, msg string, history []string) *IntentResult
RouteWithHistory determines intent considering conversation history.
func (*IntentRouter) SetEnabled ΒΆ added in v0.22.0
func (r *IntentRouter) SetEnabled(enabled bool)
SetEnabled enables or disables the router at runtime.
func (*IntentRouter) ShouldUseEngine ΒΆ added in v0.22.0
func (r *IntentRouter) ShouldUseEngine(result *IntentResult) bool
ShouldUseEngine returns true if the message should be forwarded to the Engine.
func (*IntentRouter) Stats ΒΆ added in v0.22.0
func (r *IntentRouter) Stats() map[string]interface{}
Stats returns router statistics.
type IntentRouterConfig ΒΆ added in v0.22.0
IntentRouterConfig holds configuration for IntentRouter.
func DefaultIntentRouterConfig ΒΆ added in v0.22.0
func DefaultIntentRouterConfig() IntentRouterConfig
DefaultIntentRouterConfig returns default configuration.
type IntentRouterFeatureConfig ΒΆ added in v0.22.0
type IntentRouterFeatureConfig struct {
Enabled bool `json:"enabled"` // Enable intent routing
ConfidenceThreshold float64 `json:"confidence_threshold"` // Minimum confidence for classification
CacheSize int `json:"cache_size"` // Cache size for classification results
}
IntentRouterFeatureConfig configures intent routing features.
type IntentType ΒΆ added in v0.22.0
type IntentType string
IntentType represents the classification of user intent.
const ( // IntentTypeChat indicates casual conversation that can be handled by Brain. IntentTypeChat IntentType = "chat" // IntentTypeCommand indicates a configuration or status command. IntentTypeCommand IntentType = "command" // IntentTypeTask indicates a complex task requiring Engine Provider. IntentTypeTask IntentType = "task" // IntentTypeUnknown indicates unclear intent, defaults to Engine. IntentTypeUnknown IntentType = "unknown" )
type MemoryCompressionConfig ΒΆ added in v0.22.0
type MemoryCompressionConfig struct {
Enabled bool // Enable context compression
TokenThreshold int // Trigger compression at this token count
TargetTokenCount int // Target tokens after compression
PreserveTurns int // Recent turns to preserve during compression
MaxSummaryTokens int // Maximum tokens for summary
CompressionRatio float64 // Target compression ratio (0.0-1.0)
SessionTTL string // Session time-to-live (e.g., "24h")
}
MemoryCompressionConfig configures context compression.
type MemoryManager ΒΆ added in v0.22.0
type MemoryManager struct {
// contains filtered or unexported fields
}
MemoryManager provides high-level memory management capabilities. It combines context compression with persistent memory storage.
func GlobalMemoryManager ΒΆ added in v0.22.0
func GlobalMemoryManager() *MemoryManager
GlobalMemoryManager returns the global MemoryManager instance.
func NewMemoryManager ΒΆ added in v0.22.0
func NewMemoryManager(compressor *ContextCompressor, brain Brain, logger *slog.Logger) *MemoryManager
NewMemoryManager creates a new MemoryManager.
func (*MemoryManager) ExtractPreferences ΒΆ added in v0.22.0
func (m *MemoryManager) ExtractPreferences(ctx context.Context, userID, conversation string) error
ExtractPreferences uses Brain to extract preferences from conversation.
func (*MemoryManager) GenerateUserContext ΒΆ added in v0.22.0
func (m *MemoryManager) GenerateUserContext(userID string) string
GenerateUserContext generates context string with user preferences.
func (*MemoryManager) GetAllUserPreferences ΒΆ added in v0.22.0
func (m *MemoryManager) GetAllUserPreferences(userID string) map[string]string
GetAllUserPreferences returns all preferences for a user.
func (*MemoryManager) GetCompressor ΒΆ added in v0.22.0
func (m *MemoryManager) GetCompressor() *ContextCompressor
GetCompressor returns the underlying compressor.
func (*MemoryManager) GetUserPreference ΒΆ added in v0.22.0
func (m *MemoryManager) GetUserPreference(userID, key string) (string, bool)
GetUserPreference retrieves a user preference.
func (*MemoryManager) RecordUserPreference ΒΆ added in v0.22.0
func (m *MemoryManager) RecordUserPreference(userID, key, value string)
RecordUserPreference stores a user preference.
type MetricsConfig ΒΆ added in v0.18.0
type MetricsConfig struct {
Enabled bool // Enable metrics collection
ServiceName string // Service name for metrics identification
Endpoint string // Metrics export endpoint (e.g., OTLP collector)
ExportInterval time.Duration // Interval for periodic metric export
}
MetricsConfig configures observability and metrics export.
type ModelConfig ΒΆ added in v0.18.0
type ModelConfig struct {
Provider string // LLM provider: "openai", "anthropic", "google"
Model string // Model name: "gpt-4o-mini", "claude-3-haiku", etc.
Endpoint string // Custom API endpoint (optional, for self-hosted)
TimeoutS int // Request timeout in seconds
}
ModelConfig configures the LLM backend for Brain operations.
type ObservableBrain ΒΆ added in v0.18.0
type ObservableBrain interface {
Brain
// GetMetrics returns current metrics statistics.
GetMetrics() llm.MetricsStats
// GetCostCalculator returns the cost calculator.
GetCostCalculator() *llm.CostCalculator
}
ObservableBrain provides observability and metrics access.
type PriorityBrain ΒΆ added in v0.18.0
type PriorityBrain interface {
Brain
// GetPriorityScheduler returns the priority scheduler.
GetPriorityScheduler() *llm.PriorityScheduler
// SubmitWithPriority submits a request with specified priority.
SubmitWithPriority(ctx context.Context, prompt string, priority llm.Priority) (string, error)
}
PriorityBrain extends Brain with priority queue capabilities.
type PriorityConfig ΒΆ added in v0.18.0
type PriorityConfig struct {
Enabled bool // Enable priority queuing
MaxQueueSize int // Maximum queued requests
EnableLowPriorityDrop bool // Drop low-priority requests when queue full
HighPriorityReserve int // Reserved slots for high-priority requests
}
PriorityConfig configures request prioritization.
type RateLimitConfig ΒΆ added in v0.18.0
type RateLimitConfig struct {
Enabled bool // Enable rate limiting
RPS float64 // Requests per second limit
Burst int // Burst capacity (token bucket)
QueueSize int // Queue size for waiting requests
QueueTimeout time.Duration // Max wait time in queue
PerModel bool // Apply limit per-model instead of global
}
RateLimitConfig configures request rate limiting.
type ResilientBrain ΒΆ added in v0.18.0
type ResilientBrain interface {
Brain
// GetCircuitBreaker returns the circuit breaker.
GetCircuitBreaker() *llm.CircuitBreaker
// GetFailoverManager returns the failover manager.
GetFailoverManager() *llm.FailoverManager
// ResetCircuitBreaker manually resets the circuit breaker.
ResetCircuitBreaker()
// ManualFailover manually switches to a specific provider.
ManualFailover(providerName string) error
}
ResilientBrain extends Brain with circuit breaker and failover capabilities.
type RetryConfig ΒΆ added in v0.18.0
type RetryConfig struct {
Enabled bool // Enable retry mechanism
MaxAttempts int // Maximum retry attempts
MinWaitMs int // Minimum wait between retries (milliseconds)
MaxWaitMs int // Maximum wait between retries (milliseconds)
}
RetryConfig configures retry behavior for transient failures.
type RoutableBrain ΒΆ added in v0.18.0
type RoutableBrain interface {
Brain
// ChatWithModel generates a response using a specific model.
ChatWithModel(ctx context.Context, model string, prompt string) (string, error)
// AnalyzeWithModel performs analysis using a specific model.
AnalyzeWithModel(ctx context.Context, model string, prompt string, target any) error
}
RoutableBrain extends Brain with model routing capability.
type RouterConfig ΒΆ added in v0.18.0
type RouterConfig struct {
Enabled bool // Enable model routing
DefaultStage string // Default routing strategy: "cost_priority", "latency_priority"
Models []llm.ModelConfig // Available models with cost/latency info
}
RouterConfig configures intelligent model routing.
type SafetyGuard ΒΆ added in v0.22.0
type SafetyGuard struct {
// contains filtered or unexported fields
}
SafetyGuard provides security guardrails for Brain operations. It acts as a middleware between user input and Brain/Engine processing.
Thread Safety: All public methods are safe for concurrent use. The mu mutex protects metrics counters and pattern updates.
func GlobalGuard ΒΆ added in v0.22.0
func GlobalGuard() *SafetyGuard
GlobalGuard returns the global SafetyGuard instance.
func NewSafetyGuard ΒΆ added in v0.22.0
func NewSafetyGuard(brain Brain, config GuardConfig, logger *slog.Logger) (*SafetyGuard, error)
NewSafetyGuard creates a new SafetyGuard instance.
func (*SafetyGuard) AddBanPattern ΒΆ added in v0.22.0
func (g *SafetyGuard) AddBanPattern(pattern string) error
AddBanPattern adds a new ban pattern.
func (*SafetyGuard) AnalyzeDangerEvent ΒΆ added in v0.22.0
func (g *SafetyGuard) AnalyzeDangerEvent(ctx context.Context, event map[string]interface{}) (string, error)
AnalyzeDangerEvent analyzes a danger.blocked event for context.
func (*SafetyGuard) CheckInput ΒΆ added in v0.22.0
func (g *SafetyGuard) CheckInput(ctx context.Context, input string) *GuardResult
CheckInput validates input for safety concerns. It performs a multi-stage check:
- If guard is disabled, returns safe immediately (fast path)
- Rate limit check: enforces per-user rate limiting
- Length check: blocks inputs exceeding MaxInputLength
- Pattern scan: matches against banPatterns (prompt injection, jailbreak)
- Deep analysis: if sensitivity > "low", uses Brain AI for subtle threat detection
Returns GuardResult with Action indicating the recommended handling. userID is optional; if empty, rate limiting is applied globally.
func (*SafetyGuard) CheckInputWithUser ΒΆ added in v0.22.0
func (g *SafetyGuard) CheckInputWithUser(ctx context.Context, input string, userID string) *GuardResult
CheckInputWithUser validates input for safety concerns with explicit user context. The userID parameter enables per-user rate limiting.
func (*SafetyGuard) CheckOutput ΒΆ added in v0.22.0
func (g *SafetyGuard) CheckOutput(output string) *GuardResult
CheckOutput validates and sanitizes output for sensitive data. Unlike CheckInput, this never blocks - it only redacts sensitive information.
Patterns detected and redacted:
- API keys (api_key=xxx, secret_key=xxx)
- AWS access keys (AKIA...)
- Private keys (-----BEGIN RSA PRIVATE KEY-----)
- JWT tokens (eyJ...)
- Internal IP addresses (10.x, 172.16-31.x, 192.168.x)
- Database connection strings with credentials
- Passwords in config format
Returns GuardResult with SanitizedInput containing the redacted version.
func (*SafetyGuard) DiagnoseError ΒΆ added in v0.22.0
func (g *SafetyGuard) DiagnoseError(ctx context.Context, err error, eventContext map[string]interface{}) (string, error)
DiagnoseError analyzes an error and provides diagnostic suggestions.
func (*SafetyGuard) ExecuteConfigIntent ΒΆ added in v0.22.0
func (g *SafetyGuard) ExecuteConfigIntent(ctx context.Context, intent *ConfigIntent) (string, error)
ExecuteConfigIntent executes a configuration intent. This returns a human-readable response.
func (*SafetyGuard) IsAdmin ΒΆ added in v0.22.0
func (g *SafetyGuard) IsAdmin(userID, channelID string) bool
IsAdmin checks if a user or channel has admin privileges.
func (*SafetyGuard) ParseConfigIntent ΒΆ added in v0.22.0
func (g *SafetyGuard) ParseConfigIntent(ctx context.Context, msg string) (*ConfigIntent, error)
ParseConfigIntent parses a natural language config command.
func (*SafetyGuard) ReloadPatterns ΒΆ added in v0.22.0
func (g *SafetyGuard) ReloadPatterns() error
ReloadPatterns reloads ban patterns from config.
func (*SafetyGuard) SanitizeOutput ΒΆ added in v0.22.0
func (g *SafetyGuard) SanitizeOutput(output string) string
SanitizeOutput applies sanitization to output string.
func (*SafetyGuard) SetEnabled ΒΆ added in v0.22.0
func (g *SafetyGuard) SetEnabled(enabled bool)
SetEnabled enables or disables the guard at runtime.
func (*SafetyGuard) Stats ΒΆ added in v0.22.0
func (g *SafetyGuard) Stats() map[string]interface{}
Stats returns guard statistics.
func (*SafetyGuard) UpdateBanPatterns ΒΆ added in v0.22.0
func (g *SafetyGuard) UpdateBanPatterns(patterns []string) error
UpdateBanPatterns updates the ban patterns.
type SafetyGuardFeatureConfig ΒΆ added in v0.22.0
type SafetyGuardFeatureConfig struct {
Enabled bool // Enable safety guard
InputGuardEnabled bool // Enable input validation
OutputGuardEnabled bool // Enable output sanitization
Chat2ConfigEnabled bool // Enable natural language config changes (security risk)
MaxInputLength int // Maximum input length
ScanDepth int // Depth for nested context scanning
Sensitivity string // Detection sensitivity: "low", "medium", "high"
AdminUsers []string // User IDs with admin privileges
AdminChannels []string // Channel IDs with admin privileges
ResponseTimeout time.Duration // Timeout for Brain API calls
RateLimitRPS float64 // Requests per second per user (0 = disabled)
RateLimitBurst int // Burst capacity per user
}
SafetyGuardFeatureConfig configures safety guardrails.
type SessionHistory ΒΆ added in v0.22.0
type SessionHistory struct {
SessionID string `json:"session_id"`
Turns []Turn `json:"turns"`
TotalTokens int `json:"total_tokens"`
Compressed bool `json:"compressed"`
Summary string `json:"summary,omitempty"`
}
SessionHistory represents the conversation history of a session.
func CheckCompress ΒΆ added in v0.22.0
func CheckCompress(ctx context.Context, sessionID string) (*SessionHistory, error)
CheckCompress is a convenience function to check and compress.
type StreamingBrain ΒΆ added in v0.18.0
type StreamingBrain interface {
Brain
// ChatStream returns a channel that streams tokens as they are generated.
// The channel is closed when the stream completes or an error occurs.
// Best used for long responses, real-time UI updates, or progressive rendering.
ChatStream(ctx context.Context, prompt string) (<-chan string, error)
}
StreamingBrain extends Brain with streaming capabilities. It provides token-by-token streaming for real-time responses.
type ThreatLevel ΒΆ added in v0.22.0
type ThreatLevel string
ThreatLevel represents the severity of a detected threat.
const ( ThreatLevelNone ThreatLevel = "none" ThreatLevelLow ThreatLevel = "low" ThreatLevelMedium ThreatLevel = "medium" ThreatLevelHigh ThreatLevel = "high" ThreatLevelCritical ThreatLevel = "critical" )