Files
opencode2api/webui/js/pages/config.js
T
michaelliunsky a0da31ed08 feat(webui): rebuild console with tabbed pages
Replace the single-file frontend (app.js, styles.css) with a multi-file
ES module structure served from go:embed, and reorganize the 13 pages
into 8 tabbed pages under 4 navigation groups: runtime (overview,
diagnostics), integrations (access guide, API keys), models (quotas,
rotation), and system (configuration, account).

The redesign adopts a monochrome light/dark design system driven by
prefers-color-scheme, replaces prompt()/confirm() flows with in-page
dialogs, and keeps every management API call and deep link (old hashes
alias to the new pages).

Behavior changes: the log SSE connection now spans the whole console
session and buffers while the stream tab is hidden; quota polling runs
only while its tab is active; the first-run checklist appears on the
overview page and hides once ready.

Also fixes a crash on the rotation page: the persisted state serializes
empty orders and maps as JSON null, which is now normalized before
rendering, with the same defensive handling in the quota and API key
pages.

CI now syntax-checks the ES modules via node --input-type=module, and
the README structure notes are updated.
2026-10-16 18:09:42 +08:00

192 lines
7.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Page: 配置中心 — masked config editing with validation, hot reload and reveal.
import { api } from "../api.js";
import { loadConfig, loadDebugModels } from "../data.js";
import * as store from "../store.js";
import { $, bindTabs, el, openDialog, toast } from "../ui.js";
function renderChips(name) {
const host = $(`#${name}-chips`);
host.replaceChildren();
(store.get("config")?.[name] || []).forEach((item) => {
const remove = el("button", { type: "button", title: "移除", text: "×" });
const chip = el("span", { class: "chip", dataset: { id: item.id } });
remove.addEventListener("click", () => chip.remove());
chip.append(el("span", { text: item.display }), remove);
host.append(chip);
});
}
function secretInputs(name) {
const kept = [...$(`#${name}-chips`).children].map((node) => ({ id: node.dataset.id }));
const added = $(`#${name}-new`)
.value.split(/\r?\n/)
.map((value) => value.trim())
.filter(Boolean)
.map((value) => ({ value }));
return kept.concat(added);
}
const numberValue = (selector) => Number($(selector).value);
function fillConfig(value) {
if (!value) return;
$("#c-listen").value = value.listen;
$("#c-prefer").value = value.prefer;
$("#c-web-listen").value = value.webui.listen;
$("#c-session").value = value.webui.session_ttl_minutes;
$("#c-web-enabled").checked = value.webui.enabled;
$("#c-anonymous").checked = !!value.anonymous;
$("#c-proxyfile").value = value.proxyfile;
$("#c-up-zen").value = value.upstream.zen;
$("#c-up-go").value = value.upstream.go;
$("#c-attempts").value = value.retry.max_attempts;
$("#c-timeout").value = value.retry.timeout_seconds;
$("#c-refresh").value = value.models.refresh_seconds;
$("#c-protocols").value = JSON.stringify(value.models.protocols || {}, null, 2);
$("#c-free-only").checked = !!value.models.free_only;
const reasoning = value.reasoning || {};
$("#c-reasoning-effort").value = reasoning.effort || "";
$("#c-reasoning-by-model").value = JSON.stringify(reasoning.effort_by_model || {}, null, 2);
$("#c-idle").value = value.performance.max_idle_conns;
$("#c-idle-host").value = value.performance.max_idle_conns_per_host;
$("#c-max-host").value = value.performance.max_conns_per_host;
$("#c-idle-timeout").value = value.performance.idle_conn_timeout_seconds;
$("#c-connect").value = value.performance.connect_timeout_seconds;
$("#c-cooldown").value = value.performance.failure_cooldown_seconds;
$("#c-attempt-timeout").value = value.performance.attempt_timeout_seconds;
$("#c-first-event-timeout").value = value.performance.first_event_timeout_seconds || 0;
$("#c-level").value = value.logging.level;
$("#c-ring").value = value.logging.ring_size;
$("#c-dump-bodies").checked = !!value.logging.dump_request_bodies;
["server_keys", "zen_keys", "go_keys", "proxies"].forEach(renderChips);
const notice = $("#restart-notice");
if (value.restart_required_fields?.length) {
notice.textContent = `已保存但尚未生效:${value.restart_required_fields.join(", ")}。当前 API ${value.effective.listen}、WebUI ${value.effective.webui_listen}。请重启进程。`;
notice.classList.remove("hidden");
} else {
notice.classList.add("hidden");
}
}
async function save(event) {
event.preventDefault();
const current = store.get("config");
if (!current) return;
try {
const update = {
listen: $("#c-listen").value,
server_keys: secretInputs("server_keys"),
zen_keys: secretInputs("zen_keys"),
go_keys: secretInputs("go_keys"),
anonymous: $("#c-anonymous").checked,
proxies: secretInputs("proxies"),
proxyfile: $("#c-proxyfile").value,
prefer: $("#c-prefer").value,
upstream: { zen: $("#c-up-zen").value, go: $("#c-up-go").value },
retry: { max_attempts: numberValue("#c-attempts"), timeout_seconds: numberValue("#c-timeout") },
models: {
refresh_seconds: numberValue("#c-refresh"),
protocols: JSON.parse($("#c-protocols").value || "{}"),
free_only: $("#c-free-only").checked,
},
reasoning: {
effort: $("#c-reasoning-effort").value,
effort_by_model: JSON.parse($("#c-reasoning-by-model").value || "{}"),
},
performance: {
max_idle_conns: numberValue("#c-idle"),
max_idle_conns_per_host: numberValue("#c-idle-host"),
max_conns_per_host: numberValue("#c-max-host"),
idle_conn_timeout_seconds: numberValue("#c-idle-timeout"),
connect_timeout_seconds: numberValue("#c-connect"),
failure_cooldown_seconds: numberValue("#c-cooldown"),
attempt_timeout_seconds: numberValue("#c-attempt-timeout"),
first_event_timeout_seconds: numberValue("#c-first-event-timeout"),
},
logging: {
level: $("#c-level").value,
ring_size: numberValue("#c-ring"),
dump_request_bodies: $("#c-dump-bodies").checked,
},
webui: {
enabled: $("#c-web-enabled").checked,
listen: $("#c-web-listen").value,
username: current.webui.username,
session_ttl_minutes: numberValue("#c-session"),
},
};
const result = await api("/api/config", { method: "PUT", body: JSON.stringify(update) });
store.set("config", result.config);
["server_keys", "zen_keys", "go_keys", "proxies"].forEach(
(name) => ($(`#${name}-new`).value = ""),
);
await loadDebugModels();
toast(
result.result.restart_required
? `已应用;${result.result.restart_fields.join(", ")} 需重启`
: "配置已保存并立即应用",
);
} catch (error) {
toast(error.message, true);
}
}
async function reload() {
try {
const result = await api("/api/config/reload", { method: "POST", body: "{}" });
await loadConfig();
await loadDebugModels();
toast(result.restart_required ? "磁盘配置已重载,监听字段需重启" : "磁盘配置已重载");
} catch (error) {
toast(error.message, true);
}
}
function reveal() {
const input = el("input", { type: "password", autocomplete: "current-password" });
const submit = async (close) => {
try {
const values = await api("/api/config/reveal", {
method: "POST",
body: JSON.stringify({ password: input.value }),
});
close();
openDialog({
title: "敏感配置",
size: "dialog-lg",
body: el("pre", { text: JSON.stringify(values, null, 2) }),
actions: [{ label: "关闭" }],
});
} catch (error) {
toast(error.message, true);
}
};
const dialog = openDialog({
title: "查看敏感配置",
body: el("div", { class: "field" }, [el("label", { text: "请再次输入管理密码" }), input]),
actions: [
{ label: "取消" },
{ label: "查看", kind: "btn-primary", onClick: submit },
],
});
input.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
submit(dialog.close);
}
});
}
export default {
id: "config",
title: "配置中心",
sub: "验证后原子保存,并热应用可变字段。",
init() {
store.on("config", fillConfig);
bindTabs($("#page-config"));
$("#config-form").addEventListener("submit", save);
$("#reload-config").addEventListener("click", reload);
$("#reveal").addEventListener("click", reveal);
},
};