MCPcopy Create free account
hub / github.com/pingdotgg/t3code / startOpenCodeServerProcess

Function startOpenCodeServerProcess

apps/server/src/provider/opencodeRuntime.ts:321–477  ·  view source on GitHub ↗
(input)

Source from the content-addressed store, hash-verified

319 );
320
321 const startOpenCodeServerProcess: OpenCodeRuntimeShape["startOpenCodeServerProcess"] = (input) =>
322 Effect.gen(function* () {
323 // Bind this server's lifetime to the caller's scope. When the caller's
324 // scope closes, the spawned child is killed and all associated fibers
325 // are interrupted automatically — no `close()` method needed.
326 const runtimeScope = yield* Scope.Scope;
327
328 const hostname = input.hostname ?? DEFAULT_HOSTNAME;
329 const port =
330 input.port ??
331 (yield* netService.findAvailablePort(0).pipe(
332 Effect.mapError(
333 (cause) =>
334 new OpenCodeRuntimeError({
335 operation: "startOpenCodeServerProcess",
336 detail: `Failed to find available port: ${openCodeRuntimeErrorDetail(cause)}`,
337 cause,
338 }),
339 ),
340 ));
341 const timeoutMs = input.timeoutMs ?? DEFAULT_OPENCODE_SERVER_TIMEOUT_MS;
342 const args = ["serve", `--hostname=${hostname}`, `--port=${port}`];
343 const spawnCommand = yield* resolveCommand(input.binaryPath, args, input.environment);
344
345 const child = yield* spawner
346 .spawn(
347 ChildProcess.make(spawnCommand.command, spawnCommand.args, {
348 detached: hostPlatform !== "win32",
349 shell: spawnCommand.shell,
350 env: {
351 ...input.environment,
352 OPENCODE_CONFIG_CONTENT: OPENCODE_EMPTY_CONFIG_CONTENT,
353 },
354 extendEnv: input.environment === undefined,
355 }),
356 )
357 .pipe(
358 Effect.provideService(Scope.Scope, runtimeScope),
359 Effect.mapError(
360 (cause) =>
361 new OpenCodeRuntimeError({
362 operation: "startOpenCodeServerProcess",
363 detail: `Failed to spawn OpenCode server process: ${openCodeRuntimeErrorDetail(cause)}`,
364 cause,
365 }),
366 ),
367 );
368
369 const killOpenCodeProcessGroup = (signal: NodeJS.Signals) =>
370 hostPlatform === "win32"
371 ? child.kill({ killSignal: signal, forceKillAfter: "1 second" }).pipe(Effect.asVoid)
372 : Effect.sync(() => {
373 try {
374 process.kill(-Number(child.pid), signal);
375 } catch {
376 // The direct child may already have exited after starting the
377 // server; the process group kill is best-effort cleanup for
378 // any serve process left in that group.

Callers 1

connectToOpenCodeServerFunction · 0.85

Calls 7

resolveCommandFunction · 0.85
killOpenCodeProcessGroupFunction · 0.85
ensureRuntimeErrorFunction · 0.85
spawnMethod · 0.80
getMethod · 0.80
failMethod · 0.80

Tested by

no test coverage detected