File size: 7,580 Bytes
dc3879e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/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())