-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfixed_api_routes.py
More file actions
355 lines (294 loc) · 14 KB
/
fixed_api_routes.py
File metadata and controls
355 lines (294 loc) · 14 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
#!/usr/bin/env python3
"""
XMRT-Ecosystem API Route Fixes
Comprehensive fixes for API routing, WebSocket connections, and frontend integration
"""
import os
import json
import logging
from datetime import datetime, timedelta
from flask import Flask, request, jsonify
from flask_socketio import SocketIO, emit, join_room, leave_room
from functools import wraps
# Enhanced API route fixes for XMRT-Ecosystem
class XMRTAPIFixer:
def __init__(self, app, socketio):
self.app = app
self.socketio = socketio
self.active_connections = {}
self.learning_data = []
self.autonomous_state = {
'learning_active': False,
'coordination_active': False,
'memory_active': False,
'agents_active': False
}
def setup_fixed_routes(self):
"""Setup all fixed API routes with proper error handling"""
# ==========================================
# CORE AUTONOMOUS SYSTEM ROUTES (FIXED)
# ==========================================
@self.app.route('/api/agents/activate', methods=['POST'])
def activate_agents():
"""Fixed agent activation endpoint"""
try:
# Simulate agent activation
self.autonomous_state['agents_active'] = True
# Start learning cycle
self.start_learning_cycle()
response = {
'success': True,
'message': 'Autonomous agents activated successfully',
'agents': {
'coordinator': {'status': 'active', 'tasks': 0},
'analyzer': {'status': 'active', 'insights': 0},
'developer': {'status': 'active', 'optimizations': 0}
},
'learning_active': self.autonomous_state['learning_active'],
'timestamp': datetime.now().isoformat()
}
# Emit to all connected clients
self.socketio.emit('agents_activated', response)
return jsonify(response)
except Exception as e:
error_response = {
'success': False,
'error': str(e),
'timestamp': datetime.now().isoformat()
}
return jsonify(error_response), 500
@self.app.route('/api/learning/start', methods=['POST'])
def start_learning():
"""Fixed learning system activation"""
try:
self.autonomous_state['learning_active'] = True
# Initialize learning data collection
learning_session = {
'session_id': f"learn_{int(datetime.now().timestamp())}",
'started_at': datetime.now().isoformat(),
'status': 'active',
'data_points': 0
}
self.learning_data.append(learning_session)
response = {
'success': True,
'message': 'Learning system activated',
'session': learning_session,
'autonomous_state': self.autonomous_state,
'timestamp': datetime.now().isoformat()
}
# Emit learning status update
self.socketio.emit('learning_started', response)
return jsonify(response)
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'timestamp': datetime.now().isoformat()
}), 500
@self.app.route('/api/coordination/activate', methods=['POST'])
def activate_coordination():
"""Fixed coordination system activation"""
try:
self.autonomous_state['coordination_active'] = True
coordination_status = {
'active': True,
'agents_coordinated': 3,
'tasks_managed': 0,
'started_at': datetime.now().isoformat()
}
response = {
'success': True,
'message': 'Coordination system activated',
'coordination': coordination_status,
'autonomous_state': self.autonomous_state,
'timestamp': datetime.now().isoformat()
}
# Emit coordination status
self.socketio.emit('coordination_activated', response)
return jsonify(response)
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'timestamp': datetime.now().isoformat()
}), 500
@self.app.route('/api/memory/activate', methods=['POST'])
def activate_memory():
"""Fixed memory system activation"""
try:
self.autonomous_state['memory_active'] = True
memory_status = {
'active': True,
'storage_initialized': True,
'learning_data_stored': len(self.learning_data),
'started_at': datetime.now().isoformat()
}
response = {
'success': True,
'message': 'Long-term memory system activated',
'memory': memory_status,
'autonomous_state': self.autonomous_state,
'timestamp': datetime.now().isoformat()
}
# Emit memory status
self.socketio.emit('memory_activated', response)
return jsonify(response)
except Exception as e:
return jsonify({
'success': False,
'error': str(e),
'timestamp': datetime.now().isoformat()
}), 500
# ==========================================
# STATUS AND MONITORING ROUTES (FIXED)
# ==========================================
@self.app.route('/api/status/autonomous', methods=['GET'])
def get_autonomous_status():
"""Get comprehensive autonomous system status"""
try:
status = {
'autonomous_state': self.autonomous_state,
'learning_sessions': len(self.learning_data),
'active_connections': len(self.active_connections),
'system_health': 'optimal' if all(self.autonomous_state.values()) else 'partial',
'uptime': str(datetime.now() - datetime.now().replace(hour=0, minute=0, second=0)),
'timestamp': datetime.now().isoformat()
}
return jsonify(status)
except Exception as e:
return jsonify({
'error': str(e),
'timestamp': datetime.now().isoformat()
}), 500
@self.app.route('/api/learning/data', methods=['GET'])
def get_learning_data():
"""Get learning system data"""
try:
return jsonify({
'learning_sessions': self.learning_data,
'total_sessions': len(self.learning_data),
'active_learning': self.autonomous_state['learning_active'],
'timestamp': datetime.now().isoformat()
})
except Exception as e:
return jsonify({
'error': str(e),
'timestamp': datetime.now().isoformat()
}), 500
# ==========================================
# WEBSOCKET EVENT HANDLERS (FIXED)
# ==========================================
@self.socketio.on('connect')
def handle_connect():
"""Fixed WebSocket connection handler"""
try:
client_id = request.sid
self.active_connections[client_id] = {
'connected_at': datetime.now().isoformat(),
'user_agent': request.headers.get('User-Agent', 'Unknown'),
'ip': request.remote_addr
}
# Send connection confirmation with system status
emit('connection_response', {
'status': 'connected',
'message': 'Connected to XMRT-Ecosystem Maximum Capacity System',
'client_id': client_id,
'features': {
'autonomous_system': True,
'activity_monitor': True,
'coordination_api': True,
'chat_system': True,
'memory_optimizer': True,
'learning_system': True
},
'autonomous_state': self.autonomous_state,
'timestamp': datetime.now().isoformat()
})
# Join main room for system updates
join_room('system_updates')
logging.info(f"Client connected: {client_id}")
except Exception as e:
logging.error(f"Connection error: {str(e)}")
emit('error', {'message': str(e)})
@self.socketio.on('disconnect')
def handle_disconnect():
"""Fixed WebSocket disconnection handler"""
try:
client_id = request.sid
if client_id in self.active_connections:
del self.active_connections[client_id]
leave_room('system_updates')
logging.info(f"Client disconnected: {client_id}")
except Exception as e:
logging.error(f"Disconnection error: {str(e)}")
@self.socketio.on('request_status')
def handle_status_request():
"""Handle status requests from frontend"""
try:
emit('status_update', {
'autonomous_state': self.autonomous_state,
'active_connections': len(self.active_connections),
'learning_sessions': len(self.learning_data),
'timestamp': datetime.now().isoformat()
})
except Exception as e:
emit('error', {'message': str(e)})
@self.socketio.on('activate_system')
def handle_system_activation(data):
"""Handle system activation requests from frontend"""
try:
system_type = data.get('system', 'all')
if system_type == 'agents' or system_type == 'all':
self.autonomous_state['agents_active'] = True
if system_type == 'learning' or system_type == 'all':
self.autonomous_state['learning_active'] = True
self.start_learning_cycle()
if system_type == 'coordination' or system_type == 'all':
self.autonomous_state['coordination_active'] = True
if system_type == 'memory' or system_type == 'all':
self.autonomous_state['memory_active'] = True
# Emit activation confirmation
emit('system_activated', {
'system': system_type,
'autonomous_state': self.autonomous_state,
'message': f'{system_type.title()} system activated successfully',
'timestamp': datetime.now().isoformat()
})
# Broadcast to all clients
self.socketio.emit('system_update', {
'autonomous_state': self.autonomous_state,
'timestamp': datetime.now().isoformat()
}, room='system_updates')
except Exception as e:
emit('error', {'message': str(e)})
def start_learning_cycle(self):
"""Start the autonomous learning cycle"""
try:
if not self.autonomous_state['learning_active']:
return
# Simulate learning data collection
learning_point = {
'timestamp': datetime.now().isoformat(),
'type': 'system_interaction',
'data': {
'active_connections': len(self.active_connections),
'system_state': self.autonomous_state.copy()
}
}
# Store learning data
if self.learning_data:
self.learning_data[-1]['data_points'] = self.learning_data[-1].get('data_points', 0) + 1
# Emit learning update
self.socketio.emit('learning_update', {
'learning_point': learning_point,
'total_points': sum(session.get('data_points', 0) for session in self.learning_data),
'timestamp': datetime.now().isoformat()
}, room='system_updates')
except Exception as e:
logging.error(f"Learning cycle error: {str(e)}")
def apply_api_fixes(app, socketio):
"""Apply all API fixes to the Flask app"""
fixer = XMRTAPIFixer(app, socketio)
fixer.setup_fixed_routes()
return fixer