Skip to content

Persistent Context Across Sessions for Every Agent – Captures everything your agent does during sessions, compresses it with AI, and injects relevant context back into future sessions. Works with Claude Code, OpenClaw, Codex, Gemini, Hermes, Copilot, OpenCode + More

8.9 KB
Raw Blame
/**
 * KimiHooksInstaller.ts — first-class Kimi Code CLI harness integration.
 *
 * Kimi hooks live in `[[hooks]]` tables inside $KIMI_CODE_HOME/config.toml
 * (TOML). Following the CodexCliInstaller precedent we merge text-level,
 * inside a marker-delimited managed block — no TOML library. The MCP server
 * registration goes into $KIMI_CODE_HOME/mcp.json (plain JSON merge).
 * Absolute paths are baked per Rule B (install-paths.ts): Kimi performs no
 * variable substitution on hook commands.
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { kimiCodeHome, kimiConfigPath, kimiMcpJsonPath } from '../../shared/kimi-paths.js';
import {
  getBunAbsolutePath,
  getMcpServerAbsolutePath,
  getNodeAbsolutePath,
  getWorkerServiceAbsolutePath,
} from './install-paths.js';
import { parseJsonWithBom, writeJsonFileAtomic, writeTextFileAtomic } from '../../shared/atomic-json.js';

export const KIMI_MARKER_BEGIN = '# >>> claude-mem kimi hooks (managed by claude-mem; do not edit) >>>';
export const KIMI_MARKER_END = '# <<< claude-mem kimi hooks <<<';

/**
 * Render a value as a TOML string.
 *
 * A literal string (`'...'`) is preferred because it performs no escape
 * processing at all, which is what Windows paths with backslashes want. It
 * cannot contain a single quote, though, and a home directory like
 * `/home/o'brien` would terminate the string early and leave the whole
 * config.toml unparseable — taking the user's own hooks down with it. Fall back
 * to a basic string (`"..."`) with escaped backslashes and quotes in that case.
 */
function tomlString(value: string): string {
  if (!value.includes("'")) return `'${value}'`;
  return `"${value.replace(/\\/g, '\\\\').replace(/"/g, '\\"')}"`;
}

/** Seven rules: the Claude Code hook surface, plus PostToolUseFailure (a failed tool call is still an observation). */
export function buildKimiHooksBlock(bunPath: string, workerPath: string): string {
  const run = (suffix: string) => tomlString(`"${bunPath}" "${workerPath}" ${suffix}`);
  const rules = [
    { event: 'SessionStart', matcher: 'startup|resume', command: run('start'), timeout: 120 },
    { event: 'UserPromptSubmit', matcher: undefined, command: run('hook kimi session-init-context'), timeout: 120 },
    { event: 'PostToolUse', matcher: undefined, command: run('hook kimi observation'), timeout: 120 },
    { event: 'PostToolUseFailure', matcher: undefined, command: run('hook kimi observation'), timeout: 120 },
    { event: 'PreToolUse', matcher: 'Read', command: run('hook kimi file-context'), timeout: 60 },
    { event: 'Stop', matcher: undefined, command: run('hook kimi summarize'), timeout: 120 },
    { event: 'PreCompact', matcher: 'manual|auto', command: run('hook kimi summarize'), timeout: 120 },
  ];
  const body = rules
    .map((rule) => {
      const lines = ['[[hooks]]', `event = "${rule.event}"`];
      if (rule.matcher) lines.push(`matcher = "${rule.matcher}"`);
      lines.push(`command = ${rule.command}`, `timeout = ${rule.timeout}`);
      return lines.join('\n');
    })
    .join('\n\n');
  return `${KIMI_MARKER_BEGIN}\n${body}\n${KIMI_MARKER_END}`;
}

export function upsertManagedBlock(content: string, block: string): string {
  const begin = content.indexOf(KIMI_MARKER_BEGIN);
  const end = content.indexOf(KIMI_MARKER_END);
  if (begin !== -1 && end !== -1 && end > begin) {
    return content.slice(0, begin) + block + content.slice(end + KIMI_MARKER_END.length);
  }
  const separator = content.length === 0 || content.endsWith('\n\n') ? '' : content.endsWith('\n') ? '\n' : '\n\n';
  return content + separator + block + '\n';
}

export function removeManagedBlock(content: string): string {
  const begin = content.indexOf(KIMI_MARKER_BEGIN);
  const end = content.indexOf(KIMI_MARKER_END);
  if (begin === -1 || end === -1 || end <= begin) return content;
  const prefix = content.slice(0, begin).replace(/\n+$/, '');
  const suffix = content.slice(end + KIMI_MARKER_END.length).replace(/^\n+/, '');
  if (prefix.length === 0) return suffix;
  if (suffix.length === 0) return `${prefix}\n`;
  return `${prefix}\n\n${suffix}`;
}

function backupOnce(configPath: string): void {
  const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  const backupPath = `${configPath}.bak-${stamp}`;
  if (existsSync(configPath) && !existsSync(backupPath)) {
    // Owner-only: the backup holds every Kimi setting, and the default umask
    // would make it world-readable even when config.toml is not.
    writeFileSync(backupPath, readFileSync(configPath, 'utf-8'), { mode: 0o600 });
  }
}

export function installKimiHooks(): number {
  const workerPath = getWorkerServiceAbsolutePath();
  if (!workerPath) {
    console.error('Could not find worker-service.cjs (expected under the installed plugin scripts/ directory)');
    return 1;
  }
  const bunPath = getBunAbsolutePath();
  const configPath = kimiConfigPath();
  try {
    mkdirSync(kimiCodeHome(), { recursive: true });
    backupOnce(configPath);
    const current = existsSync(configPath) ? readFileSync(configPath, 'utf-8') : '';
    writeTextFileAtomic(configPath, upsertManagedBlock(current, buildKimiHooksBlock(bunPath, workerPath)));
    console.log(`  Installed Kimi hooks into ${configPath}`);
    console.log(`    Bun runtime: ${bunPath}`);
    console.log(`    Worker: ${workerPath}`);
    return 0;
  } catch (error) {
    console.error(`Failed to install Kimi hooks: ${(error as Error).message}`);
    return 1;
  }
}

export function configureKimiMcp(): number {
  const serverPath = getMcpServerAbsolutePath();
  if (!serverPath) {
    console.error('Could not find mcp-server.cjs (expected under the installed plugin scripts/ directory)');
    return 1;
  }
  const mcpPath = kimiMcpJsonPath();
  try {
    mkdirSync(kimiCodeHome(), { recursive: true });
    const config: { mcpServers?: Record<string, unknown> } = existsSync(mcpPath)
      ? parseJsonWithBom(readFileSync(mcpPath, 'utf-8'))
      : {};
    config.mcpServers ??= {};
    if (config.mcpServers['mcp-search']) {
      console.log('  MCP server mcp-search already configured in mcp.json');
      return 0;
    }
    config.mcpServers['mcp-search'] = {
      type: 'stdio',
      command: getNodeAbsolutePath(),
      args: [serverPath],
    };
    writeJsonFileAtomic(mcpPath, config);
    console.log(`  Configured MCP server in ${mcpPath}`);
    return 0;
  } catch (error) {
    console.error(`Failed to configure Kimi MCP: ${(error as Error).message}`);
    return 1;
  }
}

/**
 * Remove the `mcp-search` server from mcp.json, but only the entry claude-mem
 * wrote (its args point at our mcp-server.cjs): the name is generic, and
 * configureKimiMcp leaves a pre-existing entry of that name alone.
 */
function removeKimiMcpServer(): void {
  const mcpPath = kimiMcpJsonPath();
  if (!existsSync(mcpPath)) return;
  const config: { mcpServers?: Record<string, { args?: unknown }> } = parseJsonWithBom(readFileSync(mcpPath, 'utf-8'));
  const args = config.mcpServers?.['mcp-search']?.args;
  const isOurs = Array.isArray(args) && args.some((arg) => typeof arg === 'string' && arg.endsWith('mcp-server.cjs'));
  if (!isOurs) return;
  delete config.mcpServers!['mcp-search'];
  writeJsonFileAtomic(mcpPath, config);
  console.log(`  Removed MCP server mcp-search from ${mcpPath}`);
}

export function uninstallKimiHooks(): number {
  try {
    removeKimiMcpServer();
  } catch (error) {
    console.error(`Failed to remove the Kimi MCP server: ${(error as Error).message}`);
    return 1;
  }
  const configPath = kimiConfigPath();
  if (!existsSync(configPath)) {
    console.log('  No Kimi config.toml found; nothing to uninstall');
    return 0;
  }
  try {
    const current = readFileSync(configPath, 'utf-8');
    const next = removeManagedBlock(current);
    if (next !== current) {
      writeTextFileAtomic(configPath, next);
      console.log('  Removed claude-mem hooks from config.toml');
    } else {
      console.log('  No claude-mem hook block found in config.toml');
    }
    return 0;
  } catch (error) {
    console.error(`Failed to uninstall Kimi hooks: ${(error as Error).message}`);
    return 1;
  }
}

export function checkKimiHooksStatus(): number {
  const configPath = kimiConfigPath();
  const installed = existsSync(configPath) && readFileSync(configPath, 'utf-8').includes(KIMI_MARKER_BEGIN);
  if (installed) {
    console.log(`  Kimi hooks: installed (${configPath})`);
    return 0;
  }
  console.log('  Kimi hooks: not installed');
  return 1;
}

export async function handleKimiCommand(subcommand: string | undefined, _args: string[]): Promise<number> {
  switch (subcommand) {
    case 'install': {
      const hooks = installKimiHooks();
      const mcp = configureKimiMcp();
      return hooks === 0 && mcp === 0 ? 0 : 1;
    }
    case 'uninstall':
      return uninstallKimiHooks();
    case 'status':
      return checkKimiHooksStatus();
    default:
      console.log('Usage: claude-mem kimi <install|uninstall|status>');
      return subcommand === undefined || subcommand === 'help' ? 0 : 1;
  }
}