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

use-selected-thread-requests.ts

210 lines6.9 KB

1import { derivePendingRequests } from "@t3tools/client-runtime/pending-requests";
2import { useAtomValue } from "@effect/atom-react";
3import { useCallback, useMemo, useState } from "react";
4​
5import {
6 ApprovalRequestId,
7 type ProviderApprovalDecision,
8 type UserInputQuestion,
9} from "@t3tools/contracts";
10import { Atom } from "effect/unstable/reactivity";
11​
12import { threadEnvironment } from "../state/threads";
13import { scopedRequestKey } from "../lib/scopedEntities";
14import {
15 buildPendingUserInputAnswers,
16 setPendingUserInputCustomAnswer,
17 togglePendingUserInputOptionSelection,
18 type PendingUserInputDraftAnswer,
19} from "../lib/threadActivity";
20import { appAtomRegistry } from "./atom-registry";
21import { useSelectedThreadDetail } from "./use-thread-detail";
22import { useThreadSelection } from "./use-thread-selection";
23import { useAtomCommand } from "./use-atom-command";
24​
25const userInputDraftsByRequestKeyAtom = Atom.make<
26 Record<string, Record<string, PendingUserInputDraftAnswer>>
27>({}).pipe(Atom.keepAlive, Atom.withLabel("mobile:user-input-drafts"));
28​
29function setUserInputDraftOption(
30 requestKey: string,
31 question: UserInputQuestion,
32 value: string,
33): void {
34 const current = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
35 appAtomRegistry.set(userInputDraftsByRequestKeyAtom, {
36 ...current,
37 [requestKey]: {
38 ...current[requestKey],
39 [question.id]: togglePendingUserInputOptionSelection(
40 question,
41 current[requestKey]?.[question.id],
42 value,
43 ),
44 },
45 });
47​
48function setUserInputDraftCustomAnswer(
49 requestKey: string,
50 question: UserInputQuestion,
51 customAnswer: string,
52): void {
53 const current = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
54 appAtomRegistry.set(userInputDraftsByRequestKeyAtom, {
55 ...current,
56 [requestKey]: {
57 ...current[requestKey],
58 [question.id]: setPendingUserInputCustomAnswer(
59 question,
60 current[requestKey]?.[question.id],
61 customAnswer,
62 ),
63 },
64 });
66​
67export function useSelectedThreadRequests() {
68 const respondToApproval = useAtomCommand(
69 threadEnvironment.respondToApproval,
70 "thread approval response",
71 );
72 const respondToUserInput = useAtomCommand(
73 threadEnvironment.respondToUserInput,
74 "thread user input response",
75 );
76 const dismissUserInput = useAtomCommand(
77 threadEnvironment.dismissUserInput,
78 "thread user input dismissal",
79 );
80 const { selectedThread: selectedThreadShell } = useThreadSelection();
81 const selectedThread = useSelectedThreadDetail();
82 const userInputDraftsByRequestKey = useAtomValue(userInputDraftsByRequestKeyAtom);
83 const [respondingApprovalId, setRespondingApprovalId] = useState<ApprovalRequestId | null>(null);
84 const [respondingUserInputId, setRespondingUserInputId] = useState<ApprovalRequestId | null>(
85 null,
86 );
87​
88 const { approvals: activePendingApprovals, userInputs: activePendingUserInputs } = useMemo(
89 () => derivePendingRequests(selectedThread?.activities ?? []),
90 [selectedThread?.activities],
91 );
92 const activePendingApproval = activePendingApprovals[0] ?? null;
93 const activePendingUserInput = activePendingUserInputs[0] ?? null;
94 const activePendingUserInputDrafts =
95 activePendingUserInput && selectedThreadShell
96 ? (userInputDraftsByRequestKey[
97 scopedRequestKey(selectedThreadShell.environmentId, activePendingUserInput.requestId)
98 ] ?? {})
99 : {};
100 const activePendingUserInputAnswers = activePendingUserInput
101 ? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingUserInputDrafts)
102 : null;
103​
104 const onSelectUserInputOption = useCallback(
105 (requestId: ApprovalRequestId, question: UserInputQuestion, value: string) => {
106 if (!selectedThreadShell) {
107 return;
108 }
109​
110 const requestKey = scopedRequestKey(selectedThreadShell.environmentId, requestId);
111 setUserInputDraftOption(requestKey, question, value);
112 },
113 [selectedThreadShell],
114 );
115​
116 const onChangeUserInputCustomAnswer = useCallback(
117 (requestId: ApprovalRequestId, questionId: string, customAnswer: string) => {
118 const question = activePendingUserInputs
119 .find((request) => request.requestId === requestId)
120 ?.questions.find((entry) => entry.id === questionId);
121 if (!selectedThreadShell || !question) {
122 return;
123 }
124​
125 const requestKey = scopedRequestKey(selectedThreadShell.environmentId, requestId);
126 setUserInputDraftCustomAnswer(requestKey, question, customAnswer);
127 },
128 [activePendingUserInputs, selectedThreadShell],
129 );
130​
131 const onRespondToApproval = useCallback(
132 async (requestId: ApprovalRequestId, decision: ProviderApprovalDecision) => {
133 if (!selectedThreadShell) {
134 return;
135 }
136​
137 setRespondingApprovalId(requestId);
138 const result = await respondToApproval({
139 environmentId: selectedThreadShell.environmentId,
140 input: {
141 threadId: selectedThreadShell.id,
142 requestId,
143 decision,
144 },
145 });
146 setRespondingApprovalId((current) => (current === requestId ? null : current));
147 return result;
148 },
149 [respondToApproval, selectedThreadShell],
150 );
151​
152 const onSubmitUserInput = useCallback(async () => {
153 if (!selectedThreadShell || !activePendingUserInput || !activePendingUserInputAnswers) {
154 return;
155 }
156​
157 setRespondingUserInputId(activePendingUserInput.requestId);
158 const result = await respondToUserInput({
159 environmentId: selectedThreadShell.environmentId,
160 input: {
161 threadId: selectedThreadShell.id,
162 requestId: activePendingUserInput.requestId,
163 answers: activePendingUserInputAnswers,
164 },
165 });
166 setRespondingUserInputId((current) =>
167 current === activePendingUserInput.requestId ? null : current,
168 );
169 return result;
170 }, [
171 activePendingUserInput,
172 activePendingUserInputAnswers,
173 respondToUserInput,
174 selectedThreadShell,
175 ]);
176​
177 // Closes an async question without messaging the agent.
178 const onDismissUserInput = useCallback(async () => {
179 if (!selectedThreadShell || !activePendingUserInput) {
180 return;
181 }
182​
183 setRespondingUserInputId(activePendingUserInput.requestId);
184 const result = await dismissUserInput({
185 environmentId: selectedThreadShell.environmentId,
186 input: {
187 threadId: selectedThreadShell.id,
188 requestId: activePendingUserInput.requestId,
189 },
190 });
191 setRespondingUserInputId((current) =>
192 current === activePendingUserInput.requestId ? null : current,
193 );
194 return result;
195 }, [activePendingUserInput, dismissUserInput, selectedThreadShell]);
196​
197 return {
198 activePendingApproval,
199 activePendingUserInput,
200 activePendingUserInputDrafts,
201 activePendingUserInputAnswers,
202 respondingApprovalId,
203 respondingUserInputId,
204 onRespondToApproval,
205 onSelectUserInputOption,
206 onChangeUserInputCustomAnswer,
207 onSubmitUserInput,
208 onDismissUserInput,
209 };