Skip to content

The agent that grows with you

feat(desktop): Show earlier pages the DOM, then pulls older history from the store

Brooklyn Nicholson62012a5362f6
diff --git a/apps/desktop/src/app/chat/index.tsx b/apps/desktop/src/app/chat/index.tsx
index 7a0c0a2674c..1bb98203b5d 100644
--- a/apps/desktop/src/app/chat/index.tsx
+++ b/apps/desktop/src/app/chat/index.tsx
@@ -8,6 +8,7 @@ import { useLocation } from 'react-router'
import type { SubmitTextOptions } from '@/app/session/hooks/use-prompt-actions/utils'
import { Thread } from '@/components/assistant-ui/thread'
+import { TranscriptWindowProvider } from '@/components/assistant-ui/thread/transcript-window'
import { Backdrop } from '@/components/Backdrop'
import { COMPOSER_HEART_CONFIG, HeartField } from '@/components/chat/vibe-hearts'
import { usePaneVisible } from '@/components/pane-shell/pane-visibility'
@@ -63,6 +64,7 @@ import { ScrollToBottomButton } from './scroll-to-bottom-button'
import { useSessionView } from './session-view'
import { SessionActionsMenu } from './sidebar/session-actions-menu'
import { threadLoadingState } from './thread-loading'
+import { selectTranscriptWindow } from './transcript-window'
interface ChatViewProps extends Omit<React.ComponentProps<'div'>, 'onSubmit'> {
gateway: HermesGateway | null
@@ -220,9 +222,34 @@ function ChatRuntimeBoundary({
onThreadMessagesChange,
suppressMessages
}: ChatRuntimeBoundaryProps) {
- const storeMessages = useMessagesWhileVisible(useSessionView().$messages)
+ const view = useSessionView()
+ const runtimeId = useStore(view.$runtimeId)
+ const storeMessages = useMessagesWhileVisible(view.$messages)
const messages = suppressMessages ? NO_MESSAGES : storeMessages
- const runtimeMessageRepository = useRuntimeMessageRepository(messages)
+
+ const [windowPages, setWindowPages] = useState(1)
+ const [windowSessionKey, setWindowSessionKey] = useState(runtimeId)
+
+ // Reset the window on session swap during RENDER, so a large expand from the
+ // previous chat can't leak into the next one's first paint (#55191).
+ if (windowSessionKey !== runtimeId) {
+ setWindowSessionKey(runtimeId)
+ setWindowPages(1)
+ }
+
+ const { messages: windowedMessages, windowed } = useMemo(
+ () => selectTranscriptWindow(messages, windowPages),
+ [messages, windowPages]
+ )
+
+ const runtimeMessageRepository = useRuntimeMessageRepository(windowedMessages)
+
+ const expandWindow = useCallback(() => setWindowPages(pages => pages + 1), [])
+
+ const transcriptWindow = useMemo(
+ () => ({ olderAvailable: windowed, expandWindow }),
+ [expandWindow, windowed]
+ )
const runtime = useIncrementalExternalStoreRuntime<ThreadMessage>({
messageRepository: runtimeMessageRepository,
@@ -237,7 +264,11 @@ function ChatRuntimeBoundary({
onReload
})
- return <AssistantRuntimeProvider runtime={runtime}>{children}</AssistantRuntimeProvider>
+ return (
+ <TranscriptWindowProvider value={transcriptWindow}>
+ <AssistantRuntimeProvider runtime={runtime}>{children}</AssistantRuntimeProvider>
+ </TranscriptWindowProvider>
+ )
}
// Memoized: the tile caller (session-tile.tsx) and the contrib surface re-render
diff --git a/apps/desktop/src/components/assistant-ui/thread/transcript-window.test.ts b/apps/desktop/src/components/assistant-ui/thread/transcript-window.test.ts
new file mode 100644
index 00000000000..ee41e41561d
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/transcript-window.test.ts
@@ -0,0 +1,18 @@
+import { describe, expect, it } from 'vitest'
+
+import { resolveShowEarlierAction } from './transcript-window'
+
+describe('resolveShowEarlierAction', () => {
+ it('spends the already-materialized DOM page first', () => {
+ expect(resolveShowEarlierAction(3, true)).toBe('dom')
+ expect(resolveShowEarlierAction(3, false)).toBe('dom')
+ })
+
+ it('expands the store window once the DOM page is exhausted', () => {
+ expect(resolveShowEarlierAction(0, true)).toBe('window')
+ })
+
+ it('is a no-op when neither DOM nor store has older content', () => {
+ expect(resolveShowEarlierAction(0, false)).toBe(null)
+ })
+})
diff --git a/apps/desktop/src/components/assistant-ui/thread/transcript-window.tsx b/apps/desktop/src/components/assistant-ui/thread/transcript-window.tsx
new file mode 100644
index 00000000000..331fec19146
--- /dev/null
+++ b/apps/desktop/src/components/assistant-ui/thread/transcript-window.tsx
@@ -0,0 +1,43 @@
+import { createContext, type ReactNode, useContext } from 'react'
+
+export interface TranscriptWindowValue {
+ /** Store holds older messages the runtime window has not materialized. */
+ olderAvailable: boolean
+ /** Pull one more page of older messages out of the session store. */
+ expandWindow: () => void
+}
+
+const TranscriptWindowContext = createContext<TranscriptWindowValue>({
+ olderAvailable: false,
+ expandWindow: () => {}
+})
+
+export function TranscriptWindowProvider({
+ children,
+ value
+}: {
+ children: ReactNode
+ value: TranscriptWindowValue
+}) {
+ return <TranscriptWindowContext.Provider value={value}>{children}</TranscriptWindowContext.Provider>
+}
+
+export function useTranscriptWindow(): TranscriptWindowValue {
+ return useContext(TranscriptWindowContext)
+}
+
+/**
+ * "Show earlier" pages the DOM budget first and only then asks the store for
+ * more messages — the DOM page is already-materialized content, so spending it
+ * first keeps the click cheap and the store window as small as it can be.
+ */
+export function resolveShowEarlierAction(
+ hiddenCount: number,
+ olderAvailable: boolean
+): 'dom' | 'window' | null {
+ if (hiddenCount > 0) {
+ return 'dom'
+ }
+
+ return olderAvailable ? 'window' : null
+}