brain

package
v0.22.3 Latest Latest
Warning

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

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 16 Imported by: 0

README ΒΆ

Native Brain: Production-Grade LLM Intelligence

The brain package is the core intelligence engine of HotPlex. It provides a highly reliable, observable, and cost-effective interface for Large Language Model (LLM) reasoning.

πŸ› Architecture Overview

The system is designed with a layered "Enhanced Brain" architecture. It decorates a base LLM client with multiple production-readiness middleware layers.

graph TD
    User([Application]) --> Inter[Brain Interface]

    subgraph Layers [Production Layers]
        Inter --> Priority[Priority Scheduler]
        Priority --> Router[Model Router]
        Router --> Balancer[Load Balancer / Failover]
        Balancer --> Resil[Resiliency: Circuit Breaker / Retry]
        Resil --> Cache[LRU Cache]
        Cache --> RL[Rate Limiter]
        RL --> Obs[Observability: Metrics / Cost]
    end

    subgraph Providers [LLM Providers]
        Obs --> OpenAI[OpenAI SDK]
        Obs --> Custom[Custom Providers]
    end

🧠 Native Brain Core Components (v0.22.0)

1. IntentRouter

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:

Intent Description Handler
chat Casual conversation, greetings Brain generates response
command Status queries, config commands Brain generates response
task Code operations, debugging Forward to Engine Provider
unknown Ambiguous intent Default to Engine for safety

Fast-Path Optimization: Obvious cases are handled without Brain API calls:

  • Greetings ("hi", "hello") β†’ chat
  • Status commands ("ping", "status") β†’ command
  • Code keywords ("function", "debug") β†’ task
router := brain.NewIntentRouter(brainClient)
result, err := router.Classify(ctx, "Write a Python function")
// result.Type == IntentTypeTask
2. ContextCompressor

Manages conversation context to prevent context window overflow while preserving important 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:

  1. Wait until token count exceeds TokenThreshold (default: 8000)
  2. Keep last PreserveTurns (default: 5) turns intact
  3. Summarize older turns using Brain AI
  4. Replace old turns with summary, update total token count
compressor := brain.NewContextCompressor(brainClient, brain.CompressionConfig{
    Enabled:        true,
    TokenThreshold: 8000,
    PreserveTurns:  5,
    MaxSummaryTokens: 500,
})
compressed := compressor.Compress(ctx, sessionHistory)
3. SafetyGuard

Multi-layer security for input validation and output sanitization.

SafetyGuard
β”œβ”€β”€ CheckInput()     β†’ Pattern scan β†’ Brain analysis β†’ allow/block
β”œβ”€β”€ CheckOutput()    β†’ Pattern match β†’ sanitize β†’ allow
└── ParseConfigIntent() β†’ Brain NLU β†’ ExecuteConfigIntent()

Threat Detection Flow:

  1. Fast path: Regex patterns catch obvious attacks (prompt injection, jailbreak)
  2. Deep analysis: Brain AI classifies subtle threats with confidence scores
  3. Action: allow (safe), block (threat), or sanitize (redact sensitive data)

Default Blocked Patterns:

  • Prompt injection (ignore previous instructions)
  • Jailbreak attempts (DAN mode, developer mode)
  • System override attempts (you are now admin)
guard := brain.NewSafetyGuard(brainClient, brain.DefaultGuardConfig())

// Input validation
result := guard.CheckInput(ctx, userInput)
if result.Action == brain.GuardActionBlock {
    return errors.New("blocked: " + result.Reason)
}

// Output sanitization
sanitized := guard.CheckOutput(ctx, llmResponse)

Core Components (Continued)
  • Brain Interface: Unified API for high-level reasoning (Chat, Analyze) and streaming (ChatStream).
  • Resiliency Engine: Implements exponential backoff retries and the Circuit Breaker pattern to handle transient failures and provider outages.
  • Dynamic Router: Automatically selects the optimal model based on the scenario (e.g., Code vs. Chat) and configured strategies (Cost vs. Latency vs. Quality).
  • High Availability: Multi-provider failover with automatic recovery (failback).
  • Resource Control: Distributed rate limiting and token bucket management per model.
  • Budget Guardrails: Multi-level token budget tracking (Daily/Weekly/Session) with hard and soft limits.

πŸ›  Developer Guide

Interface Definitions

The package exports several interfaces to allow for granular usage of brain capabilities:

// Base reasoning
type Brain interface {
    Chat(ctx context.Context, prompt string) (string, error)
    Analyze(ctx context.Context, prompt string, target any) error
}

// Specialized capabilities
type StreamingBrain interface { ChatStream(...) }
type RoutableBrain interface { ChatWithModel(...) }
type ObservableBrain interface { GetMetrics(...) }
type ResilientBrain interface { GetCircuitBreaker(...); GetFailoverManager(...) }
Advanced Usage Scenarios & Patterns
1. 🎬 Real-time Streaming (Live UI)

Best for large language outputs where low perceived latency is critical.

func StreamAnswer(ctx context.Context, question string) {
    if sb, ok := brain.Global().(brain.StreamingBrain); ok {
        stream, err := sb.ChatStream(ctx, question)
        if err != nil {
            log.Fatal(err)
        }
        
        for token := range stream {
            fmt.Print(token) // Progressive rendering
        }
    }
}
2. 🚦 Explicit Multi-Model Selection

Forces a specific model for specialized tasks (e.g., GPT-4o for complex reasoning).

func SpecializedTask(ctx context.Context) {
    if rb, ok := brain.Global().(brain.RoutableBrain); ok {
        // High quality model override
        ans, _ := rb.ChatWithModel(ctx, "gpt-4o", "Deep technical analysis...")
        fmt.Println(ans)
    }
}
3. 🎯 Intelligent Scenario Routing

Automatically detects task type (Code, Chat, or Analysis) and routes to the most cost-effective model configured in your strategies.

func AutoRouteAction(ctx context.Context, userPrompt string) {
    // router selected based on StrategyBalanced/StrategyCostPriority
    // System automatically detects 'Write a Python script' as ScenarioCode
    resp, err := brain.Global().Chat(ctx, userPrompt)
    if err != nil {
        log.Printf("Routing error: %v", err)
    }
}
4. πŸ₯ Resilience Management

Observing provider health and manually controlling the circuit state.

func MonitorResilience() {
    if rb, ok := brain.Global().(brain.ResilientBrain); ok {
        cb := rb.GetCircuitBreaker()
        fm := rb.GetFailoverManager()
        
        fmt.Printf("Circuit State: %s | Failure Count: %d\n", 
            cb.GetState(), cb.GetStats().FailRequests)
            
        // Manual failover if we detect high latency on primary
        if fm.GetCurrentProvider().Name == "openai" {
            _ = fm.ManualFailover("dashscope") 
        }
    }
}
5. πŸ’° Session Budget Guardrails

Tracking and enforcing financial limits for specific chat sessions.

func ControlledSession(sessionID string) {
    if bb, ok := brain.Global().(brain.BudgetControlledBrain); ok {
        tracker := bb.GetBudgetTracker(sessionID)
        
        // Estimated cost check before heavy processing
        if allowed, _, _ := tracker.CheckBudget(0.05); !allowed {
            fmt.Println("Session budget exceeded. Stopping.")
            return
        }
    }
}

πŸ”§ Client Builder Pattern (Issue #217)

The llm subpackage provides a fluent Builder API for composing middleware layers.

Wrapping Order (Innermost to Outermost)
Metrics β†’ Circuit β†’ RateLimit β†’ Retry β†’ Cache β†’ OpenAI
Layer Purpose
Cache LRU response caching
Retry Exponential backoff retries
Rate Limit Token bucket rate limiting
Circuit Breaker Fail-fast on repeated errors
Metrics Observability and cost tracking
Preset Configurations
// Production: all capabilities
client, _ := llm.ProductionClient(apiKey, "gpt-4")

// Development: minimal overhead
client, _ := llm.DevelopmentClient(apiKey, "gpt-4")

// High throughput: aggressive caching
client, _ := llm.HighThroughputClient(apiKey, "gpt-4")

// Maximum reliability: aggressive retries
client, _ := llm.ReliableClient(apiKey, "gpt-4")
Custom Configuration
client, _ := llm.NewClientBuilder().
    WithAPIKey(apiKey).
    WithEndpoint("https://api.deepseek.com/v1").
    WithModel("deepseek-chat").
    WithCache(5000).
    WithRetry(5).
    WithCircuitBreaker(llm.CircuitBreakerConfig{...}).
    WithRateLimit(100).
    WithMetrics().
    Build()
Independent Features (Non-Builder)

Budget tracking and priority scheduling are standalone:

// Budget Control
client, _ := llm.NewBudgetManagedClient(apiKey, "gpt-4", 10.0) // $10/day

// Priority Scheduling
scheduler, client := llm.PrioritySchedulerWithClient(5*time.Minute, nil)
client.Submit(ctx, "req-1", llm.PriorityHigh, func() error { ... })
ObservableClient

Extract runtime statistics:

obs := llm.AsObservable(client)
health := obs.GetClientHealth(ctx)
// health.CircuitState, health.CacheHitRate, health.TotalRequests

πŸ“Š Observability & Metrics

The system tracks enterprise-level metrics using OpenTelemetry:

  • Latency: Detailed histogram of request durations.
  • Token Usage: Granular tracking of input/output tokens.
  • Financials: Real-time USD cost calculation based on model-specific pricing.
  • Reliability: Error rates and circuit breaker state transitions.
Metric Type Description
llm_request_duration Histogram Latency per model/operation
llm_tokens_total Counter Total tokens consumed
llm_cost_usd Gauge Cumulative cost in USD
llm_error_rate Gauge Failure percentage

βš™οΈ Configuration Reference

Variable Description Default
HOTPLEX_BRAIN_PROVIDER Primary provider (openai/dashscope/etc) openai
HOTPLEX_BRAIN_CIRCUIT_BREAKER_ENABLED Enable circuit breaker protection false
HOTPLEX_BRAIN_ROUTER_ENABLED Enable scenario-based model routing false
HOTPLEX_BRAIN_FAILOVER_ENABLED Enable automatic multi-provider switching false
HOTPLEX_BRAIN_BUDGET_LIMIT Hard USD limit for the period 10.0

πŸ§ͺ Testing

The package includes high-coverage unit tests and integration tests for providers.

go test -v ./brain/...
  • Unit tests: Fast, mocks provider calls.
  • Integration tests: Requires API keys, tests real connectivity.
  • Scenario tests: Validates routing and budget logic under load.

Package Status: Production Ready (Phase 3)
Maintainer: HotPlex Core Team

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 ΒΆ

  1. Fast path: Regex patterns catch obvious attacks (prompt injection, jailbreak)
  2. Deep analysis: Brain AI classifies subtle threats with confidence scores
  3. 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 ΒΆ

  1. Wait until token count exceeds TokenThreshold (default: 8000)
  2. Keep last PreserveTurns (default: 5) turns intact
  3. Summarize older turns using Brain AI (max MaxSummaryTokens)
  4. 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 ΒΆ

View Source
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.

View Source
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

func GetRouter() *llm.Router

GetRouter returns the global router if the brain supports routing.

func GetSessionSummary ΒΆ added in v0.22.0

func GetSessionSummary(sessionID string) string

GetSessionSummary is a convenience function to get session summary.

func Init ΒΆ

func Init(logger *slog.Logger) error

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

func IsRelevant(ctx context.Context, msg string, botMentioned bool) bool

IsRelevant is a convenience function for group chat filtering.

func QuickResponse ΒΆ added in v0.22.0

func QuickResponse(ctx context.Context, msg string, result *IntentResult) (string, error)

QuickResponse generates a quick response for non-task intents.

func RecordTurn ΒΆ added in v0.22.0

func RecordTurn(sessionID, role, content string, tokenCount int)

RecordTurn is a convenience function to record a turn.

func SanitizeOutputString ΒΆ added in v0.22.0

func SanitizeOutputString(output string) string

SanitizeOutputString is a convenience function for output sanitization.

func SetGlobal ΒΆ

func SetGlobal(b Brain)

SetGlobal sets the global Brain instance.

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.

func Global ΒΆ

func Global() Brain

Global returns the globally configured Brain instance. If no brain is configured, it returns nil.

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:

  1. Create with NewContextCompressor()
  2. Record turns with RecordTurn()
  3. Check compression with CheckAndCompress()
  4. 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.

func Route ΒΆ added in v0.22.0

func Route(ctx context.Context, msg string) *IntentResult

Route is a convenience function that uses the global IntentRouter.

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

func (r *IntentRouter) IsRelevant(ctx context.Context, msg string, botMentioned bool) bool

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:

  1. Fast path: Rule-based detection for obvious cases (greetings, status commands) β†’ Returns immediately without Brain API call

  2. 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

type IntentRouterConfig struct {
	Enabled             bool
	ConfidenceThreshold float64
	CacheSize           int
}

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:

  1. If guard is disabled, returns safe immediately (fast path)
  2. Rate limit check: enforces per-user rate limiting
  3. Length check: blocks inputs exceeding MaxInputLength
  4. Pattern scan: matches against banPatterns (prompt injection, jailbreak)
  5. 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"
)

type Turn ΒΆ added in v0.22.0

type Turn struct {
	Role       string    `json:"role"` // "user" or "assistant"
	Content    string    `json:"content"`
	Timestamp  time.Time `json:"timestamp"`
	TokenCount int       `json:"token_count"`
}

Turn represents a single conversation turn (user + assistant exchange).

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

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