model_metadata_http.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. """Model probes share chat's TLS policy, with scoped redirect credentials."""
  2. from __future__ import annotations
  3. from contextlib import contextmanager
  4. # Wall-clock cap on one probe (headers + whole body), on top of the per-op httpx timeouts.
  5. TOTAL_DEADLINE_S = 15.0
  6. def resolve_verify(base_url: str = ""):
  7. from agent.ssl_verify import resolve_httpx_verify
  8. settings = {}
  9. if base_url:
  10. try:
  11. from hermes_cli.config import get_custom_provider_tls_settings
  12. settings = get_custom_provider_tls_settings(base_url)
  13. except Exception:
  14. pass # Metadata remains optional when config discovery fails.
  15. return resolve_httpx_verify(
  16. ca_bundle=settings.get("ssl_ca_cert"),
  17. ssl_verify=settings.get("ssl_verify"),
  18. base_url=base_url,
  19. )
  20. @contextmanager
  21. def stream(url: str, *, headers=None, params=None, timeout=10.0, verify=None):
  22. import httpx
  23. from agent.deadline import DeadlineExpired, run_bounded_sync
  24. from hermes_cli.urllib_security import url_origin
  25. origin = url_origin(url)
  26. private_headers = {name.lower() for name in headers or {} if name.lower() not in {"accept", "user-agent"}}
  27. private_headers.update({"authorization", "cookie", "proxy-authorization"})
  28. def scope_credentials(request):
  29. if url_origin(str(request.url)) != origin:
  30. for name in private_headers:
  31. request.headers.pop(name, None)
  32. if isinstance(timeout, tuple):
  33. connect, read = timeout
  34. timeout = httpx.Timeout(read, connect=connect)
  35. with httpx.Client(
  36. verify=resolve_verify() if verify is None else verify,
  37. timeout=timeout,
  38. follow_redirects=True,
  39. event_hooks={"request": [scope_credentials]},
  40. ) as client:
  41. def fetch():
  42. response = client.send(client.build_request("GET", url, headers=headers, params=params), stream=True)
  43. if response.is_success: # a rejection closes on its headers, never waits for a body
  44. response.read()
  45. return response
  46. # Probes run inside requests (session.create resolves the context window through them), and
  47. # httpx timeouts bound each socket op, not the exchange: a body trickling in under the read
  48. # timeout, or a hung DNS lookup, would hold the caller for as long as it lasts. Leaving the
  49. # client block on expiry closes the pool under the abandoned worker.
  50. bounded = run_bounded_sync(fetch, TOTAL_DEADLINE_S, label=f"model metadata {origin}")
  51. if bounded.timed_out:
  52. raise DeadlineExpired(f"model metadata {origin}", TOTAL_DEADLINE_S)
  53. try:
  54. yield bounded.value
  55. finally:
  56. bounded.value.close()
  57. def get(url: str, *, headers=None, params=None, timeout=10.0, verify=None):
  58. with stream(url, headers=headers, params=params, timeout=timeout, verify=verify) as response:
  59. return response