-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.sh
More file actions
executable file
·613 lines (527 loc) · 18.7 KB
/
install.sh
File metadata and controls
executable file
·613 lines (527 loc) · 18.7 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
#!/usr/bin/env bash
set -euo pipefail
# ---------------------------------------------------------------------------
# OpenCode SFX - Installer
# ---------------------------------------------------------------------------
# Installs sound effects for AI coding agents. Detects which clients you have
# installed (OpenCode, Claude Code, Gemini CLI, Codex) and configures each.
#
# Usage:
# # From a cloned repo:
# ./install.sh
#
# # One-liner (clones the repo for you):
# curl -fsSL https://raw.githubusercontent.com/jimrubenstein/opencode-sfx/main/install.sh | bash
#
# # Non-interactive (accepts all defaults):
# ./install.sh --yes
# curl -fsSL https://raw.githubusercontent.com/jimrubenstein/opencode-sfx/main/install.sh | bash -s -- --yes
# ---------------------------------------------------------------------------
REPO_URL="https://github.com/jimrubenstein/opencode-sfx.git"
DEFAULT_INSTALL_DIR="$HOME/.config/opencode/plugins/opencode-sfx"
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
RED='\033[0;31m'
BOLD='\033[1m'
NC='\033[0m'
info() { echo -e "${BOLD}$1${NC}"; }
ok() { echo -e "${GREEN}✓${NC} $1"; }
warn() { echo -e "${YELLOW}!${NC} $1"; }
err() { echo -e "${RED}✗${NC} $1"; }
# ---------------------------------------------------------------------------
# Parse arguments
# ---------------------------------------------------------------------------
AUTO_YES=false
for arg in "$@"; do
case "$arg" in
--yes|-y) AUTO_YES=true ;;
esac
done
# Helper: prompt user or auto-accept in non-interactive mode
confirm() {
local prompt="$1"
local default="${2:-Y}"
if [ "$AUTO_YES" = true ]; then
return 0
fi
read -rp "$prompt " answer
case "$default" in
Y) [[ ! "$answer" =~ ^[Nn] ]] ;;
N) [[ "$answer" =~ ^[Yy] ]] ;;
esac
}
echo ""
info "OpenCode SFX Installer"
echo " Sound effects for AI coding agents"
echo ""
# ---------------------------------------------------------------------------
# Determine plugin directory (clone if needed)
# ---------------------------------------------------------------------------
# Check if we're running from inside the repo already
SCRIPT_DIR=""
if [ -n "${BASH_SOURCE[0]:-}" ] && [ -f "${BASH_SOURCE[0]}" ]; then
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
fi
if [ -n "$SCRIPT_DIR" ] && [ -f "$SCRIPT_DIR/plugin.ts" ] && [ -f "$SCRIPT_DIR/package.json" ]; then
# Running from inside the repo (git clone && ./install.sh)
PLUGIN_DIR="$SCRIPT_DIR"
ok "Running from cloned repo: $PLUGIN_DIR"
else
# Running via curl | bash — need to clone the repo
PLUGIN_DIR="$DEFAULT_INSTALL_DIR"
if [ -d "$PLUGIN_DIR/.git" ] && [ -f "$PLUGIN_DIR/plugin.ts" ]; then
info "Found existing installation at $PLUGIN_DIR"
echo " Pulling latest changes..."
(cd "$PLUGIN_DIR" && git pull --quiet 2>/dev/null) && \
ok "Updated to latest version." || \
warn "Could not pull updates. Continuing with existing version."
else
info "Cloning opencode-sfx..."
if command -v git &>/dev/null; then
mkdir -p "$(dirname "$PLUGIN_DIR")"
git clone --quiet "$REPO_URL" "$PLUGIN_DIR" 2>/dev/null && \
ok "Cloned to $PLUGIN_DIR" || \
{ err "Failed to clone repository. Check your internet connection."; exit 1; }
else
err "git is required but not found. Install git and try again."
exit 1
fi
fi
fi
PLUGIN_PATH="$PLUGIN_DIR/plugin.ts"
# ---------------------------------------------------------------------------
# Install npm dependencies
# ---------------------------------------------------------------------------
echo ""
if [ -f "$PLUGIN_DIR/package.json" ]; then
info "Installing dependencies..."
if command -v npm &>/dev/null; then
(cd "$PLUGIN_DIR" && npm install --silent) && \
ok "Dependencies installed." || \
warn "npm install failed — run 'npm install' manually in $PLUGIN_DIR"
else
warn "npm not found. Run 'npm install' manually in $PLUGIN_DIR"
fi
fi
# ---------------------------------------------------------------------------
# Locate the OpenCode config
# ---------------------------------------------------------------------------
DEFAULT_CONFIG_DIR="$HOME/.config/opencode"
CONFIG_FILE=""
# Look for existing config in the default location
for ext in json jsonc; do
candidate="$DEFAULT_CONFIG_DIR/opencode.$ext"
if [ -f "$candidate" ]; then
CONFIG_FILE="$candidate"
break
fi
done
if [ -n "$CONFIG_FILE" ]; then
echo ""
info "Found OpenCode config:"
echo " $CONFIG_FILE"
if ! confirm "Use this config? [Y/n]"; then
CONFIG_FILE=""
fi
fi
if [ -z "$CONFIG_FILE" ]; then
if [ "$AUTO_YES" = true ]; then
# In auto mode, create default config if it doesn't exist
CONFIG_FILE="$DEFAULT_CONFIG_DIR/opencode.json"
if [ ! -f "$CONFIG_FILE" ]; then
mkdir -p "$DEFAULT_CONFIG_DIR"
echo '{}' > "$CONFIG_FILE"
ok "Created new OpenCode config at $CONFIG_FILE"
fi
else
echo ""
read -rp "Path to your OpenCode config file: " CONFIG_FILE
CONFIG_FILE="${CONFIG_FILE/#\~/$HOME}"
if [ ! -f "$CONFIG_FILE" ]; then
err "File not found: $CONFIG_FILE"
exit 1
fi
fi
fi
CONFIG_DIR="$(dirname "$CONFIG_FILE")"
echo ""
info "Configuring OpenCode SFX..."
echo ""
# ---------------------------------------------------------------------------
# Add plugin to config
# ---------------------------------------------------------------------------
if grep -q "plugin.ts" "$CONFIG_FILE" 2>/dev/null; then
# Check if it's specifically our plugin
if grep -q "$PLUGIN_PATH" "$CONFIG_FILE" 2>/dev/null || \
grep -q "opencode-sfx/plugin.ts" "$CONFIG_FILE" 2>/dev/null; then
warn "Plugin already present in config — skipping."
else
warn "A different plugin.ts is already configured. You may need to add this plugin manually:"
echo " $PLUGIN_PATH"
fi
else
# Add the plugin entry to the config
# Strategy: find "plugin": [...] and append, or add the key if missing
if grep -q '"plugin"' "$CONFIG_FILE" 2>/dev/null; then
# "plugin" key exists — append our path to the array
if python3 -c "
import json, re, sys
path = sys.argv[1]
plugin = sys.argv[2]
with open(path) as f:
raw = f.read()
# Strip JSONC comments (// style) for parsing
stripped = re.sub(r'//.*$', '', raw, flags=re.MULTILINE)
# Strip trailing commas before } or ]
stripped = re.sub(r',(\s*[}\]])', r'\1', stripped)
data = json.loads(stripped)
if plugin not in data.get('plugin', []):
data.setdefault('plugin', []).append(plugin)
# Reconstruct: we re-serialize and try to preserve the original style.
# Since the config may have comments/trailing commas (JSONC), we do a
# targeted insertion instead of a full rewrite.
import re as _re
m = _re.search(r'\"plugin\"\s*:\s*\[', raw)
if m:
# Find the closing bracket
depth = 1
i = m.end()
while i < len(raw) and depth > 0:
if raw[i] == '[': depth += 1
elif raw[i] == ']': depth -= 1
i += 1
bracket_pos = i - 1 # position of ]
# Check if array already has entries
array_content = raw[m.end():bracket_pos].strip()
if array_content:
# Has entries - add after the last one with a comma
insert_pos = bracket_pos
while insert_pos > m.end() and raw[insert_pos-1] in ' \t\n\r':
insert_pos -= 1
# Detect indentation from existing entries
lines = raw[m.end():bracket_pos].split('\n')
indent = ' '
for line in lines:
stripped_line = line.strip()
if stripped_line and stripped_line != '':
indent = line[:len(line) - len(line.lstrip())]
break
comma = ',' if not raw[insert_pos-1] == ',' else ''
insertion = comma + '\n' + indent + json.dumps(plugin) + ','
new_raw = raw[:insert_pos] + insertion + '\n' + raw[bracket_pos:]
else:
# Empty array - add the entry
indent = ' '
insertion = '\n' + indent + json.dumps(plugin) + ',\n '
new_raw = raw[:bracket_pos] + insertion + raw[bracket_pos:]
with open(path, 'w') as f:
f.write(new_raw)
sys.exit(0)
else:
sys.exit(1)
" "$CONFIG_FILE" "$PLUGIN_PATH" 2>/dev/null; then
ok "Added plugin to existing plugin array."
else
err "Could not automatically update the plugin array."
echo " Please add this to your config's \"plugin\" array manually:"
echo " \"$PLUGIN_PATH\""
fi
else
# No "plugin" key — insert one
if python3 -c "
import json, sys
path = sys.argv[1]
plugin = sys.argv[2]
with open(path) as f:
raw = f.read()
# If it's a minimal {} (possibly with whitespace), just rewrite the whole file
stripped = raw.strip()
if stripped == '{}' or stripped == '':
new_raw = '{\n \"plugin\": [\n ' + json.dumps(plugin) + '\n ]\n}\n'
else:
# Find the first { and insert after its line
brace = raw.index('{')
eol = raw.index('\n', brace)
indent = ' '
insertion = '\n' + indent + '\"plugin\": [\n' + indent + ' ' + json.dumps(plugin) + '\n' + indent + '],'
new_raw = raw[:eol] + insertion + raw[eol:]
with open(path, 'w') as f:
f.write(new_raw)
" "$CONFIG_FILE" "$PLUGIN_PATH" 2>/dev/null; then
ok "Added plugin entry to config."
else
err "Could not automatically update config."
echo " Please add this to your OpenCode config manually:"
echo ""
echo " \"plugin\": ["
echo " \"$PLUGIN_PATH\""
echo " ]"
fi
fi
fi
# ---------------------------------------------------------------------------
# Install /sfx command
# ---------------------------------------------------------------------------
echo ""
INSTALL_CMD=true
if [ "$AUTO_YES" = false ]; then
info "The /sfx command lets you manage themes at runtime."
echo " It can be installed globally (~/.config/opencode/commands/)"
echo " or used locally from this project directory."
echo ""
if ! confirm "Install /sfx command globally? [Y/n]"; then
INSTALL_CMD=false
fi
fi
if [ "$INSTALL_CMD" = true ]; then
COMMANDS_DIR="$CONFIG_DIR/commands"
mkdir -p "$COMMANDS_DIR"
cp "$PLUGIN_DIR/commands/sfx.md" "$COMMANDS_DIR/sfx.md"
ok "Installed /sfx command to $COMMANDS_DIR/sfx.md"
else
# Add command definition to local opencode.json so it works from project root
LOCAL_CONFIG="$PLUGIN_DIR/opencode.json"
if grep -q '"command"' "$LOCAL_CONFIG" 2>/dev/null; then
ok "/sfx command already defined in local opencode.json."
else
python3 -c "
import json, re, sys
path = sys.argv[1]
with open(path) as f:
raw = f.read()
# Strip trailing commas for parsing
stripped = re.sub(r',(\s*[}\]])', r'\1', raw)
data = json.loads(stripped)
data['command'] = {
'sfx': {
'description': 'Manage sound effect themes',
'template': open(sys.argv[2]).read().split('---', 2)[2].strip()
}
}
with open(path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
" "$LOCAL_CONFIG" "$PLUGIN_DIR/commands/sfx.md" 2>/dev/null && \
ok "Added /sfx command to local opencode.json." || \
warn "Could not update local opencode.json. The /sfx command file is at: commands/sfx.md"
fi
echo ""
warn "The /sfx command will only be available when running opencode from:"
echo " $PLUGIN_DIR"
fi
# ---------------------------------------------------------------------------
# Symlink CLI to PATH
# ---------------------------------------------------------------------------
echo ""
CLI_BIN="$PLUGIN_DIR/bin/opencode-sfx"
if [ -x "$CLI_BIN" ]; then
# Try common user-local bin directories
LINK_DIR=""
for candidate in "$HOME/.local/bin" "/usr/local/bin"; do
if [ -d "$candidate" ]; then
LINK_DIR="$candidate"
break
fi
done
if [ -n "$LINK_DIR" ]; then
LINK_TARGET="$LINK_DIR/opencode-sfx"
if [ -L "$LINK_TARGET" ] || [ -f "$LINK_TARGET" ]; then
ok "CLI already available at $LINK_TARGET"
else
if ln -sf "$CLI_BIN" "$LINK_TARGET" 2>/dev/null; then
ok "CLI linked: $LINK_TARGET"
else
warn "Could not symlink CLI to $LINK_DIR (permission denied)."
echo " You can run the CLI directly: $CLI_BIN"
fi
fi
else
warn "No standard bin directory found (~/.local/bin or /usr/local/bin)."
echo " You can run the CLI directly: $CLI_BIN"
fi
fi
# ---------------------------------------------------------------------------
# Detect and configure other AI coding agent clients
# ---------------------------------------------------------------------------
echo ""
info "Checking for other AI coding agent clients..."
echo ""
# --- Claude Code ---
CLAUDE_DETECTED=false
CLAUDE_CONFIG_DIR_PATH="${CLAUDE_CONFIG_DIR:-$HOME/.claude}"
if command -v claude &>/dev/null; then
CLAUDE_DETECTED=true
elif [ -d "$CLAUDE_CONFIG_DIR_PATH" ]; then
CLAUDE_DETECTED=true
fi
if [ "$CLAUDE_DETECTED" = true ]; then
INSTALL_CLAUDE=false
CLAUDE_SETTINGS="$CLAUDE_CONFIG_DIR_PATH/settings.json"
HOOK_CMD="$PLUGIN_DIR/integrations/claude-code/hook.sh"
if [ "$AUTO_YES" = true ]; then
INSTALL_CLAUDE=true
else
echo -e " ${GREEN}Found:${NC} Claude Code ($CLAUDE_CONFIG_DIR_PATH)"
echo " Events: task complete, permission request, errors, startup"
if confirm " Configure Claude Code? [Y/n]"; then
INSTALL_CLAUDE=true
fi
fi
if [ "$INSTALL_CLAUDE" = true ]; then
# Check if hooks are already configured
if [ -f "$CLAUDE_SETTINGS" ] && grep -q "opencode-sfx" "$CLAUDE_SETTINGS" 2>/dev/null; then
warn "Claude Code hooks already configured — skipping."
else
mkdir -p "$CLAUDE_CONFIG_DIR_PATH"
# Build hooks JSON and merge into settings
if python3 -c "
import json, sys, os
settings_path = sys.argv[1]
hook_cmd = sys.argv[2]
# Load existing settings or start fresh
if os.path.exists(settings_path):
with open(settings_path) as f:
data = json.load(f)
else:
data = {}
hooks = data.setdefault('hooks', {})
# Only add hooks that aren't already present
hook_entry = lambda: [{'hooks': [{'type': 'command', 'command': hook_cmd}]}]
startup_entry = lambda: [{'matcher': 'startup', 'hooks': [{'type': 'command', 'command': hook_cmd}]}]
if 'Stop' not in hooks:
hooks['Stop'] = hook_entry()
if 'SubagentStop' not in hooks:
hooks['SubagentStop'] = [] # explicitly empty — no sounds for subagents
if 'PermissionRequest' not in hooks:
hooks['PermissionRequest'] = hook_entry()
if 'PostToolUseFailure' not in hooks:
hooks['PostToolUseFailure'] = hook_entry()
if 'SessionStart' not in hooks:
hooks['SessionStart'] = startup_entry()
with open(settings_path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
" "$CLAUDE_SETTINGS" "$HOOK_CMD" 2>/dev/null; then
ok "Configured Claude Code hooks in $CLAUDE_SETTINGS"
else
err "Could not configure Claude Code automatically."
echo " See: $PLUGIN_DIR/integrations/claude-code/hook.sh"
fi
fi
fi
else
echo " Claude Code — not found"
fi
# --- Gemini CLI ---
GEMINI_DETECTED=false
GEMINI_CONFIG_DIR_PATH="${GEMINI_CLI_HOME:-$HOME/.gemini}"
if command -v gemini &>/dev/null; then
GEMINI_DETECTED=true
elif [ -d "$GEMINI_CONFIG_DIR_PATH" ]; then
GEMINI_DETECTED=true
fi
if [ "$GEMINI_DETECTED" = true ]; then
INSTALL_GEMINI=false
GEMINI_SETTINGS="$GEMINI_CONFIG_DIR_PATH/settings.json"
HOOK_CMD="$PLUGIN_DIR/integrations/gemini-cli/hook.sh"
if [ "$AUTO_YES" = true ]; then
INSTALL_GEMINI=true
else
echo -e " ${GREEN}Found:${NC} Gemini CLI ($GEMINI_CONFIG_DIR_PATH)"
echo " Events: task complete, notifications, startup"
if confirm " Configure Gemini CLI? [Y/n]"; then
INSTALL_GEMINI=true
fi
fi
if [ "$INSTALL_GEMINI" = true ]; then
if [ -f "$GEMINI_SETTINGS" ] && grep -q "opencode-sfx" "$GEMINI_SETTINGS" 2>/dev/null; then
warn "Gemini CLI hooks already configured — skipping."
else
mkdir -p "$GEMINI_CONFIG_DIR_PATH"
if python3 -c "
import json, sys, os
settings_path = sys.argv[1]
hook_cmd = sys.argv[2]
if os.path.exists(settings_path):
with open(settings_path) as f:
data = json.load(f)
else:
data = {}
hooks = data.setdefault('hooks', {})
def make_hook(event_name):
return [{'hooks': [{'type': 'command', 'command': f'{hook_cmd} {event_name}', 'timeout': 5000}]}]
def make_startup_hook(event_name):
return [{'matcher': 'startup', 'hooks': [{'type': 'command', 'command': f'{hook_cmd} {event_name}', 'timeout': 5000}]}]
if 'AfterAgent' not in hooks:
hooks['AfterAgent'] = make_hook('AfterAgent')
if 'Notification' not in hooks:
hooks['Notification'] = make_hook('Notification')
if 'SessionStart' not in hooks:
hooks['SessionStart'] = make_startup_hook('SessionStart')
with open(settings_path, 'w') as f:
json.dump(data, f, indent=2)
f.write('\n')
" "$GEMINI_SETTINGS" "$HOOK_CMD" 2>/dev/null; then
ok "Configured Gemini CLI hooks in $GEMINI_SETTINGS"
else
err "Could not configure Gemini CLI automatically."
echo " See: $PLUGIN_DIR/integrations/gemini-cli/hook.sh"
fi
fi
fi
else
echo " Gemini CLI — not found"
fi
# --- Codex ---
CODEX_DETECTED=false
CODEX_HOME_PATH="${CODEX_HOME:-$HOME/.codex}"
if command -v codex &>/dev/null; then
CODEX_DETECTED=true
elif [ -d "$CODEX_HOME_PATH" ]; then
CODEX_DETECTED=true
fi
if [ "$CODEX_DETECTED" = true ]; then
INSTALL_CODEX=false
CODEX_CONFIG="$CODEX_HOME_PATH/config.toml"
NOTIFY_CMD="$PLUGIN_DIR/integrations/codex/notify.sh"
if [ "$AUTO_YES" = true ]; then
INSTALL_CODEX=true
else
echo -e " ${GREEN}Found:${NC} Codex CLI ($CODEX_HOME_PATH)"
echo " Events: task complete only (limited)"
if confirm " Configure Codex CLI? [Y/n]"; then
INSTALL_CODEX=true
fi
fi
if [ "$INSTALL_CODEX" = true ]; then
if [ -f "$CODEX_CONFIG" ] && grep -q "opencode-sfx" "$CODEX_CONFIG" 2>/dev/null; then
warn "Codex CLI notify already configured — skipping."
else
mkdir -p "$CODEX_HOME_PATH"
if [ -f "$CODEX_CONFIG" ] && grep -q "^notify" "$CODEX_CONFIG" 2>/dev/null; then
warn "Codex config already has a 'notify' entry."
echo " To use opencode-sfx, set notify in $CODEX_CONFIG to:"
echo " notify = [\"$NOTIFY_CMD\"]"
else
# Append notify line to config (or create the file)
echo "" >> "$CODEX_CONFIG"
echo "# opencode-sfx: play sound on agent turn complete" >> "$CODEX_CONFIG"
echo "notify = [\"$NOTIFY_CMD\"]" >> "$CODEX_CONFIG"
ok "Configured Codex CLI notify in $CODEX_CONFIG"
fi
fi
fi
else
echo " Codex CLI — not found"
fi
# ---------------------------------------------------------------------------
# Done
# ---------------------------------------------------------------------------
echo ""
echo -e "${GREEN}${BOLD}Installation complete!${NC}"
echo ""
echo " A default notification theme is included — sounds work out of the box."
echo ""
echo " Restart your AI coding agent to activate sound effects."
echo " In OpenCode, run /sfx to manage themes."
echo ""