Class: E2B::Services::Pty
- Inherits:
-
BaseService
- Object
- BaseService
- E2B::Services::Pty
- Includes:
- LiveStreamable
- Defined in:
- lib/e2b/services/pty.rb
Overview
PTY (pseudo-terminal) service for E2B sandbox.
Provides methods to create, connect to, and manage interactive pseudo-terminals inside the sandbox. Uses Connect RPC protocol with the process.Process service.
Constant Summary collapse
- DEFAULT_SHELL =
Default shell to use for PTY sessions
"/bin/bash"- DEFAULT_SHELL_ARGS =
Default shell arguments for interactive login shell
["-i", "-l"].freeze
Constants inherited from BaseService
BaseService::DEFAULT_USERNAME, BaseService::ENVD_DEFAULT_USER_VERSION, BaseService::ENVD_PORT, BaseService::ENVD_RECURSIVE_WATCH_VERSION
Instance Method Summary collapse
-
#close_stdin(pid) ⇒ void
Close the stdin of a PTY process.
-
#connect(pid, timeout: 60) ⇒ CommandHandle
Connect to an existing PTY process.
-
#create(size: PtySize.new, user: nil, cwd: nil, envs: nil, cmd: DEFAULT_SHELL, args: DEFAULT_SHELL_ARGS, timeout: 60) ⇒ CommandHandle
Create a new PTY (pseudo-terminal) session in the sandbox.
-
#kill(pid, headers: nil) ⇒ Boolean
Kill a PTY process with SIGKILL.
-
#list ⇒ Array<Hash>
List running processes in the sandbox.
-
#resize(pid, size) ⇒ void
Resize a PTY terminal.
-
#send_stdin(pid, data, headers: nil) ⇒ void
Send input data to a PTY.
Methods inherited from BaseService
Constructor Details
This class inherits a constructor from E2B::Services::BaseService
Instance Method Details
#close_stdin(pid) ⇒ void
This method returns an undefined value.
Close the stdin of a PTY process.
After calling this, no more input can be sent to the PTY via #send_stdin.
209 210 211 212 213 |
# File 'lib/e2b/services/pty.rb', line 209 def close_stdin(pid) envd_rpc("process.Process", "CloseStdin", body: { process: { pid: pid } }) end |
#connect(pid, timeout: 60) ⇒ CommandHandle
Connect to an existing PTY process.
Attaches to a running PTY process by PID and returns a handle for sending input and receiving output.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/e2b/services/pty.rb', line 126 def connect(pid, timeout: 60) body = { process: { pid: pid } } build_live_handle( rpc_method: "Connect", body: body, headers: user_auth_headers(nil), timeout: timeout + 30 ) end |
#create(size: PtySize.new, user: nil, cwd: nil, envs: nil, cmd: DEFAULT_SHELL, args: DEFAULT_SHELL_ARGS, timeout: 60) ⇒ CommandHandle
Create a new PTY (pseudo-terminal) session in the sandbox.
Starts an interactive shell process with a PTY attached. The returned CommandHandle can be used to send input, receive output, and manage the PTY lifecycle.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/e2b/services/pty.rb', line 85 def create(size: PtySize.new, user: nil, cwd: nil, envs: nil, cmd: DEFAULT_SHELL, args: DEFAULT_SHELL_ARGS, timeout: 60) envs = build_pty_envs(envs) headers = user_auth_headers(user) process_spec = { cmd: cmd, args: args, envs: envs } process_spec[:cwd] = cwd if cwd body = { process: process_spec, pty: { size: size.to_h }, stdin: false } build_live_handle( rpc_method: "Start", body: body, headers: headers, timeout: timeout + 30 ) end |
#kill(pid, headers: nil) ⇒ Boolean
Kill a PTY process with SIGKILL.
170 171 172 173 174 175 176 177 178 |
# File 'lib/e2b/services/pty.rb', line 170 def kill(pid, headers: nil) envd_rpc("process.Process", "SendSignal", body: { process: { pid: pid }, signal: 9 # SIGKILL }, headers: headers) true rescue E2B::E2BError false end |
#list ⇒ Array<Hash>
List running processes in the sandbox.
218 219 220 221 |
# File 'lib/e2b/services/pty.rb', line 218 def list response = envd_rpc("process.Process", "List", body: {}) response["processes"] || response[:processes] || [] end |
#resize(pid, size) ⇒ void
This method returns an undefined value.
Resize a PTY terminal.
Should be called when the terminal window size changes to keep the remote PTY in sync.
192 193 194 195 196 197 198 199 |
# File 'lib/e2b/services/pty.rb', line 192 def resize(pid, size) envd_rpc("process.Process", "Update", body: { process: { pid: pid }, pty: { size: size.to_h } }) end |
#send_stdin(pid, data, headers: nil) ⇒ void
This method returns an undefined value.
Send input data to a PTY.
The data is base64-encoded and sent as PTY input (not stdin), which means it goes through the terminal emulator and supports control characters, escape sequences, etc.
155 156 157 158 159 160 161 |
# File 'lib/e2b/services/pty.rb', line 155 def send_stdin(pid, data, headers: nil) encoded = Base64.strict_encode64(data.is_a?(String) ? data : data.to_s) envd_rpc("process.Process", "SendInput", body: { process: { pid: pid }, input: { pty: encoded } }, headers: headers) end |