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

http.ts

128 lines5.0 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 { cleanupFailedUploadedAttachments, normalizeDispatchCommand } from "./Normalizer.ts";
12import {
13 annotateEnvironmentRequest,
14 failEnvironmentInternal,
15 failEnvironmentInvalidRequest,
16 failEnvironmentNotFound,
17 requireEnvironmentScope,
18} from "../auth/http.ts";
19import * as ProjectCloneTracker from "../project/ProjectCloneTracker.ts";
20import { OrchestrationEngineService } from "./Services/OrchestrationEngine.ts";
21import { ProjectionSnapshotQuery } from "./Services/ProjectionSnapshotQuery.ts";
22​
23export const orchestrationHttpApiLayer = HttpApiBuilder.group(
24 EnvironmentHttpApi,
25 "orchestration",
26 Effect.fnUntraced(function* (handlers) {
27 const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
28 const orchestrationEngine = yield* OrchestrationEngineService;
29 const projectCloneTracker = yield* ProjectCloneTracker.ProjectCloneTracker;
30​
31 return handlers
32 .handle(
33 "snapshot",
34 Effect.fn("environment.orchestration.snapshot")(function* (args) {
35 yield* annotateEnvironmentRequest(args.endpoint.name);
36 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
37 // Serve the lightweight command read model (thread bodies empty)
38 // instead of the fully hydrated snapshot. Hydrating every message
39 // and activity payload in the database has OOM-killed servers, and
40 // the route's only consumer (the project CLI) reads projects alone —
41 // UI clients load the shell and per-thread snapshots instead.
42 return yield* projectionSnapshotQuery
43 .getCommandReadModel()
44 .pipe(
45 Effect.catch((cause) =>
46 failEnvironmentInternal("orchestration_snapshot_failed", cause),
47 ),
48 );
49 }),
50 )
51 .handle(
52 "shellSnapshot",
53 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
54 yield* annotateEnvironmentRequest(args.endpoint.name);
55 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
56 return yield* projectionSnapshotQuery
57 .getShellSnapshot()
58 .pipe(
59 Effect.catch((cause) =>
60 failEnvironmentInternal("orchestration_snapshot_failed", cause),
61 ),
62 );
63 }),
64 )
65 .handle(
66 "threadSnapshot",
67 Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
68 yield* annotateEnvironmentRequest(args.endpoint.name);
69 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
70 const snapshot = yield* projectionSnapshotQuery
71 .getThreadDetailSnapshot(
72 args.params.threadId,
73 args.payload.turnLimit === undefined
74 ? undefined
75 : {
76 turnLimit: args.payload.turnLimit,
77 ...(args.payload.beforeCursor !== undefined
78 ? { beforeCursor: args.payload.beforeCursor }
79 : {}),
80 },
81 )
82 .pipe(
83 Effect.catch((cause) =>
84 failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
85 ),
86 );
87 if (Option.isNone(snapshot)) {
88 return yield* failEnvironmentNotFound("thread_not_found");
89 }
90 return projectThreadDetailSnapshot(
91 snapshot.value,
92 args.payload.reasoningMessages === "true",
93 );
94 }),
95 )
96 .handle(
97 "dispatch",
98 Effect.fn("environment.orchestration.dispatch")(function* (args) {
99 yield* annotateEnvironmentRequest(args.endpoint.name);
100 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
101 yield* ProjectCloneTracker.rejectCommandsDuringClone(
102 projectCloneTracker,
103 args.payload,
104 ).pipe(
105 Effect.catch((cause) =>
106 failEnvironmentInternal("orchestration_dispatch_failed", cause),
107 ),
108 );
109 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
110 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
111 );
112 const result = yield* orchestrationEngine.dispatch(normalizedCommand).pipe(
113 Effect.tapError(() =>
114 cleanupFailedUploadedAttachments(args.payload, normalizedCommand),
115 ),
116 Effect.catch((cause) =>
117 failEnvironmentInternal("orchestration_dispatch_failed", cause),
118 ),
119 );
120 yield* ProjectCloneTracker.discardCloneForDeletedProject(
121 projectCloneTracker,
122 normalizedCommand,
123 );
124 return result;
125 }),
126 );
127 }),
128);