| 11 | }; |
| 12 | |
| 13 | export async function executeTool<Input>( |
| 14 | tool: ExecutableTool<Input>, |
| 15 | context: TestExecutableToolContext<Input>, |
| 16 | ): Promise<ExecutableToolResult> { |
| 17 | const { args, ...executionContext } = context; |
| 18 | let execution: ToolExecution; |
| 19 | try { |
| 20 | const resolved = tool.resolveExecution(args); |
| 21 | execution = isPromiseLike(resolved) ? await resolved : resolved; |
| 22 | } catch (error) { |
| 23 | const output = |
| 24 | error instanceof PathSecurityError |
| 25 | ? error.message |
| 26 | : `Tool "${tool.name}" failed to resolve execution: ${ |
| 27 | error instanceof Error ? error.message : String(error) |
| 28 | }`; |
| 29 | return { isError: true, output }; |
| 30 | } |
| 31 | if (execution.isError === true) return execution; |
| 32 | return execution.execute(executionContext); |
| 33 | } |
| 34 | |
| 35 | function isPromiseLike(value: ToolExecution | Promise<ToolExecution>): value is Promise<ToolExecution> { |
| 36 | return typeof (value as Promise<ToolExecution>).then === 'function'; |