Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion apps/web/src/components/chat/usePendingThreadRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
pendingUserInputRequestKey,
pruneResolvedUserInputDrafts,
setPendingUserInputCustomAnswer,
threadContextsReferencedByAnswer,
togglePendingUserInputOptionSelection,
type PendingUserInputDraftAnswer,
} from "../../pendingUserInput";
Expand Down Expand Up @@ -452,7 +453,10 @@ export function usePendingThreadRequests(input: {
);
store.addThreadContexts(
composerDraftTarget,
store.getComposerDraft(questionTarget)?.threadContexts ?? [],
threadContextsReferencedByAnswer(
displacedAnswer ?? "",
store.getComposerDraft(questionTarget)?.threadContexts ?? [],
),
{ appendReference: false },
);
}
Expand Down
17 changes: 17 additions & 0 deletions apps/web/src/pendingUserInput.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
projectPendingUserInputAnswer,
resolvePendingUserInputAnswer,
setPendingUserInputCustomAnswer,
threadContextsReferencedByAnswer,
togglePendingUserInputOptionSelection,
} from "./pendingUserInput";

Expand Down Expand Up @@ -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",
Expand Down
11 changes: 11 additions & 0 deletions apps/web/src/pendingUserInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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;
Expand Down
Loading