MCPcopy Create free account
hub / github.com/bearlyai/OpenADE / handleSpawn

Function handleSpawn

projects/electron/src/modules/code/pty.ts:193–249  ·  view source on GitHub ↗
(params: SpawnParams)

Source from the content-addressed store, hash-verified

191}
192
193async 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

Callers 1

spawnRuntimePtyFunction · 0.85

Calls 10

getDefaultShellFunction · 0.85
hasMethod · 0.80
onExitMethod · 0.80
errorMethod · 0.80
validateCwdFunction · 0.70
sendOutputFunction · 0.70
sendExitFunction · 0.70
emitLifecycleFunction · 0.70
spawnMethod · 0.65
setMethod · 0.65

Tested by

no test coverage detected