Files
syndarix/backend/app/services/context/__init__.py
Felipe Cardoso 7828d35e06 feat(context): implement model adapters for Claude and OpenAI (#83)
Phase 5 of Context Management Engine - Model Adapters:

- Add ModelAdapter abstract base class with model matching
- Add DefaultAdapter for unknown models (plain text)
- Add ClaudeAdapter with XML-based formatting:
  - <system_instructions> for system context
  - <reference_documents>/<document> for knowledge
  - <conversation_history>/<message> for chat
  - <tool_results>/<tool_result> for tool outputs
  - XML escaping for special characters
- Add OpenAIAdapter with markdown formatting:
  - ## headers for sections
  - ### Source headers for documents
  - **ROLE** bold labels for conversation
  - Code blocks for tool outputs
- Add get_adapter() factory function for model selection

Tests: 33 new tests, 256 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:36:32 +01:00

185 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,
)
# 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",
# 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",
]