You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -180,10 +180,6 @@ You can disconnect from a PTY session while keeping it running, then reconnect l
180
180
- Sharing terminal access between multiple clients
181
181
- Implementing terminal session persistence
182
182
183
-
<Note>
184
-
While disconnected, the PTY continues to accumulate output. You can access all output generated during the session (including while disconnected) via the `stdout` property on the terminal handle.
185
-
</Note>
186
-
187
183
<CodeGroup>
188
184
```js JavaScript & TypeScript
189
185
import { Sandbox } from'@e2b/code-interpreter'
@@ -256,33 +252,6 @@ asyncio.run(main())
256
252
```
257
253
</CodeGroup>
258
254
259
-
### Accessing accumulated output
260
-
261
-
After reconnecting, you can access all output generated during the session (including while disconnected) via the `stdout` property:
262
-
263
-
<CodeGroup>
264
-
```js JavaScript & TypeScript
265
-
// After reconnecting...
266
-
constreconnected=awaitsandbox.pty.connect(pid, {
267
-
onData: (data) => { /* handle new data */ },
268
-
})
269
-
270
-
// Access all accumulated output from the session
271
-
console.log('All output so far:', reconnected.stdout)
272
-
```
273
-
274
-
```python Python
275
-
# After reconnecting...
276
-
reconnected =await sandbox.pty.connect(
277
-
pid,
278
-
on_data=lambdadata: print(data.decode(), end=''),
279
-
)
280
-
281
-
# Access all accumulated output from the session
282
-
print('All output so far:', reconnected.stdout)
283
-
```
284
-
</CodeGroup>
285
-
286
255
## Kill the PTY
287
256
288
257
Terminate the PTY session with `kill()`.
@@ -378,3 +347,107 @@ async def main():
378
347
asyncio.run(main())
379
348
```
380
349
</CodeGroup>
350
+
351
+
## Interactive terminal (SSH-like)
352
+
353
+
For a fully interactive terminal experience similar to SSH, you need to:
354
+
1. Set your local terminal to raw mode
355
+
2. Forward local stdin to the PTY
356
+
3. Write PTY output to local stdout
357
+
4. Handle terminal resize events
358
+
359
+
<CodeGroup>
360
+
```js JavaScript & TypeScript
361
+
import { Sandbox } from'@e2b/code-interpreter'
362
+
363
+
asyncfunctionmain() {
364
+
constsandbox=awaitSandbox.create()
365
+
366
+
// Set terminal to raw mode for character-by-character input
367
+
process.stdin.setRawMode(true)
368
+
369
+
constterminal=awaitsandbox.pty.create({
370
+
cols:process.stdout.columns,
371
+
rows:process.stdout.rows,
372
+
onData: (data) =>process.stdout.write(data),
373
+
timeoutMs:0,
374
+
})
375
+
376
+
// Forward stdin to the PTY
377
+
process.stdin.on('data', async (data) => {
378
+
awaitsandbox.pty.sendInput(terminal.pid, data)
379
+
})
380
+
381
+
// Handle terminal resize
382
+
process.stdout.on('resize', () => {
383
+
sandbox.pty.resize(terminal.pid, {
384
+
cols:process.stdout.columns,
385
+
rows:process.stdout.rows,
386
+
})
387
+
})
388
+
389
+
// Wait for the session to end
390
+
try {
391
+
awaitterminal.wait()
392
+
} finally {
393
+
process.stdin.setRawMode(false)
394
+
awaitsandbox.kill()
395
+
}
396
+
}
397
+
398
+
main()
399
+
```
400
+
401
+
```python Python
402
+
import asyncio
403
+
import sys
404
+
import tty
405
+
import termios
406
+
from e2b_code_interpreter import AsyncSandbox
407
+
from e2b.sandbox_async.commands.pty import PtySize
408
+
409
+
410
+
asyncdefmain():
411
+
sandbox =await AsyncSandbox.create()
412
+
413
+
# Save terminal settings
414
+
old_settings = termios.tcgetattr(sys.stdin)
415
+
416
+
try:
417
+
# Set terminal to raw mode
418
+
tty.setraw(sys.stdin.fileno())
419
+
420
+
terminal =await sandbox.pty.create(
421
+
size=PtySize(cols=80, rows=24),
422
+
on_data=lambdadata: sys.stdout.buffer.write(data) or sys.stdout.flush(),
423
+
timeout=0,
424
+
)
425
+
426
+
# Forward stdin to PTY (simplified - production code needs async stdin reading)
427
+
asyncdefread_stdin():
428
+
loop = asyncio.get_event_loop()
429
+
whileTrue:
430
+
data =await loop.run_in_executor(None, sys.stdin.buffer.read, 1)
This is the same pattern used by the [E2B CLI](https://github.com/e2b-dev/E2B/blob/main/packages/cli/src/terminal.ts) to provide interactive sandbox sessions. For production use, consider batching input to reduce network calls.
0 commit comments