#!/usr/bin/env python3 """Integration validation script for AI chatbot. This script validates that all components of the AI chatbot are properly configured and integrated. [From]: Phase III Integration Testing Usage: python scripts/validate_chat_integration.py """ import sys import os from pathlib import Path # Add backend directory to path backend_dir = Path(__file__).parent.parent sys.path.insert(0, str(backend_dir)) def check_environment(): """Check if required environment variables are set.""" print("\nšŸ” Checking environment variables...") from core.config import get_settings try: settings = get_settings() # Check database URL if not settings.database_url: print("āŒ DATABASE_URL not set") return False print(f"āœ… DATABASE_URL: {settings.database_url[:20]}...") # Check Gemini API key (optional for testing, required for production) if not settings.gemini_api_key: print("āš ļø GEMINI_API_KEY not set (required for AI chatbot)") print(" Get your API key from: https://aistudio.google.com") else: print(f"āœ… GEMINI_API_KEY: {settings.gemini_api_key[:10]}...") # Check frontend URL if not settings.frontend_url: print("āš ļø FRONTEND_URL not set") else: print(f"āœ… FRONTEND_URL: {settings.frontend_url}") return True except Exception as e: print(f"āŒ Error loading settings: {e}") return False def check_database(): """Check database connection and schema.""" print("\nšŸ” Checking database...") from sqlmodel import select, Session from core.database import engine from models.task import Task from models.conversation import Conversation from models.message import Message try: with Session(engine) as session: # Check if conversation table exists try: session.exec(select(Conversation).limit(1)) print("āœ… Conversation table exists") except Exception as e: print(f"āŒ Conversation table error: {e}") return False # Check if message table exists try: session.exec(select(Message).limit(1)) print("āœ… Message table exists") except Exception as e: print(f"āŒ Message table error: {e}") return False # Check if task table exists try: session.exec(select(Task).limit(1)) print("āœ… Task table exists") except Exception as e: print(f"āŒ Task table error: {e}") return False return True except Exception as e: print(f"āŒ Database connection failed: {e}") return False def check_mcp_tools(): """Check if MCP tools are registered.""" print("\nšŸ” Checking MCP tools...") try: from mcp_server.tools import add_task, list_tasks # Check add_task tool if hasattr(add_task, 'tool_metadata'): print(f"āœ… add_task tool: {add_task.tool_metadata['name']}") else: print("āŒ add_task tool metadata not found") return False # Check list_tasks tool if hasattr(list_tasks, 'tool_metadata'): print(f"āœ… list_tasks tool: {list_tasks.tool_metadata['name']}") else: print("āŒ list_tasks tool metadata not found") return False return True except Exception as e: print(f"āŒ MCP tools check failed: {e}") return False def check_ai_agent(): """Check if AI agent is configured.""" print("\nšŸ” Checking AI agent...") try: from ai_agent import is_gemini_configured, get_task_agent # Check if Gemini is configured if is_gemini_configured(): print("āœ… Gemini API is configured") else: print("āš ļø Gemini API not configured (required for AI functionality)") # Try to get the agent (won't connect to API, just initializes) try: agent = get_task_agent() print(f"āœ… AI agent initialized: {agent.name}") except ValueError as e: print(f"āš ļø AI agent not initialized: {e}") return True except Exception as e: print(f"āŒ AI agent check failed: {e}") return False def check_api_routes(): """Check if chat API routes are registered.""" print("\nšŸ” Checking API routes...") try: from main import app # Get all routes routes = [route.path for route in app.routes] # Check for chat endpoint chat_routes = [r for r in routes if '/chat' in r] if chat_routes: print(f"āœ… Chat routes found: {len(chat_routes)}") for route in chat_routes: print(f" - {route}") else: print("āŒ No chat routes found") return False return True except Exception as e: print(f"āŒ API routes check failed: {e}") return False def check_dependencies(): """Check if required dependencies are installed.""" print("\nšŸ” Checking dependencies...") required_packages = [ ('fastapi', 'FastAPI'), ('agents', 'OpenAI Agents SDK'), ('openai', 'OpenAI SDK'), ('sqlmodel', 'SQLModel'), ('pydantic_settings', 'Pydantic Settings'), ] all_ok = True for package, name in required_packages: try: __import__(package) print(f"āœ… {name}") except ImportError: print(f"āŒ {name} not installed") all_ok = False return all_ok def main(): """Run all validation checks.""" print("=" * 60) print("AI Chatbot Integration Validation") print("=" * 60) checks = [ ("Dependencies", check_dependencies), ("Environment", check_environment), ("Database", check_database), ("MCP Tools", check_mcp_tools), ("AI Agent", check_ai_agent), ("API Routes", check_api_routes), ] results = [] for name, check_func in checks: try: result = check_func() results.append((name, result)) except Exception as e: print(f"\nāŒ {name} check failed with exception: {e}") results.append((name, False)) # Print summary print("\n" + "=" * 60) print("SUMMARY") print("=" * 60) all_passed = True for name, result in results: status = "āœ… PASS" if result else "āŒ FAIL" print(f"{name:20} {status}") if not result: all_passed = False print("=" * 60) if all_passed: print("\nšŸŽ‰ All checks passed! The AI chatbot is ready for integration.") print("\nNext steps:") print("1. Start the backend server: uv run python main.py") print("2. Test the chat endpoint: http://localhost:8000/docs") print("3. Access the frontend chat page: http://localhost:3000/chat") else: print("\nāš ļø Some checks failed. Please fix the issues above.") print("\nCommon fixes:") print("1. Set GEMINI_API_KEY in .env file") print("2. Run database migrations: python backend/migrations/run_migration.py") print("3. Install dependencies: uv sync") return 0 if all_passed else 1 if __name__ == "__main__": sys.exit(main())