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