This commit is contained in:
Julius Marminge authored and Julius Marminge committed 2026-02-26 13:20:36 -08:00
1 parent da3d8e2ecb
commit 3fbd02c6cf
11 files changed
+439 -378

No files matched your search

+87
View File
@@ -0,0 +1,87 @@
import * as Net from "node:net";
import { Data, Effect, FileSystem, Layer, Path, ServiceMap } from "effect";
export const DEFAULT_PORT = 3773;
export type RuntimeMode = "web" | "desktop";
export interface ServerConfigShape {
readonly mode: RuntimeMode;
readonly port: number;
readonly host: string | undefined;
readonly cwd: string;
readonly stateDir: string;
readonly staticDir: string | undefined;
readonly devUrl: URL | undefined;
readonly noBrowser: boolean;
readonly authToken: string | undefined;
}
export class ServerConfig extends ServiceMap.Service<ServerConfig, ServerConfigShape>()(
"server/ServerConfig",
) {}
// Helpers
export class NetError extends Data.TaggedError("NetError")<{
readonly message: string;
readonly cause?: unknown;
}> {}
export interface NetServiceShape {
readonly findAvailablePort: (preferred: number) => Effect.Effect<number, NetError>;
}
export class NetService extends ServiceMap.Service<NetService, NetServiceShape>()(
"server/NetService",
) {
static readonly layer = Layer.succeed(NetService, {
findAvailablePort: (preferred) =>
Effect.callback<number, NetError>((resume) => {
const server = Net.createServer();
server.listen(preferred, () => {
server.close(() => resume(Effect.succeed(preferred)));
});
server.on("error", () => {
const fallback = Net.createServer();
fallback.listen(0, () => {
const addr = fallback.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
fallback.close(() => {
resume(
port > 0
? Effect.succeed(port)
: Effect.fail(new NetError({ message: "Could not find an available port." })),
);
});
});
fallback.on("error", (cause) => {
resume(
Effect.fail(new NetError({ message: "Failed to find an available port", cause })),
);
});
});
}),
});
}
export const resolveStaticDir = Effect.fn(function* () {
const { join, resolve } = yield* Path.Path;
const { stat } = yield* FileSystem.FileSystem;
const bundledClient = resolve(join(import.meta.dirname, "client"));
const bundledStat = yield* stat(join(bundledClient, "index.html")).pipe(
Effect.orElseSucceed(() => undefined),
);
if (bundledStat) {
return bundledClient;
}
const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
const monorepoStat = yield* stat(join(monorepoClient, "index.html")).pipe(
Effect.orElseSucceed(() => undefined),
);
if (monorepoStat) {
return monorepoClient;
}
return undefined;
});
-18
View File
@@ -1,18 +0,0 @@
import { execFileSync } from "node:child_process";
export function fixPath(): void {
if (process.platform !== "darwin") return;
try {
const shell = process.env.SHELL ?? "/bin/zsh";
const result = execFileSync(shell, ["-ilc", "echo -n $PATH"], {
encoding: "utf8",
timeout: 5000,
});
if (result) {
process.env.PATH = result;
}
} catch {
// Silently ignore — keep default PATH
}
}
+8 -4
View File
@@ -3,15 +3,19 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import { CliConfigLive, makeCliCommand } from "./main";
import { CliConfig, makeCliCommand } from "./main";
import { OpenLive } from "./open";
import { Command } from "effect/unstable/cli";
import { version } from "../package.json" with { type: "json" };
import { ServerLive } from "./wsServer";
import { NetService } from "./config";
const RuntimeLayer = Layer.provideMerge(
Layer.mergeAll(CliConfigLive, ServerLive, OpenLive),
NodeServices.layer,
const RuntimeLayer = Layer.empty.pipe(
Layer.provideMerge(CliConfig.layer),
Layer.provideMerge(ServerLive),
Layer.provideMerge(OpenLive),
Layer.provideMerge(NetService.layer),
Layer.provideMerge(NodeServices.layer),
);
Command.run(makeCliCommand(), { version }).pipe(Effect.provide(RuntimeLayer), NodeRuntime.runMain);
+20 -17
View File
@@ -8,14 +8,17 @@ import * as Command from "effect/unstable/cli/Command";
import { beforeEach } from "vitest";
import { CliConfig, makeCliCommand, type CliConfigShape } from "./main";
import { NetService, ServerConfig, type ServerConfigShape } from "./config";
import { Open, type OpenShape } from "./open";
import { Server, type ServerShape, type ServerOptions } from "./wsServer";
const start = vi.fn(() => undefined);
const stop = vi.fn(() => undefined);
const createServer = vi.fn((_: ServerOptions) =>
let resolvedConfig: ServerConfigShape | null = null;
const createServer = vi.fn((_: ServerOptions | undefined) =>
Effect.acquireRelease(
Effect.sync(() => {
Effect.gen(function* () {
resolvedConfig = yield* ServerConfig;
start();
return {} as unknown as Http.Server;
}),
@@ -28,9 +31,11 @@ const testLayer = Layer.mergeAll(
Layer.succeed(CliConfig, {
cwd: "/tmp/t3-test-workspace",
fixPath: Effect.void,
findAvailablePort,
resolveStaticDir: Effect.undefined,
} satisfies CliConfigShape),
Layer.succeed(NetService, {
findAvailablePort,
}),
Layer.succeed(Server, {
createServer,
stopSignal: Effect.void,
@@ -52,11 +57,13 @@ const runCli = (
beforeEach(() => {
vi.clearAllMocks();
resolvedConfig = null;
start.mockImplementation(() => undefined);
stop.mockImplementation(() => undefined);
createServer.mockImplementation((_: ServerOptions) =>
createServer.mockImplementation((_: ServerOptions | undefined) =>
Effect.acquireRelease(
Effect.sync(() => {
Effect.gen(function* () {
resolvedConfig = yield* ServerConfig;
start();
return {} as unknown as Http.Server;
}),
@@ -72,9 +79,8 @@ it.layer(testLayer)("server cli", (it) => {
yield* runCli(["--port", "4010", "--token", "secret"]);
assert.equal(createServer.mock.calls.length, 1);
const options = createServer.mock.calls[0]?.[0];
assert.equal(options?.port, 4010);
assert.equal(options?.authToken, "secret");
assert.equal(resolvedConfig?.port, 4010);
assert.equal(resolvedConfig?.authToken, "secret");
assert.equal(start.mock.calls.length, 1);
assert.equal(stop.mock.calls.length, 1);
}),
@@ -89,9 +95,8 @@ it.layer(testLayer)("server cli", (it) => {
});
assert.equal(createServer.mock.calls.length, 1);
const options = createServer.mock.calls[0]?.[0];
assert.equal(options?.port, 4999);
assert.equal(options?.authToken, "env-token");
assert.equal(resolvedConfig?.port, 4999);
assert.equal(resolvedConfig?.authToken, "env-token");
assert.equal(findAvailablePort.mock.calls.length, 0);
}),
);
@@ -103,9 +108,8 @@ it.layer(testLayer)("server cli", (it) => {
assert.deepStrictEqual(findAvailablePort.mock.calls, [[3773]]);
assert.equal(createServer.mock.calls.length, 1);
const options = createServer.mock.calls[0]?.[0];
assert.equal(options?.port, 5444);
assert.equal(options?.host, undefined);
assert.equal(resolvedConfig?.port, 5444);
assert.equal(resolvedConfig?.mode, "web");
}),
);
@@ -118,9 +122,8 @@ it.layer(testLayer)("server cli", (it) => {
assert.equal(findAvailablePort.mock.calls.length, 0);
assert.equal(createServer.mock.calls.length, 1);
const options = createServer.mock.calls[0]?.[0];
assert.equal(options?.port, 3773);
assert.equal(options?.host, "127.0.0.1");
assert.equal(resolvedConfig?.port, 3773);
assert.equal(resolvedConfig?.mode, "desktop");
}),
);
+125 -188
View File
@@ -1,21 +1,19 @@
import net from "node:net";
import os from "node:os";
import { ServiceMap, Data, Effect, Layer, Option, Config, Path, Schema, FileSystem } from "effect";
import { Config, Data, Effect, FileSystem, Layer, Option, Path, Schema, ServiceMap } from "effect";
import { Command, Flag } from "effect/unstable/cli";
import { fixPath } from "./fixPath";
import { Open } from "./open";
import {
makeServerPersistenceLayer,
makeServerProviderLayer,
makeServerRuntimeServicesLayer,
Server,
} from "./wsServer";
const DEFAULT_PORT = 3773;
type RuntimeMode = "web" | "desktop";
DEFAULT_PORT,
NetService,
resolveStaticDir,
ServerConfig,
type RuntimeMode,
type ServerConfigShape,
} from "./config";
import { fixPath, resolveStateDir } from "./os-jank";
import { Open } from "./open";
import * as SqlitePersistence from "./persistence/Layers/Sqlite";
import { makeServerProviderLayer, makeServerRuntimeServicesLayer } from "./serverLayers";
import { Server } from "./wsServer";
export class StartupError extends Data.TaggedError("StartupError")<{
readonly message: string;
@@ -29,21 +27,34 @@ interface CliInput {
export interface CliConfigShape {
readonly cwd: string;
readonly fixPath: Effect.Effect<unknown, never>;
readonly findAvailablePort: (preferred: number) => Effect.Effect<number, StartupError>;
readonly fixPath: Effect.Effect<void>;
readonly resolveStaticDir: Effect.Effect<string | undefined>;
}
export class CliConfig extends ServiceMap.Service<CliConfig, CliConfigShape>()(
"server/CliConfig",
) {}
export class CliConfig extends ServiceMap.Service<CliConfig, CliConfigShape>()("server/CliConfig") {
static readonly layer = Layer.effect(
CliConfig,
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return {
cwd: process.cwd(),
fixPath: Effect.sync(fixPath),
resolveStaticDir: resolveStaticDir().pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
),
} satisfies CliConfigShape;
}),
);
}
const CliEnvConfig = Config.all({
mode: Config.string("T3CODE_MODE").pipe(
Config.option,
Config.map(
Option.match({
onNone: () => "web" as const,
Option.match<RuntimeMode, string>({
onNone: () => "web",
onSome: (value) => (value === "desktop" ? "desktop" : "web"),
}),
),
@@ -64,159 +75,102 @@ const CliEnvConfig = Config.all({
),
});
const expandHomePath = Effect.fn(function* (input: string) {
const { join, sep } = yield* Path.Path;
if (input === "~") return os.homedir();
if (input.startsWith(`~${sep}`)) {
return join(os.homedir(), input.slice(sep.length));
}
return input;
});
const ServerConfigLive = (input: CliInput) =>
Layer.effect(
ServerConfig,
Effect.gen(function* () {
const cliConfig = yield* CliConfig;
const { findAvailablePort } = yield* NetService;
const env = yield* CliEnvConfig.asEffect().pipe(
Effect.mapError(
(cause) =>
new StartupError({ message: "Failed to read environment configuration", cause }),
),
);
const resolveStateDir = Effect.fn(function* (raw: string | undefined) {
const { join, resolve } = yield* Path.Path;
if (!raw || raw.trim().length === 0) {
return join(os.homedir(), ".t3", "userdata");
}
return resolve(yield* expandHomePath(raw.trim()));
});
const findAvailablePort = (preferred: number) =>
Effect.callback<number, StartupError>((resume) => {
const server = net.createServer();
server.listen(preferred, () => {
server.close(() => resume(Effect.succeed(preferred)));
});
server.on("error", () => {
const fallback = net.createServer();
fallback.listen(0, () => {
const addr = fallback.address();
const port = typeof addr === "object" && addr !== null ? addr.port : 0;
fallback.close(() => {
resume(
port > 0
? Effect.succeed(port)
: Effect.fail(new StartupError({ message: "Could not find an available port." })),
);
});
});
fallback.on("error", (cause) => {
resume(
Effect.fail(new StartupError({ message: "Failed to find an available port", cause })),
);
const port = yield* Option.match(input.port, {
onSome: (value) => Effect.succeed(value),
onNone: () => {
if (env.port) {
return Effect.succeed(env.port);
}
if (env.mode === "desktop") {
return Effect.succeed(DEFAULT_PORT);
}
return findAvailablePort(DEFAULT_PORT);
},
});
const stateDir = yield* resolveStateDir(env.stateDir);
const devUrl = env.devUrl;
const noBrowser = env.noBrowser ?? env.mode === "desktop";
const authToken = Option.getOrUndefined(input.token) ?? env.authToken;
const staticDir = devUrl ? undefined : yield* cliConfig.resolveStaticDir;
const host = env.mode === "desktop" ? "127.0.0.1" : undefined;
return {
mode: env.mode,
port,
cwd: cliConfig.cwd,
host,
stateDir,
staticDir,
devUrl,
noBrowser,
authToken,
} satisfies ServerConfigShape;
}),
);
const LayerLive = (input: CliInput) =>
Layer.empty.pipe(
Layer.provideMerge(makeServerRuntimeServicesLayer()),
Layer.provideMerge(makeServerProviderLayer()),
Layer.provideMerge(SqlitePersistence.layerConfig),
Layer.provideMerge(ServerConfigLive(input)),
);
const makeServerProgram = (input: CliInput) =>
Effect.gen(function* () {
const cliConfig = yield* CliConfig;
const { createServer, stopSignal } = yield* Server;
const openDeps = yield* Open;
yield* cliConfig.fixPath;
const config = yield* ServerConfig;
if (!config.devUrl && !config.staticDir) {
yield* Effect.logWarning(
"web bundle missing and no VITE_DEV_SERVER_URL; web UI unavailable",
{
hint: "Run `bun run --cwd apps/web build` or set VITE_DEV_SERVER_URL for dev mode.",
},
);
}
yield* createServer();
const url = `http://localhost:${config.port}`;
yield* Effect.logInfo("T3 Code running", {
url,
cwd: config.cwd,
mode: config.mode,
stateDir: config.stateDir,
authEnabled: Boolean(config.authToken),
});
});
const resolveStaticDir = Effect.fn(function* () {
const { join, resolve } = yield* Path.Path;
const { stat } = yield* FileSystem.FileSystem;
const bundledClient = resolve(join(import.meta.dirname, "client"));
const bundledStat = yield* stat(join(bundledClient, "index.html")).pipe(
Effect.orElseSucceed(() => undefined),
);
if (bundledStat) return bundledClient;
if (!config.noBrowser) {
const target = config.devUrl?.toString() ?? url;
yield* openDeps.openBrowser(target).pipe(
Effect.catch(() =>
Effect.logInfo("browser auto-open unavailable", {
hint: `Open ${target} in your browser.`,
}),
),
);
}
const monorepoClient = resolve(join(import.meta.dirname, "../../web/dist"));
const monorepoStat = yield* stat(join(monorepoClient, "index.html")).pipe(
Effect.orElseSucceed(() => undefined),
);
if (monorepoStat) return monorepoClient;
return undefined;
});
const resolveCliConfig = Effect.fn(function* (input: CliInput) {
const cliConfig = yield* CliConfig;
const env = yield* CliEnvConfig.asEffect().pipe(
Effect.mapError(
(cause) =>
new StartupError({
message: "Failed to read environment configuration",
cause,
}),
),
);
const mode: RuntimeMode = env.mode === "desktop" ? "desktop" : "web";
const cliPort = Option.getOrUndefined(input.port);
const port =
(cliPort === undefined ? env.port : cliPort) ??
(mode === "desktop" ? DEFAULT_PORT : yield* cliConfig.findAvailablePort(DEFAULT_PORT));
const stateDir = yield* resolveStateDir(env.stateDir);
const devUrl = env.devUrl;
const noBrowser = env.noBrowser ?? mode === "desktop";
const authToken = Option.getOrUndefined(input.token) ?? env.authToken;
const staticDir = devUrl ? undefined : yield* cliConfig.resolveStaticDir;
return {
mode,
port,
stateDir,
devUrl,
noBrowser,
authToken,
staticDir,
cwd: cliConfig.cwd,
};
});
const makeServerProgram = Effect.fn(function* (input: CliInput) {
const cliConfig = yield* CliConfig;
const { createServer, stopSignal } = yield* Server;
const openDeps = yield* Open;
yield* cliConfig.fixPath;
const config = yield* resolveCliConfig(input);
if (!config.devUrl && !config.staticDir) {
yield* Effect.logWarning("web bundle missing and no VITE_DEV_SERVER_URL; web UI unavailable", {
hint: "Run `bun run --cwd apps/web build` or set VITE_DEV_SERVER_URL for dev mode.",
});
}
const persistenceLayer = makeServerPersistenceLayer(config.stateDir);
const providerLayer = makeServerProviderLayer(config.stateDir);
const infrastructureLayer = providerLayer.pipe(Layer.provideMerge(persistenceLayer));
const serverRuntimeLayer = Layer.merge(
makeServerRuntimeServicesLayer().pipe(Layer.provide(infrastructureLayer)),
infrastructureLayer,
);
yield* createServer({
port: config.port,
host: config.mode === "desktop" ? "127.0.0.1" : undefined,
cwd: config.cwd,
autoBootstrapProjectFromCwd: config.mode === "web",
stateDir: config.stateDir,
staticDir: config.staticDir,
devUrl: config.devUrl?.href,
authToken: config.authToken,
}).pipe(
Effect.provide(serverRuntimeLayer),
Effect.mapError((cause) => new StartupError({ message: "Failed to start server", cause })),
);
const url = `http://localhost:${config.port}`;
yield* Effect.logInfo("T3 Code running", {
url,
cwd: config.cwd,
mode: config.mode,
stateDir: config.stateDir,
authEnabled: Boolean(config.authToken),
});
if (!config.noBrowser) {
const target = config.devUrl ?? url;
yield* openDeps.openBrowser(target.toString()).pipe(
Effect.catch(() =>
Effect.logInfo("browser auto-open unavailable", {
hint: `Open ${target} in your browser.`,
}),
),
);
}
return yield* stopSignal;
}, Effect.scoped);
return yield* stopSignal;
}).pipe(Effect.provide(LayerLive(input)));
export function makeCliCommand() {
const portFlag = Flag.integer("port").pipe(
@@ -234,23 +188,6 @@ export function makeCliCommand() {
token: tokenFlag,
}).pipe(
Command.withDescription("Run the T3 Code server."),
Command.withHandler((input) => makeServerProgram(input)),
Command.withHandler((input) => Effect.scoped(makeServerProgram(input))),
);
}
export const CliConfigLive = Layer.effect(
CliConfig,
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
return {
cwd: process.cwd(),
fixPath: Effect.sync(fixPath),
findAvailablePort,
resolveStaticDir: resolveStaticDir().pipe(
Effect.provideService(FileSystem.FileSystem, fileSystem),
Effect.provideService(Path.Path, path),
),
} satisfies CliConfigShape;
}),
);
+23 -38
View File
@@ -2,6 +2,7 @@ import { spawn } from "node:child_process";
import { EDITORS, type EditorId } from "@t3tools/contracts";
import { ServiceMap, Schema, Effect, Layer } from "effect";
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
export class OpenError extends Schema.TaggedErrorClass<OpenError>()("OpenError", {
message: Schema.String,
@@ -25,7 +26,7 @@ export interface OpenShape {
export class Open extends ServiceMap.Service<Open, OpenShape>()("server/Open") {}
export function resolveEditorLaunch(
function resolveEditorLaunch(
input: OpenInEditorInput,
platform: NodeJS.Platform = process.platform,
): EditorLaunch {
@@ -52,42 +53,26 @@ export function resolveEditorLaunch(
}
}
const OpenDefault: OpenShape = {
openBrowser: (target) =>
Effect.tryPromise({
try: async () => {
const open = await import("open");
await open.default(target);
},
catch: (cause) =>
new OpenError({
message: "Browser auto-open failed",
cause,
}),
}),
openInEditor: (input) =>
Effect.try({
try: () => {
const make = Effect.gen(function* () {
const open = yield* Effect.promise(() => import("open"));
const { spawn } = yield* ChildProcessSpawner.ChildProcessSpawner;
return {
openBrowser: (target) =>
Effect.tryPromise({
try: () => open.default(target),
catch: (cause) => new OpenError({ message: "Browser auto-open failed", cause }),
}),
openInEditor: (input) =>
Effect.gen(function* () {
const launch = resolveEditorLaunch(input);
const child = spawn(launch.command, [...launch.args], {
detached: true,
stdio: "ignore",
});
child.on("error", () => {
// Best-effort behavior for detached editor launches.
});
child.unref();
},
catch: (cause) =>
cause instanceof OpenError
? cause
: new OpenError({
message: "Failed to open editor",
cause,
}),
}),
};
const child = yield* spawn(ChildProcess.make(launch.command, launch.args));
yield* Effect.forkDetach(child.exitCode);
}).pipe(
Effect.catch((error) => Effect.logError("Failed to open editor", { cause: error })),
Effect.scoped,
),
} satisfies OpenShape;
});
export const OpenLive = Layer.succeed(Open, OpenDefault);
export const OpenDefaultService = OpenDefault;
export const OpenLive = Layer.effect(Open, make);
+39
View File
@@ -0,0 +1,39 @@
import * as OS from "node:os";
import { Effect, Path } from "effect";
import { execFileSync } from "node:child_process";
export function fixPath(): void {
if (process.platform !== "darwin") return;
try {
const shell = process.env.SHELL ?? "/bin/zsh";
const result = execFileSync(shell, ["-ilc", "echo -n $PATH"], {
encoding: "utf8",
timeout: 5000,
});
if (result) {
process.env.PATH = result;
}
} catch {
// Silently ignore — keep default PATH
}
}
export const expandHomePath = Effect.fn(function* (input: string) {
const { join, sep } = yield* Path.Path;
if (input === "~") {
return OS.homedir();
}
if (input.startsWith(`~${sep}`)) {
return join(OS.homedir(), input.slice(sep.length));
}
return input;
});
export const resolveStateDir = Effect.fn(function* (raw: string | undefined) {
const { join, resolve } = yield* Path.Path;
if (!raw || raw.trim().length === 0) {
return join(OS.homedir(), ".t3", "userdata");
}
return resolve(yield* expandHomePath(raw.trim()));
});
@@ -2,6 +2,7 @@ import { Effect, Layer, FileSystem, Path } from "effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { runMigrations } from "../Migrations.ts";
import { ServerConfig } from "../../config.ts";
type RuntimeSqliteLayerConfig = {
readonly filename: string;
@@ -47,3 +48,10 @@ export const SqlitePersistenceMemory = Layer.provideMerge(
setup,
makeRuntimeSqliteLayer({ filename: ":memory:" }),
);
export const layerConfig = Effect.gen(function* () {
const { stateDir } = yield* ServerConfig;
const { join } = yield* Path.Path;
const dbPath = join(stateDir, "orchestration.sqlite");
return makeSqlitePersistenceLive(dbPath);
}).pipe(Layer.unwrap);
+83
View File
@@ -0,0 +1,83 @@
import path from "node:path";
import { NodeServices } from "@effect/platform-node";
import { Effect, Layer } from "effect";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { CheckpointStoreLive } from "./checkpointing/Layers/CheckpointStore";
import { ServerConfig } from "./config";
import { CheckpointStore } from "./checkpointing/Services/CheckpointStore";
import { OrchestrationCommandReceiptRepositoryLive } from "./persistence/Layers/OrchestrationCommandReceipts";
import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore";
import { ProviderSessionRuntimeRepositoryLive } from "./persistence/Layers/ProviderSessionRuntime";
import { OrchestrationEngineLive } from "./orchestration/Layers/OrchestrationEngine";
import { CheckpointReactorLive } from "./orchestration/Layers/CheckpointReactor";
import { OrchestrationReactorLive } from "./orchestration/Layers/OrchestrationReactor";
import { ProviderCommandReactorLive } from "./orchestration/Layers/ProviderCommandReactor";
import { OrchestrationProjectionPipelineLive } from "./orchestration/Layers/ProjectionPipeline";
import { OrchestrationProjectionSnapshotQueryLive } from "./orchestration/Layers/ProjectionSnapshotQuery";
import { ProviderRuntimeIngestionLive } from "./orchestration/Layers/ProviderRuntimeIngestion";
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine";
import { OrchestrationReactor } from "./orchestration/Services/OrchestrationReactor";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
import { makeCodexAdapterLive } from "./provider/Layers/CodexAdapter";
import { ProviderAdapterRegistryLive } from "./provider/Layers/ProviderAdapterRegistry";
import { makeProviderServiceLive } from "./provider/Layers/ProviderService";
import { ProviderSessionDirectoryLive } from "./provider/Layers/ProviderSessionDirectory";
import { ProviderService } from "./provider/Services/ProviderService";
export function makeServerProviderLayer(): Layer.Layer<
ProviderService,
unknown,
SqlClient.SqlClient | ServerConfig
> {
return Effect.gen(function* () {
const { stateDir } = yield* ServerConfig;
const providerLogsDir = path.join(stateDir, "logs", "providers");
const codexAdapterLayer = makeCodexAdapterLive({
nativeEventLogPath: path.join(providerLogsDir, "provider-native.ndjson"),
});
const adapterRegistryLayer = ProviderAdapterRegistryLive.pipe(Layer.provide(codexAdapterLayer));
const providerSessionDirectoryLayer = ProviderSessionDirectoryLive.pipe(
Layer.provide(ProviderSessionRuntimeRepositoryLive),
);
return makeProviderServiceLive({
canonicalEventLogPath: path.join(providerLogsDir, "provider-canonical.ndjson"),
}).pipe(Layer.provide(adapterRegistryLayer), Layer.provide(providerSessionDirectoryLayer));
}).pipe(Layer.unwrap);
}
export function makeServerRuntimeServicesLayer(): Layer.Layer<
OrchestrationEngineService | ProjectionSnapshotQuery | CheckpointStore | OrchestrationReactor,
unknown,
SqlClient.SqlClient | ProviderService
> {
const orchestrationLayer = OrchestrationEngineLive.pipe(
Layer.provide(OrchestrationProjectionPipelineLive),
Layer.provide(OrchestrationEventStoreLive),
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
);
const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(NodeServices.layer));
const runtimeServicesLayer = Layer.mergeAll(
orchestrationLayer,
OrchestrationProjectionSnapshotQueryLive,
checkpointStoreLayer,
);
const runtimeIngestionLayer = ProviderRuntimeIngestionLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const providerCommandReactorLayer = ProviderCommandReactorLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const checkpointReactorLayer = CheckpointReactorLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const orchestrationReactorLayer = OrchestrationReactorLive.pipe(
Layer.provideMerge(runtimeIngestionLayer),
Layer.provideMerge(providerCommandReactorLayer),
Layer.provideMerge(checkpointReactorLayer),
);
return orchestrationReactorLayer;
}
+29 -13
View File
@@ -6,8 +6,10 @@ import { EventEmitter } from "node:events";
import { Effect, Exit, Layer, PubSub, Scope, Stream } from "effect";
import { describe, expect, it, afterEach, vi } from "vitest";
import { createServer, makeServerProviderLayer, makeServerRuntimeServicesLayer } from "./wsServer";
import { createServer } from "./wsServer";
import WebSocket from "ws";
import { ServerConfig, type ServerConfigShape } from "./config";
import { makeServerProviderLayer, makeServerRuntimeServicesLayer } from "./serverLayers";
import {
DEFAULT_TERMINAL_ID,
@@ -40,7 +42,7 @@ import type { TerminalManager } from "./terminalManager";
import { SqlitePersistenceMemory } from "./persistence/Layers/Sqlite";
import { SqlClient } from "effect/unstable/sql";
import { ProviderService, type ProviderServiceShape } from "./provider/Services/ProviderService";
import type { OpenShape } from "./open";
import { Open, type OpenShape } from "./open";
interface PendingMessages {
queue: unknown[];
@@ -56,6 +58,11 @@ const asProviderThreadId = (value: string): ProviderThreadId => ProviderThreadId
const asProviderTurnId = (value: string): ProviderTurnId => ProviderTurnId.makeUnsafe(value);
const asProviderItemId = (value: string): ProviderItemId => ProviderItemId.makeUnsafe(value);
const defaultOpenService: OpenShape = {
openBrowser: () => Effect.void,
openInEditor: () => Effect.void,
};
class MockTerminalManager extends EventEmitter<{ event: [event: TerminalEvent] }> {
private readonly sessions = new Map<string, TerminalSessionSnapshot>();
@@ -323,28 +330,37 @@ describe("WebSocket Server", () => {
const stateDir = options.stateDir ?? makeTempDir("t3code-ws-state-");
const scope = await Effect.runPromise(Scope.make("sequential"));
const persistenceLayer = options.persistenceLayer ?? SqlitePersistenceMemory;
const providerLayer = options.providerLayer ?? makeServerProviderLayer(stateDir);
const providerLayer = options.providerLayer ?? makeServerProviderLayer();
const openLayer = Layer.succeed(Open, options.open ?? defaultOpenService);
const serverConfigLayer = Layer.succeed(ServerConfig, {
mode: "web",
port: 0,
host: undefined,
cwd: options.cwd ?? "/test/project",
stateDir,
staticDir: undefined,
devUrl: options.devUrl ? new URL(options.devUrl) : undefined,
noBrowser: true,
authToken: options.authToken,
} satisfies ServerConfigShape);
const infrastructureLayer = providerLayer.pipe(Layer.provideMerge(persistenceLayer));
const runtimeLayer = Layer.merge(
makeServerRuntimeServicesLayer().pipe(Layer.provide(infrastructureLayer)),
infrastructureLayer,
);
const dependenciesLayer = Layer.empty.pipe(
Layer.provideMerge(runtimeLayer),
Layer.provideMerge(openLayer),
Layer.provideMerge(serverConfigLayer),
);
const runtimeServices = await Effect.runPromise(
Layer.build(runtimeLayer).pipe(Scope.provide(scope)),
Layer.build(dependenciesLayer).pipe(Scope.provide(scope)),
);
try {
const runtime = await Effect.runPromise(
createServer({
port: 0,
cwd: options.cwd ?? "/test/project",
stateDir,
...(options.autoBootstrapProjectFromCwd !== undefined
? { autoBootstrapProjectFromCwd: options.autoBootstrapProjectFromCwd }
: {}),
...(options.devUrl ? { devUrl: options.devUrl } : {}),
...(options.authToken ? { authToken: options.authToken } : {}),
...(options.open ? { open: options.open } : {}),
autoBootstrapProjectFromCwd: options.autoBootstrapProjectFromCwd ?? false,
...(options.gitManager ? { gitManager: options.gitManager as never } : {}),
...(options.terminalManager ? { terminalManager: options.terminalManager } : {}),
}).pipe(Effect.provide(runtimeServices), Scope.provide(scope)),
+17 -100
View File
@@ -1,6 +1,5 @@
import fs from "node:fs";
import http from "node:http";
import os from "node:os";
import path from "node:path";
import type { Duplex } from "node:stream";
@@ -16,7 +15,7 @@ import {
WsPush,
WsResponse,
} from "@t3tools/contracts";
import { NodeHttpServer, NodeServices } from "@effect/platform-node";
import { NodeHttpServer } from "@effect/platform-node";
import { Cause, Effect, Exit, Layer, Schema, Scope, ServiceMap, Stream } from "effect";
import { WebSocketServer, type WebSocket } from "ws";
@@ -34,30 +33,14 @@ import {
import { TerminalManager } from "./terminalManager";
import { loadResolvedKeybindingsConfig, upsertKeybindingRule } from "./keybindings";
import { searchWorkspaceEntries } from "./workspaceEntries";
import { OrchestrationEngineLive } from "./orchestration/Layers/OrchestrationEngine";
import { CheckpointReactorLive } from "./orchestration/Layers/CheckpointReactor";
import { OrchestrationReactorLive } from "./orchestration/Layers/OrchestrationReactor";
import { ProviderCommandReactorLive } from "./orchestration/Layers/ProviderCommandReactor";
import { OrchestrationProjectionPipelineLive } from "./orchestration/Layers/ProjectionPipeline";
import { OrchestrationProjectionSnapshotQueryLive } from "./orchestration/Layers/ProjectionSnapshotQuery";
import { ProviderRuntimeIngestionLive } from "./orchestration/Layers/ProviderRuntimeIngestion";
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine";
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery";
import { OrchestrationReactor } from "./orchestration/Services/OrchestrationReactor";
import { makeSqlitePersistenceLive } from "./persistence/Layers/Sqlite";
import { OrchestrationEventStoreLive } from "./persistence/Layers/OrchestrationEventStore";
import { OrchestrationCommandReceiptRepositoryLive } from "./persistence/Layers/OrchestrationCommandReceipts";
import { makeCodexAdapterLive } from "./provider/Layers/CodexAdapter";
import { ProviderAdapterRegistryLive } from "./provider/Layers/ProviderAdapterRegistry";
import { makeProviderServiceLive } from "./provider/Layers/ProviderService";
import { ProviderSessionDirectoryLive } from "./provider/Layers/ProviderSessionDirectory";
import { ProviderService } from "./provider/Services/ProviderService";
import { CheckpointStoreLive } from "./checkpointing/Layers/CheckpointStore";
import { ProviderSessionRuntimeRepositoryLive } from "./persistence/Layers/ProviderSessionRuntime";
import { CheckpointStore } from "./checkpointing/Services/CheckpointStore";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import { clamp } from "effect/Number";
import { OpenDefaultService, type OpenShape } from "./open";
import { Open } from "./open";
import { ServerConfig } from "./config";
const MIME_TYPES: Record<string, string> = {
".html": "text/html; charset=utf-8",
@@ -75,24 +58,16 @@ const MIME_TYPES: Record<string, string> = {
};
export interface ServerOptions {
port: number;
host?: string | undefined;
cwd: string;
autoBootstrapProjectFromCwd?: boolean | undefined;
stateDir?: string | undefined;
staticDir?: string | undefined;
devUrl?: string | undefined;
logWebSocketEvents?: boolean | undefined;
gitManager?: GitManager | undefined;
terminalManager?: TerminalManager | undefined;
authToken?: string | undefined;
open?: OpenShape | undefined;
}
export interface ServerShape {
readonly createServer: (
options: ServerOptions,
) => Effect.Effect<http.Server, unknown, Scope.Scope | ServerRuntimeServices>;
options?: ServerOptions,
) => Effect.Effect<http.Server, unknown, Scope.Scope | ServerRuntimeServices | ServerConfig>;
readonly stopSignal: Effect.Effect<unknown, never>;
}
@@ -174,66 +149,11 @@ export type ServerCoreRuntimeServices =
| OrchestrationEngineService
| ProjectionSnapshotQuery
| CheckpointStore
| OrchestrationReactor;
| OrchestrationReactor
| Open;
export type ServerRuntimeServices = ServerCoreRuntimeServices | ProviderService;
export function makeServerPersistenceLayer(stateDir: string) {
const dbPath = path.join(stateDir, "orchestration.sqlite");
return makeSqlitePersistenceLive(dbPath);
}
export function makeServerProviderLayer(
stateDir: string,
): Layer.Layer<ProviderService, unknown, SqlClient.SqlClient> {
const providerLogsDir = path.join(stateDir, "logs", "providers");
const codexAdapterLayer = makeCodexAdapterLive({
nativeEventLogPath: path.join(providerLogsDir, "provider-native.ndjson"),
});
const adapterRegistryLayer = ProviderAdapterRegistryLive.pipe(Layer.provide(codexAdapterLayer));
const providerSessionDirectoryLayer = ProviderSessionDirectoryLive.pipe(
Layer.provide(ProviderSessionRuntimeRepositoryLive),
);
return makeProviderServiceLive({
canonicalEventLogPath: path.join(providerLogsDir, "provider-canonical.ndjson"),
}).pipe(Layer.provide(adapterRegistryLayer), Layer.provide(providerSessionDirectoryLayer));
}
export function makeServerRuntimeServicesLayer(): Layer.Layer<
ServerCoreRuntimeServices,
unknown,
SqlClient.SqlClient | ProviderService
> {
const orchestrationLayer = OrchestrationEngineLive.pipe(
Layer.provide(OrchestrationProjectionPipelineLive),
Layer.provide(OrchestrationEventStoreLive),
Layer.provide(OrchestrationCommandReceiptRepositoryLive),
);
const checkpointStoreLayer = CheckpointStoreLive.pipe(Layer.provide(NodeServices.layer));
const runtimeServicesLayer = Layer.mergeAll(
orchestrationLayer,
OrchestrationProjectionSnapshotQueryLive,
checkpointStoreLayer,
);
const runtimeIngestionLayer = ProviderRuntimeIngestionLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const providerCommandReactorLayer = ProviderCommandReactorLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const checkpointReactorLayer = CheckpointReactorLive.pipe(
Layer.provideMerge(runtimeServicesLayer),
);
const orchestrationReactorLayer = OrchestrationReactorLive.pipe(
Layer.provideMerge(runtimeIngestionLayer),
Layer.provideMerge(providerCommandReactorLayer),
Layer.provideMerge(checkpointReactorLayer),
);
return orchestrationReactorLayer;
}
export class ServerLifecycleError extends Schema.TaggedErrorClass<ServerLifecycleError>()(
"ServerLifecycleError",
{
@@ -246,22 +166,18 @@ class RouteRequestError extends Schema.TaggedErrorClass<RouteRequestError>()("Ro
message: Schema.String,
}) {}
export const createServer = Effect.fn(function* (
options: ServerOptions,
): Effect.fn.Return<http.Server, unknown, Scope.Scope | ServerRuntimeServices> {
export const createServer = Effect.fn(function* (options: ServerOptions = {}): Effect.fn.Return<
http.Server,
unknown, // TODO: This should not be unknown
Scope.Scope | ServerRuntimeServices | ServerConfig
> {
const { port, cwd, staticDir, devUrl, authToken, mode, host } = yield* ServerConfig;
const {
port,
host,
cwd,
autoBootstrapProjectFromCwd = false,
staticDir,
devUrl,
logWebSocketEvents: explicitLogWsEvents,
gitManager = new GitManager(),
terminalManager = new TerminalManager(),
authToken,
open: openService = OpenDefaultService,
} = options;
const autoBootstrapProjectFromCwd = options.autoBootstrapProjectFromCwd ?? mode === "web";
const clients = new Set<WebSocket>();
const logger = createLogger("ws");
@@ -299,7 +215,7 @@ export const createServer = Effect.fn(function* (
const httpServer = http.createServer((req, res) => {
// In dev mode, redirect to Vite dev server
if (devUrl) {
res.writeHead(302, { Location: devUrl });
res.writeHead(302, { Location: devUrl.href });
res.end();
return;
}
@@ -382,6 +298,7 @@ export const createServer = Effect.fn(function* (
const liveCheckpointStore = yield* CheckpointStore;
const liveProviderService = yield* ProviderService;
const orchestrationReactor = yield* OrchestrationReactor;
const { openInEditor } = yield* Open;
const subscriptionsScope = yield* Scope.make("sequential");
yield* Effect.addFinalizer(() =>
@@ -574,7 +491,7 @@ export const createServer = Effect.fn(function* (
case WS_METHODS.shellOpenInEditor: {
const body = stripRequestTag(request.body);
return yield* openService.openInEditor(body);
return yield* openInEditor(body);
}
case WS_METHODS.gitStatus: {