| author | |
| committer | |
| log | e9ed849b53bc5644551aacb389f6c2094807371c |
| tree | 57478dad56ccd65df64747f7181687c6350d1c66 |
| parent | e8f5b4ad5b28b63563fe3812998077ffb343aec8 |
| signature | Signed by PGP key B5690EEEBB952194 |
Co-authored-by: codex <codex@users.noreply.github.com>9 files changed, 272 insertions(+), 3 deletions(-)
apps/desktop/src/main.ts+34-1| ... | ... | @@ -28,6 +28,7 @@ import { autoUpdater } from "electron-updater"; |
| 28 | 28 | import type { ContextMenuItem } from "@t3tools/contracts"; |
| 29 | 29 | import { NetService } from "@t3tools/shared/Net"; |
| 30 | 30 | import { RotatingFileSink } from "@t3tools/shared/logging"; |
| 31 | import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings"; | |
| 31 | 32 | import { showDesktopConfirmDialog } from "./confirmDialog"; |
| 32 | 33 | import { syncShellEnvironment } from "./syncShellEnvironment"; |
| 33 | 34 | import { getAutoUpdateDisabledReason, shouldBroadcastDownloadProgress } from "./updateState"; |
| ... | ... | @@ -76,6 +77,7 @@ const LOG_DIR = Path.join(STATE_DIR, "logs"); |
| 76 | 77 | const LOG_FILE_MAX_BYTES = 10 * 1024 * 1024; |
| 77 | 78 | const LOG_FILE_MAX_FILES = 10; |
| 78 | 79 | const APP_RUN_ID = Crypto.randomBytes(6).toString("hex"); |
| 80 | const SERVER_SETTINGS_PATH = Path.join(STATE_DIR, "settings.json"); | |
| 79 | 81 | const AUTO_UPDATE_STARTUP_DELAY_MS = 15_000; |
| 80 | 82 | const AUTO_UPDATE_POLL_INTERVAL_MS = 4 * 60 * 60 * 1000; |
| 81 | 83 | const DESKTOP_UPDATE_CHANNEL = "latest"; |
| ... | ... | @@ -99,8 +101,10 @@ let aboutCommitHashCache: string | null | undefined; |
| 99 | 101 | let desktopLogSink: RotatingFileSink | null = null; |
| 100 | 102 | let backendLogSink: RotatingFileSink | null = null; |
| 101 | 103 | let restoreStdIoCapture: (() => void) | null = null; |
| 104 | let backendObservabilitySettings = readPersistedBackendObservabilitySettings(); | |
| 102 | 105 | |
| 103 | 106 | let destructiveMenuIconCache: Electron.NativeImage | null | undefined; |
| 107 | const expectedBackendExitChildren = new WeakSet<ChildProcess.ChildProcess>(); | |
| 104 | 108 | const desktopRuntimeInfo = resolveDesktopRuntimeInfo({ |
| 105 | 109 | platform: process.platform, |
| 106 | 110 | processArch: process.arch, |
| ... | ... | @@ -121,6 +125,21 @@ function sanitizeLogValue(value: string): string { |
| 121 | 125 | return value.replace(/\s+/g, " ").trim(); |
| 122 | 126 | } |
| 123 | 127 | |
| 128 | function readPersistedBackendObservabilitySettings(): { | |
| 129 | readonly otlpTracesUrl: string | undefined; | |
| 130 | readonly otlpMetricsUrl: string | undefined; | |
| 131 | } { | |
| 132 | try { | |
| 133 | if (!FS.existsSync(SERVER_SETTINGS_PATH)) { | |
| 134 | return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined }; | |
| 135 | } | |
| 136 | return parsePersistedServerObservabilitySettings(FS.readFileSync(SERVER_SETTINGS_PATH, "utf8")); | |
| 137 | } catch (error) { | |
| 138 | console.warn("[desktop] failed to read persisted backend observability settings", error); | |
| 139 | return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined }; | |
| 140 | } | |
| 141 | } | |
| 142 | ||
| 124 | 143 | function backendChildEnv(): NodeJS.ProcessEnv { |
| 125 | 144 | const env = { ...process.env }; |
| 126 | 145 | delete env.T3CODE_PORT; |
| ... | ... | @@ -988,6 +1007,7 @@ function scheduleBackendRestart(reason: string): void { |
| 988 | 1007 | function startBackend(): void { |
| 989 | 1008 | if (isQuitting || backendProcess) return; |
| 990 | 1009 | |
| 1010 | backendObservabilitySettings = readPersistedBackendObservabilitySettings(); | |
| 991 | 1011 | const backendEntry = resolveBackendEntry(); |
| 992 | 1012 | if (!FS.existsSync(backendEntry)) { |
| 993 | 1013 | scheduleBackendRestart(`missing server entry at ${backendEntry}`); |
| ... | ... | @@ -1016,6 +1036,12 @@ function startBackend(): void { |
| 1016 | 1036 | port: backendPort, |
| 1017 | 1037 | t3Home: BASE_DIR, |
| 1018 | 1038 | authToken: backendAuthToken, |
| 1039 | ...(backendObservabilitySettings.otlpTracesUrl | |
| 1040 | ? { otlpTracesUrl: backendObservabilitySettings.otlpTracesUrl } | |
| 1041 | : {}), | |
| 1042 | ...(backendObservabilitySettings.otlpMetricsUrl | |
| 1043 | ? { otlpMetricsUrl: backendObservabilitySettings.otlpMetricsUrl } | |
| 1044 | : {}), | |
| 1019 | 1045 | })}\n`, |
| 1020 | 1046 | ); |
| 1021 | 1047 | bootstrapStream.end(); |
| ... | ... | @@ -1042,21 +1068,26 @@ function startBackend(): void { |
| 1042 | 1068 | }); |
| 1043 | 1069 | |
| 1044 | 1070 | child.on("error", (error) => { |
| 1071 | const wasExpected = expectedBackendExitChildren.has(child); | |
| 1045 | 1072 | if (backendProcess === child) { |
| 1046 | 1073 | backendProcess = null; |
| 1047 | 1074 | } |
| 1048 | 1075 | closeBackendSession(`pid=${child.pid ?? "unknown"} error=${error.message}`); |
| 1076 | if (wasExpected) { | |
| 1077 | return; | |
| 1078 | } | |
| 1049 | 1079 | scheduleBackendRestart(error.message); |
| 1050 | 1080 | }); |
| 1051 | 1081 | |
| 1052 | 1082 | child.on("exit", (code, signal) => { |
| 1083 | const wasExpected = expectedBackendExitChildren.has(child); | |
| 1053 | 1084 | if (backendProcess === child) { |
| 1054 | 1085 | backendProcess = null; |
| 1055 | 1086 | } |
| 1056 | 1087 | closeBackendSession( |
| 1057 | 1088 | `pid=${child.pid ?? "unknown"} code=${code ?? "null"} signal=${signal ?? "null"}`, |
| 1058 | 1089 | ); |
| 1059 | if (isQuitting) return; | |
| 1090 | if (isQuitting || wasExpected) return; | |
| 1060 | 1091 | const reason = `code=${code ?? "null"} signal=${signal ?? "null"}`; |
| 1061 | 1092 | scheduleBackendRestart(reason); |
| 1062 | 1093 | }); |
| ... | ... | @@ -1073,6 +1104,7 @@ function stopBackend(): void { |
| 1073 | 1104 | if (!child) return; |
| 1074 | 1105 | |
| 1075 | 1106 | if (child.exitCode === null && child.signalCode === null) { |
| 1107 | expectedBackendExitChildren.add(child); | |
| 1076 | 1108 | child.kill("SIGTERM"); |
| 1077 | 1109 | setTimeout(() => { |
| 1078 | 1110 | if (child.exitCode === null && child.signalCode === null) { |
| ... | ... | @@ -1093,6 +1125,7 @@ async function stopBackendAndWaitForExit(timeoutMs = 5_000): Promise<void> { |
| 1093 | 1125 | if (!child) return; |
| 1094 | 1126 | const backendChild = child; |
| 1095 | 1127 | if (backendChild.exitCode !== null || backendChild.signalCode !== null) return; |
| 1128 | expectedBackendExitChildren.add(backendChild); | |
| 1096 | 1129 | |
| 1097 | 1130 | await new Promise<void>((resolve) => { |
| 1098 | 1131 | let settled = false; |
apps/server/src/cli-config.test.ts+67| ... | ... | @@ -167,6 +167,8 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { |
| 167 | 167 | authToken: "bootstrap-token", |
| 168 | 168 | autoBootstrapProjectFromCwd: false, |
| 169 | 169 | logWebSocketEvents: true, |
| 170 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 171 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 170 | 172 | }); |
| 171 | 173 | const derivedPaths = yield* deriveServerPaths(baseDir, new URL("http://127.0.0.1:5173")); |
| 172 | 174 | |
| ... | ... | @@ -202,6 +204,8 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { |
| 202 | 204 | expect(resolved).toEqual({ |
| 203 | 205 | logLevel: "Info", |
| 204 | 206 | ...defaultObservabilityConfig, |
| 207 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 208 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 205 | 209 | mode: "desktop", |
| 206 | 210 | port: 4888, |
| 207 | 211 | cwd: process.cwd(), |
| ... | ... | @@ -332,4 +336,67 @@ it.layer(NodeServices.layer)("cli config resolution", (it) => { |
| 332 | 336 | }); |
| 333 | 337 | }), |
| 334 | 338 | ); |
| 339 | ||
| 340 | it.effect("falls back to persisted observability settings when env vars are absent", () => | |
| 341 | Effect.gen(function* () { | |
| 342 | const fs = yield* FileSystem.FileSystem; | |
| 343 | const path = yield* Path.Path; | |
| 344 | const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-cli-config-settings-" }); | |
| 345 | const derivedPaths = yield* deriveServerPaths(baseDir, undefined); | |
| 346 | yield* fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true }); | |
| 347 | yield* fs.writeFileString( | |
| 348 | derivedPaths.settingsPath, | |
| 349 | `${JSON.stringify({ | |
| 350 | observability: { | |
| 351 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 352 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 353 | }, | |
| 354 | })}\n`, | |
| 355 | ); | |
| 356 | ||
| 357 | const resolved = yield* resolveServerConfig( | |
| 358 | { | |
| 359 | mode: Option.some("desktop"), | |
| 360 | port: Option.some(4888), | |
| 361 | host: Option.none(), | |
| 362 | baseDir: Option.some(baseDir), | |
| 363 | devUrl: Option.none(), | |
| 364 | noBrowser: Option.none(), | |
| 365 | authToken: Option.none(), | |
| 366 | bootstrapFd: Option.none(), | |
| 367 | autoBootstrapProjectFromCwd: Option.none(), | |
| 368 | logWebSocketEvents: Option.none(), | |
| 369 | }, | |
| 370 | Option.none(), | |
| 371 | ).pipe( | |
| 372 | Effect.provide( | |
| 373 | Layer.mergeAll( | |
| 374 | ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} })), | |
| 375 | NetService.layer, | |
| 376 | ), | |
| 377 | ), | |
| 378 | ); | |
| 379 | ||
| 380 | expect(resolved.otlpTracesUrl).toBe("http://localhost:4318/v1/traces"); | |
| 381 | expect(resolved.otlpMetricsUrl).toBe("http://localhost:4318/v1/metrics"); | |
| 382 | expect(resolved).toEqual({ | |
| 383 | logLevel: "Info", | |
| 384 | ...defaultObservabilityConfig, | |
| 385 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 386 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 387 | mode: "desktop", | |
| 388 | port: 4888, | |
| 389 | cwd: process.cwd(), | |
| 390 | baseDir, | |
| 391 | ...derivedPaths, | |
| 392 | host: "127.0.0.1", | |
| 393 | staticDir: resolved.staticDir, | |
| 394 | devUrl: undefined, | |
| 395 | noBrowser: true, | |
| 396 | authToken: undefined, | |
| 397 | autoBootstrapProjectFromCwd: false, | |
| 398 | logWebSocketEvents: false, | |
| 399 | }); | |
| 400 | }), | |
| 401 | ); | |
| 335 | 402 | }); |
apps/server/src/cli.ts+33-2| ... | ... | @@ -1,4 +1,5 @@ |
| 1 | 1 | import { NetService } from "@t3tools/shared/Net"; |
| 2 | import { parsePersistedServerObservabilitySettings } from "@t3tools/shared/serverSettings"; | |
| 2 | 3 | import { Config, Effect, FileSystem, LogLevel, Option, Path, Schema } from "effect"; |
| 3 | 4 | import { Command, Flag, GlobalFlag } from "effect/unstable/cli"; |
| 4 | 5 | |
| ... | ... | @@ -27,6 +28,8 @@ const BootstrapEnvelopeSchema = Schema.Struct({ |
| 27 | 28 | authToken: Schema.optional(Schema.String), |
| 28 | 29 | autoBootstrapProjectFromCwd: Schema.optional(Schema.Boolean), |
| 29 | 30 | logWebSocketEvents: Schema.optional(Schema.Boolean), |
| 31 | otlpTracesUrl: Schema.optional(Schema.String), | |
| 32 | otlpMetricsUrl: Schema.optional(Schema.String), | |
| 30 | 33 | }); |
| 31 | 34 | |
| 32 | 35 | const modeFlag = Flag.choice("mode", RuntimeMode.literals).pipe( |
| ... | ... | @@ -152,6 +155,17 @@ const resolveOptionPrecedence = <Value>( |
| 152 | 155 | ...values: ReadonlyArray<Option.Option<Value>> |
| 153 | 156 | ): Option.Option<Value> => Option.firstSomeOf(values); |
| 154 | 157 | |
| 158 | const loadPersistedObservabilitySettings = Effect.fn(function* (settingsPath: string) { | |
| 159 | const fs = yield* FileSystem.FileSystem; | |
| 160 | const exists = yield* fs.exists(settingsPath).pipe(Effect.orElseSucceed(() => false)); | |
| 161 | if (!exists) { | |
| 162 | return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined }; | |
| 163 | } | |
| 164 | ||
| 165 | const raw = yield* fs.readFileString(settingsPath).pipe(Effect.orElseSucceed(() => "")); | |
| 166 | return parsePersistedServerObservabilitySettings(raw); | |
| 167 | }); | |
| 168 | ||
| 155 | 169 | export const resolveServerConfig = ( |
| 156 | 170 | flags: CliServerFlags, |
| 157 | 171 | cliLogLevel: Option.Option<LogLevel.LogLevel>, |
| ... | ... | @@ -213,6 +227,9 @@ export const resolveServerConfig = ( |
| 213 | 227 | ); |
| 214 | 228 | const derivedPaths = yield* deriveServerPaths(baseDir, devUrl); |
| 215 | 229 | yield* ensureServerDirectories(derivedPaths); |
| 230 | const persistedObservabilitySettings = yield* loadPersistedObservabilitySettings( | |
| 231 | derivedPaths.settingsPath, | |
| 232 | ); | |
| 216 | 233 | const serverTracePath = env.traceFile ?? derivedPaths.serverTracePath; |
| 217 | 234 | yield* fs.makeDirectory(path.dirname(serverTracePath), { recursive: true }); |
| 218 | 235 | const noBrowser = resolveBooleanFlag( |
| ... | ... | @@ -278,8 +295,22 @@ export const resolveServerConfig = ( |
| 278 | 295 | traceBatchWindowMs: env.traceBatchWindowMs, |
| 279 | 296 | traceMaxBytes: env.traceMaxBytes, |
| 280 | 297 | traceMaxFiles: env.traceMaxFiles, |
| 281 | otlpTracesUrl: env.otlpTracesUrl, | |
| 282 | otlpMetricsUrl: env.otlpMetricsUrl, | |
| 298 | otlpTracesUrl: | |
| 299 | env.otlpTracesUrl ?? | |
| 300 | Option.getOrUndefined( | |
| 301 | Option.flatMap(bootstrapEnvelope, (bootstrap) => | |
| 302 | Option.fromUndefinedOr(bootstrap.otlpTracesUrl), | |
| 303 | ), | |
| 304 | ) ?? | |
| 305 | persistedObservabilitySettings.otlpTracesUrl, | |
| 306 | otlpMetricsUrl: | |
| 307 | env.otlpMetricsUrl ?? | |
| 308 | Option.getOrUndefined( | |
| 309 | Option.flatMap(bootstrapEnvelope, (bootstrap) => | |
| 310 | Option.fromUndefinedOr(bootstrap.otlpMetricsUrl), | |
| 311 | ), | |
| 312 | ) ?? | |
| 313 | persistedObservabilitySettings.otlpMetricsUrl, | |
| 283 | 314 | otlpExportIntervalMs: env.otlpExportIntervalMs, |
| 284 | 315 | otlpServiceName: env.otlpServiceName, |
| 285 | 316 | mode, |
apps/server/src/serverSettings.test.ts+26| ... | ... | @@ -171,6 +171,24 @@ it.layer(NodeServices.layer)("server settings", (it) => { |
| 171 | 171 | }).pipe(Effect.provide(makeServerSettingsLayer())), |
| 172 | 172 | ); |
| 173 | 173 | |
| 174 | it.effect("trims observability settings when updates are applied", () => | |
| 175 | Effect.gen(function* () { | |
| 176 | const serverSettings = yield* ServerSettingsService; | |
| 177 | ||
| 178 | const next = yield* serverSettings.updateSettings({ | |
| 179 | observability: { | |
| 180 | otlpTracesUrl: " http://localhost:4318/v1/traces ", | |
| 181 | otlpMetricsUrl: " http://localhost:4318/v1/metrics ", | |
| 182 | }, | |
| 183 | }); | |
| 184 | ||
| 185 | assert.deepEqual(next.observability, { | |
| 186 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 187 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 188 | }); | |
| 189 | }).pipe(Effect.provide(makeServerSettingsLayer())), | |
| 190 | ); | |
| 191 | ||
| 174 | 192 | it.effect("defaults blank binary paths to provider executables", () => |
| 175 | 193 | Effect.gen(function* () { |
| 176 | 194 | const serverSettings = yield* ServerSettingsService; |
| ... | ... | @@ -197,6 +215,10 @@ it.layer(NodeServices.layer)("server settings", (it) => { |
| 197 | 215 | const serverConfig = yield* ServerConfig; |
| 198 | 216 | const fileSystem = yield* FileSystem.FileSystem; |
| 199 | 217 | const next = yield* serverSettings.updateSettings({ |
| 218 | observability: { | |
| 219 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 220 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 221 | }, | |
| 200 | 222 | providers: { |
| 201 | 223 | codex: { |
| 202 | 224 | binaryPath: "/opt/homebrew/bin/codex", |
| ... | ... | @@ -208,6 +230,10 @@ it.layer(NodeServices.layer)("server settings", (it) => { |
| 208 | 230 | |
| 209 | 231 | const raw = yield* fileSystem.readFileString(serverConfig.settingsPath); |
| 210 | 232 | assert.deepEqual(JSON.parse(raw), { |
| 233 | observability: { | |
| 234 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 235 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 236 | }, | |
| 211 | 237 | providers: { |
| 212 | 238 | codex: { |
| 213 | 239 | binaryPath: "/opt/homebrew/bin/codex", |
apps/web/src/components/KeybindingsToast.browser.tsx+2| ... | ... | @@ -1,6 +1,7 @@ |
| 1 | 1 | import "../index.css"; |
| 2 | 2 | |
| 3 | 3 | import { |
| 4 | DEFAULT_SERVER_SETTINGS, | |
| 4 | 5 | ORCHESTRATION_WS_METHODS, |
| 5 | 6 | type MessageId, |
| 6 | 7 | type OrchestrationReadModel, |
| ... | ... | @@ -63,6 +64,7 @@ function createBaseServerConfig(): ServerConfig { |
| 63 | 64 | otlpMetricsEnabled: false, |
| 64 | 65 | }, |
| 65 | 66 | settings: { |
| 67 | ...DEFAULT_SERVER_SETTINGS, | |
| 66 | 68 | enableAssistantStreaming: false, |
| 67 | 69 | defaultThreadEnvMode: "local" as const, |
| 68 | 70 | textGenerationModelSelection: { provider: "codex" as const, model: "gpt-5.4-mini" }, |
packages/contracts/src/settings.ts+13| ... | ... | @@ -71,6 +71,12 @@ export const ClaudeSettings = Schema.Struct({ |
| 71 | 71 | }); |
| 72 | 72 | export type ClaudeSettings = typeof ClaudeSettings.Type; |
| 73 | 73 | |
| 74 | export const ObservabilitySettings = Schema.Struct({ | |
| 75 | otlpTracesUrl: TrimmedString.pipe(Schema.withDecodingDefault(() => "")), | |
| 76 | otlpMetricsUrl: TrimmedString.pipe(Schema.withDecodingDefault(() => "")), | |
| 77 | }); | |
| 78 | export type ObservabilitySettings = typeof ObservabilitySettings.Type; | |
| 79 | ||
| 74 | 80 | export const ServerSettings = Schema.Struct({ |
| 75 | 81 | enableAssistantStreaming: Schema.Boolean.pipe(Schema.withDecodingDefault(() => false)), |
| 76 | 82 | defaultThreadEnvMode: ThreadEnvMode.pipe( |
| ... | ... | @@ -88,6 +94,7 @@ export const ServerSettings = Schema.Struct({ |
| 88 | 94 | codex: CodexSettings.pipe(Schema.withDecodingDefault(() => ({}))), |
| 89 | 95 | claudeAgent: ClaudeSettings.pipe(Schema.withDecodingDefault(() => ({}))), |
| 90 | 96 | }).pipe(Schema.withDecodingDefault(() => ({}))), |
| 97 | observability: ObservabilitySettings.pipe(Schema.withDecodingDefault(() => ({}))), | |
| 91 | 98 | }); |
| 92 | 99 | export type ServerSettings = typeof ServerSettings.Type; |
| 93 | 100 | |
| ... | ... | @@ -158,6 +165,12 @@ export const ServerSettingsPatch = Schema.Struct({ |
| 158 | 165 | enableAssistantStreaming: Schema.optionalKey(Schema.Boolean), |
| 159 | 166 | defaultThreadEnvMode: Schema.optionalKey(ThreadEnvMode), |
| 160 | 167 | textGenerationModelSelection: Schema.optionalKey(ModelSelectionPatch), |
| 168 | observability: Schema.optionalKey( | |
| 169 | Schema.Struct({ | |
| 170 | otlpTracesUrl: Schema.optionalKey(Schema.String), | |
| 171 | otlpMetricsUrl: Schema.optionalKey(Schema.String), | |
| 172 | }), | |
| 173 | ), | |
| 161 | 174 | providers: Schema.optionalKey( |
| 162 | 175 | Schema.Struct({ |
| 163 | 176 | codex: Schema.optionalKey(CodexSettingsPatch), |
packages/shared/package.json+4| ... | ... | @@ -40,6 +40,10 @@ |
| 40 | 40 | "types": "./src/Struct.ts", |
| 41 | 41 | "import": "./src/Struct.ts" |
| 42 | 42 | }, |
| 43 | "./serverSettings": { | |
| 44 | "types": "./src/serverSettings.ts", | |
| 45 | "import": "./src/serverSettings.ts" | |
| 46 | }, | |
| 43 | 47 | "./String": { |
| 44 | 48 | "types": "./src/String.ts", |
| 45 | 49 | "import": "./src/String.ts" |
packages/shared/src/serverSettings.test.ts created+53| ... | ... | @@ -0,0 +1,53 @@ |
| 1 | import { describe, expect, it } from "vitest"; | |
| 2 | import { | |
| 3 | extractPersistedServerObservabilitySettings, | |
| 4 | normalizePersistedServerSettingString, | |
| 5 | parsePersistedServerObservabilitySettings, | |
| 6 | } from "./serverSettings"; | |
| 7 | ||
| 8 | describe("serverSettings helpers", () => { | |
| 9 | it("normalizes optional persisted strings", () => { | |
| 10 | expect(normalizePersistedServerSettingString(undefined)).toBeUndefined(); | |
| 11 | expect(normalizePersistedServerSettingString(" ")).toBeUndefined(); | |
| 12 | expect(normalizePersistedServerSettingString(" http://localhost:4318/v1/traces ")).toBe( | |
| 13 | "http://localhost:4318/v1/traces", | |
| 14 | ); | |
| 15 | }); | |
| 16 | ||
| 17 | it("extracts persisted observability settings", () => { | |
| 18 | expect( | |
| 19 | extractPersistedServerObservabilitySettings({ | |
| 20 | observability: { | |
| 21 | otlpTracesUrl: " http://localhost:4318/v1/traces ", | |
| 22 | otlpMetricsUrl: " http://localhost:4318/v1/metrics ", | |
| 23 | }, | |
| 24 | }), | |
| 25 | ).toEqual({ | |
| 26 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 27 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 28 | }); | |
| 29 | }); | |
| 30 | ||
| 31 | it("parses lenient persisted settings JSON", () => { | |
| 32 | expect( | |
| 33 | parsePersistedServerObservabilitySettings( | |
| 34 | JSON.stringify({ | |
| 35 | observability: { | |
| 36 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 37 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 38 | }, | |
| 39 | }), | |
| 40 | ), | |
| 41 | ).toEqual({ | |
| 42 | otlpTracesUrl: "http://localhost:4318/v1/traces", | |
| 43 | otlpMetricsUrl: "http://localhost:4318/v1/metrics", | |
| 44 | }); | |
| 45 | }); | |
| 46 | ||
| 47 | it("falls back cleanly when persisted settings are invalid", () => { | |
| 48 | expect(parsePersistedServerObservabilitySettings("{")).toEqual({ | |
| 49 | otlpTracesUrl: undefined, | |
| 50 | otlpMetricsUrl: undefined, | |
| 51 | }); | |
| 52 | }); | |
| 53 | }); |
packages/shared/src/serverSettings.ts created+40| ... | ... | @@ -0,0 +1,40 @@ |
| 1 | import { ServerSettings } from "@t3tools/contracts"; | |
| 2 | import { Schema } from "effect"; | |
| 3 | import { fromLenientJson } from "./schemaJson"; | |
| 4 | ||
| 5 | const ServerSettingsJson = fromLenientJson(ServerSettings); | |
| 6 | ||
| 7 | export interface PersistedServerObservabilitySettings { | |
| 8 | readonly otlpTracesUrl: string | undefined; | |
| 9 | readonly otlpMetricsUrl: string | undefined; | |
| 10 | } | |
| 11 | ||
| 12 | export function normalizePersistedServerSettingString( | |
| 13 | value: string | null | undefined, | |
| 14 | ): string | undefined { | |
| 15 | const trimmed = value?.trim(); | |
| 16 | return trimmed && trimmed.length > 0 ? trimmed : undefined; | |
| 17 | } | |
| 18 | ||
| 19 | export function extractPersistedServerObservabilitySettings(input: { | |
| 20 | readonly observability?: { | |
| 21 | readonly otlpTracesUrl?: string; | |
| 22 | readonly otlpMetricsUrl?: string; | |
| 23 | }; | |
| 24 | }): PersistedServerObservabilitySettings { | |
| 25 | return { | |
| 26 | otlpTracesUrl: normalizePersistedServerSettingString(input.observability?.otlpTracesUrl), | |
| 27 | otlpMetricsUrl: normalizePersistedServerSettingString(input.observability?.otlpMetricsUrl), | |
| 28 | }; | |
| 29 | } | |
| 30 | ||
| 31 | export function parsePersistedServerObservabilitySettings( | |
| 32 | raw: string, | |
| 33 | ): PersistedServerObservabilitySettings { | |
| 34 | try { | |
| 35 | const decoded = Schema.decodeUnknownSync(ServerSettingsJson)(raw); | |
| 36 | return extractPersistedServerObservabilitySettings(decoded); | |
| 37 | } catch { | |
| 38 | return { otlpTracesUrl: undefined, otlpMetricsUrl: undefined }; | |
| 39 | } | |
| 40 | } |