session-start 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env bash
  2. # SessionStart hook for superpowers plugin
  3. set -euo pipefail
  4. # Determine plugin root directory
  5. SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
  6. PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
  7. # Check if legacy skills directory exists and build warning
  8. warning_message=""
  9. legacy_skills_dir="${HOME}/.config/superpowers/skills"
  10. if [ -d "$legacy_skills_dir" ]; then
  11. warning_message="\n\n<important-reminder>IN YOUR FIRST REPLY AFTER SEEING THIS MESSAGE YOU MUST TELL THE USER:⚠️ **WARNING:** Superpowers now uses Claude Code's skills system. Custom skills in ~/.config/superpowers/skills will not be read. Move custom skills to ~/.claude/skills instead. To make this message go away, remove ~/.config/superpowers/skills</important-reminder>"
  12. fi
  13. # Read using-superpowers content
  14. using_superpowers_content=$(cat "${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md" 2>&1 || echo "Error reading using-superpowers skill")
  15. # Escape string for JSON embedding using bash parameter substitution.
  16. # Each ${s//old/new} is a single C-level pass - orders of magnitude
  17. # faster than the character-by-character loop this replaces.
  18. escape_for_json() {
  19. local s="$1"
  20. s="${s//\\/\\\\}"
  21. s="${s//\"/\\\"}"
  22. s="${s//$'\n'/\\n}"
  23. s="${s//$'\r'/\\r}"
  24. s="${s//$'\t'/\\t}"
  25. printf '%s' "$s"
  26. }
  27. using_superpowers_escaped=$(escape_for_json "$using_superpowers_content")
  28. warning_escaped=$(escape_for_json "$warning_message")
  29. session_context="<EXTREMELY_IMPORTANT>\nYou have superpowers.\n\n**Below is the full content of your 'superpowers:using-superpowers' skill - your introduction to using skills. For all other skills, use the 'Skill' tool:**\n\n${using_superpowers_escaped}\n\n${warning_escaped}\n</EXTREMELY_IMPORTANT>"
  30. # Output context injection as JSON.
  31. # Cursor hooks expect additional_context (snake_case).
  32. # Claude Code hooks expect hookSpecificOutput.additionalContext (nested).
  33. # Copilot CLI (v1.0.11+) and others expect additionalContext (top-level, SDK standard).
  34. # Claude Code reads BOTH additional_context and hookSpecificOutput without
  35. # deduplication, so we must emit only the field the current platform consumes.
  36. #
  37. # Uses printf instead of heredoc to work around bash 5.3+ heredoc hang.
  38. # See: https://github.com/obra/superpowers/issues/571
  39. if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
  40. # Cursor sets CURSOR_PLUGIN_ROOT (may also set CLAUDE_PLUGIN_ROOT)
  41. printf '{\n "additional_context": "%s"\n}\n' "$session_context"
  42. elif [ -n "${CLAUDE_PLUGIN_ROOT:-}" ] && [ -z "${COPILOT_CLI:-}" ]; then
  43. # Claude Code sets CLAUDE_PLUGIN_ROOT without COPILOT_CLI
  44. printf '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' "$session_context"
  45. else
  46. # Copilot CLI (sets COPILOT_CLI=1) or unknown platform — SDK standard format
  47. printf '{\n "additionalContext": "%s"\n}\n' "$session_context"
  48. fi
  49. exit 0