@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, 04:45 UTC

main
ts 53 lines 2.21 KB
Raw
1 import * as NodeRuntime from "@effect/platform-node/NodeRuntime";
2 import * as NodeServices from "@effect/platform-node/NodeServices";
3 import * as Cause from "effect/Cause";
4 import * as ConfigProvider from "effect/ConfigProvider";
5 import * as Effect from "effect/Effect";
6 import * as Layer from "effect/Layer";
7 import * as Logger from "effect/Logger";
8 import * as Scope from "effect/Scope";
9 import * as Framework from "./Framework.ts";
10 import * as Server from "./Server.ts";
11
12 export { Cwd } from "./Cwd.ts";
13
14 /**
15 * Effect's default logger minus annotations: framework child output carries an
16 * `alchemy.stream` annotation that would otherwise trail every line.
17 */
18 const logger = Logger.make<unknown, void>(({ message, logLevel, cause, date }) => {
19 const parts = Array.isArray(message) ? message : [message];
20 const text = parts.map((part) => (typeof part === "string" ? part : String(part))).join(" ");
21 console.log(`[${date.toISOString().slice(11, 23)}] ${logLevel.toUpperCase()}: ${text}`);
22 if (cause.reasons.length > 0) console.log(Cause.pretty(cause));
23 });
24
25 export const layer = Server.layer.pipe(
26 Layer.provideMerge(Logger.layer([logger])),
27 Layer.provideMerge(Framework.layer),
28 Layer.provideMerge(
29 ConfigProvider.layer(
30 ConfigProvider.fromDotEnv().pipe(Effect.orElseSucceed(() => ConfigProvider.fromEnv())),
31 ),
32 ),
33 Layer.provideMerge(NodeServices.layer),
34 );
35
36 export const runMain = <A, E>(effect: Effect.Effect<A, E, Scope.Scope>) => {
37 const scope = Scope.makeUnsafe();
38 return NodeRuntime.runMain(effect.pipe(Scope.provide(scope)), {
39 teardown: (exit, onExit) => {
40 void Effect.runPromise(Scope.close(scope, exit)).then(() => {
41 const code = exit._tag === "Success" ? 0 : 1;
42 onExit(code);
43 // `NodeRuntime.runMain` only force-exits on failure/signal; on success
44 // it waits for the event loop to drain. The runtime layers can leave
45 // lingering handles behind (e.g. `cloudflare-runtime`'s docker proxy
46 // listener is started on a detached fiber with no finalizer, keeping
47 // the loop alive forever after a successful `e2e build`). A CLI must
48 // exit deterministically once its teardown has run.
49 process.exit(code);
50 });
51 },
52 });
53 };