diff --git a/packages/devin/README.md b/packages/devin/README.md index 713d951..76edf1e 100644 --- a/packages/devin/README.md +++ b/packages/devin/README.md @@ -60,6 +60,9 @@ The plugin's `fetch` handles this: not: text, reasoning, tool calls and usage. - Failures keep their statuses: a refused token is a 401, a usage limit a 429, a prompt too long for the model a 400. +- A reply with only metadata and usage is a 502, even when output tokens + were counted. The plugin waits for actual content before opening a + successful stream, so the gateway can try another channel. ## Models diff --git a/packages/devin/empty-reply.test.mjs b/packages/devin/empty-reply.test.mjs new file mode 100644 index 0000000..905bc5e --- /dev/null +++ b/packages/devin/empty-reply.test.mjs @@ -0,0 +1,24 @@ +import { expect, test } from "bun:test" +import { readFile } from "node:fs/promises" +import { _internal } from "./index.mjs" + +const captured = new URL("./testdata/empty-reply.bin", import.meta.url) + +test("Devin's usage-only reply fails before a successful stream is opened", async () => { + const raw = await readFile(captured) + const real = globalThis.fetch + globalThis.fetch = async () => new Response(raw) + try { + for (const stream of [true, false]) { + const response = await _internal.complete( + { key: "test", server: "https://devin.test", families: _internal.familiesOf(_internal.SNAPSHOT) }, + { model: "gemini-4-argon", stream, messages: [{ role: "user", content: "?" }] }, + ) + expect(response.status).toBe(502) + expect((await response.json()).error.message).toContain("empty reply") + expect(response.headers.get("X-Magpie-Sign-In")).toBe("kept") + } + } finally { + globalThis.fetch = real + } +}) diff --git a/packages/devin/errors.test.mjs b/packages/devin/errors.test.mjs index d858fea..d5a295c 100644 --- a/packages/devin/errors.test.mjs +++ b/packages/devin/errors.test.mjs @@ -84,19 +84,19 @@ test("an empty reply is the built-in's 502 of a broken-off one", async () => { expect(errOf(r)).toBe("the reply broke off: EOF") }) -test("an error after a frame with nothing in it is streamed in the built-in's words", async () => { +test("an error after metadata fails before the stream opens", async () => { const empty = new Uint8Array(5) // a message frame with no fields const r = await ask(() => new Response(join(empty, end({ error: { code: "resource_exhausted", message: "rate limit" } }))), true) - expect(r.status).toBe(200) - const ev = r.body.split("\n\n").map((l) => l.replace(/^data: /, "")).find((l) => l.includes('"error"')) - expect(JSON.parse(ev).error.message).toBe("usage limit reached: rate limit") + expect(r.status).toBe(429) + expect(errOf(r)).toBe("usage limit reached: rate limit") }) // a success cleared no mark the built-in's account could have had -test("an answer says the account is kept, streamed or not", async () => { +test("a metadata-only answer fails and keeps the account signed in", async () => { for (const stream of [false, true]) { const r = await ask(() => new Response(join(new Uint8Array(5), end({}))), stream) - expect(r.status).toBe(200) + expect(r.status).toBe(502) + expect(errOf(r)).toContain("empty reply") expect(r.signIn).toBe("kept") } }) diff --git a/packages/devin/index.mjs b/packages/devin/index.mjs index 7f8acec..3d245a1 100644 --- a/packages/devin/index.mjs +++ b/packages/devin/index.mjs @@ -1636,6 +1636,10 @@ async function* events(it) { } } } + if (!said && tools < 0) { + yield { error: { status: 502, message: "an empty reply (Devin returned no text, reasoning or tool call)" } } + return + } if (!usage.output) usage.output = Math.floor((said + 3) / 4) const prompt = usage.input + usage.cacheRead + usage.cacheWrite yield { @@ -1659,6 +1663,11 @@ const errorResponse = ({ status, message }) => headers: { "Content-Type": "application/json", "X-Magpie-Sign-In": "kept" }, }) +async function* prepend(first, rest) { + yield first + yield* rest +} + // complete answers a chat completion through GetChatMessage. async function complete({ key, server, families }, chat, signal) { const uid = variantFor(families, chat.model, effortOf(chat.reasoning_effort)) @@ -1692,17 +1701,15 @@ async function complete({ key, server, families }, chat, signal) { const e = failure(200, dec.decode(first.value.data)) return errorResponse(e.status < 300 ? { status: 502, message: "an empty reply" } : e) } - let replay = first - const it = events({ - next: () => { - if (replay) { - const r = replay - replay = null - return Promise.resolve(r) - } - return fr.next() - }, - }) + // 先等到实际内容或错误,才能让网关在空回复时切换渠道。 + const source = events(prepend(first.value, fr)) + const head = await source.next() + if (head.done) return errorResponse({ status: 502, message: "an empty reply" }) + if (head.value.error) { + await fr.return?.() + return errorResponse(head.value.error) + } + const it = prepend(head.value, source) const id = "chatcmpl-" + randomBytes(12).toString("hex") const created = Math.floor(Date.now() / 1000) diff --git a/packages/devin/package.json b/packages/devin/package.json index 291e27b..a2f58b8 100644 --- a/packages/devin/package.json +++ b/packages/devin/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-devin-auth", - "version": "0.1.22", + "version": "0.1.23", "description": "Devin subscriptions (the devin CLI's account) in OpenCode and magpie", "type": "module", "main": "./index.mjs", diff --git a/packages/devin/testdata/empty-reply.bin b/packages/devin/testdata/empty-reply.bin new file mode 100644 index 0000000..0929027 Binary files /dev/null and b/packages/devin/testdata/empty-reply.bin differ