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

threadRequests.ts

78 lines2.6 KB

1import type {
2 OrchestrationV2ThreadProjection,
3 ProviderRequestKind,
4 RuntimeRequestId,
5} from "@t3tools/contracts";
6import * as DateTime from "effect/DateTime";
7​
8export interface ThreadPendingApproval {
9 readonly requestId: RuntimeRequestId;
10 readonly requestKind: ProviderRequestKind;
11 readonly createdAt: string;
12 readonly detail?: string;
13 readonly responseCapability: "live" | "not_resumable";
15​
16export interface ThreadUserInputQuestion {
17 readonly id: string;
18 readonly header: string;
19 readonly question: string;
20 readonly options: ReadonlyArray<{
21 readonly label: string;
22 readonly description: string;
23 }>;
24 readonly multiSelect: boolean;
26​
27export interface ThreadPendingUserInput {
28 readonly requestId: RuntimeRequestId;
29 readonly createdAt: string;
30 readonly questions: ReadonlyArray<ThreadUserInputQuestion>;
31 readonly responseCapability: "live" | "not_resumable";
33​
34export interface PendingThreadRequests {
35 readonly approvals: ReadonlyArray<ThreadPendingApproval>;
36 readonly userInputs: ReadonlyArray<ThreadPendingUserInput>;
38​
39/** Joins pending request entities to the request items that carry display data. */
40export function derivePendingThreadRequests(
41 projection: OrchestrationV2ThreadProjection,
42): PendingThreadRequests {
43 const approvals: ThreadPendingApproval[] = [];
44 const userInputs: ThreadPendingUserInput[] = [];
45​
46 for (const request of projection.runtimeRequests) {
47 if (request.status !== "pending") continue;
48 const responseCapability = request.responseCapability.type;
49 if (request.kind === "user_input") {
50 const item = projection.turnItems.findLast(
51 (candidate) =>
52 candidate.type === "user_input_request" && candidate.requestId === request.id,
53 );
54 if (item === undefined || item.type !== "user_input_request") continue;
55 userInputs.push({
56 requestId: request.id,
57 createdAt: DateTime.formatIso(request.createdAt),
58 questions: item.questions.map((question) => ({ ...question, multiSelect: false })),
59 responseCapability,
60 });
61 continue;
62 }
63​
64 if (request.kind === "auth_refresh" || request.kind === "dynamic_tool_call") continue;
65 const item = projection.turnItems.findLast(
66 (candidate) => candidate.type === "approval_request" && candidate.requestId === request.id,
67 );
68 approvals.push({
69 requestId: request.id,
70 requestKind: request.kind,
71 createdAt: DateTime.formatIso(request.createdAt),
72 ...(item?.type === "approval_request" && item.prompt ? { detail: item.prompt } : {}),
73 responseCapability,
74 });
75 }
76​
77 return { approvals, userInputs };