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

11.1 KB
Raw Blame
/**
 * MCodeHooksInstaller.ts — MiniMax Code (`mcode`) integration (hooks + MCP).
 *
 * MiniMax Code reads user hooks from hooks.json and MCP servers from the
 * `mcpServers` of mcp.json, both in its data directory ($MINIMAX_DATA_DIR,
 * default ~/.minimax). Its hooks send Claude Code field names, so the worker
 * runs the same handlers. Only the hook entries and the `claude-mem` server
 * this installer writes are touched. Absolute paths are baked per Rule B
 * (install-paths.ts).
 *
 * No PreToolUse rule: its output can deny or rewrite tool calls (#5085).
 */
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'fs';
import { homedir } from 'os';
import path from 'path';
import { parseJsonWithBom, writeJsonFileAtomic } from '../../shared/atomic-json.js';
import { readJsonSafe } from '../../utils/json-utils.js';
import { getBunAbsolutePath, getMcpServerAbsolutePath, getWorkerServiceAbsolutePath } from './install-paths.js';
import { writeMcpJsonConfig } from './McpIntegrations.js';

interface MCodeHookCommand {
  type: string;
  command?: string;
  timeout?: number;
  [key: string]: unknown;
}

interface MCodeHookRule {
  matcher?: string;
  hooks: MCodeHookCommand[];
  [key: string]: unknown;
}

type MCodeHooks = Record<string, MCodeHookRule[]>;

interface MCodeHooksFile {
  enabled?: unknown;
  hooks?: MCodeHooks;
  [key: string]: unknown;
}

const MCP_SERVER_NAME = 'claude-mem';
// MiniMax Code drops, without a warning, any hook whose timeout is over 10 seconds.
const MCODE_HOOK_TIMEOUT_SECONDS = 10;

export function mcodeDataDir(env: NodeJS.ProcessEnv = process.env): string {
  return env.MINIMAX_DATA_DIR || path.join(homedir(), '.minimax');
}

/** The MiniMax Code events claude-mem has a handler for. */
export function buildMCodeHooks(bunPath: string, workerPath: string): MCodeHooks {
  const rule = (suffix: string): MCodeHookRule => ({
    hooks: [{ type: 'command', command: `"${bunPath}" "${workerPath}" ${suffix}`, timeout: MCODE_HOOK_TIMEOUT_SECONDS }],
  });
  return {
    SessionStart: [rule('start'), rule('hook mcode context')],
    UserPromptSubmit: [rule('hook mcode session-init')],
    PostToolUse: [rule('hook mcode observation')],
    Stop: [rule('hook mcode summarize')],
  };
}

// Ours only in the exact shape buildMCodeHooks writes: a quoted Bun runtime,
// the quoted plugin worker, then one of our subcommands.
const OWN_COMMAND = /^"(?:[^"]*[\\/])?bun(?:\.exe)?" "[^"]*[\\/]scripts[\\/]worker-service\.cjs" (start|hook mcode (?:context|session-init|observation|summarize))$/;

export function isClaudeMemMCodeHook(hook: MCodeHookCommand): boolean {
  return typeof hook?.command === 'string' && OWN_COMMAND.test(hook.command);
}

/** The hooks without claude-mem's entries; emptied rules and events are dropped. */
export function removeMCodeHooks(hooks: MCodeHooks): MCodeHooks {
  const next: MCodeHooks = {};
  for (const [event, rules] of Object.entries(hooks)) {
    if (!Array.isArray(rules)) {
      next[event] = rules;
      continue;
    }
    const kept = rules
      .map((rule) => Array.isArray(rule?.hooks)
        ? { ...rule, hooks: rule.hooks.filter((hook) => !isClaudeMemMCodeHook(hook)) }
        : rule)
      .filter((rule) => !Array.isArray(rule?.hooks) || rule.hooks.length > 0);
    if (kept.length > 0) next[event] = kept;
  }
  return next;
}

function readHooksFile(hooksPath: string): MCodeHooksFile {
  if (!existsSync(hooksPath)) return {};
  const parsed: unknown = parseJsonWithBom(readFileSync(hooksPath, 'utf-8'));
  if (parsed === null || typeof parsed !== 'object' || Array.isArray(parsed)) {
    throw new Error(`${hooksPath} is not a JSON object; refusing to overwrite it`);
  }
  const file = parsed as MCodeHooksFile;
  if (file.hooks !== undefined && (file.hooks === null || typeof file.hooks !== 'object' || Array.isArray(file.hooks))) {
    throw new Error(`"hooks" in ${hooksPath} is not an object; refusing to overwrite it`);
  }
  return file;
}

function backupOnce(filePath: string): void {
  const stamp = new Date().toISOString().slice(0, 10).replace(/-/g, '');
  const backupPath = `${filePath}.bak-${stamp}`;
  if (existsSync(filePath) && !existsSync(backupPath)) {
    writeFileSync(backupPath, readFileSync(filePath, 'utf-8'), { mode: 0o600 });
  }
}

export function installMCode(dataDir: string = mcodeDataDir()): number {
  const workerPath = getWorkerServiceAbsolutePath();
  if (!workerPath) {
    console.error('Could not find worker-service.cjs (expected under the installed plugin scripts/ directory)');
    return 1;
  }
  const mcpServerPath = getMcpServerAbsolutePath();
  if (!mcpServerPath) {
    console.error('Could not find mcp-server.cjs (expected under the installed plugin scripts/ directory)');
    return 1;
  }
  const bunPath = getBunAbsolutePath();
  const hooksPath = path.join(dataDir, 'hooks.json');
  const mcpPath = path.join(dataDir, 'mcp.json');
  try {
    const file = readHooksFile(hooksPath);
    const ours = buildMCodeHooks(bunPath, workerPath);
    // An event that is not a list cannot be merged: `...(hooks[event] ?? [])` would
    // spread a string into its characters and rewrite the user's file as garbage.
    // The sibling installers (ZCode, Muse Code, Gemini) refuse the same file.
    for (const event of Object.keys(ours)) {
      const value = file.hooks?.[event];
      if (value !== undefined && !Array.isArray(value)) {
        throw new Error(`"hooks.${event}" in ${hooksPath} is not a JSON array; refusing to overwrite it`);
      }
    }
    mkdirSync(dataDir, { recursive: true });
    backupOnce(hooksPath);
    // One switch for every hook in this file, and a missing key means off.
    if (file.enabled === undefined) file.enabled = true;
    const hooks = removeMCodeHooks(file.hooks ?? {});
    for (const [event, rules] of Object.entries(ours)) {
      hooks[event] = [...(hooks[event] ?? []), ...rules];
    }
    file.hooks = hooks;
    writeJsonFileAtomic(hooksPath, file);
    writeMcpJsonConfig(mcpPath, mcpServerPath);
    console.log(`  Installed MiniMax Code hooks into ${hooksPath}`);
    console.log(`  Configured MCP server in ${mcpPath}`);
    if (file.enabled !== true) {
      console.warn(`  "enabled" is not true in ${hooksPath}; MiniMax Code will not run the hooks until you set it to true.`);
    }
    console.log('  Start a new MiniMax Code session to load them.');
    return 0;
  } catch (error) {
    console.error(`Failed to install MiniMax Code hooks + MCP: ${(error as Error).message}`);
    return 1;
  }
}

export function uninstallMCode(dataDir: string = mcodeDataDir()): number {
  const hooksPath = path.join(dataDir, 'hooks.json');
  const mcpPath = path.join(dataDir, 'mcp.json');
  try {
    const mcp = readJsonSafe<Record<string, any>>(mcpPath, {});
    if (mcp.mcpServers?.[MCP_SERVER_NAME]) {
      delete mcp.mcpServers[MCP_SERVER_NAME];
      writeJsonFileAtomic(mcpPath, mcp);
      console.log(`  Removed claude-mem from ${mcpPath}`);
    }
    const file = readHooksFile(hooksPath);
    if (!file.hooks) return 0;
    const next = removeMCodeHooks(file.hooks);
    if (JSON.stringify(next) === JSON.stringify(file.hooks)) return 0;
    backupOnce(hooksPath);
    file.hooks = next;
    writeJsonFileAtomic(hooksPath, file);
    console.log(`  Removed claude-mem hooks from ${hooksPath}`);
    return 0;
  } catch (error) {
    console.error(`Failed to uninstall MiniMax Code hooks + MCP: ${(error as Error).message}`);
    return 1;
  }
}

/**
 * What `claude-mem mcode status` reports; readMCodeStatus is its one writer.
 * Only `installed` means capture and recall can work.
 */
export type MCodeStatus = 'installed' | 'incomplete' | 'disabled' | 'not-installed' | 'unreadable';

export const MCODE_STATUS_LABELS: Record<MCodeStatus, string> = {
  installed: 'installed',
  incomplete: 'incomplete, run npx claude-mem mcode install',
  disabled: 'disabled, "enabled" is not true so MiniMax Code does not run them',
  'not-installed': 'not installed',
  unreadable: 'unreadable config',
};

// The subcommand after the worker path, e.g. "hook mcode context".
const ownHooks = (rules: unknown): MCodeHookCommand[] => Array.isArray(rules)
  ? rules.flatMap((rule) => Array.isArray(rule?.hooks) ? rule.hooks.filter(isClaudeMemMCodeHook) : [])
  : [];
const hookTail = (hook: MCodeHookCommand) => OWN_COMMAND.exec(hook.command ?? '')?.[1];

/**
 * The `claude-mem` MCP server 0.7.10 reads from mcp.json, or null when the file does not hold it.
 * A file that is not JSON throws; a root or `mcpServers` that is not an object holds no server.
 */
function readMCodeServer(mcpPath: string): string | null {
  const mcp: unknown = readJsonSafe<unknown>(mcpPath, {});
  const servers = mcp && typeof mcp === 'object' ? (mcp as { mcpServers?: unknown }).mcpServers : undefined;
  return servers && typeof servers === 'object' && !Array.isArray(servers) && Object.hasOwn(servers, MCP_SERVER_NAME)
    ? mcpPath
    : null;
}

export function readMCodeStatus(dataDir: string = mcodeDataDir()): { status: MCodeStatus; detail?: string } {
  const hooksPath = path.join(dataDir, 'hooks.json');
  const mcpPath = path.join(dataDir, 'mcp.json');
  let file: MCodeHooksFile;
  let server: string | null;
  try {
    file = readHooksFile(hooksPath);
    // A corrupt mcp.json is what a user runs `status` to diagnose, so it reports, never throws.
    server = readMCodeServer(mcpPath);
  } catch (error) {
    return { status: 'unreadable', detail: (error as Error).message };
  }
  const hooks = file.hooks ?? {};
  // Each event install writes, checked by its subcommand: a rule the user deleted (or a
  // hooks.json an older install wrote) leaves capture half-wired while `enabled` is true.
  // ZCode and Muse Code report the same gap as `incomplete`.
  const missing = Object.entries(buildMCodeHooks('bun', '/scripts/worker-service.cjs')).flatMap(([event, rules]) => {
    const present = new Set(ownHooks(Object.hasOwn(hooks, event) ? hooks[event] : undefined).map(hookTail));
    return rules.every((rule) => present.has(hookTail(rule.hooks[0]))) ? [] : [event];
  });
  if (!server) missing.push(`the ${MCP_SERVER_NAME} MCP server`);
  if (!server && !Object.values(hooks).some((rules) => ownHooks(rules).length > 0)) return { status: 'not-installed' };
  if (missing.length > 0) return { status: 'incomplete', detail: `missing ${missing.join(', ')}` };
  // MiniMax Code runs no hooks unless this is true; a missing key means off.
  if (file.enabled !== true) return { status: 'disabled', detail: hooksPath };
  return { status: 'installed', detail: hooksPath };
}

export function checkMCodeStatus(dataDir: string = mcodeDataDir()): number {
  const { status, detail } = readMCodeStatus(dataDir);
  const line = `  MiniMax Code hooks: ${MCODE_STATUS_LABELS[status]}${detail ? ` (${detail})` : ''}`;
  if (status === 'unreadable') console.error(line);
  else console.log(line);
  return status === 'installed' ? 0 : 1;
}

export async function handleMCodeCommand(subcommand: string | undefined, _args: string[]): Promise<number> {
  switch (subcommand) {
    case 'install':
      return installMCode();
    case 'uninstall':
      return uninstallMCode();
    case 'status':
      return checkMCodeStatus();
    default:
      console.log('Usage: claude-mem mcode <install|uninstall|status>');
      return subcommand === undefined || subcommand === 'help' ? 0 : 1;
  }
}