#!/usr/bin/env python3
"""
Stability test: Run the same request multiple times to see if it's intermittent.
"""

import os
import sys
sys.path.append(os.getcwd())

from openai import OpenAI
from slack_bot.llm.gemini import get_system_instruction
from slack_bot.tools.registry import TOOLS_SCHEMA
import time

client = OpenAI(
    api_key="sk-457cbcd2e0a4467e90db1af0ae65748e",
    base_url="http://127.0.0.1:8045/v1"
)

system_prompt = get_system_instruction()
test_message = "今天睡得怎么样？"

print("=" * 80)
print("Local Proxy Stability Test")
print("=" * 80)
print(f"\nRunning the EXACT same request 10 times...")
print(f"Message: {test_message}")
print(f"System prompt: {len(system_prompt)} chars")
print(f"Tools: {len(TOOLS_SCHEMA)}")
print()

results = []

for i in range(10):
    print(f"Attempt {i+1}/10...", end=" ", flush=True)

    try:
        start = time.time()
        response = client.chat.completions.create(
            model="gemini-3-pro-high",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": test_message}
            ],
            tools=TOOLS_SCHEMA
        )
        elapsed = time.time() - start

        msg = response.choices[0].message
        finish = response.choices[0].finish_reason
        has_content = bool(msg.content and msg.content.strip())
        has_tools = bool(msg.tool_calls)

        if has_tools:
            status = "✅ TOOL_CALL"
            detail = f"{msg.tool_calls[0].function.name}"
        elif has_content:
            status = "⚠️  TEXT"
            detail = f"{len(msg.content)} chars"
        else:
            status = "❌ EMPTY"
            detail = f"finish={finish}"

        results.append({
            "attempt": i+1,
            "status": status,
            "detail": detail,
            "finish": finish,
            "elapsed": elapsed
        })

        print(f"{status} - {detail} ({elapsed:.2f}s)")

    except Exception as e:
        results.append({
            "attempt": i+1,
            "status": "❌ ERROR",
            "detail": str(e)[:50],
            "finish": "error",
            "elapsed": 0
        })
        print(f"❌ ERROR - {str(e)[:50]}")

    # Small delay between requests
    time.sleep(1)

print("\n" + "=" * 80)
print("RESULTS SUMMARY")
print("=" * 80)

success_count = sum(1 for r in results if "TOOL_CALL" in r["status"])
empty_count = sum(1 for r in results if "EMPTY" in r["status"])
text_count = sum(1 for r in results if "TEXT" in r["status"])
error_count = sum(1 for r in results if "ERROR" in r["status"])

print(f"\n✅ Successful tool calls: {success_count}/10")
print(f"❌ Empty responses:      {empty_count}/10")
print(f"⚠️  Text responses:       {text_count}/10")
print(f"❌ Errors:               {error_count}/10")

if success_count > 0 and (empty_count > 0 or error_count > 0):
    print("\n🔍 CONCLUSION: INTERMITTENT ISSUE CONFIRMED!")
    print("   The proxy is unstable - sometimes works, sometimes doesn't.")
    print("\n   Possible causes:")
    print("   1. Rate limiting or throttling")
    print("   2. Backend health issues (upstream Gemini connection)")
    print("   3. Resource constraints (CPU/memory)")
    print("   4. Random timeout/retry logic")
    print("   5. Load balancing between multiple backends")
elif success_count == 10:
    print("\n✅ STABLE: All requests succeeded!")
    print("   The proxy might have recovered or was temporarily down.")
elif empty_count == 10:
    print("\n❌ CONSISTENTLY FAILING")
    print("   The issue is reproducible and persistent.")
else:
    print(f"\n⚠️  MIXED RESULTS: {success_count} success, {empty_count} empty, {text_count} text")

print("\n" + "=" * 80)
