"""Main Orchestration Loop — the nervous system of OmniNPC. Runs three concurrent async tasks: 1. _perception_loop: Nano Omni inference at configured FPS 2. _strategy_loop: Super inference every N seconds 3. _dashboard_loop: Metrics display refresh at 10Hz """ from __future__ import annotations import asyncio import logging import time from typing import Optional from actions.action_schema import ActionResult from capture.capture_manager import CaptureManager from dashboard.overlay import Dashboard from inference.nano_omni_agent import NanoOmniAgent from inference.super_agent import SuperAgent from metrics.latency_tracker import LatencyTracker from metrics.metrics_logger import MetricsLogger from orchestrator.hierarchy_manager import HierarchyManager from orchestrator.pipeline_state import PipelineState logger = logging.getLogger(__name__) class OmniNPCOrchestrator: """Main orchestrator: wires capture → inference → execution → metrics.""" def __init__(self, executor, config) -> None: self.config = config self.capture = CaptureManager(config, window_title=config.game.window_title) self.nano = NanoOmniAgent(config) self.super_agent = SuperAgent(config) self.hierarchy = HierarchyManager(self.super_agent, config) self.state = PipelineState() self.tracker = LatencyTracker() self.logger = MetricsLogger(config) self.dashboard = Dashboard(self.state, self.tracker) self.executor = executor self._running = False async def run(self) -> None: """Start all loops. Blocks until stopped.""" self._running = True await self.capture.start() try: await asyncio.gather( self._perception_loop(), self._strategy_loop(), self._dashboard_loop(), ) except asyncio.CancelledError: logger.info("Orchestrator cancelled") except KeyboardInterrupt: logger.info("Orchestrator interrupted") finally: self._running = False await self.capture.stop() self.logger.close() logger.info("Orchestrator shut down") async def _perception_loop(self) -> None: """Fast loop: capture → infer → execute → record.""" logger.info( "Perception loop started — interval=%.3fs", self.config.orchestrator.perception_interval, ) while self._running: t_start = time.perf_counter() frame, audio = await self.capture.get_latest() if frame is None: await asyncio.sleep(0.1) continue directive = self.hierarchy.current_directive try: action = await self.nano.perceive_and_act(frame, audio, directive) logger.info( "Nano → action=%s mag=%.2f conf=%.2f say=%r", action.action.value, action.magnitude, action.confidence, (action.say or "")[:40], ) except Exception as exc: logger.error("Nano inference error: %s", exc) from actions.action_schema import safe_idle_result action = safe_idle_result(frame.timestamp) try: await self.executor.execute(action) except Exception as exc: logger.error("Execute error: %s", exc) self.state.update(action, frame, audio) self.tracker.record(frame.timestamp, time.perf_counter(), action) self.logger.log_action(action, self.tracker.get_stats_dict()) self.dashboard.add_token_entry(action) elapsed = time.perf_counter() - t_start sleep = max(0.0, self.config.orchestrator.perception_interval - elapsed) await asyncio.sleep(sleep) async def _strategy_loop(self) -> None: """Slow loop: every N seconds, call Super for a new directive.""" logger.info( "Strategy loop started — interval=%.1fs", self.config.orchestrator.strategy_interval, ) while self._running: await asyncio.sleep(self.config.orchestrator.strategy_interval) if not self._running: break try: summary = self.state.get_scene_summary() recent = self.state.get_recent_actions(n=3) directive = await self.super_agent.get_directive( summary, self.state.mission_context, recent ) self.hierarchy.update_directive(directive) logger.info("Directive updated: %s", directive.directive) except Exception as exc: logger.error("Strategy loop error: %s", exc) async def _dashboard_loop(self) -> None: """Dashboard refresh loop at 10Hz.""" while self._running: try: self.dashboard.render() except Exception: pass await asyncio.sleep(0.1) def stop(self) -> None: """Signal all loops to stop.""" self._running = False