Skip to content

The agent that grows with you

fix(models): persist curated OpenRouter catalog to disk so picker opens fast

Hyperion4c003069f329
diff --git a/hermes_cli/models.py b/hermes_cli/models.py
index cbc288a69d1..89cc7c7ce13 100644
--- a/hermes_cli/models.py
+++ b/hermes_cli/models.py
@@ -146,6 +146,52 @@ def _custom_provider_ssl_context(base_url: str):
# Process-lifetime picker lists refreshed from the live catalogs (see fetch_*_models).
_openrouter_catalog_cache: list[tuple[str, str]] | None = None
+
+# On-disk TTL for the curated OpenRouter picker catalog. The in-memory
+# ``_openrouter_catalog_cache`` is per-process, so without a disk cache every
+# cold picker open re-downloads the full ~686KB /api/v1/models catalog
+# (~1.4-7s). Persisting the *curated* result (post-filter) lets fresh
+# processes serve the picker from disk within the TTL instead.
+_OPENROUTER_CATALOG_DISK_TTL = 3600.0 # 1h, matches provider-models cache
+
+
+def _openrouter_catalog_disk_path() -> "Path":
+ """Disk path for the persisted curated OpenRouter catalog."""
+ from hermes_constants import get_hermes_home
+ return get_hermes_home() / "cache" / "openrouter_curated_catalog.json"
+
+
+def _read_openrouter_catalog_disk() -> list[tuple[str, str]] | None:
+ """Return the last-known-good curated catalog from disk if fresh, else None."""
+ try:
+ path = _openrouter_catalog_disk_path()
+ with open(path, encoding="utf-8") as fh:
+ obj = json.load(fh)
+ if time.time() - float(obj.get("fetched_at", 0)) > _OPENROUTER_CATALOG_DISK_TTL:
+ return None
+ items = obj.get("curated")
+ if not isinstance(items, list):
+ return None
+ out: list[tuple[str, str]] = []
+ for it in items:
+ if isinstance(it, (list, tuple)) and len(it) == 2:
+ out.append((str(it[0]), str(it[1])))
+ return out or None
+ except Exception:
+ return None
+
+
+def _write_openrouter_catalog_disk(curated: list[tuple[str, str]]) -> None:
+ """Persist the curated catalog so the next cold picker open is instant."""
+ try:
+ path = _openrouter_catalog_disk_path()
+ path.parent.mkdir(parents=True, exist_ok=True)
+ tmp = path.with_suffix(".tmp")
+ with open(tmp, "w", encoding="utf-8") as fh:
+ json.dump({"fetched_at": time.time(), "curated": [list(c) for c in curated]}, fh)
+ os.replace(tmp, path)
+ except Exception as exc:
+ logger.debug("openrouter curated catalog disk write failed: %s", exc)
_ai_gateway_catalog_cache: list[tuple[str, str]] | None = None
@@ -473,6 +519,14 @@ def fetch_openrouter_models(
if _openrouter_catalog_cache is not None and not force_refresh:
return list(_openrouter_catalog_cache)
+ # Cold process: serve from the persisted disk cache when fresh so the
+ # picker doesn't re-download the full ~686KB catalog on every open.
+ if not force_refresh:
+ disk = _read_openrouter_catalog_disk()
+ if disk:
+ _openrouter_catalog_cache = disk
+ return list(disk)
+
# Remote catalog manifest first, in-repo snapshot when unreachable; the live /v1/models filter
# (tool support, free pricing) is applied on top either way.
try:
@@ -514,6 +568,7 @@ def fetch_openrouter_models(
if not curated[0][1]:
curated[0] = (curated[0][0], "recommended")
_openrouter_catalog_cache = curated
+ _write_openrouter_catalog_disk(curated)
return list(curated)