fix: change to non-streaming input method for E2B terminals

This commit is contained in:
James Murdza 2024-09-05 14:23:14 -07:00
parent a1990a189c
commit b01934bd20
4 changed files with 13 additions and 22 deletions

View File

@ -12,7 +12,7 @@
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"e2b": "^0.16.2-beta.46", "e2b": "^0.16.2-beta.47",
"express": "^4.19.2", "express": "^4.19.2",
"rate-limiter-flexible": "^5.0.3", "rate-limiter-flexible": "^5.0.3",
"simple-git": "^3.25.0", "simple-git": "^3.25.0",
@ -765,9 +765,9 @@
} }
}, },
"node_modules/e2b": { "node_modules/e2b": {
"version": "0.16.2-beta.46", "version": "0.16.2-beta.47",
"resolved": "https://registry.npmjs.org/e2b/-/e2b-0.16.2-beta.46.tgz", "resolved": "https://registry.npmjs.org/e2b/-/e2b-0.16.2-beta.47.tgz",
"integrity": "sha512-Hfh1dRmTzroFFvN5GNMj5x5xfZLN4iiMwdkXcUEky6yq7AkFsD/1ljKoddL/BzFYtSvKJpJ9iKXwsVI3Z28stA==", "integrity": "sha512-tMPDYLMD+8+JyLPrsWft3NHBhK5YKOFOXzKMwpOKR5KvXOkd1silkArDwplmBUzN/eG/uRzWdtHZs9mHUQ5b9g==",
"dependencies": { "dependencies": {
"@bufbuild/protobuf": "^1.10.0", "@bufbuild/protobuf": "^1.10.0",
"@connectrpc/connect": "^1.4.0", "@connectrpc/connect": "^1.4.0",

View File

@ -14,7 +14,7 @@
"concurrently": "^8.2.2", "concurrently": "^8.2.2",
"cors": "^2.8.5", "cors": "^2.8.5",
"dotenv": "^16.4.5", "dotenv": "^16.4.5",
"e2b": "^0.16.2-beta.46", "e2b": "^0.16.2-beta.47",
"express": "^4.19.2", "express": "^4.19.2",
"rate-limiter-flexible": "^5.0.3", "rate-limiter-flexible": "^5.0.3",
"simple-git": "^3.25.0", "simple-git": "^3.25.0",

View File

@ -3,9 +3,6 @@ import { Sandbox, ProcessHandle } from "e2b";
// Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment // Terminal class to manage a pseudo-terminal (PTY) in a sandbox environment
export class Terminal { export class Terminal {
private pty: ProcessHandle | undefined; // Holds the PTY process handle private pty: ProcessHandle | undefined; // Holds the PTY process handle
private input:
| { stop: () => void; sendData: (data: Uint8Array) => void }
| undefined; // Holds input stream controls
private sandbox: Sandbox; // Reference to the sandbox environment private sandbox: Sandbox; // Reference to the sandbox environment
// Constructor initializes the Terminal with a sandbox // Constructor initializes the Terminal with a sandbox
@ -29,19 +26,18 @@ export class Terminal {
cols, cols,
timeout: 0, timeout: 0,
onData: (data: Uint8Array) => { onData: (data: Uint8Array) => {
onData(data.toString()); // Convert received data to string and pass to handler onData(new TextDecoder().decode(data)); // Convert received data to string and pass to handler
}, },
}); });
// Set up input stream for the PTY
this.input = await this.sandbox.pty.streamInput(this.pty.pid);
} }
// Send data to the terminal // Send data to the terminal
sendData(data: string): void { async sendData(data: string) {
if (this.input) { if (this.pty) {
this.input.sendData(Buffer.from(data)); // Convert string to Buffer and send await this.sandbox.pty.sendInput(this.pty.pid, new TextEncoder().encode(data));
await this.pty.wait();
} else { } else {
console.log("Cannot send data because input is not initialized."); console.log("Cannot send data because pty is not initialized.");
} }
} }
@ -50,7 +46,7 @@ export class Terminal {
if (this.pty) { if (this.pty) {
await this.sandbox.pty.resize(this.pty.pid, size); await this.sandbox.pty.resize(this.pty.pid, size);
} else { } else {
console.log("Cannot send data because pty is not initialized."); console.log("Cannot resize terminal because pty is not initialized.");
} }
} }
@ -61,11 +57,6 @@ export class Terminal {
} else { } else {
console.log("Cannot kill pty because it is not initialized."); console.log("Cannot kill pty because it is not initialized.");
} }
if (this.input) {
this.input.stop();
} else {
console.log("Cannot stop input because it is not initialized.");
}
} }
} }

View File

@ -526,7 +526,7 @@ io.on("connection", async (socket) => {
rows: 20, rows: 20,
//onExit: () => console.log("Terminal exited", id), //onExit: () => console.log("Terminal exited", id),
}); });
terminals[id].sendData( await terminals[id].sendData(
`cd "${path.join(dirName, "projects", data.sandboxId)}"\rexport PS1='user> '\rclear\r` `cd "${path.join(dirName, "projects", data.sandboxId)}"\rexport PS1='user> '\rclear\r`
); );
console.log("Created terminal", id); console.log("Created terminal", id);