#!/usr/bin/env bash
# Queries RAG for the file being read. Prints results to stdout for Claude to see.
# Exits 0 always — never blocks the Read.
FILE="$CLAUDE_TOOL_INPUT_FILE_PATH"
WS="${MCP_RAG_WORKSPACE_ID:-workspace}"
URL="${MCP_RAG_URL:-http://localhost:8001}"

case "$FILE" in
  *.lock|*.png|*.jpg|*.gif|*.ico|node_modules/*|dist/*) exit 0 ;;
esac

QUERY=$(basename "$FILE" | sed 's/\.[^.]*$//')

RESULT=$(curl -sf --max-time 3 "$URL/query" \
  -H "Content-Type: application/json" \
  -d "{\"query\":\"$QUERY\",\"workspace_id\":\"$WS\",\"top_k\":3}" 2>/dev/null)

if [ -n "$RESULT" ]; then
  echo "## RAG context for $FILE"
  echo "$RESULT" | jq -r '.results[] | "[\(.score | . * 100 | round / 100)] \(.metadata.file_path): \(.content[:200])"' 2>/dev/null
fi
exit 0
