Files
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

95 lines
7.5 KiB
JSON

[
{ "name": "legacy keys are set as they are",
"options": { "temperature": 0.2, "max_tokens": 8000 },
"request": { "model": "m", "max_tokens": 64000, "messages": [] },
"expect_request": { "model": "m", "max_tokens": 8000, "messages": [], "temperature": 0.2 } },
{ "name": "nothing to do leaves the body",
"options": { "operations": [{ "path": "temperature", "mode": "delete" }] },
"request": { "model": "m", "messages": [] } },
{ "name": "a cap: set only when over",
"options": { "operations": [{ "path": "max_tokens", "mode": "set", "value": 32000, "conditions": [{ "path": "max_tokens", "mode": "gt", "value": 32000 }] }] },
"request": { "model": "m", "max_tokens": 64000 },
"expect_request": { "model": "m", "max_tokens": 32000 } },
{ "name": "a cap under the limit stays",
"options": { "operations": [{ "path": "max_tokens", "mode": "set", "value": 32000, "conditions": [{ "path": "max_tokens", "mode": "gt", "value": 32000 }] }] },
"request": { "model": "m", "max_tokens": 4000 } },
{ "name": "keep_origin fills only what is missing",
"options": { "operations": [{ "path": "metadata.user_id", "mode": "set", "value": "me", "keep_origin": true }, { "path": "top_p", "mode": "set", "value": 0.9, "keep_origin": true }] },
"request": { "model": "m", "top_p": 0.5 },
"expect_request": { "model": "m", "top_p": 0.5, "metadata": { "user_id": "me" } } },
{ "name": "conditions on the request: model prefix, protocol, AND",
"ctx": { "protocol": "chat", "model": "deepseek-reasoner" },
"options": { "operations": [{ "path": "temperature", "mode": "delete", "logic": "AND", "conditions": [{ "path": "model", "mode": "prefix", "value": "deepseek-r" }, { "path": "protocol", "mode": "full", "value": "chat" }] }] },
"request": { "model": "deepseek-reasoner", "temperature": 1, "messages": [] },
"expect_request": { "model": "deepseek-reasoner", "messages": [] } },
{ "name": "AND with one false does nothing; invert",
"ctx": { "protocol": "anthropic" },
"options": { "operations": [
{ "path": "temperature", "mode": "delete", "logic": "AND", "conditions": [{ "path": "model", "mode": "prefix", "value": "m" }, { "path": "protocol", "mode": "full", "value": "chat" }] },
{ "path": "x", "mode": "set", "value": 1, "conditions": [{ "path": "protocol", "mode": "full", "value": "chat", "invert": true }] }] },
"request": { "model": "m", "temperature": 1 },
"expect_request": { "model": "m", "temperature": 1, "x": 1 } },
{ "name": "a missing key fails a condition unless pass_missing_key",
"options": { "operations": [
{ "path": "a", "mode": "set", "value": 1, "conditions": [{ "path": "nope", "mode": "full", "value": 1 }] },
{ "path": "b", "mode": "set", "value": 2, "conditions": [{ "path": "nope", "mode": "full", "value": 1, "pass_missing_key": true }] }] },
"request": { "model": "m" },
"expect_request": { "model": "m", "b": 2 } },
{ "name": "return_error turns the request away",
"options": { "operations": [{ "mode": "return_error", "value": { "message": "opus is not allowed here", "status_code": 403 }, "conditions": [{ "path": "model", "mode": "contains", "value": "opus" }] }] },
"request": { "model": "claude-opus-4-5" },
"reject": { "status": 403, "message": "opus is not allowed" } },
{ "name": "return_error as a string is a 400",
"options": { "operations": [{ "mode": "return_error", "value": "no" }] },
"request": { "model": "m" },
"reject": { "status": 400, "message": "no" } },
{ "name": "move and copy",
"options": { "operations": [{ "mode": "move", "from": "max_completion_tokens", "to": "max_tokens" }, { "mode": "copy", "from": "metadata.user_id", "to": "user" }] },
"request": { "model": "m", "max_completion_tokens": 100, "metadata": { "user_id": "u1" } },
"expect_request": { "model": "m", "max_tokens": 100, "metadata": { "user_id": "u1" }, "user": "u1" } },
{ "name": "prepend to a string system, append to an array, merge an object",
"options": { "operations": [
{ "path": "system", "mode": "prepend", "value": "Be brief.\n" },
{ "path": "stop", "mode": "append", "value": ["END"] },
{ "path": "metadata", "mode": "append", "value": { "a": 1, "b": 2 }, "keep_origin": true }] },
"request": { "model": "m", "system": "You code.", "stop": ["\n\n"], "metadata": { "a": 0 } },
"expect_request": { "model": "m", "system": "Be brief.\nYou code.", "stop": ["\n\n", "END"], "metadata": { "a": 0, "b": 2 } } },
{ "name": "prepend a block to an array system",
"options": { "operations": [{ "path": "system", "mode": "prepend", "value": { "type": "text", "text": "Be brief." } }] },
"request": { "model": "m", "system": [{ "type": "text", "text": "You code." }] },
"expect_request": { "model": "m", "system": [{ "type": "text", "text": "Be brief." }, { "type": "text", "text": "You code." }] } },
{ "name": "string modes",
"options": { "operations": [
{ "path": "model", "mode": "trim_prefix", "value": "openai/" },
{ "path": "a", "mode": "ensure_suffix", "value": "!" },
{ "path": "b", "mode": "trim_space" },
{ "path": "c", "mode": "to_upper" },
{ "path": "d", "mode": "replace", "from": "x", "to": "y" },
{ "path": "e", "mode": "regex_replace", "from": "(?i)^CLAUDE-(\\d)", "to": "c${1}" }] },
"request": { "model": "openai/gpt-5", "a": "hi", "b": " sp ", "c": "up", "d": "xax", "e": "claude-4-sonnet" },
"expect_request": { "model": "gpt-5", "a": "hi!", "b": "sp", "c": "UP", "d": "yay", "e": "c4-sonnet" } },
{ "name": "wildcards and negative indexes",
"options": { "operations": [
{ "path": "tools.*.input_examples", "mode": "delete" },
{ "path": "messages.-1.content", "mode": "append", "value": " (be brief)" }] },
"request": { "model": "m", "tools": [{ "name": "a", "input_examples": [1] }, { "name": "b" }, { "name": "c", "input_examples": [] }], "messages": [{ "role": "user", "content": "one" }, { "role": "user", "content": "two" }] },
"expect_request": { "model": "m", "tools": [{ "name": "a" }, { "name": "b" }, { "name": "c" }], "messages": [{ "role": "user", "content": "one" }, { "role": "user", "content": "two (be brief)" }] } },
{ "name": "delete over a wildcard of array items",
"options": { "operations": [{ "path": "messages.*", "mode": "delete", "conditions": [{ "path": "model", "mode": "full", "value": "m" }] }] },
"request": { "model": "m", "messages": [1, 2, 3] },
"expect_request": { "model": "m", "messages": [] } },
{ "name": "prune_objects by type, and with where",
"options": { "operations": [
{ "path": "messages", "mode": "prune_objects", "value": "thinking" },
{ "path": "tools", "mode": "prune_objects", "value": { "where": { "name": "WebSearch" } } }] },
"request": { "model": "m", "messages": [{ "role": "assistant", "content": [{ "type": "thinking", "thinking": "x" }, { "type": "text", "text": "hi" }] }], "tools": [{ "name": "WebSearch" }, { "name": "Read" }] },
"expect_request": { "model": "m", "messages": [{ "role": "assistant", "content": [{ "type": "text", "text": "hi" }] }], "tools": [{ "name": "Read" }] } },
{ "name": "header modes have nothing to act on",
"options": { "operations": [{ "mode": "set_header", "path": "X-A", "value": "1" }] },
"request": { "model": "m" } },
{ "name": "replies pass untouched",
"options": { "temperature": 0 },
"events": [{ "event": "message_start", "data": { "type": "message_start", "message": { "model": "m" } } }, { "data": "[DONE]" }],
"response": { "id": "x", "model": "m" } }
]