__init__.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Actual Computer provider profile."""
  2. import os
  3. import sys
  4. from typing import Any
  5. from urllib.parse import urlparse
  6. from providers import register_provider
  7. from providers.base import ProviderProfile
  8. DEFAULT_ACTUAL_BASE_URL = "https://api.actual.inc/v1"
  9. class ActualProfile(ProviderProfile):
  10. """Actual Computer: hosted at api.actual.inc; local (offline-mode client)
  11. inference opted into via model.base_url in config.yaml."""
  12. def build_client_kwargs_extras(self, **context: Any) -> dict[str, Any]:
  13. base_url = str(context.get("base_url") or self.base_url or "")
  14. try:
  15. hostname = (urlparse(base_url).hostname or "").lower().rstrip(".")
  16. except ValueError:
  17. return {}
  18. if sys.platform != "darwin" or hostname != "api.actual.inc":
  19. return {}
  20. if any(
  21. os.getenv(key)
  22. for key in (
  23. "HERMES_CA_BUNDLE",
  24. "SSL_CERT_FILE",
  25. "REQUESTS_CA_BUNDLE",
  26. "CURL_CA_BUNDLE",
  27. )
  28. ):
  29. return {}
  30. from agent.ssl_verify import certifi_bundle_path
  31. # Re-derived when an install-environment rotation deleted the directory certifi was
  32. # imported from: a deleted path would otherwise be handed to the client as ssl_ca_cert.
  33. bundle = certifi_bundle_path()
  34. return {"ssl_ca_cert": bundle} if bundle else {}
  35. def supported_reasoning_efforts(self, model: str | None) -> tuple[str, ...] | None:
  36. from agent.reasoning_effort import ACTUAL_RELAY_EFFORTS
  37. return ACTUAL_RELAY_EFFORTS
  38. def build_api_kwargs_extras(
  39. self, *, reasoning_config: dict | None = None, **context: Any
  40. ) -> tuple[dict[str, Any], dict[str, Any]]:
  41. if not isinstance(reasoning_config, dict):
  42. return {}, {}
  43. from agent.reasoning_effort import thinking_toggle_extras
  44. # The relay accepts ``none`` as a real effort level: it switches thinking off AND
  45. # is echoed as reasoning_effort, unlike the Moonshot/DeepSeek wires.
  46. effort_none = str(reasoning_config.get("effort") or "").strip().lower() == "none"
  47. if reasoning_config.get("enabled") is not False and effort_none:
  48. return {"thinking": {"type": "disabled"}}, {"reasoning_effort": "none"}
  49. supported = self.supported_reasoning_efforts(context.get("model")) or ()
  50. return thinking_toggle_extras(reasoning_config, supported, always_emit_toggle=True)
  51. def fetch_models(
  52. self,
  53. *,
  54. api_key: str | None = None,
  55. base_url: str | None = None,
  56. timeout: float = 8.0,
  57. ) -> list[str] | None:
  58. """Use the selected route, then config.yaml, then the legacy environment override."""
  59. from hermes_cli.auth import (
  60. normalize_actual_base_url,
  61. resolve_api_key_provider_credentials,
  62. )
  63. base_url = normalize_actual_base_url(
  64. base_url or resolve_api_key_provider_credentials("actual")["base_url"]
  65. )
  66. return super().fetch_models(api_key=api_key, base_url=base_url, timeout=timeout)
  67. actual = ActualProfile(
  68. name="actual",
  69. aliases=("actual-computer", "actualcomputer", "aci"),
  70. display_name="Actual Computer",
  71. description="Actual Computer - hosted inference via api.actual.inc, or local "
  72. "offline inference via model.base_url in config.yaml",
  73. signup_url="https://actual.inc",
  74. env_vars=("ACTUAL_API_KEY",),
  75. base_url=DEFAULT_ACTUAL_BASE_URL,
  76. auth_type="api_key",
  77. api_mode="chat_completions",
  78. )
  79. register_provider(actual)