Added ToolError and IllegalArgumentError

This commit is contained in:
Dillon DuPont
2025-08-19 12:27:09 -04:00
parent e783ab5876
commit 6f5b7363f1
2 changed files with 29 additions and 1 deletions

View File

@@ -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)

View File

@@ -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):