Open History to see the last commit
1import {
2 AuthOrchestrationOperateScope,
3 AuthOrchestrationReadScope,
4 EnvironmentHttpApi,
5} from "@v12/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 return yield* projectionSnapshotQuery
35 .getSnapshot()
36 .pipe(
37 Effect.catch((cause) =>
38 failEnvironmentInternal("orchestration_snapshot_failed", cause),
39 ),
40 );
41 }),
42 )
43 .handle(
44 "shellSnapshot",
45 Effect.fn("environment.orchestration.shellSnapshot")(function* (args) {
46 yield* annotateEnvironmentRequest(args.endpoint.name);
47 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
48 return yield* projectionSnapshotQuery
49 .getShellSnapshot()
50 .pipe(
51 Effect.catch((cause) =>
52 failEnvironmentInternal("orchestration_snapshot_failed", cause),
53 ),
54 );
55 }),
56 )
57 .handle(
58 "threadSnapshot",
59 Effect.fn("environment.orchestration.threadSnapshot")(function* (args) {
60 yield* annotateEnvironmentRequest(args.endpoint.name);
61 yield* requireEnvironmentScope(AuthOrchestrationReadScope);
62 const snapshot = yield* projectionSnapshotQuery
63 .getThreadDetailSnapshot(args.params.threadId)
64 .pipe(
65 Effect.catch((cause) =>
66 failEnvironmentInternal("orchestration_thread_snapshot_failed", cause),
67 ),
68 );
69 if (Option.isNone(snapshot)) {
70 return yield* failEnvironmentNotFound("thread_not_found");
71 }
72 return snapshot.value;
73 }),
74 )
75 .handle(
76 "dispatch",
77 Effect.fn("environment.orchestration.dispatch")(function* (args) {
78 yield* annotateEnvironmentRequest(args.endpoint.name);
79 yield* requireEnvironmentScope(AuthOrchestrationOperateScope);
80 const normalizedCommand = yield* normalizeDispatchCommand(args.payload).pipe(
81 Effect.catch(() => failEnvironmentInvalidRequest("invalid_command")),
82 );
83 return yield* orchestrationEngine
84 .dispatch(normalizedCommand)
85 .pipe(
86 Effect.catch((cause) =>
87 failEnvironmentInternal("orchestration_dispatch_failed", cause),
88 ),
89 );
90 }),
91 );
92 }),
93);