- UserPromptSubmit хук перехватывает /switch-account до LLM, переключает credentials по кругу и возвращает decision:block - нулевой расход токенов - Статусная строка: effort и имя аккаунта в квадратных скобках [high·work] - ai-setup.sh деплоит хук switch-account-hook.sh и прописывает его в settings.json - Скилл switch-account оставлен как fallback-документация для setup Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
49 lines
1.5 KiB
Bash
Executable File
49 lines
1.5 KiB
Bash
Executable File
#!/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/<name>.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
|