Files
claude-mem/cursor-hooks/session-init.sh
T
Alex Newman 8d485890b9 feat(cursor): Add Claude-Mem Cursor hooks installation and management
- Introduced functionality for installing, uninstalling, and checking the status of Cursor hooks.
- Added a new command structure for managing hooks with detailed usage instructions.
- Implemented a method to locate the cursor-hooks directory across different environments.
- Updated build-hooks script to inform users about the location of Cursor hooks.

This enhancement streamlines the integration of Claude-Mem with Cursor, improving user experience and accessibility of hooks.
2025-12-29 20:14:23 -05:00

94 lines
2.7 KiB
Bash
Executable File

#!/bin/bash
# Session Initialization Hook for Cursor
# Maps to claude-mem's new-hook functionality
# Initializes a new session when a prompt is submitted
#
# NOTE: This hook runs as part of beforeSubmitPrompt and MUST output valid JSON
# with at least {"continue": true} to allow prompt submission.
# Source common utilities
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "${SCRIPT_DIR}/common.sh" 2>/dev/null || {
# Fallback - output continue and exit
echo '{"continue": true}'
exit 0
}
# Check dependencies (non-blocking - just warn)
check_dependencies >/dev/null 2>&1 || true
# Read JSON input from stdin with error handling
input=$(read_json_input)
# Extract common fields with safe fallbacks
conversation_id=$(json_get "$input" "conversation_id" "")
generation_id=$(json_get "$input" "generation_id" "")
prompt=$(json_get "$input" "prompt" "")
workspace_root=$(json_get "$input" "workspace_roots[0]" "")
# Fallback to current directory if no workspace root
if is_empty "$workspace_root"; then
workspace_root=$(pwd)
fi
# Get project name from workspace root
project_name=$(get_project_name "$workspace_root")
# Use conversation_id as session_id (stable across turns), fallback to generation_id
session_id="$conversation_id"
if is_empty "$session_id"; then
session_id="$generation_id"
fi
# Exit gracefully if no session_id available (still allow prompt)
if is_empty "$session_id"; then
echo '{"continue": true}'
exit 0
fi
# Get worker port from settings with validation
worker_port=$(get_worker_port)
# Ensure worker is running (with retries like claude-mem hooks)
if ! ensure_worker_running "$worker_port"; then
# Worker not ready - still allow prompt to continue
echo '{"continue": true}'
exit 0
fi
# Strip leading slash from commands for memory agent (parity with new-hook.ts)
# /review 101 → review 101 (more semantic for observations)
cleaned_prompt="$prompt"
if [ -n "$prompt" ] && [ "${prompt:0:1}" = "/" ]; then
cleaned_prompt="${prompt:1}"
fi
# Initialize session via HTTP - handles DB operations and privacy checks
payload=$(jq -n \
--arg sessionId "$session_id" \
--arg project "$project_name" \
--arg promptText "$cleaned_prompt" \
'{
contentSessionId: $sessionId,
project: $project,
prompt: $promptText
}' 2>/dev/null)
# Exit if payload creation failed (still allow prompt)
if [ -z "$payload" ]; then
echo '{"continue": true}'
exit 0
fi
# Send request to worker (fire-and-forget, don't wait for response)
curl -s -X POST \
"http://127.0.0.1:${worker_port}/api/sessions/init" \
-H "Content-Type: application/json" \
-d "$payload" \
>/dev/null 2>&1 &
# Always allow prompt to continue
echo '{"continue": true}'
exit 0