Log task planner conversations locally
Tests / Test apps.device-host-agent.tests.test_mcp_token.test_load_or_create_concurrent_calls_do_not_corrupt failed
Tests / Test apps.device-host-agent.tests.test_mcp_token.test_load_or_create_concurrent_calls_do_not_corrupt failed
This commit is contained in:
+28
-7
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from collections.abc import Callable
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from core.errors import TaskFailedError
|
||||
@@ -23,9 +24,11 @@ class AIPlanner(Planner):
|
||||
*,
|
||||
client: ToolCallingClient | None = None,
|
||||
config: PlannerConfig | None = None,
|
||||
event_logger: Callable[[dict[str, Any]], None] | None = None,
|
||||
) -> None:
|
||||
self.config = config or load_config()
|
||||
self.client = client or build_client(self.config)
|
||||
self.event_logger = event_logger
|
||||
|
||||
def plan(
|
||||
self,
|
||||
@@ -49,13 +52,24 @@ class AIPlanner(Planner):
|
||||
"corrected action or finish the task if it cannot proceed.\n"
|
||||
f"Previous failure: {context.step_results[-1].error or 'unknown error'}"
|
||||
)
|
||||
decision = self.client.decide(
|
||||
system_prompt=PLANNER_SYSTEM_PROMPT,
|
||||
user_prompt=user_prompt,
|
||||
screenshot=screenshot,
|
||||
tools=ALL_TOOL_SPECS,
|
||||
timeout=self.config.timeout,
|
||||
)
|
||||
self._log({"type": "llm_request", "task_id": context.task_id, "goal": goal,
|
||||
"system_prompt": PLANNER_SYSTEM_PROMPT, "user_prompt": user_prompt,
|
||||
"has_screenshot": screenshot is not None})
|
||||
try:
|
||||
decision = self.client.decide(
|
||||
system_prompt=PLANNER_SYSTEM_PROMPT,
|
||||
user_prompt=user_prompt,
|
||||
screenshot=screenshot,
|
||||
tools=ALL_TOOL_SPECS,
|
||||
timeout=self.config.timeout,
|
||||
)
|
||||
except Exception as exc:
|
||||
self._log({"type": "agent_error", "task_id": context.task_id, "error": str(exc)})
|
||||
raise
|
||||
self._log({"type": "llm_response", "task_id": context.task_id,
|
||||
"content": decision.text_output, "thinking": decision.thinking,
|
||||
"tool_name": decision.tool_name, "arguments": decision.arguments,
|
||||
"purpose": decision.purpose, "expected_outcome": decision.expected_outcome})
|
||||
|
||||
if decision.tool_name == FINISH_TASK_TOOL:
|
||||
if decision.arguments.get("success"):
|
||||
@@ -85,6 +99,13 @@ class AIPlanner(Planner):
|
||||
# (mapped to an empty plan above), never via this hook.
|
||||
return False
|
||||
|
||||
def _log(self, event: dict[str, Any]) -> None:
|
||||
if self.event_logger is not None:
|
||||
try:
|
||||
self.event_logger(event)
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
|
||||
def _history_summary(world: "WorldState | None") -> list[dict[str, Any]]:
|
||||
if world is None:
|
||||
|
||||
Reference in New Issue
Block a user