Files
plugins/packages/window-warmup/src/plugin.ts
T
YangHonghang 1ab8238ade window-warmup 0.1.0: a page that sends a short message after a quota window resets, daily or every few hours (#661)
Adds packages/window-warmup (@magpie-community/page-window-warmup 0.1.0), by YangHonghang:
a page plugin whose host part runs a service on 127.0.0.1:3492 and, on a schedule
(after a quota window resets, daily at a set time, or every N hours), sends "hi"
with max_tokens 16 through magpie's gateway to a provider/account/model the user picks.
The registry entry moves from the author's own npm package magpie-window-warmup to this one.
The package README opens with the zh/en warning that sends spend real quota and that some
vendors (Claude subscriptions especially) may treat scheduled requests as unusual use.

Reviewed at 07a0b217adb2a111f839c984cbc955c5bb45b68d:
- Outside packages/window-warmup only README.md and registry.json change; no workflow and no other package.
- The service binds 127.0.0.1, checks Host (DNS rebinding) and Origin (null or itself),
  needs a per-process 32-byte token on /api/*, and serves the index with a nonce CSP.
- It reaches only MAGPIE_ADDR (default 127.0.0.1:3425). providers.json is read for sha256
  key fingerprints only; no spawn, eval or outside hosts.
- A window task sends at most once per resetsAt (or once per hour when idle); daily/every
  tasks catch up only the latest missed point, once.

Run on mote (bun 1.4.2):
- HOME=$(mktemp -d) bun test at the repo root: 5142 pass, 15 skip, 0 fail.
- scripts/check.mjs ok.
- packages/window-warmup bun test: 242 pass (dist.test rebuilds dist byte for byte).
- browser.test.ts with Playwright 1.62.0: chromium and webkit pass. Bun ignores NODE_PATH,
  so it needs node_modules/playwright linked and PLAYWRIGHT_BROWSERS_PATH set under a temp HOME;
  without them it skips.
Not tried: a real send through a live magpie gateway, and tsc --noEmit (no typescript on mote).
2026-11-03 15:43:10 +08:00

54 lines
2.4 KiB
TypeScript
Raw 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 加载本包并调用 server() 时启动本地服务(每个端口只起一次)。
// 单纯 import 不占端口、不读写磁盘;启动失败清掉对应单例,宿主下次调用可重试。
// 启动前先探测端口:响应带本插件的自标记头(server.ts 所有响应都带 x-window-warmup: 1)
// → 已有本服务在跑,直接返回 {}(不重开数据目录、不迁移、不报错);没探测到才真正启动,
// 端口被别的程序占用时 Bun.serve 仍抛原错误。
import { join } from "node:path";
import { gatewayBase } from "./gateway";
import { xdgConfigHome } from "./magpie-files";
import { alreadyServing, startService, type ServiceHandle } from "./server";
import { migrateLegacyState } from "./store";
/** 默认服务端口,与 package.json 的 magpie.options 和带路页(page/boot.js)一致 */
export const DEFAULT_PORT = 3492;
/** 数据目录:WINDOW_WARMUP_DIR 优先,否则 XDG 配置目录下的 magpie/window-warmup。
* 不导出:宿主会把模块导出的每个函数都当插件函数调(scripts/check.mjs 的约定) */
function defaultDir(env: Record<string, string | undefined> = process.env): string {
const custom = env.WINDOW_WARMUP_DIR?.trim();
if (custom) return custom;
return join(xdgConfigHome(env), "magpie", "window-warmup");
}
/** 端口 → 启动中的服务;按端口分键,端口改了旧实例也不串 */
const instances = new Map<number, Promise<ServiceHandle>>();
export default {
id: "magpie-window-warmup",
/** magpie 宿主入口;options.port 来自 package.json 的 magpie.options */
server: async (_input: unknown, opts: { port?: number } = {}) => {
const port = Number(opts?.port);
const listen = Number.isFinite(port) && port > 0 ? port : DEFAULT_PORT;
if (await alreadyServing(listen)) return {};
let instance = instances.get(listen);
if (!instance) {
instance = (async () => {
const dir = defaultDir();
// 首次启动(新目录还没有 state.json)时把旧包的数据接过来;迁移在打开 store 之前完成
await migrateLegacyState(dir);
return startService({
port: listen,
dir,
base: gatewayBase(),
});
})().catch((e: unknown) => {
instances.delete(listen);
throw e;
});
instances.set(listen, instance);
}
await instance;
return {};
},
};