Files
plugins/packages/kimi-work/official-compat.test.mjs
antior 8430d83306
bumped / bumped (push) Successful in 1h17m39s
publish / test (push) Failing after 1h28m54s
publish / dist (push) Skipped
publish / npm (push) Skipped
kimi-work 0.1.19: align requests with the official client
Match Kimi Code 0.5.1, daimon 1.0.14 and desktop 3.2.28 headers. Add standard context, Work tag, traceparent, stable account device IDs and model output caps.

Verified: node --check packages/kimi-work/index.mjs; npx --yes bun test packages/kimi-work (117 pass); npx --yes bun scripts/check.mjs kimi-work.
2026-11-20 17:17:32 +08:00

58 lines
3.4 KiB
JavaScript

import {test,expect} from 'bun:test';
import {_internal as I} from './index.mjs';
const session={uid:'fixture-account',workToken:'synthetic-work-token',webToken:'synthetic-web-token',webRefresh:'synthetic-refresh-token'};
const account={source:'kimi-work-desktop',uid:session.uid,access:session.webToken,refresh:session.webRefresh,expires:0};
test('chat matches the current official device, context, Work and trace headers',async()=>{
const calls=[];
const p=I.createPlugin({readSession:()=>session,fetchImpl:async(url,init)=>{calls.push(init);return new Response('data: [DONE]\n\n');}});
const loader=await p.auth.loader(async()=>account);
await loader.fetch(I.BASE+'/chat/completions',{method:'POST',body:'{"model":"k3-agent","messages":[]}'});
const first=new Headers(calls[0].headers),device=I.stableDeviceId(session.uid);
expect(first.get('user-agent')).toBe('kimi-code-cli/0.5.1');
expect(first.get('x-msh-version')).toBe('0.5.1');
expect(first.get('x-msh-daimon-version')).toBe('1.0.14');
expect(first.get('x-msh-device-id')).toBe(device);
expect(first.get('x-kimi-context-length')).toBe('262144');
expect(first.get('x-kimiwork-tag')).toBe('normal');
expect(first.get('traceparent')).toMatch(/^00-[0-9a-f]{32}-[0-9a-f]{16}-01$/);
await loader.fetch(I.BASE+'/chat/completions',{method:'POST',body:'{"model":"k3-agent","messages":[]}',headers:{
'X-Kimi-Context-Length':'1000000',traceparent:'00-0123456789abcdef0123456789abcdef-0123456789abcdef-01'}});
const second=new Headers(calls[1].headers);
expect(second.get('x-kimi-context-length')).toBe('1000000');
expect(second.get('traceparent')).toBe('00-0123456789abcdef0123456789abcdef-0123456789abcdef-01');
expect(second.get('x-msh-device-id')).toBe(device);
});
test('the account UUID is deterministic, namespaced and version 5',()=>{
const id=I.stableDeviceId('fixture-account');
expect(id).toBe(I.stableDeviceId('fixture-account'));
expect(id).not.toBe(I.stableDeviceId('another-account'));
expect(id).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-5[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
});
test('only output tokens above the model catalog limit are capped',()=>{
expect(I.capOutputTokens('{"model":"k3d1-agent","max_tokens":79472}',{'k3d1-agent':32768}))
.toBe('{"model":"k3d1-agent","max_tokens":32768}');
expect(I.capOutputTokens('{"model":"k3d1-agent","max_completion_tokens":40000}',{'k3d1-agent':32768}))
.toBe('{"model":"k3d1-agent","max_completion_tokens":32768}');
const small='{"model":"k3d1-agent","max_tokens":32000}';
expect(I.capOutputTokens(small,{'k3d1-agent':32768})).toBe(small);
expect(I.capOutputTokens('not json',{})).toBe('not json');
});
test('a catalog read supplies the output cap used by later chat requests',async()=>{
const bodies=[];
const menu={models:[{requestParams:{modelId:'k3d1-agent',maxTokens:32768},display:{displayName:'K3.1 Pro'},daimonModelConfig:{maxContextSize:1000000}}]};
const p=I.createPlugin({readSession:()=>session,fetchImpl:async(url,init)=>{
if(url===I.CONFIG)return Response.json(menu);
if(url===I.BASE+'/models')return Response.json({data:[]});
bodies.push(await new Response(init.body).text());return new Response('data: [DONE]\n\n');
}});
await p.provider.models({models:{}},{auth:account});
const loader=await p.auth.loader(async()=>account);
await loader.fetch(I.BASE+'/chat/completions',{method:'POST',body:'{"model":"k3d1-agent","max_tokens":79472}'});
expect(JSON.parse(bodies[0]).max_tokens).toBe(32768);
});