mirror of
https://github.com/trycua/computer.git
synced 2026-01-06 05:20:02 -06:00
Added ToolError and IllegalArgumentError
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user