skill_client.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, *, tenant_id: str) -> list[SkillDefinitionContract]:
  11. try:
  12. with httpx.Client(timeout=self.timeout_seconds) as client:
  13. response = client.get(
  14. f"{self.base_url}/skills",
  15. params={"tenant_id": tenant_id},
  16. )
  17. response.raise_for_status()
  18. return [
  19. SkillDefinitionContract.model_validate(item)
  20. for item in response.json()
  21. ]
  22. except httpx.HTTPError as exc:
  23. raise SkillServiceClientError(f"skill-service list failed: {exc}") from exc
  24. def create_skill_run(
  25. self,
  26. *,
  27. tenant_id: str,
  28. skill_id: str,
  29. skill_version_id: str | None,
  30. installation_id: str | None,
  31. input_json: dict[str, JSONValue],
  32. ) -> SkillRunContract:
  33. payload: dict[str, JSONValue] = {
  34. "tenant_id": tenant_id,
  35. "skill_id": skill_id,
  36. "input_json": input_json,
  37. }
  38. if skill_version_id is not None:
  39. payload["skill_version_id"] = skill_version_id
  40. if installation_id is not None:
  41. payload["installation_id"] = installation_id
  42. try:
  43. with httpx.Client(timeout=self.timeout_seconds) as client:
  44. response = client.post(f"{self.base_url}/skills/runs", json=payload)
  45. response.raise_for_status()
  46. return SkillRunContract.model_validate(response.json())
  47. except httpx.HTTPError as exc:
  48. raise SkillServiceClientError(f"skill-service create run failed: {exc}") from exc
  49. def execute_skill_run(
  50. self,
  51. *,
  52. tenant_id: str,
  53. skill_run_id: str,
  54. worker_key: str | None,
  55. ) -> SkillRunContract:
  56. payload: dict[str, JSONValue] = {"tenant_id": tenant_id}
  57. if worker_key is not None:
  58. payload["worker_key"] = worker_key
  59. try:
  60. with httpx.Client(timeout=self.timeout_seconds) as client:
  61. response = client.post(
  62. f"{self.base_url}/skills/runs/{skill_run_id}/execute",
  63. json=payload,
  64. )
  65. response.raise_for_status()
  66. return SkillRunContract.model_validate(response.json())
  67. except httpx.HTTPError as exc:
  68. raise SkillServiceClientError(f"skill-service execute run failed: {exc}") from exc