Skip to content

Commit 34bf07a

Browse files
authored
Merge pull request #27 from e2b-dev/add-examples
Add example apps
2 parents 7b7a080 + 50aef01 commit 34bf07a

16 files changed

+3395
-2041
lines changed

README.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,17 @@ Each sandbox is isolated from the others and can be customized with any dependen
66

77
![Desktop Sandbox](readme-assets/screenshot.png)
88

9-
### Example app using Computer Use with Anthropic's Claude
9+
## Examples
1010

11-
Check out the [example open-source app](https://github.com/e2b-dev/secure-computer-use) in a separate repository.
11+
### Open computer use
12+
13+
Check out the [example open-source app](https://github.com/e2b-dev/open-computer-use) in a separate repository.
14+
15+
### Basic SDK usage examples
16+
17+
Check out the examples directory for more examples on how to use the SDK:
18+
- [Python](./examples/basic-python)
19+
- [JavaScript](./examples/basic-javascript)
1220

1321
## 🚀 Getting started
1422

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
E2B_API_KEY=your_api_key

examples/basic-javascript/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Desktop Sandbox JavaScript Example
2+
3+
This is a basic example of how to use the Desktop Sandbox JavaScript SDK with streaming and moving mouse around inside an Electron app.
4+
5+
## How to run
6+
7+
### 1. Create `.env` file
8+
9+
```bash
10+
cp .env.example .env
11+
```
12+
13+
### 2. Set `E2B_API_KEY` in `.env` file
14+
15+
Get your API key at [e2b.dev/dashboard](https://e2b.dev/dashboard).
16+
17+
```bash
18+
E2B_API_KEY="your_api_key"
19+
```
20+
21+
### 3. Install dependencies
22+
23+
```bash
24+
npm install
25+
```
26+
27+
### 4. Run
28+
29+
```bash
30+
npm start
31+
```

examples/basic-javascript/index.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'dotenv/config'
2+
import { app, BrowserWindow } from 'electron'
3+
import { Sandbox } from '@e2b/desktop'
4+
5+
// Additional px to take into the account the window border at the top
6+
const windowFrameHeight = 29
7+
8+
app.on('window-all-closed', () => {
9+
app.quit()
10+
})
11+
12+
function wait(ms) {
13+
return new Promise(resolve => setTimeout(resolve, ms))
14+
}
15+
16+
/**
17+
* @param {string} streamUrl - The URL of the stream to load
18+
* @param {number} width - The width of the window
19+
* @param {number} height - The height of the window
20+
*/
21+
async function createWindow(streamUrl, width, height) {
22+
const win = new BrowserWindow({
23+
title: 'E2B Desktop',
24+
width,
25+
height: height + windowFrameHeight,
26+
webPreferences: {
27+
nodeIntegration: false,
28+
contextIsolation: true,
29+
webviewTag: true,
30+
},
31+
})
32+
33+
console.log('> Loading stream URL...')
34+
await win.loadURL(streamUrl)
35+
console.log(' - Stream URL loaded')
36+
}
37+
38+
/**
39+
* @param {import('@e2b/desktop').Sandbox} desktop - E2B desktop sandbox
40+
* @param {number} width - The width of the window
41+
* @param {number} height - The height of the window
42+
*/
43+
async function moveAround(desktop, width, height) {
44+
console.log('\n> Randomly moving mouse and right clicking 5 times...')
45+
for (let i = 0; i < 5; i++) {
46+
const x = Math.floor(Math.random() * width)
47+
const y = Math.floor(Math.random() * height)
48+
await desktop.moveMouse(x, y)
49+
console.log(` - Moved mouse to ${x}, ${y}`)
50+
await desktop.rightClick()
51+
console.log(` - Right clicked ${i}`)
52+
console.log(' - Waiting 2 seconds...\n')
53+
await wait(2000)
54+
}
55+
}
56+
57+
async function main() {
58+
console.log('> Waiting for electron app to be ready...')
59+
await app.whenReady()
60+
console.log(' - Electron app is ready')
61+
62+
console.log('\n> Starting desktop sandbox...')
63+
const desktop = await Sandbox.create()
64+
console.log(' - Desktop sandbox started, ID:', desktop.sandboxId)
65+
66+
const size = await desktop.getScreenSize()
67+
console.log(' - Desktop sandbox screen size:', size)
68+
69+
console.log('\n> Starting desktop stream...')
70+
await desktop.stream.start()
71+
72+
console.log('\n> Waiting 5 seconds for the stream to load...')
73+
for (let i = 5; i > 0; i--) {
74+
console.log(` - ${i} seconds remaining until the next step...`)
75+
await wait(1000)
76+
}
77+
78+
const url = desktop.stream.getUrl()
79+
console.log(' - Stream URL:', url)
80+
81+
console.log('\n> Creating browser window...')
82+
createWindow(url, size.width, size.height)
83+
console.log(' - Browser window created')
84+
85+
await moveAround(desktop, size.width, size.height)
86+
87+
console.log('\nPress enter to kill the sandbox and close the window...')
88+
process.stdin.once('data', async () => {
89+
console.log('\n> Stopping desktop stream...')
90+
await desktop.stream.stop()
91+
console.log(' - Desktop stream stopped')
92+
93+
console.log('\n> Closing browser window...')
94+
console.log(' - Browser window closed')
95+
96+
console.log('\n> Killing desktop sandbox...')
97+
await desktop.kill()
98+
console.log(' - Desktop sandbox killed')
99+
100+
app.quit()
101+
})
102+
}
103+
104+
main().then().catch(console.error)

0 commit comments

Comments
 (0)