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

config.ts

134 lines4.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, Path, ServiceMap } from "effect";
10​
11export const DEFAULT_PORT = 3773;
12​
13export type RuntimeMode = "web" | "desktop";
14​
15/**
16 * ServerDerivedPaths - Derived paths from the base directory.
17 */
18export interface ServerDerivedPaths {
19 readonly stateDir: string;
20 readonly dbPath: string;
21 readonly keybindingsConfigPath: string;
22 readonly worktreesDir: string;
23 readonly attachmentsDir: string;
24 readonly logsDir: string;
25 readonly serverLogPath: string;
26 readonly providerLogsDir: string;
27 readonly providerEventLogPath: string;
28 readonly terminalLogsDir: string;
29 readonly anonymousIdPath: string;
31​
32/**
33 * ServerConfigShape - Process/runtime configuration required by the server.
34 */
35export interface ServerConfigShape extends ServerDerivedPaths {
36 readonly mode: RuntimeMode;
37 readonly port: number;
38 readonly host: string | undefined;
39 readonly cwd: string;
40 readonly baseDir: string;
41 readonly staticDir: string | undefined;
42 readonly devUrl: URL | undefined;
43 readonly noBrowser: boolean;
44 readonly authToken: string | undefined;
45 readonly autoBootstrapProjectFromCwd: boolean;
46 readonly logWebSocketEvents: boolean;
48​
49export const deriveServerPaths = Effect.fn(function* (
50 baseDir: ServerConfigShape["baseDir"],
51 devUrl: ServerConfigShape["devUrl"],
52): Effect.fn.Return<ServerDerivedPaths, never, Path.Path> {
53 const { join } = yield* Path.Path;
54 const stateDir = join(baseDir, devUrl !== undefined ? "dev" : "userdata");
55 const dbPath = join(stateDir, "state.sqlite");
56 const attachmentsDir = join(stateDir, "attachments");
57 const logsDir = join(stateDir, "logs");
58 const providerLogsDir = join(logsDir, "provider");
59 return {
60 stateDir,
61 dbPath,
62 keybindingsConfigPath: join(stateDir, "keybindings.json"),
63 worktreesDir: join(baseDir, "worktrees"),
64 attachmentsDir,
65 logsDir,
66 serverLogPath: join(logsDir, "server.log"),
67 providerLogsDir,
68 providerEventLogPath: join(providerLogsDir, "events.log"),
69 terminalLogsDir: join(logsDir, "terminals"),
70 anonymousIdPath: join(stateDir, "anonymous-id"),
71 };
72});
73​
74/**
75 * ServerConfig - Service tag for server runtime configuration.
76 */
77export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
78 "t3/config/ServerConfig",
79) {
80 static readonly layerTest = (cwd: string, baseDirOrPrefix: string | { prefix: string }) =>
81 Layer.effect(
82 ServerConfig,
83 Effect.gen(function* () {
84 const devUrl = undefined;
85​
86 const fs = yield* FileSystem.FileSystem;
87 const baseDir =
88 typeof baseDirOrPrefix === "string"
89 ? baseDirOrPrefix
90 : yield* fs.makeTempDirectoryScoped({ prefix: baseDirOrPrefix.prefix });
91 const derivedPaths = yield* deriveServerPaths(baseDir, devUrl);
92​
93 yield* fs.makeDirectory(derivedPaths.stateDir, { recursive: true });
94 yield* fs.makeDirectory(derivedPaths.logsDir, { recursive: true });
95 yield* fs.makeDirectory(derivedPaths.attachmentsDir, { recursive: true });
96​
97 return {
98 cwd,
99 baseDir,
100 ...derivedPaths,
101 mode: "web",
102 autoBootstrapProjectFromCwd: false,
103 logWebSocketEvents: false,
104 port: 0,
105 host: undefined,
106 authToken: undefined,
107 staticDir: undefined,
108 devUrl,
109 noBrowser: false,
110 } satisfies ServerConfigShape;
111 }),
112 );
114​
115export const resolveStaticDir = Effect.fn(function* () {
116 const { join, resolve } = yield* Path.Path;
117 const { exists } = yield* FileSystem.FileSystem;
118 const bundledClient = resolve(join(import.meta.dirname, "client"));
119 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
120 Effect.orElseSucceed(() => false),
121 );
122 if (bundledStat) {
123 return bundledClient;
124 }
125​
126 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
127 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
128 Effect.orElseSucceed(() => false),
129 );
130 if (monorepoStat) {
131 return monorepoClient;
132 }
133 return undefined;
134});