Fix: Handle dict input for tool arguments in call_tool

This commit is contained in:
cyclop-bot
2025-06-02 15:56:43 -05:00
parent 7ad100e7a7
commit 4adef5754e
+19 -5
View File
@@ -78,16 +78,30 @@ class BaseTelegramInferenceBot(ABC):
def call_tool(self, function_call_name, function_call_arguments): def call_tool(self, function_call_name, function_call_arguments):
function_name = function_call_name function_name = function_call_name
try: function_args = None
function_args = json.loads(function_call_arguments if function_call_arguments is not None else "{}") if isinstance(function_call_arguments, dict):
except json.JSONDecodeError as e: function_args = function_call_arguments
logging.error(f"Error decoding function call arguments for {function_call_name}: {e}. Arguments: {function_call_arguments}") elif isinstance(function_call_arguments, str):
return f"Error: Malformed arguments for tool call: {e}" try:
function_args = json.loads(function_call_arguments)
except json.JSONDecodeError as e:
logging.error(f"Error decoding function call arguments (string) for {function_call_name}: {e}. Arguments: {function_call_arguments}")
return f"Error: Malformed arguments for tool call: {e}"
else: # Handle cases where arguments might be None or other unexpected types
if function_call_arguments is None:
function_args = {} # Default to empty dict if arguments are None
else:
logging.error(f"Unexpected type for function_call_arguments for {function_call_name}: {type(function_call_arguments)}. Arguments: {function_call_arguments}")
return f"Error: Invalid argument type for tool call: {type(function_call_arguments)}"
for tool in self.tools: for tool in self.tools:
for function in tool.get_functions(): for function in tool.get_functions():
if function["function"]["name"] == function_name: if function["function"]["name"] == function_name:
try: try:
# Ensure function_args is a dictionary before unpacking
if not isinstance(function_args, dict):
logging.error(f"Internal error: function_args not a dict for {function_name} before execution. Args: {function_args}")
return f"Internal error preparing arguments for tool {function_name}."
return tool.execute(function_name, **function_args) return tool.execute(function_name, **function_args)
except Exception as e: except Exception as e:
logging.error(f"Error executing tool {function_name} with args {function_args}: {e}") logging.error(f"Error executing tool {function_name} with args {function_args}: {e}")