agent_client.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import httpx
  2. from core_domain import AgentRunContract
  3. from core_shared import JSONValue
  4. class AgentServiceClientError(Exception):
  5. pass
  6. class AgentServiceClient:
  7. def __init__(self, base_url: str, timeout_seconds: float = 30.0) -> None:
  8. self.base_url = base_url.rstrip("/")
  9. self.timeout_seconds = timeout_seconds
  10. def create_agent_run(
  11. self,
  12. *,
  13. agent_id: str,
  14. agent_config_id: str | None,
  15. session_id: str | None,
  16. input_text: str | None,
  17. input_json: dict[str, JSONValue] | None) -> AgentRunContract:
  18. payload: dict[str, JSONValue] = {
  19. "agent_id": agent_id,
  20. }
  21. if agent_config_id is not None:
  22. payload["agent_config_id"] = agent_config_id
  23. if session_id is not None:
  24. payload["session_id"] = session_id
  25. if input_text is not None:
  26. payload["input_text"] = input_text
  27. if input_json is not None:
  28. payload["input_json"] = input_json
  29. try:
  30. with httpx.Client(timeout=self.timeout_seconds) as client:
  31. response = client.post(f"{self.base_url}/agents/runs", json=payload)
  32. response.raise_for_status()
  33. return AgentRunContract.model_validate(response.json())
  34. except httpx.HTTPStatusError as exc:
  35. detail = exc.response.text[:500]
  36. raise AgentServiceClientError(
  37. f"agent-service create run failed: {exc.response.status_code} {detail}") from exc
  38. except httpx.HTTPError as exc:
  39. raise AgentServiceClientError(f"agent-service create run failed: {exc}") from exc
  40. def get_agent_run(
  41. self,
  42. *,
  43. agent_run_id: str) -> AgentRunContract:
  44. try:
  45. with httpx.Client(timeout=self.timeout_seconds) as client:
  46. response = client.post(
  47. f"{self.base_url}/agents/runs/detail",
  48. json={"agent_run_id": agent_run_id})
  49. response.raise_for_status()
  50. return AgentRunContract.model_validate(response.json())
  51. except httpx.HTTPStatusError as exc:
  52. detail = exc.response.text[:500]
  53. raise AgentServiceClientError(
  54. f"agent-service get run failed: {exc.response.status_code} {detail}") from exc
  55. except httpx.HTTPError as exc:
  56. raise AgentServiceClientError(f"agent-service get run failed: {exc}") from exc
  57. def execute_agent_run(
  58. self,
  59. *,
  60. agent_run_id: str,
  61. worker_key: str | None,
  62. dry_run: bool) -> AgentRunContract:
  63. payload: dict[str, JSONValue] = {
  64. "dry_run": dry_run,
  65. }
  66. if worker_key is not None:
  67. payload["worker_key"] = worker_key
  68. try:
  69. with httpx.Client(timeout=self.timeout_seconds) as client:
  70. response = client.post(
  71. f"{self.base_url}/agents/runs/{agent_run_id}/execute",
  72. json=payload)
  73. response.raise_for_status()
  74. response_payload = response.json()
  75. run_payload = response_payload.get("run")
  76. if not isinstance(run_payload, dict):
  77. raise AgentServiceClientError("agent-service execute response missing run")
  78. return AgentRunContract.model_validate(run_payload)
  79. except httpx.TimeoutException as exc:
  80. try:
  81. recovered_run = self.get_agent_run(agent_run_id=agent_run_id)
  82. except AgentServiceClientError as recovery_exc:
  83. raise AgentServiceClientError(
  84. f"agent-service execute run timed out and recovery failed: {recovery_exc}") from exc
  85. if recovered_run.status in {"completed", "failed", "cancelled"}:
  86. return recovered_run
  87. raise AgentServiceClientError(
  88. f"agent-service execute run timed out; recovered status={recovered_run.status}") from exc
  89. except httpx.HTTPStatusError as exc:
  90. detail = exc.response.text[:500]
  91. raise AgentServiceClientError(
  92. f"agent-service execute run failed: {exc.response.status_code} {detail}") from exc
  93. except httpx.HTTPError as exc:
  94. raise AgentServiceClientError(f"agent-service execute run failed: {exc}") from exc