File size: 2,079 Bytes
f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 f9474bf dc0fe62 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import os
import time
from typing import Optional
from backend.database import SessionLocal, Tool, ToolExecution, Connection
from backend.composio_client import get_composio
class ToolExecutor:
async def execute(
self,
tool_id: str,
params: dict,
connection_id: Optional[str] = None,
user_id: str = "",
source: str = "api"
) -> dict:
start = time.time()
status = "success"
error_message = None
result = None
composio = get_composio()
try:
if composio:
resp = composio.tools.execute(
slug=tool_id,
arguments=params,
connected_account_id=connection_id,
user_id=user_id,
dangerously_skip_version_check=True,
)
result = resp.get("data", resp)
else:
result = {"tool_id": tool_id, "params": params, "message": "Mock execution - set COMPOSIO_API_KEY for real execution"}
except Exception as e:
status = "error"
error_message = str(e)
result = {"error": error_message}
latency = int((time.time() - start) * 1000)
self._log_execution(user_id, tool_id, connection_id, params, result, status, latency, error_message, source)
return {"execution_id": f"exec_{int(time.time()*1000)}", "status": status, "result": result, "latency_ms": latency, "error": error_message}
def _log_execution(self, user_id, tool_id, connection_id, params, result, status, latency, error, source):
if not user_id:
return
db = SessionLocal()
try:
db.add(ToolExecution(
user_id=user_id, tool_id=tool_id, connection_id=connection_id,
input_params=params, output_result=result, status=status,
latency_ms=latency, error_message=error, source=source
))
db.commit()
finally:
db.close()
executor = ToolExecutor()
|