"""Live Gemini Subprocess control endpoints.""" from __future__ import annotations import asyncio from fastapi import APIRouter, HTTPException, Query from Project.Sanad.config import BASE_DIR from Project.Sanad.vision import recognition_state router = APIRouter() _STATE_PATH = BASE_DIR / "data" / ".recognition_state.json" def _sub_or_503(): from Project.Sanad.main import live_sub if live_sub is None: raise HTTPException(503, "Live subprocess not available") return live_sub @router.get("/status") async def subprocess_status(): from Project.Sanad.main import live_sub # record_enabled is a live flag (recognition_state) the panel toggle drives; # surface it so the UI shows the current state even before a session starts. rec = bool(recognition_state.read(_STATE_PATH).record_enabled) if live_sub is None: return {"available": False, "state": "unavailable", "record_enabled": rec} return {**live_sub.status(), "record_enabled": rec} @router.post("/record") async def set_record(on: bool = Query(...)): """Toggle auto-recording of conversation turns to data/recordings/. Takes effect live (the voice child syncs its recorder) — no session restart.""" st = await asyncio.to_thread( recognition_state.mutate, _STATE_PATH, record_enabled=bool(on)) return {"ok": True, "record_enabled": st.record_enabled} @router.post("/start") async def start_subprocess(): live_sub = _sub_or_503() try: return await asyncio.to_thread(live_sub.start) except RuntimeError as exc: raise HTTPException(404, str(exc)) @router.post("/stop") async def stop_subprocess(): return await asyncio.to_thread(_sub_or_503().stop)