fix(web): keep composer draft when stopping during a pending approval #9117
Closedlnieuwenhuis(GitHub profile, opens in a new tab)wants to merge 16 commits into
main fromrefs/pull/9117/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/web/src/components/ChatView.logic.ts line 288OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -247,0 +268,21 @@268+ currentDraftTarget: TDraftTarget;269+ answerForPrevious: string | null | undefined;270+ wasSubmitted: boolean;271+}): string | null {272+ const { previous, nextRequestId, currentDraftTarget, answerForPrevious, wasSubmitted } = input;273+ if (!previous) {274+ return null;275+ }276+ if (previous.requestId === nextRequestId) {277+ return null;278+ }279+ if (wasSubmitted) {280+ return null;281+ }282+ if (previous.draftTarget !== currentDraftTarget) {283+ return null;284+ }285+ if (!answerForPrevious || answerForPrevious.trim().length === 0) {286+ return null;287+ }288+ return answerForPrevious;apps/web/src/components/ChatView.logic.ts line 279OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -247,0 +259,21 @@259+ * teardown), which would silently discard the text (issue #8963). Returns the260+ * text to rescue into the composer draft, or null when the request did not261+ * change, the answer was submitted, the thread on screen has changed (an262+ * off-screen cancellation is left alone rather than risk the wrong draft), or263+ * there is nothing non-blank to keep.264+ */265+export function resolveCancelledPendingUserInputSnapshot<TDraftTarget>(input: {266+ previous: PendingUserInputRequestSnapshot<TDraftTarget> | null;267+ nextRequestId: string | null;268+ currentDraftTarget: TDraftTarget;269+ answerForPrevious: string | null | undefined;270+ wasSubmitted: boolean;271+}): string | null {272+ const { previous, nextRequestId, currentDraftTarget, answerForPrevious, wasSubmitted } = input;273+ if (!previous) {274+ return null;275+ }276+ if (previous.requestId === nextRequestId) {277+ return null;278+ }279+ if (wasSubmitted) {
- apps/web/src/components/ChatView.tsx line 2455OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -2421,0 +2435,21 @@2435+ // Rescues a pending question's typed custom answer into the composer draft2436+ // when the question disappears without being answered (issue #8963).2437+ // `prevPendingUserInputRef` is written only here, so "previous" is always2438+ // the state before this transition.2439+ useEffect(() => {2440+ const nextRequestId = activePendingUserInput?.requestId ?? null;2441+ const previous = prevPendingUserInputRef.current;2442+ if (previous && previous.requestId !== nextRequestId) {2443+ const wasSubmitted = submittedPendingUserInputRequestIdsRef.current.has(previous.requestId);2444+ submittedPendingUserInputRequestIdsRef.current.delete(previous.requestId);2445+ // Answers by request id are never pruned, so the outgoing request's2446+ // typed text is still readable here.2447+ let answerForPrevious: string | null = null;2448+ for (const draft of Object.values(2449+ pendingUserInputAnswersByRequestId[previous.requestId] ?? {},2450+ )) {2451+ if (draft.customAnswer && draft.customAnswer.trim().length > 0) {2452+ answerForPrevious = draft.customAnswer;2453+ break;2454+ }2455+ }
- apps/web/src/components/ChatView.tsx line 2465OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2421,0 +2445,21 @@2445+ );2446+ if (text === null) return;2447+ const existingDraftPrompt =2448+ useComposerDraftStore.getState().getComposerDraft(draftTarget)?.prompt ?? "";2449+ const nextDraftPrompt = mergeComposerDraftPromptWithPendingAnswer(existingDraftPrompt, text);2450+ if (nextDraftPrompt !== null) {2451+ setComposerDraftPrompt(draftTarget, nextDraftPrompt);2452+ }2453+ },2454+ [pendingUserInputAnswersByRequestId, setComposerDraftPrompt],2455+ );2456+ // Rescues a pending question's typed custom answers into the composer draft2457+ // when the question disappears without being answered (issue #8963).2458+ // `prevPendingUserInputRef` is written only here, so "previous" is always2459+ // the state before this transition.2460+ useEffect(() => {2461+ const nextRequestId = activePendingUserInput?.requestId ?? null;2462+ const previous = prevPendingUserInputRef.current;2463+ if (previous && previous.requestId !== nextRequestId) {2464+ const wasSubmitted = submittedPendingUserInputRequestIdsRef.current.has(previous.requestId);2465+ submittedPendingUserInputRequestIdsRef.current.delete(previous.requestId);
Mnigos(GitHub profile, opens in a new tab) requested changes
- apps/web/src/components/ChatView.tsx line 6566OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -6415,3 +6548,19 @@64156548 setRespondingUserInputRequestIds((existing) =>64166549 existing.includes(requestId) ? existing : [...existing, requestId],64176550 );6551+ // Marked before the round trip: the resolved activity can arrive over the6552+ // socket before this RPC settles, and the rescue effect must already know6553+ // the question was answered rather than cancelled. Unmarked on failure so6554+ // a later real Stop of the still-open question can rescue the text.6555+ submittedPendingUserInputRequestIdsRef.current.add(requestId);6556+ // The answer is what gets sent, so the persisted copy of text carried in6557+ // from the draft goes now, before the resolve can bring the draft back6558+ // on screen.6559+ const carried = carriedComposerDraftByRequestIdRef.current.get(requestId);6560+ if (carried) {6561+ carriedComposerDraftByRequestIdRef.current.delete(requestId);6562+ const draftPrompt =6563+ useComposerDraftStore.getState().getComposerDraft(carried.draftTarget)?.prompt ?? "";6564+ if (draftPrompt === carried.text) {6565+ setComposerDraftPrompt(carried.draftTarget, "");6566+ }
Mnigos(GitHub profile, opens in a new tab) approved these changes
- apps/web/src/components/ChatView.tsx line 2606OutdatedResolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -2563,3 +2587,20 @@25632587 : null,25642588 [activePendingDraftAnswers, activePendingQuestionIndex, activePendingUserInput],25652589 );2590+ // Text typed in the composer is never dropped: it is sent, or it stays in2591+ // the draft. Appends text that is leaving a request's answer slot unsent to2592+ // the thread's composer draft, replacing the persisted copy of text that2593+ // was carried in from the draft.2594+ const returnTextToComposerDraft = useCallback(2595+ (requestId: string, text: string, draftTarget: ScopedThreadRef | DraftId) => {2596+ const carried = carriedComposerDraftByRequestIdRef.current.get(requestId);2597+ carriedComposerDraftByRequestIdRef.current.delete(requestId);2598+ const draftPrompt =2599+ useComposerDraftStore.getState().getComposerDraft(draftTarget)?.prompt ?? "";2600+ const existingDraftPrompt =2601+ carried?.draftTarget === draftTarget && draftPrompt === carried.text ? "" : draftPrompt;2602+ const nextDraftPrompt =2603+ mergeComposerDraftPromptWithPendingAnswer(existingDraftPrompt, text) ?? existingDraftPrompt;2604+ if (nextDraftPrompt !== draftPrompt) {2605+ setComposerDraftPrompt(draftTarget, nextDraftPrompt);2606+ }
- apps/web/src/components/ChatView.tsx lines 7382 to 7394OutdatedResolvedby lnieuwenhuis(GitHub profile, opens in a new tab)@@ -7225,3 +7375,20 @@72257375 setRespondingUserInputRequestIds((existing) =>72267376 existing.includes(requestId) ? existing : [...existing, requestId],72277377 );7378+ // Marked before the round trip: the resolved activity can arrive over the7379+ // socket before this RPC settles, and the rescue effect must already know7380+ // the question was answered rather than cancelled. Unmarked on failure so7381+ // a later real Stop of the still-open question can rescue the text.7382+ submittedPendingUserInputRequestIdsRef.current.add(responseKey);7383+ // The answer is what gets sent, so the persisted copy of text carried in7384+ // from the draft goes now, before the resolve can bring the draft back7385+ // on screen.7386+ const carried = carriedComposerDraftByRequestIdRef.current.get(responseKey);7387+ if (carried) {7388+ carriedComposerDraftByRequestIdRef.current.delete(responseKey);7389+ const draftPrompt =7390+ useComposerDraftStore.getState().getComposerDraft(carried.draftTarget)?.prompt ?? "";7391+ if (draftPrompt === carried.text) {7392+ setComposerDraftPrompt(carried.draftTarget, "");7393+ }7394+ }
juliusmarminge closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed