From 2ee40b0e14833af978e05c1a4d848d10990dea97 Mon Sep 17 00:00:00 2001 From: Michael McQuade Date: Tue, 17 Nov 2026 11:32:02 -0600 Subject: [PATCH] perf(ui): find in a long transcript opens at once instead of drawing the whole conversation, and holds a third less memory on a phone Find drew every message of the conversation while it was open, through the message list's `whole`, so it could search all of it. On a long agent session that froze the page, and on a phone it took more memory than the tab had. Find now searches the whole conversation without drawing it: - The list reads the messages it doesn't draw a few at a time, out of sight and between frames, as they would show, and keeps their text. It reads only while find is open, and keeps the text of a turn it lets go of as the reader left it. - Find counts the matches in the text it has read along with those in the turns drawn. Until it has read them all, the count says more may come. - Stepping to a match in a turn the list doesn't draw draws that turn and shows the match, as stepping to one on screen does. - A match no longer runs from one message into the next. `whole` still draws every message for a host that asks for it; `SessionView` no longer does. --- .../ai/components/ChatMessageList.stories.tsx | 2 +- .../ui/src/ai/components/ChatMessageList.tsx | 206 ++++++++++--- .../ChatMessageList.window.test.tsx | 86 +++++- .../ai/components/SessionView.find.test.tsx | 63 +++- .../src/ai/components/SessionView.stories.tsx | 54 +++- .../ui/src/ai/components/SessionView.test.tsx | 2 +- packages/ui/src/ai/components/SessionView.tsx | 15 +- .../ui/src/ai/components/TranscriptFind.tsx | 279 ++++++++++++++---- packages/ui/src/ai/core/textIndex.ts | 41 +++ packages/ui/src/ai/core/transcriptFind.ts | 37 ++- 10 files changed, 670 insertions(+), 115 deletions(-) create mode 100644 packages/ui/src/ai/core/textIndex.ts diff --git a/packages/ui/src/ai/components/ChatMessageList.stories.tsx b/packages/ui/src/ai/components/ChatMessageList.stories.tsx index ca866824..09a81fac 100644 --- a/packages/ui/src/ai/components/ChatMessageList.stories.tsx +++ b/packages/ui/src/ai/components/ChatMessageList.stories.tsx @@ -259,7 +259,7 @@ const agentSession: ChatMessage[] = Array.from({ length: 40 }, (_, n) => [ // Forty turns of sixty tool calls each: the list draws the last few turns, // then the turns near the view as you scroll, and lets go of those a few -// screens behind. Whole draws every turn, as a search of the conversation does. +// screens behind. Whole draws every turn at once. export const LongAgentSession: StoryObj<{ whole: boolean }> = { args: { whole: false }, render: ({ whole }) => ( diff --git a/packages/ui/src/ai/components/ChatMessageList.tsx b/packages/ui/src/ai/components/ChatMessageList.tsx index fbacdd54..3ac2029d 100644 --- a/packages/ui/src/ai/components/ChatMessageList.tsx +++ b/packages/ui/src/ai/components/ChatMessageList.tsx @@ -19,6 +19,8 @@ import { CheckIcon, ClockIcon } from '../../icons' import { useChat, useChatStream } from '../core/ChatProvider' import { type ApprovalAnswerHandler, useChatConfig } from '../core/config' import { createRowMemory, RowMemoryContext, useExpandAll } from '../core/rowMemory' +import type { TextIndex } from '../core/textIndex' +import { rowOf, visibleText } from '../core/transcriptFind' import type { ChatMessage as Message } from '../types' import Markdown from '../ui/Markdown' import AgentMessageParts from './agent/AgentMessageParts' @@ -86,13 +88,6 @@ function turnEnd(messages: Message[], i: number): number { return at } -/** The row a node is in, of those under content. */ -function rowOf(node: Node | null | undefined, content: Element): HTMLElement | null { - const el = node instanceof Element ? node : (node?.parentElement ?? null) - const row = el?.closest('[data-row]') ?? null - return row && content.contains(row) ? row : null -} - /** Rows the list must not let go of: one a reader is selecting text in, or * one they reached with the keyboard, which would otherwise lose its place. */ function heldRows(content: Element): HTMLElement[] { @@ -109,6 +104,31 @@ function heldRows(content: Element): HTMLElement[] { return rows.filter((row): row is HTMLElement => row !== null) } +// About how many parts the list reads at a time for a search, between which +// the page goes on. +const READ_PARTS = 50 + +/** The next messages to read for a search: those the list doesn't draw and + * hasn't read, about READ_PARTS parts of them, nearest the window first. */ +function unread(messages: Message[], start: number, end: number, index: TextIndex): Message[] { + const picked: number[] = [] + let parts = 0 + let up = start - 1 + let down = end + let upward = true + while (parts < READ_PARTS && (up >= 0 || down < messages.length)) { + const i = (upward && up >= 0) || down >= messages.length ? up-- : down++ + upward = !upward + const message = messages[i] + if (!message || message.pseudo || index.get(message) !== undefined) { + continue + } + picked.push(i) + parts += 1 + (message.parts?.length ?? 0) + } + return picked.sort((a, b) => a - b).flatMap((i) => messages[i] ?? []) +} + const jumpClasses = 'chat-composer chat-jump rounded-full px-3 py-1.5 text-xs font-medium theme-text transition-colors' @@ -167,16 +187,24 @@ interface ChatMessageListProps { /** Pins the prompt of the turn in view to the top once it scrolls out of * sight, as a terminal transcript does; a click returns to it. */ pinPrompt?: boolean - /** Draws every message, as a search of the whole conversation needs. - * Otherwise a long conversation draws its end, then the turns near the view - * as the reader scrolls, letting go of those it left far behind. */ + /** Draws every message at once, for a host that needs all of them on the + * page. Otherwise a long conversation draws its end, then the turns near the + * view as the reader scrolls, letting go of those it left far behind. */ whole?: boolean + /** Keeps the text a reader sees in each message the list doesn't draw, for + * a search of the whole conversation that doesn't draw it all. */ + textIndex?: TextIndex | undefined + /** Reads the messages it doesn't draw into textIndex, a few at a time. */ + indexing?: boolean | undefined ref?: Ref | undefined } export interface ChatMessageListHandle { /** Returns to the newest content and follows it again. */ jumpToLatest: () => void + /** Draws the turn a message is in and brings the message into view; + * resolves once it is drawn. */ + reveal: (messageId: string) => Promise } export default function ChatMessageList({ @@ -208,6 +236,8 @@ export default function ChatMessageList({ workingIndicator = true, pinPrompt, whole = false, + textIndex, + indexing = false, ref, }: ChatMessageListProps) { const { adapter, isStreaming: providerStreaming } = useChat() @@ -323,14 +353,49 @@ export default function ChatMessageList({ const [conversations, setConversations] = useState(0) // biome-ignore lint/correctness/useExhaustiveDependencies: a new memory for each, not read from them const memory = useMemo(() => createRowMemory(), [expandAll, conversations]) - const windowRef = useRef({ messages, indexOf, start, end, whole, editingMessageId }) + const windowRef = useRef({ messages, indexOf, start, end, whole, editingMessageId, textIndex }) useLayoutEffect(() => { - windowRef.current = { messages, indexOf, start, end, whole, editingMessageId } + windowRef.current = { messages, indexOf, start, end, whole, editingMessageId, textIndex } }) + // What a search read of the messages depends on what the reader opened in them. + // biome-ignore lint/correctness/useExhaustiveDependencies: a new memory means what was read is stale + useEffect(() => { + textIndex?.clear() + }, [memory, textIndex]) + // The messages a search is reading now: drawn out of sight, read, let go of. + const [reading, setReading] = useState(NO_MESSAGES) + const readerRef = useRef(null) + // biome-ignore lint/correctness/useExhaustiveDependencies: a new memory clears what was read, so reading starts again + useEffect(() => { + if (!indexing || !textIndex || reading.length > 0) { + return + } + // The page goes on between batches. + const timer = setTimeout(() => { + const next = unread(messages, start, end, textIndex) + if (next.length > 0) { + setReading(next) + } + }, 0) + return () => clearTimeout(timer) + }, [indexing, textIndex, reading, messages, start, end, memory]) + useLayoutEffect(() => { + if (reading.length === 0 || !textIndex) { + return + } + const rows = [...(readerRef.current?.children ?? [])] + for (const message of reading) { + const row = rows.find((r) => r instanceof HTMLElement && r.dataset['row'] === message.id) + textIndex.set(message, row ? visibleText(row) : '') + } + setReading(NO_MESSAGES) + }, [reading, textIndex]) // The first row in view and where it was, so the rows drawn or let go of // around it leave what the reader is looking at where it is. const keptRef = useRef<{ row: Element; top: number } | null>(null) const jumpedRef = useRef(false) + // A message reveal is drawing, and what to call once it shows. + const revealRef = useRef<{ id: string; done: () => void } | null>(null) // Draws the turns beyond either edge of the window once the reader is // within DRAW_SCREENS of it, or the window doesn't fill the view, and lets @@ -375,13 +440,25 @@ export default function ChatMessageList({ const keep = [...held.filter((i) => i >= first && i < last), ...near.map(indexOfRow)] const lowest = keep.length > 0 ? Math.min(...keep) : indexOfRow(rows.at(-1)) const highest = keep.length > 0 ? Math.max(...keep) : indexOfRow(rows[0]) + // A search reads a row let go of from what it showed, as the reader left it. + const remember = (gone: (i: number) => boolean) => { + const index = windowRef.current.textIndex + for (const row of index ? rows : []) { + const message = all[indexOfRow(row)] + if (message && gone(indexOfRow(row))) { + index?.set(message, visibleText(row)) + } + } + } const keepFrom = turnStart(all, lowest) if (keepFrom > first) { + remember((i) => i < keepFrom) setFrom(all[keepFrom]?.id ?? null) return } const keepTo = turnEnd(all, highest) if (keepTo < last) { + remember((i) => i >= keepTo) setTo(all[keepTo - 1]?.id ?? null) } }, []) @@ -450,6 +527,27 @@ export default function ChatMessageList({ setTo(null) }, []) + const reveal = useCallback( + (id: string) => + new Promise((resolve) => { + const { messages: all, indexOf: at, start: first, end: last } = windowRef.current + const i = at.get(id) + if (i === undefined || (i >= first && i < last)) { + resolve() + return + } + revealRef.current?.done() + revealRef.current = { id, done: resolve } + isNearBottomRef.current = false + setPinned(false) + const from = turnStart(all, i) + const to = Math.max(windowEnd(all, from), turnEnd(all, i)) + setFrom(all[from]?.id ?? null) + setTo(to >= all.length ? null : (all[to - 1]?.id ?? null)) + }), + [], + ) + // A reader who left the end keeps the turns they left it from, as new // ones arrive below. useEffect(() => { @@ -466,7 +564,7 @@ export default function ChatMessageList({ } }, [whole]) - useImperativeHandle(ref, () => ({ jumpToLatest }), [jumpToLatest]) + useImperativeHandle(ref, () => ({ jumpToLatest, reveal }), [jumpToLatest, reveal]) // Attach listeners once the container exists (it is absent in the empty state) const queuedCount = queuedMessages?.length ?? 0 @@ -581,7 +679,18 @@ export default function ChatMessageList({ useLayoutEffect(() => { const el = containerRef.current const kept = keptRef.current - if (el && jumpedRef.current) { + const revealing = revealRef.current + if (revealing) { + revealRef.current = null + const row = [...(contentRef.current?.children ?? [])].find( + (r) => r instanceof HTMLElement && r.dataset['row'] === revealing.id, + ) + if (el && row) { + el.scrollTop += row.getBoundingClientRect().top - el.getBoundingClientRect().top + lastScrollTopRef.current = el.scrollTop + } + revealing.done() + } else if (el && jumpedRef.current) { jumpedRef.current = false el.scrollTop = el.scrollHeight lastScrollTopRef.current = el.scrollTop @@ -703,6 +812,48 @@ export default function ChatMessageList({ const pinnedText = pinPrompt && anchor ? promptLine(messages.find((m) => m.id === anchor)) : '' + const rowProps = { + conversation: messages, + allMessages: rowMessages, + prompts: !!pinPrompt, + flush, + foldEveryRun, + editingMessageId, + showAuthor, + currentUsername, + attachmentDownloadUrl, + onOpenAttachment, + onNavigateBranch, + onEditMessage, + onSaveEdit, + onCancelEdit, + onRegenerateMessage, + onRetryMessage, + onOpenReasoning, + onApprovalAnswer, + } + + // Out of sight and out of reach: the rows a search reads, as they would show. + const reader = textIndex && ( + + ) + const list = (
{/* Thinking / "Thought for" section — visible during and after thinking */} {attached && isStreaming && streamingReasoning && ( @@ -859,7 +993,12 @@ export default function ChatMessageList({ ) if (!pinPrompt) { - return list + return ( + <> + {list} + {reader} + + ) } // The pin lies over the log rather than in it, so its coming and going is // not announced as a new message. It comes first so the tab order meets it @@ -876,6 +1015,7 @@ export default function ChatMessageList({ /> )} {list} + {reader}
) } diff --git a/packages/ui/src/ai/components/ChatMessageList.window.test.tsx b/packages/ui/src/ai/components/ChatMessageList.window.test.tsx index f00963ef..f0a90bab 100644 --- a/packages/ui/src/ai/components/ChatMessageList.window.test.tsx +++ b/packages/ui/src/ai/components/ChatMessageList.window.test.tsx @@ -1,8 +1,9 @@ // @vitest-environment jsdom import '@testing-library/jest-dom/vitest' -import { act, fireEvent, render, screen } from '@testing-library/react' +import { act, fireEvent, render, screen, waitFor } from '@testing-library/react' import { type ComponentProps, createRef } from 'react' import type { useChat, useChatStream } from '../core/ChatProvider' +import { createTextIndex } from '../core/textIndex' import type { ChatMessage as Message } from '../types' import { ExpandAllProvider } from './agent/AgentMessageParts' import ChatMessageList, { type ChatMessageListHandle } from './ChatMessageList' @@ -388,3 +389,86 @@ describe('ChatMessageList scrolled through a long conversation', () => { expect(drawnIds()[0]).toBe('u22') }) }) + +describe('ChatMessageList for a search', () => { + it('draws the turn of a message it reveals, with the message at the top of the view', async () => { + const { log, ref } = renderList(turns(1, 30)) + + let revealed: Promise | undefined + act(() => { + revealed = ref.current?.reveal('a5') + }) + await revealed + + expect(drawnIds()).toContain('a5') + expect(drawnIds()).not.toContain('a30') + expect(log.scrollTop).toBe(ROW) + expect(screen.queryByText('Jump to latest')).toBeInTheDocument() + }) + + it('reads the turns it does not draw, a few at a time', async () => { + const conversation = turns(1, 10) + const index = createTextIndex() + // The rows each batch draws out of sight: a batch is drawn, read and let go of at once. + const batches: number[] = [] + const watch = new MutationObserver((records) => { + const read = records.flatMap((r) => + r.target instanceof Element && r.target.closest('[data-transcript]') + ? [] + : [...r.addedNodes].filter((n) => n instanceof HTMLElement && n.hasAttribute('data-row')), + ) + if (read.length > 0) { + batches.push(read.length) + } + }) + watch.observe(document.body, { childList: true, subtree: true }) + renderList(conversation, { textIndex: index, indexing: true }) + + await waitFor(() => expect(index.get(conversation[0] as Message)).toBe('Question 1')) + watch.disconnect() + + expect(index.get(conversation[13] as Message)).toBe('') + expect(drawnIds()).toEqual(['u8', 'a8', 'u9', 'a9', 'u10', 'a10']) + // About 50 parts at a time, here a turn or two of the fourteen rows it doesn't draw. + expect(batches.length).toBeGreaterThanOrEqual(3) + expect(Math.max(...batches)).toBeLessThanOrEqual(4) + }) + + it('reads nothing while no search asks it to', async () => { + const conversation = turns(1, 10) + const index = createTextIndex() + renderList(conversation, { textIndex: index }) + + await act(() => new Promise((resolve) => setTimeout(resolve, 50))) + + // The reply just before the window would be read first. + expect(index.get(conversation[13] as Message)).toBeUndefined() + }) + + it('keeps what a turn it lets go of showed', () => { + const conversation = turns(1, 30) + const index = createTextIndex() + const { log } = renderList(conversation, { textIndex: index }) + scrollWithin(log, 0) + + scrollWithin(log, 15 * ROW) + + expect(index.get(conversation[42] as Message)).toBe('Question 22') + }) + + it('forgets what it read once what the reader opened starts afresh', () => { + const conversation = turns(1, 3) + const index = createTextIndex() + const list = (all: boolean) => ( + + + + ) + const { rerender } = render(list(false)) + index.set(conversation[0] as Message, 'Question 1') + + rerender(list(true)) + + expect(index.get(conversation[0] as Message)).toBeUndefined() + }) +}) diff --git a/packages/ui/src/ai/components/SessionView.find.test.tsx b/packages/ui/src/ai/components/SessionView.find.test.tsx index 0e2d5054..8a518c41 100644 --- a/packages/ui/src/ai/components/SessionView.find.test.tsx +++ b/packages/ui/src/ai/components/SessionView.find.test.tsx @@ -198,22 +198,63 @@ describe('SessionView find', () => { expect(current()?.map((r) => r.toString())).toEqual(['timeout']) }) - it('finds in turns too far back for the list to have drawn them', () => { - const notes = (n: number): Message['parts'] => - Array.from({ length: 99 }, (_, i) => ({ kind: 'text' as const, text: `note ${n}.${i}` })) - const long: Message[] = [ - messages[0] as Message, - ...[0, 1, 2, 3].flatMap((n): Message[] => [ - { id: `q${n}`, role: 'user', content: `Step ${n}` }, - { id: `r${n}`, role: 'assistant', parts: notes(n) }, - ]), - ] + // Turns of 99 notes each: the list draws the last of them, and the first turn + // asks about the login test. + const notes = (n: number): Message['parts'] => + Array.from({ length: 99 }, (_, i) => ({ kind: 'text' as const, text: `note ${n}.${i}` })) + const long: Message[] = [ + messages[0] as Message, + ...[0, 1, 2, 3].flatMap((n): Message[] => [ + { id: `q${n}`, role: 'user', content: `Step ${n}` }, + { id: `r${n}`, role: 'assistant', parts: notes(n) }, + ]), + ] + const drawnRows = () => document.querySelectorAll('[data-transcript] [data-row]').length + + it('finds in turns too far back for the list to have drawn them, and draws the turn to show it', async () => { render() expect(screen.queryByText(/login test/)).not.toBeInTheDocument() fireEvent.change(openFind(), { target: { value: 'login' } }) - expect(count()).toHaveTextContent('1 of 1') + await waitFor(() => expect(count()).toHaveTextContent('1 of 1')) + await waitFor(() => expect(current()?.map((r) => r.toString())).toEqual(['login'])) + expect(screen.getByText(/login test/)).toBeInTheDocument() + }) + + it('counts the matches in turns it does not draw without drawing them all', async () => { + render() + const drawnAtFirst = drawnRows() + + fireEvent.change(openFind(), { target: { value: 'note' } }) + + await waitFor(() => expect(count()).toHaveTextContent(/^\d+ of 396$/)) + expect(drawnRows()).toBeLessThan(long.length) + expect(drawnRows()).toBeLessThanOrEqual(drawnAtFirst + 2) + }) + + it('says more may match while it is still reading the turns it does not draw', async () => { + render() + + fireEvent.change(openFind(), { target: { value: 'note' } }) + + expect(count()).not.toHaveTextContent(/^\d+ of \d+$/) + await waitFor(() => expect(count()).toHaveTextContent(/\+$/)) + await waitFor(() => expect(count()).toHaveTextContent(/^\d+ of 396$/)) + }) + + it('draws the turn of a match it steps to', async () => { + render() + const input = openFind() + fireEvent.change(input, { target: { value: 'Step' } }) + // Only the last turn is drawn, so the match shown is its prompt's, the last of four. + await waitFor(() => expect(count()).toHaveTextContent('4 of 4')) + expect(screen.queryByText('Step 0')).not.toBeInTheDocument() + + fireEvent.keyDown(input, { key: 'Enter' }) + + await waitFor(() => expect(count()).toHaveTextContent('1 of 4')) + await waitFor(() => expect(current()?.[0]?.startContainer.textContent).toBe('Step 0')) }) it('keeps the query to search again when reopened', () => { diff --git a/packages/ui/src/ai/components/SessionView.stories.tsx b/packages/ui/src/ai/components/SessionView.stories.tsx index 15ecd4eb..3f00b617 100644 --- a/packages/ui/src/ai/components/SessionView.stories.tsx +++ b/packages/ui/src/ai/components/SessionView.stories.tsx @@ -1,6 +1,7 @@ import type { Meta, StoryObj } from '@storybook/react-vite' import { useEffect, useRef, useState } from 'react' -import { within } from 'storybook/test' +import { userEvent, within } from 'storybook/test' +import { FIND_IN_TRANSCRIPT_EVENT } from '../core/events' import { makeAgentTurn, makeLongConversation, @@ -488,3 +489,54 @@ export const HandBackAfterFailure: StoryObj = { }, render: (args) => , } + +// Forty turns of sixty commands each, as a long agent session runs. +const longSession: ChatMessage[] = Array.from({ length: 40 }, (_, n) => [ + makeMessage({ + id: `find-ask-${n}`, + role: 'user', + content: `Run shard ${n + 1} and tell me what failed.`, + }), + makeMessage({ + id: `find-work-${n}`, + role: 'assistant', + content: null, + parts: [ + ...Array.from({ length: 60 }, () => makeToolCallPart({ resultLines: 2 })), + { kind: 'text', text: `Shard ${n + 1} passed once the flaky wait was retried.` }, + ], + }), +]).flat() + +type FindInLongSessionArgs = { query: string } + +/** Find in a long session, of which the view draws the last few turns: it + * counts the matches in every turn, reading those it doesn't draw a few at a + * time, and draws the turn of the match it shows. Until it has read them all, + * the count says more may come. */ +export const FindInLongSession: StoryObj = { + args: { query: 'Shard 3 passed' }, + argTypes: { query: { control: 'text' } }, + render: () => ( + + + true} + flush + foldEveryRun + find + workingIndicator={false} + /> + + + ), + play: async ({ canvasElement, args }) => { + const canvas = within(canvasElement) + await canvas.findByText(/Shard 40 passed/) + document.dispatchEvent(new Event(FIND_IN_TRANSCRIPT_EVENT)) + const input = await canvas.findByRole('textbox', { name: 'Find in conversation' }) + await userEvent.type(input, args.query) + }, +} diff --git a/packages/ui/src/ai/components/SessionView.test.tsx b/packages/ui/src/ai/components/SessionView.test.tsx index dbf8278a..94ea346f 100644 --- a/packages/ui/src/ai/components/SessionView.test.tsx +++ b/packages/ui/src/ai/components/SessionView.test.tsx @@ -29,7 +29,7 @@ vi.mock('./ChatMessageList', () => ({ __esModule: true, default: (props: ComponentProps) => { listProps = props - useImperativeHandle(props.ref, () => ({ jumpToLatest }), []) + useImperativeHandle(props.ref, () => ({ jumpToLatest, reveal: async () => {} }), []) return (
{props.footer} diff --git a/packages/ui/src/ai/components/SessionView.tsx b/packages/ui/src/ai/components/SessionView.tsx index 62215d69..98092b7c 100644 --- a/packages/ui/src/ai/components/SessionView.tsx +++ b/packages/ui/src/ai/components/SessionView.tsx @@ -5,6 +5,7 @@ import { useCallback, useEffect, useImperativeHandle, + useMemo, useRef, useState, } from 'react' @@ -12,6 +13,7 @@ import { useMarkdownReady } from '../../components/Markdown' import { useChat } from '../core/ChatProvider' import type { ApprovalAnswerHandler } from '../core/config' import { listenWhileShown } from '../core/onScreen' +import { createTextIndex } from '../core/textIndex' import useDragDrop from '../core/useDragDrop' import { escapeClosesLayer } from '../core/useKeyboardShortcuts' import type { ChatMessage as Message } from '../types' @@ -19,7 +21,7 @@ import ChatActivity from './ChatActivity' import ChatInput, { type ChatInputHandle } from './ChatInput' import ChatMessageList, { type ChatMessageListHandle } from './ChatMessageList' import DragOverlay from './DragOverlay' -import { TranscriptFindBar, useTranscriptFind } from './TranscriptFind' +import { type FindSource, TranscriptFindBar, useTranscriptFind } from './TranscriptFind' type ComposerProps = Omit< ComponentProps, @@ -137,7 +139,8 @@ export default function SessionView({ const listRef = useRef(null) const editQueued = useCallback(() => inputRef.current?.recall(), []) const viewRef = useRef(null) - const finder = useTranscriptFind(viewRef, find) + // What find reads of the messages the list doesn't draw. + const [textIndex] = useState(createTextIndex) const [editingId, setEditingId] = useState(null) const { isStreaming: providerStreaming, stopStreaming } = useChat() const running = turn ? turn.running : providerStreaming @@ -182,6 +185,12 @@ export default function SessionView({ ...(queuedMessages ? { queuedMessages } : {}), } : { messages: [], allMessages: [], streamingMessage: null, queuedMessages: [] } + const reveal = useCallback((id: string) => listRef.current?.reveal(id) ?? Promise.resolve(), []) + const findSource = useMemo( + () => (find ? { messages: transcript.messages, index: textIndex, reveal } : undefined), + [find, transcript.messages, textIndex, reveal], + ) + const finder = useTranscriptFind(viewRef, find, findSource) // Escape stops the turn, wherever the turn runs, while the view is on screen. useEffect(() => { @@ -304,7 +313,7 @@ export default function SessionView({ {...(flush ? { flush } : {})} {...(foldEveryRun ? { foldEveryRun } : {})} {...(pinPrompt ? { pinPrompt } : {})} - whole={finder.open} + {...(find ? { textIndex, indexing: finder.open } : {})} workingIndicator={workingIndicator ?? !activity} inputElement={input} /> diff --git a/packages/ui/src/ai/components/TranscriptFind.tsx b/packages/ui/src/ai/components/TranscriptFind.tsx index 88f4e3a6..fddf017b 100644 --- a/packages/ui/src/ai/components/TranscriptFind.tsx +++ b/packages/ui/src/ai/components/TranscriptFind.tsx @@ -3,6 +3,7 @@ import { type RefObject, useCallback, useEffect, + useLayoutEffect, useRef, useState, } from 'react' @@ -13,52 +14,179 @@ import { ChevronDownIcon, ChevronUpIcon, CloseIcon, SearchIcon } from '../../ico import { useChatConfig } from '../core/config' import { FIND_IN_TRANSCRIPT_EVENT } from '../core/events' import { listenWhileShown, onScreen } from '../core/onScreen' +import type { TextIndex } from '../core/textIndex' import { + countMatches, + findPattern, findRanges, - firstInView, MAX_MATCHES, revealRange, + rowOf, transcriptOf, } from '../core/transcriptFind' +import type { ChatMessage as Message } from '../types' import { isMac } from '../utils' // The ::highlight() names ai.css paints. const MATCHES = 'transcript-find' const CURRENT = 'transcript-find-current' +/** A match: a range in a message the list draws, or the k-th match in the + * text of one it doesn't, which gets its range once that message is drawn. */ +interface Hit { + id: string | null + k: number + range: Range | null + /** Above the messages drawn, among them, or below them. */ + where: 'before' | 'drawn' | 'after' +} + interface Found { - ranges: Range[] + hits: Hit[] current: number + /** More may match than counted: a message not read yet, or the cap. */ + more: boolean +} + +const NONE: Found = { hits: [], current: 0, more: false } + +/** What find searches beyond what the list draws: every message of the + * conversation, the text the list read of those it doesn't draw, and a way + * to draw one. */ +export interface FindSource { + messages: Message[] + index: TextIndex + reveal: (messageId: string) => Promise } // Painted with the CSS Custom Highlight API, so the transcript's DOM, which // React owns, is never touched. A browser without it still counts and scrolls. -function paint({ ranges, current }: Found) { +function paint({ hits, current }: Pick) { if (typeof CSS === 'undefined' || !('highlights' in CSS)) { return } - const shown = ranges[current] - if (!shown) { + const shown = hits[current]?.range + const others = hits.flatMap((hit) => (hit.range && hit.range !== shown ? [hit.range] : [])) + if (others.length > 0) { + CSS.highlights.set(MATCHES, new Highlight(...others)) + } else { CSS.highlights.delete(MATCHES) + } + if (!shown) { CSS.highlights.delete(CURRENT) return } - CSS.highlights.set(MATCHES, new Highlight(...ranges.filter((_, i) => i !== current))) const highlight = new Highlight(shown) highlight.priority = 1 CSS.highlights.set(CURRENT, highlight) } +/** Every match of the query, in reading order: in the messages above those + * drawn, in those drawn, and in those below, up to MAX_MATCHES. */ +function search( + transcript: Element | null, + query: string, + source: FindSource | undefined, +): Omit { + if (!findPattern(query)) { + return { hits: [], more: false } + } + const counted = new Map() + const drawn = (transcript ? findRanges(transcript, query) : []).map((range): Hit => { + const id = transcript ? (rowOf(range.startContainer, transcript)?.dataset['row'] ?? null) : null + const k = counted.get(id) ?? 0 + counted.set(id, k + 1) + return { id, k, range, where: 'drawn' } + }) + let more = drawn.length === MAX_MATCHES + if (!source) { + return { hits: drawn, more } + } + const { messages, index } = source + const rows = new Set( + [...(transcript?.querySelectorAll('[data-row]') ?? [])].map( + (row) => row.dataset['row'], + ), + ) + const first = messages.findIndex((m) => rows.has(m.id)) + const last = messages.findLastIndex((m) => rows.has(m.id)) + const read = (from: number, to: number, where: Hit['where']) => { + const hits: Hit[] = [] + for (const message of messages.slice(from, to)) { + if (message.pseudo) { + continue + } + const text = index.get(message) + if (text === undefined) { + more = true + continue + } + const count = countMatches(text, query, MAX_MATCHES) + for (let k = 0; k < count; k++) { + hits.push({ id: message.id, k, range: null, where }) + } + } + return hits + } + const hits = + first === -1 + ? read(0, messages.length, 'after') + : [...read(0, first, 'before'), ...drawn, ...read(last + 1, messages.length, 'after')] + if (hits.length > MAX_MATCHES) { + more = true + } + return { hits: hits.slice(0, MAX_MATCHES), more } +} + +/** The first match from the top of what the reader is looking at, as a + * browser's find starts from where the page is scrolled to. */ +function fromView(hits: Hit[], transcript: Element | null): number { + const top = transcript?.closest('[role="log"]')?.getBoundingClientRect().top + const index = hits.findIndex( + (hit) => + hit.where === 'after' || + (hit.where === 'drawn' && + (top === undefined || + !hit.range || + typeof hit.range.getBoundingClientRect !== 'function' || + hit.range.getBoundingClientRect().top >= top)), + ) + return index === -1 ? Math.max(hits.length - 1, 0) : index +} + +/** Where hit is among hits found again, or for a message that matches fewer + * times now, its last match there. */ +function sameHit(hits: Hit[], hit: Hit | undefined): number { + if (!hit) { + return -1 + } + const at = hits.findIndex((h) => h.id === hit.id && h.k === hit.k) + return at !== -1 || hit.id === null ? at : hits.findLastIndex((h) => h.id === hit.id) +} + /** Find in the transcript under root: ⌘F or FIND_IN_TRANSCRIPT_EVENT opens it, - * and it finds again as the transcript streams. */ -export function useTranscriptFind(root: RefObject, enabled: boolean) { + * and it finds again as the transcript streams. With a source, it also finds + * in the messages the list doesn't draw, and draws one to show a match in it. */ +export function useTranscriptFind( + root: RefObject, + enabled: boolean, + source?: FindSource, +) { const [open, setOpen] = useState(false) const [query, setQuery] = useState('') - const [found, setFound] = useState({ ranges: [], current: 0 }) + const [found, setFound] = useState(NONE) const [focusTick, setFocusTick] = useState(0) const inputRef = useRef(null) const returnFocus = useRef(null) const foundRef = useRef(found) + const queryRef = useRef(query) + const sourceRef = useRef(source) + useLayoutEffect(() => { + queryRef.current = query + sourceRef.current = source + }) + // Each landing on a match; one that finishes after a newer one began does nothing. + const landing = useRef(0) const show = useCallback(() => { if (!inputRef.current) { @@ -69,18 +197,46 @@ export function useTranscriptFind(root: RefObject, enabled: setFocusTick((n) => n + 1) }, []) - const step = useCallback((by: 1 | -1) => { - const { ranges, current } = foundRef.current - const target = (current + by + ranges.length) % ranges.length - const range = ranges[target] - if (!range) { - return - } - foundRef.current = { ranges, current: target } - setFound(foundRef.current) - revealRange(range) + const settle = useCallback((next: Found) => { + foundRef.current = next + setFound(next) }, []) + // Shows the match at target: one in a message the list doesn't draw is drawn first. + const land = useCallback( + async (base: Omit, target: number) => { + const ticket = ++landing.current + const hit = base.hits[target] + settle({ ...base, current: target }) + if (hit && !hit.range && hit.id !== null && sourceRef.current) { + await sourceRef.current.reveal(hit.id) + if (ticket !== landing.current) { + return + } + const again = search(transcriptOf(root.current), queryRef.current, sourceRef.current) + const at = sameHit(again.hits, hit) + settle({ + ...again, + current: at === -1 ? Math.min(target, Math.max(again.hits.length - 1, 0)) : at, + }) + } + const range = foundRef.current.hits[foundRef.current.current]?.range + if (range) { + revealRange(range) + } + }, + [root, settle], + ) + + const step = useCallback( + (by: 1 | -1) => { + const { hits, current } = foundRef.current + if (hits.length > 0) { + void land(foundRef.current, (current + by + hits.length) % hits.length) + } + }, + [land], + ) useEffect(() => { if (!enabled) { return @@ -125,68 +281,81 @@ export function useTranscriptFind(root: RefObject, enabled: useEffect(() => { if (!open) { - setFound({ ranges: [], current: 0 }) + landing.current++ + settle(NONE) return } const transcript = transcriptOf(root.current) - const ranges = transcript ? findRanges(transcript, query) : [] - const current = firstInView(ranges, transcript) - setFound({ ranges, current }) - const range = ranges[current] - if (range) { - revealRange(range) - } - }, [open, query, root]) + const base = search(transcript, query, sourceRef.current) + void land(base, fromView(base.hits, transcript)) + }, [open, query, root, land, settle]) - // Streaming adds text and a remount replaces it. Find again, keeping the - // reader's place rather than scrolling them away from what they are reading. + // Streaming adds text, a remount replaces it, and the list reads the messages + // it doesn't draw. Find again, keeping the reader's place rather than + // scrolling them away from what they are reading. + const index = source?.index useEffect(() => { const target = root.current if (!open || !query.trim() || !target) { return } let timer: ReturnType | undefined - const observer = new MutationObserver((records) => { - const transcript = transcriptOf(target) - // The bar's own count and the composer change too; only the messages, - // or a remount above them, can change what matches. - const changed = records.some( - (r) => !transcript || transcript.contains(r.target) || r.target.contains(transcript), - ) - if (timer || !changed) { + const again = () => { + if (timer) { return } timer = setTimeout(() => { timer = undefined - const scope = transcriptOf(target) - const ranges = scope ? findRanges(scope, query) : [] - setFound((prev) => ({ - ranges, - current: Math.min(prev.current, Math.max(ranges.length - 1, 0)), - })) + const prev = foundRef.current + const transcript = transcriptOf(target) + const next = search(transcript, query, sourceRef.current) + // The first matches found while the list reads are shown as find shows any. + if (prev.hits.length === 0 && next.hits.length > 0) { + void land(next, fromView(next.hits, transcript)) + return + } + const at = sameHit(next.hits, prev.hits[prev.current]) + settle({ + ...next, + current: at === -1 ? Math.min(prev.current, Math.max(next.hits.length - 1, 0)) : at, + }) }, 200) + } + const observer = new MutationObserver((records) => { + const transcript = transcriptOf(target) + // The bar's own count, the composer and the rows the list reads change + // too; only the messages, or a remount above them, can change what matches. + if ( + records.some( + (r) => !transcript || transcript.contains(r.target) || r.target.contains(transcript), + ) + ) { + again() + } }) observer.observe(target, { childList: true, subtree: true, characterData: true }) + const stop = index?.subscribe(again) return () => { observer.disconnect() + stop?.() clearTimeout(timer) } - }, [open, query, root]) + }, [open, query, root, index, land, settle]) // The highlights are the page's, not this view's: only clear what this view painted, // so another view mounting or unmounting leaves an open find's matches alone. const painted = useRef(false) useEffect(() => { - foundRef.current = found - if (found.ranges.length > 0 || painted.current) { + const ranged = found.hits.some((hit) => hit.range) + if (ranged || painted.current) { paint(found) - painted.current = found.ranges.length > 0 + painted.current = ranged } }, [found]) useEffect( () => () => { if (painted.current) { - paint({ ranges: [], current: 0 }) + paint(NONE) } }, [], @@ -207,8 +376,9 @@ export function useTranscriptFind(root: RefObject, enabled: open: enabled && open, query, setQuery, - total: found.ranges.length, + total: found.hits.length, current: found.current, + more: found.more, next, previous, close, @@ -225,12 +395,15 @@ export function TranscriptFindBar({ find }: { find: TranscriptFind }) { if (!find.open) { return null } - const { total, current } = find + const { total, current, more } = find + // While the list still reads the messages it doesn't draw, the count says more may come. const count = !find.query.trim() ? '' : total === 0 - ? t.none - : (total === MAX_MATCHES ? t.countCapped : t.count)(current + 1, total) + ? more + ? '' + : t.none + : (more ? t.countCapped : t.count)(current + 1, total) const onKeyDown = (e: ReactKeyboardEvent) => { if (e.key === 'Escape') { diff --git a/packages/ui/src/ai/core/textIndex.ts b/packages/ui/src/ai/core/textIndex.ts new file mode 100644 index 00000000..1be25de3 --- /dev/null +++ b/packages/ui/src/ai/core/textIndex.ts @@ -0,0 +1,41 @@ +import type { ChatMessage as Message } from '../types' + +/** The text a reader sees in each message of a transcript, kept for the + * messages the list doesn't draw, so a search reaches them without drawing + * them all. A message is known by its object, which a change replaces, so one + * read before it changed is read again. */ +export interface TextIndex { + get(message: Message): string | undefined + set(message: Message, text: string): void + clear(): void + subscribe(listener: () => void): () => void +} + +export function createTextIndex(): TextIndex { + let texts = new WeakMap() + const listeners = new Set<() => void>() + const notify = () => { + for (const listener of listeners) { + listener() + } + } + return { + get: (message) => texts.get(message), + set(message, text) { + if (texts.get(message) !== text) { + texts.set(message, text) + notify() + } + }, + clear() { + texts = new WeakMap() + notify() + }, + subscribe(listener) { + listeners.add(listener) + return () => { + listeners.delete(listener) + } + }, + } +} diff --git a/packages/ui/src/ai/core/transcriptFind.ts b/packages/ui/src/ai/core/transcriptFind.ts index c93325b1..28d6dd74 100644 --- a/packages/ui/src/ai/core/transcriptFind.ts +++ b/packages/ui/src/ai/core/transcriptFind.ts @@ -102,22 +102,37 @@ export function findRanges(scope: Element, query: string): Range[] { return ranges } +/** The text a reader sees in scope, as find reads it. */ +export function visibleText(scope: Element): string { + return collectText(scope).text +} + +/** How many times the query appears in text, up to max. */ +export function countMatches(text: string, query: string, max: number): number { + const pattern = findPattern(query) + if (!pattern || max <= 0) { + return 0 + } + let count = 0 + for (const _ of text.matchAll(pattern)) { + count++ + if (count === max) { + break + } + } + return count +} + /** The messages under root, without the composer that scrolls with them. */ export function transcriptOf(root: Element | null): Element | null { return root?.querySelector(TRANSCRIPT) ?? null } -/** The first match from the top of what the reader is looking at, as a - * browser's find starts from where the page is scrolled to. */ -export function firstInView(ranges: Range[], transcript: Element | null): number { - const top = transcript?.closest('[role="log"]')?.getBoundingClientRect().top - if (top === undefined) { - return 0 - } - const index = ranges.findIndex( - (r) => typeof r.getBoundingClientRect !== 'function' || r.getBoundingClientRect().top >= top, - ) - return index === -1 ? Math.max(ranges.length - 1, 0) : index +/** The message row a node is in, of those under scope. */ +export function rowOf(node: Node | null | undefined, scope: Element): HTMLElement | null { + const el = node instanceof Element ? node : (node?.parentElement ?? null) + const row = el?.closest('[data-row]') ?? null + return row && scope.contains(row) ? row : null } /** Brings a match into the part of the transcript the composer doesn't cover,