Co-authored-by: codex <[email protected]> Co-authored-by: Cursor Agent <[email protected]>
157 lines
5.2 KiB
TypeScript
157 lines
5.2 KiB
TypeScript
/**
|
|
* ServerConfig - Runtime configuration services.
|
|
*
|
|
* Defines process-level server configuration and networking helpers used by
|
|
* startup and runtime layers.
|
|
*
|
|
* @module ServerConfig
|
|
*/
|
|
import { Effect, FileSystem, Layer, LogLevel, Path, Schema, ServiceMap } from "effect";
|
|
|
|
export const DEFAULT_PORT = 3773;
|
|
|
|
export const RuntimeMode = Schema.Literals(["web", "desktop"]);
|
|
export type RuntimeMode = typeof RuntimeMode.Type;
|
|
|
|
/**
|
|
* ServerDerivedPaths - Derived paths from the base directory.
|
|
*/
|
|
export interface ServerDerivedPaths {
|
|
readonly stateDir: string;
|
|
readonly dbPath: string;
|
|
readonly keybindingsConfigPath: string;
|
|
readonly settingsPath: string;
|
|
readonly worktreesDir: string;
|
|
readonly attachmentsDir: string;
|
|
readonly logsDir: string;
|
|
readonly serverLogPath: string;
|
|
readonly providerLogsDir: string;
|
|
readonly providerEventLogPath: string;
|
|
readonly terminalLogsDir: string;
|
|
readonly anonymousIdPath: string;
|
|
}
|
|
|
|
/**
|
|
* ServerConfigShape - Process/runtime configuration required by the server.
|
|
*/
|
|
export interface ServerConfigShape extends ServerDerivedPaths {
|
|
readonly logLevel: LogLevel.LogLevel;
|
|
readonly mode: RuntimeMode;
|
|
readonly port: number;
|
|
readonly host: string | undefined;
|
|
readonly cwd: string;
|
|
readonly baseDir: string;
|
|
readonly staticDir: string | undefined;
|
|
readonly devUrl: URL | undefined;
|
|
readonly noBrowser: boolean;
|
|
readonly authToken: string | undefined;
|
|
readonly autoBootstrapProjectFromCwd: boolean;
|
|
readonly logWebSocketEvents: boolean;
|
|
}
|
|
|
|
export const deriveServerPaths = Effect.fn(function* (
|
|
baseDir: ServerConfigShape["baseDir"],
|
|
devUrl: ServerConfigShape["devUrl"],
|
|
): Effect.fn.Return<ServerDerivedPaths, never, Path.Path> {
|
|
const { join } = yield* Path.Path;
|
|
const stateDir = join(baseDir, devUrl !== undefined ? "dev" : "userdata");
|
|
const dbPath = join(stateDir, "state.sqlite");
|
|
const attachmentsDir = join(stateDir, "attachments");
|
|
const logsDir = join(stateDir, "logs");
|
|
const providerLogsDir = join(logsDir, "provider");
|
|
return {
|
|
stateDir,
|
|
dbPath,
|
|
keybindingsConfigPath: join(stateDir, "keybindings.json"),
|
|
settingsPath: join(stateDir, "settings.json"),
|
|
worktreesDir: join(baseDir, "worktrees"),
|
|
attachmentsDir,
|
|
logsDir,
|
|
serverLogPath: join(logsDir, "server.log"),
|
|
providerLogsDir,
|
|
providerEventLogPath: join(providerLogsDir, "events.log"),
|
|
terminalLogsDir: join(logsDir, "terminals"),
|
|
anonymousIdPath: join(stateDir, "anonymous-id"),
|
|
};
|
|
});
|
|
|
|
export const ensureServerDirectories = Effect.fn(function* (derivedPaths: ServerDerivedPaths) {
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const path = yield* Path.Path;
|
|
|
|
yield* Effect.all(
|
|
[
|
|
fs.makeDirectory(derivedPaths.stateDir, { recursive: true }),
|
|
fs.makeDirectory(derivedPaths.logsDir, { recursive: true }),
|
|
fs.makeDirectory(derivedPaths.providerLogsDir, { recursive: true }),
|
|
fs.makeDirectory(derivedPaths.terminalLogsDir, { recursive: true }),
|
|
fs.makeDirectory(derivedPaths.attachmentsDir, { recursive: true }),
|
|
fs.makeDirectory(derivedPaths.worktreesDir, { recursive: true }),
|
|
fs.makeDirectory(path.dirname(derivedPaths.keybindingsConfigPath), { recursive: true }),
|
|
fs.makeDirectory(path.dirname(derivedPaths.settingsPath), { recursive: true }),
|
|
fs.makeDirectory(path.dirname(derivedPaths.anonymousIdPath), { recursive: true }),
|
|
],
|
|
{ concurrency: "unbounded" },
|
|
);
|
|
});
|
|
|
|
/**
|
|
* ServerConfig - Service tag for server runtime configuration.
|
|
*/
|
|
export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
|
|
"t3/config/ServerConfig",
|
|
) {
|
|
static readonly layerTest = (cwd: string, baseDirOrPrefix: string | { prefix: string }) =>
|
|
Layer.effect(
|
|
ServerConfig,
|
|
Effect.gen(function* () {
|
|
const devUrl = undefined;
|
|
|
|
const fs = yield* FileSystem.FileSystem;
|
|
const baseDir =
|
|
typeof baseDirOrPrefix === "string"
|
|
? baseDirOrPrefix
|
|
: yield* fs.makeTempDirectoryScoped({ prefix: baseDirOrPrefix.prefix });
|
|
const derivedPaths = yield* deriveServerPaths(baseDir, devUrl);
|
|
yield* ensureServerDirectories(derivedPaths);
|
|
|
|
return {
|
|
logLevel: "Error",
|
|
cwd,
|
|
baseDir,
|
|
...derivedPaths,
|
|
mode: "web",
|
|
autoBootstrapProjectFromCwd: false,
|
|
logWebSocketEvents: false,
|
|
port: 0,
|
|
host: undefined,
|
|
authToken: undefined,
|
|
staticDir: undefined,
|
|
devUrl,
|
|
noBrowser: false,
|
|
} satisfies ServerConfigShape;
|
|
}),
|
|
);
|
|
}
|
|
|
|
export const resolveStaticDir = Effect.fn(function* () {
|
|
const { join, resolve } = yield* Path.Path;
|
|
const { exists } = yield* FileSystem.FileSystem;
|
|
const bundledClient = resolve(join(import.meta.dirname, "client"));
|
|
const bundledStat = yield* exists(join(bundledClient, "index.html")).pipe(
|
|
Effect.orElseSucceed(() => false),
|
|
);
|
|
if (bundledStat) {
|
|
return bundledClient;
|
|
}
|
|
|
|
const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
|
|
const monorepoStat = yield* exists(join(monorepoClient, "index.html")).pipe(
|
|
Effect.orElseSucceed(() => false),
|
|
);
|
|
if (monorepoStat) {
|
|
return monorepoClient;
|
|
}
|
|
return undefined;
|
|
});
|