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

RuntimeRequestService.ts

141 lines5.4 KB

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 reason: Schema.Literals([
21 "request-missing",
22 "request-not-ready",
23 "request-not-resumable",
24 "provider-session-not-active",
25 "unexpected-failure",
26 ]),
27 threadId: ThreadId,
28 providerSessionId: ProviderSessionId,
29 requestId: RuntimeRequestId,
30 cause: Schema.optional(Schema.Defect()),
31 },
32) {
33 override get message(): string {
34 switch (this.reason) {
35 case "request-missing":
36 return `Runtime request ${this.requestId} no longer exists on thread ${this.threadId}.`;
37 case "request-not-ready":
38 return `Runtime request ${this.requestId} on thread ${this.threadId} is not ready for response execution.`;
39 case "request-not-resumable":
40 return `Runtime request ${this.requestId} on thread ${this.threadId} is not resumable on provider session ${this.providerSessionId}.`;
41 case "provider-session-not-active":
42 return `Provider session ${this.providerSessionId} is not active for runtime request ${this.requestId} on thread ${this.threadId}.`;
43 case "unexpected-failure":
44 return `Failed to respond to runtime request ${this.requestId} on thread ${this.threadId} via provider session ${this.providerSessionId}.`;
45 }
46 }
48​
49const isRuntimeRequestResponseExecutionError = Schema.is(RuntimeRequestResponseExecutionError);
50​
51export interface RuntimeRequestServiceV2Shape {
52 readonly respond: (input: {
53 readonly threadId: ThreadId;
54 readonly providerSessionId: ProviderSessionId;
55 readonly requestId: RuntimeRequestId;
56 readonly decision?: ProviderApprovalDecision;
57 readonly answers?: ProviderUserInputAnswers;
58 }) => Effect.Effect<void, RuntimeRequestResponseExecutionError>;
60​
61export class RuntimeRequestServiceV2 extends Context.Service<
62 RuntimeRequestServiceV2,
63 RuntimeRequestServiceV2Shape
64>()("t3/orchestration-v2/RuntimeRequestService/RuntimeRequestServiceV2") {}
65​
66export const layer: Layer.Layer<
67 RuntimeRequestServiceV2,
68 never,
69 ProjectionStoreV2 | ProviderSessionManagerV2
70> = Layer.effect(
71 RuntimeRequestServiceV2,
72 Effect.gen(function* () {
73 const projections = yield* ProjectionStoreV2;
74 const sessions = yield* ProviderSessionManagerV2;
75​
76 return RuntimeRequestServiceV2.of({
77 respond: (input) =>
78 Effect.gen(function* () {
79 const projection = yield* projections.getThreadProjection(input.threadId);
80 const request = projection.runtimeRequests.find(
81 (candidate) => candidate.id === input.requestId,
82 );
83 if (request === undefined) {
84 return yield* new RuntimeRequestResponseExecutionError({
85 reason: "request-missing",
86 threadId: input.threadId,
87 providerSessionId: input.providerSessionId,
88 requestId: input.requestId,
89 });
90 }
91 // Dispatch validates the request while it is pending, then persists the
92 // resolved projection before this effect is executed.
93 if (request.status !== "resolved") {
94 return yield* new RuntimeRequestResponseExecutionError({
95 reason: "request-not-ready",
96 threadId: input.threadId,
97 providerSessionId: input.providerSessionId,
98 requestId: input.requestId,
99 });
100 }
101 if (
102 request.responseCapability.type !== "live" ||
103 request.responseCapability.providerSessionId !== input.providerSessionId
104 ) {
105 return yield* new RuntimeRequestResponseExecutionError({
106 reason: "request-not-resumable",
107 threadId: input.threadId,
108 providerSessionId: input.providerSessionId,
109 requestId: input.requestId,
110 });
111 }
112 const session = yield* sessions.get(input.providerSessionId);
113 if (Option.isNone(session)) {
114 return yield* new RuntimeRequestResponseExecutionError({
115 reason: "provider-session-not-active",
116 threadId: input.threadId,
117 providerSessionId: input.providerSessionId,
118 requestId: input.requestId,
119 });
120 }
121 yield* session.value.respondToRuntimeRequest({
122 requestId: input.requestId,
123 ...(input.decision === undefined ? {} : { decision: input.decision }),
124 ...(input.answers === undefined ? {} : { answers: input.answers }),
125 });
126 }).pipe(
127 Effect.mapError((cause) =>
128 isRuntimeRequestResponseExecutionError(cause)
129 ? cause
130 : new RuntimeRequestResponseExecutionError({
131 reason: "unexpected-failure",
132 threadId: input.threadId,
133 providerSessionId: input.providerSessionId,
134 requestId: input.requestId,
135 cause,
136 }),
137 ),
138 ),
139 });
140 }),
141);