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

config.ts

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