mirror of
https://gitcode.com/JianFeeeee/ModelRouter.git
synced 2026-10-03 23:54:06 +00:00
## WebUI:新增「插件」页
- 列表来自 on_disk(不是 loaded 集合)——**加载失败的插件也必须显示并带错误**,
否则一个语法错误看起来和"插件没装"完全一样
- 启用/禁用(PUT {"enabled":bool})、删除、编辑源码、安装/覆盖
- 显示 hook_errors:插件抛异常在别处毫无痕迹,没有这一栏的症状就是
"功能就是不work"
- 插到 dropzone 与代码编辑器都做了泛型化(bindDropzone / openCodeModal),
适配器与插件共用一份,而不是复制第二份只改 4 个 id 的函数
## TABS 收敛为单一常量
tab 清单原本是字面量散在三处:goTab、refresh()、admin-only 隐藏列表。
加一个 tab 意味着三处都要记得改,漏一处就是"路由认得但界面不显示"——
和今天早些时候 chain_step 漏报同一类静默缺口。现在只有 const TABS。
## Electron 壳:设置面板里的插件管理
渲染进程不能直连内嵌核心(没有 key、不知道端口),所以走 IPC:
renderer → plugins:proxy → main → HTTP /api/plugins
代理是 (method, path, body) 透传而不是固定命令表:固定表每加一个端点就要扩,
而"按钮存在但什么都不做"比"没有这个按钮"更糟。透传让渲染层能调用核心将来
新增的任何 /api/plugins 路由,路径在主进程校验。
## ★ GUI 此前零测试,而本次改动就引入了三类"看起来没事"的问题
1. 引用了不存在的 CSS 类(.tag / .sm)——渲染成无样式文本
2. 引用了不存在的 helper(esc / escAttr)——那是 WebUI 的,renderer/app.js
是独立文档,点击时 ReferenceError
3. .ghost/.primary 只在 .form .actions 作用域内生效,插件按钮在 .pl-acts 里
于是是无样式裸按钮
补 4 个静态判据(不启动 Electron,守卫的正是"打开应用才看得见"那一类):
TestGUICSSClassesExist 用到的类必须在样式表里定义
TestGUIHelperFunctionsAreDefined 被调用的函数必须有定义
TestGUIPluginPanelIsReachable 面板在 overlay 内、按钮已绑定、打开设置会加载
TestGUIIPCPathIsConstrained 代理必须限定 /api/plugins 前缀并拒绝路径穿越
写第一个判据时我错了三次:CSS 解析器先丢最后一个 selector、再把变量块当
selector、最后漏掉复合选择器(.tb-btn.tb-close)。两次"判据自己坏了"的
教训和本项目一贯一致——**判据出错的信号是它报了一个假问题**。现在改用宽松的
token 提取 + 显式的 guiKnownUnstyled 豁免表(blob/tgl/rail 是既有无样式类,
不是本次引入,失败它们只会让判据对新工作失去意义)。
## 变异验证
改坏唯一的 CSS 定义(.pl-empty)→ TestGUICSSClassesExist 红
改坏 helper 名 → TestGUIHelperFunctionsAreDefined 红
★ 第一次变异我改了 .pl-broken,判据**正确地没报**——因为它还被另一条规则定义。
这是变异选错目标,不是判据有洞;换 .pl-empty 后如期变红。
363 个测试全绿。
381 lines
12 KiB
JavaScript
381 lines
12 KiB
JavaScript
// ModelRouter Desktop GUI — renderer
|
|
// Clash-Verge-style: the content area is a full-screen iframe loading the
|
|
// real WebUI 1:1 (status / chat / keys / priority / sources / adapters).
|
|
// The shell only adds the titlebar, settings overlay and tray integration.
|
|
const state = {
|
|
core: {
|
|
running: false,
|
|
ready: false,
|
|
baseUrl: "",
|
|
port: 8787,
|
|
profileDir: "",
|
|
configFile: "",
|
|
coreExists: true,
|
|
},
|
|
settings: {
|
|
port: 8787,
|
|
autoStart: true,
|
|
silentStart: false,
|
|
minimizeToTray: true,
|
|
},
|
|
theme: localStorage.getItem("mr-theme") || "light",
|
|
loadedOnce: false, // iframe has been pointed at the web UI at least once
|
|
frameLoaded: false, // last loadFrame actually finished loading (did-finish-load)
|
|
loadTries: 0,
|
|
};
|
|
|
|
const $ = (s) => document.querySelector(s);
|
|
|
|
function toast(msg, isErr, ms) {
|
|
const t = $("#toast");
|
|
t.textContent = msg;
|
|
t.className = "show" + (isErr ? " err" : "");
|
|
clearTimeout(t._tm);
|
|
t._tm = setTimeout(() => {
|
|
t.className = "";
|
|
}, ms || 2600);
|
|
}
|
|
|
|
const frame = () => $("#webui-frame");
|
|
|
|
// ===== core status =====
|
|
async function refreshCore() {
|
|
state.core = await window.modelrouter.core.state();
|
|
renderCoreStatus();
|
|
}
|
|
|
|
function renderCoreStatus() {
|
|
const core = state.core;
|
|
const dot = $("#conn-dot");
|
|
dot.className = "status-dot" + (core.running ? " ok" : " bad");
|
|
dot.title = !core.running
|
|
? "内核未运行"
|
|
: core.ready
|
|
? "内核运行中"
|
|
: "内核启动中…";
|
|
}
|
|
|
|
function showOffline() {
|
|
frame().style.display = "none";
|
|
$("#offline").style.display = "flex";
|
|
$("#console-loading").style.display = "none";
|
|
}
|
|
|
|
function showLoading() {
|
|
frame().style.display = "none";
|
|
$("#offline").style.display = "none";
|
|
$("#console-loading").style.display = "flex";
|
|
}
|
|
|
|
// probe the core's web root directly (fetch from the renderer). Using
|
|
// fetch() instead of relying on the iframe's own load means the core's
|
|
// readiness is confirmed by an actual HTTP round-trip before we point the
|
|
// iframe at it — this is what prevents the blank-screen race where the
|
|
// frame load happens while the listener is still coming up.
|
|
function coreReachable() {
|
|
return fetch(
|
|
state.core.baseUrl || "http://127.0.0.1:" + (state.core.port || 8787),
|
|
{ method: "GET", redirect: "follow", cache: "no-store" },
|
|
)
|
|
.then(() => true)
|
|
.catch(() => false);
|
|
}
|
|
|
|
// point the iframe at the WebUI, but only after the core is reachable.
|
|
// If the load fails (core still booting / raced), we retry with backoff
|
|
// instead of giving up forever (the old code set loadedOnce blindly and
|
|
// never retried -> permanent blank screen).
|
|
let frameRetryTimer = null;
|
|
function scheduleFrameLoad() {
|
|
if (frameRetryTimer) return; // already scheduled
|
|
frameRetryTimer = setTimeout(async () => {
|
|
frameRetryTimer = null;
|
|
if (!state.core.running) return; // core stopped meanwhile
|
|
if (!(await coreReachable())) {
|
|
// core not reachable yet: wait a bit and try again
|
|
state.loadTries += 1;
|
|
showLoading();
|
|
scheduleFrameLoad();
|
|
return;
|
|
}
|
|
loadFrame(state.core.baseUrl || "http://127.0.0.1:" + state.core.port);
|
|
}, state.loadTries === 0 ? 800 : Math.min(3000, 400 * state.loadTries));
|
|
}
|
|
|
|
function loadFrame(url) {
|
|
const f = frame();
|
|
f.style.display = "block";
|
|
$("#offline").style.display = "none";
|
|
$("#console-loading").style.display = "none";
|
|
if (state.loadedOnce && f.src === url) return; // already pointing there
|
|
f.src = url;
|
|
state.loadedOnce = true;
|
|
}
|
|
|
|
async function ensureCore() {
|
|
await refreshCore();
|
|
if (state.core.running) {
|
|
scheduleFrameLoad();
|
|
} else {
|
|
state.loadedOnce = false;
|
|
showOffline();
|
|
}
|
|
}
|
|
|
|
// ===== side rail toggles =====
|
|
function renderRail() {
|
|
const auto = $("#rail-autostart-dot");
|
|
const sil = $("#rail-silent-dot");
|
|
if (auto) auto.classList.toggle("on", !!state.settings.autoStart);
|
|
if (sil) sil.classList.toggle("on", !!state.settings.silentStart);
|
|
}
|
|
|
|
async function toggleAutoStart() {
|
|
const next = !state.settings.autoStart;
|
|
state.settings.autoStart = next;
|
|
await window.modelrouter.settings.set({ autoStart: next });
|
|
renderRail();
|
|
toast(next ? "开机自启已开启" : "开机自启已关闭");
|
|
}
|
|
|
|
async function toggleSilent() {
|
|
const next = !state.settings.silentStart;
|
|
state.settings.silentStart = next;
|
|
await window.modelrouter.settings.set({ silentStart: next });
|
|
renderRail();
|
|
toast(next ? "静默启动已开启" : "静默启动已关闭");
|
|
}
|
|
|
|
// ===== settings overlay =====
|
|
async function openSettings() {
|
|
state.settings = await window.modelrouter.settings.get();
|
|
$("#set-port").value = state.settings.port ?? 8787;
|
|
$("#set-autostart").checked = !!state.settings.autoStart;
|
|
$("#set-silent").checked = !!state.settings.silentStart;
|
|
$("#set-tray").checked = !!state.settings.minimizeToTray;
|
|
$("#settings-overlay").style.display = "flex";
|
|
renderRail();
|
|
loadPlugins();
|
|
}
|
|
function closeSettings() {
|
|
$("#settings-overlay").style.display = "none";
|
|
}
|
|
|
|
async function saveSettings() {
|
|
const port = parseInt($("#set-port").value, 10);
|
|
const autoStart = $("#set-autostart").checked;
|
|
const silentStart = $("#set-silent").checked;
|
|
const minimizeToTray = $("#set-tray").checked;
|
|
await window.modelrouter.settings.set({
|
|
port,
|
|
autoStart,
|
|
silentStart,
|
|
minimizeToTray,
|
|
});
|
|
closeSettings();
|
|
toast("设置已保存");
|
|
await refreshCore();
|
|
if (state.core.running) {
|
|
scheduleFrameLoad();
|
|
} else {
|
|
state.loadedOnce = false;
|
|
showOffline();
|
|
}
|
|
}
|
|
|
|
// esc / escAttr escape text for innerHTML. The WebUI has its own copies; the
|
|
// shell needs its own because renderer/app.js is a separate document that
|
|
// never loads index.html's script.
|
|
function esc(s) {
|
|
return String(s == null ? "" : s).replace(
|
|
/[&<>"']/g,
|
|
(c) => ({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[c],
|
|
);
|
|
}
|
|
function escAttr(s) {
|
|
return esc(s).replace(/`/g, "`");
|
|
}
|
|
|
|
// ===== plugin management =====
|
|
//
|
|
// The desktop shell manages plugins through the embedded core's /api/plugins
|
|
// surface, proxied over IPC (see plugins:proxy in the main process). The
|
|
// renderer never holds the admin key.
|
|
//
|
|
// Scope note: the desktop build has no plugin_dir configured by default, so this
|
|
// panel normally reports "plugins disabled" with the one-line fix. That is a
|
|
// deliberate state, not an error — the packaged profile is a per-user directory
|
|
// and seeding a plugin tree into someone's home without asking would be rude.
|
|
|
|
async function loadPlugins() {
|
|
const list = $("#pl-list");
|
|
const hint = $("#set-plugins-hint");
|
|
if (!list || !hint) return;
|
|
let j;
|
|
try {
|
|
j = await window.modelrouter.plugins.request("GET", "/api/plugins");
|
|
} catch (e) {
|
|
hint.textContent = "内核未就绪:" + e.message;
|
|
list.innerHTML = "";
|
|
return;
|
|
}
|
|
if (!j.plugin_dir) {
|
|
hint.innerHTML =
|
|
'未配置 <code>plugin_dir</code>,插件功能未启用。在 config.yaml 加一行后重启内核即可。';
|
|
list.innerHTML = "";
|
|
return;
|
|
}
|
|
const rows = j.on_disk || [];
|
|
const active = rows.filter((p) => p.loaded && !p.disabled).length;
|
|
const broken = rows.filter((p) => !p.loaded).length;
|
|
hint.textContent =
|
|
`${rows.length} 个插件 · ${active} 个启用中` +
|
|
(broken ? ` · ${broken} 个加载失败` : "");
|
|
list.innerHTML = rows.length
|
|
? rows
|
|
.map((p) => {
|
|
const cls = !p.loaded ? "pl-broken" : p.disabled ? "pl-off" : "pl-on";
|
|
const label = !p.loaded
|
|
? "加载失败"
|
|
: p.disabled
|
|
? "已禁用"
|
|
: "启用中";
|
|
const btn = p.loaded
|
|
? `<button class="ghost" data-act="toggle" data-name="${escAttr(
|
|
p.name,
|
|
)}" data-en="${p.disabled ? "1" : "0"}">${
|
|
p.disabled ? "启用" : "禁用"
|
|
}</button>`
|
|
: "";
|
|
const builtin = p.builtin
|
|
? '<span class="pl-builtin">内置</span>'
|
|
: "";
|
|
return `<div class="pl-item ${cls}">
|
|
<div class="pl-head"><b>${esc(p.name)}</b>${builtin}<span class="pl-state">${label}</span></div>
|
|
${p.description ? `<div class="pl-desc">${esc(p.description)}</div>` : ""}
|
|
${p.error ? `<div class="pl-err">${esc(String(p.error).slice(0, 160))}</div>` : ""}
|
|
<div class="pl-acts">${btn}</div>
|
|
</div>`;
|
|
})
|
|
.join("")
|
|
: '<div class="pl-empty">插件目录为空</div>';
|
|
list.querySelectorAll('button[data-act="toggle"]').forEach((b) => {
|
|
b.onclick = () => togglePlugin(b.dataset.name, b.dataset.en === "1");
|
|
});
|
|
}
|
|
|
|
async function togglePlugin(name, disabled) {
|
|
try {
|
|
await window.modelrouter.plugins.request("PUT", `/api/plugins/${encodeURIComponent(name)}`, {
|
|
enabled: disabled,
|
|
});
|
|
toast(disabled ? `已禁用 ${name}` : `已启用 ${name}`);
|
|
await loadPlugins();
|
|
} catch (e) {
|
|
toast(e.message, true);
|
|
}
|
|
}
|
|
|
|
async function enableAllPlugins() {
|
|
let j;
|
|
try {
|
|
j = await window.modelrouter.plugins.request("GET", "/api/plugins");
|
|
} catch (e) {
|
|
return toast(e.message, true);
|
|
}
|
|
const off = (j.on_disk || []).filter((p) => p.loaded && p.disabled);
|
|
for (const p of off) {
|
|
try {
|
|
await window.modelrouter.plugins.request(
|
|
"PUT",
|
|
`/api/plugins/${encodeURIComponent(p.name)}`,
|
|
{ enabled: true },
|
|
);
|
|
} catch (e) {
|
|
toast(`${p.name}: ${e.message}`, true);
|
|
}
|
|
}
|
|
toast(off.length ? `已启用 ${off.length} 个插件` : "没有处于禁用状态的插件");
|
|
await loadPlugins();
|
|
}
|
|
|
|
// ===== theme =====
|
|
function applyTheme() {
|
|
document.documentElement.dataset.theme = state.theme;
|
|
try {
|
|
localStorage.setItem("mr-theme", state.theme);
|
|
} catch (e) {}
|
|
}
|
|
|
|
// ===== init =====
|
|
function init() {
|
|
$("#tb-min").onclick = () => window.modelrouter.win.minimize();
|
|
$("#tb-max").onclick = () => window.modelrouter.win.toggleMaximize();
|
|
$("#tb-close").onclick = () => window.modelrouter.win.close();
|
|
$("#tb-settings").onclick = openSettings;
|
|
$("#rail-settings").onclick = openSettings;
|
|
const plReload = document.getElementById("pl-reload");
|
|
if (plReload) plReload.onclick = loadPlugins;
|
|
const plAll = document.getElementById("pl-toggle-all");
|
|
if (plAll) plAll.onclick = enableAllPlugins;
|
|
$("#rail-autostart").onclick = toggleAutoStart;
|
|
$("#rail-silent").onclick = toggleSilent;
|
|
$("#rail-theme").onclick = () => {
|
|
state.theme = state.theme === "dark" ? "light" : "dark";
|
|
applyTheme();
|
|
};
|
|
$("#set-close").onclick = closeSettings;
|
|
$("#set-cancel").onclick = closeSettings;
|
|
$("#set-save").onclick = saveSettings;
|
|
$("#set-dir").onclick = () =>
|
|
window.modelrouter.shell.openPath(state.core.profileDir);
|
|
$("#set-log").onclick = async () => {
|
|
const p = await window.modelrouter.settings.logFile();
|
|
window.modelrouter.shell.openPath(p);
|
|
};
|
|
$("#off-start").onclick = async () => {
|
|
showLoading();
|
|
await window.modelrouter.core.start();
|
|
await refreshCore();
|
|
if (state.core.running) scheduleFrameLoad();
|
|
else showOffline();
|
|
};
|
|
// when the iframe finishes loading, mark loaded and refresh core status so
|
|
// the conn-dot reflects the actual WebUI (not just the core reachability)
|
|
frame().onload = () => {
|
|
state.frameLoaded = true;
|
|
refreshCore();
|
|
};
|
|
// load failure (e.g. the frame was pointed at the core just before it
|
|
// finished booting) -> clear loadedOnce and let scheduleFrameLoad retry
|
|
frame().onerror = () => {
|
|
state.loadedOnce = false;
|
|
state.frameLoaded = false;
|
|
scheduleFrameLoad();
|
|
};
|
|
window.modelrouter.core.onState((d) => {
|
|
state.core = Object.assign({}, state.core, d);
|
|
renderCoreStatus();
|
|
if (d.running && !state.loadedOnce) scheduleFrameLoad();
|
|
if (!d.running) {
|
|
state.loadedOnce = false;
|
|
showOffline();
|
|
}
|
|
});
|
|
// Escape closes overlay
|
|
document.addEventListener("keydown", (e) => {
|
|
if (e.key === "Escape" && $("#settings-overlay").style.display === "flex")
|
|
closeSettings();
|
|
});
|
|
}
|
|
|
|
async function boot() {
|
|
applyTheme();
|
|
init();
|
|
state.settings = await window.modelrouter.settings.get();
|
|
renderRail();
|
|
await ensureCore();
|
|
}
|
|
|
|
boot();
|