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',()=>{