context.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. """Context builders backed only by ``find_agent_v2_*`` tables."""
  2. from __future__ import annotations
  3. import json
  4. from typing import Any
  5. from find_agent_v2.observability import InputSlot
  6. from find_agent_v2.service import get_find_agent_v2_service
  7. from find_agent_v2.state import DiscoverySnapshot
  8. def load_full_state(run_id: str, *, limit: int = 100) -> dict[str, Any]:
  9. return get_find_agent_v2_service().get_full_state(run_id, limit=limit)
  10. def snapshot_run(run_id: str) -> DiscoverySnapshot:
  11. return get_find_agent_v2_service().snapshot(run_id)
  12. def require_running_run(run_id: str) -> dict[str, Any]:
  13. run = get_find_agent_v2_service().require_run(run_id)
  14. if str(run.get("status") or "") not in {"running", "finished"}:
  15. raise ValueError(f"run_id={run_id} 当前状态不可执行: {run.get('status')}")
  16. return run
  17. def render_node_context(
  18. *, user_input: str, full_state: dict[str, Any], round_index: int, plan: str = "",
  19. ) -> str:
  20. return (
  21. f"【当前轮次】\n{round_index}\n\n"
  22. f"【原始任务】\n{user_input}\n\n"
  23. f"【find_agent_v2 数据库状态快照】\n"
  24. f"{json.dumps(full_state, ensure_ascii=False, default=str)}\n\n"
  25. f"【本轮搜索计划】\n{plan}"
  26. )
  27. def build_node_slots(
  28. *, user_input: str, full_state: dict[str, Any], round_index: int, plan: str = "",
  29. ) -> tuple[InputSlot, ...]:
  30. """Stable input structure shared by all node instances and obagent declarations."""
  31. return (
  32. InputSlot("当前轮次", str(round_index), "round_index", "FindAgentV2.begin_round", False),
  33. InputSlot("原始任务", user_input, "task", "find_agent_v2_run.input_json", False),
  34. InputSlot(
  35. "数据库状态快照",
  36. json.dumps(full_state, ensure_ascii=False, default=str),
  37. "state_snapshot",
  38. "FindAgentV2Service.get_full_state",
  39. False,
  40. ),
  41. InputSlot("本轮搜索计划", plan, "round_plan", "planner output"),
  42. )