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:
neil 2026-11-06 18:35:41 -08:00
parent c8b70afdbf
commit 3e9ebeed42
10 changed files with 266 additions and 109 deletions

View file

@ -173,7 +173,9 @@ pre {
.dialog .actions { display: flex; justify-content: flex-end; }
.dialog details.manual { margin-bottom: 12px; font-size: 13px; }
.dialog details.manual summary { cursor: pointer; color: var(--muted); margin-bottom: 8px; }
#cx-install { white-space: pre-wrap; overflow-wrap: anywhere; }
.dialog .snip pre.cmd { white-space: pre-wrap; overflow-wrap: anywhere; }
#cx-installs .where { font-size: 12.5px; color: var(--muted); margin: 0 0 4px; }
#cx-installs .snip { margin: 0 0 14px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg);
padding: 8px 14px; border-radius: 8px; font-size: 13px; opacity: 0; transition: opacity .25s; pointer-events: none; }
.toast.show { opacity: 1; }
@ -308,15 +310,12 @@ pre {
<div class="keyrow"><input id="cx-key" type="password" readonly autocomplete="off" aria-label="API key"><button id="cx-show">Show</button><button id="cx-copy">Copy</button></div>
<h4>Add to opencode</h4>
<p>The API's address, as the machine running opencode reaches it:</p>
<div class="keyrow"><input id="cx-url" type="url" spellcheck="false" autocomplete="off"
placeholder="https://llm-api.example.com" aria-label="API base URL"></div>
<p>Then run this there, or send it to whoever you're giving access, with the key. It needs only the API, not this
dashboard: it installs opencode if it's missing, asks for the key (hidden), saves it, and adds this server to
<code>opencode.json</code>, keeping their other settings:</p>
<div class="snip" style="margin-bottom:14px"><pre id="cx-install"></pre><button class="small" data-copy="cx-install">Copy</button></div>
<p>Run this on the machine with opencode, or send it to whoever you're giving access, with the key. It needs
only the API, not this dashboard: it installs opencode if it's missing, asks for the key (hidden), saves it,
and adds this server to <code>opencode.json</code>, keeping their other settings.</p>
<div id="cx-installs"></div>
<details class="manual">
<summary>Or set it up by hand</summary>
<summary id="cx-manual-title">Or set it up by hand</summary>
<ol>
<li>Save the key where the provider reads it (paste it at the prompt; it stays out of your shell history):
<div class="snip"><pre id="cx-keycmd"></pre><button class="small" data-copy="cx-keycmd">Copy</button></div></li>
@ -427,7 +426,22 @@ function closeConnect() {
// filled in here: baseURL, and the provider id and label named after its host.
function renderOpencode() {
if (!CONNECT) return;
let url = $("cx-url").value.trim().replace(/\/+$/, "");
// One command per address the API is reached at (server's choice; see client_targets).
const box = $("cx-installs"); box.textContent = "";
for (const t of CONNECT.targets) {
if (CONNECT.targets.length > 1) {
const w = document.createElement("div"); w.className = "where";
w.textContent = t.where[0].toUpperCase() + t.where.slice(1) + ":"; box.appendChild(w);
}
const snip = document.createElement("div"); snip.className = "snip";
const pre = document.createElement("pre"); pre.className = "cmd"; pre.textContent = t.command;
const b = document.createElement("button"); b.className = "small"; b.textContent = "Copy";
b.onclick = async () => toast(await copyText(t.command) ? "Copied" : "Couldn't copy; select it and copy by hand");
snip.append(pre, b); box.appendChild(snip);
}
// By hand: for the first (most useful) address.
let url = CONNECT.targets[0].url.replace(/\/+$/, "");
$("cx-manual-title").textContent = `Or set it up by hand (for ${url})`;
let host = "";
try { host = new URL(url).hostname; } catch (e) {}
if (url && !/\/v1$/.test(url)) url += "/v1";
@ -450,27 +464,16 @@ function renderOpencode() {
// One line: pasted as two, the first line's `read` would swallow the second as the key.
`mkdir -p ~/.config/opencode && printf 'API key: ' && stty -echo && read -r k; stty echo; echo; ` +
`[ -n "$k" ] && (umask 077 && printf '%s\\n' "$k" > ${keyfile}) && echo "saved ${keyfile}"; unset k`;
$("cx-install").textContent = url
? `/bin/bash -c "$(curl -fsSL ${CONNECT.installer})" -- --url ${url.replace(/\/v1$/, "")}`
: "Type the API's address above first.";
$("cx-pick").textContent = `${block.name} → ${Object.values(block.models)[0]?.name || CONNECT.model}`;
try { localStorage.setItem("llmhost-api-url", $("cx-url").value.trim()); } catch (e) {}
}
$("connect").onclick = async () => {
let c;
try { c = await api("/api/connect"); } catch (e) { if (e.message !== "unauthorized") toast("Couldn't load: " + e.message); return; }
CONNECT = c;
$("cx-key").value = c.key;
// The public API address can't be known here, so remember what the viewer typed; on a
// plain-http LAN visit the API is on this same host, at its own port.
let saved = "";
try { saved = localStorage.getItem("llmhost-api-url") || ""; } catch (e) {}
if (!saved && location.protocol === "http:") saved = `http://${location.hostname}:${c.api_port}/v1`;
$("cx-url").value = saved;
renderOpencode();
$("connect-modal").classList.add("show"); $("cx-close").focus();
};
$("cx-url").addEventListener("input", renderOpencode);
document.querySelectorAll("#connect-modal [data-copy]").forEach((b) => {
b.onclick = async () => toast(await copyText($(b.dataset.copy).textContent) ? "Copied" : "Couldn't copy; select it and copy by hand");
});

View file

@ -417,42 +417,8 @@ class Sampler:
threading.Thread(target=fn, args=("model",), daemon=True).start()
DEFAULT_REPO = "https://git.neilbantoc.dev/pub/llm-host.git" # install.sh's default too
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():
"""The opencode installer's URL in the repo this server was installed from, so the
command the Connect dialog gives out works for people who can't reach this dashboard
(an API-only tunnel) and follows a fork. Falls back to the main repo."""
global _installer
if _installer is None:
import subprocess
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", "opencode-install.sh")
or raw_file_url(DEFAULT_REPO, "main", "opencode-install.sh"))
return _installer
# The opencode installer's URL is worked out once, from the repo this was installed from.
raw_file_url, installer_url = opencode.raw_file_url, opencode.installer_url
def make_handler(sampler, key, oidc=None, otp=None, key_login=True):
@ -602,17 +568,19 @@ def make_handler(sampler, key, oidc=None, otp=None, key_login=True):
if self.authed():
self.send_json(200, sampler.snapshot())
elif path == "/api/connect":
# What the Connect dialog hands to clients: the key, and opencode's provider
# block from the same builder as `llmhost opencode-config`. The public API
# address can't be known here, so the page fills in baseURL, the provider id
# and its label from what the viewer types. Behind the same sign-in as
# everything else; send_json marks it no-store.
# What the Connect dialog hands to clients: the key; the API's addresses with
# the opencode-install.sh command for each, chosen by the same rule as setup and
# `llmhost opencode-config` (opencode.client_targets); and opencode's provider
# block for setting it up by hand, whose address-dependent parts the page fills
# in. Behind the same sign-in as everything else; send_json marks it no-store.
if self.authed():
cfg = paths.load_config() or sampler.cfg
h = socket.gethostname()
targets = [{"where": w, "url": u, "command": opencode.remote_command(u)}
for w, u in opencode.client_targets(cfg, h if "." in h else h + ".local")]
block = opencode.provider_block(cfg, "HOST", "KEYFILE", "LABEL")
self.send_json(200, {"key": key, "api_port": cfg["api_port"],
"model": plan.SERVED_ALIAS, "opencode": block,
"installer": installer_url()})
self.send_json(200, {"key": key, "model": plan.SERVED_ALIAS, "opencode": block,
"targets": targets})
elif path == "/api/logs":
if self.authed():
lines = [l for l in service.logs("model", 1500)