( fn: ToolRegistrationInput<any>["handler"], args: Parameters<ToolRegistrationInput<any>["handler"]>, )
| 9 | }; |
| 10 | |
| 11 | export const executeFn = async ( |
| 12 | fn: ToolRegistrationInput<any>["handler"], |
| 13 | args: Parameters<ToolRegistrationInput<any>["handler"]>, |
| 14 | ): Promise<Result> => { |
| 15 | const start = Date.now(); |
| 16 | try { |
| 17 | const result = await fn(...args); |
| 18 | |
| 19 | const interupt = extractInterrupt(result); |
| 20 | |
| 21 | if (interupt) { |
| 22 | return { |
| 23 | content: interupt, |
| 24 | type: "interrupt", |
| 25 | functionExecutionTime: Date.now() - start, |
| 26 | }; |
| 27 | } |
| 28 | |
| 29 | return { |
| 30 | content: result, |
| 31 | type: "resolution", |
| 32 | functionExecutionTime: Date.now() - start, |
| 33 | }; |
| 34 | } catch (e) { |
| 35 | const interupt = extractInterrupt(e); |
| 36 | if (interupt) { |
| 37 | return { |
| 38 | content: interupt, |
| 39 | type: "interrupt", |
| 40 | functionExecutionTime: Date.now() - start, |
| 41 | }; |
| 42 | } |
| 43 | const functionExecutionTime = Date.now() - start; |
| 44 | if (e instanceof Error) { |
| 45 | return { |
| 46 | content: serializeError(e), |
| 47 | type: "rejection", |
| 48 | functionExecutionTime, |
| 49 | }; |
| 50 | } else if (typeof e === "string") { |
| 51 | return { |
| 52 | content: serializeError(new Error(e)), |
| 53 | type: "rejection", |
| 54 | functionExecutionTime, |
| 55 | }; |
| 56 | } else { |
| 57 | return { |
| 58 | content: new Error( |
| 59 | "Inferable encountered an unexpected error type. Make sure you are throwing an Error object.", |
| 60 | ), |
| 61 | type: "rejection", |
| 62 | functionExecutionTime, |
| 63 | }; |
| 64 | } |
| 65 | } |
| 66 | }; |
no test coverage detected