2025-10-09 13:11:12 -07:00
#!/usr/bin/env bash
# SessionStart hook for superpowers plugin
2025-10-10 14:01:45 -07:00
set -euo pipefail
2025-10-16 08:12:18 -07:00
# Determine plugin root directory
2026-03-15 18:40:33 +00:00
SCRIPT_DIR = " $( cd " $( dirname " $0 " ) " && pwd ) "
2025-10-11 11:14:44 -07:00
PLUGIN_ROOT = " $( cd " ${ SCRIPT_DIR } /.." && pwd ) "
2025-10-11 12:05:08 -07:00
2025-10-16 08:12:18 -07:00
# Read using-superpowers content
using_superpowers_content = $( cat " ${ PLUGIN_ROOT } /skills/using-superpowers/SKILL.md" 2>& 1 || echo "Error reading using-superpowers skill" )
2025-10-10 22:59:26 -07:00
2026-02-05 11:38:06 -08:00
# Escape string for JSON embedding using bash parameter substitution.
# Each ${s//old/new} is a single C-level pass - orders of magnitude
# faster than the character-by-character loop this replaces.
2025-12-01 15:42:12 -08:00
escape_for_json() {
2026-02-05 11:38:06 -08:00
local s = " $1 "
s = " ${ s // \\ / \\\\ } "
s = " ${ s // \" / \\\" } "
s = " ${ s // $'\n' / \\ n } "
s = " ${ s // $'\r' / \\ r } "
s = " ${ s // $'\t' / \\ t } "
printf '%s' " $s "
2025-12-01 15:42:12 -08:00
}
using_superpowers_escaped = $( escape_for_json " $using_superpowers_content " )
2026-05-14 14:48:16 -07:00
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</EXTREMELY_IMPORTANT>"
2025-10-11 12:05:08 -07:00
2026-02-13 12:00:52 -05:00
# Output context injection as JSON.
2026-03-25 14:05:56 -07:00
# Cursor hooks expect additional_context (snake_case).
# Claude Code hooks expect hookSpecificOutput.additionalContext (nested).
# Copilot CLI (v1.0.11+) and others expect additionalContext (top-level, SDK standard).
# Claude Code reads BOTH additional_context and hookSpecificOutput without
# deduplication, so we must emit only the field the current platform consumes.
2026-03-15 19:14:34 +00:00
#
2026-03-25 14:05:56 -07:00
# Uses printf instead of heredoc to work around bash 5.3+ heredoc hang.
2026-03-15 19:14:34 +00:00
# See: https://github.com/obra/superpowers/issues/571
2026-03-15 19:35:18 +00:00
if [ -n " ${ CURSOR_PLUGIN_ROOT :- } " ] ; then
2026-03-25 14:05:56 -07:00
# Cursor sets CURSOR_PLUGIN_ROOT (may also set CLAUDE_PLUGIN_ROOT)
2026-03-15 19:35:18 +00:00
printf '{\n "additional_context": "%s"\n}\n' " $session_context "
2026-03-25 14:05:56 -07:00
elif [ -n " ${ CLAUDE_PLUGIN_ROOT :- } " ] && [ -z " ${ COPILOT_CLI :- } " ] ; then
# Claude Code sets CLAUDE_PLUGIN_ROOT without COPILOT_CLI
2026-03-15 19:14:34 +00:00
printf '{\n "hookSpecificOutput": {\n "hookEventName": "SessionStart",\n "additionalContext": "%s"\n }\n}\n' " $session_context "
2026-03-09 17:20:31 -07:00
else
2026-03-25 14:05:56 -07:00
# Copilot CLI (sets COPILOT_CLI=1) or unknown platform — SDK standard format
printf '{\n "additionalContext": "%s"\n}\n' " $session_context "
2026-03-09 17:20:31 -07:00
fi
2025-10-09 13:11:12 -07:00
exit 0