Release v3.9.9

Published from npm package build
Source: https://github.com/thedotmack/claude-mem-source
This commit is contained in:
Alex Newman
2025-10-03 18:20:47 -04:00
parent 4d5b307a74
commit 85ed7c3d2f
85 changed files with 11156 additions and 7458 deletions
-89
View File
@@ -1,89 +0,0 @@
#!/usr/bin/env node
/**
* Pre-Compact Hook for Claude Memory System
*
* Updated to use the centralized PromptOrchestrator and HookTemplates system.
* This hook validates the pre-compact request and executes compression using
* standardized response templates for consistent Claude Code integration.
*/
import { loadCliCommand } from './shared/config-loader.js';
import { getLogsDir } from './shared/path-resolver.js';
import {
createHookResponse,
executeCliCommand,
validateHookPayload,
debugLog
} from './shared/hook-helpers.js';
// Set up stdin immediately
process.stdin.setEncoding('utf8');
process.stdin.resume(); // Explicitly enter flowing mode to prevent data loss
// Read input from stdin
let input = '';
process.stdin.on('data', chunk => {
input += chunk;
});
process.stdin.on('end', async () => {
try {
// Load CLI command inside try-catch to handle config errors properly
const cliCommand = loadCliCommand();
const payload = JSON.parse(input);
debugLog('Pre-compact hook started', { payload });
// Validate payload using centralized validation
const validation = validateHookPayload(payload, 'PreCompact');
if (!validation.valid) {
const response = createHookResponse('PreCompact', false, { reason: validation.error });
debugLog('Validation failed', { response });
// Exit silently - validation failure is expected flow control
process.exit(0);
}
// Check for environment-based blocking conditions
if (payload.trigger === 'auto' && process.env.DISABLE_AUTO_COMPRESSION === 'true') {
const response = createHookResponse('PreCompact', false, {
reason: 'Auto-compression disabled by configuration'
});
debugLog('Auto-compression disabled', { response });
// Exit silently - disabled compression is expected flow control
process.exit(0);
}
// Execute compression using standardized CLI execution helper
debugLog('Executing compression command', {
command: cliCommand,
args: ['compress', payload.transcript_path]
});
const result = await executeCliCommand(cliCommand, ['compress', payload.transcript_path]);
if (!result.success) {
const response = createHookResponse('PreCompact', false, {
reason: `Compression failed: ${result.stderr || 'Unknown error'}`
});
debugLog('Compression command failed', { stderr: result.stderr, response });
console.log(`claude-mem error: compression failed, see logs at ${getLogsDir()}`);
process.exit(1); // Exit with error code for actual compression failure
}
// Success - exit silently (suppressOutput is true)
debugLog('Compression completed successfully');
process.exit(0);
} catch (error) {
const response = createHookResponse('PreCompact', false, {
reason: `Hook execution error: ${error.message}`
});
debugLog('Pre-compact hook error', { error: error.message, response });
console.log(`claude-mem error: hook failed, see logs at ${getLogsDir()}`);
process.exit(1);
}
});
-61
View File
@@ -1,61 +0,0 @@
#!/usr/bin/env node
/**
* Session End Hook - Handles session end events including /clear
*/
import { loadCliCommand } from './shared/config-loader.js';
import { getSettingsPath, getArchivesDir } from './shared/path-resolver.js';
import { execSync } from 'child_process';
import { existsSync, readFileSync } from 'fs';
const cliCommand = loadCliCommand();
// Check if save-on-clear is enabled
function isSaveOnClearEnabled() {
const settingsPath = getSettingsPath();
if (existsSync(settingsPath)) {
try {
const settings = JSON.parse(readFileSync(settingsPath, 'utf8'));
return settings.saveMemoriesOnClear === true;
} catch (error) {
return false;
}
}
return false;
}
// Set up stdin immediately before any async operations
process.stdin.setEncoding('utf8');
process.stdin.resume(); // Explicitly enter flowing mode to prevent data loss
// Read input
let input = '';
process.stdin.on('data', chunk => {
input += chunk;
});
process.stdin.on('end', async () => {
const data = JSON.parse(input);
// Check if this is a clear event and save-on-clear is enabled
if (data.reason === 'clear' && isSaveOnClearEnabled()) {
console.error('🧠 Saving memories before clearing context...');
try {
// Use the CLI to compress current transcript
execSync(`${cliCommand} compress --output ${getArchivesDir()}`, {
stdio: 'inherit',
env: { ...process.env, CLAUDE_MEM_SILENT: 'true' }
});
console.error('✅ Memories saved successfully');
} catch (error) {
console.error('[session-end] Failed to save memories:', error.message);
// Don't block the clear operation if memory saving fails
}
}
// Always continue
console.log(JSON.stringify({ continue: true }));
});
-170
View File
@@ -1,170 +0,0 @@
#!/usr/bin/env node
/**
* Session Start Hook - Load context when Claude Code starts
*
* Updated to use the centralized PromptOrchestrator and HookTemplates system.
* This hook loads previous session context using standardized formatting and
* provides rich context messages for Claude Code integration.
*/
import path from 'path';
import { loadCliCommand } from './shared/config-loader.js';
import {
createHookResponse,
formatSessionStartContext,
executeCliCommand,
parseContextData,
validateHookPayload,
debugLog
} from './shared/hook-helpers.js';
const cliCommand = loadCliCommand();
// Set up stdin immediately before any async operations
process.stdin.setEncoding('utf8');
process.stdin.resume(); // Explicitly enter flowing mode to prevent data loss
// Read input from stdin
let input = '';
process.stdin.on('data', chunk => {
input += chunk;
});
process.stdin.on('end', async () => {
try {
const payload = JSON.parse(input);
debugLog('Session start hook started', { payload });
// Validate payload using centralized validation
const validation = validateHookPayload(payload, 'SessionStart');
if (!validation.valid) {
debugLog('Payload validation failed', { error: validation.error });
// For session start, continue even with invalid payload but log the error
const response = createHookResponse('SessionStart', false, {
error: `Payload validation failed: ${validation.error}`
});
console.log(JSON.stringify(response));
process.exit(0);
}
// Skip load-context when source is "resume" to avoid duplicate context
if (payload.source === 'resume') {
debugLog('Skipping load-context for resume source');
// Output valid JSON response with suppressOutput for resume
const response = createHookResponse('SessionStart', true);
console.log(JSON.stringify(response));
process.exit(0);
}
// Extract project name from current working directory
const projectName = path.basename(process.cwd());
// Load context using standardized CLI execution helper
const contextResult = await executeCliCommand(cliCommand, [
'load-context',
'--format', 'session-start',
'--project', projectName
]);
if (!contextResult.success) {
debugLog('Context loading failed', { stderr: contextResult.stderr });
// Don't fail the session start, just provide error context
const response = createHookResponse('SessionStart', false, {
error: contextResult.stderr || 'Failed to load context'
});
console.log(JSON.stringify(response));
process.exit(0);
}
const rawContext = contextResult.stdout;
debugLog('Raw context loaded', { contextLength: rawContext.length });
// Check if the output is actually an error message (starts with ❌)
if (rawContext && rawContext.trim().startsWith('❌')) {
debugLog('Detected error message in stdout', { rawContext });
// Extract the clean error message without the emoji and format
const errorMatch = rawContext.match(/❌\s*[^:]+:\s*([^\n]+)(?:\n\n💡\s*(.+))?/);
let errorMsg = 'No previous memories found';
let suggestion = '';
if (errorMatch) {
errorMsg = errorMatch[1] || errorMsg;
suggestion = errorMatch[2] || '';
}
// Create a clean response without duplicating the error formatting
const response = createHookResponse('SessionStart', false, {
error: errorMsg + (suggestion ? `. ${suggestion}` : '')
});
console.log(JSON.stringify(response));
process.exit(0);
}
if (!rawContext || !rawContext.trim()) {
debugLog('No context available, creating empty response');
// No context available - use standardized empty response
const response = createHookResponse('SessionStart', true);
console.log(JSON.stringify(response));
process.exit(0);
}
// Parse context data and format using centralized templates
const contextData = parseContextData(rawContext);
contextData.projectName = projectName;
// If we have raw context (not structured data), use it directly
let formattedContext;
if (contextData.rawContext) {
formattedContext = contextData.rawContext;
} else {
// Use standardized formatting for structured context
formattedContext = formatSessionStartContext(contextData);
}
debugLog('Context formatted successfully', {
memoryCount: contextData.memoryCount,
hasStructuredData: !contextData.rawContext
});
// Create standardized session start response using HookTemplates
const response = createHookResponse('SessionStart', true, {
context: formattedContext
});
console.log(JSON.stringify(response));
process.exit(0);
} catch (error) {
debugLog('Session start hook error', { error: error.message });
// Even on error, continue the session with error information
const response = createHookResponse('SessionStart', false, {
error: `Hook execution error: ${error.message}`
});
console.log(JSON.stringify(response));
process.exit(0);
}
});
/**
* Extracts project name from transcript path
* @param {string} transcriptPath - Path to transcript file
* @returns {string|null} Extracted project name or null
*/
function extractProjectName(transcriptPath) {
if (!transcriptPath) return null;
// Look for project pattern: /path/to/PROJECT_NAME/.claude/
// Need to get PROJECT_NAME, not the parent directory
const parts = transcriptPath.split(path.sep);
const claudeIndex = parts.indexOf('.claude');
if (claudeIndex > 0) {
// Get the directory immediately before .claude
return parts[claudeIndex - 1];
}
// Fall back to directory containing the transcript
const dir = path.dirname(transcriptPath);
return path.basename(dir);
}
-26
View File
@@ -1,26 +0,0 @@
#!/usr/bin/env node
/**
* Shared configuration loader utility for Claude Memory hooks
* Loads CLI command name from config.json with proper fallback handling
*/
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
import { readFileSync, existsSync } from 'fs';
/**
* Loads the CLI command name from the hooks config.json file
* @returns {string} The CLI command name (defaults to 'claude-mem')
*/
export function loadCliCommand() {
const __dirname = dirname(fileURLToPath(import.meta.url));
const configPath = join(__dirname, '..', 'config.json');
if (existsSync(configPath)) {
const config = JSON.parse(readFileSync(configPath, 'utf-8'));
return config.cliCommand || 'claude-mem';
}
return 'claude-mem';
}
-227
View File
@@ -1,227 +0,0 @@
#!/usr/bin/env node
/**
* Hook Helper Functions
*
* This module provides JavaScript wrappers around the TypeScript PromptOrchestrator
* and HookTemplates system, making them accessible to the JavaScript hook scripts.
*/
import { spawn } from 'child_process';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* Creates a standardized hook response using the HookTemplates system
* @param {string} hookType - Type of hook ('PreCompact' or 'SessionStart')
* @param {boolean} success - Whether the operation was successful
* @param {Object} options - Additional options
* @returns {Object} Formatted hook response
*/
export function createHookResponse(hookType, success, options = {}) {
if (hookType === 'PreCompact') {
if (success) {
return {
continue: true,
suppressOutput: true
};
} else {
return {
continue: false,
stopReason: options.reason || 'Pre-compact operation failed',
suppressOutput: true
};
}
}
if (hookType === 'SessionStart') {
if (success && options.context) {
return {
continue: true,
suppressOutput: true,
hookSpecificOutput: {
hookEventName: 'SessionStart',
additionalContext: options.context
}
};
} else if (success) {
// No context - just suppress output without any message
return {
continue: true,
suppressOutput: true
};
} else {
return {
continue: true, // Continue even on context loading failure
suppressOutput: true,
hookSpecificOutput: {
hookEventName: 'SessionStart',
additionalContext: `Context loading encountered an issue: ${options.error || 'Unknown error'}. Starting without previous context.`
}
};
}
}
// Generic response for unknown hook types
return {
continue: success,
suppressOutput: true,
...(options.reason && !success ? { stopReason: options.reason } : {})
};
}
/**
* Formats a session start context message using standardized templates
* @param {Object} contextData - Context information
* @returns {string} Formatted context message
*/
export function formatSessionStartContext(contextData) {
const {
projectName = 'unknown project',
memoryCount = 0,
lastSessionTime,
recentComponents = [],
recentDecisions = []
} = contextData;
const timeInfo = lastSessionTime ? ` (last worked: ${lastSessionTime})` : '';
const contextParts = [];
contextParts.push(`🧠 Loaded ${memoryCount} memories from previous sessions for ${projectName}${timeInfo}`);
if (recentComponents.length > 0) {
contextParts.push(`\n🎯 Recent components: ${recentComponents.slice(0, 3).join(', ')}`);
}
if (recentDecisions.length > 0) {
contextParts.push(`\n🔄 Recent decisions: ${recentDecisions.slice(0, 2).join(', ')}`);
}
if (memoryCount > 0) {
contextParts.push('\n💡 Use search_nodes("keywords") to find related work or open_nodes(["entity_name"]) to load specific components');
}
return contextParts.join('');
}
/**
* Executes a CLI command and returns the result
* @param {string} command - CLI command to execute
* @param {Array} args - Command arguments
* @param {Object} options - Spawn options
* @returns {Promise<{stdout: string, stderr: string, success: boolean}>}
*/
export async function executeCliCommand(command, args = [], options = {}) {
return new Promise((resolve) => {
const process = spawn(command, args, {
stdio: ['ignore', 'pipe', 'pipe'],
...options
});
let stdout = '';
let stderr = '';
if (process.stdout) {
process.stdout.on('data', (data) => {
stdout += data.toString();
});
}
if (process.stderr) {
process.stderr.on('data', (data) => {
stderr += data.toString();
});
}
process.on('close', (code) => {
resolve({
stdout: stdout.trim(),
stderr: stderr.trim(),
success: code === 0
});
});
process.on('error', (error) => {
resolve({
stdout: '',
stderr: error.message,
success: false
});
});
});
}
/**
* Parses context data from CLI output
* @param {string} output - Raw CLI output
* @returns {Object} Parsed context data
*/
export function parseContextData(output) {
if (!output || !output.trim()) {
return {
memoryCount: 0,
recentComponents: [],
recentDecisions: []
};
}
// Try to parse as JSON first (if CLI outputs structured data)
try {
const parsed = JSON.parse(output);
return {
memoryCount: parsed.memoryCount || 0,
recentComponents: parsed.recentComponents || [],
recentDecisions: parsed.recentDecisions || [],
lastSessionTime: parsed.lastSessionTime
};
} catch (e) {
// If not JSON, treat as plain text context
const lines = output.split('\n').filter(line => line.trim());
return {
memoryCount: lines.length,
recentComponents: [],
recentDecisions: [],
rawContext: output
};
}
}
/**
* Validates hook payload structure
* @param {Object} payload - Hook payload to validate
* @param {string} expectedHookType - Expected hook event name
* @returns {{valid: boolean, error?: string}} Validation result
*/
export function validateHookPayload(payload, expectedHookType) {
if (!payload || typeof payload !== 'object') {
return { valid: false, error: 'Payload must be a valid object' };
}
if (!payload.session_id || typeof payload.session_id !== 'string') {
return { valid: false, error: 'Missing or invalid session_id' };
}
if (!payload.transcript_path || typeof payload.transcript_path !== 'string') {
return { valid: false, error: 'Missing or invalid transcript_path' };
}
if (expectedHookType && payload.hook_event_name !== expectedHookType) {
return { valid: false, error: `Expected hook_event_name to be ${expectedHookType}` };
}
return { valid: true };
}
/**
* Logs debug information if debug mode is enabled
* @param {string} message - Debug message
* @param {Object} data - Additional data to log
*/
export function debugLog(message, data = {}) {
if (process.env.DEBUG === 'true' || process.env.CLAUDE_MEM_DEBUG === 'true') {
const timestamp = new Date().toISOString();
console.error(`[${timestamp}] HOOK DEBUG: ${message}`, data);
}
}
-63
View File
@@ -1,63 +0,0 @@
#!/usr/bin/env node
/**
* Path resolver utility for Claude Memory hooks
* Provides proper path handling using environment variables
*/
import { join } from 'path';
import { homedir } from 'os';
/**
* Gets the base data directory for claude-mem
* @returns {string} Data directory path
*/
export function getDataDir() {
return process.env.CLAUDE_MEM_DATA_DIR || join(homedir(), '.claude-mem');
}
/**
* Gets the settings file path
* @returns {string} Settings file path
*/
export function getSettingsPath() {
return join(getDataDir(), 'settings.json');
}
/**
* Gets the archives directory path
* @returns {string} Archives directory path
*/
export function getArchivesDir() {
return process.env.CLAUDE_MEM_ARCHIVES_DIR || join(getDataDir(), 'archives');
}
/**
* Gets the logs directory path
* @returns {string} Logs directory path
*/
export function getLogsDir() {
return process.env.CLAUDE_MEM_LOGS_DIR || join(getDataDir(), 'logs');
}
/**
* Gets the compact flag file path
* @returns {string} Compact flag file path
*/
export function getCompactFlagPath() {
return join(getDataDir(), '.compact-running');
}
/**
* Gets all common paths used by hooks
* @returns {Object} Object containing all common paths
*/
export function getPaths() {
return {
dataDir: getDataDir(),
settingsPath: getSettingsPath(),
archivesDir: getArchivesDir(),
logsDir: getLogsDir(),
compactFlagPath: getCompactFlagPath()
};
}