File size: 24,758 Bytes
9b006e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
from fastapi import FastAPI, HTTPException, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse, JSONResponse
from pydantic import BaseModel
import asyncio
import json
from datetime import datetime
from typing import List, Dict, Any, Optional
import os
from dotenv import load_dotenv

load_dotenv()

from src.agent.research_agent import Web3ResearchAgent
from src.api.airaa_integration import AIRAAIntegration  
from src.utils.logger import get_logger
from src.utils.config import config

logger = get_logger(__name__)

app = FastAPI(
    title="Web3 Research Co-Pilot",
    description="AI-powered cryptocurrency research assistant",
    version="1.0.0"
)

# Pydantic models for request/response
class QueryRequest(BaseModel):
    query: str
    chat_history: Optional[List[Dict[str, str]]] = []

class QueryResponse(BaseModel):
    success: bool
    response: str
    sources: Optional[List[str]] = []
    metadata: Optional[Dict[str, Any]] = {}
    error: Optional[str] = None

class Web3CoPilotService:
    def __init__(self):
        try:
            logger.info("πŸš€ Initializing Web3CoPilotService...")
            logger.info(f"πŸ“‹ GEMINI_API_KEY configured: {'Yes' if config.GEMINI_API_KEY else 'No'}")
            
            if config.GEMINI_API_KEY:
                logger.info("πŸ€– Initializing AI agent...")
                self.agent = Web3ResearchAgent()
                logger.info("βœ… AI agent initialized successfully")
            else:
                logger.warning("⚠️ GEMINI_API_KEY not found - AI features disabled")
                self.agent = None
            
            logger.info("πŸ”— Initializing AIRAA integration...")
            self.airaa = AIRAAIntegration()
            logger.info(f"πŸ”— AIRAA integration: {'Enabled' if self.airaa.enabled else 'Disabled'}")
            
            self.enabled = bool(config.GEMINI_API_KEY)
            logger.info(f"🎯 Web3CoPilotService initialized successfully (AI enabled: {self.enabled})")
            
        except Exception as e:
            logger.error(f"❌ Service initialization failed: {e}")
            self.agent = None
            self.airaa = None
            self.enabled = False
    
    async def process_query(self, query: str) -> QueryResponse:
        logger.info(f"πŸ” Processing query: {query[:50]}{'...' if len(query) > 50 else ''}")
        
        if not query.strip():
            logger.warning("⚠️ Empty query received")
            return QueryResponse(success=False, response="Please enter a query.", error="Empty query")
        
        try:
            if not self.enabled:
                logger.info("πŸ”§ AI disabled - providing limited response")
                response = """⚠️ **AI Agent Disabled**: GEMINI_API_KEY not configured.

**Limited Data Available:**
- CoinGecko API (basic crypto data)
- DeFiLlama API (DeFi protocols) 
- Etherscan API (gas prices)

Please configure GEMINI_API_KEY for full AI analysis."""
                return QueryResponse(success=True, response=response, sources=["Configuration"])
            
            logger.info("πŸ€– Sending query to AI agent...")
            result = await self.agent.research_query(query)
            logger.info(f"βœ… AI agent responded: {result.get('success', False)}")
            
            if result.get("success"):
                response = result.get("result", "No response generated")
                sources = result.get("sources", [])
                metadata = result.get("metadata", {})
                
                # Send to AIRAA if enabled
                if self.airaa and self.airaa.enabled:
                    try:
                        logger.info("πŸ”— Sending data to AIRAA...")
                        await self.airaa.send_research_data(query, response)
                        logger.info("βœ… Data sent to AIRAA successfully")
                    except Exception as e:
                        logger.warning(f"⚠️ AIRAA integration failed: {e}")
                
                logger.info("βœ… Query processed successfully")
                return QueryResponse(success=True, response=response, sources=sources, metadata=metadata)
            else:
                error_msg = result.get("error", "Research failed. Please try again.")
                logger.error(f"❌ AI agent failed: {error_msg}")
                return QueryResponse(success=False, response=error_msg, error=error_msg)
            
        except Exception as e:
            logger.error(f"❌ Query processing error: {e}")
            error_msg = f"Error processing query: {str(e)}"
            return QueryResponse(success=False, response=error_msg, error=error_msg)

# Initialize service
logger.info("πŸš€ Starting Web3 Research Co-Pilot...")
service = Web3CoPilotService()

# API Routes
@app.get("/", response_class=HTMLResponse)
async def get_homepage(request: Request):
    logger.info("πŸ“„ Serving homepage")
    html_content = """
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Web3 Research Co-Pilot</title>
        <link rel="icon" href="data:image/svg+xml,<svg xmlns=%22http://www.w3.org/2000/svg%22 viewBox=%220 0 100 100%22><text y=%22.9em%22 font-size=%2290%22>πŸš€</text></svg>">
        <style>
            * { margin: 0; padding: 0; box-sizing: border-box; }
            body { 
                font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif; 
                background: linear-gradient(135deg, #0f1419 0%, #1a1f2e 100%); 
                color: #e6e6e6; 
                min-height: 100vh;
                overflow-x: hidden;
            }
            .container { max-width: 1200px; margin: 0 auto; padding: 20px; }
            .header { 
                text-align: center; 
                margin-bottom: 30px; 
                background: linear-gradient(135deg, #00d4aa, #4a9eff);
                -webkit-background-clip: text;
                -webkit-text-fill-color: transparent;
                background-clip: text;
            }
            .header h1 { 
                font-size: 3em; 
                margin-bottom: 10px; 
                font-weight: 700;
                text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
            }
            .header p { 
                color: #b0b0b0; 
                font-size: 1.2em; 
                font-weight: 300;
            }
            .status { 
                padding: 15px; 
                border-radius: 12px; 
                margin-bottom: 25px; 
                text-align: center; 
                font-weight: 500;
                box-shadow: 0 4px 15px rgba(0,0,0,0.2);
                transition: all 0.3s ease;
            }
            .status.enabled { 
                background: linear-gradient(135deg, #1a4d3a, #2a5d4a); 
                border: 2px solid #00d4aa; 
                color: #00d4aa; 
            }
            .status.disabled { 
                background: linear-gradient(135deg, #4d1a1a, #5d2a2a); 
                border: 2px solid #ff6b6b; 
                color: #ff6b6b; 
            }
            .status.checking {
                background: linear-gradient(135deg, #3a3a1a, #4a4a2a); 
                border: 2px solid #ffdd59; 
                color: #ffdd59;
                animation: pulse 1.5s infinite;
            }
            @keyframes pulse {
                0% { opacity: 1; }
                50% { opacity: 0.7; }
                100% { opacity: 1; }
            }
            .chat-container { 
                background: rgba(26, 26, 26, 0.8); 
                border-radius: 16px; 
                padding: 25px; 
                margin-bottom: 25px; 
                backdrop-filter: blur(10px);
                border: 1px solid rgba(255,255,255,0.1);
                box-shadow: 0 8px 32px rgba(0,0,0,0.3);
            }
            .chat-messages { 
                height: 450px; 
                overflow-y: auto; 
                background: rgba(10, 10, 10, 0.6); 
                border-radius: 12px; 
                padding: 20px; 
                margin-bottom: 20px;
                border: 1px solid rgba(255,255,255,0.05);
            }
            .chat-messages::-webkit-scrollbar { width: 6px; }
            .chat-messages::-webkit-scrollbar-track { background: #2a2a2a; border-radius: 3px; }
            .chat-messages::-webkit-scrollbar-thumb { background: #555; border-radius: 3px; }
            .chat-messages::-webkit-scrollbar-thumb:hover { background: #777; }
            .message { 
                margin-bottom: 20px; 
                padding: 16px; 
                border-radius: 12px; 
                transition: all 0.3s ease;
                position: relative;
            }
            .message:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.2); }
            .message.user { 
                background: linear-gradient(135deg, #2a2a3a, #3a3a4a); 
                border-left: 4px solid #00d4aa; 
                margin-left: 50px;
            }
            .message.assistant { 
                background: linear-gradient(135deg, #1a2a1a, #2a3a2a); 
                border-left: 4px solid #4a9eff; 
                margin-right: 50px;
            }
            .message .sender { 
                font-weight: 600; 
                margin-bottom: 8px; 
                font-size: 0.9em; 
                display: flex;
                align-items: center;
                gap: 8px;
            }
            .message.user .sender { color: #00d4aa; }
            .message.assistant .sender { color: #4a9eff; }
            .message .content { line-height: 1.6; }
            .input-container { 
                display: flex; 
                gap: 12px; 
                align-items: stretch;
            }
            .input-container input { 
                flex: 1; 
                padding: 16px; 
                border: 2px solid #333; 
                background: rgba(42, 42, 42, 0.8); 
                color: #e6e6e6; 
                border-radius: 12px; 
                font-size: 16px;
                backdrop-filter: blur(10px);
                transition: all 0.3s ease;
            }
            .input-container input:focus { 
                outline: none; 
                border-color: #00d4aa; 
                box-shadow: 0 0 0 3px rgba(0, 212, 170, 0.2);
            }
            .input-container input::placeholder { color: #888; }
            .input-container button { 
                padding: 16px 24px; 
                background: linear-gradient(135deg, #00d4aa, #00b894); 
                color: #000; 
                border: none; 
                border-radius: 12px; 
                cursor: pointer; 
                font-weight: 600;
                font-size: 16px;
                transition: all 0.3s ease;
                white-space: nowrap;
            }
            .input-container button:hover:not(:disabled) { 
                background: linear-gradient(135deg, #00b894, #00a085); 
                transform: translateY(-2px);
                box-shadow: 0 4px 12px rgba(0, 212, 170, 0.3);
            }
            .input-container button:active { transform: translateY(0); }
            .input-container button:disabled { 
                background: #666; 
                cursor: not-allowed; 
                transform: none;
                box-shadow: none;
            }
            .examples { 
                display: grid; 
                grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); 
                gap: 15px; 
                margin-top: 25px; 
            }
            .example-btn { 
                padding: 16px; 
                background: linear-gradient(135deg, #2a2a3a, #3a3a4a); 
                border: 2px solid #444; 
                border-radius: 12px; 
                cursor: pointer; 
                text-align: center; 
                transition: all 0.3s ease;
                font-weight: 500;
                position: relative;
                overflow: hidden;
            }
            .example-btn:before {
                content: '';
                position: absolute;
                top: 0;
                left: -100%;
                width: 100%;
                height: 100%;
                background: linear-gradient(90deg, transparent, rgba(0, 212, 170, 0.1), transparent);
                transition: left 0.5s;
            }
            .example-btn:hover:before { left: 100%; }
            .example-btn:hover { 
                background: linear-gradient(135deg, #3a3a4a, #4a4a5a); 
                border-color: #00d4aa; 
                transform: translateY(-3px);
                box-shadow: 0 6px 20px rgba(0, 212, 170, 0.2);
            }
            .loading { 
                color: #ffdd59; 
                font-style: italic; 
                display: flex;
                align-items: center;
                gap: 8px;
            }
            .loading:after {
                content: '';
                width: 12px;
                height: 12px;
                border: 2px solid #ffdd59;
                border-top: 2px solid transparent;
                border-radius: 50%;
                animation: spin 1s linear infinite;
            }
            @keyframes spin {
                0% { transform: rotate(0deg); }
                100% { transform: rotate(360deg); }
            }
            .sources { 
                margin-top: 12px; 
                font-size: 0.85em; 
                color: #999; 
                display: flex;
                flex-wrap: wrap;
                gap: 6px;
            }
            .sources .label { margin-right: 8px; font-weight: 600; }
            .sources span { 
                background: rgba(51, 51, 51, 0.8); 
                padding: 4px 8px; 
                border-radius: 6px; 
                font-size: 0.8em;
                border: 1px solid #555;
            }
            .welcome-message {
                background: linear-gradient(135deg, #1a2a4a, #2a3a5a);
                border-left: 4px solid #4a9eff;
                border-radius: 12px;
                padding: 16px;
                margin-bottom: 20px;
                text-align: center;
            }
            .footer {
                text-align: center;
                margin-top: 30px;
                color: #666;
                font-size: 0.9em;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <div class="header">
                <h1>πŸš€ Web3 Research Co-Pilot</h1>
                <p>AI-powered cryptocurrency research assistant</p>
            </div>
            
            <div id="status" class="status checking">
                <span>πŸ”„ Checking system status...</span>
            </div>
            
            <div class="chat-container">
                <div id="chatMessages" class="chat-messages">
                    <div class="welcome-message">
                        <div class="sender">πŸ€– AI Research Assistant</div>
                        <div>πŸ‘‹ Welcome! I'm your Web3 Research Co-Pilot. Ask me anything about cryptocurrency markets, DeFi protocols, blockchain analysis, or trading insights.</div>
                    </div>
                </div>
                <div class="input-container">
                    <input type="text" id="queryInput" placeholder="Ask about Bitcoin, Ethereum, DeFi yields, market analysis..." maxlength="500">
                    <button id="sendBtn" onclick="sendQuery()">πŸš€ Research</button>
                </div>
            </div>
            
            <div class="examples">
                <div class="example-btn" onclick="setQuery('What is the current Bitcoin price and market sentiment?')">
                    πŸ“ˆ Bitcoin Analysis
                </div>
                <div class="example-btn" onclick="setQuery('Show me the top DeFi protocols by TVL')">
                    🏦 DeFi Overview
                </div>
                <div class="example-btn" onclick="setQuery('What are the trending cryptocurrencies today?')">
                    πŸ”₯ Trending Coins
                </div>
                <div class="example-btn" onclick="setQuery('Analyze Ethereum gas prices and network activity')">
                    β›½ Gas Tracker
                </div>
                <div class="example-btn" onclick="setQuery('Find the best yield farming opportunities')">
                    🌾 Yield Farming
                </div>
                <div class="example-btn" onclick="setQuery('Compare Solana vs Ethereum ecosystems')">
                    βš–οΈ Ecosystem Compare
                </div>
            </div>
            
            <div class="footer">
                <p>Powered by AI β€’ Real-time Web3 data β€’ Built with ❀️</p>
            </div>
        </div>
        
        <script>
            let chatHistory = [];
            
            async function checkStatus() {
                try {
                    console.log('πŸ” Checking system status...');
                    const response = await fetch('/status');
                    const status = await response.json();
                    console.log('πŸ“Š Status received:', status);
                    
                    const statusDiv = document.getElementById('status');
                    
                    if (status.enabled && status.gemini_configured) {
                        statusDiv.className = 'status enabled';
                        statusDiv.innerHTML = `
                            <span>βœ… AI Research Agent: Online</span><br>
                            <small>Tools available: ${status.tools_available.join(', ')}</small>
                        `;
                        console.log('βœ… System fully operational');
                    } else {
                        statusDiv.className = 'status disabled';
                        statusDiv.innerHTML = `
                            <span>⚠️ Limited Mode: GEMINI_API_KEY not configured</span><br>
                            <small>Basic data available: ${status.tools_available.join(', ')}</small>
                        `;
                        console.log('⚠️ System in limited mode');
                    }
                } catch (error) {
                    console.error('❌ Status check failed:', error);
                    const statusDiv = document.getElementById('status');
                    statusDiv.className = 'status disabled';
                    statusDiv.innerHTML = '<span>❌ Connection Error</span>';
                }
            }
            
            async function sendQuery() {
                const input = document.getElementById('queryInput');
                const sendBtn = document.getElementById('sendBtn');
                const query = input.value.trim();
                
                if (!query) {
                    input.focus();
                    return;
                }
                
                console.log('πŸ“€ Sending query:', query);
                
                // Add user message
                addMessage('user', query);
                input.value = '';
                
                // Show loading
                sendBtn.disabled = true;
                sendBtn.innerHTML = '<span class="loading">Processing</span>';
                
                try {
                    const response = await fetch('/query', {
                        method: 'POST',
                        headers: { 'Content-Type': 'application/json' },
                        body: JSON.stringify({ query, chat_history: chatHistory })
                    });
                    
                    const result = await response.json();
                    console.log('πŸ“₯ Response received:', result);
                    
                    if (result.success) {
                        addMessage('assistant', result.response, result.sources);
                        console.log('βœ… Query processed successfully');
                    } else {
                        addMessage('assistant', result.response || 'An error occurred');
                        console.log('⚠️ Query failed:', result.error);
                    }
                } catch (error) {
                    console.error('❌ Network error:', error);
                    addMessage('assistant', '❌ Network error. Please check your connection and try again.');
                } finally {
                    sendBtn.disabled = false;
                    sendBtn.innerHTML = 'πŸš€ Research';
                    input.focus();
                }
            }
            
            function addMessage(sender, content, sources = []) {
                console.log(`πŸ’¬ Adding ${sender} message`);
                const messagesDiv = document.getElementById('chatMessages');
                const messageDiv = document.createElement('div');
                messageDiv.className = `message ${sender}`;
                
                let sourcesHtml = '';
                if (sources && sources.length > 0) {
                    sourcesHtml = `<div class="sources"><span class="label">Sources:</span> ${sources.map(s => `<span>${s}</span>`).join('')}</div>`;
                }
                
                const senderIcon = sender === 'user' ? 'πŸ‘€' : 'πŸ€–';
                const senderName = sender === 'user' ? 'You' : 'AI Research Assistant';
                
                messageDiv.innerHTML = `
                    <div class="sender">${senderIcon} ${senderName}</div>
                    <div class="content">${content.replace(/\n/g, '<br>')}</div>
                    ${sourcesHtml}
                `;
                
                messagesDiv.appendChild(messageDiv);
                messagesDiv.scrollTop = messagesDiv.scrollHeight;
                
                // Update chat history
                chatHistory.push({ role: sender, content });
                if (chatHistory.length > 20) chatHistory = chatHistory.slice(-20);
            }
            
            function setQuery(query) {
                console.log('πŸ“ Setting query:', query);
                const input = document.getElementById('queryInput');
                input.value = query;
                input.focus();
                
                // Optional: auto-send after a short delay
                setTimeout(() => {
                    if (input.value === query) { // Only if user didn't change it
                        sendQuery();
                    }
                }, 100);
            }
            
            // Handle Enter key
            document.getElementById('queryInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    sendQuery();
                }
            });
            
            // Initialize
            document.addEventListener('DOMContentLoaded', function() {
                console.log('πŸš€ Web3 Research Co-Pilot initialized');
                checkStatus();
                document.getElementById('queryInput').focus();
            });
        </script>
    </body>
    </html>
    """
    return HTMLResponse(content=html_content)

@app.get("/status")
async def get_status():
    logger.info("πŸ“Š Status endpoint called")
    status = {
        "enabled": service.enabled,
        "gemini_configured": bool(config.GEMINI_API_KEY),
        "tools_available": ["CoinGecko", "DeFiLlama", "Etherscan"],
        "airaa_enabled": service.airaa.enabled if service.airaa else False,
        "timestamp": datetime.now().isoformat()
    }
    logger.info(f"πŸ“Š Status response: {status}")
    return status

@app.post("/query", response_model=QueryResponse)
async def process_query(request: QueryRequest):
    logger.info(f"πŸ“₯ Query endpoint called: {request.query[:50]}{'...' if len(request.query) > 50 else ''}")
    result = await service.process_query(request.query)
    logger.info(f"πŸ“€ Query response: success={result.success}")
    return result

@app.get("/health")
async def health_check():
    logger.info("❀️ Health check endpoint called")
    return {
        "status": "healthy",
        "timestamp": datetime.now().isoformat(),
        "service_enabled": service.enabled,
        "version": "1.0.0"
    }

if __name__ == "__main__":
    import uvicorn
    logger.info("🌟 Starting FastAPI server...")
    uvicorn.run(app, host="0.0.0.0", port=7860, log_level="info")