Files
pragma-stack/backend/app/services/context/__init__.py
Felipe Cardoso c2466ab401 feat(context): implement Redis-based caching layer (#84)
Phase 6 of Context Management Engine - Caching Layer:

- Add ContextCache with Redis integration
- Support fingerprint-based assembled context caching
- Support token count caching (model-specific)
- Support score caching (scorer + context + query)
- Add in-memory fallback with LRU eviction
- Add cache invalidation with pattern matching
- Add cache statistics reporting

Key features:
- Hierarchical cache key structure (ctx:type:hash)
- Automatic TTL expiration
- Memory cache for fast repeated access
- Graceful degradation when Redis unavailable

Tests: 29 new tests, 285 total context tests passing

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-04 02:41:21 +01:00

190 lines
3.6 KiB
Python

"""
Context Management Engine
Sophisticated context assembly and optimization for LLM requests.
Provides intelligent context selection, token budget management,
and model-specific formatting.
Usage:
from app.services.context import (
ContextSettings,
get_context_settings,
SystemContext,
KnowledgeContext,
ConversationContext,
TaskContext,
ToolContext,
TokenBudget,
BudgetAllocator,
TokenCalculator,
)
# Get settings
settings = get_context_settings()
# Create budget for a model
allocator = BudgetAllocator(settings)
budget = allocator.create_budget_for_model("claude-3-sonnet")
# Create context instances
system_ctx = SystemContext.create_persona(
name="Code Assistant",
description="You are a helpful code assistant.",
capabilities=["Write code", "Debug issues"],
)
"""
# Budget Management
from .budget import (
BudgetAllocator,
TokenBudget,
TokenCalculator,
)
# Configuration
from .config import (
ContextSettings,
get_context_settings,
get_default_settings,
reset_context_settings,
)
# Exceptions
from .exceptions import (
AssemblyTimeoutError,
BudgetExceededError,
CacheError,
CompressionError,
ContextError,
ContextNotFoundError,
FormattingError,
InvalidContextError,
ScoringError,
TokenCountError,
)
# Assembly
from .assembly import (
ContextPipeline,
PipelineMetrics,
)
# Compression
from .compression import (
ContextCompressor,
TruncationResult,
TruncationStrategy,
)
# Adapters
from .adapters import (
ClaudeAdapter,
DefaultAdapter,
get_adapter,
ModelAdapter,
OpenAIAdapter,
)
# Cache
from .cache import ContextCache
# Prioritization
from .prioritization import (
ContextRanker,
RankingResult,
)
# Scoring
from .scoring import (
BaseScorer,
CompositeScorer,
PriorityScorer,
RecencyScorer,
RelevanceScorer,
ScoredContext,
)
# Types
from .types import (
AssembledContext,
BaseContext,
ContextPriority,
ContextType,
ConversationContext,
KnowledgeContext,
MessageRole,
SystemContext,
TaskComplexity,
TaskContext,
TaskStatus,
ToolContext,
ToolResultStatus,
)
__all__ = [
# Adapters
"ClaudeAdapter",
"DefaultAdapter",
"get_adapter",
"ModelAdapter",
"OpenAIAdapter",
# Assembly
"ContextPipeline",
"PipelineMetrics",
# Budget Management
"BudgetAllocator",
"TokenBudget",
"TokenCalculator",
# Cache
"ContextCache",
# Compression
"ContextCompressor",
"TruncationResult",
"TruncationStrategy",
# Configuration
"ContextSettings",
"get_context_settings",
"get_default_settings",
"reset_context_settings",
# Exceptions
"AssemblyTimeoutError",
"BudgetExceededError",
"CacheError",
"CompressionError",
"ContextError",
"ContextNotFoundError",
"FormattingError",
"InvalidContextError",
"ScoringError",
"TokenCountError",
# Prioritization
"ContextRanker",
"RankingResult",
# Scoring
"BaseScorer",
"CompositeScorer",
"PriorityScorer",
"RecencyScorer",
"RelevanceScorer",
"ScoredContext",
# Types - Base
"AssembledContext",
"BaseContext",
"ContextPriority",
"ContextType",
# Types - Conversation
"ConversationContext",
"MessageRole",
# Types - Knowledge
"KnowledgeContext",
# Types - System
"SystemContext",
# Types - Task
"TaskComplexity",
"TaskContext",
"TaskStatus",
# Types - Tool
"ToolContext",
"ToolResultStatus",
]