From 6858c8263104994192388489695a5b55918dd9ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E7=81=BF?= Date: Mon, 30 Nov 2026 15:09:36 +0800 Subject: [PATCH] Chat routing: keep the image model out of the catalog so suffixed ids route as Messages Since 0.1.160 the catalog lists gemini-3.1-flash-image, its only @ai-sdk/openai-compatible entry. With it there, magpie 0.1.4479 sends an id it can't find in the catalog (an effort-suffixed gemini-4-hydrogen-medium, which every Sub2API mapping uses) to the plugin as /v1/chat/completions. The bridge answers as Messages, magpie reads no reply, retries up to 4 times (each a real agy turn) and returns 502 "an empty reply". On the fleet every suffixed request failed this way from the 0.1.160 rollout on. Reproduced on a container and gone with the entry left out. AGY_BRIDGE_IMAGE_MODEL=1 lists it again; no image request on the fleet ever succeeded. Co-Authored-By: Claude Opus 5.5 --- .claude/skills/agy-cli-fork/SKILL.md | 3 ++- FORK.md | 5 ++++- fork/fork.sh | 2 +- fork/patch-e2e.mjs | 13 +++++++++++++ fork/swap-agy-cli.sh | 4 ++-- index.mjs | 11 ++++++++++- test/testkit.mjs | 2 +- 7 files changed, 33 insertions(+), 7 deletions(-) diff --git a/.claude/skills/agy-cli-fork/SKILL.md b/.claude/skills/agy-cli-fork/SKILL.md index 137d6b1..33d7684 100644 --- a/.claude/skills/agy-cli-fork/SKILL.md +++ b/.claude/skills/agy-cli-fork/SKILL.md @@ -69,6 +69,7 @@ fork/fork.sh sync # 默认合并上游 main;也可以指定 ref:for 3. `requestedEffort()` 前面的 `disabledEffort()`,以及 `requestedEffort()` 里 disabled 那一行改为 `return disabledEffort(body)`(disabled 选最低档)。 `fork/patch-e2e.mjs` 的三个 thinking 用例可以验证。上游 `test/smoke.mjs` 检查 disabled 时不出思考,所以 `fork/fork.sh test` 跑上游测试时设了 `AGY_BRIDGE_THINKING_WHEN_DISABLED=0`。 +- 模型目录不放图片模型(`Chat routing (local patch)`),挂在 2 个位置:`videoOn` 前面的 `imageModelOn()`;`provider.models` 末尾 `if (imageModelOn()) out[IMAGE_MODEL_ID] = …`。`fork/patch-e2e.mjs` 的 image model 用例验证;`fork/fork.sh test` 跑上游测试时设 `AGY_BRIDGE_IMAGE_MODEL=1`(上游 `test/images.mjs` 要它在目录里),testkit 放行了这个变量。上游以后如果再往目录里加非 Anthropic 协议的模型,同样会让带后缀的模型名走错协议,合并时留意 `npm:` 不是 `@ai-sdk/anthropic` 的条目。 - 解决原则:上游的改动全部保留,再把上面这些位置接回去。上游如果改了函数名或结构,就按新结构找等价的位置。 - 解决完执行 `git add -A && git commit --no-edit`,再跑 `fork/fork.sh test`。 @@ -101,7 +102,7 @@ fork/fork.sh release `fork/swap-agy-cli.sh <新spec>` 在容器里以 abc 身份运行,它会: - 备份插件配置,rm 旧来源,add 新来源。add 失败时自动把旧来源装回去。 -- 检查四项:来源对不对、登录状态和换之前是否一致、四处补丁是否都在(按 Video、Retry attach、Denied ending、Parallel lanes、Thinking when disabled 五种标记数)、插件进程是否重启(最多等 60 秒)。 +- 检查四项:来源对不对、登录状态和换之前是否一致、四处补丁是否都在(按 Video、Retry attach、Denied ending、Parallel lanes、Thinking when disabled、Chat routing 六种标记数)、插件进程是否重启(最多等 60 秒)。 - 任何一项不符就以非 0 退出。 ```sh diff --git a/FORK.md b/FORK.md index 8f72545..73335f6 100644 --- a/FORK.md +++ b/FORK.md @@ -2,7 +2,7 @@ 本仓库 fork 自 [magpie-community/plugins](https://github.com/magpie-community/plugins) 的 `packages/agy-cli`,npm 包名是 `@magpie-community/opencode-agy-cli-auth`。上游的 LICENSE(MIT)和 NOTICE.md 原样保留。 -和上游相比多五处改动,都在 `index.mjs` 里(“容量不足报错改为 503”上游 0.1.95 已经自带,分叉里那份已删除): +和上游相比多六处改动,都在 `index.mjs` 里(“容量不足报错改为 503”上游 0.1.95 已经自带,分叉里那份已删除): 1. **视频和图片链接(OpenAI 写法)**:上游 0.1.155 起自带视频/音频输入,但只收 `video`/`audio` 块里的 base64;图片也只收 base64,链接一律 400(agy 本身不收图片链接)。补丁把 `image_url` 里的视频(`data:video/…` 或 http(s) 链接)、图片链接,以及 Magpie 从部分 `/v1/responses` 写法原样留下的 `image_url` / `input_image` 块,下载或解码后转成上游的 base64 `image`/`video` 块,之后全走上游。链接下载禁止内网地址,最大 100 MB,缓存一小时。代码里搜 `Video (local patch)`。 2. **重发的请求接上原来那一轮**:客户端等不到首字就断开,把同一份请求再发一遍,而原请求还在跑;或者原请求的回复(一次工具调用)在路上丢了。上游插件对这种重发返回错误(0.1.106 之前一律是 409 `no active MCP call matches these tool results`,0.1.106 起原请求还在出流时是 503),要等原来那一轮结束才变,而原请求照常跑完并计费,产出没人收。现在重发的请求会等原请求出结果,拿到同一份回复,用量记 0,不重复计费。原请求失败或被取消时,重发的请求收到 503,客户端再发一次就会重新跑。已经正常结束、没有待答工具调用的回复不走这条路,再发一遍仍然重新生成。带工具结果的请求、原请求已经结束时,交给上游 0.1.131 的 `replay_tool_step`(比对整份请求的摘要,回放同一个消息 id);本补丁只接原请求还在跑的重发,以及不带工具结果的请求丢了回复的重发。代码里搜 `Retry attach (local patch)`。 @@ -20,6 +20,8 @@ 一个对话最多 `AGY_BRIDGE_MAX_LANES` 个进程(默认 3,设成 1 就是上游行为),满了就复用最久没用的空闲通道;全机进程上限仍是 `AGY_BRIDGE_MAX_PROCS`,闲置 10 分钟的进程照常回收。代码里搜 `Parallel lanes (local patch)`。 5. **thinking 关闭时照样转发思考**:有的网关会给没提 thinking 的请求补上 `"thinking": {"type": "disabled"}`。上游插件见到 disabled 就丢掉所有思考片段,可 agy 关不掉思考:它没有关闭思考的档位,上游的 `requestedEffort()` 遇到 disabled 不设档位,于是选了这个模型的默认档(有 high 就是 high),想得最久,常常 8–12k token、两分钟以上。这期间流里只有 `message_start` 和 ping,网关等不到真正的内容就一直扣着流(最长 4 分钟),只发 `: keepalive` 注释;ZCode 这类 ai-sdk 客户端大约 155 秒就放弃,这次生成白做了。思考片段一到网关,网关就放行,插件 15 秒一次的 ping 也就能到客户端。现在 disabled 的请求也照常转发思考(`thinking_delta`),思考块带插件平时的签名(`thinkingSignature`),和要了 thinking 的请求一样(插件回放历史时不校验这个签名)。非流式回复由同一个流拼出来,同样带思考块。设 `AGY_BRIDGE_THINKING_WHEN_DISABLED=0` 退回上游行为。另外 disabled 的请求改选最低档(`disabledEffort()`):请求带了 `output_config.effort` 就用它,否则用 low(只有一档的模型选最接近的那档)。本机实测 gemini-4-carbon 同一道长题,high 档首字 87–90 秒,low 档 7 秒。模型名带档位后缀(`-high`)时仍按后缀。设 `AGY_BRIDGE_DISABLED_EFFORT=0` 退回上游的默认档,设成别的档位名就用那一档。代码里搜 `Thinking when disabled (local patch)`。 +6. **带思考档后缀的模型名不再 502**:上游 0.1.160 起在模型目录里加了 `gemini-3.1-flash-image`,它是目录里唯一的 `@ai-sdk/openai-compatible` 模型。目录里有它时,Magpie(0.1.4479)把目录里找不到的模型名(`gemini-4-hydrogen-medium` 这类带档位后缀的,Sub2API 的映射全是这种)按 `/v1/chat/completions` 交给插件;插件一律按 Messages 回复,Magpie 读不到内容,当成空回复重试 3–4 次(每次都真跑 agy、扣额度),最后 502 `an empty reply`。补丁默认不把图片模型放进目录,`AGY_BRIDGE_IMAGE_MODEL=1` 恢复。代价是不能经 agy 出图(2026-11-30 查机群,没有一条成功的出图请求)。代码里搜 `Chat routing (local patch)`。 + ## 为什么要绕一圈 agy 的 ACP 不收视频(image 块报 `unsupported mimeType`,resource blob 被 `unsupported blob type` 丢掉),只有它自己读文件的工具能把视频原生交给 Gemini。上游 0.1.155 的做法:把 `video`/`audio` 块写到会话工作区的 `attachments/.`,prompt 里叫模型去读,只有带媒体的会话才有 `tools: [view_file]`,并且不加 `--add-dir`、把 TMPDIR 指到会话工作区,`view_file` 读不到别的目录。 @@ -44,6 +46,7 @@ agy 的 ACP 不收视频(image 块报 `unsupported mimeType`,resource blob | `AGY_BRIDGE_MAX_LANES` | 3 | 一个对话最多同时用几个 agy 进程(并行通道)。设为 `1` 退回上游的一个对话一个进程 | | `AGY_BRIDGE_THINKING_WHEN_DISABLED` | 开启 | 请求写了 `thinking: {type: "disabled"}` 时仍转发思考。设为 `0` 退回上游行为(丢掉思考) | | `AGY_BRIDGE_DISABLED_EFFORT` | `low` | `thinking: {type: "disabled"}` 且没带 `output_config.effort` 时选的档位。设为 `0` 退回上游行为(模型默认档,通常是 high) | +| `AGY_BRIDGE_IMAGE_MODEL` | 关闭 | 设为 `1` 时把 `gemini-3.1-flash-image` 放回模型目录(上游行为);放回后带档位后缀的模型名会走错协议,见第 6 条 | 限制: - 链接下载的视频最大 100 MB。下载超时 120 秒,最多跟 3 次重定向。 diff --git a/fork/fork.sh b/fork/fork.sh index 2bb453a..2053848 100755 --- a/fork/fork.sh +++ b/fork/fork.sh @@ -105,7 +105,7 @@ run_tests() { echo "bun test(约 2–3 分钟)…" # each run gets an empty HOME, as upstream runs them: this machine's own agy # sign-ins otherwise reach signin/smoke/usage tests and fail them - HOME=$(mktemp -d) AGY_BRIDGE_VIDEO=0 AGY_BRIDGE_MAX_LANES=1 AGY_BRIDGE_THINKING_WHEN_DISABLED=0 AGY_BRIDGE_DISABLED_EFFORT=0 bun test >"$log" 2>&1 || true + HOME=$(mktemp -d) AGY_BRIDGE_VIDEO=0 AGY_BRIDGE_MAX_LANES=1 AGY_BRIDGE_THINKING_WHEN_DISABLED=0 AGY_BRIDGE_DISABLED_EFFORT=0 AGY_BRIDGE_IMAGE_MODEL=1 bun test >"$log" 2>&1 || true grep -E '^ *[0-9]+ (pass|fail)$' "$log" || true [ -z "$(fails "$log")" ] && [ "$(fail_count "$log")" = 0 ] && return 0 echo "有失败,对照上游快照在同一环境再跑一遍…" diff --git a/fork/patch-e2e.mjs b/fork/patch-e2e.mjs index 8795e98..9bbde39 100644 --- a/fork/patch-e2e.mjs +++ b/fork/patch-e2e.mjs @@ -133,6 +133,19 @@ test("an image by link or as a raw image_url part goes to agy as inline base64", } }) +// Chat routing (local patch): no openai-compatible model in the chat catalog, +// so magpie routes effort-suffixed ids as Messages; AGY_BRIDGE_IMAGE_MODEL=1 lists it. +test("the image model stays out of provider.models unless AGY_BRIDGE_IMAGE_MODEL=1", async () => { + await withKit(async (kit) => { + const models = await kit.models() + assert.ok(Object.keys(models).length > 0) + assert.deepEqual(Object.values(models).filter((m) => m.npm !== "@ai-sdk/anthropic").map((m) => m.id), []) + }, { agyPath: null }) + await withKit(async (kit) => { + assert.ok((await kit.models())["gemini-3.1-flash-image"]) + }, { agyPath: null, env: { AGY_BRIDGE_IMAGE_MODEL: "1" } }) +}) + // agy asks the client to allow its own read_file on a path outside the media // folder (a client path), as agy 1.5.10 does; returns the bridge's answer. async function askReadFile(kit, agent, file) { diff --git a/fork/swap-agy-cli.sh b/fork/swap-agy-cli.sh index 28caaed..afdf69b 100755 --- a/fork/swap-agy-cli.sh +++ b/fork/swap-agy-cli.sh @@ -38,7 +38,7 @@ fi now=$(spec) after=$(state) # distinct patch markers present (one per patch, however many lines carry it) -p=$(grep -o -e "Video (local patch)" -e "Retry attach (local patch)" -e "Denied ending (local patch)" -e "Parallel lanes (local patch)" -e "Thinking when disabled (local patch)" \ +p=$(grep -o -e "Video (local patch)" -e "Retry attach (local patch)" -e "Denied ending (local patch)" -e "Parallel lanes (local patch)" -e "Thinking when disabled (local patch)" -e "Chat routing (local patch)" \ "$C/plugins/node_modules/@magpie-community/opencode-agy-cli-auth/index.mjs" | sort -u | wc -l | tr -d " ") h="" for _ in $(seq 1 60); do @@ -48,4 +48,4 @@ for _ in $(seq 1 60); do done echo "$old -> $now | $before -> $after | patches=$p | plugin host $h0 -> $h | backup $B" -[ "$now" = "$NEW" ] && [ "$after" = "$before" ] && [ "$p" = 5 ] && [ -n "$h" ] && [ "$h" != "$h0" ] || exit 7 +[ "$now" = "$NEW" ] && [ "$after" = "$before" ] && [ "$p" = 6 ] && [ -n "$h" ] && [ "$h" != "$h0" ] || exit 7 diff --git a/index.mjs b/index.mjs index a7fef1c..832d819 100644 --- a/index.mjs +++ b/index.mjs @@ -265,6 +265,8 @@ const FORWARD_THINKING = process.env.AGY_BRIDGE_THINKING !== "0" // on as an image block, or, from some /v1/responses shapes, as the raw // image_url / input_image part. videoizeBody downloads links and turns all of // these into upstream's base64 image or video block. AGY_BRIDGE_VIDEO=0 turns it off. +// Chat routing (local patch): see provider.models +const imageModelOn = () => process.env.AGY_BRIDGE_IMAGE_MODEL === "1" const videoOn = () => !/^(?:0|false|off|no)$/i.test(process.env.AGY_BRIDGE_VIDEO || "") const VIDEO_MAX_BYTES = 100 * 1024 * 1024 // agy's file reading refuses larger files const videoUrlTypes = new Map() // url -> image or video mime, or "" when it is neither @@ -11352,7 +11354,14 @@ export const AntigravityBridgePlugin = async () => { } : {}), } } - out[IMAGE_MODEL_ID] = imageModelEntry(PROVIDER_ID) + // Chat routing (local patch). The image model is the catalog's only + // @ai-sdk/openai-compatible entry, and with it listed magpie (0.1.4479) + // sends an id it can't find in the catalog, such as an effort-suffixed + // gemini-4-hydrogen-medium, to the plugin as /v1/chat/completions. The + // bridge answers every request as Messages, so magpie reads no reply, + // retries up to 4 times (each a real agy turn) and returns 502 "an + // empty reply". Off unless AGY_BRIDGE_IMAGE_MODEL=1. + if (imageModelOn()) out[IMAGE_MODEL_ID] = imageModelEntry(PROVIDER_ID) return out }) }, diff --git a/test/testkit.mjs b/test/testkit.mjs index f2e4905..4756ac0 100644 --- a/test/testkit.mjs +++ b/test/testkit.mjs @@ -585,7 +585,7 @@ export async function createBridgeKit(options = {}) { AGY_BRIDGE_SPARE: "0", // Fork: the local patches' switches pass through, so fork/fork.sh can run // upstream's suites with the patches off (a test's own env still wins). - ...Object.fromEntries(["AGY_BRIDGE_VIDEO", "AGY_BRIDGE_MAX_LANES", "AGY_BRIDGE_THINKING_WHEN_DISABLED", "AGY_BRIDGE_DISABLED_EFFORT"] + ...Object.fromEntries(["AGY_BRIDGE_VIDEO", "AGY_BRIDGE_MAX_LANES", "AGY_BRIDGE_THINKING_WHEN_DISABLED", "AGY_BRIDGE_DISABLED_EFFORT", "AGY_BRIDGE_IMAGE_MODEL"] .filter((key) => process.env[key] != null).map((key) => [key, process.env[key]])), ...(options.env || {}), }