Optimize chat rendering & add web session activity tracking (#214)

* feat: add chat virtualization and turn grouping infrastructure

Implement VirtualMessageList with virtualization and overscan for smooth scrolling
Refactor MessageList to render only visible messages and pass scroll refs
Introduce TurnGroupingContext and provider to stabilize per-turn rendering and reduce re-renders

* feat: add web server session activity endpoint for visibility restore

Add /api/session-activity endpoint to expose tracked session activity
Use web server activity first when restoring visibility, with fallback to global status
Update server SSE to set session phase on activity events

* feat(chat): split TurnGroupingContext into UI/Streaming contexts

Add separate UI state and streaming contexts to reduce re-renders.
Skip animations for items visible in collapsed view when expanding.
Track shown parts in collapsed mode to fade in progressively

* feat(ui): cache turn groups with expand state and guard web activity

Add isExpanded to the turn cache key to trigger updates correctly
Expose isGroupExpanded from UI state so groups reflect expand/collapse
Limit web server session activity fetch to web runtime only
This commit is contained in:
Bohdan Triapitsyn
2026-01-25 19:16:26 +02:00
committed by GitHub
parent adbc5af95f
commit f34027df10
20 changed files with 1000 additions and 170 deletions
+37 -34
View File
@@ -96,6 +96,8 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
currentSessionId: state.currentSessionId,
getAgentModelForSession: state.getAgentModelForSession,
getSessionModelSelection: state.getSessionModelSelection,
revertToMessage: state.revertToMessage,
forkFromMessage: state.forkFromMessage,
}))
);
@@ -105,11 +107,17 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
currentSessionId,
getAgentModelForSession,
getSessionModelSelection,
revertToMessage,
forkFromMessage,
} = sessionState;
const providers = useConfigStore((state) => state.providers);
const showReasoningTraces = useUIStore((state) => state.showReasoningTraces);
const toolCallExpansion = useUIStore((state) => state.toolCallExpansion);
const { showReasoningTraces, toolCallExpansion } = useUIStore(
useShallow((state) => ({
showReasoningTraces: state.showReasoningTraces,
toolCallExpansion: state.toolCallExpansion,
}))
);
React.useEffect(() => {
if (currentSessionId) {
@@ -136,11 +144,11 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
const sessionId = message.info.sessionID;
// Subscribe to context changes so badges update immediately on mode switches.
const currentContextAgent = useContextStore(
(state) => (sessionId ? state.currentAgentContext.get(sessionId) : undefined)
);
const savedSessionAgentSelection = useContextStore(
(state) => (sessionId ? state.sessionAgentSelections.get(sessionId) : undefined)
const { currentContextAgent, savedSessionAgentSelection } = useContextStore(
useShallow((state) => ({
currentContextAgent: sessionId ? state.currentAgentContext.get(sessionId) : undefined,
savedSessionAgentSelection: sessionId ? state.sessionAgentSelections.get(sessionId) : undefined,
}))
);
const normalizedParts = React.useMemo(() => {
@@ -566,30 +574,28 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
}, []);
const userMessageIdForTurn = turnGroupingContext?.turnId;
const assistantSummaryFromStore = useMessageStore((state) => {
if (!userMessageIdForTurn) return undefined;
const sessionId = message.info.sessionID;
if (!sessionId) return undefined;
const sessionMessages = state.messages.get(sessionId);
if (!sessionMessages) return undefined;
const userMsg = sessionMessages.find((entry) => entry.info?.id === userMessageIdForTurn);
if (!userMsg) return undefined;
const summary = (userMsg.info as { summary?: { body?: string | null | undefined } | null | undefined }).summary;
const body = summary?.body;
return typeof body === 'string' && body.trim().length > 0 ? body : undefined;
});
const variantFromTurnStore = useMessageStore((state) => {
if (!userMessageIdForTurn) return undefined;
const sessionId = message.info.sessionID;
if (!sessionId) return undefined;
const sessionMessages = state.messages.get(sessionId);
if (!sessionMessages) return undefined;
const userMsg = sessionMessages.find((entry) => entry.info?.id === userMessageIdForTurn);
if (!userMsg) return undefined;
const variant = (userMsg.info as { variant?: unknown }).variant;
return typeof variant === 'string' && variant.trim().length > 0 ? variant : undefined;
});
const { assistantSummaryFromStore, variantFromTurnStore } = useMessageStore(
useShallow((state) => {
if (!userMessageIdForTurn || !message.info.sessionID) {
return { assistantSummaryFromStore: undefined, variantFromTurnStore: undefined };
}
const sessionMessages = state.messages.get(message.info.sessionID);
if (!sessionMessages) {
return { assistantSummaryFromStore: undefined, variantFromTurnStore: undefined };
}
const userMsg = sessionMessages.find((entry) => entry.info?.id === userMessageIdForTurn);
if (!userMsg) {
return { assistantSummaryFromStore: undefined, variantFromTurnStore: undefined };
}
const summary = (userMsg.info as { summary?: { body?: string | null | undefined } | null | undefined }).summary;
const body = summary?.body;
const variant = (userMsg.info as { variant?: unknown }).variant;
return {
assistantSummaryFromStore: typeof body === 'string' && body.trim().length > 0 ? body : undefined,
variantFromTurnStore: typeof variant === 'string' && variant.trim().length > 0 ? variant : undefined,
};
})
);
const headerVariantRaw = !isUser ? (variantFromTurnStore ?? previousUserMetadata?.variant) : undefined;
@@ -699,9 +705,6 @@ const ChatMessage: React.FC<ChatMessageProps> = ({
setTimeout(() => setCopiedMessage(false), 2000);
}, [copyTextToClipboard, messageTextContent]);
const revertToMessage = useSessionStore((state) => state.revertToMessage);
const forkFromMessage = useSessionStore((state) => state.forkFromMessage);
const handleRevert = React.useCallback(() => {
if (!sessionId || !message.info.id) return;
revertToMessage(sessionId, message.info.id);