system-prompt 0.1.1: models matches a model's own name, and a routing group's try on it

@liucl on X (11-09): a prompt only for gemini-3.8-flash. `models` was
checked with String(body.model ?? ctx.model).startsWith(m), but an agent
wired to magpie names its model magpie's way, provider first
(google/gemini-3.8-flash, openrouter/google/gemini-3.8-flash), or names a
routing group (group/...). So "models": ["gemini-3.8-flash"] never
matched, though the README said "models whose names begin with one of
these prefixes".

A prefix now matches the whole id or the part after any "/" of it, case
aside; full-id prefixes (google/, deepseek) work as before. magpie's
middleware gets the agent's body at onRequest (ctx.model is modelOf(body),
serveAgent in internal/gateway/middleware.go), so a group request names
the group there. The member is known on each try: onProvider gets
ctx.provider {id, model} and can change that try's body
(docs/subsystems/gateway-middleware.md, triedOn). A request onRequest
didn't match is matched there against the member's model (and id/model),
so only the tries on gemini-3.8-flash get the prompt. ctx.state.given
keeps a try from adding it a second time after onRequest did.

Run on mote: packages/system-prompt bun test, 24 pass 0 fail (13 new
cases); bun scripts/check.mjs, all ok. In magpie's own engine: a
throwaway test in a detached magpie worktree at origin/main (removed, not
committed) loaded this module through internal/middleware's Begin,
Request and Tried in moejs: google/ and openrouter/google/gemini-3.8-flash
got it once, gemini-3.8-pro none, group/fast got it on the google
gemini-3.8-flash try and not on a deepseek try, no failures counted.
Broken by hand: with the old startsWith in onRequest, the 4 name cases go
red; with onProvider not exported, the 2 group-try cases; without
ctx.state.given, the "not given it twice" case.
Not tried: a real agent through a real magpie gateway and Gemini account.
Without override, an agent's own system prompt is kept and none is
added, as before; the README's example sets override.
This commit is contained in:
yetone
2026-11-09 14:54:54 +08:00
parent 065758f58b
commit ff9d3a6446
4 changed files with 129 additions and 9 deletions
+6 -2
View File
@@ -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"]}`。
+67
View File
@@ -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" } }
+1 -1
View File
@@ -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"],
@@ -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) {