@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 110 lines 3.64 KB
Raw
1 import { PlatformServices, runMain, httpServer } from "alchemy/Util/PlatformServices";
2 import * as Effect from "effect/Effect";
3 import * as FileSystem from "effect/FileSystem";
4 import * as HttpServer from "effect/http/HttpServer";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import * as Path from "effect/Path";
8 import * as ChildProcess from "effect/process/ChildProcess";
9 import * as Schedule from "effect/Schedule";
10
11 const mode = process.env.LIFECYCLE_MODE ?? "cooperative";
12 const leaf = process.env.LIFECYCLE_LEAF === "1";
13 const directory = process.env.LIFECYCLE_DIR!;
14
15 const program = Effect.gen(function* () {
16 const fs = yield* FileSystem.FileSystem;
17 const path = yield* Path.Path;
18 const prefix = path.join(directory, leaf ? "leaf" : "wrapper");
19 if (mode !== "cooperative") {
20 yield* Effect.acquireRelease(
21 Effect.sync(() => {
22 const onTerm = () =>
23 Effect.runFork(
24 fs
25 .writeFileString(`${prefix}.term`, "TERM")
26 .pipe(
27 Effect.andThen(
28 mode === "early-exit" && !leaf ? Effect.sync(() => process.exit(0)) : Effect.void,
29 ),
30 ),
31 );
32 process.on("SIGTERM", onTerm);
33 return onTerm;
34 }),
35 (listener) =>
36 Effect.sync(() => {
37 process.removeListener("SIGTERM", listener);
38 }),
39 );
40 }
41 if (leaf) {
42 yield* fs.writeFileString(`${prefix}.ready`, "ready");
43 } else {
44 const entry = yield* path.fromFileUrl(new URL(import.meta.url));
45 const child = yield* ChildProcess.make("bun", ["run", entry], {
46 detached: mode === "cooperative",
47 env: { LIFECYCLE_LEAF: "1" },
48 extendEnv: true,
49 stdout: "ignore",
50 stderr: "inherit",
51 forceKillAfter: "200 millis",
52 });
53 const pid = yield* Effect.sync(() => process.pid);
54 yield* fs.writeFileString(
55 path.join(directory, "pids.json"),
56 JSON.stringify({ wrapper: pid, leaf: child.pid }),
57 );
58 yield* fs.exists(path.join(directory, "leaf.ready")).pipe(
59 Effect.repeat({
60 schedule: Schedule.spaced("10 millis"),
61 until: Boolean,
62 times: 500,
63 }),
64 );
65 if (mode === "cooperative") {
66 yield* Effect.addFinalizer(() =>
67 Effect.gen(function* () {
68 yield* fs.writeFileString(`${prefix}.term`, "TERM");
69 yield* Effect.sleep("150 millis");
70 yield* child.kill({ forceKillAfter: "200 millis" }).pipe(Effect.ignore);
71 yield* fs.writeFileString(`${prefix}.clean`, "clean");
72 }).pipe(Effect.orDie),
73 );
74 }
75 yield* fs.writeFileString(path.join(directory, "wrapper.ready"), "ready");
76 const server = yield* HttpServer.HttpServer;
77 yield* server.serve(Effect.succeed(HttpServerResponse.text("ready")));
78 yield* fs.writeFileString(
79 path.join(directory, "url"),
80 HttpServer.formatAddress(server.address),
81 );
82 yield* Effect.sync(() => console.log(HttpServer.formatAddress(server.address)));
83 }
84 if (mode === "crash" && !leaf) {
85 yield* fs.exists(path.join(directory, "crash")).pipe(
86 Effect.repeat({
87 schedule: Schedule.spaced("10 millis"),
88 until: Boolean,
89 times: 1000,
90 }),
91 );
92 yield* Effect.sync(() => process.exit(2));
93 }
94 yield* Effect.never;
95 });
96 const runnable = program.pipe(
97 Effect.scoped,
98 Effect.provide(
99 Layer.provideMerge(
100 httpServer(0, "127.0.0.1", { gracefulShutdownTimeout: 0 }),
101 PlatformServices,
102 ),
103 ),
104 );
105 if (mode === "cooperative") runMain(runnable);
106 else
107 void Effect.runPromise(runnable).catch((error) => {
108 console.error(error);
109 process.exitCode = 1;
110 });