Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

config.ts

180 lines6.2 KB

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