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

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

C = run(['bash','-c','docker ps --format "{{.Names}}" | grep friend-deploy | head -1']).stdout.strip()
FT = run(['bash','-c',"docker exec postgres_db psql -U knowledge -d yunwoai -t -A -c \"SELECT friend_token FROM user_friend_agents WHERE instance_id='friend-abc1d454';\""]).stdout.strip()
print("container:", C, "ft_len:", len(FT))

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})
    inner = "wget -qO /tmp/r.out --header=\"Authorization: Bearer %s\" --header=\"Content-Type: application/json\" --body-data='%s' --method=PUT http://127.0.0.1:8000/api/llm/routes/%s 2>/dev/null; cat /tmp/r.out" % (FT, body, purpose)
    r = run(["docker","exec",C,"sh","-c",inner])
    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))
