#!/usr/bin/env python3 """Validate the OpenRouter setup in the backend.""" import json import os import sys from pathlib import Path from dotenv import load_dotenv # Load environment variables load_dotenv() def check_env_variables(): """Check that required environment variables are set.""" print("=" * 60) print("Checking Environment Variables") print("=" * 60) required = ["OPENROUTER_API_KEY", "HF_TOKEN"] optional = ["OPENROUTER_MODEL", "OPENROUTER_REFERER", "OPENROUTER_APP_TITLE"] all_good = True for var in required: value = os.environ.get(var) if value: masked = value[:20] + "..." if len(value) > 20 else value print(f"✅ {var}: {masked}") else: print(f"❌ {var}: NOT SET (Required)") all_good = False for var in optional: value = os.environ.get(var) if value: print(f"✅ {var}: {value}") else: print(f"⚠️ {var}: Not set (Optional)") return all_good def check_config_file(): """Check that the config file is properly configured.""" print("\n" + "=" * 60) print("Checking Config File") print("=" * 60) config_path = Path(__file__).parent / "configs" / "main_agent_config.json" if not config_path.exists(): print(f"❌ Config file not found: {config_path}") return False try: with open(config_path) as f: config = json.load(f) print(f"✅ Config file loaded: {config_path}") # Check key fields checks = [ ("model_name", config.get("model_name")), ("openrouter_enabled", config.get("openrouter_enabled")), ("openrouter_model", config.get("openrouter_model")), ] for key, value in checks: if value: print(f"✅ {key}: {value}") else: print(f"⚠️ {key}: Not set") # Verify OpenRouter is enabled if config.get("openrouter_enabled"): print("\n✅ OpenRouter is ENABLED in config") else: print("\n⚠️ OpenRouter is NOT enabled in config") return True except json.JSONDecodeError as e: print(f"❌ Invalid JSON in config file: {e}") return False except Exception as e: print(f"❌ Error reading config: {e}") return False def check_agent_loop(): """Check that agent_loop.py has the OpenRouter fixes.""" print("\n" + "=" * 60) print("Checking Agent Loop (OpenRouter Integration)") print("=" * 60) agent_loop_path = Path(__file__).parent / "agent" / "core" / "agent_loop.py" if not agent_loop_path.exists(): print(f"❌ agent_loop.py not found: {agent_loop_path}") return False with open(agent_loop_path) as f: content = f.read() checks = [ ("api_base_to_use", "api_base parameter"), ("extra_headers", "extra_headers parameter"), ("https://openrouter.ai/api/v1", "OpenRouter API base URL"), ("HTTP-Referer", "HTTP-Referer header"), ("X-Title", "X-Title header"), ] all_good = True for keyword, description in checks: if keyword in content: print(f"✅ {description}: Found") else: print(f"❌ {description}: NOT found") all_good = False return all_good def check_routes(): """Check that routes/agent.py has the OpenRouter fixes.""" print("\n" + "=" * 60) print("Checking Routes (OpenRouter Integration)") print("=" * 60) routes_path = Path(__file__).parent / "routes" / "agent.py" if not routes_path.exists(): print(f"❌ agent.py not found: {routes_path}") return False with open(routes_path) as f: content = f.read() checks = [ ("llm_health_check", "Health check endpoint"), ("generate_title", "Title generation endpoint"), ("api_base", "api_base in routes"), ("openrouter_enabled", "openrouter_enabled check"), ] all_good = True for keyword, description in checks: if keyword in content: print(f"✅ {description}: Found") else: print(f"❌ {description}: NOT found") all_good = False return all_good def main(): """Run all validation checks.""" print("=" * 60) print("OpenRouter Setup Validation") print("=" * 60) results = [] results.append(("Environment Variables", check_env_variables())) results.append(("Config File", check_config_file())) results.append(("Agent Loop", check_agent_loop())) results.append(("Routes", check_routes())) print("\n" + "=" * 60) print("Validation Summary") print("=" * 60) for name, passed in results: status = "✅ PASS" if passed else "❌ FAIL" print(f"{name}: {status}") all_passed = all(r[1] for r in results) if all_passed: print("\n🎉 All validation checks passed!") print("The backend is configured correctly for OpenRouter.") return 0 else: print("\n⚠️ Some validation checks failed.") print("Please review the errors above and fix the issues.") return 1 if __name__ == "__main__": sys.exit(main())