Files
orca/mobile/src/session/MobileNativeChatRowItemSource.tsx
T
Brennan BensonandNeil 25d331af76 perf(mobile): a streaming reply re-renders only the rows that changed (#23840)
* perf(mobile): a streaming reply re-renders only the rows that changed

Every structured-chat batch rebuilt the mobile transcript: the fold cloned each
historical tool run, image and echo rows rebuilt their blocks, each settled
turn's status object was rebuilt, and the list's renderItem changed identity,
so every mounted row re-rendered per batch.

Move the desktop list's by-id row reuse into src/shared and apply it to the
mobile list rows and to their derived turn props, and turn on FlatList
strictMode so a stable renderItem skips unchanged cells.

* perf(mobile): resolve native chat row props only when rows or turns change

The view re-renders on every composer keystroke; the row-props pass walked the
whole loaded transcript each time. Also drop a test that pinned a limit rather
than behavior, and a turn-status comment the row reuse made stale.

* refactor(native-chat): type the shared row reuse comparison

* perf(native-chat): compare rebuilt block lists with an index loop

Folding hands each tool-run assistant row a new block list every batch.
Walking that list as a generic object allocated Object.keys arrays for
both sides; comparing two arrays by length and element skips that.

* fix(mobile): carry the not-sent notice through the reused chat rows

Main added a per-row not-sent notice (#27936) after this branch's row reuse;
it now travels in each row's reused props, so a rejection arriving redraws
that row. Row reuse also compares only plain objects and arrays by value: a
Set (the open subagent groups) keeps its contents off its own keys, so two
different Sets read as equal and a toggled group kept its stale state.
Restores the row-renderer hook and the shared message comparison the
branch's merge commits carried.

* perf(native-chat): keep the desktop list's row comparison as it was

The deeper value comparison is only needed for mobile's re-derived rows; the
desktop projection goes back to comparing block identity.

* Restore main's files the pre-commit formatter rewrote during the merge

* Allow the FlatList strictMode augmentation's interface

* Pass FlatList strictMode through a spread instead of a type augmentation

* Measure streaming row churn on the phone page against main

* Keep unchanged phone chat row inputs stable during streaming

* Project full-message recovery context with each phone chat row

* Cover recovery updates and stale requests in streaming row contract

* Inspect row messages through their recovery provider in view tests

* Use a type-only module import in the recovery mock

* Preserve chat recovery identity and skip unchanged row derivation

* Keep transcript reuse at mounted rows and publish current recovery

* Remove duplicate React type import from transcript parity test

* Use static type imports in transcript streaming tests

* Complete the streaming recovery output fixture

* Temporarily expose failing mobile test compiler diagnostics

* Correct streaming test callback and selector types

---------

Co-authored-by: Neil <neil@stably.ai>
2026-10-26 00:50:13 -07:00

89 lines
3.0 KiB
TypeScript

import { memo, useContext, useMemo, type ComponentProps } from 'react'
import {
MobileNativeChatItemSourceContext,
MobileNativeChatRootItemSourceContext
} from './mobile-native-chat-item-read'
import { MobileNativeChatMessage } from './MobileNativeChatMessage'
import {
resolveMobileNativeChatRowProps,
type MobileNativeChatRowLookups
} from './use-mobile-native-chat-row-props'
import { equalMobileNativeChatRowContent } from './mobile-native-chat-row-projection'
import type { MobileNativeChatVisualSource } from './mobile-native-chat-visual-read'
import type { MobileNativeChatMessageDetail } from './use-mobile-native-chat-message-detail'
import type { NativeChatMessage } from '../../../src/shared/native-chat-types'
type Recovery = MobileNativeChatVisualSource & {
load: (id: string) => Promise<void>
details: {
id: string
raw: NativeChatMessage | undefined
state: ReturnType<MobileNativeChatMessageDetail['state']>
}[]
}
export type MobileNativeChatRowContentProps = ComponentProps<typeof MobileNativeChatMessage> & {
scope: string
source: Recovery | null
}
// React's mounted content props are the sole reuse baseline.
export const MobileNativeChatRowContent = memo(function MobileNativeChatRowContent({
source,
scope: _scope,
...props
}: MobileNativeChatRowContentProps) {
const value = useMemo(() => {
if (!source) {
return null
}
const details = new Map(source.details.map((detail) => [detail.id, detail]))
return {
client: source.client,
sessionId: source.sessionId,
messageDetail: {
raw: (id: string) => details.get(id)?.raw,
state: (id: string) => details.get(id)?.state ?? null,
load: source.load
}
}
}, [source])
return (
<MobileNativeChatItemSourceContext.Provider value={value}>
<MobileNativeChatMessage {...props} />
</MobileNativeChatItemSourceContext.Provider>
)
}, equalMobileNativeChatRowContent)
export function MobileNativeChatRow({
message,
index,
lookups,
...outer
}: Pick<
MobileNativeChatRowContentProps,
'scope' | 'fontScale' | 'onOpenFile' | 'structuredActivityUi' | 'onToggleTurn'
> & { message: NativeChatMessage; index: number; lookups: MobileNativeChatRowLookups }) {
const current = useContext(MobileNativeChatRootItemSourceContext)
const props = resolveMobileNativeChatRowProps(index, message, lookups)
const detail = current?.messageDetail
const source: Recovery | null = current
? {
client: current.client,
sessionId: current.sessionId,
load: detail?.load ?? unavailable,
details: (props.workRun ?? [message]).map((member) => {
const raw = detail?.raw(member.id)
return {
id: member.id,
raw: raw?.shortened ? raw : undefined,
state: detail?.state(member.id) ?? null
}
})
}
: null
return <MobileNativeChatRowContent {...props} {...outer} source={source} />
}
async function unavailable(): Promise<void> {
throw new Error('Message unavailable')
}