feat: model registry UI — hosts, models, role assignments
Replaces the single-host local model settings page with a full model
registry interface at /settings/local.
Hosts section:
- List existing hosts with inline edit + save + remove
- Collapsible "Add host" form
- Per-host "Fetch models" button
Models section:
- List all models with label, model name, host, context_k badge, tags
- Remove button
Add Model section:
- Host dropdown, label, model name, context_k, tags (comma-separated)
- "Fetch models from host" with auto-fill picker
Role Assignments section:
- One row per defined role (chat, orchestrator, distill, coder, research)
- Primary + backup_1 + backup_2 dropdowns per role
- Dropdowns pre-filled from registry on load
- AJAX save on change (POST /api/models/role) with toast confirmation
- Built-in models (claude_cli, gemini_cli, gemini_api) always available in dropdowns
Backend:
- All user_settings references replaced with model_registry
- host/{id}/remove route added
- fetch-models now accepts host_id query param
- POST /api/models/role for AJAX role assignment
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,13 +1,14 @@
|
||||
"""
|
||||
Local LLM settings — per-user host and model configuration.
|
||||
Model Registry settings — hosts, models, and role assignments.
|
||||
|
||||
Routes:
|
||||
GET /settings/local → settings page
|
||||
POST /settings/local/host → save/create host
|
||||
POST /settings/local/models/add → add model entry
|
||||
POST /settings/local/models/{id}/activate → set active model
|
||||
POST /settings/local/models/{id}/remove → remove model entry
|
||||
GET /api/local-llm/fetch-models → proxy to host /api/models (JSON)
|
||||
GET /settings/local → settings page
|
||||
POST /settings/local/host → save/create a host
|
||||
POST /settings/local/host/{id}/remove → remove a host (and its models)
|
||||
POST /settings/local/models/add → add a model entry
|
||||
POST /settings/local/models/{id}/remove → remove a model
|
||||
POST /api/models/role → AJAX: set a role assignment
|
||||
GET /api/local-llm/fetch-models → proxy to host /api/models (JSON)
|
||||
"""
|
||||
import logging
|
||||
from pathlib import Path
|
||||
@@ -19,7 +20,7 @@ from fastapi.responses import HTMLResponse, JSONResponse, RedirectResponse
|
||||
|
||||
from auth_utils import COOKIE_NAME, decode_token
|
||||
from config import settings as app_settings
|
||||
import user_settings as us
|
||||
import model_registry as reg
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
router = APIRouter()
|
||||
@@ -42,81 +43,141 @@ def _get_user(request: Request) -> str | None:
|
||||
# ── Page renderer ─────────────────────────────────────────────────────────────
|
||||
|
||||
def _render(username: str, success: str = "", error: str = "") -> str:
|
||||
cfg = us.get_config(username)
|
||||
hosts = cfg["hosts"]
|
||||
models = cfg["models"]
|
||||
active = cfg.get("active_model_id")
|
||||
registry = reg.get_registry(username)
|
||||
hosts = registry.get("hosts", [])
|
||||
models = registry.get("models", [])
|
||||
roles = registry.get("roles", {})
|
||||
builtins = reg._builtins()
|
||||
|
||||
# Build a host lookup for model rows
|
||||
host_by_id = {h["id"]: h for h in hosts}
|
||||
|
||||
# ── Host section ──────────────────────────────────────────────────────────
|
||||
if hosts:
|
||||
h = hosts[0] # one host for now
|
||||
host_id_val = h["id"]
|
||||
host_label = h.get("label", "")
|
||||
host_url = h.get("api_url", "")
|
||||
host_key_hint = f"…{h['api_key'][-4:]}" if h.get("api_key") else "not set"
|
||||
else:
|
||||
host_id_val = ""
|
||||
host_label = ""
|
||||
host_url = app_settings.local_api_url
|
||||
host_key_hint = f"server default (…{app_settings.local_api_key[-4:]})" \
|
||||
if app_settings.local_api_key else "not set"
|
||||
# ── Host rows ─────────────────────────────────────────────────────────────
|
||||
host_rows = ""
|
||||
for h in hosts:
|
||||
key_hint = f"…{h['api_key'][-4:]}" if h.get("api_key") else "not set"
|
||||
host_rows += f'''
|
||||
<div class="host-row">
|
||||
<form method="POST" action="/settings/local/host" class="host-form">
|
||||
<input type="hidden" name="host_id" value="{h["id"]}">
|
||||
<div class="field-row">
|
||||
<div class="field">
|
||||
<label>Label</label>
|
||||
<input type="text" name="label" value="{h.get("label","")}"
|
||||
placeholder="Home ML Laptop" autocomplete="off" data-form-type="other">
|
||||
</div>
|
||||
<div class="field" style="flex:2">
|
||||
<label>API URL</label>
|
||||
<input type="text" name="api_url" value="{h.get("api_url","")}"
|
||||
placeholder="http://192.168.x.x:3000"
|
||||
autocomplete="off" spellcheck="false" data-form-type="other">
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>API Key</label>
|
||||
<input type="password" name="api_key" placeholder="Leave blank to keep existing"
|
||||
autocomplete="new-password" data-1p-ignore data-lpignore="true" data-form-type="other">
|
||||
<p class="key-status">Current: {key_hint}</p>
|
||||
</div>
|
||||
<div class="btn-row">
|
||||
<button type="submit" class="btn btn-secondary btn-sm">Save host</button>
|
||||
<button type="button" class="btn btn-secondary btn-sm fetch-btn"
|
||||
data-host-id="{h["id"]}">Fetch models</button>
|
||||
<span class="fetch-status" id="fetch-{h["id"]}"></span>
|
||||
</div>
|
||||
</form>
|
||||
<form method="POST" action="/settings/local/host/{h["id"]}/remove"
|
||||
onsubmit="return confirm('Remove host and all its models?')" style="margin-top:0.5rem">
|
||||
<button type="submit" class="btn-link danger">Remove host</button>
|
||||
</form>
|
||||
</div>'''
|
||||
|
||||
if not host_rows:
|
||||
host_rows = '<p class="empty-note">No hosts configured yet. Add one below.</p>'
|
||||
|
||||
# ── Host options for add-model form ───────────────────────────────────────
|
||||
host_options = "".join(
|
||||
f'<option value="{h["id"]}">{h.get("label") or h["api_url"]}</option>'
|
||||
for h in hosts
|
||||
)
|
||||
add_model_hidden = "" if hosts else ' style="display:none"'
|
||||
|
||||
# ── Model rows ────────────────────────────────────────────────────────────
|
||||
model_rows = ""
|
||||
for m in models:
|
||||
is_active = m["id"] == active
|
||||
host = host_by_id.get(m["host_id"], {})
|
||||
host_name = host.get("label") or host.get("api_url") or "unknown host"
|
||||
badge = '<span class="active-badge">active</span>' if is_active else ""
|
||||
activate_btn = (
|
||||
'<span class="active-label">✓ Active</span>'
|
||||
if is_active else
|
||||
f'''<form method="POST" action="/settings/local/models/{m["id"]}/activate" style="display:inline">
|
||||
<button type="submit" class="row-btn">Set active</button>
|
||||
</form>'''
|
||||
resolved = reg._resolve_model(registry, m["id"])
|
||||
if not resolved:
|
||||
continue
|
||||
host_name = ""
|
||||
if m.get("type") == "local_openai" and m.get("host_id"):
|
||||
h = host_by_id.get(m["host_id"], {})
|
||||
host_name = h.get("label") or h.get("api_url", "")
|
||||
|
||||
ctx_badge = f'<span class="ctx-badge">{m.get("context_k",0)}k ctx</span>' if m.get("context_k") else ""
|
||||
tags_html = " ".join(
|
||||
f'<span class="tag">{t}</span>' for t in (m.get("tags") or [])
|
||||
)
|
||||
host_html = f'<span class="model-host">{host_name}</span>' if host_name else ""
|
||||
|
||||
model_rows += f'''
|
||||
<div class="model-row{" model-active" if is_active else ""}">
|
||||
<div class="model-row" id="model-{m["id"]}">
|
||||
<div class="model-info">
|
||||
<span class="model-label">{m.get("label") or m["model_name"]}</span>{badge}
|
||||
<span class="model-name">{m["model_name"]}</span>
|
||||
<span class="model-host">{host_name}</span>
|
||||
<span class="model-label">{m.get("label") or m.get("model_name","")}</span>
|
||||
<span class="model-name">{m.get("model_name","")}</span>
|
||||
{host_html}{ctx_badge}
|
||||
<div class="tag-row">{tags_html}</div>
|
||||
</div>
|
||||
<div class="model-actions">
|
||||
{activate_btn}
|
||||
<form method="POST" action="/settings/local/models/{m["id"]}/remove" style="display:inline"
|
||||
onsubmit="return confirm('Remove {m.get('label') or m['model_name']}?')">
|
||||
<form method="POST" action="/settings/local/models/{m["id"]}/remove"
|
||||
onsubmit="return confirm('Remove this model?')" style="display:inline">
|
||||
<button type="submit" class="row-btn danger">Remove</button>
|
||||
</form>
|
||||
</div>
|
||||
</div>'''
|
||||
|
||||
if not model_rows:
|
||||
model_rows = '<p class="empty-note">No models added yet. Use "Add Model" below.</p>'
|
||||
model_rows = '<p class="empty-note">No models added yet.</p>'
|
||||
|
||||
# ── Host select for Add Model ─────────────────────────────────────────────
|
||||
host_options = "".join(
|
||||
f'<option value="{h["id"]}">{h.get("label") or h["api_url"]}</option>'
|
||||
for h in hosts
|
||||
)
|
||||
add_section_hidden = "" if hosts else ' style="display:none"'
|
||||
# ── Role assignment rows ──────────────────────────────────────────────────
|
||||
# Build option list: (none) + built-ins + user models
|
||||
model_opts = '<option value="">— .env default —</option>\n'
|
||||
model_opts += '<optgroup label="Built-in">\n'
|
||||
for bid, bm in builtins.items():
|
||||
model_opts += f' <option value="{bid}">{bm["label"]}</option>\n'
|
||||
model_opts += '</optgroup>\n'
|
||||
if models:
|
||||
model_opts += '<optgroup label="Local models">\n'
|
||||
for m in models:
|
||||
lbl = m.get("label") or m.get("model_name", m["id"])
|
||||
model_opts += f' <option value="{m["id"]}">{lbl}</option>\n'
|
||||
model_opts += '</optgroup>\n'
|
||||
|
||||
role_rows = ""
|
||||
for role in app_settings.get_defined_roles():
|
||||
role_cfg = roles.get(role, {})
|
||||
role_rows += f'<div class="role-row" data-role="{role}"><span class="role-name">{role.title()}</span><div class="role-slots">'
|
||||
for slot in reg.PRIORITY_KEYS[:3]: # primary + backup_1 + backup_2
|
||||
current = role_cfg.get(slot) or ""
|
||||
slot_label = slot.replace("_", " ").title()
|
||||
sel_html = f'<select class="role-select" data-role="{role}" data-slot="{slot}" title="{slot_label}">\n{model_opts}\n</select>'
|
||||
# Pre-select current value via JS (simpler than string-building selected attrs)
|
||||
role_rows += f'<div class="role-slot"><span class="slot-label">{slot_label}</span>{sel_html}</div>'
|
||||
role_rows += '</div></div>'
|
||||
|
||||
# JS data for pre-selecting current role values
|
||||
import json as _json
|
||||
role_data_js = _json.dumps({
|
||||
role: {slot: (roles.get(role, {}).get(slot) or "") for slot in reg.PRIORITY_KEYS[:3]}
|
||||
for role in app_settings.get_defined_roles()
|
||||
})
|
||||
|
||||
html = (_STATIC / "local_llm.html").read_text()
|
||||
first_host_id = hosts[0]["id"] if hosts else ""
|
||||
|
||||
html = html.replace("{{ username }}", username)
|
||||
html = html.replace("{{ host_id }}", host_id_val)
|
||||
html = html.replace("{{ host_label }}", host_label)
|
||||
html = html.replace("{{ host_url }}", host_url)
|
||||
html = html.replace("{{ host_key_hint }}", host_key_hint)
|
||||
html = html.replace("{{ model_rows }}", model_rows)
|
||||
html = html.replace("{{ host_options }}", host_options)
|
||||
html = html.replace("{{ first_host_id }}", first_host_id)
|
||||
html = html.replace("{{ add_section_hidden }}", add_section_hidden)
|
||||
html = html.replace("{{ has_host }}", "true" if hosts else "false")
|
||||
html = html.replace("{{ host_rows }}", host_rows)
|
||||
html = html.replace("{{ model_rows }}", model_rows)
|
||||
html = html.replace("{{ host_options }}", host_options)
|
||||
html = html.replace("{{ add_model_hidden }}", add_model_hidden)
|
||||
html = html.replace("{{ role_rows }}", role_rows)
|
||||
html = html.replace("{{ role_data_js }}", role_data_js)
|
||||
if success:
|
||||
html = html.replace("<!-- SUCCESS -->", f'<p class="msg success">{success}</p>')
|
||||
if error:
|
||||
@@ -127,7 +188,7 @@ def _render(username: str, success: str = "", error: str = "") -> str:
|
||||
# ── Routes ────────────────────────────────────────────────────────────────────
|
||||
|
||||
@router.get("/settings/local", include_in_schema=False)
|
||||
async def local_llm_page(request: Request):
|
||||
async def models_page(request: Request):
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
@@ -145,45 +206,40 @@ async def save_host(
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
|
||||
if not api_url.strip():
|
||||
return HTMLResponse(_render(username, error="API URL is required."))
|
||||
|
||||
us.save_host(username, host_id or None, label, api_url, api_key)
|
||||
logger.info("local LLM host saved: %s", username)
|
||||
reg.save_host(username, host_id or None, label, api_url, api_key)
|
||||
logger.info("model registry host saved: %s", username)
|
||||
return HTMLResponse(_render(username, success="Host saved."))
|
||||
|
||||
|
||||
@router.post("/settings/local/host/{host_id}/remove", include_in_schema=False)
|
||||
async def remove_host(request: Request, host_id: str):
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
reg.remove_host(username, host_id)
|
||||
return HTMLResponse(_render(username, success="Host removed."))
|
||||
|
||||
|
||||
@router.post("/settings/local/models/add", include_in_schema=False)
|
||||
async def add_model(
|
||||
request: Request,
|
||||
host_id: str = Form(...),
|
||||
label: str = Form(""),
|
||||
model_name: str = Form(...),
|
||||
context_k: int = Form(0),
|
||||
tags: str = Form(""),
|
||||
):
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
|
||||
if not model_name.strip():
|
||||
return HTMLResponse(_render(username, error="Model name is required."))
|
||||
|
||||
us.add_model(username, host_id, label, model_name)
|
||||
logger.info("local model added: %s / %s", username, model_name)
|
||||
return HTMLResponse(_render(username, success=f"Model \"{label or model_name}\" added."))
|
||||
|
||||
|
||||
@router.post("/settings/local/models/{model_id}/activate", include_in_schema=False)
|
||||
async def activate_model(request: Request, model_id: str):
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
|
||||
if not us.set_active_model(username, model_id):
|
||||
return HTMLResponse(_render(username, error="Model not found."))
|
||||
|
||||
logger.info("active local model set: %s / %s", username, model_id)
|
||||
return HTMLResponse(_render(username, success="Active model updated."))
|
||||
tag_list = [t.strip() for t in tags.split(",") if t.strip()]
|
||||
reg.save_model(username, None, host_id, label, model_name, context_k, tag_list)
|
||||
logger.info("model added to registry: %s / %s", username, model_name)
|
||||
return HTMLResponse(_render(username, success=f'Model "{label or model_name}" added.'))
|
||||
|
||||
|
||||
@router.post("/settings/local/models/{model_id}/remove", include_in_schema=False)
|
||||
@@ -191,30 +247,58 @@ async def remove_model(request: Request, model_id: str):
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return RedirectResponse("/login", status_code=302)
|
||||
|
||||
us.remove_model(username, model_id)
|
||||
logger.info("local model removed: %s / %s", username, model_id)
|
||||
reg.remove_model(username, model_id)
|
||||
return HTMLResponse(_render(username, success="Model removed."))
|
||||
|
||||
|
||||
@router.get("/api/local-llm/fetch-models")
|
||||
async def fetch_models(request: Request) -> JSONResponse:
|
||||
"""Proxy to the configured host's /api/models endpoint.
|
||||
@router.post("/api/models/role")
|
||||
async def set_role(request: Request) -> JSONResponse:
|
||||
"""AJAX: assign a model to a role priority slot.
|
||||
|
||||
Returns [{id, name}] sorted by name, or an error dict.
|
||||
Body: {"role": "chat", "slot": "primary", "model_id": "abc123" | ""}
|
||||
"""
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return JSONResponse({"error": "Not authenticated"}, status_code=401)
|
||||
try:
|
||||
body = await request.json()
|
||||
except Exception:
|
||||
return JSONResponse({"error": "Invalid JSON"}, status_code=400)
|
||||
|
||||
cfg = us.get_config(username)
|
||||
hosts = cfg.get("hosts", [])
|
||||
role = body.get("role", "").strip()
|
||||
slot = body.get("slot", "").strip()
|
||||
model_id = body.get("model_id", "").strip() or None
|
||||
|
||||
# Fall back to .env if no host configured yet
|
||||
if hosts:
|
||||
h = hosts[0]
|
||||
api_url = h.get("api_url", "")
|
||||
api_key = h.get("api_key", "")
|
||||
if not role or not slot:
|
||||
return JSONResponse({"error": "role and slot are required"}, status_code=400)
|
||||
|
||||
ok = reg.set_role(username, role, slot, model_id)
|
||||
if not ok:
|
||||
return JSONResponse({"error": f"Invalid slot or model_id not found"}, status_code=400)
|
||||
|
||||
logger.info("role set: %s %s.%s = %s", username, role, slot, model_id)
|
||||
return JSONResponse({"ok": True})
|
||||
|
||||
|
||||
@router.get("/api/local-llm/fetch-models")
|
||||
async def fetch_models(request: Request, host_id: str = "") -> JSONResponse:
|
||||
"""Proxy to the host's /api/models endpoint. host_id selects which host."""
|
||||
username = _get_user(request)
|
||||
if not username:
|
||||
return JSONResponse({"error": "Not authenticated"}, status_code=401)
|
||||
|
||||
registry = reg.get_registry(username)
|
||||
hosts = registry.get("hosts", [])
|
||||
|
||||
if host_id:
|
||||
host = next((h for h in hosts if h["id"] == host_id), None)
|
||||
else:
|
||||
host = hosts[0] if hosts else None
|
||||
|
||||
# Fall back to .env
|
||||
if host:
|
||||
api_url = host.get("api_url", "")
|
||||
api_key = host.get("api_key", "")
|
||||
else:
|
||||
api_url = app_settings.local_api_url
|
||||
api_key = app_settings.local_api_key
|
||||
|
||||
Reference in New Issue
Block a user