Skip to content
TypeScript

fix(web): deleted answer chips no longer return after picking an option (#19483)

Scott Norteman7ee6428aa8f5
diff --git a/apps/web/src/components/chat/usePendingThreadRequests.ts b/apps/web/src/components/chat/usePendingThreadRequests.ts
index 88e8f82dc0b..9ce6172a28d 100644
--- a/apps/web/src/components/chat/usePendingThreadRequests.ts
+++ b/apps/web/src/components/chat/usePendingThreadRequests.ts
@@ -37,6 +37,7 @@ import {
pendingUserInputRequestKey,
pruneResolvedUserInputDrafts,
setPendingUserInputCustomAnswer,
+ threadContextsReferencedByAnswer,
togglePendingUserInputOptionSelection,
type PendingUserInputDraftAnswer,
} from "../../pendingUserInput";
@@ -452,7 +453,10 @@ export function usePendingThreadRequests(input: {
);
store.addThreadContexts(
composerDraftTarget,
- store.getComposerDraft(questionTarget)?.threadContexts ?? [],
+ threadContextsReferencedByAnswer(
+ displacedAnswer ?? "",
+ store.getComposerDraft(questionTarget)?.threadContexts ?? [],
+ ),
{ appendReference: false },
);
}
diff --git a/apps/web/src/pendingUserInput.test.ts b/apps/web/src/pendingUserInput.test.ts
index d6d14b0c4ca..407b84be2af 100644
--- a/apps/web/src/pendingUserInput.test.ts
+++ b/apps/web/src/pendingUserInput.test.ts
@@ -12,6 +12,7 @@ import {
projectPendingUserInputAnswer,
resolvePendingUserInputAnswer,
setPendingUserInputCustomAnswer,
+ threadContextsReferencedByAnswer,
togglePendingUserInputOptionSelection,
} from "./pendingUserInput";
@@ -42,6 +43,22 @@ describe("projectPendingUserInputAnswer", () => {
});
});
+describe("threadContextsReferencedByAnswer", () => {
+ const kept = threadContextRecord(
+ { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("kept-thread") },
+ "Kept work",
+ );
+ const deleted = threadContextRecord(
+ { environmentId: EnvironmentId.make("env-answer"), threadId: ThreadId.make("deleted-thread") },
+ "Deleted work",
+ );
+
+ it("preserves remaining chips without restoring deleted thread records", () => {
+ const answer = `Compare ${formatInlineContextReference(threadContextReference(kept))}`;
+ expect(threadContextsReferencedByAnswer(answer, [deleted, kept])).toEqual([kept]);
+ });
+});
+
const singleSelectQuestion = {
id: "scope",
header: "Scope",
diff --git a/apps/web/src/pendingUserInput.ts b/apps/web/src/pendingUserInput.ts
index 2b37af07b3e..fb159426b5f 100644
--- a/apps/web/src/pendingUserInput.ts
+++ b/apps/web/src/pendingUserInput.ts
@@ -2,6 +2,8 @@ import type { ThreadContextRecord, UserInputQuestion } from "@t3tools/contracts"
import { replaceComposerContextReferences } from "@t3tools/shared/composerContextReferences";
import { formatThreadLink } from "@t3tools/shared/threadLinks";
+import { collectInlineContextIds } from "./lib/composerContextReferences";
+
export function projectPendingUserInputAnswer(
answer: unknown,
threadContexts: ReadonlyArray<ThreadContextRecord>,
@@ -14,6 +16,15 @@ export function projectPendingUserInputAnswer(
});
}
+// Question drafts retain deleted chip records for undo; only transfer referenced records.
+export function threadContextsReferencedByAnswer(
+ answer: string,
+ threadContexts: ReadonlyArray<ThreadContextRecord>,
+) {
+ const referencedIds = new Set(collectInlineContextIds(answer));
+ return threadContexts.filter((record) => referencedIds.has(record.contextId));
+}
+
export interface PendingUserInputDraftAnswer {
selectedOptionValues?: string[];
customAnswer?: string;