From bf7ced3156c2e2ce0d317803848b608d4a35576f Mon Sep 17 00:00:00 2001 From: yetone Date: Sat, 3 Oct 2026 16:10:04 +0800 Subject: [PATCH] =?UTF-8?q?qoder=200.2.4:=20Qoder's=20token=20usage=20is?= =?UTF-8?q?=20read=20=E2=80=94=20it=20comes=20in=20a=20chunk=20of=20its=20?= =?UTF-8?q?own=20after=20the=20finish,=20with=20no=20choices,=20and=20the?= =?UTF-8?q?=20stream=20was=20read=20only=20up=20to=20the=20finish,=20so=20?= =?UTF-8?q?every=20request=20through=20the=20plugin=20was=20counted=20as?= =?UTF-8?q?=200=20tokens=20(found=20testing=20a=20real=20qmodel=20account:?= =?UTF-8?q?=20prompt=5Ftokens=200=20on=20every=20reply).=20events()=20now?= =?UTF-8?q?=20reads=20to=20[DONE],=20keeps=20the=20finish=20reason,=20and?= =?UTF-8?q?=20takes=20the=20usage,=20cached=20and=20reasoning=20tokens=20i?= =?UTF-8?q?ncluded,=20from=20whichever=20chunk=20carries=20it,=20as=20the?= =?UTF-8?q?=20built-in=20did=20(qoder.go).=20stream.test.mjs,=20a=20reply?= =?UTF-8?q?=20shaped=20as=20a=20real=20one,=20fails=20on=20the=20old=20cod?= =?UTF-8?q?e=20and=20passes.=20Tried=20with=20a=20real=20Qoder=20account?= =?UTF-8?q?=20in=20a=20sandbox=20magpie:=20chat,=20streamed=20chat=20and?= =?UTF-8?q?=20an=20Anthropic=20tool=20call=20all=20report=20their=20tokens?= =?UTF-8?q?=20(40=20in=20/=20120=20out,=20107=20reasoning;=20298=20/=2086?= =?UTF-8?q?=20with=20the=20tool).?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/qoder/index.mjs | 22 +++++++++++++++------- packages/qoder/package.json | 2 +- packages/qoder/stream.test.mjs | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 49 insertions(+), 8 deletions(-) create mode 100644 packages/qoder/stream.test.mjs diff --git a/packages/qoder/index.mjs b/packages/qoder/index.mjs index ca6aa66..e4b2f4c 100644 --- a/packages/qoder/index.mjs +++ b/packages/qoder/index.mjs @@ -726,6 +726,7 @@ async function* events(body) { yield { error: failure(Number(env.statusCodeValue), inner || payload) } return } + if (inner.trim() === "[DONE]") break let chunk try { chunk = JSON.parse(inner) @@ -749,12 +750,19 @@ async function* events(body) { yield { tool: { index: ++index, id: tc.id || "call_" + hexID(), name: tc.function?.name ?? "", args: tc.function?.arguments ?? "" } } } else if (tc.function?.arguments) yield { args: { index, text: tc.function.arguments } } } - fr = chunk?.choices?.[0]?.finish_reason ?? "" - if (fr) { - const u = chunk.usage - if (u) usage = { prompt_tokens: u.prompt_tokens ?? 0, completion_tokens: u.completion_tokens ?? 0, total_tokens: (u.prompt_tokens ?? 0) + (u.completion_tokens ?? 0) } - break - } + fr = chunk?.choices?.[0]?.finish_reason || fr + // the usage comes in a chunk of its own after the finish, with no + // choices, as the built-in read it (qoder.go): every request was + // counted as 0 tokens when the finish ended the reading + const u = chunk?.usage + if (u) + usage = { + prompt_tokens: u.prompt_tokens ?? 0, + completion_tokens: u.completion_tokens ?? 0, + total_tokens: (u.prompt_tokens ?? 0) + (u.completion_tokens ?? 0), + ...(u.prompt_tokens_details?.cached_tokens ? { prompt_tokens_details: { cached_tokens: u.prompt_tokens_details.cached_tokens } } : {}), + ...(u.completion_tokens_details?.reasoning_tokens ? { completion_tokens_details: { reasoning_tokens: u.completion_tokens_details.reasoning_tokens } } : {}), + } } yield* flushed() yield { stop: a.sawTool ? "tool_calls" : fr === "length" ? "length" : "stop", usage } @@ -1245,4 +1253,4 @@ export const QoderAuthPlugin = makePlugin(SITES.qoder) export const QoderCNAuthPlugin = makePlugin(SITES["qoder-cn"]) // for tests -export const _internal = { parseUsage, gfmt, when, failure, modelInfo, SITES, decodeBody } +export const _internal = { parseUsage, gfmt, when, failure, modelInfo, SITES, decodeBody, events } diff --git a/packages/qoder/package.json b/packages/qoder/package.json index e6adac5..ca2a99c 100644 --- a/packages/qoder/package.json +++ b/packages/qoder/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-qoder-auth", - "version": "0.2.3", + "version": "0.2.4", "description": "Qoder subscriptions in OpenCode and magpie: Qoder (qoder.com) and Qoder CN (qoder.cn)", "type": "module", "main": "./index.mjs", diff --git a/packages/qoder/stream.test.mjs b/packages/qoder/stream.test.mjs new file mode 100644 index 0000000..8146ccb --- /dev/null +++ b/packages/qoder/stream.test.mjs @@ -0,0 +1,33 @@ +// Qoder's stream gives the usage in a chunk of its own after the finish, +// with no choices (as a real qmodel reply did); read up to the finish only, +// every request was counted as 0 tokens. +import { expect, test } from "bun:test" +import { _internal } from "./index.mjs" + +const line = (body) => `data:${JSON.stringify({ headers: {}, body: typeof body === "string" ? body : JSON.stringify(body), statusCodeValue: 200, statusCode: "OK" })}\n\n` + +test("the usage after the finish is read", async () => { + const sse = + line({ choices: [{ delta: { content: "Hi", reasoning_content: "" }, index: 0 }], object: "chat.completion.chunk" }) + + line({ choices: [{ delta: { content: "" }, finish_reason: "stop", index: 0 }], object: "chat.completion.chunk" }) + + line({ + choices: [], + object: "chat.completion.chunk", + usage: { completion_tokens: 106, completion_tokens_details: { reasoning_tokens: 94 }, prompt_tokens: 40, prompt_tokens_details: { cached_tokens: 32 }, total_tokens: 146 }, + }) + + line("[DONE]") + + `data:${JSON.stringify({ firstTokenDuration: 821, totalDuration: 1982 })}\n\n` + const out = [] + for await (const e of _internal.events(new Response(sse).body)) out.push(e) + expect(out.map((e) => e.text).filter(Boolean).join("")).toBe("Hi") + expect(out.at(-1)).toEqual({ + stop: "stop", + usage: { + prompt_tokens: 40, + completion_tokens: 106, + total_tokens: 146, + prompt_tokens_details: { cached_tokens: 32 }, + completion_tokens_details: { reasoning_tokens: 94 }, + }, + }) +})