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"],