Skip to content

armgit / E2B

Public Imported from GitHub
0

Open-source, secure environment with real-world tools for enterprise-grade agents.

fix: add queue for terminal input (#924)

Jakub Dobry4da865c1f3ef
diff --git a/.changeset/grumpy-moons-melt.md b/.changeset/grumpy-moons-melt.md
new file mode 100644
index 000000000..372fcfac2
--- /dev/null
+++ b/.changeset/grumpy-moons-melt.md
@@ -0,0 +1,5 @@
+---
+'@e2b/cli': patch
+---
+
+add queue for terminal input to help with network delays
diff --git a/packages/cli/src/terminal.ts b/packages/cli/src/terminal.ts
index a29bf2e87..713314e63 100644
--- a/packages/cli/src/terminal.ts
+++ b/packages/cli/src/terminal.ts
@@ -1,5 +1,7 @@
import * as e2b from 'e2b'
+const FLUSH_INPUT_INTERVAL_MS = 10
+
function getStdoutSize() {
return {
cols: process.stdout.columns,
@@ -22,13 +24,20 @@ export async function spawnConnectedTerminal(sandbox: e2b.Sandbox) {
timeoutMs: 0,
})
+ const inputQueue = new BatchedQueue<Buffer>(async (batch) => {
+ const combined = Buffer.concat(batch)
+ await sandbox.pty.sendInput(terminalSession.pid, combined)
+ }, FLUSH_INPUT_INTERVAL_MS)
+
const resizeListener = process.stdout.on('resize', () =>
sandbox.pty.resize(terminalSession.pid, getStdoutSize())
)
- const stdinListener = process.stdin.on('data', async (data) => {
- await sandbox.pty.sendInput(terminalSession.pid, data)
+ const stdinListener = process.stdin.on('data', (data) => {
+ inputQueue.push(data)
})
+ inputQueue.start()
+
// Wait for terminal session to finish
try {
await terminalSession.wait()
@@ -48,6 +57,52 @@ export async function spawnConnectedTerminal(sandbox: e2b.Sandbox) {
process.stdout.write('\n')
resizeListener.destroy()
stdinListener.destroy()
+ await inputQueue.stop()
process.stdin.setRawMode(false)
}
}
+
+class BatchedQueue<T> {
+ private queue: T[] = []
+ private isFlushing = false
+ private intervalId?: NodeJS.Timeout
+
+ constructor(
+ private flushHandler: (batch: T[]) => Promise<void>,
+ private flushIntervalMs: number
+ ) {}
+
+ push(item: T) {
+ this.queue.push(item)
+ }
+
+ start() {
+ this.intervalId = setInterval(async () => {
+ if (this.isFlushing) return
+
+ this.isFlushing = true
+ await this.flush()
+ this.isFlushing = false
+ }, this.flushIntervalMs)
+ }
+
+ async stop() {
+ if (this.intervalId) {
+ clearInterval(this.intervalId)
+ this.intervalId = undefined
+ }
+
+ await this.flush()
+ }
+
+ private async flush() {
+ if (this.queue.length === 0) return
+
+ const batch = this.queue.splice(0, this.queue.length)
+ try {
+ await this.flushHandler(batch)
+ } catch (err) {
+ console.error('Error sending input:', err)
+ }
+ }
+}