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
TypeScript aiai-agentsai-memoryanthropicartificial-intelligencechromadbclaudeclaude-agent-sdkclaude-agentsclaude-codeclaude-code-pluginclaude-skillsembeddingslong-term-memorymem0memory-engineopenmemoryragsqlitesupermemory
import { existsSync } from 'fs';
import { homedir } from 'os';
import { join } from 'path';
import type { PlatformAdapter, NormalizedHookInput, HookResult } from '../types.js';
import { AdapterRejectedInput, isValidCwd } from './errors.js';
/**
* Derive the on-disk path to a Cursor agent transcript JSONL given the
* workspace cwd and the conversation id. Cursor stores transcripts at:
*
* ~/.cursor/projects/<workspace-slug>/agent-transcripts/<UUID>/<UUID>.jsonl
*
* where <workspace-slug> is Cursor's slug of the workspace path: every
* character other than an ASCII letter or digit becomes '-', runs of '-'
* collapse to one, and leading and trailing '-' are dropped (e.g.
* /Users/foo.bar/workspaces -> Users-foo-bar-workspaces,
* c:\Users\me\my_app -> c-Users-me-my-app). Returns undefined if the file
* does not exist.
*/
// Cursor session ids are UUID-style identifiers. Restrict to a safe character
// set so a malicious sessionId from stdin cannot escape ~/.cursor/projects via
// path separators, '..' segments, or null bytes (security review on PR #2282).
const SAFE_SESSION_ID_RE = /^[A-Za-z0-9_-]+$/;
export function deriveCursorTranscriptPath(cwd: string | undefined, sessionId: string | undefined): string | undefined {
if (!cwd || !sessionId) return undefined;
if (!SAFE_SESSION_ID_RE.test(sessionId)) return undefined;
const slug = cwd.replace(/[^a-zA-Z0-9]/g, '-').replace(/-+/g, '-').replace(/^-+|-+$/g, '');
const candidate = join(homedir(), '.cursor', 'projects', slug, 'agent-transcripts', sessionId, `${sessionId}.jsonl`);
return existsSync(candidate) ? candidate : undefined;
}
// afterMCPExecution serializes its arguments and result as JSON strings.
// Decode the host envelope once; worker ingestion owns the storage encoding.
function decodeMcpJson(value: unknown): unknown {
if (typeof value !== 'string') return value;
try { return JSON.parse(value); }
catch { return value; }
}
export const cursorAdapter: PlatformAdapter = {
normalizeInput(raw) {
const r = (raw ?? {}) as any;
const isShellCommand = !!r.command && !r.tool_name;
const isMcpEvent = r.result_json !== undefined || r.hook_event_name === 'afterMCPExecution'
|| typeof r.mcp_server_name === 'string';
const cwd = r.workspace_roots?.[0] || r.cwd || process.cwd();
if (!isValidCwd(cwd)) {
throw new AdapterRejectedInput('invalid_cwd');
}
const sessionId = r.conversation_id || r.generation_id || r.id;
// postToolUse sends tool_output where the per-tool hooks send result_json.
// postToolUseFailure sends error_message instead of a result: keep it as an
// is_error response so the attempted paths are not read as completed work.
const failureMessage = typeof r.error_message === 'string' ? r.error_message : undefined;
const toolResponse = isShellCommand
? { output: r.output }
: r.result_json !== undefined ? decodeMcpJson(r.result_json)
: r.tool_output ?? (failureMessage === undefined ? undefined : { is_error: true, error: failureMessage });
return {
sessionId,
cwd,
prompt: r.prompt ?? r.query ?? r.input ?? r.message,
toolName: isShellCommand ? 'Bash' : r.tool_name,
toolInput: isShellCommand ? { command: r.command } : isMcpEvent ? decodeMcpJson(r.tool_input) : r.tool_input,
toolResponse,
toolUseId: typeof r.tool_use_id === 'string' ? r.tool_use_id : (typeof r.tool_call_id === 'string' ? r.tool_call_id : undefined),
// Cursor's stop hook does not pass a transcript path on stdin, but it
// does write a JSONL transcript to disk under ~/.cursor/projects/...,
// so we derive the path from cwd + conversation id.
transcriptPath: deriveCursorTranscriptPath(cwd, sessionId),
filePath: r.file_path,
edits: r.edits,
};
},
formatOutput(result) {
return { continue: result.continue ?? true };
}
};