Open History to see the last commit
1import {
2 ProviderApprovalDecision,
3 ProviderSessionId,
4 ProviderUserInputAnswers,
5 RuntimeRequestId,
6 ThreadId,
7} from "@t3tools/contracts";
8import * as Context from "effect/Context";
9import * as Effect from "effect/Effect";
10import * as Layer from "effect/Layer";
11import * as Option from "effect/Option";
12import * as Schema from "effect/Schema";
13
14import { ProjectionStoreV2 } from "./ProjectionStore.ts";
15import { ProviderSessionManagerV2 } from "./ProviderSessionManager.ts";
16
17export class RuntimeRequestResponseExecutionError extends Schema.TaggedErrorClass<RuntimeRequestResponseExecutionError>()(
18 "RuntimeRequestResponseExecutionError",
19 {
20 threadId: ThreadId,
21 requestId: RuntimeRequestId,
22 cause: Schema.optional(Schema.Defect()),
23 },
24) {}
25
26export interface RuntimeRequestServiceV2Shape {
27 readonly respond: (input: {
28 readonly threadId: ThreadId;
29 readonly providerSessionId: ProviderSessionId;
30 readonly requestId: RuntimeRequestId;
31 readonly decision?: ProviderApprovalDecision;
32 readonly answers?: ProviderUserInputAnswers;
33 }) => Effect.Effect<void, RuntimeRequestResponseExecutionError>;
34}
35
36export class RuntimeRequestServiceV2 extends Context.Service<
37 RuntimeRequestServiceV2,
38 RuntimeRequestServiceV2Shape
39>()("t3/orchestration-v2/RuntimeRequestService/RuntimeRequestServiceV2") {}
40
41export const layer: Layer.Layer<
42 RuntimeRequestServiceV2,
43 never,
44 ProjectionStoreV2 | ProviderSessionManagerV2
45> = Layer.effect(
46 RuntimeRequestServiceV2,
47 Effect.gen(function* () {
48 const projections = yield* ProjectionStoreV2;
49 const sessions = yield* ProviderSessionManagerV2;
50
51 return RuntimeRequestServiceV2.of({
52 respond: (input) =>
53 Effect.gen(function* () {
54 const projection = yield* projections.getThreadProjection(input.threadId);
55 const request = projection.runtimeRequests.find(
56 (candidate) => candidate.id === input.requestId,
57 );
58 if (request === undefined) {
59 return yield* new RuntimeRequestResponseExecutionError({
60 threadId: input.threadId,
61 requestId: input.requestId,
62 cause: "The runtime request no longer exists.",
63 });
64 }
65 if (
66 request.responseCapability.type !== "live" ||
67 request.responseCapability.providerSessionId !== input.providerSessionId
68 ) {
69 return yield* new RuntimeRequestResponseExecutionError({
70 threadId: input.threadId,
71 requestId: input.requestId,
72 cause: "The runtime request is not resumable on the recorded provider session.",
73 });
74 }
75 const session = yield* sessions.get(input.providerSessionId);
76 if (Option.isNone(session)) {
77 return yield* new RuntimeRequestResponseExecutionError({
78 threadId: input.threadId,
79 requestId: input.requestId,
80 cause: `Provider session ${input.providerSessionId} is not active.`,
81 });
82 }
83 yield* session.value.respondToRuntimeRequest({
84 requestId: input.requestId,
85 ...(input.decision === undefined ? {} : { decision: input.decision }),
86 ...(input.answers === undefined ? {} : { answers: input.answers }),
87 });
88 }).pipe(
89 Effect.mapError((cause) =>
90 Schema.is(RuntimeRequestResponseExecutionError)(cause)
91 ? cause
92 : new RuntimeRequestResponseExecutionError({
93 threadId: input.threadId,
94 requestId: input.requestId,
95 cause,
96 }),
97 ),
98 ),
99 });
100 }),
101);