#!/bin/bash
INST="friend-0f02c9c6"
TUN=$(curl -sS -X POST -H "Authorization: Bearer dev-admin-token" -H "Content-Type: application/json" \
  -d '{"instance_id":"'$INST'","ttl_seconds":600}' \
  "http://127.0.0.1:19088/api/v1/instances/$INST/tunnels")
TK=$(echo "$TUN" | python3 -c "import sys,json,re;u=json.load(sys.stdin).get('url','');m=re.search(r'fctun_[a-f0-9]+',u);print(m.group(0) if m else '')")
BASE="http://127.0.0.1:19088/panel/$INST"

echo "=== llm-proxy 插件配置 ==="
curl -sS -m 10 -H "Cookie: friend_cluster_tunnel=$TK" "$BASE/api/plugins/llm-proxy/config" | head -c 500
echo
echo "=== 渠道模型明细 ==="
curl -sS -m 10 -H "Cookie: friend_cluster_tunnel=$TK" "$BASE/api/llm/channels/1/models" | python3 -c "
import sys,json
try:
    for m in json.load(sys.stdin):
        print(m.get('id'), m.get('model_key'), '| enabled:', m.get('enabled'))
except Exception as e: print('err:', e)
"
echo "=== 推送 llm-proxy 上游配置 ==="
curl -sS -m 10 -X PUT -H "Cookie: friend_cluster_tunnel=$TK" -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"}}' \
  "$BASE/api/plugins/llm-proxy/config" | head -c 200
echo
sleep 3
echo "=== 添加三个新模型 ==="
add_model() {
  curl -sS -m 15 -X POST -H "Cookie: friend_cluster_tunnel=$TK" -H "Content-Type: application/json" \
    -d '{"model_key":"'$1'","upstream_model_id":"'$1'","display_name":"'$2'","enabled":true}' \
    "$BASE/api/llm/channels/1/models" | head -c 120
  echo
}
add_model "mistral-medium-latest" "mistral-medium-latest"
add_model "nvidia/nemotron-3-super-120b-a12b:free" "nemotron-3-super-120b-a12b"
add_model "openrouter/free" "openrouter-free"
echo "$TK" > /tmp/baseline_tunnel_tk.txt
