Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
966 lines (867 loc) · 40.2 KB
/
Copy pathmain.py
File metadata and controls
966 lines (867 loc) · 40.2 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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
#!/usr/bin/env python3
"""
SARYA Web Interface
Flask application that provides a web interface for the SARYA system.
"""
import os
import json
import threading
import time
import signal
import traceback
from flask import render_template, request, jsonify, redirect, url_for
from app import app, db
from models import Clone, Metric, ReflexLog, Plugin, SystemConfig, DiagnosticResult, Tool
from sarya import SaryaSystem
# Initialize SARYA
sarya = SaryaSystem()
config_path = os.path.join(os.path.dirname(__file__), "sarya_config.json")
sarya_initialized = sarya.initialize(config_path)
sarya_thread = None
# Make sure the config has redis disabled
try:
with open(config_path, 'r') as f:
config = json.load(f)
if 'redis' not in config or config['redis'].get('enabled', True):
config['redis'] = {'enabled': False}
with open(config_path, 'w') as f:
json.dump(config, f, indent=2)
except Exception as e:
print(f"Error updating config: {e}")
@app.route('/')
def index():
"""Home page"""
status = "Running" if sarya_thread and sarya_thread.is_alive() else "Stopped"
# Get the system status
system_info = {
"status": status,
"initialized": sarya_initialized,
"config_path": config_path,
"modules": []
}
return render_template('index.html', system_info=system_info)
@app.route('/start', methods=['POST'])
def start_sarya():
"""Start the SARYA system"""
global sarya_thread
if sarya_thread and sarya_thread.is_alive():
return jsonify({"status": "SARYA is already running"})
# Reset the signals for the main thread
signal.signal(signal.SIGINT, signal.SIG_IGN)
def run_sarya():
"""Run SARYA in a separate thread"""
try:
sarya.start()
sarya.run()
except Exception as e:
print(f"Error in SARYA thread: {e}")
sarya_thread = threading.Thread(target=run_sarya)
sarya_thread.daemon = True
sarya_thread.start()
# Wait a bit for it to start
time.sleep(1)
return jsonify({"status": "SARYA started successfully"})
@app.route('/stop', methods=['POST'])
def stop_sarya():
"""Stop the SARYA system"""
if not sarya_thread or not sarya_thread.is_alive():
return jsonify({"status": "SARYA is not running"})
sarya.stop()
sarya_thread.join(timeout=5)
if sarya_thread.is_alive():
return jsonify({"status": "Failed to stop SARYA"})
return jsonify({"status": "SARYA stopped successfully"})
@app.route('/status')
def status():
"""Get the current status of the SARYA system"""
status = "Running" if sarya_thread and sarya_thread.is_alive() else "Stopped"
# Get clone queue information
from clone_system.clone_queue import clone_queue
try:
queue_info = {
"queue_size": clone_queue.get_queue_size(),
"processing_size": clone_queue.get_processing_size(),
"queued_clones": clone_queue.get_queued_clones(),
"processing_clones": clone_queue.get_processing_clones()
}
except Exception as e:
queue_info = {
"error": str(e),
"queue_size": 0,
"processing_size": 0,
"queued_clones": [],
"processing_clones": []
}
return jsonify({
"status": status,
"initialized": sarya_initialized,
"queue_info": queue_info
})
@app.route('/config')
def get_config():
"""Get the current configuration"""
try:
with open(config_path, 'r') as f:
config = json.load(f)
return jsonify(config)
except Exception as e:
return jsonify({"error": str(e)})
# Database routes
@app.route('/clones')
def get_clones():
"""Get all clones from the database"""
try:
clones = Clone.query.all()
return jsonify([clone.to_dict() for clone in clones])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/diagnostics')
def get_diagnostics():
"""Get all diagnostic results from the database"""
try:
diagnostics = DiagnosticResult.query.order_by(DiagnosticResult.created_at.desc()).limit(100).all()
return jsonify([d.to_dict() for d in diagnostics])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/diagnostics/run', methods=['POST'])
def run_diagnostics():
"""Run system diagnostics"""
try:
from core.diagnostics import DiagnosticsModule
# Create diagnostics module
diagnostics = DiagnosticsModule()
diagnostics._initialize()
# Run diagnostics
results = diagnostics.run_full_diagnosis()
# Save results to database
for issue in diagnostics.results:
diagnostic = DiagnosticResult(
issue=issue.get('issue', 'Unknown issue'),
file=issue.get('file'),
severity=issue.get('severity', 'low'),
category=issue.get('category', 'general'),
details=json.dumps(issue),
is_resolved=False
)
db.session.add(diagnostic)
db.session.commit()
return jsonify({
"status": "success",
"message": f"Diagnostics completed, found {results.get('total_issues', 0)} issues",
"results": results
})
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e), "traceback": traceback.format_exc()})
@app.route('/tools')
def get_tools():
"""Get all tools from the database"""
try:
tools = Tool.query.all()
return jsonify([tool.to_dict() for tool in tools])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/tools/sync', methods=['POST'])
def sync_tools():
"""Sync tools from registry to database"""
try:
from core.tools_registry import ToolsRegistry
# Create tools registry
registry = ToolsRegistry()
registry._initialize()
# Get all tools from registry
tools_dict = registry.get_all_tools()
# Sync with database
for name, tool_data in tools_dict.items():
# Check if tool exists
tool = Tool.query.filter_by(name=name).first()
if tool:
# Update existing tool
tool.tool_type = tool_data.get('type', 'unknown')
tool.access_method = tool_data.get('access_method', 'unknown')
tool.capabilities = tool_data.get('capabilities', [])
tool.config = tool_data
tool.status = tool_data.get('status', 'disabled')
else:
# Create new tool
tool = Tool(
name=name,
tool_type=tool_data.get('type', 'unknown'),
access_method=tool_data.get('access_method', 'unknown'),
capabilities=tool_data.get('capabilities', []),
config=tool_data,
status=tool_data.get('status', 'disabled')
)
db.session.add(tool)
db.session.commit()
return jsonify({
"status": "success",
"message": f"Synced {len(tools_dict)} tools with database"
})
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e), "traceback": traceback.format_exc()})
@app.route('/clones/<clone_id>')
def get_clone(clone_id):
"""Get a specific clone from the database"""
try:
clone = Clone.query.get(clone_id)
if clone:
return jsonify(clone.to_dict())
else:
return jsonify({"error": "Clone not found"}), 404
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/clones/<clone_id>', methods=['DELETE'])
def delete_clone(clone_id):
"""Delete a clone from the database"""
try:
clone = Clone.query.get(clone_id)
if clone:
db.session.delete(clone)
db.session.commit()
return jsonify({"status": f"Clone {clone_id} deleted successfully"})
else:
return jsonify({"error": "Clone not found"}), 404
except Exception as e:
db.session.rollback()
return jsonify({"error": str(e)})
@app.route('/metrics')
def get_metrics():
"""Get all metrics from the database"""
try:
metrics = Metric.query.order_by(Metric.timestamp.desc()).limit(100).all()
return jsonify([metric.to_dict() for metric in metrics])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/reflex_logs')
def get_reflex_logs():
"""Get all reflex logs from the database"""
try:
reflex_logs = ReflexLog.query.order_by(ReflexLog.created_at.desc()).limit(100).all()
return jsonify([log.to_dict() for log in reflex_logs])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/intelligence/knowledge')
def get_knowledge():
"""Get knowledge base concepts"""
try:
from sarya import intelligence_module
# Get all concepts from knowledge base
if "concepts" in intelligence_module.knowledge_base:
concepts = intelligence_module.knowledge_base["concepts"]
return jsonify(concepts)
else:
return jsonify({})
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/intelligence/add_knowledge', methods=['POST'])
def add_knowledge():
"""Add knowledge to the knowledge base"""
try:
from sarya import intelligence_module
data = request.json
concept_id = data.get('concept_id')
concept_data = {
"name": data.get('name'),
"description": data.get('description'),
"tags": data.get('tags', [])
}
# Add to knowledge base
success = intelligence_module.add_knowledge(concept_id, concept_data)
if success:
return jsonify({"status": "success", "message": f"Added concept {concept_id}"})
else:
return jsonify({"status": "error", "message": "Failed to add concept"})
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/intelligence/reason', methods=['POST'])
def reason():
"""Apply reasoning to a query"""
try:
from sarya import intelligence_module
data = request.json
reasoning_type = data.get('reasoning_type', 'deductive')
query = data.get('query', {})
# Apply reasoning
result = intelligence_module.reason(query, reasoning_type)
return jsonify(result)
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/intelligence/decisions')
def get_decisions():
"""Get decision history"""
try:
from sarya import intelligence_module
# Get decision history
decisions = intelligence_module.get_decision_history()
return jsonify(decisions)
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/plugins')
def get_plugins():
"""Get all plugins from the database"""
try:
plugins = Plugin.query.all()
return jsonify([plugin.to_dict() for plugin in plugins])
except Exception as e:
return jsonify({"error": str(e)})
@app.route('/system_config')
def get_system_configs():
"""Get all system configs from the database"""
try:
configs = SystemConfig.query.all()
return jsonify([config.to_dict() for config in configs])
except Exception as e:
return jsonify({"error": str(e)})
@app.errorhandler(404)
def page_not_found(e):
"""404 error handler"""
return render_template('404.html'), 404
@app.errorhandler(500)
def server_error(e):
"""500 error handler"""
return render_template('500.html'), 500
if __name__ == '__main__':
# Create templates directory if it doesn't exist
os.makedirs('templates', exist_ok=True)
# Create basic templates if they don't exist
if not os.path.exists('templates/index.html'):
with open('templates/index.html', 'w') as f:
f.write("""<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SARYA - AI Intelligence Framework</title>
<link href="https://cdn.replit.com/agent/bootstrap-agent-dark-theme.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-4">
<h1>SARYA - AI Intelligence Framework</h1>
<ul class="nav nav-tabs mb-4" id="mainTab" role="tablist">
<li class="nav-item" role="presentation">
<button class="nav-link active" id="status-tab" data-bs-toggle="tab" data-bs-target="#status" type="button" role="tab" aria-controls="status" aria-selected="true">Status</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="intelligence-tab" data-bs-toggle="tab" data-bs-target="#intelligence" type="button" role="tab" aria-controls="intelligence" aria-selected="false">Intelligence</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="clones-tab" data-bs-toggle="tab" data-bs-target="#clones" type="button" role="tab" aria-controls="clones" aria-selected="false">Clones</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="diagnostics-tab" data-bs-toggle="tab" data-bs-target="#diagnostics" type="button" role="tab" aria-controls="diagnostics" aria-selected="false">Diagnostics</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="tools-tab" data-bs-toggle="tab" data-bs-target="#tools" type="button" role="tab" aria-controls="tools" aria-selected="false">Tools</button>
</li>
<li class="nav-item" role="presentation">
<button class="nav-link" id="logs-tab" data-bs-toggle="tab" data-bs-target="#logs" type="button" role="tab" aria-controls="logs" aria-selected="false">Logs</button>
</li>
</ul>
<div class="tab-content" id="mainTabContent">
<!-- Status Tab -->
<div class="tab-pane fade show active" id="status" role="tabpanel" aria-labelledby="status-tab">
<div class="card mb-4">
<div class="card-header">
System Status
</div>
<div class="card-body">
<p><strong>Status:</strong> {{ system_info.status }}</p>
<p><strong>Initialized:</strong> {{ system_info.initialized }}</p>
<p><strong>Config Path:</strong> {{ system_info.config_path }}</p>
<div class="mb-3">
<button id="startBtn" class="btn btn-success">Start SARYA</button>
<button id="stopBtn" class="btn btn-danger">Stop SARYA</button>
</div>
<div id="statusMessage" class="alert alert-info d-none"></div>
</div>
</div>
</div>
<!-- Intelligence Tab -->
<div class="tab-pane fade" id="intelligence" role="tabpanel" aria-labelledby="intelligence-tab">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Intelligence Module</span>
<button id="refreshIntelligence" class="btn btn-sm btn-secondary">Refresh</button>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-6">
<div class="card mb-3">
<div class="card-header">Knowledge Base</div>
<div class="card-body">
<h5>Core Concepts</h5>
<div id="knowledgeList" class="list-group mb-3">
<div class="list-group-item">Loading...</div>
</div>
<h5>Add Knowledge</h5>
<form id="addKnowledgeForm" class="mt-2">
<div class="mb-2">
<label for="conceptId" class="form-label">Concept ID</label>
<input type="text" class="form-control" id="conceptId" required>
</div>
<div class="mb-2">
<label for="conceptName" class="form-label">Name</label>
<input type="text" class="form-control" id="conceptName" required>
</div>
<div class="mb-2">
<label for="conceptDescription" class="form-label">Description</label>
<textarea class="form-control" id="conceptDescription" rows="2" required></textarea>
</div>
<div class="mb-2">
<label for="conceptTags" class="form-label">Tags (comma separated)</label>
<input type="text" class="form-control" id="conceptTags">
</div>
<button type="submit" class="btn btn-primary btn-sm">Add Concept</button>
</form>
</div>
</div>
</div>
<div class="col-md-6">
<div class="card mb-3">
<div class="card-header">Reasoning</div>
<div class="card-body">
<h5>Test Reasoning</h5>
<form id="reasoningForm" class="mt-2">
<div class="mb-2">
<label for="reasoningType" class="form-label">Reasoning Type</label>
<select class="form-select" id="reasoningType" required>
<option value="deductive">Deductive (General to Specific)</option>
<option value="inductive">Inductive (Specific to General)</option>
<option value="abductive">Abductive (Best Explanation)</option>
<option value="analogical">Analogical (Source to Target)</option>
</select>
</div>
<div class="mb-2">
<label for="reasoningQuery" class="form-label">Query (JSON)</label>
<textarea class="form-control" id="reasoningQuery" rows="5" required>{"premises": ["All humans are mortal", "Socrates is human"], "conclusion": "Socrates is mortal"}</textarea>
</div>
<button type="submit" class="btn btn-primary btn-sm">Execute Reasoning</button>
</form>
<div class="mt-3">
<h5>Result</h5>
<pre id="reasoningResult" class="p-2 bg-dark text-light rounded" style="max-height: 200px; overflow-y: auto;">Submit a query to see results</pre>
</div>
</div>
</div>
<div class="card">
<div class="card-header">Decision History</div>
<div class="card-body">
<div id="decisionHistory" class="list-group">
<div class="list-group-item">No decisions yet</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Clones Tab -->
<div class="tab-pane fade" id="clones" role="tabpanel" aria-labelledby="clones-tab">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Clones</span>
<button id="refreshClones" class="btn btn-sm btn-secondary">Refresh</button>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Type</th>
<th>Status</th>
<th>Progress</th>
<th>Created</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="clonesTable">
<tr>
<td colspan="7" class="text-center">Loading...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Diagnostics Tab -->
<div class="tab-pane fade" id="diagnostics" role="tabpanel" aria-labelledby="diagnostics-tab">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span>System Diagnostics</span>
<div>
<button id="runDiagnostics" class="btn btn-sm btn-primary">Run Diagnostics</button>
<button id="refreshDiagnostics" class="btn btn-sm btn-secondary ms-2">Refresh</button>
</div>
</div>
<div class="card-body">
<div id="diagnosticsResults">
<div class="alert alert-info">Click "Run Diagnostics" to analyze the system's health.</div>
</div>
<div class="table-responsive mt-3">
<table class="table table-striped">
<thead>
<tr>
<th>Issue</th>
<th>File</th>
<th>Severity</th>
<th>Category</th>
<th>Created</th>
<th>Status</th>
</tr>
</thead>
<tbody id="diagnosticsTable">
<tr>
<td colspan="6" class="text-center">Loading...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Tools Tab -->
<div class="tab-pane fade" id="tools" role="tabpanel" aria-labelledby="tools-tab">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Tools Registry</span>
<div>
<button id="syncTools" class="btn btn-sm btn-primary">Sync Tools</button>
<button id="refreshTools" class="btn btn-sm btn-secondary ms-2">Refresh</button>
</div>
</div>
<div class="card-body">
<div id="toolsResults">
<div class="alert alert-info">Click "Sync Tools" to update tools from the registry.</div>
</div>
<div class="table-responsive mt-3">
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>Type</th>
<th>Access Method</th>
<th>Status</th>
<th>Updated</th>
</tr>
</thead>
<tbody id="toolsTable">
<tr>
<td colspan="5" class="text-center">Loading...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Logs Tab -->
<div class="tab-pane fade" id="logs" role="tabpanel" aria-labelledby="logs-tab">
<div class="card mb-4">
<div class="card-header d-flex justify-content-between align-items-center">
<span>Reflex Logs</span>
<button id="refreshLogs" class="btn btn-sm btn-secondary">Refresh</button>
</div>
<div class="card-body">
<div class="table-responsive">
<table class="table table-striped">
<thead>
<tr>
<th>Type</th>
<th>Trigger</th>
<th>Response</th>
<th>Intensity</th>
<th>Created</th>
</tr>
</thead>
<tbody id="logsTable">
<tr>
<td colspan="5" class="text-center">Loading...</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
<script>
// System Status Tab
document.getElementById('startBtn').addEventListener('click', function() {
fetch('/start', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
const statusMsg = document.getElementById('statusMessage');
statusMsg.textContent = data.status;
statusMsg.classList.remove('d-none');
setTimeout(() => {
window.location.reload();
}, 1000);
});
});
document.getElementById('stopBtn').addEventListener('click', function() {
fetch('/stop', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
const statusMsg = document.getElementById('statusMessage');
statusMsg.textContent = data.status;
statusMsg.classList.remove('d-none');
setTimeout(() => {
window.location.reload();
}, 1000);
});
});
// Clones Tab
function loadClones() {
fetch('/clones')
.then(response => response.json())
.then(clones => {
const table = document.getElementById('clonesTable');
if (clones.length === 0) {
table.innerHTML = '<tr><td colspan="7" class="text-center">No clones found</td></tr>';
return;
}
table.innerHTML = '';
clones.forEach(clone => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${clone.id}</td>
<td>${clone.name}</td>
<td>${clone.clone_type}</td>
<td>${clone.status}</td>
<td>${(clone.progress * 100).toFixed(1)}%</td>
<td>${new Date(clone.created_at).toLocaleString()}</td>
<td>
<button class="btn btn-sm btn-danger" onclick="deleteClone('${clone.id}')">Delete</button>
</td>
`;
table.appendChild(tr);
});
})
.catch(error => {
console.error('Error loading clones:', error);
document.getElementById('clonesTable').innerHTML =
'<tr><td colspan="7" class="text-center text-danger">Error loading clones</td></tr>';
});
}
function deleteClone(id) {
if (confirm('Are you sure you want to delete this clone?')) {
fetch('/clones/' + id, {
method: 'DELETE'
})
.then(response => response.json())
.then(data => {
alert(data.status || data.error);
loadClones();
})
.catch(error => {
console.error('Error deleting clone:', error);
alert('Error deleting clone');
});
}
}
document.getElementById('refreshClones').addEventListener('click', loadClones);
// Diagnostics Tab
function loadDiagnostics() {
fetch('/diagnostics')
.then(response => response.json())
.then(diagnostics => {
const table = document.getElementById('diagnosticsTable');
if (diagnostics.length === 0) {
table.innerHTML = '<tr><td colspan="6" class="text-center">No diagnostic results found</td></tr>';
return;
}
table.innerHTML = '';
diagnostics.forEach(diagnostic => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${diagnostic.issue}</td>
<td>${diagnostic.file || 'N/A'}</td>
<td><span class="badge ${getSeverityClass(diagnostic.severity)}">${diagnostic.severity}</span></td>
<td>${diagnostic.category}</td>
<td>${new Date(diagnostic.created_at).toLocaleString()}</td>
<td>${diagnostic.is_resolved ? 'Resolved' : 'Open'}</td>
`;
table.appendChild(tr);
});
})
.catch(error => {
console.error('Error loading diagnostics:', error);
document.getElementById('diagnosticsTable').innerHTML =
'<tr><td colspan="6" class="text-center text-danger">Error loading diagnostics</td></tr>';
});
}
function getSeverityClass(severity) {
switch(severity) {
case 'high': return 'bg-danger';
case 'medium': return 'bg-warning';
case 'low': return 'bg-info';
default: return 'bg-secondary';
}
}
document.getElementById('runDiagnostics').addEventListener('click', function() {
const resultsDiv = document.getElementById('diagnosticsResults');
resultsDiv.innerHTML = '<div class="alert alert-info">Running diagnostics, please wait...</div>';
fetch('/diagnostics/run', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data.error) {
resultsDiv.innerHTML = `<div class="alert alert-danger">Error: ${data.error}</div>`;
return;
}
resultsDiv.innerHTML = `
<div class="alert alert-success">
${data.message}
<div class="mt-2">
<strong>Issues by severity:</strong>
<span class="badge bg-danger ms-2">High: ${data.results.severity_counts.high || 0}</span>
<span class="badge bg-warning ms-2">Medium: ${data.results.severity_counts.medium || 0}</span>
<span class="badge bg-info ms-2">Low: ${data.results.severity_counts.low || 0}</span>
</div>
</div>
`;
loadDiagnostics();
})
.catch(error => {
console.error('Error running diagnostics:', error);
resultsDiv.innerHTML = '<div class="alert alert-danger">Error running diagnostics</div>';
});
});
document.getElementById('refreshDiagnostics').addEventListener('click', loadDiagnostics);
// Tools Tab
function loadTools() {
fetch('/tools')
.then(response => response.json())
.then(tools => {
const table = document.getElementById('toolsTable');
if (tools.length === 0) {
table.innerHTML = '<tr><td colspan="5" class="text-center">No tools found</td></tr>';
return;
}
table.innerHTML = '';
tools.forEach(tool => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${tool.name}</td>
<td>${tool.tool_type}</td>
<td>${tool.access_method}</td>
<td><span class="badge ${tool.status === 'enabled' ? 'bg-success' : 'bg-secondary'}">${tool.status}</span></td>
<td>${new Date(tool.updated_at).toLocaleString()}</td>
`;
table.appendChild(tr);
});
})
.catch(error => {
console.error('Error loading tools:', error);
document.getElementById('toolsTable').innerHTML =
'<tr><td colspan="5" class="text-center text-danger">Error loading tools</td></tr>';
});
}
document.getElementById('syncTools').addEventListener('click', function() {
const resultsDiv = document.getElementById('toolsResults');
resultsDiv.innerHTML = '<div class="alert alert-info">Syncing tools, please wait...</div>';
fetch('/tools/sync', {
method: 'POST'
})
.then(response => response.json())
.then(data => {
if (data.error) {
resultsDiv.innerHTML = `<div class="alert alert-danger">Error: ${data.error}</div>`;
return;
}
resultsDiv.innerHTML = `<div class="alert alert-success">${data.message}</div>`;
loadTools();
})
.catch(error => {
console.error('Error syncing tools:', error);
resultsDiv.innerHTML = '<div class="alert alert-danger">Error syncing tools</div>';
});
});
document.getElementById('refreshTools').addEventListener('click', loadTools);
// Logs Tab
function loadLogs() {
fetch('/reflex_logs')
.then(response => response.json())
.then(logs => {
const table = document.getElementById('logsTable');
if (logs.length === 0) {
table.innerHTML = '<tr><td colspan="5" class="text-center">No logs found</td></tr>';
return;
}
table.innerHTML = '';
logs.forEach(log => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${log.reflex_type}</td>
<td>${log.trigger}</td>
<td>${log.response}</td>
<td>${log.intensity.toFixed(2)}</td>
<td>${new Date(log.created_at).toLocaleString()}</td>
`;
table.appendChild(tr);
});
})
.catch(error => {
console.error('Error loading logs:', error);
document.getElementById('logsTable').innerHTML =
'<tr><td colspan="5" class="text-center text-danger">Error loading logs</td></tr>';
});
}
document.getElementById('refreshLogs').addEventListener('click', loadLogs);
// Initialize tabs
document.addEventListener('DOMContentLoaded', function() {
// Load initial data
loadClones();
loadDiagnostics();
loadTools();
loadLogs();
// Add tab switching event listeners
document.querySelectorAll('button[data-bs-toggle="tab"]').forEach(tab => {
tab.addEventListener('shown.bs.tab', function(event) {
const targetId = event.target.getAttribute('aria-controls');
switch(targetId) {
case 'clones':
loadClones();
break;
case 'diagnostics':
loadDiagnostics();
break;
case 'tools':
loadTools();
break;
case 'logs':
loadLogs();
break;
}
});
});
});
// Make deleteClone available globally
window.deleteClone = deleteClone;
</script>
</body>
</html>""")
# Handle shutting down SARYA when the Flask app exits
def signal_handler(sig, frame):
print('Shutting down SARYA...')
if sarya_thread and sarya_thread.is_alive():
sarya.stop()
sarya_thread.join(timeout=5)
exit(0)
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Start the Flask app
app.run(host='0.0.0.0', port=5000)