router

package
v0.60.3 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package router provides the LLM routing service.

Package router provides the LLM routing service interface for AI agents. This interface is consumed by Team B (Assistant+Schedule) and Team C (Memo Enhancement).

Package router provides the LLM routing service.

Package router provides the LLM routing service.

Package router provides the LLM routing service.

Index

Constants

View Source
const ClassificationPrompt = `` /* 656-byte string literal not displayed */

ClassificationPrompt is the prompt template for intent classification.

Variables

This section is empty.

Functions

func WithUserID

func WithUserID(ctx context.Context, userID int32) context.Context

WithUserID returns a context with user ID.

Types

type AgentType

type AgentType string

AgentType represents the agent type for routing.

const (
	AgentTypeMemo     AgentType = "memo"
	AgentTypeSchedule AgentType = "schedule"
	AgentTypeAmazing  AgentType = "amazing"
	AgentTypeUnknown  AgentType = "unknown"
)

func IntentToAgentType

func IntentToAgentType(intent Intent) AgentType

IntentToAgentType converts Intent to AgentType. This is the canonical mapping used across the routing system.

type Config

type Config struct {
	MemoryService memory.MemoryService
	LLMClient     LLMClient
}

Config contains the configuration for the router service.

type HistoryMatchResult

type HistoryMatchResult struct {
	Intent     Intent
	SourceID   int64
	Confidence float32
	Matched    bool
}

HistoryMatchResult contains the result of history matching.

type HistoryMatcher

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

HistoryMatcher implements Layer 2 history-based intent matching. Layer 2a: Lexical similarity (~1ms) - Jaccard on character bigrams Layer 2b: Semantic similarity (~50ms) - Embedding cosine similarity (optional) Target: Handle 30%+ of requests that pass Layer 1.

func NewHistoryMatcher

func NewHistoryMatcher(ms memory.MemoryService) *HistoryMatcher

NewHistoryMatcher creates a new history matcher.

func (*HistoryMatcher) Match

func (m *HistoryMatcher) Match(ctx context.Context, userID int32, input string) (*HistoryMatchResult, error)

Match attempts to classify intent by finding similar historical patterns. Layer 2a: Lexical matching (~1ms) - high precision Layer 2b: Semantic matching (~50ms) - triggered when lexical is ambiguous Returns matched=true if a similar pattern was found with confidence >= threshold.

func (*HistoryMatcher) SaveDecision

func (m *HistoryMatcher) SaveDecision(ctx context.Context, userID int32, input string, intent Intent, success bool) error

SaveDecision saves a routing decision to memory for future matching.

func (*HistoryMatcher) SetEmbeddingService

func (m *HistoryMatcher) SetEmbeddingService(es ai.EmbeddingService)

SetEmbeddingService sets the embedding service for semantic similarity matching.

type Intent

type Intent string

Intent represents the type of user intent.

const (
	IntentMemoSearch     Intent = "memo_search"
	IntentMemoCreate     Intent = "memo_create"
	IntentScheduleQuery  Intent = "schedule_query"
	IntentScheduleCreate Intent = "schedule_create"
	IntentScheduleUpdate Intent = "schedule_update"
	IntentBatchSchedule  Intent = "batch_schedule"
	IntentAmazing        Intent = "amazing"
	IntentUnknown        Intent = "unknown"
)

func AgentTypeToIntent

func AgentTypeToIntent(agentType AgentType) Intent

AgentTypeToIntent converts AgentType to default Intent. Used when a specific intent subtype cannot be determined.

type LLMClassifier

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

LLMClassifier implements Layer 3 LLM-based intent classification. Target: ~400ms latency, handle only ~20% of requests that pass Layer 1&2.

func NewLLMClassifier

func NewLLMClassifier(client LLMClient) *LLMClassifier

NewLLMClassifier creates a new LLM classifier.

func (*LLMClassifier) Classify

func (c *LLMClassifier) Classify(ctx context.Context, input string) (*LLMClassifyResult, error)

Classify classifies user intent using LLM. This is the fallback layer for truly ambiguous inputs.

type LLMClassifyResult

type LLMClassifyResult struct {
	Intent     Intent
	Reasoning  string
	Confidence float32
}

LLMClassifyResult contains the result of LLM classification.

type LLMClient

type LLMClient interface {
	// Complete sends a completion request and returns the response.
	Complete(ctx context.Context, prompt string, config ModelConfig) (string, error)
}

LLMClient defines the interface for LLM API calls.

type MockRouterService

type MockRouterService struct {
	// IntentOverrides allows tests to override intent classification results
	IntentOverrides map[string]Intent
	// ModelOverrides allows tests to override model selection results
	ModelOverrides map[TaskType]ModelConfig
}

MockRouterService is a mock implementation of RouterService for testing.

func NewMockRouterService

func NewMockRouterService() *MockRouterService

NewMockRouterService creates a new MockRouterService.

func (*MockRouterService) ClassifyIntent

func (m *MockRouterService) ClassifyIntent(ctx context.Context, input string) (Intent, float32, error)

ClassifyIntent classifies user intent using rule-based matching.

func (*MockRouterService) SelectModel

func (m *MockRouterService) SelectModel(ctx context.Context, task TaskType) (ModelConfig, error)

SelectModel selects an appropriate model based on task type.

type ModelConfig

type ModelConfig struct {
	Provider    string  `json:"provider"` // local/cloud
	Model       string  `json:"model"`    // model name
	MaxTokens   int     `json:"max_tokens"`
	Temperature float32 `json:"temperature"`
}

ModelConfig represents the configuration for a model.

type RouterService

type RouterService interface {
	// ClassifyIntent classifies user intent from input text.
	// Returns: intent type, confidence (0-1), error
	// Implementation: rule-based first (0ms) -> LLM fallback (~400ms)
	ClassifyIntent(ctx context.Context, input string) (Intent, float32, error)

	// SelectModel selects an appropriate model based on task type.
	// Returns: model configuration (local/cloud)
	SelectModel(ctx context.Context, task TaskType) (ModelConfig, error)
}

Consumers: Team B (Assistant+Schedule), Team C (Memo Enhancement).

type RuleMatcher

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

RuleMatcher implements Layer 1 rule-based intent matching. Target: 0ms latency, handle 60%+ of requests.

func NewRuleMatcher

func NewRuleMatcher() *RuleMatcher

NewRuleMatcher creates a new rule matcher with predefined keyword weights.

func (*RuleMatcher) Match

func (m *RuleMatcher) Match(input string) (Intent, float32, bool)

Returns: intent, confidence, matched (true if rule matched).

type Service

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

Layer 3: LLM classification (~400ms) - fallback for remaining ~20%.

func NewService

func NewService(cfg Config) *Service

NewService creates a new router service.

func (*Service) ClassifyIntent

func (s *Service) ClassifyIntent(ctx context.Context, input string) (Intent, float32, error)

Implementation: rule-based first (0ms) -> history match (~10ms) -> LLM fallback (~400ms).

func (*Service) SelectModel

func (s *Service) SelectModel(ctx context.Context, task TaskType) (ModelConfig, error)

Returns: model configuration (local/cloud).

type TaskType

type TaskType string

TaskType represents the type of task for model selection.

const (
	TaskIntentClassification TaskType = "intent_classification"
	TaskEntityExtraction     TaskType = "entity_extraction"
	TaskSimpleQA             TaskType = "simple_qa"
	TaskComplexReasoning     TaskType = "complex_reasoning"
	TaskSummarization        TaskType = "summarization"
	TaskTagSuggestion        TaskType = "tag_suggestion"
)

Jump to

Keyboard shortcuts

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