The on_user_prompt hook previously ran a blind semantic search using the raw user prompt as the query and injected the top results unconditionally. That undermined the agent's judgment with low-quality context and made heavy API calls on prompts where memory wouldn't help. Replace with a decision rubric injected into context per turn. The agent decides whether memory would improve the response and, if so, runs targeted searches with metadata.type filters and proper query phrasing. Mirror the same rules in mem0-codex/SKILL.md so the skill (loaded once) and the hook (re-asserted per turn) agree. Skip gates preserved: prompts under 20 chars and missing MEM0_API_KEY. - on_user_prompt.sh: API call replaced with stdout rubric injection - hooks.json / codex-hooks.json: statusMessage updated to match new behavior - mem0-codex/SKILL.md: "On every new task" rewritten with search/skip rules, query phrasing guidance, metadata filter table, and a worked example
64 lines
2.3 KiB
Bash
Executable File
64 lines
2.3 KiB
Bash
Executable File
#!/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
|
|
|
|
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
|
|
|
|
USER_ID="${MEM0_USER_ID:-${USER:-default}}"
|
|
|
|
cat <<EOF
|
|
## Memory check
|
|
|
|
Before responding, decide whether persistent memory context from mem0 would
|
|
improve your answer. The agent -- not this hook -- owns this decision.
|
|
|
|
**Search WHEN** the user:
|
|
- references past work, decisions, or things "we" built
|
|
- asks "how should we...", "best way to...", or any decision-style question
|
|
- hits an error, bug, or asks for debugging help
|
|
- requests work that touches their stack, tools, conventions, or preferences
|
|
- starts a non-trivial task in a known project
|
|
|
|
**Skip WHEN:**
|
|
- the prompt is an acknowledgement or continuation
|
|
- the user is *stating* new info -- that's a write trigger (\`add_memory\`), not a search
|
|
- it's a pure syntax / factual question answerable from general knowledge
|
|
- you already searched this scope earlier in the turn
|
|
|
|
**If searching, do it well:**
|
|
- Run **2-4 parallel** \`search_memories\` calls with different angles, not one
|
|
query that echoes the user's prompt.
|
|
- Phrase queries as **nouns** ("auth module decisions"), not full sentences.
|
|
- Use \`metadata.type\` filters where they fit:
|
|
- \`{"metadata.type": "decision"}\` -- design / architecture questions
|
|
- \`{"metadata.type": "anti_pattern"}\` -- debugging, error handling
|
|
- \`{"metadata.type": "user_preference"}\` -- tooling, stack, style
|
|
- \`{"metadata.type": "convention"}\` -- established patterns in this project
|
|
- Scope every query with \`{"user_id": "$USER_ID"}\`.
|
|
- Empty results are normal -- proceed without context.
|
|
EOF
|
|
|
|
exit 0
|