onRequest returned early for ctx.protocol === "gemini", on the grounds that a
Gemini request names its model in the URL and not in the body. magpie puts it
in the body before the middleware runs (gateway.go, serveAgent is called with
withFields(body, {"model": model, "stream": stream})), and reads it from there
to route, so the mapping applied to every API but Gemini: a Gemini client
asking for a mapped name got "magpie knows no model" as a 404.
onEvent and onResponse also never looked at modelVersion, where a Gemini reply
names its model (internal/gateway/member.go lists exactly that path), so the
name a Gemini client read back was the upstream's even when the mapping did
apply.
cases.json's "Gemini's body has no model to map" asserted the old assumption
with a body carrying no model — a body the gateway never sends. It is replaced
by one that carries the model the gateway fills, covering the request, the
stream and a whole reply; a second case keeps the no-model body passing as it
is. The new case fails on 0.1.0 and passes here.
Co-authored-by: TryWorld2026 <tryworld@users.noreply.github.com>
@magpie-community/middleware-model-map
New API's 模型重定向 (a channel's model_mapping) as magpie gateway middleware. The model an agent asks for is sent upstream as another model. Replies then name the model the agent asked for, so an agent that reads its model's name back (Claude Code does, to size its context window) sees the name it asked for.
Install it from magpie's Plugins › Discover, or with magpie plugin add @magpie-community/middleware-model-map. Set its options under Plugins › Installed › Options, or with magpie plugin options model-map '<json>'.
Options
{ "mapping": {
"fast": "deepseek-chat",
"/^claude-(.*)$/": "glm-$1"
} }
- Mapping. A name maps to its target. Names chain (
a → b → c) until there is nothing more to map, and a cycle stops before it repeats. - Patterns. A key written
/like this/is a regular expression, and its target can use$1for the first group. - Flat form. New API's flat form,
{"fast": "deepseek-chat"}, works as well. keep_upstream_name. Set it totrueto leave the upstream model's name in replies.
The target has to be a model magpie can route: a model of one of your providers, or one of magpie's own ids (deepseek/deepseek-chat, for example).
Gemini. A Gemini request names its model in the URL; magpie moves it into the body before the middleware runs, so it is mapped like any other API, and the reply's modelVersion names the model asked for again.
中文:New API 的「模型重定向」,作为 magpie 网关中间件。回复里的模型名会还原成 agent 请求时用的名字。