Files
opencode2api/webui/js/logstream.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

86 lines
2.7 KiB
JavaScript

// Live log stream: SSE connection, bounded buffer and rendering. The
// connection lifecycle is owned by the console shell.
import { $, el, timeOnly, toast } from "./ui.js";
let events = [];
let source = null;
let paused = false;
let visible = false;
let renderPending = false;
function render() {
const level = $("#log-level").value;
const component = $("#log-component").value.toLowerCase();
const query = $("#log-search").value.toLowerCase();
const rows = events
.filter(
(item) =>
(!level || item.level === level) &&
(!component || (item.component || "").toLowerCase().includes(component)) &&
(!query || JSON.stringify(item).toLowerCase().includes(query)),
)
.slice(-500);
const host = $("#log-list");
host.replaceChildren();
rows.forEach((item) => {
const levelClass =
item.level === "error" ? "bad" : item.level === "warn" ? "warn" : item.level === "info" ? "ok" : "";
host.append(
el("div", { class: "log" }, [
el("span", { class: "log-time", text: timeOnly(item.time) }),
el("span", { class: levelClass, text: item.level }),
el("span", { class: "log-component", text: item.component || "core" }),
el("span", { class: "log-message", text: item.message }),
el("span", { class: "log-fields", text: JSON.stringify(item.fields || {}) }),
]),
);
});
if (!rows.length) host.append(el("div", { class: "empty", text: "等待日志事件…" }));
host.scrollTop = host.scrollHeight;
}
export function connect() {
disconnect();
// The server replays its recent ring on every subscribe; start from a clean
// buffer so a re-login does not duplicate the previous session's events.
events = [];
source = new EventSource("/api/logs/stream");
source.addEventListener("log", (event) => {
events.push(JSON.parse(event.data));
if (events.length > 2000) events.shift();
if (!paused && visible && !renderPending) {
renderPending = true;
requestAnimationFrame(() => {
renderPending = false;
render();
});
}
});
source.addEventListener("gap", () => toast("较早日志已从内存清除", true));
}
export function disconnect() {
if (source) source.close();
source = null;
}
// The stream tab reports its visibility so hidden panels skip rendering while
// the buffer keeps filling.
export function setVisible(value) {
visible = value;
if (visible) render();
}
export function bind() {
["#log-level", "#log-component", "#log-search"].forEach((selector) =>
$(selector).addEventListener("input", render),
);
$("#log-pause").addEventListener("click", () => {
paused = !paused;
$("#log-pause").textContent = paused ? "继续" : "暂停";
if (!paused) render();
});
}
export { render };