Files
mem0/mem0-plugin/scripts/on_stop_cursor.sh
T
kartik-mem0 82ab253f2c fix(mem0-plugin): sanitize marker path, add pre-compact REST backup, fix cross-tool gaps
- Sanitize project_id in onboard marker path (tr '/:' '--') to handle
  slashes from env var overrides
- Fire on_pre_compact.py REST backup from PreCompact hook (was only
  called from Stop hooks — crash before Stop = data loss)
- Source _identity.sh in on_stop_codex.sh and on_stop_cursor.sh so
  MEM0_PROJECT_ID + API key fallback are available
- Note dream --schedule is Claude Code-only (Codex/Cursor lack schedule)
2026-05-21 17:43:40 +05:30

52 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
# Hook: Stop (Cursor)
#
# Fires when Cursor agent completes a turn. Wraps the same logic as
# on_stop.sh but outputs JSON (Cursor expects {"followup_message":"..."}).
#
# Input: JSON on stdin with status, loop_count, conversation_id, etc.
# Output: JSON on stdout: {"followup_message":"<reminder text>"}
set -uo pipefail
if [ -n "${MEM0_DEBUG:-}" ]; then
mkdir -p "$HOME/.mem0" && exec 2>>"$HOME/.mem0/hooks.log"
fi
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
# shellcheck source=_identity.sh
. "$SCRIPT_DIR/_identity.sh"
INPUT=$(cat)
# Session-end report (best-effort)
REPORT=$(python3 "$SCRIPT_DIR/session_stats.py" report 2>/dev/null || echo "")
REPORT_BLOCK=""
if [ -n "$REPORT" ]; then
REPORT_BLOCK="---\nmem0 $REPORT\n---\n\n"
mkdir -p "$HOME/.mem0" 2>/dev/null || true
echo "$(date -u +%Y-%m-%dT%H:%M:%SZ) | $REPORT" >> "$HOME/.mem0/session-log.md" 2>/dev/null || true
fi
MESSAGE=$(cat <<EOF
${REPORT_BLOCK}Before finishing, check if there are important learnings from this interaction that should be persisted using the mem0 \`add_memory\` tool:
1. Were any significant decisions made? -> Store with metadata \`{"type": "decision"}\`
2. Were any new patterns or strategies discovered? -> Store with metadata \`{"type": "task_learning"}\`
3. Did any approach fail? -> Store with metadata \`{"type": "anti_pattern"}\`
4. Did you learn anything about the user's preferences? -> Store with metadata \`{"type": "user_preference"}\`
5. Were there environment/setup discoveries? -> Store with metadata \`{"type": "environmental"}\`
Always include \`app_id\` (the active project_id from session start) as a top-level parameter in every \`add_memory\` call.
If nothing notable happened, it's fine to skip. Only store genuinely useful learnings.
EOF
)
jq -cn --arg msg "$MESSAGE" '{followup_message:$msg}'
# Capture transcript state in the background via Mem0 REST API
echo "$INPUT" | python3 "$SCRIPT_DIR/on_pre_compact.py" --source=session-end 2>/dev/null &
exit 0