From 96897dc07036989226c163b1ad9a600eb2475164 Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Wed, 2 Sep 2026 23:55:00 -0700 Subject: [PATCH 1/3] =?UTF-8?q?refactor(hermes=5Fcli/web=5Frouters):=20loc?= =?UTF-8?q?al=5Fmodels=20=E2=80=94=20fold=20download-job/target/runtime-ta?= =?UTF-8?q?rget=20helpers,=20nvidia-smi=20+=20restart=20phase=20helpers?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- hermes_cli/web_routers/local_models.py | 601 ++++++++++++------------- 1 file changed, 282 insertions(+), 319 deletions(-) diff --git a/hermes_cli/web_routers/local_models.py b/hermes_cli/web_routers/local_models.py index cb771e482a..111b874696 100644 --- a/hermes_cli/web_routers/local_models.py +++ b/hermes_cli/web_routers/local_models.py @@ -45,6 +45,11 @@ _JOBS: Dict[str, Dict[str, Any]] = {} _JOBS_LOCK = threading.Lock() _LLAMACPP_PROVIDERS = ("llamacpp", "llama.cpp", "llama-cpp") _SPLIT_PART_RE = r"-\d{5}-of-\d{5}" +# One TCP stream to a CDN rarely fills a fast line; 8 ranged connections +# writing into a preallocated file saturate consumer gigabit. +_DOWNLOAD_CONNECTIONS = 8 +_CHUNK = 4 << 20 +_SERVER_START_FAILED = "The local server could not start — check the runtime is installed" def _human_gb(n: int | float) -> str: @@ -64,6 +69,15 @@ def _http_error(status: int, prefix: str = ""): raise HTTPException(status_code=status, detail=f"{prefix}{exc}") from exc +def _quiet(fn: Callable[[], Any], default: Any) -> Any: + """``fn()`` or ``default`` on any exception — for garnish that must never 500.""" + try: + return fn() + except Exception: # noqa: BLE001 + return default + + +# ── jobs ───────────────────────────────────────────────────── def _job(kind: str, target: str, model_id: str | None = None) -> Dict[str, Any]: job = { "job_id": uuid.uuid4().hex[:12], "kind": kind, "target": target, @@ -94,13 +108,17 @@ def _finish(job: Dict[str, Any], detail: str) -> None: job["status"] = "done" -def _spawn_job(job: Dict[str, Any], name: str, body: Callable[[], None], *, - fail_msg: str | None = None, on_exit: Callable[[], None] | None = None) -> None: +def _spawn_job(job: Dict[str, Any], name: str, body: Callable[[], None], *, fail_msg: str | None = None, + on_exit: Callable[[], None] | None = None, download_label: str | None = None) -> None: """Run ``body`` on a daemon thread; an exception marks the job errored (warning - ``fail_msg`` when given). ``on_exit`` always runs last.""" + ``fail_msg`` when given). ``on_exit`` always runs last. ``download_label`` makes it + a download job: it finishes as "