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

6.4 KB
Raw Blame
import type { PlatformAdapter, NormalizedHookInput, HookResult } from '../types.js';
import { AdapterRejectedInput, isValidCwd } from './errors.js';
import { resolveHookProjectPath } from '../../utils/project-name.js';
import { withPersistedOutputTail } from './claude-code-persisted-output.js';
import { SESSION_START_CARD_FILE_KEY } from '../../shared/session-start-card-slot.js';

const MAX_AGENT_FIELD_LEN = 128;
const pickAgentField = (v: unknown): string | undefined =>
  typeof v === 'string' && v.length > 0 && v.length <= MAX_AGENT_FIELD_LEN ? v : undefined;
const pickStringField = (v: unknown): string | undefined =>
  typeof v === 'string' ? v : undefined;
// Snake-case wins when it is a non-empty string. Grok Build sends camelCase
// (sessionId, toolName, toolResult, lastAssistantMessage) on the same payload.
const firstFilledString = (...values: unknown[]): string | undefined => {
  for (const value of values) {
    if (typeof value === 'string' && value !== '') return value;
  }
  return undefined;
};
const firstDefined = (...values: unknown[]): unknown => {
  for (const value of values) {
    if (value != null) return value;
  }
  return undefined;
};
/** Grok Build's read tool. Only its input is given a `file_path`; Claude Code's Grep, Glob and LS carry a
 * directory in `path`, and copying that would make every search look like a file read. */
const GROK_BUILD_READ_TOOL = 'read_file';
const withGrokReadPath = (toolName: string | undefined, toolInput: unknown): unknown => {
  if (toolName !== GROK_BUILD_READ_TOOL) return toolInput;
  if (!toolInput || typeof toolInput !== 'object' || Array.isArray(toolInput)) return toolInput;
  const record = toolInput as Record<string, unknown>;
  if (typeof record.file_path === 'string' && record.file_path !== '') return toolInput;
  const target = record.target_file ?? record.filePath ?? record.path;
  if (typeof target !== 'string' || target === '') return toolInput;
  return { ...record, file_path: target };
};

/**
 * A failed tool call arrives on PostToolUseFailure, which carries `error` and
 * `is_interrupt` where PostToolUse carries `tool_response`. Recording them as
 * the response puts the failure, and whether the user stopped the call, in both
 * the stored row and the observer's <outcome>. The error text alone does not say
 * the call was interrupted.
 */
const failedToolResponse = (r: Record<string, unknown>): { is_error: true; error: unknown; is_interrupt: boolean } | undefined =>
  r.hook_event_name === 'PostToolUseFailure' ? { is_error: true, error: r.error, is_interrupt: r.is_interrupt === true } : undefined;

/**
 * Read Qwen Code's `submitted_prompt` into the three states the handler needs.
 *
 * The distinction that matters is presence, not truthiness: an absent field
 * means the host cannot tell a continuation send from a user turn, and an empty
 * one means the host can and is saying this was not a user turn. Collapsing
 * those two is what wrote a fake `[media prompt]` row for every tool round
 * (#4215).
 */
export const normalizeSubmittedPrompt = (raw: unknown): string | null | undefined => {
  if (typeof raw !== 'object' || raw === null) return undefined;
  const record = raw as Record<string, unknown>;
  if (!Object.prototype.hasOwnProperty.call(record, 'submitted_prompt')) return undefined;
  const value = record.submitted_prompt;
  if (typeof value !== 'string') return null;
  return value.trim() ? value : null;
};

export const claudeCodeAdapter: PlatformAdapter = {
  normalizeInput(raw) {
    const r = (raw ?? {}) as any;
    const inputCwd = r.cwd ?? process.cwd();
    if (!isValidCwd(inputCwd)) {
      throw new AdapterRejectedInput('invalid_cwd');
    }
    const cwd = resolveHookProjectPath(inputCwd);
    if (!isValidCwd(cwd)) {
      throw new AdapterRejectedInput('invalid_cwd');
    }
    const source = r.source;
    const toolName = firstFilledString(r.tool_name, r.toolName);
    return {
      sessionId: r.session_id ?? r.id ?? r.sessionId,
      cwd,
      sessionSource: source === 'startup' || source === 'resume' || source === 'clear' || source === 'compact'
        ? source
        : undefined,
      sessionStartCardFile: firstFilledString(r[SESSION_START_CARD_FILE_KEY]),
      prompt: firstFilledString(r.prompt, r.userPrompt),
      submittedPrompt: normalizeSubmittedPrompt(r),
      toolName,
      toolInput: withGrokReadPath(toolName, firstDefined(r.tool_input, r.toolInput)),
      toolResponse: withPersistedOutputTail(firstDefined(r.tool_response, r.toolResponse, r.toolResult)) ?? failedToolResponse(r),
      toolUseId: firstFilledString(r.tool_use_id, r.toolUseId),
      transcriptPath: firstFilledString(r.transcript_path, r.transcriptPath),
      agentTranscriptPath: pickStringField(r.agent_transcript_path),
      // Claude Code's Stop input carries the last assistant text so the
      // summarize handler can skip reading the transcript (#5445). An empty
      // string (a session ending mid-tool-call) is no message: leave it
      // undefined so the handler falls back to the transcript, same as codex.
      // Grok Build sends lastAssistantMessage and writes no Claude transcript.
      lastAssistantMessage: firstFilledString(r.last_assistant_message, r.lastAssistantMessage),
      // stop_hook_active is deliberately not mapped. Claude Code sets it once a
      // Stop hook has blocked the stop and Claude kept working. claude-mem's
      // Stop hook never blocks (it always exits 0 with continue: true), so the
      // flag can only come from another plugin and never marks a loop
      // claude-mem must break. Honoring it (#3168) dropped the summary and the
      // advisor capture for every turn after such a hook fired. Grok Build's
      // camelCase stopHookActive is ignored for the same reason.
      reason: pickStringField(r.reason),
      agentId: pickAgentField(r.agent_id) ?? pickAgentField(r.agentId),
      agentType: pickAgentField(r.agent_type) ?? pickAgentField(r.agentType) ?? pickAgentField(r.subagentType),
    };
  },
  formatOutput(result) {
    const r = result ?? ({} as HookResult);
    if (r.hookSpecificOutput) {
      const output: Record<string, unknown> = { hookSpecificOutput: result.hookSpecificOutput };
      if (r.systemMessage) {
        output.systemMessage = r.systemMessage;
      }
      return output;
    }
    const output: Record<string, unknown> = {};
    if (r.systemMessage) {
      output.systemMessage = r.systemMessage;
    }
    return output;
  }
};