Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.
Rust agentai-agentapplecomputer-usecomputer-use-agentcontainerizationcuadesktop-automationhacktoberfestlumemacosmanusoperatorswiftvirtualizationvirtualization-frameworkwindowswindows-sandbox
Added cancel() to BaseLoop
Dillon DuPontd4f623efdf42
diff --git a/libs/agent/agent/core/base.py b/libs/agent/agent/core/base.pyindex 702be207e9..53c3aca9b1 100644--- a/libs/agent/agent/core/base.py+++ b/libs/agent/agent/core/base.py@@ -131,6 +131,15 @@ class BaseLoop(ABC):An async generator that yields agent responses"""raise NotImplementedError++ @abstractmethod+ async def cancel(self) -> None:+ """Cancel the currently running agent loop task.++ This method should stop any ongoing processing in the agent loop+ and clean up resources appropriately.+ """+ raise NotImplementedError############################################ EXPERIMENT AND TRAJECTORY MANAGEMENTdiff --git a/libs/agent/agent/providers/anthropic/loop.py b/libs/agent/agent/providers/anthropic/loop.pyindex 130a43cb26..8b7ef1d683 100644--- a/libs/agent/agent/providers/anthropic/loop.py+++ b/libs/agent/agent/providers/anthropic/loop.py@@ -101,6 +101,7 @@ class AnthropicLoop(BaseLoop):self.tool_manager = Noneself.callback_manager = Noneself.queue = asyncio.Queue() # Initialize queue+ self.loop_task = None # Store the loop task for cancellation# Initialize handlersself.api_handler = AnthropicAPIHandler(self)@@ -169,7 +170,7 @@ class AnthropicLoop(BaseLoop):logger.info("Client initialized successfully")# Start loop in background task- loop_task = asyncio.create_task(self._run_loop(queue, messages))+ self.loop_task = asyncio.create_task(self._run_loop(queue, messages))# Process and yield messages as they arrivewhile True:@@ -184,7 +185,7 @@ class AnthropicLoop(BaseLoop):continue# Wait for loop to complete- await loop_task+ await self.loop_task# Send completion messageyield {@@ -200,6 +201,31 @@ class AnthropicLoop(BaseLoop):"content": f"Error: {str(e)}","metadata": {"title": "❌ Error"},}++ async def cancel(self) -> None:+ """Cancel the currently running agent loop task.++ This method stops the ongoing processing in the agent loop+ by cancelling the loop_task if it exists and is running.+ """+ if self.loop_task and not self.loop_task.done():+ logger.info("Cancelling Anthropic loop task")+ self.loop_task.cancel()+ try:+ # Wait for the task to be cancelled with a timeout+ await asyncio.wait_for(self.loop_task, timeout=2.0)+ except asyncio.TimeoutError:+ logger.warning("Timeout while waiting for loop task to cancel")+ except asyncio.CancelledError:+ logger.info("Loop task cancelled successfully")+ except Exception as e:+ logger.error(f"Error while cancelling loop task: {str(e)}")+ finally:+ # Put None in the queue to signal any waiting consumers to stop+ await self.queue.put(None)+ logger.info("Anthropic loop task cancelled")+ else:+ logger.info("No active Anthropic loop task to cancel")############################################ AGENT LOOP IMPLEMENTATIONdiff --git a/libs/agent/agent/providers/omni/loop.py b/libs/agent/agent/providers/omni/loop.pyindex 18e0375f70..b77194500f 100644--- a/libs/agent/agent/providers/omni/loop.py+++ b/libs/agent/agent/providers/omni/loop.py@@ -105,6 +105,7 @@ class OmniLoop(BaseLoop):# Set API client attributesself.client = Noneself.retry_count = 0+ self.loop_task = None # Store the loop task for cancellation# Initialize handlersself.api_handler = OmniAPIHandler(loop=self)@@ -583,7 +584,31 @@ class OmniLoop(BaseLoop):# Initialize the message manager with the provided messagesself.message_manager.messages = messages.copy()logger.info(f"Starting OmniLoop run with {len(self.message_manager.messages)} messages")++ # Create a task to run the loop+ self.loop_task = asyncio.create_task(self._run_loop(messages))+ # Yield from the loop task+ try:+ async for response in self.loop_task:+ yield response+ except Exception as e:+ logger.error(f"Error in run method: {str(e)}")+ yield {+ "role": "assistant",+ "content": f"Error: {str(e)}",+ "metadata": {"title": "❌ Error"},+ }++ async def _run_loop(self, messages: List[Dict[str, Any]]) -> AsyncGenerator[AgentResponse, None]:+ """Internal method to run the agent loop with provided messages.++ Args:+ messages: List of messages in standard OpenAI format++ Yields:+ Agent response format+ """# Continue running until explicitly told to stoprunning = Trueturn_created = False@@ -688,7 +713,7 @@ class OmniLoop(BaseLoop):except Exception as e:attempt += 1- error_msg = f"Error in run method (attempt {attempt}/{max_attempts}): {str(e)}"+ error_msg = f"Error in _run_loop method (attempt {attempt}/{max_attempts}): {str(e)}"logger.error(error_msg)# If this is our last attempt, provide more info about the error@@ -696,12 +721,36 @@ class OmniLoop(BaseLoop):logger.error(f"Maximum retry attempts reached. Last error was: {str(e)}")yield {- "error": str(e),+ "role": "assistant",+ "content": f"Error: {str(e)}","metadata": {"title": "❌ Error"},}# Create a brief delay before retryingawait asyncio.sleep(1)++ async def cancel(self) -> None:+ """Cancel the currently running agent loop task.++ This method stops the ongoing processing in the agent loop+ by cancelling the loop_task if it exists and is running.+ """+ if self.loop_task and not self.loop_task.done():+ logger.info("Cancelling Omni loop task")+ self.loop_task.cancel()+ try:+ # Wait for the task to be cancelled with a timeout+ await asyncio.wait_for(self.loop_task, timeout=2.0)+ except asyncio.TimeoutError:+ logger.warning("Timeout while waiting for loop task to cancel")+ except asyncio.CancelledError:+ logger.info("Loop task cancelled successfully")+ except Exception as e:+ logger.error(f"Error while cancelling loop task: {str(e)}")+ finally:+ logger.info("Omni loop task cancelled")+ else:+ logger.info("No active Omni loop task to cancel")async def process_model_response(self, response_text: str) -> Optional[Dict[str, Any]]:"""Process model response to extract tool calls.diff --git a/libs/agent/agent/providers/openai/loop.py b/libs/agent/agent/providers/openai/loop.pyindex baff6ff004..87719d1b16 100644--- a/libs/agent/agent/providers/openai/loop.py+++ b/libs/agent/agent/providers/openai/loop.py@@ -87,6 +87,7 @@ class OpenAILoop(BaseLoop):self.acknowledge_safety_check_callback = acknowledge_safety_check_callbackself.queue = asyncio.Queue() # Initialize queueself.last_response_id = None # Store the last response ID across runs+ self.loop_task = None # Store the loop task for cancellation# Initialize handlersself.api_handler = OpenAIAPIHandler(self)@@ -138,7 +139,7 @@ class OpenAILoop(BaseLoop):await self.tool_manager.initialize()# Start loop in background task- loop_task = asyncio.create_task(self._run_loop(queue, messages))+ self.loop_task = asyncio.create_task(self._run_loop(queue, messages))# Process and yield messages as they arrivewhile True:@@ -153,7 +154,7 @@ class OpenAILoop(BaseLoop):continue# Wait for loop to complete- await loop_task+ await self.loop_task# Send completion messageyield {@@ -169,6 +170,31 @@ class OpenAILoop(BaseLoop):"content": f"Error: {str(e)}","metadata": {"title": "❌ Error"},}++ async def cancel(self) -> None:+ """Cancel the currently running agent loop task.++ This method stops the ongoing processing in the agent loop+ by cancelling the loop_task if it exists and is running.+ """+ if self.loop_task and not self.loop_task.done():+ logger.info("Cancelling OpenAI loop task")+ self.loop_task.cancel()+ try:+ # Wait for the task to be cancelled with a timeout+ await asyncio.wait_for(self.loop_task, timeout=2.0)+ except asyncio.TimeoutError:+ logger.warning("Timeout while waiting for loop task to cancel")+ except asyncio.CancelledError:+ logger.info("Loop task cancelled successfully")+ except Exception as e:+ logger.error(f"Error while cancelling loop task: {str(e)}")+ finally:+ # Put None in the queue to signal any waiting consumers to stop+ await self.queue.put(None)+ logger.info("OpenAI loop task cancelled")+ else:+ logger.info("No active OpenAI loop task to cancel")############################################ AGENT LOOP IMPLEMENTATIONdiff --git a/libs/agent/agent/providers/uitars/loop.py b/libs/agent/agent/providers/uitars/loop.pyindex 848e35049f..3766cd92d3 100644--- a/libs/agent/agent/providers/uitars/loop.py+++ b/libs/agent/agent/providers/uitars/loop.py@@ -93,6 +93,7 @@ class UITARSLoop(BaseLoop):# Set API client attributesself.client = Noneself.retry_count = 0+ self.loop_task = None # Store the loop task for cancellation# Initialize visualization helperself.viz_helper = VisualizationHelper(agent=self)@@ -465,7 +466,31 @@ class UITARSLoop(BaseLoop):# Initialize the message manager with the provided messagesself.message_manager.messages = messages.copy()logger.info(f"Starting UITARSLoop run with {len(self.message_manager.messages)} messages")++ # Create a task to run the loop+ self.loop_task = asyncio.create_task(self._run_loop(messages))+ # Yield from the loop task+ try:+ async for response in self.loop_task:+ yield response+ except Exception as e:+ logger.error(f"Error in run method: {str(e)}")+ yield {+ "role": "assistant",+ "content": f"Error: {str(e)}",+ "metadata": {"title": "❌ Error"},+ }++ async def _run_loop(self, messages: List[Dict[str, Any]]) -> AsyncGenerator[AgentResponse, None]:+ """Internal method to run the agent loop with provided messages.++ Args:+ messages: List of messages in standard OpenAI format++ Yields:+ Agent response format+ """# Continue running until explicitly told to stoprunning = Trueturn_created = False@@ -558,6 +583,29 @@ class UITARSLoop(BaseLoop):# Create a brief delay before retryingawait asyncio.sleep(1)+ async def cancel(self) -> None:+ """Cancel the currently running agent loop task.++ This method stops the ongoing processing in the agent loop+ by cancelling the loop_task if it exists and is running.+ """+ if self.loop_task and not self.loop_task.done():+ logger.info("Cancelling UITARS loop task")+ self.loop_task.cancel()+ try:+ # Wait for the task to be cancelled with a timeout+ await asyncio.wait_for(self.loop_task, timeout=2.0)+ except asyncio.TimeoutError:+ logger.warning("Timeout while waiting for loop task to cancel")+ except asyncio.CancelledError:+ logger.info("Loop task cancelled successfully")+ except Exception as e:+ logger.error(f"Error while cancelling loop task: {str(e)}")+ finally:+ logger.info("UITARS loop task cancelled")+ else:+ logger.info("No active UITARS loop task to cancel")+############################################ UTILITY METHODS###########################################