cc-os/plugins/memory/hooks/session-context.sh

47 lines
1.8 KiB
Bash
Raw Normal View History

#!/usr/bin/env bash
# UserPromptSubmit hook — injects project graph path on the FIRST user prompt per session.
# Outputs: {"hookSpecificOutput": {"hookEventName": "UserPromptSubmit", "additionalContext": "..."}}
# On subsequent prompts: outputs {} (no-op).
# Do NOT set -e — failures are expected and handled.
# ---------------------------------------------------------------------------
# 0. Read stdin
# ---------------------------------------------------------------------------
STDIN_JSON="$(cat 2>/dev/null || true)"
SESSION_ID="$(printf '%s' "$STDIN_JSON" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('session_id','unknown'))" 2>/dev/null || true)"
CWD="$(printf '%s' "$STDIN_JSON" | python3 -c "import sys,json; d=json.load(sys.stdin); print(d.get('cwd',''))" 2>/dev/null || true)"
CWD="${CWD:-$PWD}"
FLAG_FILE="/tmp/memory-context-injected-${SESSION_ID:-unknown}"
if [ -f "$FLAG_FILE" ]; then
echo '{}'
exit 0
fi
touch "$FLAG_FILE"
# ---------------------------------------------------------------------------
# 1. Project graph pointer (only output)
# ---------------------------------------------------------------------------
CONTEXT=""
PROJECT_ROOT="$(git -C "$CWD" rev-parse --show-toplevel 2>/dev/null || true)"
if [ -n "$PROJECT_ROOT" ]; then
PROJECT_GRAPH="$PROJECT_ROOT/graphify-out/graph.json"
if [ -f "$PROJECT_GRAPH" ]; then
CONTEXT="## Project Graph
Project graph: ${PROJECT_GRAPH}
"
fi
fi
# ---------------------------------------------------------------------------
# 2. Emit JSON
# ---------------------------------------------------------------------------
OUTPUT="$(python3 -c "import json, sys; print(json.dumps({'hookSpecificOutput': {'hookEventName': 'UserPromptSubmit', 'additionalContext': sys.argv[1]}}))" "$CONTEXT" 2>/dev/null)"
printf '%s\n' "$OUTPUT"
exit 0