opencode: offer one model, not a menu where four of five fail
Only one model is loaded at a time, so listing every model that fits gave a picker with one working entry and four that now answer 404 -- and the entry that worked changed id on every `llmhost switch`, invalidating any client's saved default. Replace the list with a single entry, `default`, which is a name the API itself answers to: vLLM takes it as a second --served-model-name and still reports the real slug, being first in the list; the MLX proxy accepts it as an alias. So `llmhost/default` survives a switch. The local entry is labelled with whatever is loaded, refreshed by both switch paths; a remote block is pasted once and never refreshed, so it names no model rather than make a claim that goes stale. Existing configs point at a specific model, which this provider no longer has and opencode would reject, so configure() and sync() migrate `<prov>/<model>` to `<prov>/default`. Other providers' defaults are untouched, and an older remote block that still lists many models keeps working, including as the fallback when the local provider is removed. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
51c2880a2e
commit
9af7f1e545
6 changed files with 124 additions and 50 deletions
|
|
@ -164,8 +164,9 @@ def _uncached(usage):
|
|||
|
||||
|
||||
def make_handler(inner_port, served_name, context, key, stats, aliases=(), reasoning=None):
|
||||
# Names a client may call the loaded model: its llm-host name and its repo id.
|
||||
accepted = {served_name.lower(), *(a.lower() for a in aliases if a)}
|
||||
# Names a client may call the loaded model: its llm-host name, its repo id, and the
|
||||
# stable alias that means "whatever is loaded" (plan.SERVED_ALIAS).
|
||||
accepted = {served_name.lower(), plan.SERVED_ALIAS, *(a.lower() for a in aliases if a)}
|
||||
|
||||
class Proxy(BaseHTTPRequestHandler):
|
||||
protocol_version = "HTTP/1.1"
|
||||
|
|
@ -222,7 +223,9 @@ def make_handler(inner_port, served_name, context, key, stats, aliases=(), reaso
|
|||
self._json(404, {"error": {
|
||||
"message": f"model '{want}' is not loaded; this server is serving "
|
||||
f"'{served_name}'. Load it with `llmhost switch {want}` "
|
||||
f"(one model is loaded at a time), or ask for '{served_name}'.",
|
||||
f"(one model is loaded at a time), or ask for '{served_name}' "
|
||||
f"-- or for '{plan.SERVED_ALIAS}', which always means whatever "
|
||||
f"is loaded.",
|
||||
"type": "invalid_request_error", "code": "model_not_found", "param": "model"}})
|
||||
return False
|
||||
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import subprocess
|
|||
from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
from . import paths
|
||||
from . import paths, plan
|
||||
|
||||
PROVIDER = "llmhost"
|
||||
DISPLAY = "LLMHost"
|
||||
|
|
@ -113,24 +113,28 @@ def key_ref(key_path):
|
|||
return "{file:" + str(key_path) + "}"
|
||||
|
||||
|
||||
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 model_label(active, label, name_it):
|
||||
"""What opencode shows for the single entry. One model is loaded at a time, so there is
|
||||
nothing to choose here -- the label's job is to say which one answers right now. Only
|
||||
where llm-host keeps the block in sync, though: a remote block is pasted once and never
|
||||
refreshed, so naming a model there would be a claim that goes stale on the next switch."""
|
||||
if name_it and active:
|
||||
return f"Default model \u00b7 {active.get('title') or active['name']} ({label})"
|
||||
return f"Default model ({label})"
|
||||
|
||||
|
||||
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.
|
||||
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
|
||||
models[m["name"]] = entry
|
||||
# A single entry, keyed by the alias the API also answers to. Listing every model that
|
||||
# fits would be a menu of four things that 404 and one that works, and naming the loaded
|
||||
# one would break every client's saved default on each `llmhost switch`.
|
||||
active = next((m for m in cfg["models"] if m["name"] == cfg.get("active")), None)
|
||||
ctx = (active or {}).get("est_context") or min(
|
||||
(m["est_context"] for m in cfg["models"]), default=32768)
|
||||
entry = {"name": model_label(active, label, mark_loaded), "tool_call": True,
|
||||
"limit": {"context": ctx, "output": min(65536, ctx // 2)}}
|
||||
if (active or {}).get("reasoning"):
|
||||
entry["reasoning"] = True
|
||||
models = {plan.SERVED_ALIAS: entry}
|
||||
return {
|
||||
"npm": "@ai-sdk/openai-compatible",
|
||||
"name": f"{DISPLAY} ({label})",
|
||||
|
|
@ -270,7 +274,9 @@ def configure(cfg):
|
|||
existed = prov in providers
|
||||
providers[prov] = local_block(cfg)
|
||||
if "model" not in data:
|
||||
data["model"] = f"{prov}/{cfg['active']}"
|
||||
data["model"] = f"{prov}/{plan.SERVED_ALIAS}"
|
||||
else:
|
||||
_retarget_model(data, prov)
|
||||
# Unconditional: a cached key for our provider is stale or redundant either way.
|
||||
cleared = bool(clear_auth(prov))
|
||||
if json.dumps(data, sort_keys=True) == before:
|
||||
|
|
@ -280,6 +286,18 @@ def configure(cfg):
|
|||
return ("updated" if existed else "added"), path, prov, cleared
|
||||
|
||||
|
||||
def _retarget_model(data, prov):
|
||||
"""Move a default that named one of our models onto the alias. Our provider used to
|
||||
list every model that fits, so an existing config can point at `llmhost/qwen3.8-27b`
|
||||
-- an id this provider no longer has, which opencode would reject outright."""
|
||||
model = data.get("model") or ""
|
||||
their_prov, _, their_model = model.partition("/")
|
||||
if their_prov == prov and their_model != plan.SERVED_ALIAS:
|
||||
data["model"] = f"{prov}/{plan.SERVED_ALIAS}"
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
def _pick_fallback_model(providers, removed):
|
||||
"""A `model` value from a surviving llm-host provider: its first model, which is the
|
||||
one its own plan preferred, rather than whichever name happens to sort first.
|
||||
|
|
@ -360,6 +378,7 @@ def sync(cfg):
|
|||
prov = mine[0]
|
||||
before = json.dumps(data, sort_keys=True)
|
||||
providers[prov] = local_block(cfg)
|
||||
_retarget_model(data, prov)
|
||||
if json.dumps(data, sort_keys=True) == before:
|
||||
return None
|
||||
_write(path, data, backup=False)
|
||||
|
|
|
|||
|
|
@ -120,7 +120,7 @@ def catalog_entry(catalog, name):
|
|||
def vllm_args(entry, choice, port, tp):
|
||||
"""Arguments after `vllm serve`."""
|
||||
v = entry.get("vllm", {})
|
||||
args = [choice["repo"], "--served-model-name", entry["name"],
|
||||
args = [choice["repo"], "--served-model-name", entry["name"], SERVED_ALIAS,
|
||||
"--host", "0.0.0.0", "--port", str(port),
|
||||
"--max-model-len", "-1", # let vLLM fit the context to free memory (capped at native)
|
||||
"--gpu-memory-utilization", f"{choice['util']:.2f}",
|
||||
|
|
@ -162,6 +162,14 @@ def mlx_args(entry, choice, port):
|
|||
return args
|
||||
|
||||
|
||||
# A second name the API answers to, besides the model's own. opencode (and any client
|
||||
# with a saved default) points at this instead of a specific model, so the choice keeps
|
||||
# working after `llmhost switch` -- only one model is loaded at a time, so naming one in
|
||||
# a client config only ever goes stale. vLLM takes it as a second --served-model-name and
|
||||
# still reports the real slug, being the first; the MLX proxy accepts it as an alias.
|
||||
SERVED_ALIAS = "default"
|
||||
|
||||
|
||||
# ---- reasoning effort -------------------------------------------------------------
|
||||
# Not a backend flag: it's a chat-template kwarg, read per request. "default" means send
|
||||
# nothing and let the template decide; "off" suppresses thinking; anything else is an
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue