109 lines
3.1 KiB
Python
109 lines
3.1 KiB
Python
from __future__ import annotations
|
|
|
|
from dataclasses import dataclass, field
|
|
from typing import Any, Literal
|
|
|
|
|
|
@dataclass
|
|
class Observation:
|
|
scene_summary: str
|
|
semantic_page: str | None = None
|
|
semantic_intents: list[str] = field(default_factory=list)
|
|
world_app: str | None = None
|
|
world_page: str | None = None
|
|
world_variables: dict[str, Any] = field(default_factory=dict)
|
|
raw_scene: dict[str, Any] | None = None
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"scene_summary": self.scene_summary,
|
|
"semantic_page": self.semantic_page,
|
|
"semantic_intents": list(self.semantic_intents),
|
|
"world_app": self.world_app,
|
|
"world_page": self.world_page,
|
|
"world_variables": dict(self.world_variables),
|
|
"raw_scene": self.raw_scene,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> Observation:
|
|
return cls(
|
|
scene_summary=str(data["scene_summary"]),
|
|
semantic_page=data.get("semantic_page"),
|
|
semantic_intents=list(data.get("semantic_intents", [])),
|
|
world_app=data.get("world_app"),
|
|
world_page=data.get("world_page"),
|
|
world_variables=dict(data.get("world_variables", {})),
|
|
raw_scene=data.get("raw_scene"),
|
|
)
|
|
|
|
|
|
VerdictResult = Literal["achieved", "not_achieved"]
|
|
|
|
|
|
@dataclass
|
|
class VerificationVerdict:
|
|
result: VerdictResult
|
|
reasoning: str
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"result": self.result,
|
|
"reasoning": self.reasoning,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> VerificationVerdict:
|
|
result = data["result"]
|
|
if result not in ("achieved", "not_achieved"):
|
|
raise ValueError(f"invalid verdict result: {result}")
|
|
return cls(
|
|
result=result,
|
|
reasoning=str(data["reasoning"]),
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class ReflectionAction:
|
|
action: str
|
|
description: str
|
|
args: dict[str, Any] = field(default_factory=dict)
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"action": self.action,
|
|
"description": self.description,
|
|
"args": dict(self.args),
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> ReflectionAction:
|
|
return cls(
|
|
action=str(data["action"]),
|
|
description=str(data["description"]),
|
|
args=dict(data.get("args", {})),
|
|
)
|
|
|
|
|
|
@dataclass
|
|
class ReflectionOutcome:
|
|
replan: bool = False
|
|
action: ReflectionAction | None = None
|
|
reasoning: str = ""
|
|
|
|
def to_dict(self) -> dict[str, Any]:
|
|
return {
|
|
"replan": self.replan,
|
|
"action": self.action.to_dict() if self.action else None,
|
|
"reasoning": self.reasoning,
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: dict[str, Any]) -> ReflectionOutcome:
|
|
action_data = data.get("action")
|
|
return cls(
|
|
replan=bool(data.get("replan", False)),
|
|
action=ReflectionAction.from_dict(action_data) if action_data else None,
|
|
reasoning=str(data.get("reasoning", "")),
|
|
)
|