Hand out the right opencode command: the API's public URL, and setup uses the install script
install-newt now asks for the API's public URL first (--api-url) and records it; it names the Pangolin resource and refuses an endpoint on the same host. expose-dashboard records only that the dashboard is public. One rule (opencode.client_targets) decides which opencode-install.sh commands are shown -- local only without a tunnel; internet and local with one; internet only once the dashboard is public -- and setup's closing message, `llmhost opencode-config` and the dashboard's Connect dialog all use it. Setup's offer to install opencode now goes through opencode-install.sh --install-only, a new mode that only installs opencode; this machine's own provider is still configured by llm-host, which keeps it in sync on every switch. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
c8b70afdbf
commit
3e9ebeed42
10 changed files with 266 additions and 109 deletions
|
|
@ -17,6 +17,7 @@ never overwritten. Other providers and settings are left untouched.
|
|||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
|
@ -28,7 +29,8 @@ PROVIDER = "llmhost"
|
|||
DISPLAY = "LLMHost"
|
||||
LOCAL_LABEL = "Local"
|
||||
LOCALHOSTS = ("localhost", "127.0.0.1", "::1", "[::1]")
|
||||
INSTALL_URL = "https://opencode.ai/install"
|
||||
INSTALL_SCRIPT = paths.SRC / "opencode-install.sh"
|
||||
DEFAULT_REPO = "https://git.neilbantoc.dev/pub/llm-host.git" # install.sh's default too
|
||||
|
||||
|
||||
def find():
|
||||
|
|
@ -40,12 +42,70 @@ def find():
|
|||
|
||||
|
||||
def install(log=print):
|
||||
"""Run opencode's official installer (installs to ~/.opencode/bin, adds it to PATH)."""
|
||||
log(f"$ curl -fsSL {INSTALL_URL} | bash")
|
||||
r = subprocess.run(["bash", "-c", f"set -o pipefail; curl -fsSL {INSTALL_URL} | bash"])
|
||||
"""Install opencode the way every machine does: through llm-host's opencode-install.sh,
|
||||
which runs opencode's official installer (into ~/.opencode/bin, added to PATH). Only
|
||||
the install step: this machine's provider is configure()'s, since it reads the key
|
||||
file in place and is kept in sync on every switch."""
|
||||
log(f"$ {INSTALL_SCRIPT.name} --install-only")
|
||||
r = subprocess.run(["bash", str(INSTALL_SCRIPT), "--install-only"])
|
||||
return find() if r.returncode == 0 else None
|
||||
|
||||
|
||||
def raw_file_url(repo, ref, name):
|
||||
"""Where a file in `repo` at `ref` is fetched raw: GitHub's raw host, or the
|
||||
/raw/branch/ path Gitea and Forgejo use. None for a remote that isn't https."""
|
||||
m = re.match(r"^https://([^/]+)/(.+?)(\.git)?/?$", repo or "")
|
||||
if not m:
|
||||
return None
|
||||
host, path = m.group(1), m.group(2)
|
||||
if host.lower() == "github.com":
|
||||
return f"https://raw.githubusercontent.com/{path}/{ref}/{name}"
|
||||
return f"https://{host}/{path}/raw/branch/{ref}/{name}"
|
||||
|
||||
|
||||
_installer = None
|
||||
|
||||
|
||||
def installer_url():
|
||||
"""opencode-install.sh's URL in the repo this server was installed from, so the command
|
||||
handed to other machines follows a fork and needs nothing but the API to work. Falls
|
||||
back to the main repo."""
|
||||
global _installer
|
||||
if _installer is None:
|
||||
def git(*args):
|
||||
try:
|
||||
r = subprocess.run(["git", "-C", str(paths.SRC), *args], capture_output=True, text=True, timeout=5)
|
||||
return r.stdout.strip() if r.returncode == 0 else ""
|
||||
except (OSError, subprocess.TimeoutExpired):
|
||||
return ""
|
||||
ref = git("rev-parse", "--abbrev-ref", "HEAD")
|
||||
_installer = (raw_file_url(git("config", "--get", "remote.origin.url"),
|
||||
ref if ref and ref != "HEAD" else "main", INSTALL_SCRIPT.name)
|
||||
or raw_file_url(DEFAULT_REPO, "main", INSTALL_SCRIPT.name))
|
||||
return _installer
|
||||
|
||||
|
||||
def remote_command(base_url):
|
||||
"""The one line another machine runs to connect its opencode to this server."""
|
||||
return f'/bin/bash -c "$(curl -fsSL {installer_url()})" -- --url {base_url}'
|
||||
|
||||
|
||||
def client_targets(cfg, lan_host):
|
||||
"""Where other machines reach the API, as [(where, base URL)], most useful first.
|
||||
|
||||
The API's public address (recorded by `llmhost install-newt`) when there is one, and
|
||||
the local-network address too unless the dashboard is public -- a public dashboard
|
||||
is seen from outside, where a LAN address is only noise."""
|
||||
lan = ("on your local network", f"http://{lan_host}:{cfg['api_port']}")
|
||||
public = (cfg.get("newt") or {}).get("api_url")
|
||||
if not public:
|
||||
return [lan]
|
||||
out = [("from the internet", public)]
|
||||
if not cfg.get("dashboard_public"):
|
||||
out.append(lan)
|
||||
return out
|
||||
|
||||
|
||||
def config_dir():
|
||||
return Path(os.environ.get("XDG_CONFIG_HOME") or paths.HOME / ".config") / "opencode"
|
||||
|
||||
|
|
@ -139,7 +199,7 @@ def effort_variants():
|
|||
return {level: {"disabled": True} for level in OPENCODE_EFFORTS}
|
||||
|
||||
|
||||
def provider_block(cfg, host, key_path, label, mark_loaded=False):
|
||||
def provider_block(cfg, host, key_path, label, mark_loaded=False, base_url=None):
|
||||
# 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`.
|
||||
|
|
@ -157,7 +217,7 @@ def provider_block(cfg, host, key_path, label, mark_loaded=False):
|
|||
return {
|
||||
"npm": "@ai-sdk/openai-compatible",
|
||||
"name": f"{DISPLAY} ({label})",
|
||||
"options": {"baseURL": f"http://{host}:{cfg['api_port']}/v1", "apiKey": key_ref(key_path)},
|
||||
"options": {"baseURL": base_url or f"http://{host}:{cfg['api_port']}/v1", "apiKey": key_ref(key_path)},
|
||||
"models": models,
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue