from typing import Any, List, Optional from tools.text import normalize_message class UIController: def __init__( self, logger: Any, chat_handler: Any, agent: Any = None, startup_error: Optional[str] = None, ): self.logger = logger self.chat_handler = chat_handler self.agent = agent self.startup_error = startup_error def status(self) -> str: if self.startup_error: return f"startup_error: {self.startup_error}" if self.agent is None: return "startup_error: agent unavailable" state = self.agent.status_snapshot() running = "running" if state["running"] else "stopped" return f"state={running} last_status={state['last_status']} dry_run={state['dry_run']}" def start(self) -> str: if self.startup_error: return f"startup_error: {self.startup_error}" if self.agent is None: return "startup_error: agent unavailable" result = self.agent.start() return f"agent {result}" def stop(self) -> str: if self.startup_error: return f"startup_error: {self.startup_error}" if self.agent is None: return "startup_error: agent unavailable" result = self.agent.stop() return f"agent {result}" def logs(self) -> str: return self.logger.snapshot() def chat_send(self, message: str, history: Optional[List[Any]]) -> tuple[str, List[Any]]: try: if self.chat_handler is None: clean = normalize_message(message) if not clean: return "", history or [] return "", (history or []) + [ {"role": "user", "content": clean}, {"role": "assistant", "content": "startup_error: chat unavailable"}, ] return self.chat_handler.send(message, history) except Exception as exc: # noqa: BLE001 self.logger.error("Manual chat failed", {"error": str(exc)}) clean = normalize_message(message) if not clean: return "", history or [] return "", (history or []) + [ {"role": "user", "content": clean}, {"role": "assistant", "content": f"error: {exc}"}, ]