From 1fbe22b3ad7fa9bb0af94310a551a532d8431a02 Mon Sep 17 00:00:00 2001 From: yetone Date: Sun, 25 Oct 2026 17:13:43 +0800 Subject: [PATCH] kimi-work 0.1.11: a usage read that fails says why on the card and keeps the Work sign-in MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 泠夜Soul (Discord), latest magpie and kimi-work 0.1.10: the Kimi Work usage card says only "暂时无法获取额度", with no error anywhere. Cause: auth.usage threw on every failure. magpie puts a plugin's reason on the card only when usage returns it as `error` (quotaOfPlugin sets PluginError; app.js quotaError then says "暂时无法获取额度:"). A thrown error reaches the card as the bare text, with the reason in a tooltip. The read uses Kimi's web sign-in (credentials.kimiWeb.accessToken), not the Work token chat uses. Kimi desktop 3.2.20 rewrites that token in daimon config.json only while it runs (its token-change sync). Once the desktop is closed, www.kimi.com answers it 401 "Session expired" (REASON_INVALID_AUTH_TOKEN), the same stale token 0.1.10 found on the menu. That 401 was thrown as expired() with signIn "expired", so magpie also marked the Work account signed out while chat still worked. The header changes in 0.1.8-0.1.10 aren't the cause: signed out or with a bad token, GetSubscriptionStats answers the same 401 with or without the x-msh headers. Their effect with a valid token wasn't seen. What changed: - usage never throws on a failed read. It returns {windows: [], error, signIn: "kept"}. A 401 says "Kimi 网页登录已过期,打开一次 Kimi 桌面版即可刷新额度(对话不受影响)", in words that magpie's signInGone doesn't take for a lapsed sign-in. Other statuses keep their HTTP code. Each failure is logged without the token. - subscriptionBalance is read in camelCase or snake_case. A balance whose amountUsedRatio Connect JSON left out (zero) reads 0% used. Before, that gave no window at all. A reply with no balance says so. Field names are from the desktop's membership.v2 descriptor: subscription_balance is optional in GetSubscriptionStatsResponse. Run at this tree (origin/main 743e8af + this change), macOS, bun: - bun test packages/kimi-work: 53 pass, 0 fail. With index.mjs put back to 743e8af's: 50 pass, 3 fail, the three usage tests: 403 refused, stale web sign-in (fixture: www.kimi.com's literal 401 body, curl 10-25), and zero-omitted / snake_case / missing balance. - Live: createPlugin's auth.usage against the real www.kimi.com with a stale-shaped web token. New code returns the error above with signIn "kept". 743e8af's throws "请先在本机 Kimi 桌面版登录 Work…" with signIn "expired". - HOME=$(mktemp -d) bun test: 2628 pass, 0 fail. - bun scripts/check.mjs: every plugin ok. git diff --check clean. Not tried: a signed-in Kimi Work account (none on this machine), so the reporter's actual failure isn't seen. This release puts it on their card and in the log. If it's the stale web token, opening Kimi desktop refreshes it. --- packages/kimi-work/index.mjs | 26 +++++++++++++++---- packages/kimi-work/package.json | 2 +- packages/kimi-work/tests/provider.test.mjs | 29 ++++++++++++++++++++-- 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/packages/kimi-work/index.mjs b/packages/kimi-work/index.mjs index 3970803..fd40352 100644 --- a/packages/kimi-work/index.mjs +++ b/packages/kimi-work/index.mjs @@ -414,13 +414,29 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo return response; }, }), + // The allowance is read with Kimi's web sign-in (kimiWeb), not the + // Work one: the desktop rewrites it in its file only while it runs, so + // a closed desktop leaves a token www.kimi.com answers 401 "Session + // expired". Through 0.1.10 every failure was thrown, which magpie + // shows as a bare "暂时无法获取额度" with the reason only in a tooltip, + // and a 401 marked the Work account signed out though chat (Work's + // own token) still worked. A failure is now the card's error, and + // the Work sign-in is kept. usage:async getAuth=>{ const s=current(await getAuth()); - const j=await jsonRequest(MEMBER+'GetSubscriptionStats',s.webToken,{}); - const b=j.subscriptionBalance,used=percent(b?.amountUsedRatio); - return {plan:'Kimi Work · 会员共享额度',windows:used===undefined?[]:[{ - name:'会员共享额度',used, - }]}; + const plan='Kimi Work · 会员共享额度'; + let j; + try {j=await jsonRequest(MEMBER+'GetSubscriptionStats',s.webToken,{});} + catch(error) { + const why=error?.status===401?'Kimi 网页登录已过期,打开一次 Kimi 桌面版即可刷新额度(对话不受影响)' + :error?.name==='TimeoutError'?'读取 Kimi 会员额度超时':String(error?.message||error); + console.warn('kimi-work: GetSubscriptionStats failed ('+(error?.status?'HTTP '+error.status:why)+')'); + return {plan,windows:[],error:why,signIn:'kept'}; + } + // Connect JSON leaves a zero out: a balance with no ratio is 0% used. + const b=pick(j,'subscriptionBalance','subscription_balance'),used=isObj(b)?percent(pick(b,'amountUsedRatio','amount_used_ratio')??0):undefined; + if(used===undefined)return {plan,windows:[],error:'Kimi 会员额度回复里没有共享额度(subscriptionBalance)',signIn:'kept'}; + return {plan,windows:[{name:'会员共享额度',used}]}; }, }, provider:{id:ID,models:async(provider,{auth}={})=>{ diff --git a/packages/kimi-work/package.json b/packages/kimi-work/package.json index add1600..ae0cdda 100644 --- a/packages/kimi-work/package.json +++ b/packages/kimi-work/package.json @@ -1,6 +1,6 @@ { "name": "@magpie-community/opencode-kimi-work-auth", - "version": "0.1.10", + "version": "0.1.11", "type": "module", "main": "./index.mjs", "files": [ diff --git a/packages/kimi-work/tests/provider.test.mjs b/packages/kimi-work/tests/provider.test.mjs index 5f673b0..56a3d89 100644 --- a/packages/kimi-work/tests/provider.test.mjs +++ b/packages/kimi-work/tests/provider.test.mjs @@ -74,8 +74,33 @@ test('models come from Work; usage reports only the shared membership allowance' test('an upstream usage refusal does not incorrectly invalidate the desktop sign-in',async()=>{ const p=I.createPlugin({readSession:()=>session,fetchImpl:async()=>new Response('{}',{status:403})}); - try{await p.auth.usage(async()=>account);throw Error('expected upstream refusal');} - catch(error){expect(error.signIn).toBeUndefined();expect(error.message).toContain('403');} + const u=await p.auth.usage(async()=>account); + expect(u.signIn).toBe('kept');expect(u.windows).toEqual([]);expect(u.error).toContain('403'); +}); + +// www.kimi.com's literal answer to a web token it no longer takes (10-25, +// curl, signed out and with a stale token alike). +const sessionExpired={"code":"unauthenticated","details":[{"type":"common.error.v1.ErrorDetail","value":"CGQSLgoFZW4tVVMSJVNlc3Npb24gZXhwaXJlZC4gUGxlYXNlIGxvZyBpbiBhZ2Fpbi4","debug":{"reason":"REASON_INVALID_AUTH_TOKEN","localizedMessage":{"locale":"en-US","message":"Session expired. Please log in again."}}}]}; + +test('a stale web sign-in is said on the card and keeps the Work sign-in',async()=>{ + const p=I.createPlugin({readSession:()=>session,fetchImpl:async()=>Response.json(sessionExpired,{status:401})}); + const u=await p.auth.usage(async()=>account); + expect(u.windows).toEqual([]);expect(u.signIn).toBe('kept'); + expect(u.error).toContain('Kimi 网页登录已过期'); + // magpie's signInGone would mark the account signed out on these words + expect(u.error).not.toMatch(/sign-in has (expired|lapsed)|sign in again/i); + expect(u.error).not.toContain(session.webToken); +}); + +test('a balance with nothing used, its zero left out, reads 0%; no balance is said',async()=>{ + let reply={subscriptionBalance:{expireTime:'2030-01-01T00:00:00Z'}}; + const p=I.createPlugin({readSession:()=>session,fetchImpl:async()=>Response.json(reply)}); + expect((await p.auth.usage(async()=>account)).windows).toEqual([{name:'会员共享额度',used:0}]); + reply={subscription_balance:{amount_used_ratio:0.5}}; + expect((await p.auth.usage(async()=>account)).windows).toEqual([{name:'会员共享额度',used:50}]); + reply={overdrawn:false}; + const u=await p.auth.usage(async()=>account); + expect(u.windows).toEqual([]);expect(u.error).toContain('subscriptionBalance'); }); test('the real redacted catalog and additional advertised reasoning levels are preserved',()=>{