TypeScript
fix(web): reveal folded imported responses when opening citations (#18423)
Alex Southwell821189362209
diff --git a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsxindex 5c6e62d114f..a4451656d66 100644--- a/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.browser.test.tsx@@ -795,6 +795,62 @@ describe("MessagesTimeline attachments", () => {});describe("MessagesTimeline history and thread links", () => {+ it.each([+ ["imported runless", null],+ ["standard", RunId.make("citation-run")],+ ] as const)("reveals a cited response inside a folded %s turn", async (_label, runId) => {+ const quote = "The imported response contains this cited detail.";+ const props = {+ ...baseProps(),+ timelineEntries: [+ message("Investigate the issue.", { runId }, "citation-prompt"),+ message(quote, { role: "assistant", runId }, "citation-source"),+ message("Investigation complete.", { role: "assistant", runId }, "citation-answer"),+ ],+ };+ await renderElement(<MessagesTimeline {...props} />);+ const source = () =>+ container.querySelector<HTMLElement>('[data-assistant-citation-source="citation-source"]');+ await vi.waitFor(() => expect(text()).toContain("Investigation complete."));+ expect(buttons(/^Worked/)).toHaveLength(1);+ expect(source()).toBeNull();++ await act(async () =>+ roots.at(-1)!.render(+ <MessagesTimeline+ {...props}+ citationRequest={{+ key: "open-citation",+ citation: {+ version: 1,+ environmentId: ENVIRONMENT_ID,+ threadId: ThreadId.make(`thread-${threadCount}`),+ messageId: MessageId.make("citation-source"),+ text: quote,+ start: 0,+ end: quote.length,+ prefix: "",+ suffix: "",+ },+ }}+ />,+ ),+ );++ await vi.waitFor(() => {+ expect(source()?.textContent ?? "").toContain(quote);+ const highlight = CSS.highlights.get("t3-assistant-citation");+ expect(highlight).toBeDefined();+ const range = [...highlight!][0] as Range;+ expect(range.toString()).toBe(quote);+ const viewport = container.querySelector<HTMLElement>("[data-assistant-citation-viewport]")!;+ const bounds = range.getBoundingClientRect();+ expect(bounds.height).toBeGreaterThan(0);+ expect(bounds.top).toBeGreaterThanOrEqual(viewport.getBoundingClientRect().top);+ expect(bounds.bottom).toBeLessThanOrEqual(viewport.getBoundingClientRect().bottom);+ });+ });+it("loads earlier turns from a control ahead of the bounded timeline", async () => {const onLoadEarlier = vi.fn();await renderTimeline([message("Recent activity")], {diff --git a/apps/web/src/components/chat/MessagesTimeline.tsx b/apps/web/src/components/chat/MessagesTimeline.tsxindex 9133244f52d..e8551c8de65 100644--- a/apps/web/src/components/chat/MessagesTimeline.tsx+++ b/apps/web/src/components/chat/MessagesTimeline.tsx@@ -800,8 +800,7 @@ const ConversationTimeline = memo(function ConversationTimeline({});}, [latestRun, listIdentityKey]);- // Find the fold that hides the match: imported turns have no run id but still- // fold under a synthetic key, so the match's own run id cannot open them.+ // Imported turns fold under synthetic keys rather than the entries' own run ids.const findFoldRunIds = findActive? timelineTurnFoldRunIdsByEntryId({timelineEntries,@@ -811,9 +810,8 @@ const ConversationTimeline = memo(function ConversationTimeline({runlessWorkActive,}): null;- const activeFindRunId = activeFindMatch- ? findFoldRunIds?.get(activeFindMatch.entryId)- : undefined;+ const activeFindRunId =+ findActive && activeFindMatch ? findFoldRunIds?.get(activeFindMatch.entryId) : undefined;const [expandedFindRunId, setExpandedFindRunId] = useState<RunId | undefined>();if (expandedFindRunId !== activeFindRunId) {setExpandedFindRunId(activeFindRunId);@@ -1003,6 +1001,17 @@ const ConversationTimeline = memo(function ConversationTimeline({viewport: timelineViewportElement,historyLoading: citationHistoryLoading,loadEarlier,+ getTurnFoldRunId: (entryId) =>+ (+ findFoldRunIds ??+ timelineTurnFoldRunIdsByEntryId({+ timelineEntries,+ latestRun,+ runningRunId,+ isWorking,+ runlessWorkActive,+ })+ ).get(entryId),onExpandTurn: expandCitedRun,onManualNavigation,});diff --git a/apps/web/src/components/chat/useAssistantCitationTarget.ts b/apps/web/src/components/chat/useAssistantCitationTarget.tsindex d8bfa53c953..106ac958ec1 100644--- a/apps/web/src/components/chat/useAssistantCitationTarget.ts+++ b/apps/web/src/components/chat/useAssistantCitationTarget.ts@@ -20,6 +20,7 @@ export function useAssistantCitationTarget({viewport,historyLoading,loadEarlier,+ getTurnFoldRunId,onExpandTurn,onManualNavigation,}: {@@ -30,6 +31,7 @@ export function useAssistantCitationTarget({viewport: HTMLElement | null;historyLoading: boolean;loadEarlier: CitationHistoryPage | null;+ getTurnFoldRunId: (entryId: string) => RunId | undefined;onExpandTurn: (runId: RunId) => void;onManualNavigation: () => void;}) {@@ -123,12 +125,14 @@ export function useAssistantCitationTarget({(row) => row.kind === "message" && row.message.id === navigation.target.citation.messageId,);if (index < 0) {- if (source.message.runId) onExpandTurn(source.message.runId);+ const foldRunId = getTurnFoldRunId(source.id);+ if (foldRunId) onExpandTurn(foldRunId);return;}if (listLoaded && listRef.current) setReady(navigation.target);}, [entries,+ getTurnFoldRunId,historyLoading,listLoaded,listRef,