#!/usr/bin/env python3
"""Quick Kimi K2.5 API connectivity test via OpenRouter."""
import urllib.request
import json
import ssl

OPENROUTER_KEY = "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"

payload = json.dumps({
    "model": "moonshotai/kimi-k2.5",
    "messages": [{"role": "user", "content": "Reply with exactly: KIMI_ONLINE"}],
    "temperature": 1.0,
    "max_tokens": 50
}).encode("utf-8")

req = urllib.request.Request(
    "https://openrouter.ai/api/v1/chat/completions",
    data=payload,
    headers={
        "Authorization": f"Bearer {OPENROUTER_KEY}",
        "Content-Type": "application/json",
        "HTTP-Referer": "https://sunaivadigital.com",
        "X-Title": "Genesis Swarm"
    },
    method="POST"
)

ctx = ssl.create_default_context()
try:
    with urllib.request.urlopen(req, context=ctx, timeout=60) as resp:
        data = json.loads(resp.read())
        print("STATUS: SUCCESS")
        print("RESPONSE:", data["choices"][0]["message"]["content"])
        print("MODEL:", data.get("model", "?"))
        print("TOKENS:", data.get("usage", {}))
except urllib.error.HTTPError as e:
    body = e.read().decode("utf-8", errors="replace")
    print(f"HTTP ERROR {e.code}: {body}")
except Exception as e:
    print(f"ERROR: {type(e).__name__}: {e}")
