| 7 | |
| 8 | |
| 9 | class ToolInvoker: |
| 10 | def __init__(self, client): |
| 11 | self.__client = client |
| 12 | |
| 13 | def get_tools(self) -> List[dict]: |
| 14 | """Fetch tools from the API.""" |
| 15 | tool_response = self.__client.list_tools( |
| 16 | {"params": {"clusterId": self.__client.cluster_id}} |
| 17 | ) |
| 18 | |
| 19 | if tool_response.get("status") != 200: |
| 20 | raise AgentRPCError( |
| 21 | f"Failed to list AgentRPC tools: {tool_response.get('status')}", |
| 22 | status_code=tool_response.get("status"), |
| 23 | response=tool_response, |
| 24 | ) |
| 25 | |
| 26 | return tool_response.get("body", []) |
| 27 | |
| 28 | def execute_tool(self, function_name: str, arguments: dict) -> str: |
| 29 | """Execute a tool by function name and arguments.""" |
| 30 | try: |
| 31 | job_result = self.__client.create_and_poll_job( |
| 32 | cluster_id=self.__client.cluster_id, |
| 33 | tool_name=function_name, |
| 34 | input_data=arguments, |
| 35 | ) |
| 36 | |
| 37 | status = job_result.get("status") |
| 38 | if status != "done": |
| 39 | if status == "failure": |
| 40 | raise AgentRPCError( |
| 41 | f"Tool execution failed: {job_result.get('result')}" |
| 42 | ) |
| 43 | raise AgentRPCError(f"Unexpected job status: {status}") |
| 44 | |
| 45 | return f"{job_result.get('resultType')}: {job_result.get('result', 'Function executed successfully but returned no result.')}" |
| 46 | except Exception as e: |
| 47 | raise AgentRPCError(f"Error executing function: {str(e)}") |
| 48 | |
| 49 | |
| 50 | class OpenAIIntegration: |