Files
plugins/packages/kimi-desktop/library.mjs
T
kupakupa 5fa537146e kimi-desktop 0.1.1: ordinary Kimi takes the Library's instructions, MCP and skills (#189)
Ordinary Kimi (agent-kimi-desktop) becomes a Library target: agent.library
names ~/.config/magpie-kimi/AGENTS.md, library-native/mcp.json (format kimi)
and library-skills. bootstrap() links library-native to Kimi's
daimon/runtime/kimi-code/home and library-skills to daimon/skills (junction on
Windows, dir symlink elsewhere); a different object at either path stops with
library_path_conflict. The adapter reads AGENTS.md on every inference request
and inserts one separate system message after the leading system/developer
ones; an unreadable or non-UTF-8 file answers 503 instructions_unavailable.

Run at the reviewed head 5b9f445 (by the reviewer, sandbox HOME, no real
credentials):
- bun test packages/kimi-desktop: 14 pass, 0 fail (95 expects), bun 1.4.2.
- bun scripts/check.mjs: every package ok (agent packages are skipped there).
- Against magpie's own Library code (origin/main e085d6e92): a throwaway Go
  test in internal/library loaded this kimi-desktop.agent.js through
  agentplug, ran prepareLibrary on a fake Kimi tree, then SaveInstructions,
  SaveServer and InstallSkills to the kimi-desktop target. The MCP server
  landed in the native runtime mcp.json beside the existing entry, the skill
  in daimon/skills, library-native stayed a symlink; applyInstructions put
  the written block in as a second system message. RemoveServer,
  RemoveSkill and clearing the instructions left the existing MCP entry and
  skill, and applyInstructions then added nothing.
- CI on the PR: test and on-npm green.

Not tried: a real Kimi desktop (the author's COMPATIBILITY.md records it on
Windows 11 and macOS). agent-kimi-desktop is not on npm yet; it is served
from the dist branch until the owner's first publish.
2026-10-17 13:19:09 +08:00

53 lines
2.1 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
const BEGIN = '<magpie-kimi-shared-instructions>';
const END = '</magpie-kimi-shared-instructions>';
const LIMIT = 1024 * 1024;
function linkDirectory(link, target) {
fs.mkdirSync(target, {recursive: true, mode: 0o700});
let stat;
try {
stat = fs.lstatSync(link);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
// A directory junction needs no Windows developer-mode file-symlink privilege.
fs.symlinkSync(target, link, process.platform === 'win32' ? 'junction' : 'dir');
return;
}
try {
if (!stat.isSymbolicLink() || fs.realpathSync(link) !== fs.realpathSync(target)) throw Error();
} catch {throw Error('library_path_conflict');}
}
export function prepareLibrary(stateDir, kimiDir) {
linkDirectory(path.join(stateDir, 'library-native'), path.join(kimiDir, 'runtime', 'kimi-code', 'home'));
linkDirectory(path.join(stateDir, 'library-skills'), path.join(kimiDir, 'skills'));
}
export function applyInstructions(payload, file) {
if (!Array.isArray(payload.messages)) throw Error('invalid_messages');
let text = '';
try {
const stat = fs.statSync(file);
if (!stat.isFile() || stat.size > LIMIT) throw Error('instructions_unavailable');
const bytes = fs.readFileSync(file);
if (bytes.length > LIMIT) throw Error('instructions_unavailable');
text = new TextDecoder('utf-8', {fatal: true}).decode(bytes).replace(/^\uFEFF/, '').trim();
} catch (error) {
if (error.code !== 'ENOENT') throw Error('instructions_unavailable');
}
// Remove only our separate system message; leave Kimi's and the user's own
// instructions, tool messages, image parts and conversation history intact.
const messages = payload.messages.filter(message => !(message?.role === 'system'
&& typeof message.content === 'string' && message.content.startsWith(BEGIN + '\n')
&& message.content.endsWith('\n' + END)));
if (text) {
let at = 0;
while (['system', 'developer'].includes(messages[at]?.role)) at++;
messages.splice(at, 0, {role: 'system', content: BEGIN + '\n' + text + '\n' + END});
}
return {...payload, messages};
}