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
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue