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

RuntimeRequestService.ts

143 lines5.6 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 * as ProjectionStore from "./ProjectionStore.ts";
15import * as ProviderSessionManager from "./ProviderSessionManager.ts";
16​
17export class RuntimeRequestResponseExecutionError extends Schema.TaggedError<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 feedback?: string;
58 readonly answers?: ProviderUserInputAnswers;
59 }) => Effect.Effect<void, RuntimeRequestResponseExecutionError>;
61​
62export class RuntimeRequestServiceV2 extends Context.Service<
63 RuntimeRequestServiceV2,
64 RuntimeRequestServiceV2Shape
65>()("t3/orchestration-v2/RuntimeRequestService/RuntimeRequestServiceV2") {}
66​
67export const layer: Layer.Layer<
68 RuntimeRequestServiceV2,
69 never,
70 ProjectionStore.ProjectionStoreV2 | ProviderSessionManager.ProviderSessionManagerV2
71> = Layer.effect(
72 RuntimeRequestServiceV2,
73 Effect.gen(function* () {
74 const projections = yield* ProjectionStore.ProjectionStoreV2;
75 const sessions = yield* ProviderSessionManager.ProviderSessionManagerV2;
76​
77 return RuntimeRequestServiceV2.of({
78 respond: Effect.fn("RuntimeRequestService.respond")(
79 function* (input) {
80 const request = yield* projections.getRuntimeRequest(input.threadId, input.requestId);
81 if (request === undefined) {
82 return yield* new RuntimeRequestResponseExecutionError({
83 reason: "request-missing",
84 threadId: input.threadId,
85 providerSessionId: input.providerSessionId,
86 requestId: input.requestId,
87 });
88 }
89 // Dispatch validates the request while it is pending, then persists the
90 // resolved projection before this effect is executed.
91 if (request.status !== "resolved") {
92 return yield* new RuntimeRequestResponseExecutionError({
93 reason: "request-not-ready",
94 threadId: input.threadId,
95 providerSessionId: input.providerSessionId,
96 requestId: input.requestId,
97 });
98 }
99 if (
100 request.responseCapability.type !== "live" ||
101 request.responseCapability.providerSessionId !== input.providerSessionId
102 ) {
103 return yield* new RuntimeRequestResponseExecutionError({
104 reason: "request-not-resumable",
105 threadId: input.threadId,
106 providerSessionId: input.providerSessionId,
107 requestId: input.requestId,
108 });
109 }
110 const session = yield* sessions.get(input.providerSessionId);
111 if (Option.isNone(session)) {
112 return yield* new RuntimeRequestResponseExecutionError({
113 reason: "provider-session-not-active",
114 threadId: input.threadId,
115 providerSessionId: input.providerSessionId,
116 requestId: input.requestId,
117 });
118 }
119 yield* session.value.respondToRuntimeRequest({
120 requestId: input.requestId,
121 ...(input.decision === undefined ? {} : { decision: input.decision }),
122 ...(input.feedback === undefined ? {} : { feedback: input.feedback }),
123 ...(input.answers === undefined ? {} : { answers: input.answers }),
124 });
125 },
126 (effect, input) =>
127 effect.pipe(
128 Effect.mapError((cause) =>
129 isRuntimeRequestResponseExecutionError(cause)
130 ? cause
131 : new RuntimeRequestResponseExecutionError({
132 reason: "unexpected-failure",
133 threadId: input.threadId,
134 providerSessionId: input.providerSessionId,
135 requestId: input.requestId,
136 cause,
137 }),
138 ),
139 ),
140 ),
141 });
142 }),
143);