skill_client.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. skill_version_id: str | None,
  26. installation_id: str | None,
  27. input_json: dict[str, JSONValue]) -> SkillRunContract:
  28. payload: dict[str, JSONValue] = {
  29. "skill_id": skill_id,
  30. "input_json": input_json,
  31. }
  32. if skill_version_id is not None:
  33. payload["skill_version_id"] = skill_version_id
  34. if installation_id is not None:
  35. payload["installation_id"] = installation_id
  36. try:
  37. with httpx.Client(timeout=self.timeout_seconds) as client:
  38. response = client.post(f"{self.base_url}/skills/runs", json=payload)
  39. response.raise_for_status()
  40. return SkillRunContract.model_validate(response.json())
  41. except httpx.HTTPError as exc:
  42. raise SkillServiceClientError(f"skill-service create run failed: {exc}") from exc
  43. def execute_skill_run(
  44. self,
  45. *,
  46. skill_run_id: str,
  47. worker_key: str | None) -> SkillRunContract:
  48. payload: dict[str, JSONValue] = {}
  49. if worker_key is not None:
  50. payload["worker_key"] = worker_key
  51. try:
  52. with httpx.Client(timeout=self.timeout_seconds) as client:
  53. response = client.post(
  54. f"{self.base_url}/skills/runs/{skill_run_id}/execute",
  55. json=payload)
  56. response.raise_for_status()
  57. return SkillRunContract.model_validate(response.json())
  58. except httpx.HTTPError as exc:
  59. raise SkillServiceClientError(f"skill-service execute run failed: {exc}") from exc