runner.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """Public run creation and sync/async entry points."""
  2. from __future__ import annotations
  3. import asyncio
  4. from find_agent_v2.agent import FindAgentV2, create_find_agent_v2
  5. from find_agent_v2.service import get_find_agent_v2_service
  6. from find_agent_v2.state import FindAgentResult
  7. from supply_agent.config import Settings
  8. def create_find_agent_v2_run(
  9. *,
  10. user_input: str,
  11. demand_word: str,
  12. demand_grade_id: int | None = None,
  13. run_id: str | None = None,
  14. rule_config: dict | None = None,
  15. ) -> str:
  16. """Create a run in the new table namespace; old run IDs are never reused implicitly."""
  17. return get_find_agent_v2_service().create_run(
  18. user_input=user_input,
  19. demand_word=demand_word,
  20. demand_grade_id=demand_grade_id,
  21. run_id=run_id,
  22. rule_config=rule_config,
  23. )
  24. async def arun_find_agent_v2(
  25. user_input: str,
  26. *,
  27. run_id: str,
  28. agent: FindAgentV2 | None = None,
  29. settings: Settings | None = None,
  30. model: str | None = None,
  31. resume: bool = False,
  32. ) -> FindAgentResult:
  33. runner = agent or create_find_agent_v2(settings=settings, model=model)
  34. return await runner.arun(run_id=run_id, user_input=user_input, resume=resume)
  35. def run_find_agent_v2(
  36. user_input: str,
  37. *,
  38. run_id: str,
  39. agent: FindAgentV2 | None = None,
  40. settings: Settings | None = None,
  41. model: str | None = None,
  42. resume: bool = False,
  43. ) -> FindAgentResult:
  44. try:
  45. asyncio.get_running_loop()
  46. except RuntimeError:
  47. return asyncio.run(arun_find_agent_v2(
  48. user_input,
  49. run_id=run_id,
  50. agent=agent,
  51. settings=settings,
  52. model=model,
  53. resume=resume,
  54. ))
  55. raise RuntimeError("run_find_agent_v2 不能在已运行的事件循环内调用;请使用 arun_find_agent_v2")
  56. def run_prepared_find_agent_v2(
  57. run_id: str,
  58. *,
  59. agent: FindAgentV2 | None = None,
  60. settings: Settings | None = None,
  61. model: str | None = None,
  62. resume: bool = False,
  63. ) -> FindAgentResult:
  64. """Execute a prepared run using its database-persisted immutable user input."""
  65. user_input = get_find_agent_v2_service().get_run_user_input(run_id)
  66. return run_find_agent_v2(
  67. user_input,
  68. run_id=run_id,
  69. agent=agent,
  70. settings=settings,
  71. model=model,
  72. resume=resume,
  73. )