Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

Added ToolError and IllegalArgumentError

Dillon DuPont6f5b7363f179
diff --git a/libs/python/agent/agent/agent.py b/libs/python/agent/agent/agent.py
index 7f30166f5d..81164e4255 100644
--- a/libs/python/agent/agent/agent.py
+++ b/libs/python/agent/agent/agent.py
@@ -7,7 +7,12 @@ from typing import Dict, List, Any, Optional, AsyncGenerator, Union, cast, Calla
from litellm.responses.utils import Usage
-from .types import Messages, AgentCapability
+from .types import (
+ Messages,
+ AgentCapability,
+ ToolError,
+ IllegalArgumentError
+)
from .decorators import find_agent_config
import json
import litellm
@@ -30,6 +35,14 @@ from .computers import (
make_computer_handler
)
+def is_callable_with(f, *args, **kwargs):
+ """Check if function can be called with given arguments."""
+ try:
+ inspect.signature(f).bind(*args, **kwargs)
+ return True
+ except TypeError:
+ return False
+
def get_json(obj: Any, max_depth: int = 10) -> Any:
def custom_serializer(o: Any, depth: int = 0, seen: Optional[Set[int]] = None) -> Any:
if seen is None:
@@ -439,6 +452,8 @@ class ComputerAgent:
# Execute the computer action
computer_method = getattr(computer, action_type, None)
if computer_method:
+ if not is_callable_with(computer_method, **action_args):
+ raise IllegalArgumentError(f"Invalid arguments for computer method {action_type}: {action_args}")
await computer_method(**action_args)
else:
print(f"Unknown computer action: {action_type}")
@@ -493,6 +508,10 @@ class ComputerAgent:
args = json.loads(item.get("arguments"))
+ # Validate arguments before execution
+ if not is_callable_with(function, **args):
+ raise IllegalArgumentError(f"Invalid arguments for function {item.get('name')}: {args}")
+
# Execute function - use asyncio.to_thread for non-async functions
if inspect.iscoroutinefunction(function):
result = await function(**args)
diff --git a/libs/python/agent/agent/types.py b/libs/python/agent/agent/types.py
index 23946c863b..f47c92861a 100644
--- a/libs/python/agent/agent/types.py
+++ b/libs/python/agent/agent/types.py
@@ -16,6 +16,15 @@ Tools = Optional[Iterable[ToolParam]]
AgentResponse = ResponsesAPIResponse
AgentCapability = Literal["step", "click"]
+# Exception types
+class ToolError(RuntimeError):
+ """Base exception for tool-related errors"""
+ pass
+
+class IllegalArgumentError(ToolError):
+ """Exception raised when function arguments are invalid"""
+ pass
+
# Agent config registration
class AgentConfigInfo(BaseModel):