opencode: name providers LLMHost (Local) / LLMHost (<host>), identify by key file
The local and remote setup paths already used distinct provider ids, so one
opencode config can point at a local server and any number of remote ones.
Name them after the machine they reach so they're tellable apart in /models.
Decide which block is *this* machine's by the key-file reference it carries
rather than by its name, so:
- a provider renamed by hand is still recognised and updated in place
instead of a second one being added next to it;
- a remote provider sitting under the `llmhost` id is no longer silently
overwritten with a localhost baseURL (setup takes `llmhost-local` and
says so) -- the one-shot .before-llmhost backup could not recover it;
- uninstall removes only the local provider, never `llmhost-*` blocks for
servers that are still running, and never providers it didn't create.
Add `llmhost opencode-remove` for the case the servers can't handle for each
other: uninstalling on a server can't reach the clients pointing at it. Wire
uninstall to it, repointing opencode's default model at a surviving llm-host
provider and dropping `model` only when none is left.
Also split config read/write out of configure() and report `unparsable`: a
syntactically broken opencode.json used to raise ValueError mid-setup.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
a250812d42
commit
504ea3018a
3 changed files with 264 additions and 47 deletions
|
|
@ -209,15 +209,19 @@ def setup_opencode(cfg, ask, install_ok=False, later="llmhost opencode-setup"):
|
|||
say("opencode's installer failed; see the output above. Retry later with: llmhost opencode-setup")
|
||||
return
|
||||
say("opencode installed to ~/.opencode/bin (open a new terminal, or run: export PATH=\"$HOME/.opencode/bin:$PATH\")")
|
||||
status, path = opencode.configure(cfg)
|
||||
if status == "has-comments":
|
||||
say(f"\n{path} has comments, which an automatic edit would delete, so it was left as is.\n"
|
||||
f"Add this to its \"provider\" section yourself:\n{opencode.snippet(cfg)}")
|
||||
status, path, prov = opencode.configure(cfg)
|
||||
if status in ("has-comments", "unparsable"):
|
||||
why = "has comments, which an automatic edit would delete" if status == "has-comments" else "isn't valid JSON"
|
||||
say(f"\n{path} {why}, so it was left as is.\n"
|
||||
f"Add this to its \"provider\" section yourself:\n{opencode.snippet(cfg, prov)}")
|
||||
return
|
||||
msg = {"added": "Added", "updated": "Updated", "unchanged": "Already up to date:"}[status]
|
||||
say(f"\nopencode: {msg} the `{opencode.PROVIDER}` provider in {path} (http://localhost:{cfg['api_port']}/v1).")
|
||||
label = f"{opencode.DISPLAY} ({opencode.LOCAL_LABEL})"
|
||||
say(f"\nopencode: {msg} the `{prov}` provider \u2014 {label} \u2014 in {path} (http://localhost:{cfg['api_port']}/v1).")
|
||||
if prov != opencode.PROVIDER:
|
||||
say(f"(`{opencode.PROVIDER}` is taken by another provider, which was left alone.)")
|
||||
if status == "added":
|
||||
say(f"Start it with: opencode (pick a model with /models; this server answers as {opencode.PROVIDER}/{cfg['active']})")
|
||||
say(f"Start it with: opencode (pick a model with /models; this server answers as {prov}/{cfg['active']})")
|
||||
|
||||
|
||||
def cmd_opencode_setup(a):
|
||||
|
|
@ -459,11 +463,11 @@ def cmd_download(a):
|
|||
def cmd_opencode(a):
|
||||
cfg = _service_check()
|
||||
host = a.host or default_host()
|
||||
prov = a.provider or f"llmhost-{host.split('.')[0]}"
|
||||
keyfile = f"~/.config/opencode/{prov}.key"
|
||||
prov = a.provider or opencode.remote_provider(host)
|
||||
keyfile = opencode.remote_key_file(prov)
|
||||
snippet = {
|
||||
"$schema": "https://opencode.ai/config.json",
|
||||
"provider": {prov: opencode.provider_block(cfg, host, keyfile, host.split(".")[0])},
|
||||
"provider": {prov: opencode.provider_block(cfg, host, keyfile, host)},
|
||||
"model": f"{prov}/{cfg['active']}",
|
||||
}
|
||||
if a.json:
|
||||
|
|
@ -480,7 +484,10 @@ On another machine running opencode:
|
|||
2. Merge this into ~/.config/opencode/opencode.json:
|
||||
""")
|
||||
say(json.dumps(snippet, indent=2))
|
||||
say(f"\nOnly the active model answers at a time; switch with `llmhost switch <name>` or the dashboard.")
|
||||
say(f"""
|
||||
It shows up in opencode as "{opencode.DISPLAY} ({host})". Only the active model answers at a
|
||||
time; switch with `llmhost switch <name>` or the dashboard. If this server goes away, remove
|
||||
the block there with: llmhost opencode-remove --host {host}""")
|
||||
|
||||
|
||||
def cmd_update(a):
|
||||
|
|
@ -526,6 +533,14 @@ def cmd_uninstall(a):
|
|||
for d in ("venv-vllm", "venv-mlx", "cuda", "nvcc-test"):
|
||||
shutil.rmtree(paths.DATA / d, ignore_errors=True)
|
||||
say("Removed services and backend environments.")
|
||||
# Before the --purge below: the provider is identified by its reference to KEY_FILE.
|
||||
oc_status, oc_path, oc_removed, oc_model = opencode.remove(local=True)
|
||||
if oc_status == "removed":
|
||||
say(f"opencode: removed {', '.join('`' + r + '`' for r in oc_removed)} from {oc_path}."
|
||||
+ (f" Its default model is now {oc_model}." if oc_model else ""))
|
||||
elif oc_status in ("has-comments", "unparsable"):
|
||||
why = "has comments" if oc_status == "has-comments" else "isn't valid JSON"
|
||||
say(f"opencode: {oc_path} {why}, so it was left alone; remove the llm-host provider by hand.")
|
||||
if a.purge:
|
||||
shutil.rmtree(paths.CONFIG_DIR, ignore_errors=True)
|
||||
shutil.rmtree(paths.DATA, ignore_errors=True)
|
||||
|
|
@ -554,6 +569,22 @@ def cmd_uninstall(a):
|
|||
+ " ".join(cmd).replace(str(paths.HOME), "~"))
|
||||
|
||||
|
||||
def cmd_opencode_remove(a):
|
||||
explicit = bool(a.provider or a.host or a.all)
|
||||
status, path, removed, new_model = opencode.remove(
|
||||
prov_ids=[a.provider] if a.provider else None,
|
||||
local=not explicit, host=a.host, every=a.all)
|
||||
if status in ("has-comments", "unparsable"):
|
||||
why = "has comments" if status == "has-comments" else "isn't valid JSON"
|
||||
raise SystemExit(f"{path} {why}; edit it by hand.")
|
||||
if status == "none":
|
||||
say(f"No matching llm-host provider in {path}.")
|
||||
return
|
||||
say(f"Removed {', '.join('`' + r + '`' for r in removed)} from {path}.")
|
||||
if new_model:
|
||||
say(f"opencode's default model is now {new_model}.")
|
||||
|
||||
|
||||
def cmd_run_model(a):
|
||||
cfg = _service_check()
|
||||
cat = paths.load_catalog()
|
||||
|
|
@ -626,6 +657,12 @@ def main(argv=None):
|
|||
s.add_argument("--provider", help="opencode provider id")
|
||||
s.add_argument("--json", action="store_true", help="print only the JSON")
|
||||
s.set_defaults(fn=cmd_opencode)
|
||||
s = sub.add_parser("opencode-remove", help="remove llm-host providers from this machine's opencode config")
|
||||
s.add_argument("--host", help="remove the provider pointing at this host (a retired remote server)")
|
||||
s.add_argument("--provider", help="remove this provider id")
|
||||
s.add_argument("--all", action="store_true", help="remove every llm-host provider, local and remote")
|
||||
s.set_defaults(fn=cmd_opencode_remove)
|
||||
|
||||
s = sub.add_parser("update", help="pull the latest llm-host and re-plan")
|
||||
s.add_argument("--backend", action="store_true", help="also reinstall/upgrade the backend")
|
||||
s.set_defaults(fn=cmd_update)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue