#!/bin/bash
MANAGER="http://127.0.0.1:19088"
TOKEN="dev-admin-token"
TUNNEL_URL="http://friend-4f864178.100.122.74.120.nip.io:19088/?__friend_tunnel=fctun_0c91eecdb079d77758df6ffed211e8bae16bb20f1978b23a"

echo "=== 通过 panel tunnel 注入插件配置 ==="
curl -sS -X PUT \
  -H "Content-Type: application/json" \
  -d '{"config":{"api_key":"sk-m2ZMH0W1EYSL4vrXwE0WjvQTSOKhhX4oMp31zZskZ6xDPl96","upstream_base":"http://10.200.0.20:3000","listen_addr":"127.0.0.1:39527"}}' \
  "${TUNNEL_URL}&__path=/api/plugins/llm-proxy/config" 2>/dev/null
echo ""

echo "等待配置生效..."
sleep 5

echo "=== 通过 tunnel 检查插件健康 ==="
curl -sS "${TUNNEL_URL}&__path=/api/plugins/llm-proxy/health" 2>/dev/null
echo ""

echo "=== 通过 tunnel 检查 LLM overview ==="
curl -sS "${TUNNEL_URL}&__path=/api/llm/overview" 2>/dev/null
echo ""

echo "=== 通过 tunnel 检查插件列表 ==="
curl -sS "${TUNNEL_URL}&__path=/api/plugins" 2>/dev/null | python3 -c "
import json,sys
d=json.load(sys.stdin)
if isinstance(d, list):
    for p in d:
        print(f\"  name={p.get('name','')} status={p.get('status','')} managed={p.get('managed','')}\")
else:
    print(json.dumps(d, indent=2)[:500])
" 2>/dev/null || echo "parse error"
echo ""

echo "=== 通过 tunnel 测试聊天 ==="
curl -sS -X POST \
  -H "Content-Type: application/json" \
  -d '{"model":"glm-5-2","messages":[{"role":"user","content":"hello"}],"max_tokens":50}' \
  "${TUNNEL_URL}&__path=/v1/chat/completions" 2>/dev/null | head -c 500
echo ""
