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

3.4 KiB
Raw Permalink Blame History

@magpie-community/middleware-param-override

New API's 参数覆盖 (a channel's param_override) as magpie gateway middleware. It sets, deletes, moves and rewrites any field of the requests agents send through magpie, whichever provider serves them. The options are New API's JSON as it is, so you can paste a param_override from New API unchanged.

Install it from magpie's Plugins › Discover, or with magpie plugin add @magpie-community/middleware-param-override. Then set its options under Plugins › Installed › Options, or with magpie plugin options param-override '<json>'.

Options

There are two forms, as in New API:

{ "temperature": 0.2, "max_tokens": 8000 }

Each key is set on the body as it is.

{ "operations": [
  { "path": "max_tokens", "mode": "set", "value": 32000,
    "conditions": [{ "path": "max_tokens", "mode": "gt", "value": 32000 }] },
  { "path": "temperature", "mode": "delete",
    "conditions": [{ "path": "model", "mode": "contains", "value": "reasoner" }] },
  { "mode": "return_error", "value": { "message": "opus is not allowed here", "status_code": 403 },
    "conditions": [{ "path": "model", "mode": "contains", "value": "opus" }] }
] }

The operations run in order. Keys set beside operations are applied first.

Field
path A gjson path: a.b.0. messages.-1 counts from the end, tools.*.name matches every item, and \. is a dot inside a key.
mode set (keep_origin: only when missing), delete, move / copy (from, to), prepend / append (arrays joined, strings joined, objects merged; keep_origin keeps keys already there), trim_prefix, trim_suffix, ensure_prefix, ensure_suffix, trim_space, to_lower, to_upper, replace / regex_replace (from, to; Go's ${1} works), return_error, prune_objects
value What to set, join or trim. For return_error, a message or {message, status_code}; the agent gets the error in its own API's shape. For prune_objects, a type or {type, where, conditions, logic, recursive}.
conditions [{path, mode, value, invert, pass_missing_key}], where mode is full, prefix, suffix, contains, gt, gte, lt or lte. A path is looked up in the body first, then in the request: model, request_path, protocol (anthropic, chat, responses, gemini), agent, stream.
logic OR (New API's default) or AND.

Some of New API's modes have nothing to act on here and are left out: the header modes (set_header, pass_headers…), sync_fields, and conditions on retries, users and groups. The middleware sees only the body.

Unlike New API, an operation that can't apply is skipped (a move from a missing path, trim_prefix on a number) and the request goes on. If the middleware itself fails, magpie sends the request as it came.

Recipes

  • Cap max_tokens for a provider with a lower limit: the first example above.
  • Drop a parameter a provider refuses: {"path": "tools.*.input_examples", "mode": "delete"}.
  • Strip a vendor prefix from model names: {"path": "model", "mode": "trim_prefix", "value": "openai/"}.
  • Keep an agent off a model: return_error with a model condition.
  • Remove old thinking blocks: {"path": "messages", "mode": "prune_objects", "value": "thinking"}.

中文:New API 的「参数覆盖」,作为 magpie 网关中间件。配置是 New API 的同一份 JSON,可以原样粘贴过来。