diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index 7afe670..b01ca3a 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -1729,6 +1729,12 @@ const QUEUE = { recoveries: 10, // the queue waited out this many times, at most, for one request failures: 3, // status asks failing in a row that end the wait ask: 30_000, // the longest one status ask may take + // the longest a call waits for Qoder's first event (headers included). + // Qoder CN once held a call 15 minutes with nothing sent and nothing to + // log (#1204); after this it is a 504 that says so, which magpie can + // fail over from. A queued call answers its 10605 at once, and a + // thinking model streams its reasoning, so a real answer starts sooner. + silent: 120_000, now: () => Date.now(), // every calls fn each ms until the returned stop is called every: (fn, ms) => { @@ -3792,11 +3798,22 @@ const makePlugin = (site) => async ({ client }) => { const call = new AbortController() const callSignal = signal ? AbortSignal.any([signal, call.signal]) : call.signal let res, open + let silent = false + const hush = setTimeout(() => { + silent = true + call.abort() + }, QUEUE.silent) try { res = await fetch(CHAT_URL, { method: "POST", headers, body: wire, signal: callSignal }) if (res.ok) open = await opened(res, chat.tool_choice !== "none" ? (chat.tools ?? []).filter((t) => t?.type === "function") : []) } catch (e) { + if (silent) { + const who = siteOf(site, cred).name ?? "Qoder" + return errorResponse({ status: 504, message: `${who} sent nothing for ${Math.round(QUEUE.silent / 1000)}s after the request (${res ? "no first event" : "no reply headers"}); the call was dropped`, signIn: "" }) + } return errorResponse({ status: 502, message: e.message }) + } finally { + clearTimeout(hush) } const f = !res.ok ? failure(res.status, (await res.text()).slice(0, 1 << 20)) : open.first.value?.error if (f) call.abort() diff --git a/packages/qoder/package.json b/packages/qoder/package.json index 58c4c90..055bca7 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.69", + "version": "0.2.70", "description": "Qoder and QwenWork (千问办公) subscriptions in OpenCode and magpie: qoder.com, qoder.cn, gateway.qwenwork.cn and the enterprise flagship, VPC and dedicated domains", "type": "module", "main": "./opencode.mjs", diff --git a/packages/qoder/silent.test.mjs b/packages/qoder/silent.test.mjs new file mode 100644 index 0000000..2330188 --- /dev/null +++ b/packages/qoder/silent.test.mjs @@ -0,0 +1,83 @@ +// Qoder CN once held a call 15 minutes with nothing sent: no reply +// headers, nothing logged, and the client gave up first (xiaotao6869, +// #1204). A call that hears nothing for QUEUE.silent is dropped, and its +// 504 says so, so magpie can fail over and the log shows where it waited. +// The upstream here never answers until its signal is aborted; the reply +// that sends headers and then nothing is the same wait, one step later. +import { afterEach, expect, test } from "bun:test" +import { QoderAuthPlugin, _internal } from "./index.mjs" + +const { QUEUE } = _internal +const real = globalThis.fetch +const saved = { ...QUEUE } +afterEach(() => { + globalThis.fetch = real + Object.assign(QUEUE, saved) +}) + +const API_CHAT = "https://api3.qoder.sh/v1/chat/completions" +const LISTING = { chat: [{ key: "dfmodel", display_name: "DeepSeek Flash", enable: true }] } +const json = (v, status = 200) => new Response(JSON.stringify(v), { status, headers: { "Content-Type": "application/json" } }) +const hang = (signal) => + new Promise((_, no) => signal?.addEventListener("abort", () => no(signal.reason ?? new Error("aborted")), { once: true })) + +async function loader(chat) { + const auth = { type: "oauth", access: "jt", refresh: "rt", expires: Date.now() + 3_600_000, accountId: "one@x", uid: "u1", machineId: "m1" } + let calls = 0 + globalThis.fetch = async (url, init = {}) => { + const u = new URL(String(url)) + if (u.pathname.endsWith("/model/list")) return json(LISTING) + calls++ + return chat(init.signal) + } + const hooks = await QoderAuthPlugin({ client: { auth: { set: async () => {} } } }) + const l = await hooks.auth.loader(async () => auth) + return { fetch: l.fetch, calls: () => calls } +} + +for (const stream of [false, true]) { + test(`a call Qoder sends no reply headers to is dropped after QUEUE.silent with a 504 that says so (stream ${stream})`, async () => { + QUEUE.silent = 50 + const { fetch, calls } = await loader((signal) => hang(signal)) + const began = Date.now() + const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", stream, messages: [{ role: "user", content: "hi" }] }) }) + expect(res.status).toBe(504) + expect((await res.json()).error.message).toMatch(/sent nothing for 0s after the request \(no reply headers\)/) + expect(res.headers.get("X-Magpie-Sign-In")).toBeNull() + expect(Date.now() - began).toBeLessThan(5_000) + expect(calls()).toBe(1) + }) +} + +test("a reply whose headers came and whose first event never does is dropped the same way", async () => { + QUEUE.silent = 50 + const { fetch } = await loader( + (signal) => + new Response( + new ReadableStream({ + start(ctl) { + signal?.addEventListener("abort", () => ctl.error(signal.reason ?? new Error("aborted")), { once: true }) + }, + }), + { headers: { "Content-Type": "text/event-stream" } }, + ), + ) + const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", messages: [{ role: "user", content: "hi" }] }) }) + expect(res.status).toBe(504) + expect((await res.json()).error.message).toMatch(/no first event/) +}) + +test("a reply that answers within QUEUE.silent is not cut, and the timer is gone after it", async () => { + QUEUE.silent = 200 + const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200 })}\n\n` + const { fetch } = await loader(async () => { + await new Promise((r) => setTimeout(r, 20)) + return new Response(line({ choices: [{ delta: { content: "hello" } }] }) + line({ choices: [{ delta: {}, finish_reason: "stop" }] }) + line("[DONE]"), { + headers: { "Content-Type": "text/event-stream" }, + }) + }) + const res = await fetch(API_CHAT, { method: "POST", body: JSON.stringify({ model: "dfmodel", messages: [{ role: "user", content: "hi" }] }) }) + expect(res.status).toBe(200) + expect((await res.json()).choices[0].message.content).toBe("hello") + await new Promise((r) => setTimeout(r, 300)) +})