intelligence

package
v0.5.23 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2026 License: Apache-2.0 Imports: 26 Imported by: 0

Documentation

Overview

Package intelligence provides the Cortex Router for intelligent request routing. The Cortex Router implements multi-tier classification (Reflex → Semantic → Cognitive) with memory integration for learning and preference-based routing.

Package intelligence provides the main orchestrator for Phase 2 intelligent routing features. It manages the lifecycle of all intelligence services including discovery, capability analysis, dynamic matrix building, skill registry, embedding engine, semantic tier, and more.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheEntry

type CacheEntry struct {
	Query     string
	Decision  string
	Metadata  map[string]interface{}
	Timestamp string
}

CacheEntry represents a cached routing decision. This is a copy of cache.CacheEntry to avoid circular imports.

type CascadeDecision

type CascadeDecision struct {
	ShouldCascade bool                   `json:"should_cascade"`
	CurrentTier   string                 `json:"current_tier"`
	NextTier      string                 `json:"next_tier,omitempty"`
	Signals       []CascadeQualitySignal `json:"signals,omitempty"`
	QualityScore  float64                `json:"quality_score"`
	Reason        string                 `json:"reason"`
}

CascadeDecision represents the outcome of a cascade evaluation. This is a copy of cascade.CascadeDecision to avoid circular imports.

type CascadeManagerInterface

type CascadeManagerInterface interface {
	IsEnabled() bool
	EvaluateResponse(response string, currentTier string) *CascadeDecision
	GetMetricsAsMap() map[string]interface{}
}

CascadeManagerInterface defines the interface for model cascading operations. This interface is used by the Lua plugin engine to access cascade functionality.

type CascadeQualitySignal

type CascadeQualitySignal struct {
	Type        string  `json:"type"`
	Severity    float64 `json:"severity"`
	Description string  `json:"description"`
}

CascadeQualitySignal represents a detected quality issue.

type Classification

type Classification struct {
	Intent     string  `json:"intent"`
	Complexity string  `json:"complexity"`
	Privacy    string  `json:"privacy"`
	Confidence float64 `json:"confidence"`
}

Classification represents the result of LLM classification

type CortexRouter

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

CortexRouter implements intelligent multi-tier routing with memory integration. It provides three routing tiers: Reflex (fast rules), Semantic (intent matching), and Cognitive (LLM classification), with learned preferences from memory.

func NewCortexRouter

func NewCortexRouter(cfg *config.IntelligenceConfig, memoryManager memory.MemoryManager) *CortexRouter

NewCortexRouter creates a new Cortex Router with optional memory integration.

func (*CortexRouter) Close

func (cr *CortexRouter) Close()

Close shuts down the router and its components.

func (*CortexRouter) CreateOutcome

func (cr *CortexRouter) CreateOutcome(decision *RoutingDecision, success bool, responseTimeMs int64, err error) *RoutingOutcome

CreateOutcome creates a RoutingOutcome from a routing decision and execution result. This is a helper method to make it easier to record outcomes from executors.

func (*CortexRouter) GetStats

func (cr *CortexRouter) GetStats() map[string]interface{}

GetStats returns routing statistics.

func (*CortexRouter) IsEnabled

func (cr *CortexRouter) IsEnabled() bool

IsEnabled returns whether the Cortex Router is enabled.

func (*CortexRouter) IsMemoryEnabled

func (cr *CortexRouter) IsMemoryEnabled() bool

IsMemoryEnabled returns whether memory integration is enabled.

func (*CortexRouter) RecordOutcome

func (cr *CortexRouter) RecordOutcome(outcome *RoutingOutcome) error

RecordOutcome records the outcome of a routing decision for learning.

func (*CortexRouter) Route

func (cr *CortexRouter) Route(ctx context.Context, request *RoutingRequest) (*RoutingDecision, error)

Route performs intelligent routing using the multi-tier approach. It tries tiers in order: Reflex → Semantic → Cognitive, with memory integration.

func (*CortexRouter) SetEventBus

func (cr *CortexRouter) SetEventBus(bus *hooks.EventBus)

SetEventBus sets the event bus for publishing routing events.

func (*CortexRouter) SetSemanticCache

func (cr *CortexRouter) SetSemanticCache(cache SemanticCacheInterface)

SetSemanticCache sets the semantic cache for caching routing decisions.

func (*CortexRouter) SetSemanticTier

func (cr *CortexRouter) SetSemanticTier(tier SemanticTierInterface)

SetSemanticTier sets the semantic tier for intent matching.

func (*CortexRouter) SetSteeringEngine

func (cr *CortexRouter) SetSteeringEngine(engine *steering.SteeringEngine)

type DiscoveryServiceInterface

type DiscoveryServiceInterface interface {
	GetAvailableModelsAsMap() []map[string]interface{}
}

DiscoveryServiceInterface defines the interface for model discovery operations. This interface is used by the Lua plugin engine to access discovery functionality.

type FeedbackCollectorInterface

type FeedbackCollectorInterface interface {
	IsEnabled() bool
	Record(ctx interface{}, record *FeedbackRecord) error
	GetStats(ctx interface{}) (map[string]interface{}, error)
	GetRecent(ctx interface{}, limit int) (interface{}, error)
}

FeedbackCollectorInterface defines the interface for feedback collection operations. This interface is used by the Lua plugin engine to access feedback functionality.

type FeedbackRecord

type FeedbackRecord struct {
	ID              int64                  `json:"id"`
	Timestamp       string                 `json:"timestamp"`
	Query           string                 `json:"query"`
	Intent          string                 `json:"intent"`
	SelectedModel   string                 `json:"selected_model"`
	RoutingTier     string                 `json:"routing_tier"`
	Confidence      float64                `json:"confidence"`
	MatchedSkill    string                 `json:"matched_skill,omitempty"`
	CascadeOccurred bool                   `json:"cascade_occurred"`
	ResponseQuality float64                `json:"response_quality,omitempty"`
	LatencyMs       int64                  `json:"latency_ms"`
	Success         bool                   `json:"success"`
	ErrorMessage    string                 `json:"error_message,omitempty"`
	Metadata        map[string]interface{} `json:"metadata,omitempty"`
}

FeedbackRecord represents a single routing feedback entry. This is a copy of feedback.FeedbackRecord to avoid circular imports.

type MatrixBuilderInterface

type MatrixBuilderInterface interface {
	GetCurrentMatrixAsMap() map[string]interface{}
}

MatrixBuilderInterface defines the interface for dynamic matrix operations. This interface is used by the Lua plugin engine to access matrix functionality.

type RoutingDecision

type RoutingDecision struct {
	// Request information
	APIKeyHash  string    `json:"api_key_hash"`
	RequestHash string    `json:"request_hash"`
	Timestamp   time.Time `json:"timestamp"`

	// Classification results
	Intent     string `json:"intent"`
	Complexity string `json:"complexity"`
	Privacy    string `json:"privacy"`
	Tier       string `json:"tier"` // reflex, semantic, cognitive, learned

	// Routing decision
	SelectedModel string  `json:"selected_model"`
	Provider      string  `json:"provider"`
	Confidence    float64 `json:"confidence"`

	// Performance metrics
	LatencyMs int64 `json:"latency_ms"`

	// Memory integration
	UsedMemory   bool   `json:"used_memory"`
	MemorySource string `json:"memory_source,omitempty"` // preferences, cache, quirks

	// Reasoning
	Reason string `json:"reason"`
}

RoutingDecision represents the router's decision with confidence and reasoning.

type RoutingOutcome

type RoutingOutcome struct {
	Decision       *RoutingDecision `json:"decision"`
	Success        bool             `json:"success"`
	ResponseTimeMs int64            `json:"response_time_ms"`
	Error          string           `json:"error,omitempty"`
	QualityScore   float64          `json:"quality_score"` // 0.0 to 1.0
	UserFeedback   string           `json:"user_feedback,omitempty"`
}

RoutingOutcome represents the outcome of a routing decision for learning.

type RoutingRequest

type RoutingRequest struct {
	APIKey   string                 `json:"api_key"`
	Model    string                 `json:"model"`
	Messages []map[string]string    `json:"messages"`
	Content  string                 `json:"content"` // Extracted content for analysis
	Metadata map[string]interface{} `json:"metadata"`
}

RoutingRequest represents an incoming request to be routed.

type SemanticCacheInterface

type SemanticCacheInterface interface {
	Lookup(query string) (interface{}, error)
	Store(query string, embedding []float32, decision string, metadata map[string]interface{}) error
	IsEnabled() bool
	GetMetricsAsMap() map[string]interface{}
}

SemanticCacheInterface defines the interface for semantic caching operations. This interface is used by the Lua plugin engine to access cache functionality.

type SemanticMatchResult

type SemanticMatchResult struct {
	Intent     string  `json:"intent"`
	Confidence float64 `json:"confidence"`
	LatencyMs  int64   `json:"latency_ms"`
}

SemanticMatchResult represents the result of semantic intent matching. This is a copy of semantic.MatchResult to avoid circular imports.

type SemanticTierInterface

type SemanticTierInterface interface {
	MatchIntent(query string) (*SemanticMatchResult, error)
	IsEnabled() bool
	GetMetrics() map[string]interface{}
}

SemanticTierInterface defines the interface for semantic intent matching. This interface is used by the Lua plugin engine to access semantic tier functionality.

type Service

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

Service is the main orchestrator for all intelligence services. It manages the lifecycle of Phase 2 features and provides graceful degradation when services are disabled or unavailable.

func NewService

func NewService(cfg *config.IntelligenceConfig) *Service

NewService creates a new IntelligenceService instance. The service is not initialized until Initialize() is called.

Parameters:

  • cfg: The intelligence configuration

Returns:

  • *Service: A new service instance

func (*Service) GetCascadeManager

func (s *Service) GetCascadeManager() CascadeManagerInterface

GetCascadeManager returns the cascade manager instance. Returns nil if cascade is not enabled or not initialized.

Returns:

  • CascadeManagerInterface: The cascade manager instance, or nil

func (*Service) GetConfidenceScorer

func (s *Service) GetConfidenceScorer() *confidence.Scorer

GetConfidenceScorer returns the confidence scorer instance.

func (*Service) GetConfig

func (s *Service) GetConfig() *config.IntelligenceConfig

GetConfig returns the current intelligence configuration. This is useful for services that need to access configuration values.

Returns:

  • *config.IntelligenceConfig: The intelligence configuration

func (*Service) GetCortexRouter

func (s *Service) GetCortexRouter() *CortexRouter

GetCortexRouter returns the Cortex Router instance. Returns nil if not initialized or memory integration is not available.

func (*Service) GetDiscoveryService

func (s *Service) GetDiscoveryService() DiscoveryServiceInterface

GetDiscoveryService returns the discovery service instance. Returns nil if discovery is not enabled or not initialized.

Returns:

  • DiscoveryServiceInterface: The discovery service instance, or nil

func (*Service) GetEmbeddingEngine

func (s *Service) GetEmbeddingEngine() *embedding.Engine

GetEmbeddingEngine returns the embedding engine instance. Returns nil if embedding is not enabled or not initialized.

Returns:

  • *embedding.Engine: The embedding engine instance, or nil

func (*Service) GetFeedbackCollector

func (s *Service) GetFeedbackCollector() FeedbackCollectorInterface

GetFeedbackCollector returns the feedback collector instance. Returns nil if feedback is not enabled or not initialized.

Returns:

  • FeedbackCollectorInterface: The feedback collector instance, or nil

func (*Service) GetMatrixBuilder

func (s *Service) GetMatrixBuilder() MatrixBuilderInterface

GetMatrixBuilder returns the dynamic matrix builder instance. Returns nil if auto-assign is not enabled or not initialized.

Returns:

  • MatrixBuilderInterface: The matrix builder instance, or nil

func (*Service) GetSemanticCache

func (s *Service) GetSemanticCache() SemanticCacheInterface

GetSemanticCache returns the semantic cache instance. Returns nil if semantic cache is not enabled or not initialized.

Returns:

  • SemanticCacheInterface: The semantic cache instance, or nil

func (*Service) GetSemanticTier

func (s *Service) GetSemanticTier() SemanticTierInterface

GetSemanticTier returns the semantic tier instance. Returns nil if semantic tier is not enabled or not initialized.

Returns:

  • SemanticTierInterface: The semantic tier instance, or nil

func (*Service) GetSkillRegistry

func (s *Service) GetSkillRegistry() *skills.Registry

GetSkillRegistry returns the skill registry instance as an interface. Returns nil if skills are not enabled or not initialized.

Returns:

  • *skills.Registry: The skill registry instance, or nil

func (*Service) GetStateBox

func (s *Service) GetStateBox() *util.StateBox

GetStateBox returns the StateBox instance used by the intelligence service. Returns nil if StateBox was not initialized.

func (*Service) GetVerifier

func (s *Service) GetVerifier() *verification.Verifier

GetVerifier returns the verifier instance.

func (*Service) Initialize

func (s *Service) Initialize(ctx context.Context) error

Initialize starts all enabled intelligence services. Services are started based on their individual feature flags. If the master switch (intelligence.enabled) is false, this is a no-op.

Parameters:

  • ctx: Context for initialization operations

Returns:

  • error: Any error encountered during initialization

func (*Service) IsEnabled

func (s *Service) IsEnabled() bool

IsEnabled returns whether intelligence services are active. This is used by Lua API functions to determine if they should return errors or attempt to use intelligence features.

Returns:

  • bool: true if intelligence services are enabled and initialized

func (*Service) IsModelAvailable

func (s *Service) IsModelAvailable(modelID string) bool

IsModelAvailable checks if a model ID is in the discovered models. Returns false if discovery is not enabled or the model is not found.

Parameters:

  • modelID: The model ID to check

Returns:

  • bool: true if the model is available

func (*Service) SetMemoryManager

func (s *Service) SetMemoryManager(mm memory.MemoryManager)

SetMemoryManager sets the memory manager for intelligence services.

func (*Service) Shutdown

func (s *Service) Shutdown(ctx context.Context) error

Shutdown gracefully stops all running intelligence services. It ensures all resources are properly cleaned up and any pending operations are completed or cancelled.

Parameters:

  • ctx: Context for shutdown operations (with timeout)

Returns:

  • error: Any error encountered during shutdown

Directories

Path Synopsis
Package cache provides semantic caching for routing decisions.
Package cache provides semantic caching for routing decisions.
Package capability provides model capability analysis for the intelligence system.
Package capability provides model capability analysis for the intelligence system.
Package cascade provides model cascading functionality for intelligent routing.
Package cascade provides model cascading functionality for intelligent routing.
Package discovery provides model discovery services for the intelligence system.
Package discovery provides model discovery services for the intelligence system.
Package embedding provides an ONNX-based embedding engine for semantic matching.
Package embedding provides an ONNX-based embedding engine for semantic matching.
Package feedback provides feedback collection and storage for routing decisions.
Package feedback provides feedback collection and storage for routing decisions.
Package matrix provides dynamic capability matrix building for the intelligence system.
Package matrix provides dynamic capability matrix building for the intelligence system.
Package semantic provides semantic intent matching for Phase 2 intelligent routing.
Package semantic provides semantic intent matching for Phase 2 intelligent routing.
Package skills provides an enhanced skill registry for Phase 2 intelligent routing.
Package skills provides an enhanced skill registry for Phase 2 intelligent routing.

Jump to

Keyboard shortcuts

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