|
| 1 | +#!/bin/bash |
| 2 | +# Emits an OSC terminal escape sequence using the best available method. |
| 3 | +# |
| 4 | +# Claude Code 2.1.141 added a `terminalSequence` JSON output field for hooks, |
| 5 | +# so they can deliver OSC sequences without a controlling terminal. Older |
| 6 | +# Claude Code doesn't know that field, and Stop hooks reject unknown fields |
| 7 | +# ("Stop hook error: JSON validation failed"), so on older versions we must |
| 8 | +# write to /dev/tty instead. |
| 9 | +# |
| 10 | +# Decision tree: |
| 11 | +# 1. CLAUDE_CODE_VERSION known, >= 2.1.141 → emit terminalSequence JSON |
| 12 | +# 2. CLAUDE_CODE_VERSION known, < 2.1.141 → write /dev/tty; give up if missing |
| 13 | +# 3. CLAUDE_CODE_VERSION unknown → try /dev/tty, fall back to JSON |
| 14 | +# |
| 15 | +# Usage: |
| 16 | +# source "$SCRIPT_DIR/emit-terminal-sequence.sh" |
| 17 | +# SEQ=$(printf '\033]777;notify;%s;%s\007' "$TITLE" "$BODY") |
| 18 | +# emit_terminal_sequence "$SEQ" |
| 19 | +# |
| 20 | +# When the sequence is delivered via /dev/tty (side effect), nothing is printed |
| 21 | +# to stdout. When it must go through terminalSequence, a JSON object is printed |
| 22 | +# to stdout — the caller should ensure this reaches the hook's stdout. |
| 23 | + |
| 24 | +# The first Claude Code version that supports the terminalSequence output field. |
| 25 | +TERMINAL_SEQUENCE_MIN_VERSION="2.1.141" |
| 26 | + |
| 27 | +# Compare two dotted version strings (e.g. "2.1.141" >= "2.1.141"). |
| 28 | +# Returns 0 (true) if $1 >= $2, 1 (false) otherwise. |
| 29 | +_version_at_least() { |
| 30 | + local a b av bv i |
| 31 | + IFS=. read -ra a <<< "$1" |
| 32 | + IFS=. read -ra b <<< "$2" |
| 33 | + for ((i = 0; i < ${#b[@]}; i++)); do |
| 34 | + av="${a[i]:-0}" |
| 35 | + bv="${b[i]:-0}" |
| 36 | + if ((av > bv)); then return 0; fi |
| 37 | + if ((av < bv)); then return 1; fi |
| 38 | + done |
| 39 | + return 0 |
| 40 | +} |
| 41 | + |
| 42 | +# Extract a bare version number (e.g. "2.1.141") from `claude --version` output, |
| 43 | +# which may look like "claude 2.1.141" or "2.1.141" or "Claude Code v2.1.141". |
| 44 | +_parse_cc_version() { |
| 45 | + echo "$1" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -1 |
| 46 | +} |
| 47 | + |
| 48 | +# Returns 0 if the running Claude Code version supports terminalSequence. |
| 49 | +_supports_terminal_sequence() { |
| 50 | + local raw="${CLAUDE_CODE_VERSION:-}" |
| 51 | + [ -z "$raw" ] && return 1 |
| 52 | + local ver |
| 53 | + ver=$(_parse_cc_version "$raw") |
| 54 | + [ -z "$ver" ] && return 1 |
| 55 | + _version_at_least "$ver" "$TERMINAL_SEQUENCE_MIN_VERSION" |
| 56 | +} |
| 57 | + |
| 58 | +emit_terminal_sequence() { |
| 59 | + local seq="$1" |
| 60 | + [ -z "$seq" ] && return 0 |
| 61 | + |
| 62 | + # Classify the running Claude Code version, if we can. |
| 63 | + local raw="${CLAUDE_CODE_VERSION:-}" |
| 64 | + local ver="" |
| 65 | + [ -n "$raw" ] && ver=$(_parse_cc_version "$raw") |
| 66 | + |
| 67 | + if [ -n "$ver" ]; then |
| 68 | + if _version_at_least "$ver" "$TERMINAL_SEQUENCE_MIN_VERSION"; then |
| 69 | + # Known new Claude Code — use the structured output field. |
| 70 | + jq -nc --arg seq "$seq" '{terminalSequence: $seq}' |
| 71 | + else |
| 72 | + # Known-old Claude Code — /dev/tty is the only safe path. |
| 73 | + # Emitting terminalSequence here would be rejected by the Stop |
| 74 | + # hook validator as an unknown field. |
| 75 | + printf '%s' "$seq" > /dev/tty 2>/dev/null || true |
| 76 | + fi |
| 77 | + return 0 |
| 78 | + fi |
| 79 | + |
| 80 | + # Unknown Claude Code version — try /dev/tty, fall back to JSON |
| 81 | + # as a best-effort attempt for new CC without version detection. |
| 82 | + if printf '%s' "$seq" > /dev/tty 2>/dev/null; then |
| 83 | + return 0 |
| 84 | + fi |
| 85 | + jq -nc --arg seq "$seq" '{terminalSequence: $seq}' |
| 86 | +} |
0 commit comments