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