// Magpie settings pages cannot fetch (connect-src 'none'). Their Connect or // Test click navigates to this loopback page, which probes saved options only. import { randomBytes, timingSafeEqual } from "node:crypto" import { readIdentities, testConnection, textsFor } from "./probe.mjs" const DEFAULT_PORT = 3481 const TOKEN_HEADER = "x-aelios-token" let running let pluginOptions = {} let token function page(lang, auto = false) { const t = textsFor(lang) const zh = lang === "zh" const labels = zh ? { title: "助手匹配列表", name: "助手", models: "主模型匹配规则", empty: "未配置主模型", unavailable: "当前服务未提供匹配列表;请升级 Aelios 后重试。", rules: "自动分配复用 Aelios 的主模型规则,按列表顺序选择首个匹配的助手。支持 *、忽略大小写,同时匹配完整模型名及最后 / 后的名称。插件先按授权匹配列表在本地筛选;未匹配时不发送对话。列表不可用时暂停回忆和记录,并显示 unavailable。", override: "当前设置包含自定义身份或路由;它们优先于自动分配。", auto: "连接成功后显示此密钥可访问的助手。" } : { title: "Assistant mappings", name: "Assistant", models: "Main model patterns", empty: "No main models configured", unavailable: "This service does not provide mappings. Upgrade Aelios and retry.", rules: "Automatic assignment uses Aelios main model rules, choosing the first matching assistant in order. Patterns support *, ignore case, and match the full model name or the name after its last /. The plugin filters locally using the authorized matching list; unmatched turns are not sent. If the list is unavailable, recall and recording pause with status unavailable.", override: "Custom identity or route settings are active and take priority over automatic assignment.", auto: "After connecting, this page lists the assistants available to this key." } return ` Aelios

Aelios

${auto ? labels.auto : t.confirm}

${t.scope}

` } function hostOK(req, port = running?.port) { const host = String(req.headers.get("host") ?? "").toLowerCase() return !!port && (host === `127.0.0.1:${port}` || host === `localhost:${port}`) } function originOK(req, port = running?.port) { const origin = req.headers.get("origin") return !!port && (origin === null || origin === "null" || origin === `http://127.0.0.1:${port}` || origin === `http://localhost:${port}`) } function tokenOK(req) { const got = Buffer.from(req.headers.get(TOKEN_HEADER) ?? "") const want = Buffer.from(token ?? "") return want.length > 0 && got.length === want.length && timingSafeEqual(got, want) } function cors(req) { return req.headers.get("origin") === "null" ? { "access-control-allow-origin": "null", "access-control-allow-headers": TOKEN_HEADER, "access-control-allow-methods": "POST, OPTIONS", vary: "Origin", } : {} } async function handle(req) { if (!hostOK(req)) return new Response("Forbidden", { status: 403 }) const url = new URL(req.url) const headers = { "cache-control": "no-store", "x-content-type-options": "nosniff", "referrer-policy": "no-referrer" } if (url.pathname === "/test" && req.method === "GET") { if ([...url.searchParams.keys()].some(key => !["lang", "auto"].includes(key)) || (url.searchParams.has('auto') && url.searchParams.get('auto') !== '1')) return new Response("Invalid request", { status: 400, headers }) return new Response(page(url.searchParams.get("lang"), url.searchParams.get('auto') === '1'), { headers: { ...headers, "content-type": "text/html; charset=utf-8" } }) } if (url.pathname === "/api/test" && ["POST", "OPTIONS"].includes(req.method)) { if (!originOK(req) || (req.method === "POST" && !tokenOK(req))) return new Response("Forbidden", { status: 403, headers }) if (url.search || req.body !== null) return new Response("Invalid request", { status: 400, headers }) if (req.method === "OPTIONS") return new Response(null, { status: 204, headers: { ...headers, ...cors(req) } }) const options = structuredClone(pluginOptions) const result = await testConnection(options) if (result.ok) { result.mapping = await readIdentities(options) result.custom = Boolean(options.identity || options.routes?.length) } return Response.json(result, { headers: { ...headers, ...cors(req) } }) } return new Response("Not found", { status: 404, headers }) } export default { id: "magpie-aelios", async server(_input, options) { pluginOptions = options && typeof options === "object" ? structuredClone(options) : {} if (running) return {} const port = Number(pluginOptions.port ?? DEFAULT_PORT) if (!Number.isInteger(port) || port < 1024 || port > 65535) { console.error("[aelios] invalid connection test port") return {} } try { token = randomBytes(32).toString("hex") running = Bun.serve({ hostname: "127.0.0.1", port, fetch: handle }) console.log(`[aelios] connection test on http://127.0.0.1:${running.port}/test`) } catch { // Neither options nor runtime errors (which might contain secrets) escape. console.error("[aelios] connection test service could not start") } return {} }, } export const _internal = { hostOK, originOK, stop() { running?.stop(true); running = undefined; pluginOptions = {}; token = undefined }, }