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

http.ts

109 lines4.2 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(
70 args.params.threadId,
71 args.payload.turnLimit === undefined
72 ? undefined
73 : {
74 turnLimit: args.payload.turnLimit,
75 ...(args.payload.beforeCursor !== undefined
76 ? { beforeCursor: args.payload.beforeCursor }
77 : {}),
78 },
79 )
80 .pipe(
81 Effect.catch((cause) =>
82 failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
83 ),
84 );
85 if (Option.isNone(snapshot)) {
86 return yield* failEnvironmentNotFound("thread_not_found");
87 }
88 return projectThreadDetailSnapshot(snapshot.value);
89 }),
90 )
91 .handle(
92 "dispatch",
93 Effect.fn("environment.orchestration.dispatch")(function* (args) {
94 yield* annotateEnvironmentRequest(args.endpoint.name);
95 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
96 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
97 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
98 );
99 return yield* orchestrationEngine
100 .dispatch(normalizedCommand)
101 .pipe(
102 Effect.catch((cause) =>
103 failEnvironmentInternal("orchestration_dispatch_failed", cause),
104 ),
105 );
106 }),
107 );
108 }),
109);