Open History to see the last commit
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;
46}
47
48export interface DeriveServerPathsOptions {
49 readonly baseDirIsExplicit?: boolean;
50}
51
52/**
53 * ServerConfig - Service tag for server runtime configuration.
54 */
55export class ServerConfig extends Context.Service<
56 ServerConfig,
57 ServerDerivedPaths & {
58 readonly logLevel: LogLevel.LogLevel;
59 readonly traceMinLevel: LogLevel.LogLevel;
60 readonly traceTimingEnabled: boolean;
61 readonly traceBatchWindowMs: number;
62 readonly traceMaxBytes: number;
63 readonly traceMaxFiles: number;
64 readonly otlpTracesUrl: string | undefined;
65 readonly otlpMetricsUrl: string | undefined;
66 readonly otlpExportIntervalMs: number;
67 readonly otlpServiceName: string;
68 readonly mode: RuntimeMode;
69 readonly port: number;
70 readonly host: string | undefined;
71 readonly cwd: string;
72 readonly baseDir: string;
73 readonly staticDir: string | undefined;
74 readonly devUrl: URL | undefined;
75 readonly noBrowser: boolean;
76 readonly startupPresentation: StartupPresentation;
77 readonly desktopBootstrapToken: string | undefined;
78 readonly autoBootstrapProjectFromCwd: boolean;
79 readonly logWebSocketEvents: boolean;
80 readonly tailscaleServeEnabled: boolean;
81 readonly tailscaleServePort: number;
82 }
83>()("t3/config/ServerConfig") {
84 /** @deprecated Import and use `layerTest` from this module. */
85 static readonly layerTest = (
86 cwd: string,
87 baseDirOrPrefix: string | { readonly prefix: string },
88 ) => layerTest(cwd, baseDirOrPrefix);
89}
90
91export const make = (config: ServerConfig["Service"]) => ServerConfig.of(config);
92
93export const layer = (config: ServerConfig["Service"]) => Layer.succeed(ServerConfig, make(config));
94
95export const deriveServerPaths = Effect.fn(function* (
96 baseDir: ServerConfig["Service"]["baseDir"],
97 devUrl: ServerConfig["Service"]["devUrl"],
98 options: DeriveServerPathsOptions = {},
99): Effect.fn.Return<ServerDerivedPaths, never, Path.Path> {
100 const { join } = yield* Path.Path;
101 const stateDir = join(
102 baseDir,
103 devUrl !== undefined && !options.baseDirIsExplicit ? "dev" : "userdata",
104 );
105 const dbPath = join(stateDir, "state.sqlite");
106 const attachmentsDir = join(stateDir, "attachments");
107 const logsDir = join(stateDir, "logs");
108 const providerLogsDir = join(logsDir, "provider");
109 const providerStatusCacheDir = join(baseDir, "caches");
110 return {
111 stateDir,
112 dbPath,
113 keybindingsConfigPath: join(stateDir, "keybindings.json"),
114 settingsPath: join(stateDir, "settings.json"),
115 providerStatusCacheDir,
116 worktreesDir: join(baseDir, "worktrees"),
117 attachmentsDir,
118 logsDir,
119 serverLogPath: join(logsDir, "server.log"),
120 serverTracePath: join(logsDir, "server.trace.ndjson"),
121 providerLogsDir,
122 providerEventLogPath: join(providerLogsDir, "events.log"),
123 terminalLogsDir: join(logsDir, "terminals"),
124 anonymousIdPath: join(stateDir, "anonymous-id"),
125 environmentIdPath: join(stateDir, "environment-id"),
126 serverRuntimeStatePath: join(stateDir, "server-runtime.json"),
127 secretsDir: join(stateDir, "secrets"),
128 };
129});
130
131export const ensureServerDirectories = Effect.fn(function* (derivedPaths: ServerDerivedPaths) {
132 const fs = yield* FileSystem.FileSystem;
133 const path = yield* Path.Path;
134
135 yield* Effect.all(
136 [
137 fs.makeDirectory(derivedPaths.stateDir, { recursive: true }),
138 fs.makeDirectory(derivedPaths.logsDir, { recursive: true }),
139 fs.makeDirectory(derivedPaths.providerLogsDir, { recursive: true }),
140 fs.makeDirectory(derivedPaths.terminalLogsDir, { recursive: true }),
141 fs.makeDirectory(derivedPaths.attachmentsDir, { recursive: true }),
142 fs.makeDirectory(derivedPaths.worktreesDir, { recursive: true }),
143 fs.makeDirectory(path.dirname(derivedPaths.keybindingsConfigPath), { recursive: true }),
144 fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true }),
145 fs.makeDirectory(derivedPaths.providerStatusCacheDir, { recursive: true }),
146 fs.makeDirectory(path.dirname(derivedPaths.anonymousIdPath), { recursive: true }),
147 fs.makeDirectory(path.dirname(derivedPaths.serverRuntimeStatePath), { recursive: true }),
148 ],
149 { concurrency: "unbounded" },
150 );
151});
152
153const makeTest = Effect.fn("ServerConfig.makeTest")(function* (
154 cwd: string,
155 baseDirOrPrefix: string | { readonly prefix: string },
156) {
157 const devUrl = undefined;
158 const fs = yield* FileSystem.FileSystem;
159 const baseDir =
160 typeof baseDirOrPrefix === "string"
161 ? baseDirOrPrefix
162 : yield* fs.makeTempDirectoryScoped({ prefix: baseDirOrPrefix.prefix });
163 const derivedPaths = yield* deriveServerPaths(baseDir, devUrl);
164 yield* ensureServerDirectories(derivedPaths);
165
166 return ServerConfig.of({
167 logLevel: "Error",
168 traceMinLevel: "Info",
169 traceTimingEnabled: true,
170 traceBatchWindowMs: 200,
171 traceMaxBytes: 10 * 1024 * 1024,
172 traceMaxFiles: 10,
173 otlpTracesUrl: undefined,
174 otlpMetricsUrl: undefined,
175 otlpExportIntervalMs: 10_000,
176 otlpServiceName: "t3-server",
177 cwd,
178 baseDir,
179 ...derivedPaths,
180 mode: "web",
181 autoBootstrapProjectFromCwd: false,
182 logWebSocketEvents: false,
183 tailscaleServeEnabled: false,
184 tailscaleServePort: 443,
185 port: 0,
186 host: undefined,
187 desktopBootstrapToken: undefined,
188 staticDir: undefined,
189 devUrl,
190 noBrowser: false,
191 startupPresentation: "browser",
192 });
193});
194
195export const layerTest = (cwd: string, baseDirOrPrefix: string | { readonly prefix: string }) =>
196 Layer.effect(ServerConfig, makeTest(cwd, baseDirOrPrefix));
197
198export const resolveStaticDir = Effect.fn(function* () {
199 const { join, resolve } = yield* Path.Path;
200 const { exists } = yield* FileSystem.FileSystem;
201 const bundledClient = resolve(join(import.meta.dirname, "client"));
202 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
203 Effect.orElseSucceed(() => false),
204 );
205 if (bundledStat) {
206 return bundledClient;
207 }
208
209 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
210 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
211 Effect.orElseSucceed(() => false),
212 );
213 if (monorepoStat) {
214 return monorepoClient;
215 }
216 return undefined;
217});