forked from cardosofelipe/fast-next-template
Implements the foundation for Context Management Engine: Types (backend/app/services/context/types/): - BaseContext: Abstract base with ID, content, priority, scoring - SystemContext: System prompts, personas, instructions - KnowledgeContext: RAG results from Knowledge Base MCP - ConversationContext: Chat history with role support - TaskContext: Task/issue context with acceptance criteria - ToolContext: Tool definitions and execution results - AssembledContext: Final assembled context result Configuration (config.py): - Token budget allocation (system 5%, task 10%, knowledge 40%, etc.) - Scoring weights (relevance 50%, recency 30%, priority 20%) - Cache settings (TTL, prefix) - Performance settings (max assembly time, parallel scoring) - Environment variable overrides with CTX_ prefix Exceptions (exceptions.py): - ContextError: Base exception - BudgetExceededError: Token budget violations - TokenCountError: Token counting failures - CompressionError: Compression failures - AssemblyTimeoutError: Assembly timeout - ScoringError, FormattingError, CacheError - ContextNotFoundError, InvalidContextError All 86 tests pass. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
106 lines
2.1 KiB
Python
106 lines
2.1 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,
|
|
)
|
|
|
|
# Get settings
|
|
settings = get_context_settings()
|
|
|
|
# Create context instances
|
|
system_ctx = SystemContext.create_persona(
|
|
name="Code Assistant",
|
|
description="You are a helpful code assistant.",
|
|
capabilities=["Write code", "Debug issues"],
|
|
)
|
|
"""
|
|
|
|
# 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,
|
|
)
|
|
|
|
# Types
|
|
from .types import (
|
|
AssembledContext,
|
|
BaseContext,
|
|
ContextPriority,
|
|
ContextType,
|
|
ConversationContext,
|
|
KnowledgeContext,
|
|
MessageRole,
|
|
SystemContext,
|
|
TaskComplexity,
|
|
TaskContext,
|
|
TaskStatus,
|
|
ToolContext,
|
|
ToolResultStatus,
|
|
)
|
|
|
|
__all__ = [
|
|
# Configuration
|
|
"ContextSettings",
|
|
"get_context_settings",
|
|
"get_default_settings",
|
|
"reset_context_settings",
|
|
# Exceptions
|
|
"AssemblyTimeoutError",
|
|
"BudgetExceededError",
|
|
"CacheError",
|
|
"CompressionError",
|
|
"ContextError",
|
|
"ContextNotFoundError",
|
|
"FormattingError",
|
|
"InvalidContextError",
|
|
"ScoringError",
|
|
"TokenCountError",
|
|
# 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",
|
|
]
|