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 path from 'path';
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;
}
// Muse Code names its tools in snake_case (probed on Muse Code 1.4.25,
// tests/fixtures/hosts/muse-code-hooks.json). Capture keys on the Claude Code
// names; only tools with a direct counterpart are mapped, the rest (and
// `mcp__<server>__<tool>`) pass through.
const MUSE_TOOL_NAMES: Record<string, string> = {
bash: 'Bash',
read_file: 'Read',
write_file: 'Write',
edit_file: 'Edit',
search: 'Grep',
web_fetch: 'WebFetch',
};
const FILE_TOOLS = new Set(['Read', 'Write', 'Edit']);
// Muse's goal-reminder runs after every turn; it is Muse's own check, not user work.
const MUSE_INTERNAL_SUBAGENT = 'goal-reminder';
const MUSE_INTERNAL_TOOL = 'submit_reminder_decision';
// Muse file tools send `path` as the model wrote it and resolve it from the
// workspace root; `~` is not expanded by Muse, so it is not expanded here.
function withFilePath(toolName: string | undefined, toolInput: unknown, cwd: string): unknown {
if (!toolName || !FILE_TOOLS.has(toolName)) return toolInput;
if (!toolInput || typeof toolInput !== 'object' || Array.isArray(toolInput)) return toolInput;
const record = toolInput as Record<string, unknown>;
if (record.file_path !== undefined || !stringOrUndefined(record.path)) return toolInput;
return { ...record, file_path: path.resolve(cwd, record.path as string) };
}
// Muse sends Claude Code field names, so the Claude Code adapter does the rest
// of the mapping, PostToolUseFailure included.
export const museCodeAdapter: PlatformAdapter = {
normalizeInput(raw): NormalizedHookInput {
const r = (raw ?? {}) as Record<string, unknown>;
if (r.subagent_id === MUSE_INTERNAL_SUBAGENT || r.tool_name === MUSE_INTERNAL_TOOL) {
throw new AdapterRejectedInput('muse_internal_agent');
}
// A CLAUDE_PROJECT_DIR inherited from the shell that started Muse names
// another project, so it must not replace this cwd.
const cwd = r.cwd ?? process.cwd();
if (!isValidCwd(cwd)) {
throw new AdapterRejectedInput('invalid_cwd');
}
// MUSE_SESSION_ID is always the parent session; inside a subagent the
// payload session_id is the child thread, which would split the session.
const sessionId = stringOrUndefined(process.env.MUSE_SESSION_ID);
const threadId = stringOrUndefined(process.env.MUSE_THREAD_ID);
const rawToolName = stringOrUndefined(r.tool_name);
const mapped = rawToolName && Object.hasOwn(MUSE_TOOL_NAMES, rawToolName) ? MUSE_TOOL_NAMES[rawToolName] : undefined;
const toolName = mapped ?? rawToolName;
const input = claudeCodeAdapter.normalizeInput({
...r,
cwd,
session_id: sessionId ?? r.session_id,
tool_name: toolName,
tool_input: withFilePath(toolName, r.tool_input, cwd),
});
if (!stringOrUndefined(input.sessionId)) {
throw new AdapterRejectedInput('missing_session_id');
}
const inSubagent = Boolean(sessionId && threadId && sessionId !== threadId);
return {
...input,
cwd,
nativeToolName: mapped ? rawToolName : undefined,
agentId: inSubagent ? threadId : undefined,
// Muse tool events in a subagent carry no agent type field.
agentType: inSubagent ? 'subagent' : undefined,
// transcript_path is always null; summarize uses last_assistant_message.
transcriptPath: undefined,
};
},
// Muse reads `hookSpecificOutput.additionalContext`, the Claude Code envelope.
formatOutput(result: HookResult): unknown {
return claudeCodeAdapter.formatOutput(result);
},
};