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