## Changes ### New Context Type - Add MEMORY to ContextType enum for agent memory context - Create MemoryContext class with subtypes (working, episodic, semantic, procedural) - Factory methods: from_working_memory, from_episodic_memory, from_semantic_memory, from_procedural_memory ### Memory Context Source - MemoryContextSource service fetches relevant memories for context assembly - Configurable fetch limits per memory type - Parallel fetching from all memory types ### Agent Lifecycle Hooks - AgentLifecycleManager handles spawn, pause, resume, terminate events - spawn: Initialize working memory with optional initial state - pause: Create checkpoint of working memory - resume: Restore from checkpoint - terminate: Consolidate working memory to episodic memory - LifecycleHooks for custom extension points ### Context Engine Integration - Add memory_query parameter to assemble_context() - Add session_id and agent_type_id for memory scoping - Memory budget allocation (15% by default) - set_memory_source() for runtime configuration ### Tests - 48 new tests for MemoryContext, MemoryContextSource, and lifecycle hooks - All 108 memory-related tests passing - mypy and ruff checks passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
183 lines
3.5 KiB
Python
183 lines
3.5 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
|
|
# Adapters
|
|
from .adapters import (
|
|
ClaudeAdapter,
|
|
DefaultAdapter,
|
|
ModelAdapter,
|
|
OpenAIAdapter,
|
|
get_adapter,
|
|
)
|
|
|
|
# Assembly
|
|
from .assembly import (
|
|
ContextPipeline,
|
|
PipelineMetrics,
|
|
)
|
|
from .budget import (
|
|
BudgetAllocator,
|
|
TokenBudget,
|
|
TokenCalculator,
|
|
)
|
|
|
|
# Cache
|
|
from .cache import ContextCache
|
|
|
|
# Compression
|
|
from .compression import (
|
|
ContextCompressor,
|
|
TruncationResult,
|
|
TruncationStrategy,
|
|
)
|
|
|
|
# Configuration
|
|
from .config import (
|
|
ContextSettings,
|
|
get_context_settings,
|
|
get_default_settings,
|
|
reset_context_settings,
|
|
)
|
|
|
|
# Engine
|
|
from .engine import ContextEngine, create_context_engine
|
|
|
|
# Exceptions
|
|
from .exceptions import (
|
|
AssemblyTimeoutError,
|
|
BudgetExceededError,
|
|
CacheError,
|
|
CompressionError,
|
|
ContextError,
|
|
ContextNotFoundError,
|
|
FormattingError,
|
|
InvalidContextError,
|
|
ScoringError,
|
|
TokenCountError,
|
|
)
|
|
|
|
# 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,
|
|
MemoryContext,
|
|
MemorySubtype,
|
|
MessageRole,
|
|
SystemContext,
|
|
TaskComplexity,
|
|
TaskContext,
|
|
TaskStatus,
|
|
ToolContext,
|
|
ToolResultStatus,
|
|
)
|
|
|
|
__all__ = [
|
|
"AssembledContext",
|
|
"AssemblyTimeoutError",
|
|
"BaseContext",
|
|
"BaseScorer",
|
|
"BudgetAllocator",
|
|
"BudgetExceededError",
|
|
"CacheError",
|
|
"ClaudeAdapter",
|
|
"CompositeScorer",
|
|
"CompressionError",
|
|
"ContextCache",
|
|
"ContextCompressor",
|
|
"ContextEngine",
|
|
"ContextError",
|
|
"ContextNotFoundError",
|
|
"ContextPipeline",
|
|
"ContextPriority",
|
|
"ContextRanker",
|
|
"ContextSettings",
|
|
"ContextType",
|
|
"ConversationContext",
|
|
"DefaultAdapter",
|
|
"FormattingError",
|
|
"InvalidContextError",
|
|
"KnowledgeContext",
|
|
"MemoryContext",
|
|
"MemorySubtype",
|
|
"MessageRole",
|
|
"ModelAdapter",
|
|
"OpenAIAdapter",
|
|
"PipelineMetrics",
|
|
"PriorityScorer",
|
|
"RankingResult",
|
|
"RecencyScorer",
|
|
"RelevanceScorer",
|
|
"ScoredContext",
|
|
"ScoringError",
|
|
"SystemContext",
|
|
"TaskComplexity",
|
|
"TaskContext",
|
|
"TaskStatus",
|
|
"TokenBudget",
|
|
"TokenCalculator",
|
|
"TokenCountError",
|
|
"ToolContext",
|
|
"ToolResultStatus",
|
|
"TruncationResult",
|
|
"TruncationStrategy",
|
|
"create_context_engine",
|
|
"get_adapter",
|
|
"get_context_settings",
|
|
"get_default_settings",
|
|
"reset_context_settings",
|
|
]
|