#!/usr/bin/env bash # Hook: UserPromptSubmit # # Fires on every user message. Instead of pre-searching mem0 with the # raw prompt, this injects a decision rubric telling the agent when # and how to search itself. The agent has more context than this # script does -- let it decide. # # Input: JSON on stdin (prompt, session_id, cwd, transcript_path) # Output: Decision rubric injected into Claude's context (exit 0) # Intentionally omit -e so the script always exits 0 even if jq fails -- # must never block the user's prompt. set -uo pipefail if [ -n "${MEM0_DEBUG:-}" ]; then mkdir -p "$HOME/.mem0" && exec 2>>"$HOME/.mem0/hooks.log" fi INPUT=$(cat) PROMPT=$(echo "$INPUT" | jq -r '.prompt // ""' 2>/dev/null || echo "") # Acknowledgements and short replies don't warrant memory context if [ ${#PROMPT} -lt 20 ]; then exit 0 fi # No API key means the agent can't search anyway if [ -z "${MEM0_API_KEY:-}" ]; then exit 0 fi SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # shellcheck source=_identity.sh . "$SCRIPT_DIR/_identity.sh" USER_ID="$MEM0_RESOLVED_USER_ID" cat <