Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

http.ts

99 lines3.9 KB

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 // Serve the lightweight command read model (thread bodies empty)
36 // instead of the fully hydrated snapshot. Hydrating every message
37 // and activity payload in the database has OOM-killed servers, and
38 // the route's only consumer (the project CLI) reads projects alone —
39 // UI clients load the shell and per-thread snapshots instead.
40 return yield* projectionSnapshotQuery
41 .getCommandReadModel()
42 .pipe(
43 Effect.catch((cause) =>
44 failEnvironmentInternal("orchestration_snapshot_failed", cause),
45 ),
46 );
47 }),
48 )
49 .handle(
50 "shellSnapshot",
51 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
52 yield* annotateEnvironmentRequest(args.endpoint.name);
53 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
54 return yield* projectionSnapshotQuery
55 .getShellSnapshot()
56 .pipe(
57 Effect.catch((cause) =>
58 failEnvironmentInternal("orchestration_snapshot_failed", cause),
59 ),
60 );
61 }),
62 )
63 .handle(
64 "threadSnapshot",
65 Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
66 yield* annotateEnvironmentRequest(args.endpoint.name);
67 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
68 const snapshot = yield* projectionSnapshotQuery
69 .getThreadDetailSnapshot(args.params.threadId)
70 .pipe(
71 Effect.catch((cause) =>
72 failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
73 ),
74 );
75 if (Option.isNone(snapshot)) {
76 return yield* failEnvironmentNotFound("thread_not_found");
77 }
78 return projectThreadDetailSnapshot(snapshot.value);
79 }),
80 )
81 .handle(
82 "dispatch",
83 Effect.fn("environment.orchestration.dispatch")(function* (args) {
84 yield* annotateEnvironmentRequest(args.endpoint.name);
85 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
86 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
87 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
88 );
89 return yield* orchestrationEngine
90 .dispatch(normalizedCommand)
91 .pipe(
92 Effect.catch((cause) =>
93 failEnvironmentInternal("orchestration_dispatch_failed", cause),
94 ),
95 );
96 }),
97 );
98 }),
99);