#!/bin/bash
# M3 验证 v4：诊断 LLM 状态并尝试修复
BASE=http://10.168.3.36:19088
C=friend-deploy-0acc642d-5c1
INST=friend-48beca9b

ITOK=$(docker exec $C sh -c "grep api_auth_token /app/config/config.yaml" | awk '{print $2}')
TTOK=$(curl -sS --max-time 10 -X POST -H "Authorization: Bearer dev-admin-token" -H "Content-Type: application/json" -d '{}' "$BASE/api/v1/instances/$INST/tunnels" | python3 -c "
import sys,json,urllib.parse as u
print(u.parse_qs(u.urlparse(json.load(sys.stdin)['url']).query)['__friend_tunnel'][0])")

api() { curl -sS --max-time 15 -H "Cookie: friend_cluster_tunnel=$TTOK" -H "Authorization: Bearer $ITOK" "$BASE/panel/$INST$1"; }

echo "=== 1. LLM overview ==="
api /api/llm/overview; echo

echo "=== 2. LLM channels ==="
api /api/llm/channels; echo

echo "=== 3. LLM models ==="
api /api/llm/models; echo

echo "=== 4. LLM routes ==="
api /api/llm/routes; echo

echo "=== 5. 容器内 LLM proxy 插件配置 ==="
docker exec $C sh -c 'cat /var/lib/friend/plugins/llm-proxy/manifest.json 2>/dev/null | head -20'
echo

echo "=== 6. 容器内 config.yaml LLM 相关 ==="
docker exec $C sh -c 'grep -iA5 "llm\|brain\|model" /app/config/config.yaml 2>/dev/null | head -40'

echo "=== 7. 容器内 friend.log LLM 错误详情 ==="
docker exec $C sh -c 'grep -i "llm\|model\|channel\|route\|brain.think" /var/lib/friend/logs/friend.log' 2>/dev/null | tail -20

echo DONE
