Files
plugins/packages/kimi-desktop/test-fixtures/control-peer.mjs
T
Changtian Li bc5b8332ec kimi-desktop 0.1.6: per-role reasoning effort and context input budget (#813)
kimi-desktop 0.1.6: separate reasoning effort and an optional context input budget for the primary and secondary roles.

The change (f07e7f4a..1d14a185, since the round-6 approval):
- The settings page and API save `<role>ReasoningEffort` and `<role>ContextTokens` for each role. Only efforts and context limits the catalog publishes are offered; an unknown limit allows no custom budget.
- Inference drops Kimi's top-level `thinking`, `reasoning` and `reasoning_effort`, and sends the chosen effort, or none for the provider default. Kimi's native menu aliases no longer choose the model.
- A custom budget counts a copy of the request through magpie's `/v1/messages/count_tokens`. Over the budget gives 400 `context_length_exceeded`; a failed count gives fixed 503 `context_count_unavailable`. No generation is sent in either case. Without a budget, nothing extra is sent.
- Role-scoped worker profiles (`v2:[model, role]`) tell primary from secondary even on the same model. Older roleless profiles keep their binding and use defaults.
- 1d14a185 removes blank lines only.

Run at 1d14a1859f63fd4bb512d492d1de419ad3e6c79a on mote (macOS, Bun 1.4.2, HOME/TMPDIR/XDG in the run's own folder):
- `bun test` in packages/kimi-desktop: 626 pass, 0 fail, 29 files.
- `bun scripts/check.mjs kimi-desktop`: exit 0.
- 14 hand breaks, each red in the package suite:
  - drop `applyReasoningOptions`;
  - strip thinking fields only when an effort is set;
  - drop the budget check, or make it `>=`;
  - `useFresh` false, or without its model check;
  - a legacy roleless profile treated as primary;
  - a profile name without its role;
  - no `registerRoles`;
  - no `optionsMatch` in save;
  - no `refreshLiveModelOptions`;
  - count with `redirect: 'follow'`, or with no reply size limit;
  - a 131072 fallback for an unknown catalog context.
- CI at the head: test and on-npm pass. The PR merges onto main f379bf90+ with no conflicts.

Not blocking, left for a follow-up:
- Each inference now reads connection.json (adapter.mjs:547) with no fallback to the live connection. An unreadable file turns every request into 502, even without a budget.
- The comment above `applyReasoningOptions` ("Preserve the legacy connection untouched…") no longer matches the always-strip policy.
2026-11-09 10:07:07 +08:00

176 lines
9.8 KiB
JavaScript

// Exercise the real control WebSocket transport against a temporary Kimi peer.
// No desktop profile, application or external model is used.
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { KimiAdapter } from '../adapter.mjs';
import { isMenuAlias } from '../catalog.mjs';
const official = 'https://agent-gw.kimi.com/coding/v1';
const read = file => JSON.parse(fs.readFileSync(file, 'utf8'));
const write = (file, value) => {
fs.mkdirSync(path.dirname(file), { recursive: true });
fs.writeFileSync(file, JSON.stringify(value));
};
// Model the public native plugin API: install/update copy the declared source
// into the active V2 home's managed directory; enablement belongs to the host.
export function nativeRolePluginPeer(homeDirectory) {
const enabled = new Map(), records = new Map(), calls = [];
let fault = null;
const methods = new Set(['kimiPlugin.installPlugin', 'kimiPlugin.updatePlugin',
'kimiPlugin.getPluginInfo', 'kimiPlugin.setPluginEnabled', 'kimiPlugin.removePlugin']);
const installed = id => path.join(homeDirectory, 'plugins', 'managed', id);
const registryFile = path.join(homeDirectory, 'plugins', 'installed.json');
const registry = () => fs.existsSync(registryFile) ? read(registryFile) : { version: 1, plugins: [] };
const persist = (id, patch) => {
const document = registry();
const previous = document.plugins.find(row => row.id === id);
document.plugins = document.plugins.filter(row => row.id !== id);
if (patch !== null) document.plugins.push({ ...previous, ...patch, id });
write(registryFile, document);
};
return {
calls, installed,
handles: method => methods.has(method),
setEnabled(id, value) {
enabled.set(id, value);
if (registry().plugins.some(row => row.id === id)) persist(id, { enabled: value });
},
fail(method, { after = false, once = true } = {}) { fault = { method, after, once }; },
clearFault() { fault = null; },
call(method, params) {
calls.push({ method, params });
const activeFault = fault?.method === method ? fault : null;
if (activeFault?.once) fault = null;
if (activeFault && !activeFault.after) throw Error('kimi_control_timeout');
let result;
if (method === 'kimiPlugin.installPlugin' || method === 'kimiPlugin.updatePlugin') {
if (typeof params?.source !== 'string') throw Error('plugin source required');
const manifest = read(path.join(params.source, 'kimi.plugin.json'));
if (!manifest.name || JSON.stringify(manifest.agents) !== JSON.stringify(['./agents'])) throw Error('invalid plugin manifest');
const destination = installed(manifest.name);
if (method === 'kimiPlugin.installPlugin' && enabled.has(manifest.name)) throw Error('plugin already installed');
fs.rmSync(destination, { recursive: true, force: true });
fs.mkdirSync(path.dirname(destination), { recursive: true });
fs.cpSync(params.source, destination, { recursive: true });
if (!enabled.has(manifest.name)) enabled.set(manifest.name, false);
records.set(manifest.name, { root: destination, source: 'local-path', originalSource: params.source });
persist(manifest.name, { ...records.get(manifest.name), enabled: enabled.get(manifest.name) });
result = { id: manifest.name, enabled: enabled.get(manifest.name) };
} else if (method === 'kimiPlugin.getPluginInfo') {
if (!enabled.has(params.id)) throw Error('kimi_plugin_not_found');
result = { ...records.get(params.id), id: params.id, enabled: enabled.get(params.id) === true };
} else if (method === 'kimiPlugin.setPluginEnabled') {
if (!enabled.has(params.id)) throw Error('kimi_plugin_not_found');
enabled.set(params.id, params.enabled);
persist(params.id, { ...records.get(params.id), enabled: params.enabled });
result = { id: params.id, enabled: params.enabled };
} else if (method === 'kimiPlugin.removePlugin') {
if (!enabled.has(params.id)) throw Error('plugin not found');
enabled.delete(params.id);
records.delete(params.id);
persist(params.id, null); // Native removal retains managed/cache files.
result = { ok: true };
} else throw Error('unexpected plugin method');
if (activeFault?.after) throw Error('kimi_control_closed');
return result;
},
};
}
export function recoveryPeer({ secondaryModel } = {}) {
const home = fs.mkdtempSync(path.join(os.tmpdir(), 'kimi-restore-peer-'));
const kimiDir = path.join(home, 'kimi');
const stateDir = path.join(home, 'state');
let menu = [{ model: 'native', maxContextSize: 131072, capabilities: [] }];
let runtimeBase = official;
let fault = null;
let controller;
const plugins = nativeRolePluginPeer(path.join(kimiDir, 'runtime', 'kimi-code', 'home'));
const calls = [], requests = [];
const service = Bun.serve({ hostname: '127.0.0.1', port: 0,
fetch(request, server) {
if (request.headers.get('authorization') !== 'Bearer synthetic-control') return new Response('', { status: 401 });
if (server.upgrade(request)) return;
return new Response('', { status: 400 });
},
websocket: { message(socket, input) {
const { id, method, params } = JSON.parse(String(input));
const doc = read(controller.configFile);
const point = method === 'conversations.getKimiModelList' ? 'list'
: method === 'conversations.updateKimiModelList' ? 'menu'
: method === 'conversations.setModel' ? (doc.model.providers['daimon-kimi-code'].baseUrl === official ? 'reload' : 'select')
: 'probe';
calls.push({ method, point, params });
const fail = fault?.point === point;
const respond = value => socket.send(JSON.stringify({ jsonrpc: '2.0', id,
...(value ? { error: { code: -32000, message: 'synthetic unavailable' } } : { result }) }));
let result;
if (fail && !fault.after) { respond(true); return; }
if (plugins.handles(method)) result = plugins.call(method, params);
else if (method === 'kimiPlugin.reloadActiveSessions') result = { sessions: [] };
else if (method === 'kimiPlugin.ReloadSession') result = { status: 'reloaded' };
else if (method === 'models.current') result = { current: doc.model.current };
else if (method === 'conversations.getKimiModelList') result = { models: menu };
else if (method === 'conversations.updateKimiModelList') {
menu = params.models;
if (!menu.some(row => row.model === doc.model.current)) doc.model.current = menu[0].model;
doc.model.models = Object.fromEntries(menu.map(row => [row.model, { model: row.model, provider: 'daimon-kimi-code' }]));
write(controller.configFile, doc);
runtimeBase = doc.model.providers['daimon-kimi-code'].baseUrl;
result = { ok: true };
} else if (method === 'conversations.setModel') {
doc.model.current = params.modelAlias;
write(controller.configFile, doc);
runtimeBase = doc.model.providers['daimon-kimi-code'].baseUrl;
result = { current: params.modelAlias };
} else throw Error('unexpected control method');
respond(fail);
} },
});
const make = () => new KimiAdapter({ home, stateDir, kimiDir, stabilityMs: 0,
fetchImpl: async (url, options) => {
if (String(url).endsWith('/models')) return new Response(JSON.stringify({ data: [{ id: 'provider/main' }, { id: 'provider/secondary' }] }));
requests.push({ url: String(url), body: JSON.parse(options.body) });
return new Response('data: [DONE]\n\n', { headers: { 'Content-Type': 'text/event-stream' } });
} });
controller = make();
write(controller.configFile, { model: { current: 'native', models: { native: { model: 'native', provider: 'daimon-kimi-code' } },
providers: { 'daimon-kimi-code': { type: 'kimi', baseUrl: official } } },
credentials: { kimiCode: { apiKey: 'synthetic-kimi-key' } }, unrelated: 'keep' });
write(controller.runnerFile, { lifecycleStatus: 'running', daemonGeneration: 'synthetic-one', kernel: { agentKernel: 'kimi-code-v2' },
control: { endpoint: { url: `ws://127.0.0.1:${service.port}/control`, auth: { token: 'synthetic-control' } } } });
controller.bootstrap();
write(controller.connectionFile, { enabled: true, model: 'provider/main',
...(secondaryModel !== undefined ? { secondaryModel } : {}),
gateway: { url: 'http://127.0.0.1:3425/v1', key: 'synthetic-gateway' } });
return {
get controller() { return controller; }, get menu() { return menu; }, get runtimeBase() { return runtimeBase; }, calls, requests, plugins,
selectAlias({ legacy = true } = {}) {
// New connections do not install native aliases. Explicitly reproduce
// the narrow menu/config/backup state left by an earlier plugin version.
const doc = read(controller.configFile);
const native = menu.find(row => !isMenuAlias(row.model));
if (!fs.existsSync(controller.modelBackupFile)) write(controller.modelBackupFile, { current: native.model });
const row = controller.catalog.find(row => row.id === 'provider/main');
const alias = legacy ? row.alias.replace('local-model-', 'magpie-local-') : row.alias;
if (!menu.some(row => row.model === alias)) menu = [...menu, { ...row.runtime, model: alias }];
doc.model.models[alias] = { model: alias, provider: 'daimon-kimi-code' };
doc.model.current = alias;
write(controller.configFile, doc);
return alias;
},
fail(point, after = false) { fault = { point, after }; },
clearFault() { fault = null; },
disable() { write(controller.connectionFile, { ...read(controller.connectionFile), enabled: false }); },
recover() { fault = null; controller = make(); },
async cleanup() {
fault = null;
await controller.stop();
service.stop(true);
fs.rmSync(home, { recursive: true, force: true });
},
};
}