The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 2 | import * as Config from "effect/Config"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as FileSystem from "effect/FileSystem"; |
| 5 | import * as Option from "effect/Option"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | |
| 8 | import * as ProcessRunner from "../processRunner.ts"; |
| 9 | |
| 10 | interface ResolveServerEnvironmentLabelInput { |
| 11 | readonly cwdBaseName: string; |
| 12 | } |
| 13 | |
| 14 | const ServerEnvironmentLabelCommandProbe = Schema.Literals([ |
| 15 | "macos-computer-name", |
| 16 | "linux-pretty-hostname", |
| 17 | ]); |
| 18 | type ServerEnvironmentLabelCommandProbe = typeof ServerEnvironmentLabelCommandProbe.Type; |
| 19 | |
| 20 | export class ServerEnvironmentLabelFileError extends Schema.TaggedError<ServerEnvironmentLabelFileError>()( |
| 21 | "ServerEnvironmentLabelFileError", |
| 22 | { |
| 23 | operation: Schema.Literals(["inspect", "read"]), |
| 24 | path: Schema.String, |
| 25 | cause: Schema.Defect(), |
| 26 | }, |
| 27 | ) { |
| 28 | override get message(): string { |
| 29 | return `Failed to ${this.operation} environment-label file at ${this.path}.`; |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | export class ServerEnvironmentLabelCommandError extends Schema.TaggedError<ServerEnvironmentLabelCommandError>()( |
| 34 | "ServerEnvironmentLabelCommandError", |
| 35 | { |
| 36 | probe: ServerEnvironmentLabelCommandProbe, |
| 37 | executable: Schema.String, |
| 38 | argumentCount: Schema.Number, |
| 39 | cause: Schema.Defect(), |
| 40 | }, |
| 41 | ) { |
| 42 | override get message(): string { |
| 43 | return `Failed to run environment-label probe '${this.probe}' with ${this.executable}.`; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | function normalizeLabel(value: string | null | undefined): string | null { |
| 48 | const trimmed = value?.trim(); |
| 49 | return trimmed && trimmed.length > 0 ? trimmed : null; |
| 50 | } |
| 51 | |
| 52 | function parseMachineInfoValue(raw: string, key: string): string | null { |
| 53 | for (const line of raw.split(/\r?\n/g)) { |
| 54 | const trimmed = line.trim(); |
| 55 | if (trimmed.length === 0 || trimmed.startsWith("#") || !trimmed.startsWith(`${key}=`)) { |
| 56 | continue; |
| 57 | } |
| 58 | const value = trimmed.slice(key.length + 1).trim(); |
| 59 | if ( |
| 60 | (value.startsWith('"') && value.endsWith('"')) || |
| 61 | (value.startsWith("'") && value.endsWith("'")) |
| 62 | ) { |
| 63 | return normalizeLabel(value.slice(1, -1)); |
| 64 | } |
| 65 | return normalizeLabel(value); |
| 66 | } |
| 67 | return null; |
| 68 | } |
| 69 | |
| 70 | const readLinuxMachineInfo = Effect.fn("readLinuxMachineInfo")(function* () { |
| 71 | const fileSystem = yield* FileSystem.FileSystem; |
| 72 | const machineInfoPath = "/etc/machine-info"; |
| 73 | return yield* fileSystem.exists(machineInfoPath).pipe( |
| 74 | Effect.mapError( |
| 75 | (cause) => |
| 76 | new ServerEnvironmentLabelFileError({ |
| 77 | operation: "inspect", |
| 78 | path: machineInfoPath, |
| 79 | cause, |
| 80 | }), |
| 81 | ), |
| 82 | Effect.flatMap((exists) => |
| 83 | exists |
| 84 | ? fileSystem.readFileString(machineInfoPath).pipe( |
| 85 | Effect.mapError( |
| 86 | (cause) => |
| 87 | new ServerEnvironmentLabelFileError({ |
| 88 | operation: "read", |
| 89 | path: machineInfoPath, |
| 90 | cause, |
| 91 | }), |
| 92 | ), |
| 93 | ) |
| 94 | : Effect.succeed(null), |
| 95 | ), |
| 96 | Effect.catchTags({ |
| 97 | ServerEnvironmentLabelFileError: (error) => |
| 98 | Effect.logDebug(error.message).pipe( |
| 99 | Effect.annotateLogs({ |
| 100 | operation: error.operation, |
| 101 | path: error.path, |
| 102 | cause: error, |
| 103 | }), |
| 104 | Effect.as(null), |
| 105 | ), |
| 106 | }), |
| 107 | ); |
| 108 | }); |
| 109 | |
| 110 | const runFriendlyLabelCommand = Effect.fn("runFriendlyLabelCommand")(function* (input: { |
| 111 | readonly probe: ServerEnvironmentLabelCommandProbe; |
| 112 | readonly command: string; |
| 113 | readonly args: readonly string[]; |
| 114 | }) { |
| 115 | const processRunner = yield* ProcessRunner.ProcessRunner; |
| 116 | const result = yield* processRunner |
| 117 | .run({ |
| 118 | command: input.command, |
| 119 | args: input.args, |
| 120 | // Server startup waits on this; hostnamectl can block on D-Bus when hostnamed is down. |
| 121 | timeout: "5 seconds", |
| 122 | timeoutBehavior: "timedOutResult", |
| 123 | }) |
| 124 | .pipe( |
| 125 | Effect.mapError( |
| 126 | (cause) => |
| 127 | new ServerEnvironmentLabelCommandError({ |
| 128 | probe: input.probe, |
| 129 | executable: input.command, |
| 130 | argumentCount: input.args.length, |
| 131 | cause, |
| 132 | }), |
| 133 | ), |
| 134 | Effect.asSome, |
| 135 | Effect.catchTags({ |
| 136 | ServerEnvironmentLabelCommandError: (error) => |
| 137 | Effect.logDebug(error.message).pipe( |
| 138 | Effect.annotateLogs({ |
| 139 | probe: error.probe, |
| 140 | executable: error.executable, |
| 141 | argumentCount: error.argumentCount, |
| 142 | cause: error, |
| 143 | }), |
| 144 | Effect.as(Option.none()), |
| 145 | ), |
| 146 | }), |
| 147 | ); |
| 148 | |
| 149 | if (Option.isNone(result) || result.value.code !== 0) { |
| 150 | return null; |
| 151 | } |
| 152 | |
| 153 | return normalizeLabel(result.value.stdout); |
| 154 | }); |
| 155 | |
| 156 | const resolveFriendlyHostLabel = Effect.fn("resolveFriendlyHostLabel")(function* () { |
| 157 | const platform = yield* HostProcess.Platform; |
| 158 | if (platform === "darwin") { |
| 159 | return yield* runFriendlyLabelCommand({ |
| 160 | probe: "macos-computer-name", |
| 161 | command: "scutil", |
| 162 | args: ["--get", "ComputerName"], |
| 163 | }); |
| 164 | } |
| 165 | |
| 166 | if (platform === "linux") { |
| 167 | const machineInfo = normalizeLabel(yield* readLinuxMachineInfo()); |
| 168 | if (machineInfo) { |
| 169 | const prettyHostname = parseMachineInfoValue(machineInfo, "PRETTY_HOSTNAME"); |
| 170 | if (prettyHostname) { |
| 171 | return prettyHostname; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return yield* runFriendlyLabelCommand({ |
| 176 | probe: "linux-pretty-hostname", |
| 177 | command: "hostnamectl", |
| 178 | args: ["--pretty"], |
| 179 | }); |
| 180 | } |
| 181 | |
| 182 | return null; |
| 183 | }); |
| 184 | |
| 185 | const isWsl = Effect.fn("isWsl")(function* () { |
| 186 | if ((yield* HostProcess.Platform) !== "linux") { |
| 187 | return false; |
| 188 | } |
| 189 | const fileSystem = yield* FileSystem.FileSystem; |
| 190 | const kernelRelease = yield* fileSystem |
| 191 | .readFileString("/proc/sys/kernel/osrelease") |
| 192 | .pipe(Effect.orElseSucceed(() => "")); |
| 193 | return kernelRelease.toLowerCase().includes("microsoft"); |
| 194 | }); |
| 195 | |
| 196 | // Names this environment instead of the machine, e.g. for staged demos and scenario captures. |
| 197 | const labelOverride = Config.String("T3CODE_ENVIRONMENT_LABEL").pipe( |
| 198 | Config.option, |
| 199 | Config.map(Option.flatMap((label) => Option.fromNullishOr(normalizeLabel(label)))), |
| 200 | ); |
| 201 | |
| 202 | export const resolveServerEnvironmentLabel = Effect.fn("resolveServerEnvironmentLabel")(function* ( |
| 203 | input: ResolveServerEnvironmentLabelInput, |
| 204 | ) { |
| 205 | const override = yield* labelOverride.pipe(Effect.orElseSucceed(() => Option.none<string>())); |
| 206 | if (Option.isSome(override)) { |
| 207 | return override.value; |
| 208 | } |
| 209 | |
| 210 | const friendlyHostLabel = yield* resolveFriendlyHostLabel(); |
| 211 | if (friendlyHostLabel) { |
| 212 | return friendlyHostLabel; |
| 213 | } |
| 214 | |
| 215 | const hostname = normalizeLabel(yield* HostProcess.Hostname); |
| 216 | if (hostname) { |
| 217 | // WSL inherits the Windows hostname, so qualify it to tell the two environments apart. |
| 218 | if (yield* isWsl()) { |
| 219 | const distro = normalizeLabel((yield* HostProcess.Environment).WSL_DISTRO_NAME); |
| 220 | return distro ? `${hostname} WSL (${distro})` : `${hostname} WSL`; |
| 221 | } |
| 222 | return hostname; |
| 223 | } |
| 224 | |
| 225 | return normalizeLabel(input.cwdBaseName) ?? "T3 environment"; |
| 226 | }); |