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.1 KB
Raw Blame
import type { HookResult, NormalizedHookInput, PlatformAdapter } from '../types.js';
import { claudeCodeAdapter } from './claude-code.js';
import { AdapterRejectedInput, isValidCwd } from './errors.js';

function stringOrUndefined(value: unknown): string | undefined {
  return typeof value === 'string' && value.length > 0 ? value : undefined;
}

// AtomCode sends Claude Code field names (session_id, hook_event_name, cwd,
// tool_name, tool_input, tool_response, transcript_path) but snake_case tool
// names of its own. Capture keys on the Claude Code names, so only tools with a
// direct counterpart are mapped; the rest (`task`, `skill`, `ls`, `apply_patch`,
// `mcp__<server>__<tool>`) pass through unchanged.
//
// Two payload differences (atomcode crates/atomcode-capabilities/src/cc_hooks.rs):
// - The per-call id is `call_id`, not `tool_use_id`. It is passed through as
//   the tool use id so completed-observation dedup has a key.
// - PostToolUse / PostToolUseFailure carry no `tool_input`, only the result.
//   Observations from AtomCode therefore lack the tool's arguments. Pairing the
//   PreToolUse input by call_id would need a new buffer, which AGENTS.md rules out.
//   AtomCode's own file tools already name their argument `file_path`, so the
//   PreToolUse input the file-context hook reads needs no rename.
const ATOMCODE_TOOL_NAMES: Record<string, string> = {
  bash: 'Bash',
  read_file: 'Read',
  write_file: 'Write',
  edit_file: 'Edit',
  grep: 'Grep',
  glob: 'Glob',
  web_search: 'WebSearch',
  web_fetch: 'WebFetch',
};

// AtomCode's hook config is its own `hooks.json` shape, but the payload it
// sends and the `hookSpecificOutput` envelope it accepts are Claude Code's
// (docs/hooks.md: "the CC shape" is accepted on stdout), so the Claude Code
// adapter does the rest of the mapping, PostToolUseFailure included.
export const atomcodeAdapter: PlatformAdapter = {
  normalizeInput(raw): NormalizedHookInput {
    const r = (raw ?? {}) as Record<string, unknown>;
    // A CLAUDE_PROJECT_DIR inherited from the shell that started AtomCode names
    // another project, so it must not replace this cwd.
    const cwd = stringOrUndefined(r.cwd) ?? process.cwd();
    if (!isValidCwd(cwd)) {
      throw new AdapterRejectedInput('invalid_cwd');
    }
    const rawToolName = stringOrUndefined(r.tool_name);
    const mapped = rawToolName && Object.hasOwn(ATOMCODE_TOOL_NAMES, rawToolName)
      ? ATOMCODE_TOOL_NAMES[rawToolName]
      : undefined;
    const toolName = mapped ?? rawToolName;
    const input = claudeCodeAdapter.normalizeInput({
      ...r,
      cwd,
      tool_name: toolName,
      tool_use_id: stringOrUndefined(r.tool_use_id) ?? stringOrUndefined(r.call_id),
    });
    if (!stringOrUndefined(input.sessionId)) {
      throw new AdapterRejectedInput('missing_session_id');
    }
    return {
      ...input,
      cwd,
      nativeToolName: mapped ? rawToolName : undefined,
    };
  },

  // AtomCode reads `hookSpecificOutput.additionalContext`, the Claude Code envelope.
  formatOutput(result: HookResult): unknown {
    return claudeCodeAdapter.formatOutput(result);
  },
};