Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 EnvironmentHttpApi,
5} from "@t3tools/contracts";
6import * as Effect from "effect/Effect";
7import * as Option from "effect/Option";
8import * as HttpApiBuilder from "effect/unstable/httpapi/HttpApiBuilder";
9
10import { projectThreadDetailSnapshot } from "./ActivityPayloadProjection.ts";
11import { normalizeDispatchCommand } from "./Normalizer.ts";
12import {
13 annotateEnvironmentRequest,
14 failEnvironmentInternal,
15 failEnvironmentInvalidRequest,
16 failEnvironmentNotFound,
17 requireEnvironmentScope,
18} from "../auth/http.ts";
19import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
20import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
21
22export const orchestrationHttpApiLayer = HttpApiBuilder.group(
23 EnvironmentHttpApi,
24 "orchestration",
25 Effect.fnUntraced(function* (handlers) {
26 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
27 const orchestrationEngine = yield* OrchestrationEngineService;
28
29 return handlers
30 .handle(
31 "snapshot",
32 Effect.fn("environment.orchestration.snapshot")(function* (args) {
33 yield* annotateEnvironmentRequest(args.endpoint.name);
34 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
35 // The only consumer (the `t3` CLI project resolver) reads just
36 // `.projects`, so use the command read model — it returns the same
37 // OrchestrationReadModel shape but never materialises the per-thread
38 // activity/message/checkpoint tables (490MB+ on a busy DB → heap OOM).
39 return yield* projectionSnapshotQuery
40 .getCommandReadModel()
41 .pipe(
42 Effect.catch((cause) =>
43 failEnvironmentInternal("orchestration_snapshot_failed", cause),
44 ),
45 );
46 }),
47 )
48 .handle(
49 "shellSnapshot",
50 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
51 yield* annotateEnvironmentRequest(args.endpoint.name);
52 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
53 return yield* projectionSnapshotQuery
54 .getShellSnapshot()
55 .pipe(
56 Effect.catch((cause) =>
57 failEnvironmentInternal("orchestration_snapshot_failed", cause),
58 ),
59 );
60 }),
61 )
62 .handle(
63 "threadSnapshot",
64 Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
65 yield* annotateEnvironmentRequest(args.endpoint.name);
66 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
67 const snapshot = yield* projectionSnapshotQuery
68 .getThreadDetailSnapshot(args.params.threadId)
69 .pipe(
70 Effect.catch((cause) =>
71 failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
72 ),
73 );
74 if (Option.isNone(snapshot)) {
75 return yield* failEnvironmentNotFound("thread_not_found");
76 }
77 return projectThreadDetailSnapshot(snapshot.value);
78 }),
79 )
80 .handle(
81 "dispatch",
82 Effect.fn("environment.orchestration.dispatch")(function* (args) {
83 yield* annotateEnvironmentRequest(args.endpoint.name);
84 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
85 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
86 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
87 );
88 return yield* orchestrationEngine
89 .dispatch(normalizedCommand)
90 .pipe(
91 Effect.catch((cause) =>
92 failEnvironmentInternal("orchestration_dispatch_failed", cause),
93 ),
94 );
95 }),
96 );
97 }),
98);