ferock on Discord, after page-chat updated itself to 0.1.7: typing a
system prompt said "保存失败:Failed to fetch", and every message after
failed the same way. The system prompt wasn't the cause. Nothing was
listening on the page's port any more.
magpie swaps plugin hosts when a plugin is updated, installed or set
(Restart, or another magpie's change to plugins.json). An old host that
is still finishing a call (a reply, a tool call) keeps running, up to 10
minutes, and its page service keeps 3477. The new host's service tried
for 6s, threw "3477 一直被占用", and never tried again. When the old
host left, nobody had the port. The page's frame stayed on
127.0.0.1:3477, and every fetch failed until magpie was restarted.
startServer now tries for those 6s (bindTries x 500ms) without holding
up the host. Then it goes on trying once a second in the background
until the port is free, and stops when the service is closed. The page's
next request gets 403 with the new token, and the page reloads itself
as before. index.mjs logs "listening" when the port is bound.
Run (mote):
- Reproduced with real magpie serve (magpie origin/main 36af588e2) in a
sandbox HOME, page-chat on port 3592. A plugin tool that takes 40s was
in flight while page-chat 0.1.6 was auto-updated to 0.1.7 by
KeepUpdated, or while another plugin was added. 0.1.7's service logged
"could not start: 3592 一直被占用", and every save to /api/settings got
000 (no listener) after the old host left, even with a fresh token.
With this change the same run logs "还被占着" and then "listening".
A save gets 403 (new token), and the page's reload saves 200.
- Without a busy host, an update came back on its own (403, reload). So
did saves of Chinese, quoted, multi-line and 50k-char system prompts
and a chat on 0.1.7. None of them failed.
- bun test chat.test.mjs: 31 pass. The two new tests are red on the old
lib ("58585 一直被占用,服务起不来"), checked by hand.
- bun scripts/check.mjs ok.
Not tried: magpie's desktop window (WebKit). The page there was not
driven; its 403 reload path is unchanged.
50 lines
2.0 KiB
JavaScript
50 lines
2.0 KiB
JavaScript
// @magpie-community/page-chat:在 magpie 窗口里加一个「对话」页面。
|
|
//
|
|
// 两件事:
|
|
// 1. 作为 OpenCode v1 插件被 magpie 的 Bun 宿主加载(不注册任何供应商,只借宿主的进程起服务)。
|
|
// 2. 宿主调起插件时起一个本地 HTTP 服务,窗口里的页面把自己导航过去,界面与 magpie 网关同源直连。
|
|
//
|
|
// magpie 的插件页面跑在 sandbox="allow-scripts" 的框架里,没有网络权限、不能内嵌框架、
|
|
// 不能用存储,唯一能做的联网动作是把自身导航到别处(文档已明说)。所以这里只是一个
|
|
// 「带路页」,真正的界面由本插件的服务提供。
|
|
|
|
import { startServer } from "./lib/server.mjs";
|
|
|
|
// 服务在宿主调 server() 时才起(宿主加载插件就会调),只起一次;
|
|
// 单纯 import 这个模块不占端口。插件选项里的 port 和带路页(page/boot.js)读的是同一个值。
|
|
let started = null;
|
|
function start(options) {
|
|
if (!started) {
|
|
const port = Number(options?.port);
|
|
started = startServer({
|
|
configDir: process.env.MAGPIE_CHAT_CONFIG || "",
|
|
options: Number.isFinite(port) && port > 0 ? { port } : {},
|
|
})
|
|
.then((info) => {
|
|
// 端口还在上一个宿主手里时不挡宿主启动:服务在后台等它松手
|
|
if (info.listening) console.log(`[page-chat] listening on http://127.0.0.1:${info.port}`);
|
|
else {
|
|
info.ready.then(
|
|
(p) => p && console.log(`[page-chat] listening on http://127.0.0.1:${p}`),
|
|
(err) => console.error("[page-chat] could not start:", err?.message || err),
|
|
);
|
|
}
|
|
return info;
|
|
})
|
|
.catch((err) => {
|
|
console.error("[page-chat] could not start:", err?.message || err);
|
|
return null;
|
|
});
|
|
}
|
|
return started;
|
|
}
|
|
|
|
// magpie 会把每个导出的函数当成一个插件调用;用 OpenCode v1 的形式只导出服务器。
|
|
export default {
|
|
id: "magpie-chat",
|
|
server: async (input, options) => {
|
|
await start(options);
|
|
return {};
|
|
},
|
|
};
|