-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclearcouncil_chat_server.py
More file actions
679 lines (601 loc) · 24.4 KB
/
clearcouncil_chat_server.py
File metadata and controls
679 lines (601 loc) · 24.4 KB
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
#!/usr/bin/env python3
"""
ClearCouncil Chat Server - Basic HTTP Server Version
A web server that works with Python's built-in HTTP server modules,
providing the ClearCouncil chat interface without requiring Flask.
"""
import sys
import os
import json
import urllib.parse
import urllib.request
from pathlib import Path
from http.server import HTTPServer, BaseHTTPRequestHandler
from socketserver import ThreadingMixIn
from datetime import datetime
import html
import threading
import time
# Add the src directory to Python path
src_path = Path(__file__).parent / "src"
sys.path.insert(0, str(src_path))
# Import existing ClearCouncil components
try:
from clearcouncil.config.settings import list_available_councils, load_council_config
CLEARCOUNCIL_AVAILABLE = True
except ImportError as e:
print(f"Warning: ClearCouncil components not available: {e}")
CLEARCOUNCIL_AVAILABLE = False
# Import the chat bot from our minimal version
try:
from clearcouncil_chat_minimal import ClearCouncilChatBot
CHATBOT_AVAILABLE = True
except ImportError as e:
print(f"Warning: ChatBot not available: {e}")
CHATBOT_AVAILABLE = False
class ThreadedHTTPServer(ThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
daemon_threads = True
class ClearCouncilChatHandler(BaseHTTPRequestHandler):
"""HTTP request handler for the ClearCouncil chat interface."""
# Class-level chatbot instance
chatbot = None
def log_message(self, format, *args):
"""Custom logging to reduce noise."""
pass # Suppress default logging
def do_GET(self):
"""Handle GET requests."""
if self.path == '/':
self.serve_chat_page()
elif self.path == '/health':
self.serve_health_check()
elif self.path == '/api/councils':
self.serve_councils()
elif self.path.startswith('/api/'):
self.serve_api_error("GET not supported for this endpoint")
else:
self.serve_404()
def do_POST(self):
"""Handle POST requests."""
if self.path == '/api/chat':
self.handle_chat()
elif self.path == '/api/set-council':
self.handle_set_council()
else:
self.serve_api_error("Endpoint not found")
def get_chatbot(self):
"""Get or create the chatbot instance."""
if ClearCouncilChatHandler.chatbot is None:
# Load GitHub token from environment
env_file = Path(".env")
github_token = None
if env_file.exists():
with open(env_file, 'r') as f:
for line in f:
if line.startswith('GITHUB_TOKEN='):
github_token = line.split('=', 1)[1].strip()
break
print(f"🔑 GitHub token loaded: {'✅ Yes' if github_token else '❌ No'}")
if CHATBOT_AVAILABLE:
ClearCouncilChatHandler.chatbot = ClearCouncilChatBot(github_token)
else:
ClearCouncilChatHandler.chatbot = None
return ClearCouncilChatHandler.chatbot
def serve_chat_page(self):
"""Serve the main chat interface."""
html_content = """<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ClearCouncil AI Chat</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
margin: 0;
padding: 20px;
background-color: #f5f5f5;
}
.container {
max-width: 800px;
margin: 0 auto;
background: white;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
overflow: hidden;
}
.header {
background: #007bff;
color: white;
padding: 20px;
text-align: center;
}
.header h1 {
margin: 0;
font-size: 1.5em;
}
.status {
background: #e9ecef;
padding: 15px;
border-bottom: 1px solid #dee2e6;
}
.status-item {
margin: 5px 0;
font-size: 0.9em;
}
.status-connected {
color: #28a745;
}
.status-disconnected {
color: #dc3545;
}
.chat-container {
height: 400px;
overflow-y: auto;
padding: 20px;
background: #f8f9fa;
}
.message {
margin: 10px 0;
padding: 10px 15px;
border-radius: 15px;
max-width: 80%;
word-wrap: break-word;
}
.user-message {
background: #007bff;
color: white;
margin-left: auto;
text-align: right;
}
.bot-message {
background: #e9ecef;
color: #333;
margin-right: auto;
}
.system-message {
background: #fff3cd;
color: #856404;
margin: 10px auto;
text-align: center;
font-style: italic;
}
.input-container {
padding: 20px;
background: white;
border-top: 1px solid #dee2e6;
}
.input-row {
display: flex;
gap: 10px;
}
.council-select {
width: 200px;
padding: 10px;
border: 1px solid #ced4da;
border-radius: 5px;
font-size: 0.9em;
}
.message-input {
flex: 1;
padding: 10px;
border: 1px solid #ced4da;
border-radius: 5px;
font-size: 1em;
}
.send-button {
padding: 10px 20px;
background: #007bff;
color: white;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1em;
}
.send-button:hover {
background: #0056b3;
}
.send-button:disabled {
background: #6c757d;
cursor: not-allowed;
}
.quick-actions {
margin-top: 10px;
display: flex;
gap: 10px;
flex-wrap: wrap;
}
.quick-action {
padding: 5px 10px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 15px;
font-size: 0.8em;
cursor: pointer;
}
.quick-action:hover {
background: #e9ecef;
}
.loading {
opacity: 0.6;
pointer-events: none;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🏛️ ClearCouncil AI Chat</h1>
<p>Chat with your local government data using free GitHub AI</p>
</div>
<div class="status">
<div class="status-item">
<strong>Connection:</strong>
<span id="connectionStatus" class="status-disconnected">Checking...</span>
</div>
<div class="status-item">
<strong>Council:</strong>
<span id="councilStatus" class="status-disconnected">None selected</span>
</div>
<div class="status-item">
<strong>AI Model:</strong>
<span>GitHub GPT-4o-mini (Free)</span>
</div>
</div>
<div id="chatContainer" class="chat-container">
<div class="message bot-message">
<strong>🤖 ClearCouncil AI</strong><br>
Welcome! I'm your AI assistant for local government transparency.
<br><br>
<strong>To get started:</strong><br>
1. Select a council from the dropdown below<br>
2. Ask me questions about representatives, voting records, or documents<br>
3. Use the quick actions for common queries<br>
<br>
<em>Examples:</em><br>
• "Show me voting records for John Smith"<br>
• "What happened in the last council meeting?"<br>
• "Explain how local government works"
</div>
</div>
<div class="input-container">
<div class="input-row">
<select id="councilSelect" class="council-select">
<option value="">Select council...</option>
</select>
<input type="text" id="messageInput" class="message-input"
placeholder="Ask me about your council data..." disabled>
<button id="sendButton" class="send-button" onclick="sendMessage()" disabled>Send</button>
</div>
<div class="quick-actions">
<div class="quick-action" onclick="quickAction('What representatives are available?')">
📊 Show Representatives
</div>
<div class="quick-action" onclick="quickAction('What voting records are available?')">
🗳️ Voting Records
</div>
<div class="quick-action" onclick="quickAction('Explain how local government works')">
📚 How Government Works
</div>
<div class="quick-action" onclick="quickAction('What documents are available?')">
📄 Documents
</div>
</div>
</div>
</div>
<script>
let currentCouncil = '';
let isLoading = false;
// Initialize the app
document.addEventListener('DOMContentLoaded', function() {
checkConnection();
loadCouncils();
setupEventListeners();
});
function setupEventListeners() {
document.getElementById('messageInput').addEventListener('keypress', function(e) {
if (e.key === 'Enter' && !e.shiftKey) {
e.preventDefault();
sendMessage();
}
});
document.getElementById('councilSelect').addEventListener('change', function() {
setCouncil(this.value);
});
}
async function checkConnection() {
try {
const response = await fetch('/health');
const data = await response.json();
const statusElement = document.getElementById('connectionStatus');
if (data.status === 'healthy') {
if (data.ai_working) {
statusElement.textContent = 'Connected (AI Working)';
statusElement.className = 'status-connected';
} else if (data.github_token_configured) {
statusElement.textContent = 'Connected (AI Limited)';
statusElement.className = 'status-connected';
} else {
statusElement.textContent = 'Connected (No AI Token)';
statusElement.className = 'status-connected';
}
} else {
statusElement.textContent = 'Connection issues';
statusElement.className = 'status-disconnected';
}
// Log detailed status for debugging
console.log('Health check:', data);
} catch (error) {
console.error('Connection check failed:', error);
document.getElementById('connectionStatus').textContent = 'Connection failed';
document.getElementById('connectionStatus').className = 'status-disconnected';
}
}
async function loadCouncils() {
try {
const response = await fetch('/api/councils');
const data = await response.json();
const select = document.getElementById('councilSelect');
select.innerHTML = '<option value="">Select council...</option>';
if (data.councils && data.councils.length > 0) {
data.councils.forEach(council => {
const option = document.createElement('option');
option.value = council;
option.textContent = council.replace(/_/g, ' ').replace(/\\b\\w/g, l => l.toUpperCase());
select.appendChild(option);
});
// Auto-select the first council if there's only one
if (data.councils.length === 1) {
select.value = data.councils[0];
setCouncil(data.councils[0]);
}
} else {
addMessage('system', 'No councils found. Please check the server configuration.');
}
console.log('Councils loaded:', data.councils);
} catch (error) {
console.error('Error loading councils:', error);
addMessage('system', 'Error loading councils. Please refresh the page.');
}
}
async function setCouncil(councilId) {
if (!councilId) {
currentCouncil = '';
document.getElementById('councilStatus').textContent = 'None selected';
document.getElementById('councilStatus').className = 'status-disconnected';
document.getElementById('messageInput').disabled = true;
document.getElementById('sendButton').disabled = true;
return;
}
try {
const response = await fetch('/api/set-council', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ council_id: councilId })
});
const data = await response.json();
if (data.success) {
currentCouncil = councilId;
document.getElementById('councilStatus').textContent = councilId.replace('_', ' ').toUpperCase();
document.getElementById('councilStatus').className = 'status-connected';
document.getElementById('messageInput').disabled = false;
document.getElementById('sendButton').disabled = false;
addMessage('system', `✅ Connected to ${councilId.replace('_', ' ').toUpperCase()}. You can now ask questions!`);
} else {
addMessage('system', `❌ Failed to connect to ${councilId}: ${data.error}`);
}
} catch (error) {
console.error('Error setting council:', error);
addMessage('system', '❌ Error connecting to council. Please try again.');
}
}
async function sendMessage() {
if (isLoading) return;
const input = document.getElementById('messageInput');
const message = input.value.trim();
if (!message) return;
if (!currentCouncil) {
addMessage('system', '⚠️ Please select a council first.');
return;
}
// Add user message
addMessage('user', message);
input.value = '';
// Show loading
isLoading = true;
document.getElementById('sendButton').disabled = true;
document.getElementById('sendButton').textContent = 'Sending...';
try {
const response = await fetch('/api/chat', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ message: message })
});
const data = await response.json();
if (data.response) {
addMessage('bot', data.response);
} else {
addMessage('system', `❌ Error: ${data.error}`);
}
} catch (error) {
console.error('Error sending message:', error);
addMessage('system', '❌ Error sending message. Please try again.');
} finally {
isLoading = false;
document.getElementById('sendButton').disabled = false;
document.getElementById('sendButton').textContent = 'Send';
}
}
function addMessage(role, content) {
const chatContainer = document.getElementById('chatContainer');
const messageDiv = document.createElement('div');
let messageClass = '';
let prefix = '';
switch (role) {
case 'user':
messageClass = 'user-message';
prefix = '<strong>👤 You</strong><br>';
break;
case 'bot':
messageClass = 'bot-message';
prefix = '<strong>🤖 ClearCouncil AI</strong><br>';
break;
case 'system':
messageClass = 'system-message';
prefix = '';
break;
}
messageDiv.className = `message ${messageClass}`;
messageDiv.innerHTML = prefix + content.replace(/\\n/g, '<br>');
chatContainer.appendChild(messageDiv);
chatContainer.scrollTop = chatContainer.scrollHeight;
}
function quickAction(message) {
if (!currentCouncil) {
addMessage('system', '⚠️ Please select a council first.');
return;
}
document.getElementById('messageInput').value = message;
sendMessage();
}
</script>
</body>
</html>"""
self.send_response(200)
self.send_header('Content-Type', 'text/html')
self.send_header('Content-Length', str(len(html_content)))
self.end_headers()
self.wfile.write(html_content.encode('utf-8'))
def serve_health_check(self):
"""Serve health check endpoint."""
# Load GitHub token to check if it's available
env_file = Path(".env")
github_token = None
if env_file.exists():
with open(env_file, 'r') as f:
for line in f:
if line.startswith('GITHUB_TOKEN='):
github_token = line.split('=', 1)[1].strip()
break
# Check if chatbot is working
chatbot_working = False
if CHATBOT_AVAILABLE and github_token:
try:
chatbot = self.get_chatbot()
if chatbot and hasattr(chatbot, 'github_client'):
chatbot_working = getattr(chatbot.github_client, 'available', False)
except Exception as e:
print(f"Chatbot check error: {e}")
health_data = {
"status": "healthy",
"chatbot_available": CHATBOT_AVAILABLE,
"clearcouncil_available": CLEARCOUNCIL_AVAILABLE,
"github_token_configured": github_token is not None,
"ai_working": chatbot_working,
"components": {
"chatbot": "✅ Available" if CHATBOT_AVAILABLE else "❌ Not available",
"clearcouncil": "✅ Available" if CLEARCOUNCIL_AVAILABLE else "❌ Limited",
"github_token": "✅ Configured" if github_token else "❌ Not configured",
"ai_api": "✅ Working" if chatbot_working else "❌ Not working"
}
}
self.send_json_response(health_data)
def serve_councils(self):
"""Serve councils list endpoint."""
try:
if CLEARCOUNCIL_AVAILABLE:
councils = list_available_councils()
else:
councils = ["york_county_sc", "test_council"]
self.send_json_response({"councils": councils})
except Exception as e:
self.send_json_response({"error": str(e)}, status=500)
def handle_chat(self):
"""Handle chat message."""
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
message = data.get('message', '')
if not message:
self.send_json_response({"error": "Message required"}, status=400)
return
chatbot = self.get_chatbot()
if chatbot:
response = chatbot.process_message(message)
else:
response = "Sorry, the chatbot is not available. Please check the server configuration."
self.send_json_response({
"response": response,
"timestamp": datetime.now().isoformat()
})
except Exception as e:
self.send_json_response({"error": str(e)}, status=500)
def handle_set_council(self):
"""Handle set council request."""
try:
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
data = json.loads(post_data.decode('utf-8'))
council_id = data.get('council_id')
if not council_id:
self.send_json_response({"error": "Council ID required"}, status=400)
return
chatbot = self.get_chatbot()
if chatbot:
success = chatbot.set_council(council_id)
else:
success = False
if success:
self.send_json_response({
"success": True,
"council_id": council_id
})
else:
self.send_json_response({"error": "Failed to set council"}, status=500)
except Exception as e:
self.send_json_response({"error": str(e)}, status=500)
def serve_api_error(self, message):
"""Serve API error response."""
self.send_json_response({"error": message}, status=404)
def serve_404(self):
"""Serve 404 page."""
self.send_response(404)
self.send_header('Content-Type', 'text/html')
self.end_headers()
self.wfile.write(b"<h1>404 Not Found</h1><p>The requested resource was not found.</p>")
def send_json_response(self, data, status=200):
"""Send JSON response."""
json_data = json.dumps(data, indent=2)
self.send_response(status)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(json_data)))
self.end_headers()
self.wfile.write(json_data.encode('utf-8'))
def main():
"""Start the ClearCouncil chat server."""
port = int(os.environ.get('PORT', 5002))
host = os.environ.get('HOST', '0.0.0.0')
print(f"🏛️ ClearCouncil AI Chat Server")
print(f"=" * 50)
print(f"🌐 Starting server on http://{host}:{port}")
print(f"📊 ClearCouncil modules: {'✅ Available' if CLEARCOUNCIL_AVAILABLE else '❌ Limited'}")
print(f"🤖 ChatBot: {'✅ Available' if CHATBOT_AVAILABLE else '❌ Limited'}")
print(f"🔑 GitHub token: {'✅ Configured' if os.path.exists('.env') else '❌ Not found'}")
print(f"")
print(f"📱 Open in browser: http://localhost:{port}")
print(f"❌ Press Ctrl+C to stop")
print(f"=" * 50)
try:
server = ThreadedHTTPServer((host, port), ClearCouncilChatHandler)
server.serve_forever()
except KeyboardInterrupt:
print(f"\n🛑 Server stopped by user")
except Exception as e:
print(f"\n❌ Server error: {e}")
if __name__ == "__main__":
main()