capabilities.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """Capability answers for models served by the managed runtime.
  2. Capability lookups (vision, and whatever comes next) consult cloud-shaped catalogs that have never
  3. heard of a local GGUF, so a vision-capable local model reads as text-only and images detour to an
  4. auxiliary cloud model — the wrong behavior twice over for a local-first user (broken feature, and a
  5. screenshot silently leaving the machine).
  6. """
  7. from __future__ import annotations
  8. from contextlib import suppress
  9. import logging
  10. logger = logging.getLogger(__name__)
  11. from hermes_cli.local_runtime.endpoint import LLAMACPP_ALIASES as _LLAMACPP_ALIASES
  12. # Image formats the managed server's decoder actually handles. llama.cpp decodes with stb_image:
  13. # PNG/JPEG/GIF/BMP yes, WebP NO — and a WebP part fails SILENTLY (no HTTP error, no log line; the
  14. # model just never sees an image and confabulates). Anything outside this set must be transcoded
  15. # before the request. Measured live: the same red square answered 'Red' as PNG, 'Unseen' as WebP.
  16. ACCEPTED_IMAGE_MIMES = frozenset({"image/png", "image/jpeg"})
  17. def is_managed_provider(provider: str, base_url: str = "") -> bool:
  18. """True when this provider/base_url pair points at the managed server. ``custom`` only counts
  19. when the base_url IS the managed endpoint — never claim someone else's custom server."""
  20. p = (provider or "").strip().lower()
  21. if p in _LLAMACPP_ALIASES:
  22. return True
  23. if p != "custom" or not base_url:
  24. return False
  25. with suppress(Exception):
  26. from hermes_cli.local_runtime.growth import is_managed_endpoint
  27. return is_managed_endpoint(base_url)
  28. return False
  29. def _props_modalities(model_id: str) -> bool | None:
  30. """Ask the running server whether this loaded child sees images. None when the server is down,
  31. the model isn't loaded, or the build doesn't report modalities."""
  32. with suppress(Exception):
  33. from hermes_cli.local_runtime.endpoint import child_query, managed_get_json, managed_root
  34. ep = managed_root()
  35. if ep is None:
  36. return None
  37. modalities = managed_get_json(*ep, f"/props?{child_query(model_id)}", timeout_s=3).get("modalities")
  38. if isinstance(modalities, dict) and "vision" in modalities:
  39. return bool(modalities["vision"])
  40. return None
  41. def managed_model_supports_vision(model_id: str) -> bool | None:
  42. """Ground-truth vision capability for a staged model, or None when the model isn't ours /
  43. nothing is known (caller keeps falling through)."""
  44. if not model_id:
  45. return None
  46. with suppress(Exception):
  47. from hermes_cli.local_runtime.bootstrap import assets_dir, staged_model_ids
  48. from hermes_cli.local_runtime.catalog import entry_for_model
  49. # Only answer for models actually staged with us.
  50. if model_id not in staged_model_ids():
  51. return None
  52. live = _props_modalities(model_id)
  53. if live is not None:
  54. return live
  55. # Staged but not loaded (or an older server build): the catalog knows whether this model
  56. # ships a vision projector. Capability requires the projector to actually be on disk — a
  57. # model downloaded before its mmproj (partial delete, old layout) genuinely cannot see.
  58. entry = entry_for_model(model_id)
  59. if entry is None:
  60. return None
  61. return (entry.mmproj is not None
  62. and (assets_dir() / entry.mmproj.local_name).exists())
  63. return None