install-newt now sets up the tunnel for the API only; the dashboard stays on the LAN. Dashboard sign-in is its own step: install-oidc (the OIDC flow, split out) or install-otp, which pairs authenticator apps with a QR code in the terminal (stdlib QR encoder, TOTP per RFC 6238) so a code also opens the dashboard, with replay and lockout rules. expose-dashboard walks through publishing the dashboard, only after install-newt, and warns while it's protected by the API key alone; with codes it offers to switch the key off for the dashboard. Each install-* has a remove-*. Also opencode-install.sh: a standalone script, fetched from the repo, that installs opencode if needed, saves the API key to a 0600 file and adds the provider to opencode.json for new and existing configs. /v1/models reports whether the model reasons, for that script. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
96 lines
3.3 KiB
Python
96 lines
3.3 KiB
Python
"""Filesystem layout and config persistence.
|
|
|
|
Layout (overridable with LLMHOST_HOME / LLMHOST_CONFIG):
|
|
~/.local/share/llm-host/src this repo (cloned by install.sh)
|
|
~/.local/share/llm-host/venv-* backend virtualenvs
|
|
~/.local/share/llm-host/cuda merged CUDA toolkit built from pip wheels (NVIDIA)
|
|
~/.config/llm-host/config.json plan + settings
|
|
~/.config/llm-host/api_key API key (0600)
|
|
~/.local/share/llm-host/newt newt binary, after `llmhost install-newt`
|
|
~/.config/llm-host/newt_secret Pangolin site secret (0600), after install-newt
|
|
~/.config/llm-host/oidc_secret dashboard OIDC client secret (0600), after install-oidc
|
|
~/.config/llm-host/otp_secret authenticator-app secret (0600), after install-otp
|
|
"""
|
|
import json
|
|
import os
|
|
import platform
|
|
import secrets
|
|
import tempfile
|
|
from pathlib import Path
|
|
|
|
HOME = Path.home()
|
|
IS_MAC = platform.system() == "Darwin"
|
|
SRC = Path(__file__).resolve().parent.parent
|
|
DATA = Path(os.environ.get("LLMHOST_HOME", HOME / ".local/share/llm-host"))
|
|
CONFIG_DIR = Path(os.environ.get("LLMHOST_CONFIG", HOME / ".config/llm-host"))
|
|
CONFIG_FILE = CONFIG_DIR / "config.json"
|
|
KEY_FILE = CONFIG_DIR / "api_key"
|
|
OIDC_SECRET_FILE = CONFIG_DIR / "oidc_secret"
|
|
NEWT_SECRET_FILE = CONFIG_DIR / "newt_secret"
|
|
OTP_SECRET_FILE = CONFIG_DIR / "otp_secret"
|
|
NEWT_DIR = DATA / "newt"
|
|
LOG_DIR = (HOME / "Library/Logs/llm-host") if IS_MAC else (DATA / "logs")
|
|
BIN_DIR = HOME / ".local/bin"
|
|
SHIM = BIN_DIR / "llmhost"
|
|
CATALOG_FILE = Path(__file__).resolve().parent / "catalog.json"
|
|
|
|
|
|
def hf_hub_dir() -> Path:
|
|
if os.environ.get("HF_HUB_CACHE"):
|
|
return Path(os.environ["HF_HUB_CACHE"])
|
|
if os.environ.get("HF_HOME"):
|
|
return Path(os.environ["HF_HOME"]) / "hub"
|
|
return HOME / ".cache/huggingface/hub"
|
|
|
|
|
|
def load_config() -> dict:
|
|
try:
|
|
return json.loads(CONFIG_FILE.read_text())
|
|
except (OSError, ValueError):
|
|
return {}
|
|
|
|
|
|
def save_config(cfg: dict) -> None:
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
|
fd, tmp = tempfile.mkstemp(dir=CONFIG_DIR, prefix=".config.")
|
|
with os.fdopen(fd, "w") as f:
|
|
json.dump(cfg, f, indent=2)
|
|
f.write("\n")
|
|
os.replace(tmp, CONFIG_FILE)
|
|
|
|
|
|
def load_catalog() -> dict:
|
|
return json.loads(CATALOG_FILE.read_text())
|
|
|
|
|
|
def api_key(create: bool = False) -> str:
|
|
if KEY_FILE.exists():
|
|
return KEY_FILE.read_text().strip()
|
|
if not create:
|
|
raise SystemExit(f"No API key at {KEY_FILE}; run `llmhost setup` first.")
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
|
key = "sk-" + secrets.token_urlsafe(32)
|
|
fd = os.open(KEY_FILE, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
with os.fdopen(fd, "w") as f:
|
|
f.write(key + "\n")
|
|
return key
|
|
|
|
|
|
|
|
def read_secret(path, what, fix) -> str:
|
|
try:
|
|
return path.read_text().strip()
|
|
except OSError:
|
|
raise SystemExit(f"No {what} at {path}; run `{fix}` again.")
|
|
|
|
|
|
def save_secret(path, secret: str) -> None:
|
|
CONFIG_DIR.mkdir(parents=True, exist_ok=True)
|
|
fd = os.open(path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
|
with os.fdopen(fd, "w") as f:
|
|
f.write(secret + "\n")
|
|
os.chmod(path, 0o600) # O_CREAT's mode doesn't apply to a file that already existed
|
|
|
|
|
|
def oidc_secret() -> str:
|
|
return read_secret(OIDC_SECRET_FILE, "dashboard sign-in client secret", "llmhost install-oidc")
|