forked from XargonWan/Synthetic_Heart
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
377 lines (301 loc) · 15 KB
/
main.py
File metadata and controls
377 lines (301 loc) · 15 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
import os
import signal
import sys
import asyncio
from pathlib import Path
def _load_repo_env_defaults() -> None:
env_path = Path(__file__).resolve().with_name(".env")
if not env_path.exists():
return
for raw_line in env_path.read_text(encoding="utf-8").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
key = key.strip()
if not key:
continue
value = value.strip()
if value[:1] in {'"', "'"} and value[-1:] == value[:1]:
value = value[1:-1]
os.environ.setdefault(key, value)
_load_repo_env_defaults()
from core.db import init_db, test_connection, get_conn_ctx, _get_db_type # noqa: E402
# from core.blocklist import init_blocklist_table # Now handled by blocklist plugin
from core.logging_utils import ( # noqa: E402
log_debug,
log_info,
log_warning,
setup_logging,
log_error,
)
# Import exposed variables EARLY to ensure correct type registrations
# before any circular import chains or dynamic calls can cause premature registration
try:
import core.variables_engine # noqa: F401
except Exception as e:
print(f"[main] Warning: Failed to import variables_engine early: {e}", flush=True)
# Global restart flag
_restart_requested = False
_restart_event = None
# Global flag to preserve dev components state across restarts
_dev_components_enabled = False
def request_restart():
"""Request a graceful restart of the application."""
global _restart_requested, _restart_event
log_info("[main] Restart requested")
_restart_requested = True
if _restart_event:
_restart_event.set()
def set_dev_components_enabled(enabled: bool):
"""Set whether dev components should be loaded (preserved across restarts)."""
global _dev_components_enabled
_dev_components_enabled = enabled
log_info(f"[main] Dev components {'ENABLED' if enabled else 'DISABLED'} globally")
def are_dev_components_enabled() -> bool:
"""Check if dev components are enabled."""
return _dev_components_enabled
def cleanup_components():
"""Clean up all registered components (engines, plugins, interfaces)."""
try:
log_debug("[main] Starting component cleanup...")
# Let the core initializer handle cleanup of all registered components
# Cleanup Cortex engines
from core.cortex_registry import get_cortex_registry
registry = get_cortex_registry()
for engine_name in registry.get_available_engines():
try:
engine_instance = registry.get_engine(engine_name)
if engine_instance and hasattr(engine_instance, "cleanup"):
engine_instance.cleanup()
log_debug(f"[main] Cleaned up engine: {engine_name}")
except Exception as e:
log_warning(f"[main] Failed to cleanup engine {engine_name}: {e}")
log_info("[main] Component cleanup completed")
except Exception as e:
log_warning(f"[main] Component cleanup failed: {e}")
def signal_handler(signum, frame):
"""Handle termination signals gracefully."""
log_info(f"[main] Received signal {signum}, shutting down gracefully...")
# Clean up all components generically
cleanup_components()
log_info("[main] Shutdown complete")
sys.exit(0)
async def initialize_database():
"""Initialize database with proper async handling."""
log_info("[main] initialize_database() started")
# Verifica dei permessi dell'utente del database
async def check_permissions():
log_debug("[main] Checking database permissions...")
try:
async with get_conn_ctx() as conn:
async with conn.cursor() as cur:
if _get_db_type() == "postgres":
await cur.execute(
"""
SELECT
current_user AS role_name,
current_database() AS database_name,
has_database_privilege(current_user, current_database(), 'CONNECT') AS can_connect,
has_database_privilege(current_user, current_database(), 'CREATE') AS can_create,
has_schema_privilege(current_user, 'public', 'USAGE') AS public_usage,
has_schema_privilege(current_user, 'public', 'CREATE') AS public_create
"""
)
else:
await cur.execute("SHOW GRANTS FOR CURRENT_USER()")
grants = await cur.fetchall()
log_debug("[main] Database permissions check completed")
return grants
except Exception as e:
log_error(f"[main] Error checking database permissions: {repr(e)}")
raise
try:
grants = await check_permissions()
log_info(f"[main] Database user permissions: {grants}")
log_info("[main] Testing database connection...")
if not await test_connection():
log_error("[main] Database connection test failed")
return False
log_info("[main] Database connection test passed")
log_info("[main] Initializing database schema...")
await init_db()
log_info("[main] Database schema initialized")
# Persist bootstrap configurations to DB after initialization
log_debug("[main] Persisting bootstrap configurations...")
from core.config_manager import config_registry
await config_registry.persist_bootstrap_configs()
log_debug("[main] Bootstrap configurations persisted")
# NOTE: load_all_from_db() is called later in core_initializer.initialize_all()
# after all variable registrations are complete. Do not call it here as it would
# skip loading persona configurations due to incomplete registration.
# Blocklist table now handled by blocklist plugin
# log_info("[main] Initializing blocklist table...")
# await init_blocklist_table()
# log_info("[main] Blocklist table initialized")
log_info("[main] Database initialization completed successfully!")
return True
except Exception as e:
log_error(f"[main] Error in initialize_database(): {repr(e)}")
import traceback
traceback.print_exc()
return False
if __name__ == "__main__":
# Set up signal handlers for graceful shutdown
signal.signal(signal.SIGINT, signal_handler) # Ctrl+C
signal.signal(signal.SIGTERM, signal_handler) # Docker stop
setup_logging()
log_info("[main] Starting synth application...")
# 🌐 Show where the Webtop/VNC interface is available
host = os.environ.get("WEBVIEW_HOST", "localhost")
port = os.environ.get("WEBVIEW_PORT", "3000")
log_info(f"[vnc] Webtop GUI available at: http://{host}:{port}")
log_info("[main] Starting bot initialization...")
async def start_application():
"""Start the application and handle restart requests."""
global _restart_requested, _restart_event
# Test DB connectivity and initialize tables with retry mechanism
# This must be done in the main event loop to avoid creating separate pools
# Register main loop with notifier so it doesn't create new event loops
from core.notifier import _set_main_loop
loop = asyncio.get_running_loop()
_set_main_loop(loop)
max_retries = 30
retry_delay = 2
for attempt in range(max_retries):
try:
log_info(
f"[main] Attempting database connection (attempt {attempt + 1}/{max_retries})..."
)
# Initialize database async
if await initialize_database():
break
else:
raise Exception("Database initialization failed")
except Exception as e:
if attempt < max_retries - 1:
log_warning(
f"[main] Database connection attempt {attempt + 1} failed: {e}"
)
log_info(f"[main] Retrying in {retry_delay} seconds...")
await asyncio.sleep(retry_delay)
else:
log_error(
f"[main] Critical error during database initialization after {max_retries} attempts: {e}"
)
sys.exit(1)
while True:
_restart_requested = False
_restart_event = asyncio.Event()
# Initialize core components - they will auto-discover and load all interfaces/plugins/engines
try:
log_info("[main] Initializing core components...")
from core.core_initializer import core_initializer
# Restore dev components state if it was enabled before restart
if _dev_components_enabled:
log_info("[main] Restoring dev components enabled state...")
core_initializer.enable_dev_components(True)
await core_initializer.initialize_all()
log_info("[main] Core components initialized successfully")
# Start webui server if available
try:
from core.core_initializer import INTERFACE_REGISTRY
# If discovery populated the registry, use that instance.
if "synth_webui" in INTERFACE_REGISTRY:
webui_interface = INTERFACE_REGISTRY["synth_webui"]
if hasattr(webui_interface, "start"):
await webui_interface.start()
log_info("[main] WebUI interface started")
elif hasattr(webui_interface, "start_server_async"):
webui_interface.start_server_async()
log_info("[main] WebUI server started")
else:
# Fallback: attempt to import and initialize the core.webui module
# This ensures the Web UI is created even if discovery missed it
try:
import importlib
log_info(
"[main] synth_webui not found in INTERFACE_REGISTRY - attempting direct initialization"
)
webui_mod = importlib.import_module("core.webui")
if hasattr(webui_mod, "initialize_interface"):
webui_mod.initialize_interface()
# If the module created the instance it should be in the registry now
if "synth_webui" in INTERFACE_REGISTRY:
webui_interface = INTERFACE_REGISTRY["synth_webui"]
if hasattr(webui_interface, "start"):
await webui_interface.start()
log_info(
"[main] WebUI interface started (fallback initialization)"
)
elif hasattr(webui_interface, "start_server_async"):
webui_interface.start_server_async()
log_info(
"[main] WebUI server started (fallback initialization)"
)
else:
log_warning(
"[main] core.webui.initialize_interface() did not register synth_webui"
)
else:
log_warning(
"[main] core.webui has no initialize_interface() - cannot initialize Web UI"
)
except Exception as e:
log_warning(
f"[main] Fallback initialization of synth_webui failed: {e}"
)
except Exception as e:
log_warning(f"[main] Could not start webui interface: {e}")
# Start message queue consumer
from core import message_queue
asyncio.create_task(message_queue.run())
log_info("[main] Message queue consumer started")
except Exception as e:
log_error(
f"[main] Critical error initializing core components: {repr(e)}"
)
import traceback
traceback.print_exc()
sys.exit(1)
log_info("[main] All components auto-discovered and initialized")
# 🎯 Display startup summary after all components are ready (this should be the last message)
log_info("[main] All components initialized, displaying startup summary...")
core_initializer.display_startup_summary()
# Also display a quick resume even if some components are still loading
resume = core_initializer.get_system_resume()
log_info(
f"[main] 🎯 QUICK STATUS: {resume['successful']}/{resume['total_components']} components loaded, {resume['total_actions']} actions available"
)
# Keep the application running indefinitely (or until restart requested)
log_info(
"[main] Application startup completed successfully - entering main loop"
)
try:
# Wait for restart event or keyboard interrupt
await _restart_event.wait()
if _restart_requested:
log_info(
"[main] 🔄 Restart requested - cleaning up and restarting..."
)
# Cleanup components
cleanup_components()
# Clear registries
from core.core_initializer import (
INTERFACE_REGISTRY,
PLUGIN_REGISTRY,
)
INTERFACE_REGISTRY.clear()
PLUGIN_REGISTRY.clear()
# Clear Cortex registry
from core.cortex_registry import get_cortex_registry
cortex_registry = get_cortex_registry()
cortex_registry._engines.clear()
log_info("[main] ✅ Cleanup completed - restarting application...")
await asyncio.sleep(1) # Brief pause before restart
continue # Loop back to restart
except KeyboardInterrupt:
log_info("[main] Received shutdown signal, exiting...")
break
# Run the async application
asyncio.run(start_application())