Files
yetoneandMagiczerowxy 4193f7410c chat: Magic_zero's magpie-chat page, as published at Magiczerowxy/magpie-chat 15de02d
The files its package.json ships, unchanged, from
https://github.com/Magiczerowxy/magpie-chat at 15de02d (0.1.18). The
vendored KaTeX is katex@0.16.11's dist, byte for byte (checked against
npm's tarball). Not published from this commit: the next ones close the
local service to other pages and fit it to the repo.

Co-authored-by: Magiczerowxy <magiczerowxy@users.noreply.github.com>
2026-10-29 09:01:47 +08:00

49 lines
1.7 KiB
JavaScript
Raw Permalink 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.
// 独立启动对话服务,不依赖 magpie 的插件宿主。
// 用法:node serve.mjs (或双击 start-server.cmd)
//
// 如果 magpie 已经通过插件把服务起起来了,这里就什么都不做直接退出,
// 避免两个实例抢同一份设置和对话文件。
import { startServer } from "./lib/server.mjs";
const CANDIDATES = [3477, 3478, 3479, 3480];
async function alreadyRunning() {
for (const p of CANDIDATES) {
try {
const ctl = new AbortController();
const timer = setTimeout(() => ctl.abort(), 800);
const res = await fetch("http://127.0.0.1:" + p + "/api/ping", { signal: ctl.signal });
clearTimeout(timer);
if (res.ok) {
const j = await res.json().catch(() => null);
if (j && j.ok) return { port: p, version: j.version };
}
} catch { /* 这个端口没有 */ }
}
return null;
}
const alive = await alreadyRunning();
if (alive) {
console.log("");
console.log(" 对话服务已经在运行了");
console.log(" 地址: http://127.0.0.1:" + alive.port + " (版本 " + (alive.version || "?") + ")");
console.log(" 不需要再起一个,直接去 magpie 里打开「对话」即可。");
console.log("");
process.exit(0);
}
const info = await startServer({ configDir: process.env.MAGPIE_CHAT_CONFIG || "", options: {} })
.catch((err) => {
console.error("启动失败:", err?.message || err);
process.exit(1);
});
console.log("");
console.log(" 对话服务已就绪");
console.log(" 地址: http://127.0.0.1:" + info.port);
console.log(" 这个窗口最小化即可,关掉服务就停了");
console.log("");
process.on("SIGINT", async () => { await info.close(); process.exit(0); });