Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

config.ts

209 lines7.4 KB

1/**
2 * ServerConfig - Runtime configuration services.
3 *
4 * Defines process-level server configuration and networking helpers used by
5 * startup and runtime layers.
6 *
7 * @module ServerConfig
8 */
9import * as Context from "effect/Context";
10import * as Effect from "effect/Effect";
11import * as FileSystem from "effect/FileSystem";
12import * as Layer from "effect/Layer";
13import * as LogLevel from "effect/LogLevel";
14import * as Path from "effect/Path";
15import * as Schema from "effect/Schema";
16​
17export const DEFAULT_PORT = 3773;
18​
19export const RuntimeMode = Schema.Literals(["web", "desktop"]);
20export type RuntimeMode = typeof RuntimeMode.Type;
21​
22export const StartupPresentation = Schema.Literals(["browser", "headless"]);
23export type StartupPresentation = typeof StartupPresentation.Type;
24​
25/**
26 * ServerDerivedPaths - Derived paths from the base directory.
27 */
28export interface ServerDerivedPaths {
29 readonly stateDir: string;
30 readonly dbPath: string;
31 readonly keybindingsConfigPath: string;
32 readonly settingsPath: string;
33 readonly providerStatusCacheDir: string;
34 readonly worktreesDir: string;
35 readonly attachmentsDir: string;
36 readonly logsDir: string;
37 readonly serverLogPath: string;
38 readonly serverTracePath: string;
39 readonly providerLogsDir: string;
40 readonly providerEventLogPath: string;
41 readonly terminalLogsDir: string;
42 readonly anonymousIdPath: string;
43 readonly environmentIdPath: string;
44 readonly serverRuntimeStatePath: string;
45 readonly secretsDir: string;
47​
48/**
49 * ServerConfig - Service tag for server runtime configuration.
50 */
51export class ServerConfig extends Context.Service<
52 ServerConfig,
53 ServerDerivedPaths & {
54 readonly logLevel: LogLevel.LogLevel;
55 readonly traceMinLevel: LogLevel.LogLevel;
56 readonly traceTimingEnabled: boolean;
57 readonly traceBatchWindowMs: number;
58 readonly traceMaxBytes: number;
59 readonly traceMaxFiles: number;
60 readonly otlpTracesUrl: string | undefined;
61 readonly otlpMetricsUrl: string | undefined;
62 readonly otlpExportIntervalMs: number;
63 readonly otlpServiceName: string;
64 readonly mode: RuntimeMode;
65 readonly port: number;
66 readonly host: string | undefined;
67 readonly cwd: string;
68 readonly baseDir: string;
69 readonly staticDir: string | undefined;
70 readonly devUrl: URL | undefined;
71 readonly noBrowser: boolean;
72 readonly startupPresentation: StartupPresentation;
73 readonly desktopBootstrapToken: string | undefined;
74 readonly autoBootstrapProjectFromCwd: boolean;
75 readonly logWebSocketEvents: boolean;
76 readonly tailscaleServeEnabled: boolean;
77 readonly tailscaleServePort: number;
78 }
79>()("v12/config/ServerConfig") {
80 /** @deprecated Import and use `layerTest` from this module. */
81 static readonly layerTest = (
82 cwd: string,
83 baseDirOrPrefix: string | { readonly prefix: string },
84 ) => layerTest(cwd, baseDirOrPrefix);
86​
87export const make = (config: ServerConfig["Service"]) => ServerConfig.of(config);
88​
89export const layer = (config: ServerConfig["Service"]) => Layer.succeed(ServerConfig, make(config));
90​
91export const deriveServerPaths = Effect.fn(function* (
92 baseDir: ServerConfig["Service"]["baseDir"],
93 devUrl: ServerConfig["Service"]["devUrl"],
94): Effect.fn.Return<ServerDerivedPaths, never, Path.Path> {
95 const { join } = yield* Path.Path;
96 const stateDir = join(baseDir, devUrl !== undefined ? "dev" : "userdata");
97 const dbPath = join(stateDir, "state.sqlite");
98 const attachmentsDir = join(stateDir, "attachments");
99 const logsDir = join(stateDir, "logs");
100 const providerLogsDir = join(logsDir, "provider");
101 const providerStatusCacheDir = join(baseDir, "caches");
102 return {
103 stateDir,
104 dbPath,
105 keybindingsConfigPath: join(stateDir, "keybindings.json"),
106 settingsPath: join(stateDir, "settings.json"),
107 providerStatusCacheDir,
108 worktreesDir: join(baseDir, "worktrees"),
109 attachmentsDir,
110 logsDir,
111 serverLogPath: join(logsDir, "server.log"),
112 serverTracePath: join(logsDir, "server.trace.ndjson"),
113 providerLogsDir,
114 providerEventLogPath: join(providerLogsDir, "events.log"),
115 terminalLogsDir: join(logsDir, "terminals"),
116 anonymousIdPath: join(stateDir, "anonymous-id"),
117 environmentIdPath: join(stateDir, "environment-id"),
118 serverRuntimeStatePath: join(stateDir, "server-runtime.json"),
119 secretsDir: join(stateDir, "secrets"),
120 };
121});
122​
123export const ensureServerDirectories = Effect.fn(function* (derivedPaths: ServerDerivedPaths) {
124 const fs = yield* FileSystem.FileSystem;
125 const path = yield* Path.Path;
126​
127 yield* Effect.all(
128 [
129 fs.makeDirectory(derivedPaths.stateDir, { recursive: true }),
130 fs.makeDirectory(derivedPaths.logsDir, { recursive: true }),
131 fs.makeDirectory(derivedPaths.providerLogsDir, { recursive: true }),
132 fs.makeDirectory(derivedPaths.terminalLogsDir, { recursive: true }),
133 fs.makeDirectory(derivedPaths.attachmentsDir, { recursive: true }),
134 fs.makeDirectory(derivedPaths.worktreesDir, { recursive: true }),
135 fs.makeDirectory(path.dirname(derivedPaths.keybindingsConfigPath), { recursive: true }),
136 fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true }),
137 fs.makeDirectory(derivedPaths.providerStatusCacheDir, { recursive: true }),
138 fs.makeDirectory(path.dirname(derivedPaths.anonymousIdPath), { recursive: true }),
139 fs.makeDirectory(path.dirname(derivedPaths.serverRuntimeStatePath), { recursive: true }),
140 ],
141 { concurrency: "unbounded" },
142 );
143});
144​
145const makeTest = Effect.fn("ServerConfig.makeTest")(function* (
146 cwd: string,
147 baseDirOrPrefix: string | { readonly prefix: string },
148) {
149 const devUrl = undefined;
150 const fs = yield* FileSystem.FileSystem;
151 const baseDir =
152 typeof baseDirOrPrefix === "string"
153 ? baseDirOrPrefix
154 : yield* fs.makeTempDirectoryScoped({ prefix: baseDirOrPrefix.prefix });
155 const derivedPaths = yield* deriveServerPaths(baseDir, devUrl);
156 yield* ensureServerDirectories(derivedPaths);
157​
158 return ServerConfig.of({
159 logLevel: "Error",
160 traceMinLevel: "Info",
161 traceTimingEnabled: true,
162 traceBatchWindowMs: 200,
163 traceMaxBytes: 10 * 1024 * 1024,
164 traceMaxFiles: 10,
165 otlpTracesUrl: undefined,
166 otlpMetricsUrl: undefined,
167 otlpExportIntervalMs: 10_000,
168 otlpServiceName: "v12-server",
169 cwd,
170 baseDir,
171 ...derivedPaths,
172 mode: "web",
173 autoBootstrapProjectFromCwd: false,
174 logWebSocketEvents: false,
175 tailscaleServeEnabled: false,
176 tailscaleServePort: 443,
177 port: 0,
178 host: undefined,
179 desktopBootstrapToken: undefined,
180 staticDir: undefined,
181 devUrl,
182 noBrowser: false,
183 startupPresentation: "browser",
184 });
185});
186​
187export const layerTest = (cwd: string, baseDirOrPrefix: string | { readonly prefix: string }) =>
188 Layer.effect(ServerConfig, makeTest(cwd, baseDirOrPrefix));
189​
190export const resolveStaticDir = Effect.fn(function* () {
191 const { join, resolve } = yield* Path.Path;
192 const { exists } = yield* FileSystem.FileSystem;
193 const bundledClient = resolve(join(import.meta.dirname, "client"));
194 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
195 Effect.orElseSucceed(() => false),
196 );
197 if (bundledStat) {
198 return bundledClient;
199 }
200​
201 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
202 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
203 Effect.orElseSucceed(() => false),
204 );
205 if (monorepoStat) {
206 return monorepoClient;
207 }
208 return undefined;
209});