(params: SpawnParams)
| 191 | } |
| 192 | |
| 193 | async function handleSpawn(params: SpawnParams): Promise<SpawnResponse> { |
| 194 | logger.info("[Pty:spawn] Starting", JSON.stringify({ ptyId: params.ptyId, cwd: params.cwd, cols: params.cols, rows: params.rows })) |
| 195 | |
| 196 | try { |
| 197 | validateCwd(params.cwd) |
| 198 | |
| 199 | if (activePtys.size >= MAX_ACTIVE_PTYS) { |
| 200 | throw new Error(`Maximum active PTYs (${MAX_ACTIVE_PTYS}) reached`) |
| 201 | } |
| 202 | |
| 203 | // If PTY already exists, just reconnect |
| 204 | if (activePtys.has(params.ptyId)) { |
| 205 | return { ok: true } |
| 206 | } |
| 207 | |
| 208 | const shell = getDefaultShell() |
| 209 | const ptyProcess = pty.spawn(shell, [], { |
| 210 | name: "xterm-256color", |
| 211 | cols: params.cols, |
| 212 | rows: params.rows, |
| 213 | cwd: params.cwd, |
| 214 | env: { ...(process.env as Record<string, string>), ...params.env }, |
| 215 | }) |
| 216 | |
| 217 | activePtys.set(params.ptyId, { |
| 218 | pty: ptyProcess, |
| 219 | outputBuffer: [], |
| 220 | bufferSizeBytes: 0, |
| 221 | exited: false, |
| 222 | exitCode: null, |
| 223 | }) |
| 224 | |
| 225 | ptyProcess.onData((data) => { |
| 226 | sendOutput(params.ptyId, data) |
| 227 | }) |
| 228 | |
| 229 | ptyProcess.onExit(({ exitCode }) => { |
| 230 | logger.info("[Pty] Process exited", JSON.stringify({ ptyId: params.ptyId, exitCode })) |
| 231 | sendExit(params.ptyId, exitCode) |
| 232 | }) |
| 233 | |
| 234 | emitLifecycle({ |
| 235 | type: "started", |
| 236 | ptyId: params.ptyId, |
| 237 | pid: ptyProcess.pid, |
| 238 | cwd: params.cwd, |
| 239 | shell, |
| 240 | }) |
| 241 | |
| 242 | logger.info("[Pty:spawn] PTY created", JSON.stringify({ ptyId: params.ptyId, shell, pid: ptyProcess.pid })) |
| 243 | return { ok: true } |
| 244 | } catch (error: unknown) { |
| 245 | const errorMessage = error instanceof Error ? error.message : "Unknown error" |
| 246 | logger.error("[Pty:spawn] Error:", JSON.stringify({ error: errorMessage })) |
| 247 | return { ok: false, error: errorMessage } |
| 248 | } |
| 249 | } |
| 250 |
no test coverage detected