import inspect from typing import Optional from agentkit.types import OnTaskStartCallback, OnTaskProgressCallback, OnTaskCompleteCallback class AgentKitTaskProgress: def __init__(self): self.on_task_start_listeners: list[OnTaskStartCallback] = [] self.on_task_progress_listeners: list[OnTaskProgressCallback] = [] self.on_task_complete_listeners: list[OnTaskCompleteCallback] = [] def add_on_task_start_listener(self, listener: OnTaskStartCallback) -> None: self.on_task_start_listeners.append(listener) def add_on_task_progress_listener(self, listener: OnTaskProgressCallback) -> None: self.on_task_progress_listeners.append(listener) def add_on_task_complete_listener(self, listener: OnTaskCompleteCallback) -> None: self.on_task_complete_listeners.append(listener) async def _emit(self, listeners: list, *args) -> None: for listener in listeners: ret = listener(*args) if inspect.isawaitable(ret): await ret async def task_start(self, task_title: str, task_description: str = "") -> None: await self._emit(self.on_task_start_listeners, task_title, task_description) async def task_progress(self, current: int, total: Optional[int] = None, task_description: Optional[str] = None) -> None: await self._emit(self.on_task_progress_listeners, current, total, task_description) async def task_progress_infinite(self, task_description: Optional[str] = None) -> None: await self._emit(self.on_task_progress_listeners, 0, 0, task_description) async def task_complete(self) -> None: await self._emit(self.on_task_complete_listeners)