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

http.ts

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