set -euo pipefail
KEY="sk-a2003052b2ac0fcf02b6140a4ff44be9617c72a63159efc5057ba071424288a3"
BASE="https://www.codebridge.cc"

printf "== models ==\n"
curl -sS -o /tmp/t48_models.json -w "status=%{http_code} total=%{time_total}\n" \
  "$BASE/v1/models" \
  -H "Authorization: Bearer $KEY" \
  -H "Content-Type: application/json"
python3 - <<PY
import json
p=/tmp/t48_models.json
with open(p,r) as f:
    data=json.load(f)
ids=[x.get(id) for x in data.get(data,[])[:12]]
print(sample_models=, ids)
PY

printf "\n== responses gpt-4o-mini x5 ==\n"
cat >/tmp/t48_resp.json <<JSON
{"model":"gpt-4o-mini","input":[{"role":"user","content":[{"type":"input_text","text":"reply with OK only"}]}],"max_output_tokens":16}
JSON
for i in 1 2 3 4 5; do
  code=$(curl -sS -o /tmp/t48_resp_out.json -w "%{http_code}" \
    "$BASE/v1/responses" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    --data-binary @/tmp/t48_resp.json)
  echo "responses[$i] status=$code body=$(python3 - <<PY
import json
p=/tmp/t48_resp_out.json
try:
    with open(p,r) as f:
        data=json.load(f)
    if isinstance(data,dict):
        if error in data:
            print(str(data[error])[:220])
        elif detail in data:
            print(str(data[detail])[:220])
        elif output in data:
            print(has_output)
        else:
            print(str(data)[:220])
    else:
        print(str(data)[:220])
except Exception as e:
    with open(p,r,errors=ignore) as f:
        print(f.read()[:220])
PY
)"
  sleep 1
done

printf "\n== chat.completions gpt-4o-mini x5 ==\n"
cat >/tmp/t48_chat.json <<JSON
{"model":"gpt-4o-mini","messages":[{"role":"user","content":"reply with OK only"}],"max_tokens":16}
JSON
for i in 1 2 3 4 5; do
  code=$(curl -sS -o /tmp/t48_chat_out.json -w "%{http_code}" \
    "$BASE/v1/chat/completions" \
    -H "Authorization: Bearer $KEY" \
    -H "Content-Type: application/json" \
    --data-binary @/tmp/t48_chat.json)
  echo "chat[$i] status=$code body=$(python3 - <<PY
import json
p=/tmp/t48_chat_out.json
try:
    with open(p,r) as f:
        data=json.load(f)
    if isinstance(data,dict):
        if error in data:
            print(str(data[error])[:220])
        elif detail in data:
            print(str(data[detail])[:220])
        elif choices in data:
            ch=data[choices][0]
            msg=ch.get(message,{}) if isinstance(ch,dict) else {}
            print((msg.get(content) or choices_ok)[:220])
        else:
            print(str(data)[:220])
    else:
        print(str(data)[:220])
except Exception:
    with open(p,r,errors=ignore) as f:
        print(f.read()[:220])
PY
)"
  sleep 1
done
