#!/bin/bash
# Switch le modèle de Hermes via LiteLLM (hermes-default alias)
# Usage: hermes-switch [qwen|claude|status]

CONFIG=/etc/litellm/config.yaml

qwen_block='    litellm_params:\n      model: openai/qwen2.5-7b-instruct\n      api_base: http://192.168.10.20:1234/v1\n      api_key: lm-studio'
claude_block='    litellm_params:\n      model: anthropic/claude-sonnet-4-6\n      api_key: os.environ/ANTHROPIC_API_KEY'

current() {
    grep -A3 "model_name: hermes-default" "$CONFIG" | grep "model:" | awk '{print $2}'
}

status() {
    local cur=$(current)
    echo "hermes-default → $cur"
}

switch_qwen() {
    python3 - "$CONFIG" <<'EOF'
import sys, re
path = sys.argv[1]
text = open(path).read()
block = """  - model_name: hermes-default
    litellm_params:
      model: openai/qwen3-8b
      api_base: http://192.168.10.20:1234/v1
      api_key: lm-studio"""
text = re.sub(
    r'  - model_name: hermes-default\n    litellm_params:.*?(?=\n  -|\nlitellm_settings)',
    block + '\n',
    text, flags=re.DOTALL
)
open(path, 'w').write(text)
print("OK")
EOF
}

switch_claude() {
    python3 - "$CONFIG" <<'EOF'
import sys, re
path = sys.argv[1]
text = open(path).read()
block = """  - model_name: hermes-default
    litellm_params:
      model: anthropic/claude-sonnet-4-6
      api_key: os.environ/ANTHROPIC_API_KEY"""
text = re.sub(
    r'  - model_name: hermes-default\n    litellm_params:.*?(?=\n  -|\nlitellm_settings)',
    block + '\n',
    text, flags=re.DOTALL
)
open(path, 'w').write(text)
print("OK")
EOF
}

case "${1:-status}" in
    qwen)
        switch_qwen
        systemctl restart litellm
        echo "Hermes → Qwen3-8B (gpu-01)"
        ;;
    claude)
        switch_claude
        systemctl restart litellm
        echo "Hermes → Claude Sonnet 4.6 (API Anthropic)"
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: hermes-switch [qwen|claude|status]"
        exit 1
        ;;
esac
