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

config.ts

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