Open-source, secure environment with real-world tools for enterprise-grade agents.
Python agentaiai-agentai-agentscode-interpretercopilotdevelopmentdevtoolsgptgpt-4javascriptllmnextjsopenaipythonreactsoftwaretypescript
Rewrite terminal in CLI
Jakub Novak80ae7f452a41
diff --git a/packages/cli/src/commands/sandbox/connect.ts b/packages/cli/src/commands/sandbox/connect.tsindex 9a0df248e..383095ec6 100644--- a/packages/cli/src/commands/sandbox/connect.ts+++ b/packages/cli/src/commands/sandbox/connect.ts@@ -34,16 +34,7 @@ export const connectCommand = new commander.Command('connect')async function connectToSandbox({ apiKey, sandboxID }: { apiKey: string, sandboxID: string }) {const sandbox = await Sandbox.connect(sandboxID, { apiKey })- const { exited } = await spawnConnectedTerminal(- sandbox,- `Terminal connected to sandbox ${asPrimary(- sandboxID,- )} with sandbox ID ${asBold(`${sandbox.sandboxId}`)}`,- `Disconnecting terminal from sandbox ${asPrimary(- sandboxID,- )}`,- )-- await exited- console.log(`Closing terminal connection to sandbox ${asPrimary(sandboxID)}`)+ console.log(`Terminal connecting to sandbox ${asPrimary(`${sandbox.sandboxId}`)}`)+ await spawnConnectedTerminal(sandbox)+ console.log(`Closing terminal connection to sandbox ${asPrimary(sandbox.sandboxId)}`)}diff --git a/packages/cli/src/commands/sandbox/spawn.ts b/packages/cli/src/commands/sandbox/spawn.tsindex 327c39204..93cfc41a3 100644--- a/packages/cli/src/commands/sandbox/spawn.ts+++ b/packages/cli/src/commands/sandbox/spawn.ts@@ -82,18 +82,17 @@ export async function connectSandbox({}) {const sandbox = await e2b.Sandbox.create(template.templateID, { apiKey })- const { exited } = await spawnConnectedTerminal(- sandbox,- `Terminal connected to sandbox ${asFormattedSandboxTemplate(- template,- )} with sandbox ID ${asBold(- `${sandbox.sandboxId}`,- )}`,- `Disconnecting terminal from sandbox ${asFormattedSandboxTemplate(- template,- )}`,- )+ // keep-alive loop+ const intervalId = setInterval(async () => {+ await sandbox.setTimeout(10_000)+ }, 5_000)- await exited()- console.log(`Closing terminal connection to sandbox ${asFormattedSandboxTemplate(template)}`)+ console.log(`Terminal connecting to template ${asFormattedSandboxTemplate(template)} with sandbox ID ${asBold(`${sandbox.sandboxId}`)}`)+ try {+ await spawnConnectedTerminal(sandbox)+ } finally {+ clearInterval(intervalId)+ await sandbox.kill()+ console.log(`Closing terminal connection to template ${asFormattedSandboxTemplate(template)} with sandbox ID ${asBold(`${sandbox.sandboxId}`)}`)+ }}diff --git a/packages/cli/src/terminal.ts b/packages/cli/src/terminal.tsindex 6709d09a5..224ef6123 100644--- a/packages/cli/src/terminal.ts+++ b/packages/cli/src/terminal.ts@@ -1,5 +1,4 @@import * as e2b from 'e2b'-import {wait} from './utils/wait'function getStdoutSize() {@@ -9,40 +8,42 @@ function getStdoutSize() {}}-export async function spawnConnectedTerminal(- sandbox: e2b.Sandbox,- introText: string,- exitText: string,-) {+export async function spawnConnectedTerminal(sandbox: e2b.Sandbox) {// Clear local terminal emulator before starting terminal// process.stdout.write('\x1b[2J\x1b[0f')- console.log(introText)+ process.stdin.setRawMode(true)+ process.stdout.setEncoding('utf-8')+const terminalSession = await sandbox.pty.create({- onData: (data: Uint8Array) => process.stdout.write(data),- ...getStdoutSize()- })+ onData: data => {+ process.stdout.write(data)+ },+ ...getStdoutSize(),+ timeoutMs: 0,+ })- // TODO: missing newlines- process.stdin.setRawMode(true)- process.stdout.setEncoding('utf8')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 exited = async () => {- // TODO: handle exit- await wait(1000_000)+ // Wait for terminal session to finish+ try {await terminalSession.wait()- await input.stop()- console.log(exitText)+ } catch (err: any) {+ if (err instanceof e2b.ProcessExitError) {+ // TODO: Handle Ctrl+C?+ if (err.exitCode === 130) {+ return+ }+ }+ throw err+ } finally {+ // Cleanup+ process.stdout.write('\n')resizeListener.destroy()stdinListener.destroy()- }-- return {- kill: terminalSession.kill.bind(terminalSession),- exited,+ process.stdin.setRawMode(false)}}diff --git a/packages/cli/src/utils/promise.ts b/packages/cli/src/utils/promise.tsdeleted file mode 100644index a28f852c3..000000000--- a/packages/cli/src/utils/promise.ts+++ /dev/null@@ -1,14 +0,0 @@-export function createDeferredPromise<T = void>() {- let resolve: (value: T) => void- let reject: (reason?: any) => void- const promise = new Promise<T>((res, rej) => {- resolve = res- reject = rej- })-- return {- resolve: resolve!,- reject: reject!,- promise,- }-}