diff --git a/packages/system-prompt/README.md b/packages/system-prompt/README.md index 2e32c3d..4a9016b 100644 --- a/packages/system-prompt/README.md +++ b/packages/system-prompt/README.md @@ -24,8 +24,12 @@ Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @m - `"append"` (the default) puts yours after it. Agents cache their own prompt, so adding after it keeps that cache. - `"prepend"` puts yours before it, as New API does. This makes the provider read the whole prompt again on every turn. - **`agents`.** When given, the prompt applies only to these agents, by magpie's agent ids: `claude`, `codex`, `opencode`, and so on. -- **`models`.** When given, the prompt applies only to models whose names begin with one of these prefixes. +- **`models`.** When given, the prompt applies only to models whose names begin with one of these prefixes, case aside. A prefix may begin magpie's whole model id (`google/gemini`) or the model's own name after its provider (`gemini-3.8-flash` matches `google/gemini-3.8-flash` and `openrouter/google/gemini-3.8-flash`). In a routing group the prompt goes only to the tries on a model in `models`: to give Gemini 3.8 Flash a prompt and no other model, whether it is asked directly or through a group, use (`override`, since most agents send a prompt of their own) + + ```json + { "system_prompt": "Answer in Chinese.", "override": true, "models": ["gemini-3.8-flash"] } + ``` Codex sends its own model instructions, and changing them can change how its models behave. Use `agents` to leave Codex out if you don't want that. -中文:New API 的「系统提示词」,作为 magpie 网关中间件。默认把你的提示词加在 agent 自己的提示词之后,这样不会破坏提示缓存。 +中文:New API 的「系统提示词」,作为 magpie 网关中间件。默认把你的提示词加在 agent 自己的提示词之后,这样不会破坏提示缓存。`models` 按模型名前缀匹配(不分大小写),可以写完整 id(如 `google/gemini`),也可以只写模型名(如 `gemini-3.8-flash`,会匹配 `google/gemini-3.8-flash`、`openrouter/google/gemini-3.8-flash`);路由组里只有发往这些模型的请求会加上提示词。例如只给 gemini-3.8-flash 加提示词:`{"system_prompt": "用中文回答。", "override": true, "models": ["gemini-3.8-flash"]}`。 diff --git a/packages/system-prompt/cases.json b/packages/system-prompt/cases.json index 7fe5207..69ae920 100644 --- a/packages/system-prompt/cases.json +++ b/packages/system-prompt/cases.json @@ -42,6 +42,73 @@ "options": { "system_prompt": "Be brief.", "models": ["deepseek"] }, "request": { "model": "deepseek-chat" }, "expect_request": { "model": "deepseek-chat", "system": "Be brief." } }, + { "name": "models: a model's own name matches magpie's provider/model id (@liucl)", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: and the model under a reseller's provider/vendor/model id", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "request": { "model": "openrouter/google/gemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "openrouter/google/gemini-3.8-flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: a shorter prefix of the name still matches", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8"] }, + "request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: case aside", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "request": { "model": "google/Gemini-3.8-Flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "google/Gemini-3.8-Flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: a full-id prefix as before", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["google/"] }, + "request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: another model gets none", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "request": { "model": "google/gemini-3.8-pro", "messages": [{ "role": "user", "content": "hi" }] } }, + { "name": "models: a name only inside a segment is no match", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "request": { "model": "google/xgemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] } }, + { "name": "models: a group's try on the model named gets the prompt", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "provider": { "id": "google", "name": "Google", "model": "gemini-3.8-flash" }, + "request": { "model": "group/fast", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "group/fast", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: a group's try on another model gets none", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"] }, + "provider": { "id": "deepseek", "name": "DeepSeek", "model": "deepseek-v4.1-flash" }, + "request": { "model": "group/fast", "messages": [{ "role": "user", "content": "hi" }] } }, + { "name": "models: a group's try named by provider/model", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["google/gemini-3.8"] }, + "provider": { "id": "google", "name": "Google", "model": "gemini-3.8-flash" }, + "request": { "model": "group/fast", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "group/fast", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: a try after onRequest gave the prompt is not given it twice", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"], "override": true }, + "provider": { "id": "google", "name": "Google", "model": "gemini-3.8-flash" }, + "request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "google/gemini-3.8-flash", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, + { "name": "models: a group's try for an agent not named gets none", + "ctx": { "protocol": "chat", "agent": "codex" }, + "options": { "system_prompt": "Be brief.", "models": ["gemini-3.8-flash"], "agents": ["claude"] }, + "provider": { "id": "google", "name": "Google", "model": "gemini-3.8-flash" }, + "request": { "model": "group/fast", "messages": [{ "role": "user", "content": "hi" }] } }, + { "name": "no models: a try is left to onRequest's prompt", + "ctx": { "protocol": "chat" }, + "options": { "system_prompt": "Be brief." }, + "provider": { "id": "google", "name": "Google", "model": "gemini-3.8-flash" }, + "request": { "model": "group/fast", "messages": [{ "role": "user", "content": "hi" }] }, + "expect_request": { "model": "group/fast", "messages": [{ "role": "system", "content": "Be brief." }, { "role": "user", "content": "hi" }] } }, { "name": "no prompt, nothing done", "options": {}, "request": { "model": "m" } } diff --git a/packages/system-prompt/package.json b/packages/system-prompt/package.json index 8eae6b8..823eb9b 100644 --- a/packages/system-prompt/package.json +++ b/packages/system-prompt/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/middleware-system-prompt", - "version": "0.1.0", + "version": "0.1.1", "description": "New API's system_prompt (系统提示词) as magpie gateway middleware: give agents' requests a system prompt of yours, in each API's own place for it", "type": "module", "files": ["system-prompt.middleware.js", "README.md"], diff --git a/packages/system-prompt/system-prompt.middleware.js b/packages/system-prompt/system-prompt.middleware.js index 1d6aba3..cbb5f58 100644 --- a/packages/system-prompt/system-prompt.middleware.js +++ b/packages/system-prompt/system-prompt.middleware.js @@ -10,15 +10,64 @@ // 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. +// and models, when given, are the only agents and models it applies to. +// +// A model in models is a prefix of the model's name: of the whole id +// magpie gives it ("google/gemini-3.8-flash"), or of the part after any of +// its provider segments ("gemini-3.8-flash" of it, or of +// "openrouter/google/gemini-3.8-flash"), case aside. An agent wired to +// magpie names its model magpie's way, provider first, so a bare model +// name never matched the whole id (@liucl on X, 11-09). A routing group's +// request names the group ("group/…") until it is routed: the model a +// member is asked is known only on each try (onProvider, ctx.provider), so +// a request onRequest didn't match is matched again there, and only the +// tries on a model in models get the prompt. 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 + if (!wanted(o, ctx)) return + const models = modelsOf(o) + if (models && !matches(String(body.model ?? ctx.model ?? ""), models)) return + if (ctx.state) ctx.state.given = true + return give(body, ctx, o) +} + +// onProvider gives the prompt to a try on a model in models, when the +// request's own model wasn't one (a group's). Each try starts from the body +// onRequest left, so a member not in models is sent none. +export function onProvider(body, ctx) { + const o = ctx.options || {} + const models = modelsOf(o) + if (!models || ctx.state?.given || !wanted(o, ctx)) return + const p = ctx.provider || {} + const model = String(p.model ?? "") + if (!model) return + if (!matches(model, models) && !(p.id && matches(p.id + "/" + model, models))) return + return give(body, ctx, o) +} + +function wanted(o, ctx) { + if (typeof o.system_prompt !== "string" || !o.system_prompt) return false + return !(Array.isArray(o.agents) && o.agents.length && !o.agents.includes(ctx.agent)) +} + +// modelsOf is the prefixes in models, lowercase; null when it names none +// (one naming only what isn't a prefix matches nothing) +function modelsOf(o) { + if (!Array.isArray(o.models) || !o.models.length) return null + return o.models.filter((m) => typeof m === "string").map((m) => m.toLowerCase()) +} + +// matches: a prefix begins the whole id or the part after one of its "/" +function matches(id, prefixes) { + const s = id.toLowerCase() + const starts = [0] + for (let i = s.indexOf("/"); i >= 0; i = s.indexOf("/", i + 1)) starts.push(i + 1) + return prefixes.some((m) => starts.some((at) => s.startsWith(m, at))) +} + +function give(body, ctx, o) { + const prompt = o.system_prompt const before = o.position === "prepend" const over = !!o.override switch (ctx.protocol) {