#!/bin/bash
export LC_ALL=C
echo "===== EMBEDDING LATENCY (first call loads the model) ====="
for i in 1 2 3 4 5; do
  printf '  run %s: ' "$i"
  curl -s -o /tmp/emb.json -w '%{http_code} total=%{time_total}s\n' --max-time 60 \
    -X POST http://172.18.0.1:11434/api/embeddings \
    -H 'Content-Type: application/json' \
    -d '{"model":"bge-m3:latest","prompt":"شهرداری ساری کجاست؟"}' 2>/dev/null
done
printf '  vector dim: '; python3 -c "import json;print(len(json.load(open('/tmp/emb.json'))['embedding']))" 2>/dev/null

echo
echo "===== FROM N8N CONTAINER (the real path) ====="
docker exec n8n node -e '
const t0=Date.now();
fetch("http://172.18.0.1:11434/api/embeddings",{method:"POST",headers:{"Content-Type":"application/json"},
  body:JSON.stringify({model:"bge-m3:latest",prompt:"شهرداری ساری کجاست؟"})})
 .then(r=>r.json()).then(j=>console.log(`  ok dim=${j.embedding.length} in ${Date.now()-t0}ms`))
 .catch(e=>console.log("  error",e.message));' 2>&1 | head -3

echo
echo "===== KEEP-ALIVE / LOADED MODELS ====="
curl -s --max-time 6 http://172.18.0.1:11434/api/ps | head -c 300; echo

echo
echo "===== END-TO-END: 9ROUTER INTENT MODEL ====="
DB=/root/.9router/db/data.sqlite
KEY=$(python3 - "$DB" <<'PY'
import sqlite3,sys
c=sqlite3.connect(sys.argv[1])
cols=[r[1] for r in c.execute("pragma table_info(apiKeys)")]
print(c.execute("select * from apiKeys limit 1").fetchone()[cols.index('key')])
PY
)
for m in gemini/gemini-3.5-flash-lite mimo-code; do
  printf '  %-32s ' "$m"
  curl -s -o /dev/null -w '%{http_code} %{time_total}s\n' --max-time 40 -X POST \
    http://127.0.0.1:20128/v1/chat/completions -H "Authorization: Bearer $KEY" \
    -H 'Content-Type: application/json' \
    -d "{\"model\":\"$m\",\"messages\":[{\"role\":\"user\",\"content\":\"سلام، یک جمله کوتاه\"}],\"max_tokens\":40}" 2>/dev/null
done
