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

http.ts

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