more effect: stage 1
This commit is contained in:
1 parent
4f4691676a
commit
2bece01a3b
7 files changed
+132
-7
No files matched your search
@@ -9,6 +9,7 @@ import { OrchestrationCommandSchema } from "@t3tools/contracts";
|
||||
import { PubSub, Queue, Schema, Stream, Effect, Fiber, Runtime, Either } from "effect";
|
||||
|
||||
import { createLogger } from "../logger";
|
||||
import type { OrchestrationEventStore } from "./eventStore";
|
||||
import { SqliteEventStore } from "./eventStore";
|
||||
import { createEmptyReadModel, reduceEvent } from "./reducer";
|
||||
import { UI_ENTITY_CONTRACTS } from "./uiContractInventory";
|
||||
@@ -190,7 +191,7 @@ function mapCommandToEvent(command: OrchestrationCommand): Omit<OrchestrationEve
|
||||
export class OrchestrationEngine {
|
||||
private readonly logger = createLogger("orchestration");
|
||||
private readonly runtime = Runtime.defaultRuntime;
|
||||
private readonly eventStore: SqliteEventStore;
|
||||
private readonly eventStore: OrchestrationEventStore;
|
||||
|
||||
private readModel: OrchestrationReadModel;
|
||||
private commandQueue: Queue.Queue<CommandEnvelope>;
|
||||
@@ -200,9 +201,15 @@ export class OrchestrationEngine {
|
||||
private readonly readModelListeners = new Set<(snapshot: OrchestrationReadModel) => void>();
|
||||
private readonly domainEventListeners = new Set<(event: OrchestrationEvent) => void>();
|
||||
|
||||
constructor(stateDir: string) {
|
||||
const dbPath = path.join(stateDir, "orchestration.sqlite");
|
||||
this.eventStore = new SqliteEventStore(dbPath);
|
||||
constructor(stateDir: string);
|
||||
constructor(eventStore: OrchestrationEventStore);
|
||||
constructor(stateDirOrEventStore: string | OrchestrationEventStore) {
|
||||
if (typeof stateDirOrEventStore === "string") {
|
||||
const dbPath = path.join(stateDirOrEventStore, "orchestration.sqlite");
|
||||
this.eventStore = new SqliteEventStore(dbPath);
|
||||
} else {
|
||||
this.eventStore = stateDirOrEventStore;
|
||||
}
|
||||
this.readModel = createEmptyReadModel(new Date().toISOString());
|
||||
this.commandQueue = Runtime.runSync(this.runtime)(Queue.unbounded<CommandEnvelope>());
|
||||
this.readModelPubSub = Runtime.runSync(this.runtime)(
|
||||
|
||||
@@ -6,6 +6,13 @@ import type { SQLOutputValue } from "node:sqlite";
|
||||
import type { OrchestrationEvent } from "@t3tools/contracts";
|
||||
import { Effect } from "effect";
|
||||
|
||||
export interface OrchestrationEventStore {
|
||||
append(event: Omit<OrchestrationEvent, "sequence">): Effect.Effect<OrchestrationEvent>;
|
||||
readFromSequence(sequenceExclusive: number, limit?: number): Effect.Effect<OrchestrationEvent[]>;
|
||||
readAll(): Effect.Effect<OrchestrationEvent[]>;
|
||||
close(): void;
|
||||
}
|
||||
|
||||
interface EventRow {
|
||||
sequence: number;
|
||||
event_id: string;
|
||||
@@ -50,7 +57,7 @@ function readRequiredNumber(
|
||||
throw new Error(`Invalid SQLite value for ${String(column)}: expected number`);
|
||||
}
|
||||
|
||||
export class SqliteEventStore {
|
||||
export class SqliteEventStore implements OrchestrationEventStore {
|
||||
private readonly db: DatabaseSync;
|
||||
private readonly insertStatement: StatementSync;
|
||||
private closed = false;
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
import fs from "node:fs";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { createOrchestrationEngine } from "./runtime";
|
||||
|
||||
const tempDirs: string[] = [];
|
||||
|
||||
function makeTempDir(prefix: string): string {
|
||||
const dir = fs.mkdtempSync(path.join(os.tmpdir(), prefix));
|
||||
tempDirs.push(dir);
|
||||
return dir;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
for (const dir of tempDirs.splice(0, tempDirs.length)) {
|
||||
fs.rmSync(dir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
describe("orchestration layers", () => {
|
||||
it("creates an engine through Context.Tag wiring", async () => {
|
||||
const stateDir = makeTempDir("t3code-orchestration-layer-");
|
||||
const createdAt = new Date().toISOString();
|
||||
const engine = createOrchestrationEngine(stateDir);
|
||||
|
||||
await engine.start();
|
||||
await engine.dispatch({
|
||||
type: "project.create",
|
||||
commandId: "cmd-layer-1",
|
||||
projectId: "project-layer-1",
|
||||
name: "layer",
|
||||
cwd: "/tmp/layer",
|
||||
model: "gpt-5-codex",
|
||||
createdAt,
|
||||
});
|
||||
const firstSnapshot = engine.getSnapshot();
|
||||
await engine.stop();
|
||||
|
||||
const restarted = createOrchestrationEngine(stateDir);
|
||||
await restarted.start();
|
||||
expect(restarted.getSnapshot()).toEqual(firstSnapshot);
|
||||
await restarted.stop();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
import path from "node:path";
|
||||
|
||||
import { Effect, Layer } from "effect";
|
||||
|
||||
import { OrchestrationEngine } from "./engine";
|
||||
import { SqliteEventStore } from "./eventStore";
|
||||
import {
|
||||
OrchestrationConfig,
|
||||
OrchestrationEngineService,
|
||||
OrchestrationEventStoreService,
|
||||
} from "./services";
|
||||
|
||||
export const OrchestrationEventStoreLive = Layer.effect(
|
||||
OrchestrationEventStoreService,
|
||||
Effect.map(OrchestrationConfig, ({ stateDir }) => {
|
||||
const dbPath = path.join(stateDir, "orchestration.sqlite");
|
||||
return new SqliteEventStore(dbPath);
|
||||
}),
|
||||
);
|
||||
|
||||
export const OrchestrationEngineLive = Layer.effect(
|
||||
OrchestrationEngineService,
|
||||
Effect.map(OrchestrationEventStoreService, (eventStore) => new OrchestrationEngine(eventStore)),
|
||||
);
|
||||
|
||||
export const OrchestrationLive = OrchestrationEngineLive.pipe(
|
||||
Layer.provide(OrchestrationEventStoreLive),
|
||||
);
|
||||
@@ -0,0 +1,14 @@
|
||||
import { Effect, Layer, Runtime } from "effect";
|
||||
|
||||
import type { OrchestrationEngine } from "./engine";
|
||||
import { OrchestrationLive } from "./layers";
|
||||
import { OrchestrationConfig, OrchestrationEngineService } from "./services";
|
||||
|
||||
export function createOrchestrationEngine(stateDir: string): OrchestrationEngine {
|
||||
const orchestrationLayer = OrchestrationLive.pipe(
|
||||
Layer.provide(Layer.succeed(OrchestrationConfig, { stateDir })),
|
||||
);
|
||||
return Runtime.runSync(Runtime.defaultRuntime)(
|
||||
Effect.provide(OrchestrationEngineService, orchestrationLayer),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Context } from "effect";
|
||||
|
||||
import { OrchestrationEngine } from "./engine";
|
||||
import type { OrchestrationEventStore } from "./eventStore";
|
||||
|
||||
export interface OrchestrationConfigShape {
|
||||
readonly stateDir: string;
|
||||
}
|
||||
|
||||
export class OrchestrationConfig extends Context.Tag("orchestration/Config")<
|
||||
OrchestrationConfig,
|
||||
OrchestrationConfigShape
|
||||
>() {}
|
||||
|
||||
export class OrchestrationEventStoreService extends Context.Tag(
|
||||
"orchestration/EventStore",
|
||||
)<OrchestrationEventStoreService, OrchestrationEventStore>() {}
|
||||
|
||||
export class OrchestrationEngineService extends Context.Tag("orchestration/Engine")<
|
||||
OrchestrationEngineService,
|
||||
OrchestrationEngine
|
||||
>() {}
|
||||
@@ -36,7 +36,7 @@ import {
|
||||
import { TerminalManager } from "./terminalManager";
|
||||
import { loadResolvedKeybindingsConfig, upsertKeybindingRule } from "./keybindings";
|
||||
import { searchWorkspaceEntries } from "./workspaceEntries";
|
||||
import { OrchestrationEngine } from "./orchestration/engine";
|
||||
import { createOrchestrationEngine } from "./orchestration/runtime";
|
||||
|
||||
const MIME_TYPES: Record<string, string> = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
@@ -107,7 +107,7 @@ export function createServer(options: ServerOptions) {
|
||||
const projectRegistry =
|
||||
providedRegistry ?? new ProjectRegistry(path.join(os.homedir(), ".t3", "userdata"));
|
||||
const gitManager = providedGitManager ?? new GitManager();
|
||||
const orchestrationEngine = new OrchestrationEngine(
|
||||
const orchestrationEngine = createOrchestrationEngine(
|
||||
stateDir ?? path.join(os.homedir(), ".t3", "userdata"),
|
||||
);
|
||||
const clients = new Set<WebSocket>();
|
||||
|
||||
Reference in new issue
Block a user