| 19 | } |
| 20 | |
| 21 | async function runExample() { |
| 22 | console.log("Creating E2B Code Interpreter instance..."); |
| 23 | |
| 24 | try { |
| 25 | // Create a new Code Interpreter instance |
| 26 | const sandbox = await CodeInterpreter.create({ apiKey }); |
| 27 | |
| 28 | // Log the sandbox object to see its structure |
| 29 | console.log("Sandbox created:", Object.keys(sandbox)); |
| 30 | |
| 31 | // Try to execute a simple Python code if possible |
| 32 | if (typeof sandbox.execCode === 'function') { |
| 33 | console.log("Executing code using sandbox.execCode..."); |
| 34 | const result = await sandbox.execCode('print("Hello, World!")'); |
| 35 | console.log("Result:", result); |
| 36 | } else if (sandbox.notebook && typeof sandbox.notebook.execCell === 'function') { |
| 37 | console.log("Executing code using sandbox.notebook.execCell..."); |
| 38 | const result = await sandbox.notebook.execCell('print("Hello, World!")'); |
| 39 | console.log("Result:", result); |
| 40 | } else { |
| 41 | console.log("Could not find a method to execute code. Available methods:", |
| 42 | Object.getOwnPropertyNames(Object.getPrototypeOf(sandbox))); |
| 43 | } |
| 44 | |
| 45 | // Close the sandbox if possible |
| 46 | if (typeof sandbox.close === 'function') { |
| 47 | await sandbox.close(); |
| 48 | console.log("Sandbox closed"); |
| 49 | } else { |
| 50 | console.log("No close method found on sandbox"); |
| 51 | } |
| 52 | |
| 53 | } catch (error: unknown) { |
| 54 | console.error("Error:", error instanceof Error ? error.message : String(error)); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | // Run the example |
| 59 | runExample(); |