Skip to content

Commit 7f36ae3

Browse files
feat(ai-conversations): Add search to the conversation view (#131358)
Adds a search bar to the conversation transcript. - Searches the text the transcript shows, including collapsed blocks, and loads more pages (up to the 10,000-span limit) until it finds a match. - Marks every match, scrolls to the current one, opens the blocks around it and selects its span. - k/n counter with previous/next buttons, Enter and Shift+Enter, wrapping like browser find. A + shows while more pages could hold matches. - Fires `conversations.detail.span-limit-reached` when the span limit stops a search. Fixes AGT-3329 --------- Co-authored-by: getsantry[bot] <66042841+getsantry[bot]@users.noreply.github.com>
1 parent 212a54d commit 7f36ae3

9 files changed

Lines changed: 633 additions & 37 deletions

File tree

‎static/app/utils/analytics/conversationsAnalyticsEvents.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export type ConversationsEventParameters = {
1313
'conversations.detail.export-conversation': Record<string, unknown>;
1414
'conversations.detail.page-view': Record<string, unknown>;
1515
'conversations.detail.select-span': Record<string, unknown>;
16+
'conversations.detail.span-limit-reached': Record<string, unknown>;
1617
'conversations.detail.tab-switch': {fromTab: string; toTab: string};
1718
'conversations.message.click': Record<string, unknown>;
1819
'conversations.message.click-evaluation': Record<string, unknown>;
@@ -65,6 +66,7 @@ export const conversationsEventMap: Record<keyof ConversationsEventParameters, s
6566
'conversations.detail.page-view': 'Conversations: Detail Page View',
6667
'conversations.detail.tab-switch': 'Conversations: Detail Tab Switch',
6768
'conversations.detail.select-span': 'Conversations: Detail Select Span',
69+
'conversations.detail.span-limit-reached': 'Conversations: Detail Span Limit Reached',
6870
'conversations.detail.copy-conversation': 'Conversations: Detail Copy Conversation',
6971
'conversations.detail.copy-conversation-id':
7072
'Conversations: Detail Copy Conversation ID',

‎static/app/views/explore/conversations/components/conversationView.spec.tsx‎

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,21 @@
11
import {OrganizationFixture} from 'sentry-fixture/organization';
22

3-
import {act, render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
3+
import {
4+
act,
5+
cleanup,
6+
render,
7+
screen,
8+
userEvent,
9+
waitFor,
10+
} from 'sentry-test/reactTestingLibrary';
411

512
import {PageFiltersStore} from 'sentry/components/pageFilters/store';
13+
import {trackAnalytics} from 'sentry/utils/analytics';
614

715
import {ConversationViewContent} from './conversationView';
816

17+
jest.mock('sentry/utils/analytics');
18+
919
const CONVERSATION_ID = 'conv-1';
1020
const originalIntersectionObserver = window.IntersectionObserver;
1121
let triggerIntersection: ((isIntersecting: boolean) => void) | undefined;
@@ -258,6 +268,113 @@ describe('ConversationViewContent', () => {
258268
expect(nextRequest).toHaveBeenCalledTimes(1);
259269
});
260270

271+
it('searches the transcript and selects the span with the match', async () => {
272+
const onSelectSpan = jest.fn();
273+
renderView({onSelectSpan});
274+
await screen.findByText('Second answer');
275+
276+
await userEvent.type(
277+
screen.getByRole('textbox', {name: 'Search in transcript'}),
278+
'second answer{Enter}'
279+
);
280+
281+
expect(await screen.findByText('1/1')).toBeInTheDocument();
282+
expect(onSelectSpan).toHaveBeenCalledWith('span-b');
283+
284+
await userEvent.clear(screen.getByRole('textbox', {name: 'Search in transcript'}));
285+
expect(screen.queryByText('1/1')).not.toBeInTheDocument();
286+
});
287+
288+
it('loads later pages until the search finds a match', async () => {
289+
MockApiClient.clearMockResponses();
290+
mockConversation([CONVERSATION_BODY[0]!], {nextCursor: 'next'});
291+
const nextRequest = mockConversation([CONVERSATION_BODY[1]!], {cursor: 'next'});
292+
const onSelectSpan = jest.fn();
293+
renderView({onSelectSpan});
294+
await screen.findByText('First answer');
295+
296+
await userEvent.type(
297+
screen.getByRole('textbox', {name: 'Search in transcript'}),
298+
'second answer{Enter}'
299+
);
300+
301+
expect(await screen.findByText('1/1')).toBeInTheDocument();
302+
expect(nextRequest).toHaveBeenCalledTimes(1);
303+
expect(onSelectSpan).toHaveBeenCalledWith('span-b');
304+
});
305+
306+
it('shows no results once every page is searched', async () => {
307+
MockApiClient.clearMockResponses();
308+
mockConversation([CONVERSATION_BODY[0]!], {nextCursor: 'next'});
309+
const nextRequest = mockConversation([CONVERSATION_BODY[1]!], {cursor: 'next'});
310+
const onSelectSpan = jest.fn();
311+
renderView({onSelectSpan});
312+
await screen.findByText('First answer');
313+
314+
await userEvent.type(
315+
screen.getByRole('textbox', {name: 'Search in transcript'}),
316+
'not in this conversation{Enter}'
317+
);
318+
319+
expect(await screen.findByText('No results')).toBeInTheDocument();
320+
expect(nextRequest).toHaveBeenCalledTimes(1);
321+
expect(onSelectSpan).not.toHaveBeenCalled();
322+
expect(trackAnalytics).not.toHaveBeenCalledWith(
323+
'conversations.detail.span-limit-reached',
324+
expect.anything()
325+
);
326+
});
327+
328+
describe('at the span limit', () => {
329+
afterEach(() => {
330+
try {
331+
cleanup();
332+
jest.runOnlyPendingTimers();
333+
} finally {
334+
jest.useRealTimers();
335+
}
336+
});
337+
338+
it('reports when the span limit stops a search', async () => {
339+
MockApiClient.clearMockResponses();
340+
mockConversation([CONVERSATION_BODY[0]!], {nextCursor: 'next'});
341+
let page = 1;
342+
const nextRequest = MockApiClient.addMockResponse({
343+
url: `/organizations/org-slug/agents/conversations/${CONVERSATION_ID}/`,
344+
match: [MockApiClient.matchQuery({cursor: 'next'})],
345+
body: () => {
346+
page++;
347+
return {
348+
conversationId: CONVERSATION_ID,
349+
title: null,
350+
spans: [{...CONVERSATION_BODY[1]!, span_id: `span-${page}`}],
351+
};
352+
},
353+
headers: {
354+
Link: `</organizations/org-slug/agents/conversations/${CONVERSATION_ID}/?cursor=next>; rel="next"; results="true"; cursor="next"`,
355+
},
356+
});
357+
jest.useFakeTimers();
358+
renderView({onSelectSpan: jest.fn()});
359+
await screen.findByText('First answer');
360+
361+
await userEvent.type(
362+
screen.getByRole('textbox', {name: 'Search in transcript'}),
363+
'not in this conversation{Enter}',
364+
{delay: null}
365+
);
366+
await act(() => jest.runAllTimersAsync());
367+
368+
expect(screen.getByText('No results')).toBeInTheDocument();
369+
expect(nextRequest).toHaveBeenCalledTimes(99);
370+
expect(trackAnalytics).toHaveBeenCalledTimes(1);
371+
expect(trackAnalytics).toHaveBeenCalledWith(
372+
'conversations.detail.span-limit-reached',
373+
expect.objectContaining({organization: expect.anything()})
374+
);
375+
});
376+
});
377+
261378
it('does not show the timeline default while a deep link is unresolved', async () => {
262379
MockApiClient.clearMockResponses();
263380
mockConversation([CONVERSATION_BODY[0]!], {nextCursor: 'next'});

‎static/app/views/explore/conversations/components/conversationView.tsx‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
ConversationSpanDetail,
2828
} from './conversationSpanDetail';
2929
import {MessagesPanel} from './messagesPanel';
30+
import {TranscriptSearch} from './transcriptSearch';
3031

3132
export type ConversationViewTab = 'transcript' | 'timeline';
3233

@@ -64,6 +65,7 @@ export function ConversationViewContent({
6465
nodeTraceMap,
6566
canAutoFetchNextPage,
6667
hasNextPage,
68+
hasReachedPageLimit,
6769
isLoading,
6870
isFetchingNextPage,
6971
error,
@@ -111,10 +113,11 @@ export function ConversationViewContent({
111113
// off the sticky (URL) selection, not `displayedNode`: the timeline's
112114
// view-local default span is not a real selection, so entering the timeline
113115
// restores its saved offset rather than snapping to that default.
114-
const contentRef = useConversationScrollRestoration({
115-
activeTab,
116-
selectedNodeId: selectedNode?.id ?? null,
117-
});
116+
const {scrollContainerRef: contentRef, centerOnSelect} =
117+
useConversationScrollRestoration({
118+
activeTab,
119+
selectedNodeId: selectedNode?.id ?? null,
120+
});
118121

119122
const loadMoreRef = useRef<HTMLDivElement>(null);
120123

@@ -187,6 +190,20 @@ export function ConversationViewContent({
187190
leftPadding={isTranscript ? '0' : 'md'}
188191
left={
189192
<Fragment>
193+
{isTranscript && onSelectSpan && (
194+
<TranscriptSearch
195+
key={conversation.conversationId}
196+
rootRef={contentRef}
197+
canLoadMore={canAutoFetchNextPage}
198+
hasReachedPageLimit={hasReachedPageLimit}
199+
isLoadingMore={isLoading || isFetchingNextPage}
200+
loadMore={loadNextPage}
201+
onSelectMatch={(spanId, element) => {
202+
centerOnSelect(spanId, element);
203+
onSelectSpan(spanId);
204+
}}
205+
/>
206+
)}
190207
{isTranscript ? (
191208
<MessagesPanel
192209
isLoading={isLoading}

‎static/app/views/explore/conversations/components/messagesPanel.tsx‎

Lines changed: 58 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Fragment, memo, useCallback, useMemo} from 'react';
1+
import {memo, useCallback, useMemo} from 'react';
22
import {useTheme} from '@emotion/react';
33
import styled from '@emotion/styled';
44

@@ -192,6 +192,7 @@ function TranscriptMessages({
192192
return (
193193
<UserTurn
194194
key={message.id}
195+
nodeId={message.nodeId}
195196
content={message.content}
196197
hasXmlTags={hasXmlTags}
197198
/>
@@ -246,7 +247,12 @@ function TranscriptMessages({
246247

247248
if (message.subAgent) {
248249
return (
249-
<MessageRow key={message.id} from="assistant" density="line">
250+
<MessageRow
251+
key={message.id}
252+
from="assistant"
253+
density="line"
254+
data-node-id={message.nodeId}
255+
>
250256
<SubAgentRow
251257
call={message.subAgent}
252258
nodeMap={nodeMap}
@@ -299,26 +305,29 @@ const NestedMessageList = styled(Stack)`
299305
const UserTurn = memo(function UserTurnImpl({
300306
content,
301307
hasXmlTags,
308+
nodeId,
302309
}: {
303310
content: string;
304311
hasXmlTags: boolean;
312+
nodeId: string;
305313
}) {
306-
if (content === NOT_REPORTED) {
307-
return (
308-
<UserMessageBlock>
309-
<MessageText align="left" variant="muted">
310-
{content}
311-
</MessageText>
312-
</UserMessageBlock>
313-
);
314-
}
315-
316314
return (
317-
<UserMessageBlock expand={hasXmlTags}>
318-
<MessageText align="left">
319-
<AIContentRenderer text={content} inline autoCollapseLimit={10} />
320-
</MessageText>
321-
</UserMessageBlock>
315+
// Search finds the span a transcript block renders by its data-node-id.
316+
<Container display="contents" data-node-id={nodeId}>
317+
{content === NOT_REPORTED ? (
318+
<UserMessageBlock>
319+
<MessageText align="left" variant="muted">
320+
{content}
321+
</MessageText>
322+
</UserMessageBlock>
323+
) : (
324+
<UserMessageBlock expand={hasXmlTags}>
325+
<MessageText align="left">
326+
<AIContentRenderer text={content} inline autoCollapseLimit={10} />
327+
</MessageText>
328+
</UserMessageBlock>
329+
)}
330+
</Container>
322331
);
323332
});
324333

@@ -350,7 +359,13 @@ const AssistantTurn = memo(function AssistantTurnImpl({
350359
renderMessages,
351360
}: AssistantTurnProps) {
352361
const organization = useOrganization();
362+
const theme = useTheme();
353363
const generationNode = nodeMap.get(message.nodeId);
364+
// Without a bubble, the turn's first row shows the selection instead.
365+
const isRowSelected = isSelected && message.content === '';
366+
const selectedRowStyle = isRowSelected
367+
? {outline: `2px solid ${theme.tokens.focus.default}`, outlineOffset: '-2px'}
368+
: undefined;
354369
// Spans often report `gen_ai.cost.total_tokens` as 0 when the API omits cost;
355370
// treat that as absent so we don't show `<$0.01`, matching the timeline.
356371
const cost = generationNode
@@ -368,7 +383,7 @@ const AssistantTurn = memo(function AssistantTurnImpl({
368383
};
369384

370385
return (
371-
<Fragment>
386+
<Container display="contents" data-node-id={message.nodeId}>
372387
{message.toolCalls && message.toolCalls.length > 0 && (
373388
<MessageRow from="assistant" density="line">
374389
<MessageToolCalls
@@ -382,12 +397,19 @@ const AssistantTurn = memo(function AssistantTurnImpl({
382397
)}
383398
{message.reasoning && (
384399
<MessageRow from="assistant" density="line">
385-
{/* A reasoning-only turn (no assistant bubble to hang meta on) surfaces
386-
* the turn's cost/duration in the thinking row's own meta column. */}
387-
<ReasoningSection
388-
reasoning={message.reasoning}
389-
meta={message.content === '' && hasMeta ? meta : undefined}
390-
/>
400+
<Container
401+
width="100%"
402+
radius="xs"
403+
data-selected={isRowSelected}
404+
style={selectedRowStyle}
405+
>
406+
{/* A reasoning-only turn (no assistant bubble to hang meta on) surfaces
407+
* the turn's cost/duration in the thinking row's own meta column. */}
408+
<ReasoningSection
409+
reasoning={message.reasoning}
410+
meta={message.content === '' && hasMeta ? meta : undefined}
411+
/>
412+
</Container>
391413
</MessageRow>
392414
)}
393415
{message.content === '' ? (
@@ -398,7 +420,13 @@ const AssistantTurn = memo(function AssistantTurnImpl({
398420
hasMeta &&
399421
!message.reasoning && (
400422
<MessageRow from="assistant" density="line">
401-
<Flex justify="end" width="100%">
423+
<Flex
424+
justify="end"
425+
width="100%"
426+
radius="xs"
427+
data-selected={isRowSelected}
428+
style={selectedRowStyle}
429+
>
402430
{meta}
403431
</Flex>
404432
</MessageRow>
@@ -421,7 +449,7 @@ const AssistantTurn = memo(function AssistantTurnImpl({
421449
</MessageText>
422450
</AssistantMessageBlock>
423451
)}
424-
</Fragment>
452+
</Container>
425453
);
426454
});
427455

@@ -457,7 +485,7 @@ const EmbeddingTurn = memo(function EmbeddingTurnImpl({
457485
const [isOpen, setIsOpen] = useSelectionOpenState(isSelected ? message.nodeId : null);
458486

459487
return (
460-
<MessageRow from="assistant" density="line">
488+
<MessageRow from="assistant" density="line" data-node-id={message.nodeId}>
461489
<CollapsibleChatRow
462490
open={isOpen}
463491
title={
@@ -538,7 +566,7 @@ const EvaluationTurn = memo(function EvaluationTurnImpl({
538566
};
539567

540568
return (
541-
<MessageRow from="assistant" density="line">
569+
<MessageRow from="assistant" density="line" data-node-id={message.nodeId}>
542570
<TranscriptSpanRow
543571
node={node}
544572
isSelected={isSelected}
@@ -584,7 +612,7 @@ const ExtractionTurn = memo(function ExtractionTurnImpl({
584612
};
585613

586614
return (
587-
<MessageRow from="assistant" density="line">
615+
<MessageRow from="assistant" density="line" data-node-id={message.nodeId}>
588616
<TranscriptSpanRow
589617
node={node}
590618
isSelected={isSelected}
@@ -624,7 +652,7 @@ const MemoryTurn = memo(function MemoryTurnImpl({
624652
};
625653

626654
return (
627-
<MessageRow from="assistant" density="line">
655+
<MessageRow from="assistant" density="line" data-node-id={message.nodeId}>
628656
<TranscriptSpanRow
629657
node={node}
630658
isSelected={isSelected}

0 commit comments

Comments
 (0)