Repository navigation
Expand file tree
/
Copy pathmain.js
More file actions
138 lines (120 loc) · 3.97 KB
/
Copy pathmain.js
File metadata and controls
138 lines (120 loc) · 3.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Console shell: boot, login, navigation and shared data loading.
import { api, setCsrf, clearCsrf, setUnauthorizedHandler } from "./api.js";
import { refreshMonitor, loadConfig, loadDebugModels } from "./data.js";
import * as i18n from "./i18n.js";
import * as store from "./store.js";
import { $, $$, toast } from "./ui.js";
import * as logstream from "./logstream.js";
import { init as initTheme } from "./theme.js";
import overview from "./pages/overview.js";
import diagnostics from "./pages/diagnostics.js";
import access from "./pages/access.js";
import apiKeys from "./pages/api-keys.js";
import quotas from "./pages/model-quotas.js";
import rotation from "./pages/rotation.js";
import configPage from "./pages/config.js";
import account from "./pages/account.js";
const PAGES = [overview, diagnostics, access, apiKeys, quotas, rotation, configPage, account];
const byId = new Map(PAGES.map((page) => [page.id, page]));
// Hrefs from the previous 13-page layout keep working.
const HASH_ALIASES = {
guide: "overview",
usage: "overview",
playground: "diagnostics",
logs: "diagnostics",
availability: "model-quotas",
};
let current = null;
let monitorTimer = null;
function renderPageHeader(page) {
if (!page) return;
$("#page-title").textContent = i18n.t(page.title, `page.${page.id}.title`);
$("#page-sub").textContent = i18n.t(page.sub, `page.${page.id}.sub`);
}
function openPage(id) {
const page = byId.get(id);
if (!page) return;
current?.hide?.();
history.replaceState(null, "", `#${id}`);
$$(".nav-item").forEach((button) =>
button.classList.toggle("active", button.dataset.page === id),
);
$$(".page").forEach((node) => node.classList.toggle("hidden", node.id !== `page-${id}`));
renderPageHeader(page);
current = page;
page.show?.();
}
function showLogin() {
clearCsrf();
current?.hide?.();
current = null;
$("#app").classList.add("hidden");
$("#login").classList.remove("hidden");
clearInterval(monitorTimer);
monitorTimer = null;
logstream.disconnect();
for (const page of PAGES) page.reset?.();
}
async function enterConsole(session) {
setCsrf(session.csrf_token);
$("#login").classList.add("hidden");
$("#app").classList.remove("hidden");
await loadConfig();
await Promise.all([refreshMonitor(), loadDebugModels()]);
clearInterval(monitorTimer);
monitorTimer = setInterval(refreshMonitor, 3000);
logstream.connect();
const hash = location.hash.slice(1);
const id = byId.has(hash) ? hash : HASH_ALIASES[hash];
openPage(id && byId.has(id) ? id : "overview");
}
async function boot() {
try {
await enterConsole(await api("/api/auth/session"));
} catch {
showLogin();
}
}
setUnauthorizedHandler(showLogin);
store.on("monitor", (data) => {
const version = String(data.version || "");
$("#version").textContent = version.startsWith("v") || version === "dev" ? version : `v${version}`;
});
for (const page of PAGES) page.init?.();
initTheme();
$$(".nav-item").forEach((button) => {
button.addEventListener("click", () => openPage(button.dataset.page));
});
$("#refresh-now").addEventListener("click", async () => {
await Promise.all([refreshMonitor(), loadConfig(), loadDebugModels()]);
await current?.refresh?.();
toast(i18n.t("状态已刷新", "toast.refreshed"));
});
$("#logout").addEventListener("click", async () => {
try {
await api("/api/auth/logout", { method: "POST", body: "{}" });
} finally {
showLogin();
}
});
$("#login-form").addEventListener("submit", async (event) => {
event.preventDefault();
$("#login-error").textContent = "";
try {
const session = await api("/api/auth/login", {
method: "POST",
body: JSON.stringify({ username: $("#login-user").value, password: $("#login-pass").value }),
});
$("#login-pass").value = "";
await enterConsole(session);
} catch (error) {
$("#login-error").textContent = error.message;
}
});
i18n.onChange(() => {
renderPageHeader(current);
logstream.syncPauseButton();
logstream.render();
});
i18n.init();
boot();