The best way to control Claude Code, Codex and more from any computer or phone
| 1 | // A second T3 server for scenarios that need another machine: its own T3 home, the replay |
| 2 | // agent, and a pairing token for Settings > Connections > Add environment. |
| 3 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 4 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 5 | import * as NetService from "@t3tools/shared/Net"; |
| 6 | import * as Cause from "effect/Cause"; |
| 7 | import * as Deferred from "effect/Deferred"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Exit from "effect/Exit"; |
| 10 | import * as Fiber from "effect/Fiber"; |
| 11 | import * as Path from "effect/Path"; |
| 12 | |
| 13 | import { APP_DIR, T3_HOME } from "../e2e.config.ts"; |
| 14 | import { ScenarioError } from "./shell.ts"; |
| 15 | import { prepareHome, SCENARIO_MACHINE_ENV, start } from "./start-app.ts"; |
| 16 | |
| 17 | export const SECOND_HOME = `${T3_HOME}-second-environment`; |
| 18 | |
| 19 | /** A second server on its own T3 home, running until interrupted. `--dev-url` admits the web |
| 20 | * origin through CORS and registers the replay agent; `paired` gets the Add environment input. */ |
| 21 | const runSecondServer = Effect.fn("scenario.secondServer")(function* ( |
| 22 | devUrl: string, |
| 23 | paired: (target: { readonly host: string; readonly token: string }) => void, |
| 24 | ) { |
| 25 | const path = yield* Path.Path; |
| 26 | const node = yield* HostProcess.ExecutablePath; |
| 27 | const hostEnvironment = yield* HostProcess.Environment; |
| 28 | const port = yield* (yield* NetService.NetService).reserveLoopbackPort(); |
| 29 | yield* prepareHome(SECOND_HOME); |
| 30 | const token = yield* Deferred.make<string>(); |
| 31 | const server = yield* start({ |
| 32 | cwd: APP_DIR, |
| 33 | command: node, |
| 34 | args: [ |
| 35 | "apps/server/src/bin.ts", |
| 36 | "serve", |
| 37 | "--host", |
| 38 | "127.0.0.1", |
| 39 | "--port", |
| 40 | String(port), |
| 41 | "--base-dir", |
| 42 | SECOND_HOME, |
| 43 | "--dev-url", |
| 44 | devUrl, |
| 45 | ], |
| 46 | env: { |
| 47 | PATH: `${path.join(SECOND_HOME, "bin")}:${hostEnvironment.PATH ?? ""}`, |
| 48 | HOME: hostEnvironment.HOME, |
| 49 | T3CODE_AUTO_BOOTSTRAP_PROJECT_FROM_CWD: "0", |
| 50 | ...SCENARIO_MACHINE_ENV, |
| 51 | }, |
| 52 | onLine: (line) => { |
| 53 | const value = /^Token:\s*(\S+)/.exec(line)?.[1]; |
| 54 | return value === undefined ? Effect.void : Deferred.succeed(token, value).pipe(Effect.asVoid); |
| 55 | }, |
| 56 | }); |
| 57 | // A server that exits before printing its token never will: fail instead of waiting forever. |
| 58 | const exited = server.exitCode.pipe( |
| 59 | Effect.flatMap( |
| 60 | (code) => |
| 61 | new ScenarioError({ message: `The second server exited with code ${code} at startup.` }), |
| 62 | ), |
| 63 | ); |
| 64 | paired({ |
| 65 | host: `http://127.0.0.1:${port}`, |
| 66 | token: yield* Effect.raceFirst(Deferred.await(token), exited), |
| 67 | }); |
| 68 | return yield* Effect.never; |
| 69 | }); |
| 70 | |
| 71 | /** Starts the second server for the web app at `devUrl`. `ready` settles with its pairing input. */ |
| 72 | export function startSecondServer(devUrl: string) { |
| 73 | const pairing = Promise.withResolvers<{ readonly host: string; readonly token: string }>(); |
| 74 | const server = Effect.runFork( |
| 75 | runSecondServer(devUrl, pairing.resolve).pipe( |
| 76 | Effect.scoped, |
| 77 | Effect.provide([NodeServices.layer, NetService.layer]), |
| 78 | ), |
| 79 | ); |
| 80 | server.addObserver((exit) => |
| 81 | pairing.reject( |
| 82 | Exit.isFailure(exit) ? Cause.squash(exit.cause) : new Error("The second server stopped."), |
| 83 | ), |
| 84 | ); |
| 85 | return { ready: pairing.promise, stop: () => Effect.runPromise(Fiber.interrupt(server)) }; |
| 86 | } |