MCPcopy Create free account
hub / github.com/IgorWarzocha/howcode / ensureRuntimeHost

Function ensureRuntimeHost

desktop/runtime-host/client-bridge.ts:350–427  ·  view source on GitHub ↗
(host: HostConnection)

Source from the content-addressed store, hash-verified

348}
349
350async function ensureRuntimeHost(host: HostConnection) {
351 if (runtimeHostsShuttingDown) {
352 throw new Error('Pi runtime host is shutting down.')
353 }
354
355 registerHostShutdownHandlers()
356 if (host.process && !host.process.killed && host.process.exitCode === null) {
357 clearHostIdleTimer(host)
358 return host.process
359 }
360
361 if (host.startPromise) {
362 return host.startPromise
363 }
364
365 clearHostIdleTimer(host)
366 host.startPromise = (async () => {
367 const nodeExecutable = await getNodeExecutable()
368 if (runtimeHostsShuttingDown) {
369 throw new Error('Pi runtime host is shutting down.')
370 }
371
372 return await new Promise<ChildProcess>((resolve, reject) => {
373 const child = spawn(nodeExecutable, [getRuntimeHostPath()], {
374 cwd: getDesktopWorkingDirectory(),
375 env: {
376 ...process.env,
377 HOWCODE_REPO_ROOT: getDesktopWorkingDirectory(),
378 HOWCODE_ELECTRON_RESOURCES_PATH: getElectronResourcesPath(),
379 HOWCODE_BUNDLED_SKILLS_PATH: getBundledSkillsPath(),
380 },
381 stdio: ['ignore', 'pipe', 'pipe', 'ipc'],
382 }) as ChildProcess
383
384 let settled = false
385 const settleFailure = (error: Error) => {
386 if (settled) {
387 return
388 }
389 settled = true
390 host.startPromise = null
391 host.process = null
392 if (host.role === 'thread') {
393 forgetHost(host)
394 }
395 reject(error)
396 }
397
398 child.once('spawn', () => {
399 if (runtimeHostsShuttingDown) {
400 terminateHostProcess(child)
401 settleFailure(new Error('Pi runtime host is shutting down.'))
402 return
403 }
404
405 settled = true
406 host.process = child
407 host.startPromise = null

Callers 3

invokeRuntimeHostFunction · 0.85
invokeRuntimeHostOnHostFunction · 0.85

Calls 14

getNodeExecutableFunction · 0.90
getRuntimeHostPathFunction · 0.90
getElectronResourcesPathFunction · 0.90
getBundledSkillsPathFunction · 0.90
clearHostIdleTimerFunction · 0.85
spawnFunction · 0.85
terminateHostProcessFunction · 0.85
settleFailureFunction · 0.85
resolveFunction · 0.85
handleHostExitFunction · 0.85

Tested by

no test coverage detected