kimi-work: show plan names and recommended concurrency
Verification: npx --yes bun test packages/kimi-work (123 pass, 0 fail, 608 assertions) and npx --yes bun scripts/check.mjs kimi-work passed. The new plan/cache tests failed on the old implementation with 'I.planName is not a function' and 'p.auth.accountPlan is not a function'. Real NAS account, GUI and SSE verification are not yet run.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
# Kimi Work 0.1.20 compatibility
|
||||
# Kimi Work 0.1.21 compatibility
|
||||
|
||||
## Scope
|
||||
|
||||
@@ -6,6 +6,10 @@ This candidate incorporates upstream `3b3209e0ddd2c47ee5c8f77b5541131e0aa178fe`
|
||||
|
||||
The historical 2026-10-24 live renewal test at `f67d742` used Apple-silicon macOS and official Kimi Desktop **3.2.17** (build 3.2.17.4). A fresh, user-completed login belongs to disposable profiles. The installed application and everyday Magpie/Kimi profiles are not the test credential source. A temporary application copy keeps the original `app.asar` byte-for-byte; its main-source SHA-256 is `6a37077974207f7b501add47d9756eaff8d41eb87347e05b11f59f7b2386c439`. Test-only launch instrumentation redirects profile paths and exposes the original AuthService and TokenStore methods for invocation/observation; no refresh response is substituted.
|
||||
|
||||
## Plan name and recommended concurrency (0.1.21)
|
||||
|
||||
`auth.accountPlan` returns the trimmed subscription name from the same `GetSubscription` response as the numeric limit. A legacy reply with only `LEVEL_BASIC`, `LEVEL_INTERMEDIATE` or `LEVEL_ADVANCED` displays Basic, Intermediate or Advanced. An unknown level without a name stays blank rather than inventing a plan. The name and concurrency share the five-minute success cache, one-minute failure backoff and same-account in-flight request. Temporary failures retain both last known values; away or tokenless accounts disclose neither another account's name nor its limit. Magpie's account row shows the plan, and the Automatic menu shows its recommended concurrency. The separate optional hook leaves `accountConcurrency` compatible with older cores.
|
||||
|
||||
## Plan-based concurrency (0.1.20)
|
||||
|
||||
`auth.accountConcurrency` reads `MembershipService/GetSubscription` with the account's own membership access and maps its `subscription.plan` to the Agent-task limits: Free/Andante/Go 1, Moderato/Plus and Allegretto/Pro 2, Allegro/Max/Ultra 4. The observed `LEVEL_BASIC` and `LEVEL_INTERMEDIATE` values map to 2 and `LEVEL_ADVANCED` to 4; no other numeric enum is guessed. A name/level conflict uses the smaller value. Unknown plans, missing tokens and failed reads use 1 unless a last successful value is available.
|
||||
|
||||
@@ -9,6 +9,8 @@ magpie plugin login kimi-work
|
||||
|
||||
Open Kimi desktop and sign in to Work on the same computer first. Choose **使用本机 Kimi Work 登录** in the provider's sign-in menu. If the desktop provides a membership access/refresh pair, explicit sign-in imports it into the host's auth store and Magpie renews it directly with Kimi near expiry, including while the browser and desktop app are closed. Older desktops without a refresh token still sign in as reference-only accounts and read membership access from the desktop file. This package does not capture browser cookies or offer an API-key fallback.
|
||||
|
||||
Version **0.1.21** reports each account's subscription name through `auth.accountPlan`, sharing the same subscription request and cache as `auth.accountConcurrency`. Magpie shows the detected name beside the account, and its Automatic concurrency menu shows the plan's recommended limit. The numeric concurrency hook remains unchanged, so older Magpie versions that ignore `accountPlan` retain plan-based automatic limits.
|
||||
|
||||
Version **0.1.20** reports each account's Agent-task concurrency to Magpie through `auth.accountConcurrency`. Free, Andante and Go allow 1; Moderato/Plus and Allegretto/Pro allow 2; Allegro/Max and Ultra allow 4. The known numeric plan levels map Basic and Intermediate to 2 and Advanced to 4. If a name and level disagree, the smaller limit wins; an unknown plan or unavailable membership read falls back to 1, with the last successful value retained during temporary failures. Successful plan reads are cached per UID for five minutes, failures back off for one minute, and concurrent reads share one request. An away reference-only desktop account never borrows the current desktop user's membership token.
|
||||
|
||||
Magpie's precedence is **account manual limit → provider-wide manual limit → account plan value → plugin default 1**. Leave the provider's **Requests at once** field empty for plan-based automatic limits, or enter a number to override every account. Each account's own concurrency menu can override either setting; choosing **Automatic** removes only that account override. The automatic hook requires the matching Magpie core update; older Magpie versions continue using the plugin's static default of 1.
|
||||
|
||||
@@ -510,6 +510,12 @@ function planConcurrency(plan) {
|
||||
const known=[names[name],levels[level]].filter(Number.isInteger);
|
||||
return known.length?Math.min(...known):1;
|
||||
}
|
||||
function planName(plan) {
|
||||
const name=String(pick(plan,'name','name')??'').trim();
|
||||
if(name)return name;
|
||||
const level=String(pick(plan,'level','level')??'').trim().toUpperCase();
|
||||
return {LEVEL_BASIC:'Basic',LEVEL_INTERMEDIATE:'Intermediate',LEVEL_ADVANCED:'Advanced'}[level]||'';
|
||||
}
|
||||
const EFFORTS=new Set(['off','low','medium','high','xhigh','max']);
|
||||
function workConfigRows(payload) {
|
||||
const list=[...(Array.isArray(pick(payload,'models','models'))?pick(payload,'models','models'):[]),
|
||||
@@ -654,16 +660,17 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo
|
||||
} finally {if(renewing.get(key)===run)renewing.delete(key);}
|
||||
}
|
||||
// A plan's Agent task allowance is one request lane in magpie. Keep the
|
||||
// last good value if membership is temporarily unavailable, and never let
|
||||
// this auxiliary read invalidate or block the independent Work credential.
|
||||
// last good name and value if membership is temporarily unavailable, and
|
||||
// never let this auxiliary read invalidate or block the Work credential.
|
||||
const planLimits=new Map(),planRuns=new Map(),planFailures=new Map();
|
||||
async function accountConcurrency(getAuth,{signal}={}) {
|
||||
async function planDetails(getAuth,{signal}={}) {
|
||||
const fallback=cached=>({value:cached?.value??1,name:cached?.name??''});
|
||||
const auth=await getAuth();
|
||||
if(!valid(auth))return 1;
|
||||
if(!valid(auth))return fallback();
|
||||
const uid=auth.uid,cached=planLimits.get(uid),at=now();
|
||||
if(cached&&at-cached.at<PLAN_CACHE)return cached.value;
|
||||
if(cached&&at-cached.at<PLAN_CACHE)return cached;
|
||||
const failed=planFailures.get(uid);
|
||||
if(failed!==undefined&&failed+PLAN_RETRY>at)return cached?.value??1;
|
||||
if(failed!==undefined&&failed+PLAN_RETRY>at)return fallback(cached);
|
||||
let run=planRuns.get(uid);
|
||||
if(!run)run=(async()=>{
|
||||
try {
|
||||
@@ -671,24 +678,30 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo
|
||||
if(hasPair(auth))token=auth.access;
|
||||
else {
|
||||
const s=current(auth);
|
||||
if(s.away)return cached?.value??1;
|
||||
if(s.away)return fallback(cached);
|
||||
token=s.webToken;
|
||||
}
|
||||
if(!token)return cached?.value??1;
|
||||
if(!token)return fallback(cached);
|
||||
const j=await jsonRequest(MEMBER+'GetSubscription',token,{},signal);
|
||||
const subscription=pick(j,'subscription','subscription'),plan=pick(subscription,'plan','plan');
|
||||
const value=planConcurrency(plan);
|
||||
planLimits.set(uid,{value,at:now()});planFailures.delete(uid);
|
||||
return value;
|
||||
const details={value:planConcurrency(plan),name:planName(plan),at:now()};
|
||||
planLimits.set(uid,details);planFailures.delete(uid);
|
||||
return details;
|
||||
} catch(error) {
|
||||
planFailures.set(uid,now());
|
||||
console.warn('kimi-work: GetSubscription failed ('+(error?.status?'HTTP '+error.status:error?.message||String(error))+'); using the last known plan concurrency');
|
||||
return cached?.value??1;
|
||||
console.warn('kimi-work: GetSubscription failed ('+(error?.status?'HTTP '+error.status:error?.message||String(error))+'); using the last known plan details');
|
||||
return fallback(cached);
|
||||
}
|
||||
})();
|
||||
planRuns.set(uid,run);
|
||||
try{return await run;}finally{if(planRuns.get(uid)===run)planRuns.delete(uid);}
|
||||
}
|
||||
async function accountConcurrency(getAuth,options) {
|
||||
return (await planDetails(getAuth,options)).value;
|
||||
}
|
||||
async function accountPlan(getAuth,options) {
|
||||
return (await planDetails(getAuth,options)).name;
|
||||
}
|
||||
// Work's menu, as the desktop asks it with its web sign-in. When that
|
||||
// ask fails (the web sign-in in Kimi's file has gone stale, which
|
||||
// www.kimi.com answers 401 "invalid user token"; any other status; a
|
||||
@@ -774,6 +787,7 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo
|
||||
provider:ID,
|
||||
maxConcurrency:1,
|
||||
accountConcurrency,
|
||||
accountPlan,
|
||||
importFrom:IMPORT_FROM.en,
|
||||
importFroms:IMPORT_FROM.froms,
|
||||
import:(texts,{signal}={})=>importFiles(texts,signal),
|
||||
@@ -926,4 +940,4 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch,platfo
|
||||
}
|
||||
|
||||
export default async function KimiWorkProvider(){return createPlugin();}
|
||||
export const _internal={createPlugin,planConcurrency,fastSpeed,fastIds,spentQuota,moonshotTools,moonshotSchema,capOutputTokens,stableDeviceId,readDesktop,readKeys,desktopFile,shareDir,userDataDir,modelRows,workConfigRows,BASE,MEMBER,CONFIG,UA,workHeaders,webHeaders,refreshHeaders,readDeviceId,REFRESH,REFRESH_LEAD,expiry};
|
||||
export const _internal={createPlugin,planConcurrency,planName,fastSpeed,fastIds,spentQuota,moonshotTools,moonshotSchema,capOutputTokens,stableDeviceId,readDesktop,readKeys,desktopFile,shareDir,userDataDir,modelRows,workConfigRows,BASE,MEMBER,CONFIG,UA,workHeaders,webHeaders,refreshHeaders,readDeviceId,REFRESH,REFRESH_LEAD,expiry};
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-kimi-work-auth",
|
||||
"version": "0.1.20",
|
||||
"version": "0.1.21",
|
||||
"type": "module",
|
||||
"main": "./opencode.mjs",
|
||||
"files": [
|
||||
|
||||
@@ -15,6 +15,11 @@ test('every current and legacy Kimi Work plan maps to its Agent task concurrency
|
||||
[{level:'LEVEL_FUTURE',name:'Unannounced'},1],[{},1],[null,1],
|
||||
];
|
||||
for(const [plan,want] of cases)expect(I.planConcurrency(plan)).toBe(want);
|
||||
expect(I.planName({name:' Moderato '})).toBe('Moderato');
|
||||
expect(I.planName({level:'LEVEL_BASIC'})).toBe('Basic');
|
||||
expect(I.planName({level:'LEVEL_INTERMEDIATE'})).toBe('Intermediate');
|
||||
expect(I.planName({level:'LEVEL_ADVANCED'})).toBe('Advanced');
|
||||
expect(I.planName({level:'LEVEL_FUTURE'})).toBe('');
|
||||
});
|
||||
|
||||
test('each account reads its own subscription and caches a successful result for five minutes',async()=>{
|
||||
@@ -24,11 +29,13 @@ test('each account reads its own subscription and caches a successful result for
|
||||
const token=new Headers(init.headers).get('authorization');
|
||||
return reply(token==='Bearer web-a'?{name:'Moderato'}:{name:'Allegro'});
|
||||
}});
|
||||
const [a,b]=await Promise.all([
|
||||
const [a,b,aPlan,bPlan]=await Promise.all([
|
||||
p.auth.accountConcurrency(async()=>auth('a')),
|
||||
p.auth.accountConcurrency(async()=>auth('b')),
|
||||
p.auth.accountPlan(async()=>auth('a')),
|
||||
p.auth.accountPlan(async()=>auth('b')),
|
||||
]);
|
||||
expect([a,b]).toEqual([2,4]);
|
||||
expect([a,b,aPlan,bPlan]).toEqual([2,4,'Moderato','Allegro']);
|
||||
expect(calls).toHaveLength(2);
|
||||
expect(calls.every(c=>c.url===I.MEMBER+'GetSubscription')).toBe(true);
|
||||
clock+=4*60*1000;
|
||||
@@ -52,17 +59,22 @@ test('a failed refresh keeps the last known limit, and an unknown account stays
|
||||
const p=I.createPlugin({now:()=>clock,fetchImpl:async()=>{calls++;if(fail)return new Response('{}',{status:503});return reply({name:'Pro'});}});
|
||||
const getAuth=async()=>auth('known');
|
||||
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
||||
expect(await p.auth.accountPlan(getAuth)).toBe('Pro');
|
||||
clock+=5*60*1000;fail=true;
|
||||
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
||||
expect(await p.auth.accountPlan(getAuth)).toBe('Pro');
|
||||
expect(await p.auth.accountConcurrency(getAuth)).toBe(2);
|
||||
expect(calls).toBe(2,'a failure backs off for one minute');
|
||||
expect(await p.auth.accountConcurrency(async()=>auth('unknown'))).toBe(1);
|
||||
expect(await p.auth.accountPlan(async()=>auth('unknown'))).toBe('');
|
||||
});
|
||||
|
||||
test('an away desktop account never borrows the current user web token',async()=>{
|
||||
let calls=0;
|
||||
const p=I.createPlugin({readSession:()=>({uid:'current',workToken:'work-current',webToken:'web-current',keys:{away:'work-away'}}),fetchImpl:async()=>{calls++;return reply({name:'Max'});}});
|
||||
expect(await p.auth.accountConcurrency(async()=>({source:'kimi-work-desktop',uid:'away'}))).toBe(1);
|
||||
const getAuth=async()=>({source:'kimi-work-desktop',uid:'away'});
|
||||
expect(await p.auth.accountConcurrency(getAuth)).toBe(1);
|
||||
expect(await p.auth.accountPlan(getAuth)).toBe('');
|
||||
expect(calls).toBe(0);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user