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