Skip to content

The agent that grows with you

refactor(web): collapse local_models job/download/server boilerplate into shared helpers

Teknium8c5af5733a9f
diff --git a/hermes_cli/web_routers/local_models.py b/hermes_cli/web_routers/local_models.py
index 465cc8f9e32..4dc768a6563 100644
--- a/hermes_cli/web_routers/local_models.py
+++ b/hermes_cli/web_routers/local_models.py
@@ -1,33 +1,52 @@
"""Local-models dashboard routes — the desktop's window into the managed
llama.cpp runtime.
-Everything here is designed for a first-run user on an RTX laptop: every
-payload carries plain-language, pre-formatted facts the UI can show verbatim
-(what will this model do ON THIS MACHINE, how big is the download, what is
-the runtime doing right now), never raw internals the renderer would have to
-interpret.
+Every payload carries plain-language, pre-formatted facts the UI can show
+verbatim (what will this model do ON THIS MACHINE, how big is the download,
+what is the runtime doing right now), never raw internals.
Long jobs (runtime install, model download) follow the repo's job pattern:
start-POST -> {job_id} -> GET poll with byte progress. Downloads are
-byte-size checked against the catalog (no hash verification by design);
-a short download deletes the file and reports it plainly.
+byte-size checked against what the server declared (no hash verification by
+design); a short download deletes the file and reports it plainly.
"""
from __future__ import annotations
+import asyncio
import json
import logging
+import os
+import re
+import shutil
+import subprocess
import threading
import time
import urllib.parse
import urllib.request
import uuid
from pathlib import Path
-from typing import Any, Dict, Optional
+from typing import Any, Callable, Dict, Optional
from fastapi import APIRouter, HTTPException
from pydantic import BaseModel
+from starlette.concurrency import run_in_threadpool
+
+from hermes_cli import config as config_mod, web_deps
+from hermes_cli.local_runtime import (
+ binaries,
+ bootstrap,
+ catalog,
+ context_policy,
+ estimator,
+ growth,
+ hardware,
+ hf_browse,
+ load_progress,
+ presets,
+ supervisor,
+)
from hermes_cli.local_runtime.endpoint import _state_endpoint
logger = logging.getLogger(__name__)
@@ -37,6 +56,7 @@ router = APIRouter()
_GIB = 1 << 30
_JOBS: Dict[str, Dict[str, Any]] = {}
_JOBS_LOCK = threading.Lock()
+_LLAMACPP_PROVIDERS = ("llamacpp", "llama.cpp", "llama-cpp")
def _human_gb(n: int | float) -> str:
@@ -46,7 +66,7 @@ def _human_gb(n: int | float) -> str:
def _job(kind: str, target: str, model_id: str | None = None) -> Dict[str, Any]:
job = {
"job_id": uuid.uuid4().hex[:12],
- "kind": kind, # "runtime-install" | "model-download"
+ "kind": kind, # "runtime-install" | "model-download" | ...
"target": target,
"model_id": model_id, # catalog id for downloads; None otherwise
"status": "running", # running | done | error
@@ -62,6 +82,67 @@ def _job(kind: str, target: str, model_id: str | None = None) -> Dict[str, Any]:
return job
+def _job_view(job: Dict[str, Any]) -> Dict[str, Any]:
+ out = dict(job)
+ if out["total_bytes"]:
+ out["percent"] = min(100, round(out["done_bytes"] / out["total_bytes"] * 100))
+ return out
+
+
+def _finish(job: Dict[str, Any], detail: str) -> None:
+ job["phase"] = "done"
+ job["status"] = "done"
+ job["detail"] = detail
+
+
+def _spawn_job(job: Dict[str, Any], name: str, body: Callable[[], None], *,
+ fail_msg: str | None = None,
+ on_exit: Callable[[], None] | None = None) -> None:
+ """Run ``body`` on a daemon thread; any exception marks the job errored
+ (optionally warning ``fail_msg`` with the exception). ``on_exit`` always
+ runs last (lock release)."""
+ def _run():
+ try:
+ body()
+ except Exception as exc: # noqa: BLE001
+ if fail_msg:
+ logger.warning(fail_msg, exc)
+ job["status"] = "error"
+ job["error"] = str(exc)
+ finally:
+ if on_exit is not None:
+ on_exit()
+
+ threading.Thread(target=_run, daemon=True, name=name).start()
+
+
+def _refresh_runtime(skip_msg: str) -> None:
+ """Bounce a running router so it rescans the models dir; the router only
+ scans at spawn, so a new/deleted file is invisible until then. Never
+ raises — the file operation already succeeded."""
+ try:
+
+ bootstrap.refresh_local_runtime()
+ except Exception: # noqa: BLE001
+ logger.debug(skip_msg, exc_info=True)
+
+
+def _router_request(endpoint: Dict[str, Any], path: str, *, timeout: float,
+ payload: dict | None = None) -> Any:
+ """Call the local router (base_url minus ``/v1``) with its bearer key.
+ GET (no payload) returns the parsed JSON body; POST returns None."""
+ headers = {"Authorization": f"Bearer {endpoint.get('api_key', '')}"}
+ data = None
+ if payload is not None:
+ headers["Content-Type"] = "application/json"
+ data = json.dumps(payload).encode()
+ req = urllib.request.Request(
+ endpoint["base_url"].rsplit("/v1", 1)[0] + path, data=data, headers=headers,
+ method="POST" if payload is not None else None)
+ with urllib.request.urlopen(req, timeout=timeout) as r:
+ return None if payload is not None else json.loads(r.read())
+
+
# ── fast download: ranged parallel streams ───────────────────
# One TCP stream to a CDN rarely fills a fast line; 8 ranged connections
@@ -73,10 +154,8 @@ _CHUNK = 4 << 20
def _probe_range_support(url: str) -> int:
"""Total size when the server honors Range requests, else 0.
- Auth-shaped failures raise with a plain-language message — a 401/403
- from the CDN means the repo is gated or the catalog entry names a
- wrong repo, and the user deserves better than a bare status code.
- """
+ A 401/403 from the CDN means the repo is gated or the catalog names a
+ wrong repo — raise with a plain-language message, not a bare status."""
req = urllib.request.Request(url, headers={"Range": "bytes=0-0"})
try:
with urllib.request.urlopen(req, timeout=60) as r:
@@ -97,27 +176,19 @@ def _probe_range_support(url: str) -> int:
def _model_id_for(gguf: Path) -> str:
"""Variant model id for a staged file (strips split-part suffixes)."""
- import re
-
return re.sub(r"-\d{5}-of-\d{5}$", "", gguf.stem)
def _variant_files_on_disk(model_id: str) -> "list[Path]":
"""Every local file belonging to a staged model: all split parts plus
its catalog-declared assets (mmproj/draft) when present."""
- from hermes_cli.local_runtime.bootstrap import assets_dir
- from hermes_cli.local_runtime.catalog import find_entry_for_model
- mdir = _models_dir()
- files = [p for p in mdir.glob("*.gguf") if _model_id_for(p) == model_id]
- hit = find_entry_for_model(model_id)
+ files = [p for p in _models_dir().glob("*.gguf") if _model_id_for(p) == model_id]
+ hit = catalog.find_entry_for_model(model_id)
if hit is not None:
- entry, _variant = hit
- for asset in (entry.mmproj, entry.draft):
- if asset is not None:
- p = assets_dir() / asset.local_name
- if p.exists():
- files.append(p)
+ for asset in (hit[0].mmproj, hit[0].draft):
+ if asset is not None and (bootstrap.assets_dir() / asset.local_name).exists():
+ files.append(bootstrap.assets_dir() / asset.local_name)
return files
@@ -127,35 +198,38 @@ def download_file(url: str, dest: Path, job: Dict[str, Any],
"""Download url -> dest with byte progress on ``job``.
Ranged-parallel when the server supports it, single-stream fallback
- otherwise. There is no integrity check against the CATALOG by
- design: catalog sizes may lag an upstream re-upload, and a
- newer file than we know about must download fine. Completeness is
- checked only against what the SERVER declared for this transfer
- (range-probe total / Content-Length) — self-consistent and always
- current — so a dropped connection still errors instead of staging a
- truncated file. Never leaves a .part behind.
+ otherwise. No integrity check against the CATALOG by design: catalog
+ sizes may lag an upstream re-upload, and a newer file must download
+ fine. Completeness is checked only against what the SERVER declared for
+ this transfer (range-probe total / Content-Length), so a dropped
+ connection still errors instead of staging a truncated file. Never
+ leaves a .part behind.
Multi-file variants: ``base_done`` offsets the progress so this file's
bytes accumulate onto the files before it, and ``keep_totals=True``
stops the per-file size from overwriting the variant's total.
"""
- import shutil
- import threading as _threading
-
tmp = dest.with_suffix(".part")
dest.parent.mkdir(parents=True, exist_ok=True)
file_done = [0]
- progress_lock = _threading.Lock()
+ progress_lock = threading.Lock()
def bump(n: int) -> None:
with progress_lock:
file_done[0] += n
job["done_bytes"] = base_done + file_done[0]
+ def pump(r, f) -> None:
+ while True:
+ chunk = r.read(_CHUNK)
+ if not chunk:
+ break
+ f.write(chunk)
+ bump(len(chunk))
+
try:
- # The probe and the preallocation both take real seconds on a
- # 20+ GB file — narrate them, or the pane shows a dead '— of X GB'
- # until the first ranged byte lands.
+ # The probe and the preallocation both take real seconds on a 20+ GB
+ # file — narrate them, or the pane shows a dead '— of X GB'.
job["detail"] = "Connecting"
total = _probe_range_support(url)
if total:
@@ -178,17 +252,12 @@ def download_file(url: str, dest: Path, job: Dict[str, Any],
with urllib.request.urlopen(req, timeout=120) as r, \
open(tmp, "r+b") as f:
f.seek(start)
- while True:
- chunk = r.read(_CHUNK)
- if not chunk:
- break
- f.write(chunk)
- bump(len(chunk))
+ pump(r, f)
except Exception as exc: # noqa: BLE001
errors.append(exc)
- threads = [_threading.Thread(target=fetch_range, args=b, daemon=True,
- name=f"lm-dl-{i}")
+ threads = [threading.Thread(target=fetch_range, args=b, daemon=True,
+ name=f"lm-dl-{i}")
for i, b in enumerate(bounds)]
for t in threads:
t.start()
@@ -200,19 +269,13 @@ def download_file(url: str, dest: Path, job: Dict[str, Any],
raise RuntimeError(
f"download incomplete ({file_done[0]} of {total} bytes)")
else:
- # No range support: single stream, large chunks. Completeness
- # is judged by the server's own Content-Length when it sent
- # one — never by the catalog, which may lag a re-upload.
+ # No range support: single stream. Completeness is judged by the
+ # server's own Content-Length when it sent one — never the catalog.
with urllib.request.urlopen(url, timeout=120) as r, open(tmp, "wb") as f:
length = int(r.headers.get("Content-Length") or 0)
if length and not keep_totals:
job["total_bytes"] = length
- while True:
- chunk = r.read(_CHUNK)
- if not chunk:
- break
- f.write(chunk)
- bump(len(chunk))
+ pump(r, f)
if length and file_done[0] != length:
raise RuntimeError(
f"Download ended at {file_done[0]:,} bytes but the server "
@@ -225,9 +288,38 @@ def download_file(url: str, dest: Path, job: Dict[str, Any],
def _models_dir() -> Path:
- from hermes_cli.local_runtime.bootstrap import models_dir
- return models_dir()
+ return bootstrap.models_dir()
+
+
+def _hf_url(repo: str, path: str) -> str:
+ return f"https://huggingface.co/{repo}/resolve/main/{path}"
+
+
+def _download_plan(entry, variant) -> list:
+ """Everything a variant needs: split parts + mmproj/draft assets, as
+ (url, dest, bytes) tuples."""
+
+ plan = [(_hf_url(entry.repo, a.path), _models_dir() / a.local_name, a.size_bytes)
+ for a in variant.files]
+ plan += [(_hf_url(entry.repo, a.path), bootstrap.assets_dir() / a.local_name, a.size_bytes)
+ for a in (entry.mmproj, entry.draft) if a is not None]
+ return plan
+
+
+def _run_download_plan(job: Dict[str, Any], plan: list, label: str) -> None:
+ """Download every missing file in ``plan``; already-present files count
+ toward progress without a transfer."""
+ total = sum(p[2] for p in plan)
+ job["phase"] = "downloading"
+ job["detail"] = f"{label} — {_human_gb(total)}"
+ done_before = 0
+ for url, dest, size in plan:
+ if not dest.exists():
+ download_file(url, dest, job, base_done=done_before, keep_totals=True)
+ job["phase"] = "downloading"
+ done_before += size
+ job["done_bytes"] = done_before
def _engine_too_old(min_engine: str) -> bool:
@@ -237,9 +329,8 @@ def _engine_too_old(min_engine: str) -> bool:
if not min_engine:
return False
try:
- from hermes_cli.local_runtime.binaries import default_tag, installed_tags
- tags = installed_tags() or [default_tag()]
+ tags = binaries.installed_tags() or [binaries.default_tag()]
newest = max(int(t.lstrip("b")) for t in tags if t.lstrip("b").isdigit())
return newest < int(min_engine.lstrip("b"))
except Exception: # noqa: BLE001
@@ -247,10 +338,9 @@ def _engine_too_old(min_engine: str) -> bool:
def _load_config() -> dict:
- from hermes_cli.config import load_config
try:
- return load_config()
+ return config_mod.load_config()
except Exception: # noqa: BLE001
return {}
@@ -259,46 +349,131 @@ def _runtime_section() -> dict:
return (_load_config() or {}).get("local_runtime") or {}
+def _set_runtime_enabled(enabled: bool) -> dict:
+ """Persist ``local_runtime.enabled`` and return the config written."""
+
+ config = config_mod.load_config()
+ config.setdefault("local_runtime", {})["enabled"] = enabled
+ config_mod.save_config(config)
+ return config
+
+
+def _resolve_backend(section: dict, requested: str | None = None) -> str:
+
+ backend = requested or section.get("backend", "auto")
+ return binaries.select_backend(bootstrap._detect_gpu_vendor()) if backend == "auto" else backend
+
+
+def _eligible_entries():
+ """Catalog entries this engine can activate today (engine-gated ones
+ can't be the recommendation either)."""
+
+ return tuple(e for e in catalog.CATALOG if not _engine_too_old(e.min_engine))
+
+
+def _ensure_server(job: Dict[str, Any], config: dict, model_id: str, *,
+ fail_detail: str, skip_msg: str) -> None:
+ """Start the local server if needed and self-heal a stale router: the
+ model list is spawn-only, so a server started before ``model_id``
+ finished downloading can't serve it — bounce it when it doesn't know
+ the model."""
+
+ job["phase"] = "starting-server"
+ job["detail"] = "Starting the local server"
+ sup = bootstrap.ensure_local_runtime(config, force=True)
+ if sup is None and _state_endpoint() is None:
+ raise RuntimeError(fail_detail)
+ if sup is not None:
+ try:
+ if model_id not in sup.models():
+ job["detail"] = "Refreshing the local server"
+ bootstrap.refresh_local_runtime()
+ except Exception: # noqa: BLE001
+ logger.debug(skip_msg, exc_info=True)
+
+
+def _assign_default(job: Dict[str, Any], model_id: str) -> None:
+ """Make ``model_id`` the main model through the same machinery as
+ /api/model/set (late-bound so tests can stub web_deps.late)."""
+
+ job["phase"] = "setting-default"
+ job["detail"] = "Making it your default"
+ web_deps.late("_apply_model_assignment_sync")("main", "llamacpp", model_id, "", "", "")
+
+
# ── status: the one call the pane opens with ─────────────────
+def _loaded_models(running: Dict[str, Any]) -> "tuple[Dict[str, str], Dict[str, Any]]":
+ """Which staged models are resident right now, plus how each is placed.
+
+ Placement is the granted window from the child itself and the plan's
+ spill facts from the preset decision — the difference between 'fast'
+ and 'why is my CPU busy', so it must be inspectable, not inferred."""
+
+ data = _router_request(running, "/models", timeout=3)
+ # Everything resident or becoming resident: 'loading' renders as its own
+ # state in the pane (a 20-GB load in flight is the most important thing
+ # the pane can show).
+ loaded = {
+ m["id"]: m.get("status", {}).get("value", "unknown")
+ for m in data.get("data", [])
+ if m.get("status", {}).get("value") in ("loaded", "ready", "loading")
+ }
+ placement: Dict[str, Any] = {}
+ decisions = presets.read_preset_decisions()
+ for model_id, state in loaded.items():
+ facts: Dict[str, Any] = {}
+ plan = decisions.get(model_id)
+ if plan is not None:
+ facts["window"] = plan.window
+ facts["window_label"] = f"{plan.window // 1024}K"
+ facts["spilled"] = plan.spilled
+ if state in ("loaded", "ready"):
+ try:
+ props = _router_request(running, f"/props?model={model_id}", timeout=3)
+ n_ctx = props.get("default_generation_settings", {}).get("n_ctx")
+ if n_ctx:
+ facts["granted_window"] = int(n_ctx)
+ facts["granted_window_label"] = f"{int(n_ctx) // 1024}K"
+ except Exception: # noqa: BLE001
+ pass
+ if facts:
+ placement[model_id] = facts
+ return loaded, placement
+
+
@router.get("/api/local-models/status")
def local_models_status():
- """Cheap, immediate, never blocks on probes (responsiveness standard):
- config state + installed runtime + staged models + supervisor state.
- GPU facts come from /api/local-models/hardware (slower, polled).
-
- Sync def on purpose: the body does blocking urlopen/scans, so it runs
- in FastAPI's threadpool instead of stalling the event loop."""
- from hermes_cli.local_runtime.binaries import (
- default_tag,
- installed_tags,
- runtimes_root,
- server_binary,
- )
+ """Cheap, immediate, never blocks on probes: config state + installed
+ runtime + staged models + supervisor state. GPU facts come from
+ /api/local-models/hardware (slower, polled).
+
+ Sync def on purpose: blocking urlopen/scans run in FastAPI's threadpool
+ instead of stalling the event loop."""
section = _runtime_section()
- configured_tag = section.get("tag") or default_tag()
- have = installed_tags()
+ configured_tag = section.get("tag") or binaries.default_tag()
+ have = binaries.installed_tags()
# The tag actually serving (boot ladder: configured if installed, else
- # newest installed). Present tense for the pane header.
+ # newest installed).
tag = configured_tag if configured_tag in have else (have[0] if have else configured_tag)
# A pending engine update exists when the user runs the local engine
- # (enabled + something installed) and the configured tag — pinned or
- # the Hermes-release default — is newer than anything on disk. The
- # download is a button click, never automatic.
+ # (enabled + something installed) and the configured tag — pinned or the
+ # Hermes-release default — is newer than anything on disk. The download
+ # is a button click, never automatic.
update_available = bool(
section.get("enabled") and have and configured_tag not in have)
runtime_installed = False
runtime_backend = None
- root = runtimes_root() / tag
+ root = binaries.runtimes_root() / tag
if root.exists():
for backend_dir in sorted(p for p in root.iterdir() if p.is_dir()):
try:
- server_binary(backend_dir)
+ binaries.server_binary(backend_dir)
runtime_installed = True
runtime_backend = backend_dir.name
break
@@ -308,80 +483,23 @@ def local_models_status():
staged = []
mdir = _models_dir()
if mdir.exists():
- from hermes_cli.local_runtime.bootstrap import staged_models
- # Split models: report the whole variant's bytes, not one part's.
- from hermes_cli.local_runtime.catalog import find_entry_for_model
-
- for gguf in staged_models():
+ for gguf in bootstrap.staged_models():
model_id = _model_id_for(gguf)
- size = gguf.stat().st_size
- hit = find_entry_for_model(model_id)
- if hit is not None:
- size = hit[1].size_bytes
- staged.append({
- "id": model_id,
- "size_bytes": size,
- "size_label": _human_gb(size),
- })
+ # Split models: report the whole variant's bytes, not one part's.
+ hit = catalog.find_entry_for_model(model_id)
+ size = hit[1].size_bytes if hit is not None else gguf.stat().st_size
+ staged.append({"id": model_id, "size_bytes": size, "size_label": _human_gb(size)})
running = _state_endpoint()
- # Which staged models are resident right now (loaded in VRAM). Read
- # from the live router when it's up; {} when down. Feeds the pane's
+ # Resident models from the live router; {} when down. Feeds the pane's
# Loaded pills and eject buttons.
loaded: Dict[str, str] = {}
placement: Dict[str, Any] = {}
if running is not None:
try:
- import urllib.request as _url
-
- req = _url.Request(
- running["base_url"].rsplit("/v1", 1)[0] + "/models",
- headers={"Authorization": f"Bearer {running.get('api_key', '')}"})
- with _url.urlopen(req, timeout=3) as r:
- data = json.loads(r.read())
- loaded = {
- m["id"]: m.get("status", {}).get("value", "unknown")
- for m in data.get("data", [])
- # Everything resident or becoming resident: 'loading' renders
- # as its own state in the pane (a 20-GB load in flight is the
- # single most important thing the pane can show).
- if m.get("status", {}).get("value") in ("loaded", "ready", "loading")
- }
- # How each loaded model is actually running: the granted window
- # from the child itself, and the plan's spill facts from the
- # preset decision. The pane shows this verbatim — placement is
- # the difference between 'fast' and 'why is my CPU busy', so it
- # must be inspectable, not inferred from Task Manager.
- from hermes_cli.local_runtime.presets import read_preset_decisions
-
- decisions = read_preset_decisions()
- for model_id in loaded:
- entry_facts: Dict[str, Any] = {}
- plan = decisions.get(model_id)
- if plan is not None:
- entry_facts["window"] = plan.window
- entry_facts["window_label"] = f"{plan.window // 1024}K"
- entry_facts["spilled"] = plan.spilled
- if loaded[model_id] in ("loaded", "ready"):
- try:
- preq = _url.Request(
- running["base_url"].rsplit("/v1", 1)[0]
- + f"/props?model={model_id}",
- headers={"Authorization":
- f"Bearer {running.get('api_key', '')}"})
- with _url.urlopen(preq, timeout=3) as pr:
- props = json.loads(pr.read())
- n_ctx = (props.get("default_generation_settings", {})
- .get("n_ctx"))
- if n_ctx:
- entry_facts["granted_window"] = int(n_ctx)
- entry_facts["granted_window_label"] = f"{int(n_ctx) // 1024}K"
- except Exception: # noqa: BLE001
- pass
- if entry_facts:
- placement[model_id] = entry_facts
+ loaded, placement = _loaded_models(running)
except Exception as exc: # noqa: BLE001
# Never silent: an empty dict here renders as 'Not in memory'
# on a machine whose VRAM is visibly full.
@@ -392,10 +510,8 @@ def local_models_status():
# same model.provider + model.default that /api/model/set writes).
active_model_id = None
try:
- config = _load_config()
- model_section = (config or {}).get("model") or {}
- if str(model_section.get("provider", "")).strip().lower() in (
- "llamacpp", "llama.cpp", "llama-cpp"):
+ model_section = (_load_config() or {}).get("model") or {}
+ if str(model_section.get("provider", "")).strip().lower() in _LLAMACPP_PROVIDERS:
active_model_id = str(
model_section.get("default") or model_section.get("name") or ""
).strip() or None
@@ -424,9 +540,8 @@ def local_models_status():
def _loading_progress() -> Dict[str, Any]:
try:
- from hermes_cli.local_runtime.load_progress import get_loading_progress
- return get_loading_progress()
+ return load_progress.get_loading_progress()
except Exception: # noqa: BLE001 — progress is garnish, never a 500
return {}
@@ -437,12 +552,11 @@ def _loading_progress() -> Dict[str, Any]:
@router.get("/api/local-models/hardware")
def local_models_hardware():
"""The budget as plain facts. Polled by the pane and the statusbar
- resource item (throttled client-side). Sync def on purpose: the body
- shells out to nvidia-smi and probes budgets — threadpool, not loop."""
- from hermes_cli.local_runtime.hardware import probe_budget, _ram_bytes
+ resource item (throttled client-side). Sync def on purpose: shells out
+ to nvidia-smi and probes budgets — threadpool, not loop."""
- budget = probe_budget()
- ram_total, ram_avail = _ram_bytes()
+ budget = hardware.probe_budget()
+ ram_total, ram_avail = hardware._ram_bytes()
out = {
"uma": budget.uma,
"vram_total_bytes": budget.total_device_bytes,
@@ -457,11 +571,8 @@ def local_models_hardware():
# GPU identity + live utilization (NVIDIA; other vendors degrade to None
# and the UI hides those readouts).
try:
- import subprocess
- from hermes_cli.local_runtime.hardware import _nvidia_smi_path
-
- smi_exe = _nvidia_smi_path()
+ smi_exe = hardware._nvidia_smi_path()
smi = subprocess.run(
[smi_exe, "--query-gpu=name,utilization.gpu,memory.used",
"--format=csv,noheader,nounits"],
@@ -478,59 +589,47 @@ def local_models_hardware():
# ── catalog: priced for THIS machine before download ─────────
+_QUANT_REASONS = {
+ "best-large-window": ("Recommended build ({quant}) — the quant class this "
+ "engine is optimized for; runs fully on your GPU with a "
+ "large context window"),
+ "best-fits": ("Recommended build ({quant}) — the quant class this "
+ "engine is optimized for; runs fully on your GPU"),
+}
+_QUANT_REASON_COMPACT = ("Compact build sized for this machine ({quant}) — "
+ "larger than GPU memory, runs slower")
+
@router.get("/api/local-models/catalog")
def local_models_catalog():
"""Every entry answers the user's three questions up front: how big is
the download, will it fit, and what context/speed shape will I get —
- computed from the catalog's measured numbers + this machine's
- budget. Hardware-aware quant selection: the row advertises the BEST
- build for this machine (highest quality that runs fully on the GPU at
- the 64K floor; else the smallest that works, spilled and priced). No
- entry is hidden; unaffordable models show WHY. Sync def on purpose:
- probe_budget + catalog I/O block — threadpool, not loop."""
- from hermes_cli.local_runtime.catalog import (
- CATALOG,
- recommended_entry,
- refresh_catalog_soon,
- select_variant,
- )
- from hermes_cli.local_runtime.context_policy import (
- RUNTIME_OVERHEAD_BYTES,
- initial_window,
- ub_logits_bytes,
- )
- from hermes_cli.local_runtime.estimator import PhysicsRefusal
- from hermes_cli.local_runtime.hardware import probe_budget
-
- # This request serves the catalog already in memory; a TTL-gated
- # background fetch from the repo lands new entries for the next one
- # (day-0 models reach the pane without an app release).
- refresh_catalog_soon()
-
- # Planning budget: price against machine capacity, not live-free VRAM.
- # A loaded model must not make the catalog call every row unaffordable.
- budget = probe_budget(planning=True)
- # The default pick for THIS machine: quality-ranked, fit- and
- # speed-gated (recommended_entry). Engine-gated entries can't be
- # activated today, so they can't be the recommendation either. The
- # reason key ships with the row — the Recommended badge's tooltip is
+ from the catalog's measured numbers + this machine's budget. The row
+ advertises the BEST build for this machine (highest quality that runs
+ fully on the GPU at the 64K floor; else the smallest that works, spilled
+ and priced). No entry is hidden; unaffordable models show WHY. Sync def
+ on purpose: probe_budget + catalog I/O block — threadpool, not loop."""
+
+ # Serve the catalog already in memory; a TTL-gated background fetch lands
+ # new entries for the next call (day-0 models without an app release).
+ catalog.refresh_catalog_soon()
+
+ # Planning budget: price against machine capacity, not live-free VRAM —
+ # a loaded model must not make every row unaffordable.
+ budget = hardware.probe_budget(planning=True)
+ # The default pick for THIS machine: quality-ranked, fit- and speed-gated.
+ # The reason key ships with the row so the Recommended badge's tooltip is
# the branch that actually fired, not a re-derivation that can drift.
- eligible = tuple(e for e in CATALOG if not _engine_too_old(e.min_engine))
- picked = recommended_entry(budget, eligible)
+ picked = catalog.recommended_entry(budget, _eligible_entries())
recommended = picked[0].id if picked is not None else None
recommended_reason = picked[1] if picked is not None else None
- # Completeness-checked staging (split parts all present) — the same
- # answer the picker and the router see, so a mid-download model never
- # reads as downloaded here.
- from hermes_cli.local_runtime.bootstrap import staged_model_ids
-
- staged_ids = set(staged_model_ids())
+ # Completeness-checked staging (split parts all present) — same answer the
+ # picker and router see, so a mid-download model never reads as downloaded.
+ staged_ids = set(bootstrap.staged_model_ids())
entries = []
- for entry in CATALOG:
- choice = select_variant(entry, budget)
- # Any variant of this family already on disk counts as downloaded
- # (split variants stage under their first part).
+ for entry in catalog.CATALOG:
+ choice = catalog.select_variant(entry, budget)
+ # Any variant of this family on disk counts as downloaded.
downloaded_variant = next(
(v for v in entry.variants if v.model_id in staged_ids), None)
row: Dict[str, Any] = {
@@ -546,10 +645,9 @@ def local_models_catalog():
"downloaded_quant": downloaded_variant.quant if downloaded_variant else None,
"mtp": entry.mtp,
"vision": entry.mmproj is not None,
- # Day-0 architectures need the llama.cpp release where their
- # support landed. True gates download/activate in the pane
- # until the engine updates; the row still renders (visible +
- # explained beats hidden).
+ # Day-0 architectures need the llama.cpp release where their support
+ # landed. True gates download/activate in the pane until the engine
+ # updates; the row still renders (visible + explained beats hidden).
"needs_engine": _engine_too_old(entry.min_engine),
"min_engine": entry.min_engine or None,
}
@@ -568,15 +666,13 @@ def local_models_catalog():
continue
variant = choice.variant
- profile = entry.profile(variant)
- # Same overhead the launch decision prices (runtime buffers +
- # vision projector + the microbatch/MTP logits buffers): the row
- # must advertise the window the model will actually get, not a
- # paper number the server's own fit then shaves down.
- overhead = (RUNTIME_OVERHEAD_BYTES
+ # Same overhead the launch decision prices (runtime buffers + vision
+ # projector + microbatch/MTP logits): the row must advertise the window
+ # the model will actually get, not a paper number.
+ overhead = (context_policy.RUNTIME_OVERHEAD_BYTES
+ (entry.mmproj.size_bytes if entry.mmproj else 0)
- + ub_logits_bytes(entry.n_vocab, mtp_capable=entry.mtp))
- decision = initial_window(profile, budget, overhead_bytes=overhead)
+ + context_policy.ub_logits_bytes(entry.n_vocab, mtp_capable=entry.mtp))
+ decision = context_policy.initial_window(entry.profile(variant), budget, overhead_bytes=overhead)
download_total = entry.download_bytes(variant)
row.update({
"fits": True,
@@ -586,21 +682,10 @@ def local_models_catalog():
"size_bytes": download_total,
"size_label": _human_gb(download_total),
"variant_count": len(entry.variants),
+ "quant_reason": _QUANT_REASONS.get(
+ choice.reason_key, _QUANT_REASON_COMPACT).format(quant=variant.quant),
})
- if choice.reason_key == "best-large-window":
- row["quant_reason"] = (
- f"Recommended build ({variant.quant}) — the quant class this "
- "engine is optimized for; runs fully on your GPU with a "
- "large context window")
- elif choice.reason_key == "best-fits":
- row["quant_reason"] = (
- f"Recommended build ({variant.quant}) — the quant class this "
- "engine is optimized for; runs fully on your GPU")
- else:
- row["quant_reason"] = (
- f"Compact build sized for this machine ({variant.quant}) — "
- "larger than GPU memory, runs slower")
- if not isinstance(decision, PhysicsRefusal):
+ if not isinstance(decision, estimator.PhysicsRefusal):
row["start_window"] = decision.window
row["start_window_label"] = f"{decision.window // 1024}K"
row["spilled"] = decision.spilled
@@ -628,13 +713,11 @@ class RuntimeInstallBody(BaseModel):
def _runtime_progress_hook(job: Dict[str, Any]):
"""Adapter: ensure_runtime_installed's progress stream -> job fields.
- Throttled to ~4 updates/s. The byte counters are CUMULATIVE across the
- plan: a multi-asset engine (CUDA zip + cudart zip) reads as one growing
- download, not a bar that restarts at zero per asset. The total grows as
- each asset's size becomes known (sizes arrive with the response, not
- the plan). Unpack/verify keep the download's counters in place — the
- stage text says what's happening, and a bar that bounces back to zero
- after the bytes finished reads as a failure."""
+ Throttled to ~4 updates/s. Byte counters are CUMULATIVE across the plan:
+ a multi-asset engine (CUDA zip + cudart zip) reads as one growing
+ download, not a bar that restarts per asset; the total grows as each
+ asset's size becomes known. Unpack/verify keep the download's counters —
+ a bar bouncing back to zero after the bytes finished reads as failure."""
state = {"last": 0.0, "banked": 0, "asset": None, "asset_total": 0}
def hook(stage: str, done: int, total: int, label: str) -> None:
@@ -645,20 +728,17 @@ def _runtime_progress_hook(job: Dict[str, Any]):
suffix = f" ({label})" if label else ""
if stage == "download":
if label != state["asset"]:
- # Previous asset finished: bank its bytes so the counters
- # keep climbing instead of restarting for the next asset.
+ # Previous asset finished: bank its bytes so the counters keep
+ # climbing instead of restarting for the next asset.
state["banked"] += state["asset_total"]
state["asset"] = label
state["asset_total"] = total or done
plan_done = state["banked"] + done
plan_total = state["banked"] + (total or 0)
job["phase"] = "downloading-runtime"
+ job["detail"] = f"Downloading the local engine{suffix} — {_human_gb(plan_done)}"
if total:
- job["detail"] = (f"Downloading the local engine{suffix} — "
- f"{_human_gb(plan_done)} of {_human_gb(plan_total)}")
- else:
- job["detail"] = (f"Downloading the local engine{suffix} — "
- f"{_human_gb(plan_done)}")
+ job["detail"] += f" of {_human_gb(plan_total)}"
job["done_bytes"] = plan_done
job["total_bytes"] = plan_total or None
elif stage == "extract":
@@ -674,81 +754,52 @@ def _runtime_progress_hook(job: Dict[str, Any]):
@router.post("/api/local-models/runtime/install")
async def local_models_runtime_install(body: RuntimeInstallBody):
- from hermes_cli.local_runtime.binaries import (
- default_tag,
- resolve_assets,
- select_backend,
- )
- from hermes_cli.local_runtime.bootstrap import _detect_gpu_vendor
section = _runtime_section()
- tag = section.get("tag") or default_tag()
- backend = body.backend or section.get("backend", "auto")
- if backend == "auto":
- backend = select_backend(_detect_gpu_vendor())
+ tag = section.get("tag") or binaries.default_tag()
+ backend = _resolve_backend(section, body.backend)
# Resolve first so an impossible combination fails the POST, not the job.
try:
- plan = resolve_assets(tag, backend)
+ plan = binaries.resolve_assets(tag, backend)
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=400, detail=str(exc))
job = _job("runtime-install", f"llama.cpp {tag} ({backend})")
def _run():
+
+ previous = binaries.installed_tags()
+ job["phase"] = "downloading"
+ job["detail"] = f"Fetching {len(plan.assets)} package(s) for {backend}"
+ binaries.ensure_runtime_installed(tag, backend, progress=_runtime_progress_hook(job))
+
+ # Engine update path: a server already running on an older tag moves
+ # to the new one now — the click was the consent. Fresh installs (no
+ # server) skip this; Use/boot handles their start.
+ restarted = False
try:
- from hermes_cli.local_runtime.binaries import (
- ensure_runtime_installed,
- installed_tags,
- prune_old_tags,
- )
- previous = installed_tags()
- job["phase"] = "downloading"
- job["detail"] = f"Fetching {len(plan.assets)} package(s) for {backend}"
- ensure_runtime_installed(tag, backend,
- progress=_runtime_progress_hook(job))
-
- # Engine update path: a server already running on an older tag
- # moves to the new one now — the click was the consent. Fresh
- # installs (no server) skip this; Use/boot handles their start.
- restarted = False
- try:
- from hermes_cli.local_runtime.bootstrap import (
- ensure_local_runtime,
- get_supervisor,
- shutdown_local_runtime,
- )
-
- sup = get_supervisor()
- if sup is not None and previous and tag not in previous:
- job["phase"] = "restarting"
- job["detail"] = "Switching the running server to the new build"
- shutdown_local_runtime()
- ensure_local_runtime(_load_config(), force=True)
- restarted = True
- except Exception as exc: # noqa: BLE001
- # The new build is installed either way; the next boot serves
- # it. Never fail the job on the restart nicety.
- logger.warning("post-update restart skipped: %s", exc)
-
- # N-1 retention, only after the new tag verified: keep it and the
- # newest previous build as the rollback pin target.
- try:
- keep = [tag] + [t for t in previous if t != tag][:1]
- prune_old_tags(keep)
- except Exception as exc: # noqa: BLE001
- logger.warning("runtime prune skipped: %s", exc)
-
- job["phase"] = "done"
- job["status"] = "done"
- job["detail"] = (f"llama.cpp {tag} ready ({backend})"
- + (" — server restarted on the new build" if restarted else ""))
+ if bootstrap.get_supervisor() is not None and previous and tag not in previous:
+ job["phase"] = "restarting"
+ job["detail"] = "Switching the running server to the new build"
+ bootstrap.shutdown_local_runtime()
+ bootstrap.ensure_local_runtime(_load_config(), force=True)
+ restarted = True
except Exception as exc: # noqa: BLE001
- logger.warning("runtime install failed: %s", exc)
- job["status"] = "error"
- job["error"] = str(exc)
+ # The new build is installed either way; the next boot serves it.
+ logger.warning("post-update restart skipped: %s", exc)
+
+ # N-1 retention, only after the new tag verified: keep it and the
+ # newest previous build as the rollback pin target.
+ try:
+ binaries.prune_old_tags([tag] + [t for t in previous if t != tag][:1])
+ except Exception as exc: # noqa: BLE001
+ logger.warning("runtime prune skipped: %s", exc)
+
+ _finish(job, f"llama.cpp {tag} ready ({backend})"
+ + (" — server restarted on the new build" if restarted else ""))
- threading.Thread(target=_run, daemon=True, name="lr-runtime-install").start()
+ _spawn_job(job, "lr-runtime-install", _run, fail_msg="runtime install failed: %s")
return {"job_id": job["job_id"], "backend": backend, "tag": tag}
@@ -763,14 +814,8 @@ class ModelDownloadBody(BaseModel):
async def local_models_download(body: ModelDownloadBody):
"""Accepts either a family id (downloads this machine's selected
variant) or an exact variant model_id."""
- from hermes_cli.local_runtime.catalog import (
- CATALOG,
- catalog_by_id,
- select_variant,
- )
- from hermes_cli.local_runtime.hardware import probe_budget
-
- entry = catalog_by_id().get(body.model_id)
+
+ entry = catalog.catalog_by_id().get(body.model_id)
variant = None
if entry is not None:
if _engine_too_old(entry.min_engine):
@@ -780,84 +825,40 @@ async def local_models_download(body: ModelDownloadBody):
f"or newer — update the engine first"))
# Same planning budget as the catalog — the user downloads exactly
# the build the row advertised.
- choice = select_variant(entry, probe_budget(planning=True))
+ choice = catalog.select_variant(entry, hardware.probe_budget(planning=True))
if choice is None:
raise HTTPException(status_code=409,
detail=f"no variant of {entry.id} fits this machine")
variant = choice.variant
else:
- for candidate in CATALOG:
- for v in candidate.variants:
- if v.model_id == body.model_id:
- entry, variant = candidate, v
- break
- if variant:
- break
+ hit = catalog.find_entry_for_model(body.model_id)
+ if hit is not None:
+ entry, variant = hit
if entry is None or variant is None:
raise HTTPException(status_code=404, detail=f"unknown model {body.model_id}")
- from hermes_cli.local_runtime.bootstrap import assets_dir, staged_model_ids
-
- if variant.model_id in staged_model_ids():
+ if variant.model_id in bootstrap.staged_model_ids():
return {"job_id": None, "already_downloaded": True, "model_id": variant.model_id}
- # Everything this variant needs: split parts + mmproj/draft assets.
- plan = [] # (url, dest, bytes)
- for asset in variant.files:
- plan.append((f"https://huggingface.co/{entry.repo}/resolve/main/{asset.path}",
- _models_dir() / asset.local_name, asset.size_bytes))
- for asset in (entry.mmproj, entry.draft):
- if asset is not None:
- plan.append((f"https://huggingface.co/{entry.repo}/resolve/main/{asset.path}",
- assets_dir() / asset.local_name, asset.size_bytes))
-
- total = sum(p[2] for p in plan)
+ plan = _download_plan(entry, variant)
job = _job("model-download", f"{entry.display_name} ({variant.quant})",
model_id=entry.id)
- job["total_bytes"] = total
+ job["total_bytes"] = sum(p[2] for p in plan)
def _run():
- try:
- job["phase"] = "downloading"
- job["detail"] = f"{entry.display_name} — {_human_gb(total)}"
- done_before = 0
- for url, dest, size in plan:
- if dest.exists():
- done_before += size
- job["done_bytes"] = done_before
- continue
- download_file(url, dest, job,
- base_done=done_before, keep_totals=True)
- job["phase"] = "downloading"
- done_before += size
- job["done_bytes"] = done_before
- job["phase"] = "done"
- job["status"] = "done"
- job["detail"] = f"{entry.display_name} ready"
- # A running router only scans models at spawn —
- # bounce it so the new model is servable
- # immediately instead of 400ing until the next app restart.
- try:
- from hermes_cli.local_runtime.bootstrap import refresh_local_runtime
-
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("post-download runtime refresh skipped", exc_info=True)
- except Exception as exc: # noqa: BLE001
- logger.warning("model download failed: %s", exc)
- job["status"] = "error"
- job["error"] = str(exc)
+ _run_download_plan(job, plan, entry.display_name)
+ _finish(job, f"{entry.display_name} ready")
+ _refresh_runtime("post-download runtime refresh skipped")
- threading.Thread(target=_run, daemon=True, name="lr-model-download").start()
+ _spawn_job(job, "lr-model-download", _run, fail_msg="model download failed: %s")
return {"job_id": job["job_id"], "model_id": variant.model_id}
@router.delete("/api/local-models/models/{model_id}")
async def local_models_delete(model_id: str):
- """Remove a staged model: every split part plus its private assets.
- A running router keeps serving from its spawn-time scan, so bounce it
- off the request thread — deleting the active file mid-serve is the
- kind of stale state the refresh exists for."""
+ """Remove a staged model: every split part plus its private assets, then
+ bounce the router off the request thread (deleting the active file
+ mid-serve is exactly the stale state the refresh exists for)."""
files = _variant_files_on_disk(model_id)
if not files:
raise HTTPException(status_code=404, detail="model not found")
@@ -866,21 +867,13 @@ async def local_models_delete(model_id: str):
# Growth state dies with the model: a re-download starts back at its
# zero-spill window instead of inheriting a stale grown one.
try:
- from hermes_cli.local_runtime.growth import clear_window_override
- clear_window_override(model_id)
+ growth.clear_window_override(model_id)
except Exception: # noqa: BLE001
logger.debug("window-override clear skipped", exc_info=True)
- def _refresh():
- try:
- from hermes_cli.local_runtime.bootstrap import refresh_local_runtime
-
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("post-delete runtime refresh skipped", exc_info=True)
-
- threading.Thread(target=_refresh, daemon=True, name="lr-post-delete").start()
+ threading.Thread(target=_refresh_runtime, args=("post-delete runtime refresh skipped",),
+ daemon=True, name="lr-post-delete").start()
return {"ok": True}
@@ -906,56 +899,34 @@ _QUICKSTART_LOCK = threading.Lock()
@router.post("/api/local-models/quickstart")
async def local_models_quickstart(body: QuickstartBody):
- """The dummy-proof path: one job that installs the runtime (if
- missing), downloads this machine's build of the recommended model
- (if missing), and makes it the default for new chats. Each leg is
- the same code the individual routes run — this route only sequences
- them, so 'Configure' (the existing pane) and quickstart can never
- disagree about what gets installed.
+ """The dummy-proof path: one job that installs the runtime (if missing),
+ downloads this machine's build of the recommended model (if missing),
+ and makes it the default for new chats. Each leg is the same code the
+ individual routes run — this route only sequences them, so 'Configure'
+ and quickstart can never disagree about what gets installed.
Preflight rejects (no servable entry, engine too old) fail the POST
- synchronously so the button can explain itself; everything slow runs
- in the job with the usual phase/byte progress.
+ synchronously so the button can explain itself; everything slow runs in
+ the job with the usual phase/byte progress.
"""
- from hermes_cli.local_runtime.binaries import (
- default_tag,
- installed_tags,
- resolve_assets,
- select_backend,
- )
- from hermes_cli.local_runtime.bootstrap import (
- _detect_gpu_vendor,
- assets_dir,
- staged_model_ids,
- )
- from hermes_cli.local_runtime.catalog import (
- CATALOG,
- catalog_by_id,
- recommended_entry,
- select_variant,
- )
- from hermes_cli.local_runtime.hardware import probe_budget
# Resolve the target entry: explicit id, else this machine's
- # recommendation (quality-ranked, fit- and speed-gated), else the
- # first catalog entry this machine can serve.
- budget = probe_budget(planning=True)
- entry = None
+ # recommendation, else the first catalog entry this machine can serve.
+ budget = hardware.probe_budget(planning=True)
if body.model_id:
- entry = catalog_by_id().get(body.model_id)
+ entry = catalog.catalog_by_id().get(body.model_id)
if entry is None:
raise HTTPException(status_code=404,
detail=f"unknown model {body.model_id}")
candidates = [entry]
else:
- eligible = tuple(e for e in CATALOG if not _engine_too_old(e.min_engine))
- picked = recommended_entry(budget, eligible)
+ picked = catalog.recommended_entry(budget, _eligible_entries())
best = picked[0] if picked is not None else None
candidates = ([best] if best is not None else []) + [
- e for e in CATALOG if best is None or e.id != best.id]
+ e for e in catalog.CATALOG if best is None or e.id != best.id]
chosen = None
for candidate in candidates:
- choice = select_variant(candidate, budget)
+ choice = catalog.select_variant(candidate, budget)
if choice is not None and not _engine_too_old(candidate.min_engine):
chosen = (candidate, choice.variant)
break
@@ -967,177 +938,104 @@ async def local_models_quickstart(body: QuickstartBody):
entry, variant = chosen
section = _runtime_section()
- tag = section.get("tag") or default_tag()
- backend = section.get("backend", "auto")
- if backend == "auto":
- backend = select_backend(_detect_gpu_vendor())
- need_runtime = not installed_tags()
+ tag = section.get("tag") or binaries.default_tag()
+ backend = _resolve_backend(section)
+ need_runtime = not binaries.installed_tags()
if need_runtime:
# Same preflight as /runtime/install: impossible combos fail the POST.
try:
- resolve_assets(tag, backend)
+ binaries.resolve_assets(tag, backend)
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=400, detail=str(exc))
- need_download = variant.model_id not in staged_model_ids()
- download_plan = [] # (url, dest, bytes)
- if need_download:
- for asset in variant.files:
- download_plan.append(
- (f"https://huggingface.co/{entry.repo}/resolve/main/{asset.path}",
- _models_dir() / asset.local_name, asset.size_bytes))
- for asset in (entry.mmproj, entry.draft):
- if asset is not None:
- download_plan.append(
- (f"https://huggingface.co/{entry.repo}/resolve/main/{asset.path}",
- assets_dir() / asset.local_name, asset.size_bytes))
+ need_download = variant.model_id not in bootstrap.staged_model_ids()
+ download_plan = _download_plan(entry, variant) if need_download else []
+ download_bytes = sum(p[2] for p in download_plan)
if not _QUICKSTART_LOCK.acquire(blocking=False):
raise HTTPException(status_code=409,
detail="Setup is already running")
job = _job("quickstart", entry.display_name, model_id=entry.id)
- job["total_bytes"] = sum(p[2] for p in download_plan) or None
+ job["total_bytes"] = download_bytes or None
def _run():
- try:
- if need_runtime:
- from hermes_cli.local_runtime.binaries import ensure_runtime_installed
-
- job["phase"] = "installing-runtime"
- job["detail"] = "Installing the local engine"
- ensure_runtime_installed(tag, backend,
- progress=_runtime_progress_hook(job))
-
- if need_download:
- job["phase"] = "downloading"
- total = sum(p[2] for p in download_plan)
- # The runtime leg repurposed the byte counters for its own
- # stages — reset them to the model plan before download.
- job["done_bytes"] = 0
- job["total_bytes"] = total
- job["detail"] = f"{entry.display_name} — {_human_gb(total)}"
- done_before = 0
- for url, dest, size in download_plan:
- if dest.exists():
- done_before += size
- job["done_bytes"] = done_before
- continue
- download_file(url, dest, job,
- base_done=done_before, keep_totals=True)
- job["phase"] = "downloading"
- done_before += size
- job["done_bytes"] = done_before
-
- # Activate: same sequence as /activate's job body.
- from hermes_cli.config import load_config, save_config
- from hermes_cli.local_runtime.bootstrap import (
- ensure_local_runtime,
- refresh_local_runtime,
- )
-
- job["phase"] = "starting-server"
- job["detail"] = "Starting the local server"
- config = load_config()
- config.setdefault("local_runtime", {})["enabled"] = True
- save_config(config)
- sup = ensure_local_runtime(config, force=True)
- if sup is None and _state_endpoint() is None:
- raise RuntimeError(
- "The local server could not start — open Local Models for details")
- if sup is not None:
- try:
- if variant.model_id not in sup.models():
- job["detail"] = "Refreshing the local server"
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("quickstart rescan check skipped", exc_info=True)
-
- job["phase"] = "setting-default"
- job["detail"] = "Making it your default"
- from hermes_cli.web_deps import late
-
- late("_apply_model_assignment_sync")(
- "main", "llamacpp", variant.model_id, "", "", "")
-
- job["phase"] = "done"
- job["status"] = "done"
- job["detail"] = f"{entry.display_name} is ready — new chats use it"
- except Exception as exc: # noqa: BLE001
- logger.warning("quickstart failed: %s", exc)
- job["status"] = "error"
- job["error"] = str(exc)
- finally:
- _QUICKSTART_LOCK.release()
-
- threading.Thread(target=_run, daemon=True, name="lr-quickstart").start()
+ if need_runtime:
+
+ job["phase"] = "installing-runtime"
+ job["detail"] = "Installing the local engine"
+ binaries.ensure_runtime_installed(tag, backend, progress=_runtime_progress_hook(job))
+
+ if need_download:
+ # The runtime leg repurposed the byte counters for its own
+ # stages — reset them to the model plan before download.
+ job["done_bytes"] = 0
+ job["total_bytes"] = download_bytes
+ _run_download_plan(job, download_plan, entry.display_name)
+
+ # Activate: same sequence as /activate's job body.
+ _ensure_server(
+ job, _set_runtime_enabled(True), variant.model_id,
+ fail_detail="The local server could not start — open Local Models for details",
+ skip_msg="quickstart rescan check skipped")
+ _assign_default(job, variant.model_id)
+ _finish(job, f"{entry.display_name} is ready — new chats use it")
+
+ _spawn_job(job, "lr-quickstart", _run, fail_msg="quickstart failed: %s",
+ on_exit=_QUICKSTART_LOCK.release)
return {
"job_id": job["job_id"],
"model_id": entry.id,
"display_name": entry.display_name,
"needs_runtime": need_runtime,
"needs_download": need_download,
- "download_bytes": sum(p[2] for p in download_plan),
+ "download_bytes": download_bytes,
}
+def _stop_server() -> None:
+
+ if bootstrap.get_supervisor() is not None:
+ bootstrap.shutdown_local_runtime()
+ elif _state_endpoint() is not None:
+ # Server owned by another process (or an orphan): best-effort
+ # terminate via the state file's pid, then clear the state.
+ try:
+ import psutil # type: ignore
+
+
+ state = json.loads(supervisor.state_path().read_text(encoding="utf-8"))
+ pid = int(state.get("pid") or 0)
+ if pid > 0 and psutil.pid_exists(pid):
+ psutil.Process(pid).terminate()
+ supervisor.state_path().unlink(missing_ok=True)
+ except Exception: # noqa: BLE001
+ pass
+ _set_runtime_enabled(False)
+
+
+def _start_server() -> None:
+
+ sup = bootstrap.ensure_local_runtime(_set_runtime_enabled(True), force=True)
+ if sup is None and _state_endpoint() is None:
+ raise RuntimeError("The local server could not start — check the "
+ "runtime is installed")
+
+
+_SERVER_ACTIONS = {"stop": _stop_server, "start": _start_server}
+
+
@router.post("/api/local-models/server")
async def local_models_server(body: ServerActionBody):
- """Turn the local engine off (stop the server, free ALL GPU memory,
- and disable auto-start) or back on. The off switch is the whole-engine
+ """Turn the local engine off (stop the server, free ALL GPU memory, and
+ disable auto-start) or back on. The off switch is the whole-engine
counterpart of per-model eject — and unlike eject it IS durable: the
user said off, so boots stay off until they say on."""
- import asyncio
-
- from hermes_cli.config import load_config, save_config
-
action = (body.action or "").strip().lower()
- if action not in ("stop", "start"):
+ if action not in _SERVER_ACTIONS:
raise HTTPException(status_code=400, detail="action must be 'stop' or 'start'")
-
- def _stop():
- from hermes_cli.local_runtime.bootstrap import (
- get_supervisor,
- shutdown_local_runtime,
- )
-
- sup = get_supervisor()
- if sup is not None:
- shutdown_local_runtime()
- else:
- # Server owned by another process (or an orphan): best-effort
- # terminate via the state file's pid, then clear the state.
- endpoint = _state_endpoint()
- if endpoint is not None:
- try:
- import psutil # type: ignore
-
- from hermes_cli.local_runtime.supervisor import state_path
-
- state = json.loads(state_path().read_text(encoding="utf-8"))
- pid = int(state.get("pid") or 0)
- if pid > 0 and psutil.pid_exists(pid):
- psutil.Process(pid).terminate()
- state_path().unlink(missing_ok=True)
- except Exception: # noqa: BLE001
- pass
- config = load_config()
- config.setdefault("local_runtime", {})["enabled"] = False
- save_config(config)
-
- def _start():
- from hermes_cli.local_runtime.bootstrap import ensure_local_runtime
-
- config = load_config()
- config.setdefault("local_runtime", {})["enabled"] = True
- save_config(config)
- sup = ensure_local_runtime(config, force=True)
- if sup is None and _state_endpoint() is None:
- raise RuntimeError("The local server could not start — check the "
- "runtime is installed")
-
try:
- await asyncio.to_thread(_stop if action == "stop" else _start)
+ await asyncio.to_thread(_SERVER_ACTIONS[action])
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=502, detail=str(exc)) from exc
return {"ok": True, "action": action}
@@ -1156,9 +1054,8 @@ def local_models_eject(body: ModelEjectBody):
demand — the next message to it (residency v2: no automatic loading
exists anywhere). Sync def on purpose: the fallback path blocks on a
urlopen with a 120s timeout — threadpool, never the event loop."""
- from hermes_cli.local_runtime.bootstrap import get_supervisor
- sup = get_supervisor()
+ sup = bootstrap.get_supervisor()
if sup is not None:
try:
sup.unload_model(body.model_id)
@@ -1172,16 +1069,8 @@ def local_models_eject(body: ModelEjectBody):
if endpoint is None:
raise HTTPException(status_code=409, detail="local server is not running")
try:
- import urllib.request as _url
-
- req = _url.Request(
- endpoint["base_url"].rsplit("/v1", 1)[0] + "/models/unload",
- data=json.dumps({"model": body.model_id}).encode(),
- headers={"Content-Type": "application/json",
- "Authorization": f"Bearer {endpoint.get('api_key', '')}"},
- method="POST")
- with _url.urlopen(req, timeout=120):
- pass
+ _router_request(endpoint, "/models/unload", timeout=120,
+ payload={"model": body.model_id})
return {"ok": True}
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=502, detail=str(exc)) from exc
@@ -1197,64 +1086,28 @@ async def local_models_activate(body: ModelActivateBody):
(residency v2): a config write through the same machinery as
/api/model/set, plus making sure the server is up. NO model loading —
models load on first inference, always; an empty router costs nothing.
- Fast enough to be synchronous-feeling, but kept as a job for UI
- continuity."""
+ Kept as a job for UI continuity."""
# Split variants stage under their first part — resolve like the rest
# of the routes instead of assuming a single flat file.
- from hermes_cli.local_runtime.bootstrap import staged_model_ids
- if body.model_id not in staged_model_ids():
+ if body.model_id not in bootstrap.staged_model_ids():
raise HTTPException(status_code=404, detail=f"{body.model_id} is not downloaded")
job = _job("model-activate", body.model_id, model_id=body.model_id)
def _run():
- try:
- from hermes_cli.config import load_config, save_config
- from hermes_cli.local_runtime.bootstrap import (
- ensure_local_runtime,
- refresh_local_runtime,
- )
-
- job["phase"] = "starting-server"
- job["detail"] = "Starting the local server"
- config = load_config()
- sup = ensure_local_runtime(config, force=True)
- if sup is None:
- if _state_endpoint() is None:
- raise RuntimeError(
- "The local server could not start — check the runtime is installed")
-
- # Self-heal a stale router: the model list is spawn-only, so a
- # server started before this model finished downloading can't
- # serve it. If the router doesn't know the model, bounce it.
- if sup is not None:
- try:
- if body.model_id not in sup.models():
- job["detail"] = "Refreshing the local server"
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("activate rescan check skipped", exc_info=True)
-
- job["phase"] = "setting-default"
- job["detail"] = "Making it your default"
- config = load_config()
- config.setdefault("local_runtime", {})["enabled"] = True
- save_config(config)
- from hermes_cli.web_deps import late
-
- late("_apply_model_assignment_sync")(
- "main", "llamacpp", body.model_id, "", "", "")
-
- job["phase"] = "done"
- job["status"] = "done"
- job["detail"] = f"{body.model_id} is the default for new chats"
- except Exception as exc: # noqa: BLE001
- logger.warning("model activate failed: %s", exc)
- job["status"] = "error"
- job["error"] = str(exc)
- threading.Thread(target=_run, daemon=True, name="lr-model-activate").start()
+ _ensure_server(
+ job, config_mod.load_config(), body.model_id,
+ fail_detail="The local server could not start — check the runtime is installed",
+ skip_msg="activate rescan check skipped")
+ job["phase"] = "setting-default"
+ job["detail"] = "Making it your default"
+ _set_runtime_enabled(True)
+ _assign_default(job, body.model_id)
+ _finish(job, f"{body.model_id} is the default for new chats")
+
+ _spawn_job(job, "lr-model-activate", _run, fail_msg="model activate failed: %s")
return {"job_id": job["job_id"]}
@@ -1268,13 +1121,7 @@ async def local_models_jobs():
with _JOBS_LOCK:
jobs = sorted(_JOBS.values(),
key=lambda j: (j["status"] != "running", -j["started_at"]))
- out = []
- for job in jobs[:20]:
- entry = dict(job)
- if entry["total_bytes"]:
- entry["percent"] = min(100, round(entry["done_bytes"] / entry["total_bytes"] * 100))
- out.append(entry)
- return {"jobs": out}
+ return {"jobs": [_job_view(job) for job in jobs[:20]]}
@router.get("/api/local-models/jobs/{job_id}")
@@ -1283,10 +1130,7 @@ async def local_models_job(job_id: str):
job = _JOBS.get(job_id)
if job is None:
raise HTTPException(status_code=404, detail="job not found")
- out = dict(job)
- if out["total_bytes"]:
- out["percent"] = min(100, round(out["done_bytes"] / out["total_bytes"] * 100))
- return out
+ return _job_view(job)
# ── Hugging Face browser: search, repo files, arbitrary download ─
@@ -1295,16 +1139,14 @@ async def local_models_job(job_id: str):
@router.get("/api/local-models/search")
async def local_models_search(q: str, limit: int = 20):
"""Full-text HF search over GGUF models — the firehose behind the
- curated catalog. The pane's per-quant fit pills come from the
- repo-files call once the user opens a hit."""
- from starlette.concurrency import run_in_threadpool
+ curated catalog. Per-quant fit pills come from the repo-files call once
+ the user opens a hit."""
- from hermes_cli.local_runtime.hf_browse import search_models
if not q.strip():
return {"hits": []}
try:
- hits = await run_in_threadpool(search_models, q, limit)
+ hits = await run_in_threadpool(hf_browse.search_models, q, limit)
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=502,
detail=f"Hugging Face search unavailable: {exc}") from exc
@@ -1314,16 +1156,13 @@ async def local_models_search(q: str, limit: int = 20):
@router.get("/api/local-models/search/files")
async def local_models_search_files(repo: str):
"""The servable GGUFs in one HF repo with a rough pre-download fit
- verdict per quant (file size + conservative fill-ins — the GGUF
- header refines it after download)."""
- from starlette.concurrency import run_in_threadpool
+ verdict per quant (file size + conservative fill-ins — the GGUF header
+ refines it after download)."""
- from hermes_cli.local_runtime.hardware import probe_budget
- from hermes_cli.local_runtime.hf_browse import priced_repo_files
try:
groups = await run_in_threadpool(
- priced_repo_files, repo, probe_budget(planning=True))
+ hf_browse.priced_repo_files, repo, hardware.probe_budget(planning=True))
except Exception as exc: # noqa: BLE001
raise HTTPException(status_code=502,
detail=f"Could not list {repo}: {exc}") from exc
@@ -1342,49 +1181,33 @@ async def local_models_download_browsed(body: BrowsedDownloadBody):
post-download bounce regenerates presets from its real header and the
fit policy owns its launch. No catalog entry — it serves 'unverified',
capabilities answered from the live server only."""
- import re as _re
-
- from hermes_cli.local_runtime.bootstrap import staged_model_ids
paths = [p for p in (body.paths or []) if p.lower().endswith(".gguf")]
if not paths:
raise HTTPException(status_code=422, detail="no .gguf files given")
first = paths[0].rsplit("/", 1)[-1]
- model_id = _re.sub(r"-\d{5}-of-\d{5}\.gguf$", "", first, flags=_re.IGNORECASE)
+ model_id = re.sub(r"-\d{5}-of-\d{5}\.gguf$", "", first, flags=re.IGNORECASE)
model_id = model_id[:-5] if model_id.lower().endswith(".gguf") else model_id
- if model_id in staged_model_ids():
+ if model_id in bootstrap.staged_model_ids():
return {"job_id": None, "already_downloaded": True, "model_id": model_id}
job = _job("model-download", f"{model_id} (from {body.repo})",
model_id=model_id)
def _run():
- try:
+ job["phase"] = "downloading"
+ for p in paths:
+ dest = _models_dir() / p.rsplit("/", 1)[-1]
+ if dest.exists():
+ continue
+ download_file(_hf_url(body.repo, urllib.parse.quote(p)), dest, job,
+ base_done=int(job.get("done_bytes") or 0),
+ keep_totals=bool(job.get("total_bytes")))
job["phase"] = "downloading"
- for p in paths:
- url = (f"https://huggingface.co/{body.repo}"
- f"/resolve/main/{urllib.parse.quote(p)}")
- dest = _models_dir() / p.rsplit("/", 1)[-1]
- if dest.exists():
- continue
- download_file(url, dest, job,
- base_done=int(job.get("done_bytes") or 0),
- keep_totals=bool(job.get("total_bytes")))
- job["phase"] = "downloading"
- job["phase"] = "done"
- job["status"] = "done"
- job["detail"] = f"{model_id} ready"
- try:
- from hermes_cli.local_runtime.bootstrap import refresh_local_runtime
+ _finish(job, f"{model_id} ready")
+ _refresh_runtime("post-download runtime refresh skipped")
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("post-download runtime refresh skipped", exc_info=True)
- except Exception as exc: # noqa: BLE001
- job["status"] = "error"
- job["error"] = str(exc)
-
- threading.Thread(target=_run, daemon=True, name="lm-download-browsed").start()
+ _spawn_job(job, "lm-download-browsed", _run)
return {"job_id": job["job_id"], "model_id": model_id}
@@ -1398,10 +1221,6 @@ async def local_models_sideload(body: SideloadBody):
the managed models dir (copy only when linking is impossible) and
bounce the router so it serves immediately. The original stays where
it is; delete-from-Hermes removes only our link."""
- import os
- import shutil
-
- from starlette.concurrency import run_in_threadpool
src = Path(body.path)
if not src.is_file() or src.suffix.lower() != ".gguf":
@@ -1417,10 +1236,5 @@ async def local_models_sideload(body: SideloadBody):
os.symlink(src, dest) # cross-volume fallback
except OSError:
await run_in_threadpool(shutil.copyfile, src, dest)
- try:
- from hermes_cli.local_runtime.bootstrap import refresh_local_runtime
-
- refresh_local_runtime()
- except Exception: # noqa: BLE001
- logger.debug("post-sideload runtime refresh skipped", exc_info=True)
+ _refresh_runtime("post-sideload runtime refresh skipped")
return {"ok": True, "model_id": dest.stem}