Files
agentic-mobile-control/runtime/ai_planner.py
T
q792602257andClaude Sonnet 5 61ff3b425d feat(agent-runtime): add LLM-driven AI Planner with dual-provider tool calling
Replaces the stub Planner's fixed describe_screen/[] behavior with a real
decision-maker: AIPlanner uses native tool/function calling (Anthropic or
OpenAI, pluggable via AI_PLANNER_PROVIDER) to select exactly one grounded
action per turn, with an explicit finish_task(success, reason) tool for
completion/failure instead of an ambiguous "no tool call" signal. Default
disabled (AI_PLANNER_ENABLED=false) and additive; TaskRunner falls back to
the existing stub Planner unchanged when disabled.

Amends CONSTITUTION.md's Perception Boundary with one narrow exception:
only the AI Planner may receive the current step's raw screenshot bytes
alongside Scene, for vision-grounded coordinate grounding. Also fixes a
latent gap in TaskRunner.run(): observe/plan exceptions are now caught per
iteration and turned into a failed task with a failure_reason, instead of
propagating uncaught.

openspec change: ai-planner-runtime.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-12 13:48:50 +08:00

74 lines
2.3 KiB
Python

from __future__ import annotations
from typing import TYPE_CHECKING, Any
from core.errors import TaskFailedError
from core.models import Scene
from runtime.context import TaskContext
from runtime.planner import PlannedStep, Planner
from runtime.planner_config import PlannerConfig, load_config
from runtime.planner_prompts import PLANNER_SYSTEM_PROMPT, planner_user_prompt
from runtime.tool_calling_client import ToolCallingClient, build_client
from runtime.tool_specs import ALL_TOOL_SPECS
if TYPE_CHECKING:
from world.models import WorldState
FINISH_TASK_TOOL = "finish_task"
class AIPlanner(Planner):
def __init__(
self,
*,
client: ToolCallingClient | None = None,
config: PlannerConfig | None = None,
) -> None:
self.config = config or load_config()
self.client = client or build_client(self.config)
def plan(
self,
*,
goal: str,
scene: Scene,
context: TaskContext,
world: "WorldState | None" = None,
screenshot: bytes | None = None,
) -> list[PlannedStep]:
decision = self.client.decide(
system_prompt=PLANNER_SYSTEM_PROMPT,
user_prompt=planner_user_prompt(
goal=goal,
scene_json=scene.to_dict(),
history_summary=_history_summary(world),
),
screenshot=screenshot,
tools=ALL_TOOL_SPECS,
timeout=self.config.timeout,
)
if decision.tool_name == FINISH_TASK_TOOL:
if decision.arguments.get("success"):
return []
raise TaskFailedError(decision.arguments.get("reason") or "task failed")
return [
PlannedStep(
action=decision.tool_name,
description=f"AI planner: {decision.tool_name}({decision.arguments})",
args=dict(decision.arguments),
)
]
def goal_reached(self, *, goal: str, scene: Scene, context: TaskContext) -> bool:
# Completion is signaled exclusively via the finish_task tool call
# (mapped to an empty plan above), never via this hook.
return False
def _history_summary(world: "WorldState | None") -> list[dict[str, Any]]:
if world is None:
return []
return [event.to_dict() for event in world.history]