From 8c87a843b5f33e1fc40935c09005194365ed1c7d Mon Sep 17 00:00:00 2001 From: TryWorld Date: Mon, 5 Oct 2026 23:45:07 +0800 Subject: [PATCH] model-map 0.1.1: a Gemini request's model is mapped, and its reply names it back (#26) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- packages/model-map/README.md | 2 +- packages/model-map/cases.json | 11 ++++++++++- packages/model-map/model-map.middleware.js | 14 ++++++++++++-- packages/model-map/package.json | 2 +- 4 files changed, 24 insertions(+), 5 deletions(-) diff --git a/packages/model-map/README.md b/packages/model-map/README.md index e86bd58..f41decf 100644 --- a/packages/model-map/README.md +++ b/packages/model-map/README.md @@ -20,6 +20,6 @@ Install it from magpie's **Plugins › Discover**, or with `magpie plugin add @m 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, not in the body, so this middleware leaves Gemini requests alone. +**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 请求时用的名字。 diff --git a/packages/model-map/cases.json b/packages/model-map/cases.json index 60da5e5..db0d509 100644 --- a/packages/model-map/cases.json +++ b/packages/model-map/cases.json @@ -54,7 +54,16 @@ "request": { "model": "fast" }, "expect_request": { "model": "deepseek-chat" }, "response": { "model": "deepseek-chat" } }, - { "name": "Gemini's body has no model to map", + { "name": "Gemini's model is in the body the gateway fills, and is mapped", + "ctx": { "protocol": "gemini", "model": "fast" }, + "options": { "mapping": { "fast": "x" } }, + "request": { "model": "fast", "contents": [] }, + "expect_request": { "model": "x", "contents": [] }, + "events": [{ "data": { "candidates": [{ "content": { "parts": [{ "text": "hi" }], "role": "model" } }], "modelVersion": "x" } }], + "expect_events": [{ "candidates": [{ "content": { "parts": [{ "text": "hi" }], "role": "model" } }], "modelVersion": "fast" }], + "response": { "candidates": [{ "content": { "parts": [{ "text": "hi" }], "role": "model" } }], "modelVersion": "x" }, + "expect_response": { "candidates": [{ "content": { "parts": [{ "text": "hi" }], "role": "model" } }], "modelVersion": "fast" } }, + { "name": "a Gemini request naming no model passes as it is", "ctx": { "protocol": "gemini", "model": "fast" }, "options": { "mapping": { "fast": "x" } }, "request": { "contents": [] } } diff --git a/packages/model-map/model-map.middleware.js b/packages/model-map/model-map.middleware.js index 1197a79..806d2fc 100644 --- a/packages/model-map/model-map.middleware.js +++ b/packages/model-map/model-map.middleware.js @@ -13,8 +13,9 @@ // model's name in replies. export function onRequest(body, ctx) { - // Gemini's model is in the request's path, not its body - if (ctx.protocol === "gemini" || typeof body.model !== "string") return + // the gateway puts a Gemini request's model in its body too (the URL's + // model, which it reads from there as for every other API) + if (body.model === null || typeof body.model !== "string") return const to = map(body.model, rules(ctx.options)) if (to === body.model) return ctx.state.asked = body.model @@ -37,6 +38,11 @@ export function onEvent(ev, ctx) { ev.response.model = asked return ev } + // Gemini names it in modelVersion + if (typeof ev.modelVersion === "string" && ev.modelVersion !== asked) { + ev.modelVersion = asked + return ev + } } export function onResponse(body, ctx) { @@ -50,6 +56,10 @@ export function onResponse(body, ctx) { body.response.model = asked return body } + if (typeof body.modelVersion === "string" && body.modelVersion !== asked) { + body.modelVersion = asked + return body + } } // rules are the options' mappings, compiled: exact names, and patterns diff --git a/packages/model-map/package.json b/packages/model-map/package.json index 240931a..e240204 100644 --- a/packages/model-map/package.json +++ b/packages/model-map/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/middleware-model-map", - "version": "0.1.0", + "version": "0.1.1", "description": "New API's model_mapping (模型重定向) as magpie gateway middleware: send the model an agent asks for as another, and keep the asked name in replies", "type": "module", "files": ["model-map.middleware.js", "README.md"],