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