Files
plugins/packages/kimi-desktop/rpc.mjs
T
kupakupa 646776060f kimi-desktop: Kimi desktop Work as an agent, through its local runtime (#169)
Adds agent-kimi-desktop 0.1.0 (kupakupar): Kimi desktop Work on magpie's
Agents page. It moves the runtime's daimon-kimi-code baseUrl to a
loopback bridge and back, through Kimi's local control API.

Re-reviewed at dd8ed2c against the last review at 6c584bb:
1. menu-hook.cjs and selection.mjs are removed; no injected code ships.
2. No full config backup is written. Only before-menu-models.json (the
   model list and current choice) is kept, and it is removed on a clean
   disconnect.
3. providers.json and settings.json are never opened. The catalog comes
   from the gateway's /v1/models with the agent's key and its registered
   UA "Desktop Kimi Work".
4. No signal handlers and no process.exit. Restore runs on the 'exit'
   event, which fires when magpie closes the host's stdin (host.js
   rl.on("close") -> process.exit(0)). The README gives manual recovery
   after a SIGKILL.
5. macOS record: COMPATIBILITY.md (macOS 26.6.2 arm64, Kimi 3.2.17
   build 3.2.17.4, magpie 0.1.1276, Bun 1.4.3). All four control methods
   succeeded, a streamed request returned 200, and a restore followed.
   The README says what happens when a private method fails.
6. Search and fetch pass Kimi's own request, with its own key, to Kimi's
   own origin, unchanged. This is the user's own account used by that
   user, the same rule as #171, so it is accepted.

Run at dd8ed2c, and merged onto origin/main 2ea25b9 together with #171:
- HOME=$(mktemp -d) bun test: 1660 pass, 0 fail (merged: 1668 pass, 0 fail).
- bun test packages/kimi-desktop: 12 pass.
- bun scripts/check.mjs: ok (it skips agent packages).
- git diff --check, npm pack --dry-run (8 files), node scripts/dist.mjs
  (packs agent-kimi-desktop@0.1.0).
- Broken by hand: re-adding a SIGTERM handler and a full config backup
  turns 3 tests red.
- No CI ran on the PR. No real Kimi or credential was used.
2026-10-17 04:33:01 +08:00

43 lines
2.0 KiB
JavaScript

export function controlEndpoint(state) {
const endpoint = state?.control?.endpoint;
let url;
try { url = new URL(endpoint?.url); } catch { throw Error('kimi_control_unavailable'); }
if (url.protocol !== 'ws:' || !['127.0.0.1', '[::1]'].includes(url.hostname)
|| !url.port || url.pathname !== '/control' || url.username || url.password
|| url.search || url.hash || typeof endpoint?.auth?.token !== 'string'
|| !endpoint.auth.token) throw Error('kimi_control_unavailable');
return endpoint;
}
// Bun supplies the authenticated WebSocket client; no browser or Kimi Code CLI.
export async function rpc(state, method, params = {}, { timeout = 6000, Socket = globalThis.WebSocket } = {}) {
if (!['models.current', 'conversations.setModel', 'conversations.getKimiModelList',
'conversations.updateKimiModelList'].includes(method)) throw Error('unsupported_control_method');
const endpoint = controlEndpoint(state);
return new Promise((resolve, reject) => {
let socket, settled = false;
const finish = (error, value) => {
if (settled) return;
settled = true;
clearTimeout(timer);
try { socket?.close(); } catch { /* Already closed. */ }
error ? reject(Error(error)) : resolve(value);
};
const timer = setTimeout(() => finish('kimi_control_timeout'), timeout);
try {
socket = new Socket(endpoint.url, { headers: { Authorization: 'Bearer ' + endpoint.auth.token } });
socket.addEventListener('error', () => finish('kimi_control_unavailable'));
socket.addEventListener('close', () => finish('kimi_control_closed'));
socket.addEventListener('open', () => {
socket.send(JSON.stringify({ jsonrpc: '2.0', id: 1, method, params }));
});
socket.addEventListener('message', event => {
let message;
try { message = JSON.parse(String(event.data)); } catch { return; }
if (message.id !== 1) return;
finish(message.error ? 'kimi_control_rejected' : null, message.result);
});
} catch { finish('kimi_control_unavailable'); }
});
}