-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.ts
More file actions
84 lines (72 loc) · 2.46 KB
/
Copy pathindex.ts
File metadata and controls
84 lines (72 loc) · 2.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { Runloop } from "@runloop/api-client";
async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function main() {
// Initialize the Runloop client
const runloopClient = new Runloop({
bearerToken: process.env.RUNLOOP_API_KEY,
});
let devbox;
try {
// Create a devbox and wait for it to be ready
console.log("Creating devbox...");
devbox = await runloopClient.devboxes.createAndAwaitRunning({
name: "command-execution-devbox-ts",
});
console.log(`Devbox created: ${devbox.id}`);
// Example 1: Synchronous command execution
console.log("\nExecuting synchronous command...");
const syncResult = await runloopClient.devboxes.executeSync(devbox.id, {
command: "echo 'Hello from synchronous command'",
});
console.log(`Command output: ${syncResult.stdout}`);
// Example 2: Asynchronous command execution
console.log("\nExecuting asynchronous command...");
const execution = await runloopClient.devboxes.executeAsync(devbox.id, {
command:
'for i in {1..5}; do echo "Hello from async command $i"; sleep 1; done',
});
// Poll for results
while (true) {
const status = await runloopClient.devboxes.executions.retrieve(
devbox.id,
execution.execution_id
);
console.log(`Latest output: ${status.stdout}`);
if (status.status === "completed") {
break;
}
await sleep(1000);
}
// Example 3: Stateful shell operations
console.log("\nDemonstrating stateful shell operations...");
const shellName = "my-shell";
// Check initial directory
const initialDir = await runloopClient.devboxes.executeSync(devbox.id, {
command: "pwd",
shell_name: shellName,
});
console.log(`Initial directory: ${initialDir.stdout}`);
// Create and enter new directory
await runloopClient.devboxes.executeSync(devbox.id, {
command: "mkdir -p mynewfolder && cd mynewfolder",
shell_name: shellName,
});
// Verify directory change persisted
const newDir = await runloopClient.devboxes.executeSync(devbox.id, {
command: "pwd",
shell_name: shellName,
});
console.log(`New directory: ${newDir.stdout}`);
} catch (error) {
console.error("Error:", error);
} finally {
// Clean up
if (devbox) {
console.log("\nShutting down devbox...");
await runloopClient.devboxes.shutdown(devbox.id);
}
}
}
main();