diff --git a/packages/codearts/index.mjs b/packages/codearts/index.mjs index 2d638ca..798a3b9 100644 --- a/packages/codearts/index.mjs +++ b/packages/codearts/index.mjs @@ -1859,6 +1859,13 @@ async function signedFetch(cred, input, init, context) { body = utf8(JSON.stringify(chat.body)) continue } + // The account has no free channel at all (`InferHub.APIG.0101.404`, #905): + // the same model without the header is its plain one. + if (extra && /InferHub\.APIG\.0101\.404/i.test(code || text)) { + context.state?.noBenefit.add(model) + extra = undefined + continue + } if (extra && /InferHub\.400[45]\.200|benefit not found/i.test(code || text)) { // A new Beijing day whose benefit isn't claimed yet: claim it, as the // agent does each day, and send it again as the free model. @@ -1896,17 +1903,33 @@ async function signedFetch(cred, input, init, context) { /** A signed GET returning the response text, or undefined on any failure. */ async function signedGet(url, cred, fetcher, extraUnsignedHeaders) { + const answer = await signedRead(url, cred, fetcher, extraUnsignedHeaders) + return answer?.ok ? answer.text : undefined +} + +/** A signed GET's answer whatever its status (`{ ok, status, text }`), undefined when the vendor wasn't reached. */ +async function signedRead(url, cred, fetcher, extraUnsignedHeaders) { try { const headers = await signFor(cred, "GET", url, new Uint8Array()) for (const [k, v] of Object.entries(extraUnsignedHeaders ?? {})) headers.set(k, v) const res = await fetcher(url, { method: "GET", headers, redirect: "error", signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) }) - if (!res.ok) return undefined - return await res.text() + return { ok: res.ok, status: res.status, text: await res.text() } } catch { return undefined } } +/** + * gateway/config's own answer that the account has no free channel + * (`{"error_code":"4000","error_msg":"invalid channel, group not found"}`, + * #905): a list of no free models, not a failed read. + */ +function isNoBenefitChannel(text) { + const raw = safeJson(text) + if (!raw || typeof raw !== "object") return false + return String(raw.error_code ?? "") === "4000" || /group not found/i.test(String(raw.error_msg ?? "")) +} + /** A failed answer as one line: the gateway's own code and words, not just the status. */ function describeHttpFailure(status, text) { const raw = safeJson(text) @@ -2333,8 +2356,14 @@ async function fetchRemoteModels(cred, fetcher, state) { const seen = new Set() const benefitIds = [] - const gatewayText = await signedGet(OPENGW_GATEWAY_CONFIG_URL, cred, fetcher) - const gateway = gatewayText === undefined ? undefined : extractArray(gatewayText, ["result", "models"]) + const gatewayAnswer = await signedRead(OPENGW_GATEWAY_CONFIG_URL, cred, fetcher) + const gateway = !gatewayAnswer + ? undefined + : isNoBenefitChannel(gatewayAnswer.text) + ? [] + : gatewayAnswer.ok + ? extractArray(gatewayAnswer.text, ["result", "models"]) + : undefined const claimed = gateway?.length ? (state ? await ensureBenefitClaimed(cred, fetcher, state) : true) : false for (const item of claimed ? gateway : []) { if (typeof item !== "object" || item === null) continue diff --git a/packages/codearts/index.test.mjs b/packages/codearts/index.test.mjs index f845bb3..9424029 100644 --- a/packages/codearts/index.test.mjs +++ b/packages/codearts/index.test.mjs @@ -2105,6 +2105,63 @@ test("a benefit the account can't claim: no free models, and no benefit header", assert.deepEqual(sent, [null]) }) +for (const status of [200, 400]) { + test(`gateway/config's "group not found" (HTTP ${status}) is an account with no free models, not an unread list (#905)`, async () => { + const cred = await makeCred() + const list = modelsFetcher([]) + const sent = [] + const fetcher = async (url, init) => { + if (String(url) === CHAT_URL) { + sent.push(new Headers(init.headers).get("maas_type")) + return sseResponse(okStream) + } + if (String(url) === x.OPENGW_GATEWAY_CONFIG_URL) { + return new Response('{"error_code":"4000","error_msg":"invalid channel, group not found","result":null}', { status }) + } + return list(url, init) + } + const plugin = await CodeArtsAuthPlugin({ client: {} }, { fetch: fetcher }) + await plugin.provider.models({ models: {} }, { auth: makeAuth(cred) }) + const loaded = await plugin.auth.loader(async () => makeAuth(cred)) + await (await loaded.fetch(CHAT_URL, chatInit({ model: "glm-5.3-flash", messages: [] }))).text() + // The fallback list would have sent it as a free model. + assert.deepEqual(sent, [null]) + }) +} + +test("a gateway/config that fails to answer keeps the fallback list", async () => { + const state = { benefit: null, regular: null, noBenefit: new Set() } + const fetcher = async (url, init) => { + if (String(url) === x.OPENGW_GATEWAY_CONFIG_URL) return new Response("busy", { status: 503 }) + return modelsFetcher([])(url, init) + } + await x.fetchRemoteModels(signedCred, fetcher, state) + assert.equal(state.benefit, null) + assert.equal(x.isBenefitModel("glm-5.3-flash", state), true) +}) + +test("InferHub.APIG.0101.404 to the benefit header: sent again as the plain model (#905)", async () => { + const state = { benefit: null, regular: null, noBenefit: new Set(), claim: { day: x.beijingDay(), ok: false, at: Date.now() }, claiming: null } + const sent = [] + const fetcher = async (url, init) => { + const benefit = new Headers(init.headers).get("maas_type") + sent.push(benefit) + // The reporter's bytes: a 200 whose first frame is the error. + return benefit + ? sseResponse('data:{"text":"[DONE]","error_code":"InferHub.APIG.0101.404","error_msg":"The API does not exist or has not been published in the environment"}\n\n') + : sseResponse(okStream) + } + const context = { ...(await x.accountSessionIds(signedCred)), gate: x.createConcurrencyGate(3), state, fetcher } + const res = await x.signedFetch(signedCred, CHAT_URL, chatInit({ model: "glm-5.3-flash", messages: [] }), context) + assert.equal(res.status, 200) + assert.equal((await res.json()).choices[0].message.content, "你好") + assert.deepEqual(sent, ["benefit", null]) + // And the next request goes without it from the start. + sent.length = 0 + await (await x.signedFetch(signedCred, CHAT_URL, chatInit({ model: "glm-5.3-flash", messages: [] }), context)).text() + assert.deepEqual(sent, [null]) +}) + test("benefit not found on a day not yet claimed claims it, and sends the free model again", async () => { const state = { benefit: new Set(["deepseek-v4.1-flash"]), regular: new Set(["deepseek-v4.1-flash"]), noBenefit: new Set(), diff --git a/packages/codearts/package.json b/packages/codearts/package.json index ab320f6..423e773 100644 --- a/packages/codearts/package.json +++ b/packages/codearts/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-codearts-auth", - "version": "0.3.5", + "version": "0.3.6", "description": "Huawei Cloud in OpenCode and magpie: CodeArts (码道) and OfficeAce (AgentArts), each from its own Huawei Cloud OAuth sign-in — models, credit rates, credit usage and the daily check-in (unofficial)", "type": "module", "main": "./opencode.mjs",