-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_server.py
More file actions
executable file
·149 lines (120 loc) · 5.14 KB
/
Copy pathdemo_server.py
File metadata and controls
executable file
·149 lines (120 loc) · 5.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
"""
Simple HTTP Server for CONSIM Demo
Serves the Three.js frontend and provides basic API endpoints.
"""
import http.server
import socketserver
import json
import urllib.parse
import sys
from pathlib import Path
# Add src to path
sys.path.insert(0, str(Path(__file__).parent / "src"))
from lattice_demo import ConsciousnessLattice
class ConsciousnessHTTPHandler(http.server.SimpleHTTPRequestHandler):
"""HTTP handler with API support for consciousness lattice."""
def __init__(self, *args, **kwargs):
super().__init__(*args, directory=str(Path(__file__).parent), **kwargs)
def do_GET(self):
"""Handle GET requests."""
if self.path == '/':
self.path = '/static/index.html'
elif self.path.startswith('/api/'):
self.handle_api_get()
return
super().do_GET()
def do_POST(self):
"""Handle POST requests."""
if self.path.startswith('/api/'):
self.handle_api_post()
else:
self.send_error(404)
def handle_api_get(self):
"""Handle API GET requests."""
global lattice
if self.path == '/api/status':
status = {
'running': True,
'node_count': len(lattice.nodes),
'cluster_count': len(lattice.clusters),
'time': lattice.time
}
self.send_json_response(status)
elif self.path == '/api/stats':
stats = lattice._calculate_global_consciousness()
self.send_json_response(stats)
elif self.path == '/api/state':
# Get current lattice state
state = lattice.get_state_for_transmission()
# Serialize nodes properly
serialized_state = {
'nodes': [node.to_dict() for node in lattice.nodes],
'universes': [universe.to_dict() for universe in lattice.universes],
'clusters': lattice.clusters,
'global_stats': lattice._calculate_global_consciousness(),
'params': lattice.params,
'lambdas': lattice.lambdas,
'time': lattice.time
}
self.send_json_response(serialized_state)
else:
self.send_error(404)
def handle_api_post(self):
"""Handle API POST requests."""
global lattice
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
try:
data = json.loads(post_data.decode('utf-8'))
except json.JSONDecodeError:
self.send_error(400, "Invalid JSON")
return
if self.path == '/api/update':
# Update lattice
lattice.update(0.016) # ~60fps
self.send_json_response({'status': 'updated'})
elif self.path == '/api/parameters':
# Update parameters
lattice.update_params(data)
self.send_json_response({'status': 'updated', 'parameters': data})
elif self.path == '/api/nodes':
# Add new node
node = lattice.add_node(data.get('x', 0), data.get('y', 0))
self.send_json_response({'status': 'created', 'node': node.to_dict()})
elif self.path == '/api/collapse':
# Trigger quantum collapse
lattice.quantum_collapse(data.get('x', 0), data.get('y', 0))
self.send_json_response({'status': 'triggered'})
elif self.path == '/api/reset':
# Reset simulation
lattice = ConsciousnessLattice(grid_size=64)
self.send_json_response({'status': 'reset'})
else:
self.send_error(404)
def send_json_response(self, data):
"""Send JSON response."""
json_data = json.dumps(data).encode('utf-8')
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', str(len(json_data)))
self.send_header('Access-Control-Allow-Origin', '*')
self.send_header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS')
self.send_header('Access-Control-Allow-Headers', 'Content-Type')
self.end_headers()
self.wfile.write(json_data)
# Global lattice instance
lattice = ConsciousnessLattice(grid_size=64)
def start_server(port=8000):
"""Start the consciousness simulation server."""
with socketserver.TCPServer(("", port), ConsciousnessHTTPHandler) as httpd:
print(f"🧠 CONSIM Demo Server starting on http://localhost:{port}")
print(f"✨ Consciousness lattice with {len(lattice.nodes)} nodes initialized")
print(f"🌌 {len(lattice.universes)} universes with λ weights: {[f'{l:.3f}' for l in lattice.lambdas]}")
print(f"🔗 API endpoints: /api/status, /api/stats, /api/state")
print("💡 Open http://localhost:8000 in your browser")
try:
httpd.serve_forever()
except KeyboardInterrupt:
print("\n🛑 Server stopped")
if __name__ == "__main__":
start_server()