Files
agentic-mobile-control/world/models.py
T
q792602257 a5aeb8889c
Tests / Test failed: 2, passed: 849
feat(runtime): add planner reflection history with rationale and thinking
- ToolCallDecision captures thinking blocks and pre-tool text output
- AnthropicToolCallingClient supports optional extended thinking (budget_tokens + beta header)
- PlannedStep carries rationale and thinking from each LLM decision
- WorldEvent replaces scene_summary with rationale/thinking/page fields (backward-compatible)
- AI planner system prompt instructs reflection before each tool call
- _history_summary() emits compact {page, rationale, action, success} dicts
- Cloud DB migration 0011 adds nullable rationale/thinking columns to planner_decision_log
- OpenAI client extracts reasoning_content into thinking field
2026-07-15 12:43:22 +08:00

58 lines
1.8 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:
action: str
success: bool
# Optional for backward compatibility; new entries created by AIPlanner
# paths leave this as None and use rationale/thinking instead.
scene_summary: SemanticScene | Scene | None = None
rationale: str | None = None
thinking: str | None = None
page: str | None = None
timestamp: datetime = field(default_factory=utc_now)
def to_dict(self) -> dict[str, Any]:
return {
"scene_summary": self.scene_summary.to_dict() if self.scene_summary is not None else None,
"action": self.action,
"success": self.success,
"rationale": self.rationale,
"thinking": self.thinking,
"page": self.page,
"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],
}