Execute a single tool call with semaphore control.
(call: CTPToolCall)
| 64 | results: list[CTPToolResult] = [] |
| 65 | |
| 66 | async def execute_single(call: CTPToolCall) -> CTPToolResult: |
| 67 | """Execute a single tool call with semaphore control.""" |
| 68 | start_time = datetime.now(UTC) |
| 69 | |
| 70 | async with sem: |
| 71 | # Invoke start callback |
| 72 | if on_tool_start: |
| 73 | try: |
| 74 | await on_tool_start(call) |
| 75 | except Exception as e: |
| 76 | logger.warning( |
| 77 | f"on_tool_start callback failed for {call.tool}: {e}" |
| 78 | ) |
| 79 | |
| 80 | # Execute the tool |
| 81 | tool_result = await manager.execute_tool( |
| 82 | call.tool, |
| 83 | call.arguments, |
| 84 | namespace=call.namespace if call.namespace != "default" else None, |
| 85 | timeout=effective_timeout, |
| 86 | ) |
| 87 | |
| 88 | end_time = datetime.now(UTC) |
| 89 | |
| 90 | # Convert ToolCallResult to CTPToolResult |
| 91 | ctp_result = CTPToolResult( |
| 92 | id=call.id, |
| 93 | tool=call.tool, |
| 94 | result=tool_result.result if tool_result.success else None, |
| 95 | error=tool_result.error if not tool_result.success else None, |
| 96 | start_time=start_time, |
| 97 | end_time=end_time, |
| 98 | machine=platform.node(), |
| 99 | pid=os.getpid(), |
| 100 | ) |
| 101 | |
| 102 | # Invoke result callback |
| 103 | if on_tool_result: |
| 104 | try: |
| 105 | await on_tool_result(ctp_result) |
| 106 | except Exception as e: |
| 107 | logger.warning( |
| 108 | f"on_tool_result callback failed for {call.tool}: {e}" |
| 109 | ) |
| 110 | |
| 111 | return ctp_result |
| 112 | |
| 113 | # Create all tasks and execute in parallel |
| 114 | tasks = [asyncio.create_task(execute_single(call)) for call in calls] |
no test coverage detected