tools_cache.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from __future__ import annotations
  2. from dataclasses import dataclass, replace
  3. import base64
  4. import binascii
  5. import hashlib
  6. import json
  7. import time
  8. from typing import Callable, Literal, Protocol
  9. from pydantic import ValidationError
  10. from hermes_constants import get_hermes_home
  11. from tools.connectors.gateway.errors import GatewayAuthError, GatewayUnavailable, ToolGatewayError
  12. from tools.connectors.portal.client import NotModified, validate_slug
  13. from tools.connectors.portal.errors import PortalToolsUnavailable
  14. from tools.connectors.portal.wire import ConnectorTool, ConnectorToolsListing
  15. from utils import atomic_json_write, read_json_or_empty
  16. class ToolsClient(Protocol):
  17. def origin(self) -> str: ...
  18. def authorization_token(self) -> str | None: ...
  19. def tools(self, slug: str, *, if_none_match: str | None = None) -> ConnectorToolsListing | NotModified: ...
  20. TTL_SECONDS = 24 * 60 * 60
  21. @dataclass(frozen=True)
  22. class ToolsRead:
  23. connector: str
  24. toolkit_version: str
  25. etag: str
  26. fetched_at: float
  27. source: Literal["cache", "network", "revalidated"]
  28. stale: bool
  29. tools: list[ConnectorTool]
  30. def _member_key(token: str | None) -> str | None:
  31. if not isinstance(token, str):
  32. return None
  33. parts = token.split(".")
  34. if len(parts) != 3:
  35. return None
  36. try:
  37. payload = parts[1] + "=" * (-len(parts[1]) % 4)
  38. claims = json.loads(base64.urlsafe_b64decode(payload))
  39. except (binascii.Error, ValueError, UnicodeDecodeError, json.JSONDecodeError):
  40. return None
  41. subject = claims.get("sub") if isinstance(claims, dict) else None
  42. if not isinstance(subject, str) or not subject:
  43. return None
  44. return hashlib.sha256(subject.encode("utf-8")).hexdigest()[:16]
  45. def _cache_path(origin: str, member_key: str, slug: str):
  46. origin_key = hashlib.sha256(origin.encode("utf-8")).hexdigest()[:16]
  47. return get_hermes_home() / "cache" / "connectors" / origin_key / member_key / f"{slug}.json"
  48. def _cached(path) -> ToolsRead | None:
  49. raw = read_json_or_empty(path)
  50. try:
  51. tools = [ConnectorTool.model_validate(tool) for tool in raw["tools"]]
  52. return ToolsRead(
  53. connector=str(raw["connector"]),
  54. toolkit_version=str(raw["toolkit_version"]),
  55. etag=str(raw["etag"]),
  56. fetched_at=float(raw["fetched_at"]),
  57. source="cache",
  58. stale=False,
  59. tools=tools,
  60. )
  61. except (KeyError, TypeError, ValueError, ValidationError):
  62. return None
  63. def _store(path, entry: ToolsRead) -> ToolsRead:
  64. path.parent.mkdir(parents=True, exist_ok=True)
  65. atomic_json_write(
  66. path,
  67. {
  68. "etag": entry.etag,
  69. "toolkit_version": entry.toolkit_version,
  70. "connector": entry.connector,
  71. "tools": [tool.model_dump(by_alias=True) for tool in entry.tools],
  72. "fetched_at": entry.fetched_at,
  73. },
  74. )
  75. return entry
  76. def read_tools(
  77. slug: str,
  78. *,
  79. client: ToolsClient,
  80. refresh: bool = False,
  81. now: Callable[[], float] = time.time,
  82. ) -> ToolsRead:
  83. validate_slug(slug)
  84. member_key = _member_key(client.authorization_token())
  85. if member_key is None:
  86. listing = client.tools(slug)
  87. if isinstance(listing, NotModified):
  88. raise PortalToolsUnavailable("portal tools unavailable", code="INVALID_RESPONSE")
  89. return ToolsRead(
  90. connector=listing.connector,
  91. toolkit_version=listing.toolkit_version,
  92. etag=listing.etag,
  93. fetched_at=now(),
  94. source="network",
  95. stale=False,
  96. tools=listing.tools,
  97. )
  98. path = _cache_path(client.origin(), member_key, slug)
  99. cached = _cached(path)
  100. fetched_at = now()
  101. age = fetched_at - cached.fetched_at if cached is not None else None
  102. if cached is not None and not refresh and age is not None and 0 <= age < TTL_SECONDS:
  103. return cached
  104. try:
  105. listing = client.tools(slug, if_none_match=cached.etag if cached is not None else None)
  106. except GatewayUnavailable:
  107. path.unlink(missing_ok=True)
  108. raise
  109. except GatewayAuthError:
  110. raise
  111. except (PortalToolsUnavailable, ToolGatewayError):
  112. if cached is None:
  113. raise
  114. return replace(cached, stale=True)
  115. if isinstance(listing, NotModified):
  116. if cached is None:
  117. raise PortalToolsUnavailable("portal tools unavailable", code="INVALID_RESPONSE")
  118. return _store(path, replace(cached, fetched_at=fetched_at, source="revalidated"))
  119. return _store(path, ToolsRead(
  120. connector=listing.connector,
  121. toolkit_version=listing.toolkit_version,
  122. etag=listing.etag,
  123. fetched_at=fetched_at,
  124. source="network",
  125. stale=False,
  126. tools=listing.tools,
  127. ))