From c5bb11b8b1ff2e11922426ec600409b3695e4c5c Mon Sep 17 00:00:00 2001 From: kartik-mem0 Date: Thu, 21 May 2026 19:13:13 +0530 Subject: [PATCH] fix(mem0-plugin): graceful degradation without API key, stdin support - SessionStart shows "Mem0 Inactive" banner with identity info when no API key set, instead of silently exiting with zero output - on_user_prompt.sh fires error/file-path detection even without API key (local pattern matching, no API needed). Search rubric still gated. - parse_export_file.py supports '-' argument for stdin piping --- mem0-plugin/scripts/on_session_start.sh | 16 ++++++++++------ mem0-plugin/scripts/on_user_prompt.sh | 22 ++++++++++++++-------- mem0-plugin/scripts/parse_export_file.py | 7 +++++-- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/mem0-plugin/scripts/on_session_start.sh b/mem0-plugin/scripts/on_session_start.sh index 62da79418..0aa973e61 100755 --- a/mem0-plugin/scripts/on_session_start.sh +++ b/mem0-plugin/scripts/on_session_start.sh @@ -19,18 +19,22 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # shellcheck source=_identity.sh . "$SCRIPT_DIR/_identity.sh" -# Skip the bootstrap entirely if no API key is configured -- the agent -# would otherwise be told to call mem0 MCP tools that will all fail. -if [ -z "${MEM0_API_KEY:-}" ]; then - exit 0 -fi - # Initialize session stats tracker python3 "$SCRIPT_DIR/session_stats.py" init 2>/dev/null || true INPUT=$(cat) SOURCE=$(echo "$INPUT" | jq -r '.source // "startup"' 2>/dev/null || echo "startup") +# Skip bootstrap if no API key — still show banner but with "no key" status. +if [ -z "${MEM0_API_KEY:-}" ]; then + echo "## Mem0 Inactive" + echo "" + echo "\`user=${MEM0_RESOLVED_USER_ID:-$USER} | project=${MEM0_PROJECT_ID:-unknown} | branch=${MEM0_BRANCH:-unknown} | api_key=NOT_SET\`" + echo "" + echo "Set MEM0_API_KEY to enable persistent memory. Get a key at https://app.mem0.ai/dashboard/api-keys" + exit 0 +fi + # Fetch project-scoped memory count (best-effort, don't block on failure, 5s timeout) MEM0_COUNT="?" if command -v python3 >/dev/null 2>&1; then diff --git a/mem0-plugin/scripts/on_user_prompt.sh b/mem0-plugin/scripts/on_user_prompt.sh index fdfa86f05..0389dfbac 100755 --- a/mem0-plugin/scripts/on_user_prompt.sh +++ b/mem0-plugin/scripts/on_user_prompt.sh @@ -29,21 +29,27 @@ SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # shellcheck source=_identity.sh . "$SCRIPT_DIR/_identity.sh" -# No API key means the agent can't search anyway -if [ -z "${MEM0_API_KEY:-}" ]; then - exit 0 -fi -USER_ID="$MEM0_RESOLVED_USER_ID" - -# Detect stack traces and error patterns in the prompt +# Detect stack traces and error patterns in the prompt (no API needed) HAS_ERROR="" if echo "$PROMPT" | grep -qiE '(Traceback|Error:|Exception:|panic:|FAILED|fatal:| at .+\.[a-z]+:[0-9]+)'; then HAS_ERROR="true" fi -# Detect file paths in the prompt +# Detect file paths in the prompt (no API needed) FILE_PATHS=$(echo "$PROMPT" | grep -oE '([a-zA-Z0-9_./-]+\.(py|ts|tsx|js|jsx|rs|go|rb|java|sh|yaml|yml|json|toml|md|sql|css|html))\b' 2>/dev/null | head -5 || echo "") +# No API key — emit detections only, skip search rubric +if [ -z "${MEM0_API_KEY:-}" ]; then + if [ -n "$HAS_ERROR" ]; then + echo "**ERROR DETECTED in prompt.** Set MEM0_API_KEY to search past debugging context." + fi + if [ -n "$FILE_PATHS" ]; then + echo "**FILE PATHS detected:** \`$FILE_PATHS\`" + fi + exit 0 +fi +USER_ID="$MEM0_RESOLVED_USER_ID" + cat < None: filepath = sys.argv[1] try: - with open(filepath, encoding="utf-8", errors="replace") as f: - content = f.read() + if filepath == "-": + content = sys.stdin.read() + else: + with open(filepath, encoding="utf-8", errors="replace") as f: + content = f.read() except OSError as e: print(f"Error reading file: {e}", file=sys.stderr) print("[]")