Files
t3code/apps/server/src/orchestration-v2/http.ts
T
Julius Marmingeandcodex 309fcabe1f Merge origin/main into orchestrator v2
Port current mainline fixes onto the orchestrator v2 architecture.

Co-authored-by: codex <[email protected]>
2026-07-20 16:07:59 +02:00

118 lines
4.5 KiB
TypeScript

import {
AuthOrchestrationReadScope,
EnvironmentHttpApi,
type OrchestrationProjectShell,
} from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Predicate from "effect/Predicate";
import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
import * as SqlClient from "effect/unstable/sql/SqlClient";
import {
annotateEnvironmentRequest,
failEnvironmentInternal,
failEnvironmentNotFound,
requireEnvironmentScope,
} from "../auth/http.ts";
import * as ProjectionSnapshotQuery from "../orchestration/Services/ProjectionSnapshotQuery.ts";
import * as OrchestrationEventStore from "../persistence/Services/OrchestrationEventStore.ts";
import * as ProjectEnrichmentService from "../project/ProjectEnrichmentService.ts";
import * as ThreadManagementService from "./ThreadManagementService.ts";
function isThreadNotFound(error: unknown): boolean {
return (
Predicate.hasProperty(error, "cause") &&
Predicate.hasProperty(error.cause, "_tag") &&
error.cause._tag === "ProjectionStoreThreadNotFoundError"
);
}
/**
* Serves orchestration V2 snapshots over HTTP so clients can load the
* (potentially large) shell and thread projections off the socket — gzip
* compressible and cacheable — and then resume the WebSocket subscription via
* `afterSequence`.
*/
export const orchestrationHttpApiLayer = HttpApiBuilder.group(
EnvironmentHttpApi,
"orchestration",
Effect.fnUntraced(function* (handlers) {
const sql = yield* SqlClient.SqlClient;
const threadManagement = yield* ThreadManagementService.ThreadManagementService;
const applicationEvents = yield* OrchestrationEventStore.OrchestrationEventStore;
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery.ProjectionSnapshotQuery;
const projectEnrichment = yield* ProjectEnrichmentService.ProjectEnrichmentService;
const enrichProjectShells = Effect.fn("http.orchestration.enrichProjectShells")(
(projects: ReadonlyArray<OrchestrationProjectShell>) =>
Effect.forEach(
projects,
(project) =>
projectEnrichment.getAvailable(project.workspaceRoot).pipe(
Effect.map((enrichment) => ({
...project,
repositoryIdentity: enrichment.repositoryIdentity,
})),
),
{ concurrency: 16 },
),
);
const loadShellSnapshot = Effect.fn("http.orchestration.loadShellSnapshot")(function* () {
const base = yield* sql.withTransaction(
Effect.gen(function* () {
const projects = yield* projectionSnapshotQuery.getShellSnapshotWithoutEnrichment();
const threads = yield* threadManagement.getShellSnapshot();
return {
schemaVersion: threads.schemaVersion,
snapshotSequence: yield* applicationEvents.latestApplicationSequence,
projects: projects.projects,
threads: threads.threads,
archivedThreads: threads.archivedThreads,
} as const;
}),
);
const projects = yield* enrichProjectShells(base.projects);
return { ...base, projects };
});
return handlers
.handle(
"shellSnapshot",
Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
yield* annotateEnvironmentRequest(args.endpoint.name);
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
return yield* loadShellSnapshot().pipe(
Effect.catch((cause) =>
failEnvironmentInternal("orchestration_snapshot_failed", cause),
),
);
}),
)
.handle(
"threadSnapshot",
Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
yield* annotateEnvironmentRequest(args.endpoint.name);
yield* requireEnvironmentScope(AuthOrchestrationReadScope);
const snapshot = yield* threadManagement.getThreadSnapshot(args.params.threadId).pipe(
Effect.catch(
Effect.fnUntraced(function* (error) {
if (isThreadNotFound(error)) {
return yield* failEnvironmentNotFound("thread_not_found");
}
return yield* failEnvironmentInternal(
"orchestration_thread_snapshot_failed",
error,
);
}),
),
);
return {
snapshotSequence: snapshot.snapshotSequence,
projection: snapshot.projection,
};
}),
);
}),
);