File size: 1,703 Bytes
6d9c72b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Pydantic data models - typed contracts for ContextForge."""
from pydantic import BaseModel, Field
from datetime import datetime
from typing import Literal


class ContextEntry(BaseModel):
    """A registered agent context with compression support."""
    agent_id: str
    context: str
    compressed_context: str | None = None
    embedding: list[float] | None = None
    token_count: int
    compressed_token_count: int | None = None
    created_at: datetime = Field(default_factory=datetime.now)
    ttl_seconds: int = 300

    def model_post_init(self, __context) -> None:
        if self.embedding is None:
            self.embedding = []


class ContextMatch(BaseModel):
    """A semantic match between contexts."""
    agent_id: str
    similarity: float
    shared_prefix: str
    tokens_saved: int


class CompressionDecision(BaseModel):
    """Decision made by the compression coordinator."""
    strategy: Literal["apc_reuse", "compress", "compress_and_reuse", "passthrough"]
    shared_prefix: str | None = None
    compressed_context: str | None = None
    original_tokens: int
    final_tokens: int
    savings_pct: float


class MetricsSnapshot(BaseModel):
    """Real-time system metrics."""
    timestamp: datetime = Field(default_factory=datetime.now)
    vram_used_gb: float
    vram_total_gb: float
    ttft_ms: float
    tokens_processed: int
    tokens_saved: int
    dedup_rate: float
    compression_ratio: float
    active_agents: int


class ContextRegistration(BaseModel):
    """Request to register a new context."""
    agent_id: str
    context: str


class OptimizedContextRequest(BaseModel):
    """Request for optimized context."""
    agent_id: str
    context: str