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

main
ts 135 lines 4.06 KB
Raw
1 import * as Context from "effect/Context";
2 import * as Effect from "effect/Effect";
3 import * as Schema from "effect/Schema";
4 import * as NodeDns from "node:dns";
5 import * as NodeOS from "node:os";
6 import * as NodeSea from "node:sea";
7
8 export const Platform = Context.Reference<NodeJS.Platform>("@t3tools/shared/HostProcess/Platform", {
9 defaultValue: () => process.platform,
10 });
11
12 export const Architecture = Context.Reference<NodeJS.Architecture>(
13 "@t3tools/shared/HostProcess/Architecture",
14 {
15 defaultValue: () => process.arch,
16 },
17 );
18
19 export const KernelRelease = Context.Reference<string>(
20 "@t3tools/shared/HostProcess/KernelRelease",
21 {
22 defaultValue: () => NodeOS.release(),
23 },
24 );
25
26 export const LinuxLibc = Context.Reference<"gnu" | "musl">(
27 "@t3tools/shared/HostProcess/LinuxLibc",
28 {
29 defaultValue: () => {
30 try {
31 const report = process.report?.getReport() as
32 | { header?: { glibcVersionRuntime?: unknown } }
33 | undefined;
34 return typeof report?.header?.glibcVersionRuntime === "string" ? "gnu" : "musl";
35 } catch {
36 return "musl";
37 }
38 },
39 },
40 );
41
42 export const Hostname = Context.Reference<string>("@t3tools/shared/HostProcess/Hostname", {
43 defaultValue: () => NodeOS.hostname(),
44 });
45
46 export const Username = Context.Reference<string | null>("@t3tools/shared/HostProcess/Username", {
47 defaultValue: () => {
48 try {
49 return NodeOS.userInfo().username.trim() || null;
50 } catch {
51 return null;
52 }
53 },
54 });
55
56 export const HomeDirectory = Context.Reference<string>(
57 "@t3tools/shared/HostProcess/HomeDirectory",
58 {
59 defaultValue: () => NodeOS.homedir(),
60 },
61 );
62
63 export const Environment = Context.Reference<NodeJS.ProcessEnv>(
64 "@t3tools/shared/HostProcess/Environment",
65 {
66 defaultValue: () => process.env,
67 },
68 );
69
70 export const WorkingDirectory = Context.Reference<string>(
71 "@t3tools/shared/HostProcess/WorkingDirectory",
72 {
73 defaultValue: () => process.cwd(),
74 },
75 );
76
77 export const ExecutablePath = Context.Reference<string>(
78 "@t3tools/shared/HostProcess/ExecutablePath",
79 {
80 defaultValue: () => process.execPath,
81 },
82 );
83
84 export const Arguments = Context.Reference<ReadonlyArray<string>>(
85 "@t3tools/shared/HostProcess/Arguments",
86 {
87 defaultValue: () => process.argv,
88 },
89 );
90
91 /** Original shell command spelling; argv[0] and execPath instead identify the resolved binary. */
92 export const InvokedAs = Context.Reference<string>("@t3tools/shared/HostProcess/InvokedAs", {
93 defaultValue: () => process.argv0,
94 });
95
96 export const IsExecutable = Context.Reference<boolean>("@t3tools/shared/HostProcess/IsExecutable", {
97 defaultValue: () => NodeSea.isSea(),
98 });
99
100 export class HostnameLookupError extends Schema.TaggedError<HostnameLookupError>()(
101 "HostnameLookupError",
102 { hostname: Schema.String, cause: Schema.Defect() },
103 ) {
104 override get message(): string {
105 return `Could not resolve ${this.hostname}.`;
106 }
107 }
108
109 /** Includes hostname-resolved addresses absent from interfaces, such as Debian's 127.0.1.1 used by Firefox locks. */
110 export const Addresses = Context.Reference<Effect.Effect<ReadonlySet<string>>>(
111 "@t3tools/shared/HostProcess/Addresses",
112 {
113 // The enclosing operation owns the span for this inline handler.
114 defaultValue: Effect.fnUntraced(function* () {
115 const interfaces = Object.values(NodeOS.networkInterfaces())
116 .flat()
117 .flatMap((entry) => (entry ? [entry.address] : []));
118 const hostname = NodeOS.hostname();
119 const resolved = yield* Effect.tryPromise({
120 try: () => NodeDns.promises.lookup(hostname, { all: true }),
121 catch: (cause) => new HostnameLookupError({ hostname, cause }),
122 }).pipe(
123 Effect.map((entries) => entries.map((entry) => entry.address)),
124 Effect.orElseSucceed(() => [] as ReadonlyArray<string>),
125 );
126 return new Set([...interfaces, ...resolved]);
127 }),
128 },
129 );
130
131 export const UserId = Context.Reference<number | undefined>("@t3tools/shared/HostProcess/UserId", {
132 defaultValue: () => process.getuid?.(),
133 });
134
135 export const isWindows = Effect.map(Platform, (platform) => platform === "win32");