50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
from __future__ import annotations
|
|
|
|
from collections import deque
|
|
from dataclasses import dataclass, field
|
|
from datetime import datetime
|
|
from typing import Any
|
|
|
|
from core.models import Scene, utc_now
|
|
from semantic.models import SemanticScene
|
|
from world.config import DEFAULT_HISTORY_SIZE
|
|
|
|
|
|
@dataclass(frozen=True)
|
|
class WorldEvent:
|
|
scene_summary: SemanticScene | Scene
|
|
action: str
|
|
success: bool
|
|
timestamp: datetime = field(default_factory=utc_now)
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"scene_summary": self.scene_summary.to_dict(),
|
|
"action": self.action,
|
|
"success": self.success,
|
|
"timestamp": self.timestamp.isoformat(),
|
|
}
|
|
|
|
|
|
@dataclass
|
|
class WorldState:
|
|
current_app: str | None = None
|
|
current_page: str | None = None
|
|
variables: dict[str, Any] = field(default_factory=dict)
|
|
history: deque[WorldEvent] = field(
|
|
default_factory=lambda: deque(maxlen=DEFAULT_HISTORY_SIZE)
|
|
)
|
|
|
|
@classmethod
|
|
def with_history_bound(cls, history_size: int) -> "WorldState":
|
|
maxlen = history_size if history_size > 0 else DEFAULT_HISTORY_SIZE
|
|
return cls(history=deque(maxlen=maxlen))
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"current_app": self.current_app,
|
|
"current_page": self.current_page,
|
|
"variables": dict(self.variables),
|
|
"history": [event.to_dict() for event in self.history],
|
|
}
|