feat: поддержка API-ключей в add/switch-account, не только Claude.ai
Хуки add-account и switch-account теперь ветвятся по AI_LAUNCHER: claude - циклический обход сохранённых Claude.ai аккаунтов, kimi - добавление и переключение API-ключей по кругу. Skills обновлены под "account or API key". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
#!/usr/bin/env bash
|
||||
# UserPromptSubmit hook: перехватывает /switch-account и циклически меняет аккаунт.
|
||||
# UserPromptSubmit hook: перехватывает /switch-account и циклически меняет аккаунт/ключ.
|
||||
# Текущий аккаунт определяется ПО ТОКЕНУ в .credentials.json (account-email.sh),
|
||||
# а не по claude auth status — он читает рассинхронизированный oauthAccount.
|
||||
# На Linux Claude Code перечитывает .credentials.json на лету: новый аккаунт
|
||||
@@ -13,42 +13,86 @@ 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"
|
||||
EMAIL_HELPER="$HOME/.claude/hooks/account-email.sh"
|
||||
LAUNCHER="${AI_LAUNCHER:-claude}"
|
||||
|
||||
mkdir -p "$ACCOUNTS_DIR"
|
||||
case "$LAUNCHER" in
|
||||
claude)
|
||||
ACCOUNTS_DIR="$HOME/.claude/accounts"
|
||||
CREDS="$HOME/.claude/.credentials.json"
|
||||
CURRENT_FILE="$ACCOUNTS_DIR/current"
|
||||
EMAIL_HELPER="$HOME/.claude/hooks/account-email.sh"
|
||||
|
||||
# Реальный текущий аккаунт — по токену активной сессии (не по хрупкому current)
|
||||
current=$(bash "$EMAIL_HELPER" "$CREDS" 2>/dev/null)
|
||||
[ -z "$current" ] && current=$(cat "$CURRENT_FILE" 2>/dev/null)
|
||||
mkdir -p "$ACCOUNTS_DIR"
|
||||
|
||||
# Сохранить актуальные (возможно обновлённые рефрешем) токены под реальным email.
|
||||
# current выведен из самого токена — порча файла другого аккаунта исключена.
|
||||
if [ -n "$current" ] && [ -f "$CREDS" ]; then
|
||||
cp "$CREDS" "$ACCOUNTS_DIR/${current}.credentials.json"
|
||||
chmod 600 "$ACCOUNTS_DIR/${current}.credentials.json"
|
||||
fi
|
||||
# Реальный текущий аккаунт — по токену активной сессии (не по хрупкому current)
|
||||
current=$(bash "$EMAIL_HELPER" "$CREDS" 2>/dev/null)
|
||||
[ -z "$current" ] && current=$(cat "$CURRENT_FILE" 2>/dev/null)
|
||||
|
||||
mapfile -t accounts < <(ls "$ACCOUNTS_DIR"/*.credentials.json 2>/dev/null \
|
||||
| xargs -I{} basename {} .credentials.json | sort)
|
||||
# Сохранить актуальные (возможно обновлённые рефрешем) токены под реальным email.
|
||||
# current выведен из самого токена — порча файла другого аккаунта исключена.
|
||||
if [ -n "$current" ] && [ -f "$CREDS" ]; then
|
||||
cp "$CREDS" "$ACCOUNTS_DIR/${current}.credentials.json"
|
||||
chmod 600 "$ACCOUNTS_DIR/${current}.credentials.json"
|
||||
fi
|
||||
|
||||
if [ ${#accounts[@]} -le 1 ]; then
|
||||
echo "Только один аккаунт (${current:-нет}). Добавь второй через /add-account." >&2
|
||||
exit 2
|
||||
fi
|
||||
mapfile -t accounts < <(ls "$ACCOUNTS_DIR"/*.credentials.json 2>/dev/null \
|
||||
| xargs -I{} basename {} .credentials.json | sort)
|
||||
|
||||
# Найти следующий по кругу
|
||||
idx=-1
|
||||
for i in "${!accounts[@]}"; do
|
||||
[ "${accounts[$i]}" = "$current" ] && idx=$i && break
|
||||
done
|
||||
next_idx=$(( (idx + 1) % ${#accounts[@]} ))
|
||||
next="${accounts[$next_idx]}"
|
||||
if [ ${#accounts[@]} -le 1 ]; then
|
||||
echo "Только один аккаунт (${current:-нет}). Добавь второй через /add-account." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
cp "$ACCOUNTS_DIR/${next}.credentials.json" "$CREDS"
|
||||
chmod 600 "$CREDS"
|
||||
echo "$next" > "$CURRENT_FILE"
|
||||
# Найти следующий по кругу
|
||||
idx=-1
|
||||
for i in "${!accounts[@]}"; do
|
||||
[ "${accounts[$i]}" = "$current" ] && idx=$i && break
|
||||
done
|
||||
next_idx=$(( (idx + 1) % ${#accounts[@]} ))
|
||||
next="${accounts[$next_idx]}"
|
||||
|
||||
exit 0
|
||||
cp "$ACCOUNTS_DIR/${next}.credentials.json" "$CREDS"
|
||||
chmod 600 "$CREDS"
|
||||
echo "$next" > "$CURRENT_FILE"
|
||||
|
||||
exit 0
|
||||
;;
|
||||
|
||||
kimi)
|
||||
KEYS_DIR="$HOME/.config/ai-setup/kimi_keys"
|
||||
CURRENT_FILE="$KEYS_DIR/current"
|
||||
|
||||
mkdir -p "$KEYS_DIR"
|
||||
|
||||
current=$(cat "$CURRENT_FILE" 2>/dev/null || true)
|
||||
|
||||
# Если current указывает в никуда, но есть ключи — сбросить на первый попавшийся.
|
||||
if [ -n "$current" ] && [ ! -f "$KEYS_DIR/${current}.key" ]; then
|
||||
current=""
|
||||
fi
|
||||
|
||||
mapfile -t keys < <(ls "$KEYS_DIR"/*.key 2>/dev/null \
|
||||
| xargs -I{} basename {} .key | sort)
|
||||
|
||||
if [ ${#keys[@]} -le 1 ]; then
|
||||
echo "Только один Kimi ключ (${current:-нет}). Добавь второй через /add-account." >&2
|
||||
exit 2
|
||||
fi
|
||||
|
||||
idx=-1
|
||||
for i in "${!keys[@]}"; do
|
||||
[ "${keys[$i]}" = "$current" ] && idx=$i && break
|
||||
done
|
||||
next_idx=$(( (idx + 1) % ${#keys[@]} ))
|
||||
next="${keys[$next_idx]}"
|
||||
|
||||
echo "$next" > "$CURRENT_FILE"
|
||||
echo "Kimi ключ переключён на: $next. ai-kimi перезапустится с новым ключом." >&2
|
||||
|
||||
exit 0
|
||||
;;
|
||||
|
||||
*)
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
Reference in New Issue
Block a user