Refuse a request for a model that isn't loaded; show the slug in /models

The MLX proxy rewrote every request's "model" to the preloaded one, so asking
for a model the server wasn't running returned another model's output wearing
the requested name -- silently. The rewrite itself has to stay (mlx_lm would
otherwise try to load a model matching the name), so validate first: a name
that is neither the served name nor its repo id gets 404 model_not_found,
naming what is loaded and how to switch. An unstated name still means "the
loaded one", and the key is still checked before the model.

opencode's /models hid the same trap twice over. Label models
`<title> · <slug> (<where>)` so the list is searchable by the id used
everywhere else -- titles alone had dropped the slug -- and mark the loaded
one `(Local, loaded)`, refreshed by both switch paths (CLI and dashboard)
through a new opencode.sync(), which only ever refreshes a block that is
already there.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Neil Bantoc 2026-11-03 00:38:35 -08:00
parent 8c6615dbb4
commit 2c0818ca2b
5 changed files with 78 additions and 11 deletions

View file

@ -113,12 +113,20 @@ def key_ref(key_path):
return "{file:" + str(key_path) + "}"
def provider_block(cfg, host, key_path, label):
def model_label(m, label, loaded=False):
"""What opencode shows in /models: the catalog title to read, then the slug, which is
the id used everywhere else (`--model llmhost/<slug>`, `llmhost switch <slug>`) and so
the thing worth being able to search for."""
where = f"{label}, loaded" if loaded else label
return f"{m.get('title') or m['name']} \u00b7 {m['name']} ({where})"
def provider_block(cfg, host, key_path, label, mark_loaded=False):
models = {}
for m in cfg["models"]:
# Key by the slug -- that's the model id the API answers to -- but show the
# catalog's title, which is what reads well in opencode's /models list.
entry = {"name": f"{m.get('title') or m['name']} ({label})", "tool_call": True,
# Key by the slug -- that's the model id the API answers to.
entry = {"name": model_label(m, label, mark_loaded and m["name"] == cfg.get("active")),
"tool_call": True,
"limit": {"context": m["est_context"], "output": min(65536, m["est_context"] // 2)}}
if m.get("reasoning"):
entry["reasoning"] = True
@ -132,7 +140,9 @@ def provider_block(cfg, host, key_path, label):
def local_block(cfg):
return provider_block(cfg, "localhost", str(paths.KEY_FILE), LOCAL_LABEL)
# Only the local block is marked: llm-host keeps it in sync on every switch, whereas a
# remote block is a snapshot pasted elsewhere and would go stale the moment it changed.
return provider_block(cfg, "localhost", str(paths.KEY_FILE), LOCAL_LABEL, mark_loaded=True)
def base_host(block):
@ -333,5 +343,28 @@ def remove(prov_ids=None, local=True, host=None, every=False):
return "removed", path, sorted(targets), new_model
def sync(cfg):
"""Refresh an existing local provider block -- after a model switch, so the "loaded"
marker and the model list keep up. Never adds one: if opencode isn't wired up here,
switching a model is no reason to wire it. Returns the provider id, or None."""
path = config_file()
if not path.exists():
return None
data, problem = _read(path)
if problem:
return None
providers = data.get("provider") or {}
mine = local_ids(providers)
if not mine:
return None
prov = mine[0]
before = json.dumps(data, sort_keys=True)
providers[prov] = local_block(cfg)
if json.dumps(data, sort_keys=True) == before:
return None
_write(path, data, backup=False)
return prov
def snippet(cfg, prov=PROVIDER):
return json.dumps({"provider": {prov: local_block(cfg)}}, indent=2)