#!/usr/bin/env bash # UserPromptSubmit hook: перехватывает /switch-account без участия LLM input=$(cat) prompt=$(echo "$input" | jq -r '.user_prompt // empty' 2>/dev/null) # Нормализуем: убираем пробелы и слэш в начале normalized=$(echo "$prompt" | sed 's|^[[:space:]]*/||; s|[[:space:]]*$||') [ "$normalized" != "switch-account" ] && exit 0 # --- Переключаем аккаунт --- ACCOUNTS_DIR="$HOME/.claude/accounts" CREDS="$HOME/.claude/.credentials.json" CURRENT_FILE="$ACCOUNTS_DIR/current" mkdir -p "$ACCOUNTS_DIR" mapfile -t accounts < <(ls "$ACCOUNTS_DIR"/*.credentials.json 2>/dev/null \ | xargs -I{} basename {} .credentials.json | sort) if [ ${#accounts[@]} -eq 0 ]; then jq -n '{ "decision": "block", "reason": "Аккаунты не настроены. Создай ~/.claude/accounts/.credentials.json для каждого аккаунта и запиши текущий в ~/.claude/accounts/current" }' exit 0 fi current=$(cat "$CURRENT_FILE" 2>/dev/null || echo "") # Найти следующий по кругу idx=-1 for i in "${!accounts[@]}"; do [ "${accounts[$i]}" = "$current" ] && idx=$i && break done next_idx=$(( (idx + 1) % ${#accounts[@]} )) next="${accounts[$next_idx]}" cp "$ACCOUNTS_DIR/${next}.credentials.json" "$CREDS" chmod 600 "$CREDS" echo "$next" > "$CURRENT_FILE" total=${#accounts[@]} msg="Аккаунт: ${current:-?} -> ${next} (${total} аккаунтов)" jq -n --arg msg "$msg" '{"decision": "block", "reason": $msg}' exit 0