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, 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;
30}
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;
47 readonly harnessPort: number | undefined;
48 readonly harnessSecret: string | undefined;
49}
50
51export const deriveServerPaths = Effect.fn(function* (
52 baseDir: ServerConfigShape["baseDir"],
53 devUrl: ServerConfigShape["devUrl"],
54): Effect.fn.Return<ServerDerivedPaths, never, Path.Path> {
55 const { join } = yield* Path.Path;
56 const stateDir = join(baseDir, devUrl !== undefined ? "dev" : "userdata");
57 const dbPath = join(stateDir, "state.sqlite");
58 const attachmentsDir = join(stateDir, "attachments");
59 const logsDir = join(stateDir, "logs");
60 const providerLogsDir = join(logsDir, "provider");
61 return {
62 stateDir,
63 dbPath,
64 keybindingsConfigPath: join(stateDir, "keybindings.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 harnessPort: undefined,
113 harnessSecret: undefined,
114 } satisfies ServerConfigShape;
115 }),
116 );
117}
118
119export const resolveStaticDir = Effect.fn(function* () {
120 const { join, resolve } = yield* Path.Path;
121 const { exists } = yield* FileSystem.FileSystem;
122 const bundledClient = resolve(join(import.meta.dirname, "client"));
123 const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
124 Effect.orElseSucceed(() => false),
125 );
126 if (bundledStat) {
127 return bundledClient;
128 }
129
130 const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
131 const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
132 Effect.orElseSucceed(() => false),
133 );
134 if (monorepoStat) {
135 return monorepoClient;
136 }
137 return undefined;
138});