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

3.0 KB
Raw Blame
import type { EventHandler, NormalizedHookInput, HookResult } from '../types.js';
import {
  executeWithWorkerFallback,
  isWorkerFallback,
  getWorkerPort,
  getViewerBaseUrl,
} from '../../shared/worker-utils.js';
import { HOOK_EXIT_CODES } from '../../shared/hook-constants.js';
import { normalizePlatformSource } from '../../shared/platform-source.js';
import { loadFromFileOnce } from '../../shared/hook-settings.js';
import { proTrialLine } from '../../shared/pro-promo.js';
import { shouldTrackProject } from '../../shared/should-track-project.js';
import { projectsQueryString } from '../../shared/project-list.js';
import { getProjectContext } from '../../utils/project-name.js';

export const userMessageHandler: EventHandler = {
  async execute(input: NormalizedHookInput): Promise<HookResult> {
    const cwd = input.cwd ?? process.cwd();
    // Same exclusion gate as SessionStart / file-context / capture. #3511
    // closed after the SessionStart path honored CLAUDE_MEM_EXCLUDED_PROJECTS,
    // but UserPromptSubmit still fetched and bannered context for excluded dirs.
    if (!shouldTrackProject(cwd)) {
      return { exitCode: HOOK_EXIT_CODES.SUCCESS };
    }

    const port = getWorkerPort();
    // Use the same project-key resolution as SessionStart/capture (#2663, #3194).
    // Raw basename(cwd) fragments non-git subdir launches away from parent memory.
    const context = getProjectContext(cwd);
    const projectsQuery = projectsQueryString(context.allProjects);
    const colorsParam = input.platform === 'claude-code' ? '&colors=true' : '';
    const platformSourceParam = input.platform
      ? `&platformSource=${encodeURIComponent(normalizePlatformSource(input.platform))}`
      : '';

    const result = await executeWithWorkerFallback<string>(
      `/api/context/inject?${projectsQuery}${colorsParam}${platformSourceParam}`,
      'GET',
    );

    if (isWorkerFallback(result)) {
      return { exitCode: HOOK_EXIT_CODES.SUCCESS };
    }

    const output = typeof result === 'string' ? result : '';
    // IO discipline: the banner is a USER_HINT. Return it via systemMessage so
    // the platform adapter routes it (claude-code surfaces it inline, exactly
    // like the old stderr write, but inside the HookResult contract). This
    // handler MUST stay pure — no process.stderr.write / console.* / process.exit.
    const promo = proTrialLine('context-banner', loadFromFileOnce().CLAUDE_MEM_PRO_PLAN);
    const bannerText =
      "\n\n" + String.fromCodePoint(0x1F4DD) + " Claude-Mem Context Loaded\n\n" +
      output +
      "\n\n" + String.fromCodePoint(0x1F4A1) + " Wrap any message with <private> ... </private> to prevent storing sensitive information.\n" +
      "\n" + String.fromCodePoint(0x1F4AC) + " Community https://discord.gg/J4wttp9vDu" +
      `\n` + String.fromCodePoint(0x1F4FA) + ` Watch live in browser ${getViewerBaseUrl(port)}/\n` +
      (promo ? `${promo}\n` : '');

    return { exitCode: HOOK_EXIT_CODES.SUCCESS, systemMessage: bannerText };
  },
};