#!/usr/bin/env python3
import subprocess, json

def run(cmd):
    return subprocess.run(cmd, capture_output=True, text=True)

TK = open('/tmp/baseline_tunnel_tk.txt').read().strip()
BASE = "http://127.0.0.1:19088/panel/friend-0f02c9c6"
H = ["-H", "Cookie: friend_cluster_tunnel=" + TK, "-H", "Content-Type: application/json"]

M_MISTRAL=3; M_NEMO=4; M_ORF=5
def cand(mid, mk):
    return {"target_type":"channel","channel_id":1,"channel_model_id":mid,"model_key":mk}

BRAIN=[cand(M_NEMO,"nvidia/nemotron-3-super-120b-a12b:free"), cand(M_ORF,"openrouter/free")]
MULTIMODAL=[cand(M_MISTRAL,"mistral-medium-latest"), cand(M_ORF,"openrouter/free")]
DEFAULT=[cand(M_MISTRAL,"mistral-medium-latest"), cand(M_NEMO,"nvidia/nemotron-3-super-120b-a12b:free"), cand(M_ORF,"openrouter/free")]

OTHER=["memory_narrative","memory_generator","memory_episode_extractor","memory_cognition_extractor","recall_memory_summary","auto_recall_query","auto_recall_refine","mind_weave_monitor","context_compression","importance_assessment","memory_cognition_dedup","memory_step_experience_extractor","memory_task_experience_summarizer","memory_operation_detail_learner","tool_param_validator","quick"]

routes={"brain_thinking":BRAIN,"tool_multimodal":MULTIMODAL,"default":DEFAULT}
for p in OTHER: routes[p]=BRAIN

ok=fail=0
for purpose,cands in routes.items():
    body=json.dumps({"use_streaming":False,"max_attempts":8,"total_budget_seconds":0,"all_cooling_policy":"fail_fast","enabled":True,"candidates":cands})
    r = run(["curl","-sS","-m","15","-X","PUT"]+H+["-d",body,f"{BASE}/api/llm/routes/{purpose}"])
    out=r.stdout.strip()
    if '"error"' in out or not out:
        fail+=1; print("FAIL",purpose,(out or r.stderr)[:150])
    else:
        ok+=1; print("OK",purpose)
print("done ok=%d fail=%d"%(ok,fail))

# probe 新模型
for mid in [3,4,5]:
    r = run(["curl","-sS","-m","30","-X","POST","-H","Cookie: friend_cluster_tunnel="+TK,f"{BASE}/api/llm/models/{mid}/probe"])
    print("probe",mid,"->",r.stdout.strip()[:120])

# 健康 + 路由复查
r = run(["curl","-sS","-m","10","-H","Cookie: friend_cluster_tunnel="+TK,f"{BASE}/api/llm/health"])
try:
    d=json.loads(r.stdout)
    items = d if isinstance(d,list) else d.get("models") or []
    for m in items:
        st=m.get("model_state")
        if isinstance(st,dict): st=st.get("state")
        print("health:",m.get("model_key"),"|",st)
except Exception as e: print("health err:",e,r.stdout[:200])
