kimi-work 0.1.8: Kimi gets the headers its own Work client sends, not "Magpie Kimi Work provider" (#353)
Every request to agent-gw and kimi.com carried the User-Agent "Magpie Kimi Work provider/<version>". Kimi 3.2.20's daimon 0.6.10 (the desktop component that holds the Work credential in daimon-share/daimon/config.json) builds its agent-gw headers in createDaimonKimiCodeRequestHeaders: User-Agent kimi-code-cli/<Kimi Code version> (0.36.0, from its kimi-code.vendor.json; no caller passes a suffix), X-Msh-Device-Id, X-Msh-Device-Model, X-Msh-Device-Name, X-Msh-Os-Version, X-Msh-Platform "kimi-code-cli", X-Msh-Version and X-Msh-Daimon-Version 0.6.10. The plugin sends the same on /models and /chat/completions. The device id is the daimon's own: a randomUUID it writes once to <daimon>/runtime/kimi-code/home/device_id. The plugin reads that file and never writes it (it never writes the desktop app's files); when it is missing the header is left out. The www.kimi.com reads (membership, and 0.1.7's DescribeKimiWorkConfig menu) get the kimi-code-cli User-Agent only: those calls are made by Kimi's web page, whose own headers weren't read. The test also found magpie's x-magpie-* hint headers going through to agent-gw on chat requests; they are now dropped. Run: - no-magpie.test.mjs: models, the Work menu, chat (with a magpie User-Agent and an x-magpie-work-dir coming in) and membership hold no "magpie"; agent-gw gets the daimon's headers; the device id is read and never written. Both tests red on0a9415f(0.1.7). - bun test (package, rebased on0a9415f): 48 pass. bun scripts/check.mjs kimi-work: ok. Not tried live: no Kimi Work account here, and Kimi isn't installed on this Mac or the Windows box.
This commit is contained in:
@@ -11,8 +11,25 @@ const MEMBER = 'https://www.kimi.com/apiv2/kimi.gateway.membership.v2.Membership
|
||||
// gateway takes (requestParams.modelId). An account's tier models, K3.1 Pro
|
||||
// on the 99 plan among them, are listed here and not in /models.
|
||||
const CONFIG = 'https://www.kimi.com/apiv2/kimi.gateway.config.v1.ConfigService/DescribeKimiWorkConfig';
|
||||
const {version} = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
|
||||
const UA = 'Magpie Kimi Work provider/' + version;
|
||||
// What Kimi's own Work client sends agent-gw (Kimi 3.2.20's daimon 0.6.10,
|
||||
// createDaimonKimiCodeRequestHeaders): the vendored Kimi Code's version as
|
||||
// "kimi-code-cli/<version>" (no daimon caller passes a suffix), and the
|
||||
// X-Msh device headers beside it (magpie-community/plugins#353).
|
||||
const KIMI_CODE_VERSION = '0.36.0';
|
||||
const DAIMON_VERSION = '0.6.10';
|
||||
const UA = 'kimi-code-cli/' + KIMI_CODE_VERSION;
|
||||
const ascii = (v, fallback = 'unknown') => String(v ?? '').replace(/[^\x20-\x7e]/g, '').trim() || fallback;
|
||||
function deviceModel() {
|
||||
const t = os.type(), r = os.release(), a = os.arch();
|
||||
if (t === 'Darwin') return 'macOS ' + r + ' ' + a;
|
||||
if (t === 'Windows_NT') return 'Windows ' + r + ' ' + a;
|
||||
return (t + ' ' + r + ' ' + a).trim();
|
||||
}
|
||||
function workHeaders(deviceId) {
|
||||
return {'User-Agent':UA,...(deviceId?{'X-Msh-Device-Id':deviceId}:{}),
|
||||
'X-Msh-Device-Model':ascii(deviceModel()),'X-Msh-Device-Name':ascii(os.hostname()),'X-Msh-Os-Version':ascii(os.release()),
|
||||
'X-Msh-Platform':'kimi-code-cli','X-Msh-Version':KIMI_CODE_VERSION,'X-Msh-Daimon-Version':DAIMON_VERSION};
|
||||
}
|
||||
const expired = () => Object.assign(Error('请先在本机 Kimi 桌面版登录 Work,再重新连接 Kimi Work 供应商。'), {signIn:'expired',code:'kimi_work_login_expired'});
|
||||
const unavailable = error => Object.assign(Error('Kimi Work 连接失败:' + (error?.cause?.code || error?.message || '未知网络错误'), {cause:error}), {code:'kimi_work_connection_failed'});
|
||||
const percent = x => typeof x === 'number' && Number.isFinite(x) ? Math.max(0,Math.min(100,x*100)) : undefined;
|
||||
@@ -185,6 +202,17 @@ function desktopFile({home=os.homedir(), platform=process.platform, appdata=proc
|
||||
return path.join(parent,'kimi-desktop','daimon-share','daimon','config.json');
|
||||
}
|
||||
|
||||
// The install id Kimi's daimon made for its Kimi Code home
|
||||
// (<daimon>/runtime/kimi-code/home/device_id, a randomUUID it writes once)
|
||||
// and sends as X-Msh-Device-Id. It is read, never made or written here: the
|
||||
// desktop app owns it, and without it the header is left out.
|
||||
function readDeviceId(file) {
|
||||
try {
|
||||
const id=fs.readFileSync(path.join(path.dirname(file),'runtime','kimi-code','home','device_id'),'utf8').trim();
|
||||
return /^[\x21-\x7e]{1,128}$/.test(id)?id:undefined;
|
||||
} catch {return undefined;}
|
||||
}
|
||||
|
||||
// Read the live Work sign-in, including after rotation. Never consume its
|
||||
// refresh token or write the desktop app's configuration. The internal field
|
||||
// is called kimiCode, but this credential is issued for agent-gw (Work).
|
||||
@@ -196,7 +224,7 @@ function readDesktop(file=desktopFile()) {
|
||||
const work=doc.credentials?.kimiCode, web=doc.credentials?.kimiWeb;
|
||||
if(typeof work?.apiKey!=='string'||!work.apiKey || typeof web?.accessToken!=='string'||!web.accessToken
|
||||
|| typeof web.userId!=='string'||!web.userId || work.baseUrl?.replace(/\/$/,'')!==BASE) throw expired();
|
||||
return {workToken:work.apiKey,webToken:web.accessToken,
|
||||
return {workToken:work.apiKey,webToken:web.accessToken,deviceId:readDeviceId(file),
|
||||
uid:createHash('sha256').update('kimi-work-account\0'+web.userId).digest('hex')};
|
||||
} catch {throw expired();}
|
||||
}
|
||||
@@ -277,9 +305,9 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch}={}) {
|
||||
if(auth?.source!=='kimi-work-desktop'||auth.uid!==session.uid)throw expired();
|
||||
return session;
|
||||
};
|
||||
async function jsonRequest(url,token,body,signal) {
|
||||
async function jsonRequest(url,token,body,signal,deviceId) {
|
||||
const response=await fetchImpl(url,{method:body===undefined?'GET':'POST',
|
||||
headers:{Authorization:'Bearer '+token,'User-Agent':UA,'Accept':'application/json',
|
||||
headers:{Authorization:'Bearer '+token,...(url.startsWith(BASE)?workHeaders(deviceId):{'User-Agent':UA}),'Accept':'application/json',
|
||||
...(body===undefined?{}:{'Content-Type':'application/json','Connect-Protocol-Version':'1'})},
|
||||
...(body===undefined?{}:{body:JSON.stringify(body)}),signal:signal||AbortSignal.timeout(15000),redirect:'error'});
|
||||
if(response.status===401)throw expired();
|
||||
@@ -305,7 +333,7 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch}={}) {
|
||||
url:'',method:'auto',instructions:'读取本机 Kimi 桌面版的 Work 登录;不会使用 Kimi Code API Key。',
|
||||
callback:async()=>{
|
||||
try{
|
||||
const s=readSession();await jsonRequest(BASE+'/models',s.workToken);
|
||||
const s=readSession();await jsonRequest(BASE+'/models',s.workToken,undefined,undefined,s.deviceId);
|
||||
// Only an account reference is saved in Magpie. Actual credentials
|
||||
// stay in Kimi's own file and are reread for every operation.
|
||||
return {type:'success',access:'',refresh:'',expires:0,source:'kimi-work-desktop',
|
||||
@@ -325,8 +353,10 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch}={}) {
|
||||
const s=current(await getAuth());
|
||||
const headers=new Headers(request?request.headers:init.headers);
|
||||
for(const key of ['authorization','x-api-key','cookie','host','content-length','origin'])headers.delete(key);
|
||||
// magpie's own x-magpie-* hints are for the plugin, not for Kimi.
|
||||
for(const key of [...headers.keys()])if(key.startsWith('x-magpie-'))headers.delete(key);
|
||||
headers.set('Authorization','Bearer '+s.workToken);
|
||||
headers.set('User-Agent',UA);
|
||||
for(const [k,v] of Object.entries(workHeaders(s.deviceId)))headers.set(k,v);
|
||||
headers.set('Content-Type','application/json');
|
||||
// Preserve messages, stream and abort signal, and tools but for
|
||||
// the schema shapes Kimi refuses (moonshotTools). There is no
|
||||
@@ -362,7 +392,7 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch}={}) {
|
||||
// other; both failing throws, so the last good list is kept.
|
||||
const [cfgList,gw]=await Promise.allSettled([
|
||||
s.webToken?jsonRequest(CONFIG,s.webToken,{}).then(workConfigRows):Promise.reject(Error('no web token')),
|
||||
jsonRequest(BASE+'/models',s.workToken).then(modelRows)]);
|
||||
jsonRequest(BASE+'/models',s.workToken,undefined,undefined,s.deviceId).then(modelRows)]);
|
||||
const a=cfgList.status==='fulfilled'?cfgList.value:{},b=gw.status==='fulfilled'?gw.value:{};
|
||||
if(gw.status==='rejected'&&!Object.keys(a).length)throw gw.reason;
|
||||
const out={...a};
|
||||
@@ -375,4 +405,4 @@ function createPlugin({readSession=readDesktop,fetchImpl=globalThis.fetch}={}) {
|
||||
}
|
||||
|
||||
export default async function KimiWorkProvider(){return createPlugin();}
|
||||
export const _internal={createPlugin,moonshotTools,moonshotSchema,readDesktop,desktopFile,modelRows,workConfigRows,BASE,MEMBER,CONFIG,UA};
|
||||
export const _internal={createPlugin,moonshotTools,moonshotSchema,readDesktop,desktopFile,modelRows,workConfigRows,BASE,MEMBER,CONFIG,UA,workHeaders,readDeviceId};
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
// magpie-community/plugins#353: every request to Kimi went out with the
|
||||
// User-Agent "Magpie Kimi Work provider/<version>". Nothing sent names the
|
||||
// app now: agent-gw gets the headers Kimi's own Work client (the daimon)
|
||||
// sends, and the membership read the same User-Agent.
|
||||
import {test,expect} from 'bun:test';
|
||||
import fs from 'node:fs';
|
||||
import os from 'node:os';
|
||||
import path from 'node:path';
|
||||
import {_internal as I} from './index.mjs';
|
||||
|
||||
const account={type:'oauth',source:'kimi-work-desktop',uid:'fixture-account'};
|
||||
const session={uid:'fixture-account',workToken:'synthetic-work-token',webToken:'synthetic-web-token',deviceId:'0f8fad5b-d9cb-469f-a165-70867728950e'};
|
||||
|
||||
function record() {
|
||||
const sent=[];
|
||||
const fetchImpl=async(url,init={})=>{
|
||||
const headers=Object.fromEntries(new Headers(init.headers));
|
||||
sent.push({url:String(url),headers,body:typeof init.body==='string'?init.body:''});
|
||||
if(String(url).endsWith('/models'))return Response.json({data:[{id:'k3-agent',context_length:262144}]});
|
||||
if(String(url).includes('DescribeKimiWorkConfig'))return Response.json({});
|
||||
if(String(url).includes('GetSubscriptionStats'))return Response.json({subscriptionBalance:{amountUsedRatio:0.25}});
|
||||
return new Response('data: [DONE]\n\n',{headers:{'Content-Type':'text/event-stream'}});
|
||||
};
|
||||
return {sent,fetchImpl};
|
||||
}
|
||||
|
||||
test('no request to Kimi names magpie: models, the Work menu, chat and membership',async()=>{
|
||||
const {sent,fetchImpl}=record();
|
||||
const p=I.createPlugin({readSession:()=>session,fetchImpl});
|
||||
const loader=await p.auth.loader(async()=>account);
|
||||
await p.provider.models({models:{}},{auth:account});
|
||||
await (await loader.fetch(I.BASE+'/chat/completions',{method:'POST',
|
||||
headers:{'User-Agent':'magpie/1.0','x-magpie-work-dir':'/w'},
|
||||
body:JSON.stringify({model:'k3-agent',stream:true,messages:[{role:'user',content:'hi'}]})})).text();
|
||||
await p.auth.usage(async()=>account);
|
||||
expect(sent.length).toBe(4);
|
||||
for(const s of sent)expect(JSON.stringify(s)).not.toMatch(/magpie/i);
|
||||
for(const s of sent.filter(s=>s.url.startsWith(I.BASE))){
|
||||
expect(s.headers['user-agent']).toBe('kimi-code-cli/0.36.0');
|
||||
expect(s.headers['x-msh-platform']).toBe('kimi-code-cli');
|
||||
expect(s.headers['x-msh-version']).toBe('0.36.0');
|
||||
expect(s.headers['x-msh-device-id']).toBe(session.deviceId);
|
||||
}
|
||||
});
|
||||
|
||||
test("the device id is the daimon's own, read and never written",()=>{
|
||||
const dir=fs.mkdtempSync(path.join(os.tmpdir(),'kimi-work-device-'));
|
||||
try{
|
||||
const file=path.join(dir,'config.json');
|
||||
expect(I.readDeviceId(file)).toBeUndefined();
|
||||
expect(fs.existsSync(path.join(dir,'runtime'))).toBe(false);
|
||||
expect(I.workHeaders(undefined)).not.toHaveProperty('X-Msh-Device-Id');
|
||||
fs.mkdirSync(path.join(dir,'runtime','kimi-code','home'),{recursive:true});
|
||||
fs.writeFileSync(path.join(dir,'runtime','kimi-code','home','device_id'),'0f8fad5b-d9cb-469f-a165-70867728950e\n');
|
||||
expect(I.readDeviceId(file)).toBe('0f8fad5b-d9cb-469f-a165-70867728950e');
|
||||
}finally{fs.rmSync(dir,{recursive:true,force:true});}
|
||||
});
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/opencode-kimi-work-auth",
|
||||
"version": "0.1.7",
|
||||
"version": "0.1.8",
|
||||
"type": "module",
|
||||
"main": "./index.mjs",
|
||||
"files": [
|
||||
|
||||
@@ -43,7 +43,7 @@ test('inference always uses the Work gateway and live Work credential, preservin
|
||||
expect(calls[0].url).toBe(I.BASE+'/chat/completions');
|
||||
expect(calls[0].init.headers.get('authorization')).toBe('Bearer '+session.workToken);
|
||||
expect(calls[0].init.headers.get('x-api-key')).toBeNull();expect(calls[0].init.headers.get('cookie')).toBeNull();
|
||||
expect(calls[0].init.headers.get('user-agent')).toBe(I.UA);
|
||||
expect(calls[0].init.headers.get('user-agent')).toBe('kimi-code-cli/0.36.0');
|
||||
expect(calls[0].init.body).toBe(body);expect(calls[0].init.signal).toBe(signal);expect(calls[0].init.redirect).toBe('error');
|
||||
live={...live,workToken:'synthetic-rotated-work-token'};await loader.fetch(I.BASE+'/chat/completions',init);
|
||||
expect(calls[1].init.headers.get('authorization')).toBe('Bearer synthetic-rotated-work-token');
|
||||
|
||||
Reference in New Issue
Block a user