skill_client.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import httpx
  2. from core_domain import SkillDefinitionContract, SkillRunContract
  3. from core_shared import JSONValue
  4. class SkillServiceClientError(Exception):
  5. pass
  6. class SkillServiceClient:
  7. def __init__(self, base_url: str, timeout_seconds: float = 10.0) -> None:
  8. self.base_url = base_url.rstrip("/")
  9. self.timeout_seconds = timeout_seconds
  10. def list_skills(self) -> list[SkillDefinitionContract]:
  11. try:
  12. with httpx.Client(timeout=self.timeout_seconds) as client:
  13. response = client.get(f"{self.base_url}/skills")
  14. response.raise_for_status()
  15. return [
  16. SkillDefinitionContract.model_validate(item)
  17. for item in response.json()
  18. ]
  19. except httpx.HTTPError as exc:
  20. raise SkillServiceClientError(f"skill-service list failed: {exc}") from exc
  21. def create_skill_run(
  22. self,
  23. *,
  24. skill_id: str,
  25. installation_id: str | None,
  26. input_json: dict[str, JSONValue]) -> SkillRunContract:
  27. payload: dict[str, JSONValue] = {
  28. "skill_id": skill_id,
  29. "input_json": input_json,
  30. }
  31. if installation_id is not None:
  32. payload["installation_id"] = installation_id
  33. try:
  34. with httpx.Client(timeout=self.timeout_seconds) as client:
  35. response = client.post(f"{self.base_url}/skills/runs", json=payload)
  36. response.raise_for_status()
  37. return SkillRunContract.model_validate(response.json())
  38. except httpx.HTTPError as exc:
  39. raise SkillServiceClientError(f"skill-service create run failed: {exc}") from exc
  40. def execute_skill_run(
  41. self,
  42. *,
  43. skill_run_id: str,
  44. worker_key: str | None) -> SkillRunContract:
  45. payload: dict[str, JSONValue] = {}
  46. if worker_key is not None:
  47. payload["worker_key"] = worker_key
  48. try:
  49. with httpx.Client(timeout=self.timeout_seconds) as client:
  50. response = client.post(
  51. f"{self.base_url}/skills/runs/{skill_run_id}/execute",
  52. json=payload)
  53. response.raise_for_status()
  54. return SkillRunContract.model_validate(response.json())
  55. except httpx.HTTPError as exc:
  56. raise SkillServiceClientError(f"skill-service execute run failed: {exc}") from exc