forked from obra/superpowers
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession-start
More file actions
executable file
·201 lines (173 loc) · 7.38 KB
/
session-start
File metadata and controls
executable file
·201 lines (173 loc) · 7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#!/usr/bin/env bash
# session-start with opt-in activation support
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
PROJECT_ROOT="$(pwd)"
# Default configuration
SUPERPOWERS_ENABLED=true
INJECTION_MODE="full"
LEGACY_WARNING=""
# ==============================================================================
# Configuration Parsing
# ==============================================================================
# Parse YAML config (simple implementation)
parse_config() {
local config_file="$1"
if [ -f "$config_file" ]; then
# Extract mode
if grep -q "mode: opt-in" "$config_file"; then
if [ ! -f "${PROJECT_ROOT}/.superpowers/enabled" ]; then
SUPERPOWERS_ENABLED=false
fi
elif grep -q "mode: never" "$config_file"; then
SUPERPOWERS_ENABLED=false
elif grep -q "mode: opt-out" "$config_file"; then
if [ -f "${PROJECT_ROOT}/.superpowers/disabled" ]; then
SUPERPOWERS_ENABLED=false
fi
fi
# Extract level
if grep -q "level: lightweight" "$config_file"; then
INJECTION_MODE="lightweight"
elif grep -q "level: minimal" "$config_file"; then
INJECTION_MODE="minimal"
fi
# Check context-aware settings
if grep -q "disable_on_main_branch: true" "$config_file"; then
CURRENT_BRANCH=$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")
if [[ "$CURRENT_BRANCH" == "main" || "$CURRENT_BRANCH" == "master" ]]; then
SUPERPOWERS_ENABLED=false
fi
fi
if grep -q "lightweight_on_detached_head: true" "$config_file"; then
CURRENT_BRANCH=${CURRENT_BRANCH:-$(git rev-parse --abbrev-ref HEAD 2>/dev/null || echo "unknown")}
if [ "$CURRENT_BRANCH" = "HEAD" ] && [ "$INJECTION_MODE" = "full" ]; then
INJECTION_MODE="lightweight"
fi
fi
if grep -q "require_git_repo: true" "$config_file"; then
if ! git rev-parse --git-dir > /dev/null 2>&1; then
SUPERPOWERS_ENABLED=false
fi
fi
fi
}
# ==============================================================================
# Check Activation State
# ==============================================================================
# 1. Check global disable file
if [ -f "${HOME}/.superpowers/disabled" ]; then
SUPERPOWERS_ENABLED=false
fi
# 2. Check configs (layered: plugin defaults → project overrides)
if [ -f "${PLUGIN_ROOT}/lib/config.yaml" ]; then
parse_config "${PLUGIN_ROOT}/lib/config.yaml"
fi
if [ -f "${PROJECT_ROOT}/.superpowers/config.yaml" ]; then
parse_config "${PROJECT_ROOT}/.superpowers/config.yaml"
fi
# 3. Check environment variable override (highest priority)
SUPERPOWERS_SESSION_MODE="${SUPERPOWERS_MODE:-}"
if [ -n "$SUPERPOWERS_SESSION_MODE" ]; then
case "$SUPERPOWERS_SESSION_MODE" in
enabled|always|opt-out)
SUPERPOWERS_ENABLED=true
;;
disabled|never)
SUPERPOWERS_ENABLED=false
;;
opt-in)
# For opt-in, require explicit .superpowers/enabled
if [ ! -f "${PROJECT_ROOT}/.superpowers/enabled" ]; then
SUPERPOWERS_ENABLED=false
fi
;;
*)
# Unknown mode, warn but continue
;;
esac
fi
# 4. Legacy skills directory warning
LEGACY_SKILLS_DIR="${HOME}/.config/superpowers/skills"
if [ -d "$LEGACY_SKILLS_DIR" ]; then
LEGACY_WARNING="\n\n<important-reminder>⚠️ **WARNING:** Superpowers now uses Claude Code's skills system. Custom skills in ~/.config/superpowers/skills will not be read. Move to ~/.claude/skills instead. Remove the directory to dismiss this message.</important-reminder>"
fi
# ==============================================================================
# Context Injection
# ==============================================================================
escape_for_json() {
local s="$1"
s="${s//\\/\\\\}"
s="${s//\"/\\\"}"
s="${s//$'\n'/\\n}"
s="${s//$'\r'/\\r}"
s="${s//$'\t'/\\t}"
printf '%s' "$s"
}
if [ "$SUPERPOWERS_ENABLED" = "true" ]; then
# Initialize variables
CONTENT=""
CONTENT_FILE=""
# Determine which content to inject based on mode
case "$INJECTION_MODE" in
full)
CONTENT_FILE="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
;;
lightweight)
# Create lightweight content (only core skills)
CONTENT="<EXTREMELY_IMPORTANT>\nSuperpowers is active in lightweight mode.\n\nCore skills available:\n- brainstorming: Design before implementation\n- using-superpowers: How to use Superpowers\n\nFull workflow (TDD, subagent-driven-development) is disabled.\nEnable with: /superpowers level full\n</EXTREMELY_IMPORTANT>"
;;
minimal)
CONTENT="<EXTREMELY_IMPORTANT>\nSuperpowers is active in minimal mode.\n\nOnly using-superpowers skill is loaded.\nEnable more: /superpowers level lightweight|full\n</EXTREMELY_IMPORTANT>"
;;
esac
if [ -n "$CONTENT_FILE" ] && [ -f "$CONTENT_FILE" ]; then
CONTENT=$(cat "$CONTENT_FILE")
fi
CONTENT="${CONTENT}${LEGACY_WARNING}"
else
# Determine why Superpowers is disabled for accurate notice
DISABLE_REASON="unknown"
if [ -n "$SUPERPOWERS_SESSION_MODE" ]; then
DISABLE_REASON="env_var"
elif [ -f "${HOME}/.superpowers/disabled" ]; then
DISABLE_REASON="global_disabled"
elif [ -f "${PROJECT_ROOT}/.superpowers/config.yaml" ] && \
grep -q "mode: never" "${PROJECT_ROOT}/.superpowers/config.yaml"; then
DISABLE_REASON="config_never"
elif grep -q "mode: opt-in" "${PLUGIN_ROOT}/lib/config.yaml" 2>/dev/null; then
DISABLE_REASON="opt_in"
fi
# Generate mode-specific notice
case "$DISABLE_REASON" in
env_var)
CONTENT="<SUPERPOWERS_AVAILABLE>\nSuperpowers disabled by environment variable.\n\nTo enable:\n- Unset SUPERPOWERS_MODE or set to 'enabled'\n- Current: SUPERPOWERS_MODE=$SUPERPOWERS_SESSION_MODE\n</SUPERPOWERS_AVAILABLE>"
;;
global_disabled)
CONTENT="<SUPERPOWERS_AVAILABLE>\nSuperpowers disabled globally.\n\nTo enable:\n- Remove ~/.superpowers/disabled file\n</SUPERPOWERS_AVAILABLE>"
;;
config_never)
CONTENT="<SUPERPOWERS_AVAILABLE>\nSuperpowers set to 'never' in config.\n\nTo enable:\n- Edit .superpowers/config.yaml and change mode\n</SUPERPOWERS_AVAILABLE>"
;;
opt_in)
CONTENT="<SUPERPOWERS_AVAILABLE>\nSuperpowers in opt-in mode.\n\nTo enable:\n- Create .superpowers/enabled file\n- Or run: /superpowers on\n</SUPERPOWERS_AVAILABLE>"
;;
*)
CONTENT="<SUPERPOWERS_AVAILABLE>\nSuperpowers is installed but not active.\n\nTo enable:\n- Create .superpowers/enabled file\n- Or run: /superpowers on\n</SUPERPOWERS_AVAILABLE>"
;;
esac
fi
CONTENT_ESCAPED=$(escape_for_json "$CONTENT")
SESSION_CONTEXT="<EXTREMELY_IMPORTANT>\n${CONTENT_ESCAPED}\n</EXTREMELY_IMPORTANT>"
# Output JSON
cat <<EOF
{
"additional_context": "${SESSION_CONTEXT}",
"hookSpecificOutput": {
"hookEventName": "SessionStart",
"additionalContext": "${SESSION_CONTEXT}"
}
}
EOF
exit 0