Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
167 lines (138 loc) · 5.76 KB
/
Copy pathmain.py
File metadata and controls
167 lines (138 loc) · 5.76 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
import sys
# Enable DPI awareness before Qt, MSS, or PyAutoGUI initialize so screenshot pixels
# and click coordinates refer to the same physical desktop space.
from core.utils.dpi import enable_dpi_awareness
enable_dpi_awareness()
import time
import threading
from PyQt6.QtWidgets import QApplication
from core.vision.window_manager import WindowManager
from core.vision.capture import capture_module
from core.vision.change_detector import change_detector
from core.ui.automation import ui_scanner
from core.ocr.engine import ocr_engine
from core.engine.triggers import trigger_engine
from core.engine.brain import brain
from core.engine.hotkeys import hotkey_manager
from core.ocr.stt_engine import stt_engine
from core.engine.voice_controller import voice_controller
from core.engine.action_orchestrator import action_orchestrator
from core.vision.audio_capture import audio_capture
from core.state.manager import state_manager
from core.utils.logger import logger
from core.engine.event_bus import bus
from core.ui.dashboard import AyEyeDashboard
from core.ui.overlay import VisualOverlay
from core.ui.pet_widget import AyEyePet
class Orchestrator:
def __init__(self):
self.running = False
self.thread = None
self.last_heartbeat = 0
# Setup subscribers
bus.subscribe("SCREEN_UPDATED", self.on_screen_update)
bus.subscribe("AI_TRIGGERED", lambda d: logger.log_event("AI_CALL_REQUESTED", d))
def start(self):
self.running = True
hotkey_manager.start()
# Preload heavy STT model asynchronously to prevent first-speech lag
def _preload():
try:
stt_engine._lazy_load()
except Exception as e:
logger.logger.error(f"Orchestrator: STT Preload failed - {e}")
threading.Thread(target=_preload, daemon=True).start()
# Launch engine loop in background
self.thread = threading.Thread(target=self.loop, daemon=True)
self.thread.start()
# Startup Greeting (with small delay for TTS readiness)
def _greet():
time.sleep(2)
bus.publish("AI_GREETING", {"text": "System online. I am Ay-Eye, standing by for your commands."})
threading.Thread(target=_greet, daemon=True).start()
logger.log_event("SYSTEM_STARTED")
def stop(self):
self.running = False
if self.thread:
self.thread.join()
def loop(self):
while self.running:
start_time = time.time()
# 1. Window Detection
win_info = WindowManager.get_active_window_info()
if not win_info or win_info.get("is_sensitive"):
time.sleep(1)
continue
# 2. Capture
rect = win_info.get("rect")
if not rect or rect[2] <= 0 or rect[3] <= 0:
time.sleep(1)
continue
img = capture_module.capture_region(rect)
# 3. Change Detection (Emits SCREEN_UPDATED if changed)
if img:
change_detector.process(img)
# 4. Idle Check
trigger_engine.check_idle()
# Heartbeat every 5s
if time.time() - self.last_heartbeat > 5:
data = {"active_window": win_info["window"]}
logger.log_event("HEARTBEAT", data)
bus.publish("HEARTBEAT", data)
self.last_heartbeat = time.time()
# Performance Guardrail: Max 2Hz loop
elapsed = time.time() - start_time
sleep_time = max(0.5 - elapsed, 0.1)
time.sleep(sleep_time)
logger.log_performance("MAIN_LOOP", int(elapsed * 1000))
def on_screen_update(self, image):
# Triggered when screen actually changes
ui_elements = ui_scanner.scan_active_window()
text = ocr_engine.process(image)
if text:
state_manager.update(
ui_elements=ui_elements,
ocr_text=text
)
trigger_engine.check_error(text)
else:
state_manager.update(ui_elements=ui_elements)
from core.config import sys_config
from core.vision.live_perception import live_perception
if __name__ == "__main__":
# 1. Initialize QApplication on the MAIN thread
app = QApplication(sys.argv)
app.setQuitOnLastWindowClosed(False)
# 1.5 Start Live Perception if enabled
if sys_config.get("live_perception_enabled"):
live_perception.start()
# 2. Initialize Overlay (Highlighter)
overlay = VisualOverlay()
# 3. Initialize Dashboard (Status Bar)
dashboard = AyEyeDashboard(overlay=overlay)
# 3.5 Initialize the Desktop Pet — a frameless, always-on-top
# companion that mirrors agent state. Click toggles the dashboard
# command panel; double-click triggers ghost-typing dictation.
pet = AyEyePet()
pet.dashboard_toggle_requested.connect(
lambda: bus.publish("TOGGLE_COMMAND_PANEL")
)
pet.ghost_typing_requested.connect(
lambda: bus.publish("VOICE_INPUT_RECEIVED", "start ghost typing")
)
if pet._settings.visible:
# Use show_pet() (not raw QWidget.show()) so the
# PET_VISIBILITY_CHANGED event fires and the dashboard knows
# to hide its IDLE/SYSTEM pill bar. Calling .show() directly
# would leave the pill bar visible on top of the pet.
pet.show_pet()
# 4. Start the Orchestrator Engine (Background Thread)
orch = Orchestrator()
orch.start()
# 5. Enter the Qt Main Loop
try:
sys.exit(app.exec())
except KeyboardInterrupt:
live_perception.stop()
orch.stop()
sys.exit(0)