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