Files
plugins/packages/system-prompt/system-prompt.middleware.js
T
yetone d9236e976f middleware: param-override, model-map, system-prompt, word-guard and think-tags — gateway middleware that magpie runs on every agent's requests and replies, mostly New API's channel settings with the same JSON (the owner) — magpie can now run a plugin as gateway middleware (onRequest, onEvent, onResponse, in-process on moejs), and these are the first ones: param-override is New API's param_override (both forms, operations with gjson paths and conditions, return_error); model-map is model_mapping (chains, /regex/ keys with $1) and puts the asked-for name back into replies; system-prompt adds a prompt in each API's own place, keeping an agent's own unless override (New API's semantics), by agent or model; word-guard is the sensitive-word filter (reject or mask the user's text, optionally replies); think-tags strips a leading <think>…</think> from replies across stream chunks, or puts Chat reasoning_content into the text (thinking_to_content). Each is one file with no imports, named @magpie-community/middleware-<name>, with magpie.options as the example the app's editor starts from; registry.json lists them with "kind": "middleware" and no providers; check.mjs skips middleware-only packages. scripts/middleware.mjs calls the hooks as the gateway does (events split, [DONE] passed by) and runs each package's cases.json. Tested with a real DeepSeek account and real Claude Code through magpie's gateway: a mapped model named back in message_start, max_tokens capped, a system prompt appended for claude only, a word turned away as a 400 in Anthropic's shape, return_error as a 403 in Chat's, <think> stripped from Chat and Responses streams and an Anthropic whole reply, reasoning_content moved into the text. bun test (61 for these five) and bun scripts/check.mjs pass; packages/devin's two image tests fail on this machine only, as on main. Their first npm version has to be published by hand (trusted publishing can't create a package); until then magpie shows them as Coming soon.
2026-10-05 20:49:42 +08:00

92 lines
3.3 KiB
JavaScript

// New API's 系统提示词 (a channel's system_prompt, with its "override"
// switch) as magpie gateway middleware, in each agent API's own place for
// it: Anthropic's system, Chat's first system or developer message,
// Responses' instructions, Gemini's systemInstruction.
//
// {"system_prompt": "Answer in English.", "override": false,
// "position": "append", "agents": [], "models": []}
//
// A request with no system prompt is given this one. One that has its own
// keeps it as it is, unless override is true: then this one is added
// before it ("prepend", New API's way) or after it ("append", the
// default, which keeps a prompt cache on the agent's own prompt). agents
// and models, when given, are the only agents and model name prefixes it
// applies to.
export function onRequest(body, ctx) {
const o = ctx.options || {}
const prompt = typeof o.system_prompt === "string" ? o.system_prompt : ""
if (!prompt) return
if (Array.isArray(o.agents) && o.agents.length && !o.agents.includes(ctx.agent)) return
if (Array.isArray(o.models) && o.models.length && !o.models.some((m) => typeof m === "string" && String(body.model ?? ctx.model ?? "").startsWith(m))) return
const before = o.position === "prepend"
const over = !!o.override
switch (ctx.protocol) {
case "anthropic":
return anthropic(body, prompt, over, before)
case "chat":
return chat(body, prompt, over, before)
case "responses":
return responses(body, prompt, over, before)
case "gemini":
return gemini(body, prompt, over, before)
}
}
function anthropic(body, prompt, over, before) {
const s = body.system
if (s === undefined || s === null || s === "" || (Array.isArray(s) && !s.length)) {
body.system = prompt
return body
}
if (!over) return
if (typeof s === "string") body.system = before ? prompt + "\n" + s : s + "\n" + prompt
else if (Array.isArray(s)) {
const block = { type: "text", text: prompt }
if (before) s.unshift(block)
else s.push(block)
} else return
return body
}
function chat(body, prompt, over, before) {
if (!Array.isArray(body.messages)) return
const first = body.messages[0]
if (!first || (first.role !== "system" && first.role !== "developer")) {
body.messages.unshift({ role: "system", content: prompt })
return body
}
if (!over) return
if (typeof first.content === "string") first.content = before ? prompt + "\n" + first.content : first.content + "\n" + prompt
else if (Array.isArray(first.content)) {
const part = { type: "text", text: prompt }
if (before) first.content.unshift(part)
else first.content.push(part)
} else return
return body
}
function responses(body, prompt, over, before) {
const s = body.instructions
if (typeof s !== "string" || !s) {
body.instructions = prompt
return body
}
if (!over) return
body.instructions = before ? prompt + "\n" + s : s + "\n" + prompt
return body
}
function gemini(body, prompt, over, before) {
const key = body.system_instruction && !body.systemInstruction ? "system_instruction" : "systemInstruction"
const s = body[key]
if (!s || !Array.isArray(s.parts) || !s.parts.length) {
body[key] = { parts: [{ text: prompt }] }
return body
}
if (!over) return
if (before) s.parts.unshift({ text: prompt })
else s.parts.push({ text: prompt })
return body
}