|
| 1 | +import { |
| 2 | + executeJavaScript, |
| 3 | + executePython, |
| 4 | + executeTypeScript, |
| 5 | +} from "@exec0/sdk"; |
| 6 | +import { tool } from "ai"; |
| 7 | +import { z } from "zod"; |
| 8 | + |
| 9 | +const DEFAULT_TIMEOUT = 20000; |
| 10 | + |
| 11 | +export const exec0Tools = { |
| 12 | + executePython: tool({ |
| 13 | + description: |
| 14 | + "Execute Python code and return the output. Use only Python standard library", |
| 15 | + inputSchema: z.object({ |
| 16 | + code: z.string().min(1).describe("Python code to execute"), |
| 17 | + }), |
| 18 | + execute: async ({ code }) => { |
| 19 | + try { |
| 20 | + const result = await executePython({ |
| 21 | + code, |
| 22 | + timeout: DEFAULT_TIMEOUT, |
| 23 | + }); |
| 24 | + return { |
| 25 | + success: true, |
| 26 | + output: result.data.output, |
| 27 | + executionTime: result.data.executionTime, |
| 28 | + error: result.data.error, |
| 29 | + }; |
| 30 | + } catch (error) { |
| 31 | + return { |
| 32 | + success: false, |
| 33 | + error: error instanceof Error ? error.message : "Unknown error", |
| 34 | + }; |
| 35 | + } |
| 36 | + }, |
| 37 | + }), |
| 38 | + |
| 39 | + executeJavaScript: tool({ |
| 40 | + description: |
| 41 | + "Execute JavaScript code and return the output. Use only JavaScript standard library", |
| 42 | + inputSchema: z.object({ |
| 43 | + code: z.string().min(1).describe("JavaScript code to execute"), |
| 44 | + }), |
| 45 | + execute: async ({ code }) => { |
| 46 | + try { |
| 47 | + const result = await executeJavaScript({ |
| 48 | + code, |
| 49 | + timeout: DEFAULT_TIMEOUT, |
| 50 | + }); |
| 51 | + return { |
| 52 | + success: true, |
| 53 | + output: result.data.output, |
| 54 | + executionTime: result.data.executionTime, |
| 55 | + error: result.data.error, |
| 56 | + }; |
| 57 | + } catch (error) { |
| 58 | + return { |
| 59 | + success: false, |
| 60 | + error: error instanceof Error ? error.message : "Unknown error", |
| 61 | + }; |
| 62 | + } |
| 63 | + }, |
| 64 | + }), |
| 65 | + |
| 66 | + executeTypeScript: tool({ |
| 67 | + description: |
| 68 | + "Execute TypeScript code and return the output. Use only TypeScript standard library", |
| 69 | + inputSchema: z.object({ |
| 70 | + code: z.string().min(1).describe("TypeScript code to execute"), |
| 71 | + }), |
| 72 | + execute: async ({ code }) => { |
| 73 | + try { |
| 74 | + const result = await executeTypeScript({ |
| 75 | + code, |
| 76 | + timeout: DEFAULT_TIMEOUT, |
| 77 | + }); |
| 78 | + return { |
| 79 | + success: true, |
| 80 | + output: result.data.output, |
| 81 | + executionTime: result.data.executionTime, |
| 82 | + error: result.data.error, |
| 83 | + }; |
| 84 | + } catch (error) { |
| 85 | + return { |
| 86 | + success: false, |
| 87 | + error: error instanceof Error ? error.message : "Unknown error", |
| 88 | + }; |
| 89 | + } |
| 90 | + }, |
| 91 | + }), |
| 92 | +}; |
0 commit comments