Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,86 @@ describe("ChatThreadPanel", () => {
expect(scrollAnchor).toBeInTheDocument();
});

it("virtualizes a 2000-turn history to a bounded number of mounted turns", () => {
const messages: AgentMessageSnapshot[] = [];
for (let index = 0; index < 2000; index += 1) {
messages.push({
id: `user-${index}`,
role: "user",
text: `Question ${index}`,
status: "complete",
timestamp: index * 2,
});
messages.push({
id: `assistant-${index}`,
role: "assistant",
text: `Answer ${index}`,
status: "complete",
timestamp: index * 2 + 1,
});
}

const { container } = render(
<TooltipProvider>
<ChatThreadPanel
threadTitle="Signal"
messages={messages}
isStreaming={false}
lastError={null}
/>
</TooltipProvider>,
);

const mountedTurns = container.querySelectorAll("[data-turn-id]");
expect(mountedTurns.length).toBeGreaterThan(0);
expect(mountedTurns.length).toBeLessThanOrEqual(30);
expect(
container.querySelector('[data-message-id="assistant-1999"]'),
).not.toBeNull();
expect(
container.querySelector('[data-message-id="assistant-0"]'),
).toBeNull();
});

it("reveals a targeted message inside an unmounted turn (search reveal)", async () => {
const messages: AgentMessageSnapshot[] = [];
for (let index = 0; index < 2000; index += 1) {
messages.push({
id: `user-${index}`,
role: "user",
text: `Question ${index}`,
status: "complete",
timestamp: index * 2,
});
messages.push({
id: `assistant-${index}`,
role: "assistant",
text: `Answer ${index}`,
status: "complete",
timestamp: index * 2 + 1,
});
}
const onTargetMessageNavigated = vi.fn();

render(
<TooltipProvider>
<ChatThreadPanel
threadTitle="Signal"
messages={messages}
isStreaming={false}
lastError={null}
targetMessageId="assistant-123"
onTargetMessageNavigated={onTargetMessageNavigated}
/>
</TooltipProvider>,
);

await waitFor(() => {
expect(onTargetMessageNavigated).toHaveBeenCalledWith("assistant-123");
});
expect(HTMLElement.prototype.scrollIntoView).toHaveBeenCalled();
});

it("scrolls to a targeted message when message navigation is requested", async () => {
const onTargetMessageNavigated = vi.fn();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from "react";
import { StreamingIndicator } from "@/components/ui/activity-indicator";
import { ScrollButton } from "@/components/ui/scroll-button";
import { toast } from "@/lib/toast";
import { buildChatTurns } from "./chat/chat-thread-turns";
import { createChatTurnsBuilder } from "./chat/chat-thread-turns";
import type { InlineModelPickerProps } from "./chat/thread-header";
import {
ChatThreadTranscript,
Expand All @@ -13,6 +13,8 @@ import {

export interface ChatThreadPanelProps {
threadTitle: string;
/** Stable thread identity, used to preserve scroll position per thread. */
threadId?: string | null;
messages: AgentMessageSnapshot[];
isStreaming: boolean;
isLoading?: boolean;
Expand Down Expand Up @@ -46,6 +48,7 @@ export interface ChatThreadPanelProps {

export function ChatThreadPanel({
threadTitle: _threadTitle,
threadId,
messages,
isStreaming,
isLoading,
Expand Down Expand Up @@ -106,7 +109,13 @@ export function ChatThreadPanel({
[messages],
);

const turns = React.useMemo(() => buildChatTurns(messages), [messages]);
// One builder per mounted panel: unchanged turns keep their identity
// across streaming updates (issue #11).
const turnsBuilderRef = React.useRef(createChatTurnsBuilder());
const turns = React.useMemo(
() => turnsBuilderRef.current.build(messages),
[messages],
);

React.useEffect(() => {
if (!targetMessageId) {
Expand Down Expand Up @@ -158,6 +167,8 @@ export function ChatThreadPanel({
isLoading={isLoading}
isStreaming={isStreaming}
lastError={lastError}
targetMessageId={targetMessageId}
threadId={threadId}
onCopyMessage={handleCopyMessage}
onRetryLastUserMessage={onRetryLastUserMessage}
onResubmitUserMessage={onResubmitUserMessage}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import {
cn,
} from "@pi-desktop/ui";
import { Skeleton } from "boneyard-js/react";
import * as React from "react";
import type { ActivityIndicatorProps } from "@/components/ui/activity-indicator";
import { SystemMessage } from "@/components/ui/system-message";
import { describeChatError } from "./chat/chat-error";
import type { ChatThreadTurn } from "./chat/chat-thread-turns";
import { ResponseDivider } from "./chat/response-divider";
import { ChatThreadTranscriptTurn } from "./chat-thread-transcript-turn";
import { ChatThreadTurnSlot } from "./chat-thread-turn-slot";
import { ChatThreadVirtualTurnList } from "./chat-thread-virtual-turn-list";

const CHAT_SUGGESTIONS: ReadonlyArray<string> = [
"Explain this repo",
Expand Down Expand Up @@ -43,6 +43,8 @@ export interface ChatThreadTranscriptProps {
onRetryLastUserMessage?: (text: string) => void;
onResubmitUserMessage?: (messageId: string, nextText: string) => void;
getMessageTokens?: (messageId: string) => number | null | undefined;
targetMessageId?: string | null;
threadId?: string | null;
}

export function detectRunningActivities(
Expand Down Expand Up @@ -103,6 +105,8 @@ export function ChatThreadTranscript({
onRetryLastUserMessage,
onResubmitUserMessage,
getMessageTokens,
targetMessageId,
threadId,
}: ChatThreadTranscriptProps) {
const lastTurn = turns.length > 0 ? turns[turns.length - 1] : undefined;
const lastTurnDividerWorking =
Expand Down Expand Up @@ -186,30 +190,25 @@ export function ChatThreadTranscript({
</div>
) : null}

{turns.length > 0
? turns.map((turn, turnIndex) => {
const isLastTurn = turnIndex === turns.length - 1;
const failedLastUser =
isLastTurn && canRetryLastUser && turn.userMessage !== null;

return (
<React.Fragment key={turn.id}>
<ChatThreadTranscriptTurn
turn={turn}
onCopyMessage={onCopyMessage}
getMessageTokens={getMessageTokens}
isFailedLastUser={failedLastUser}
onRetryLastUserMessage={
failedLastUser ? onRetryLastUserMessage : undefined
}
onResubmitUserMessage={
isLastTurn ? onResubmitUserMessage : undefined
}
/>
</React.Fragment>
);
})
: null}
{turns.length > 0 ? (
<ChatThreadVirtualTurnList
turns={turns}
isStreaming={isStreaming}
targetMessageId={targetMessageId}
scrollStoreKey={threadId}
renderTurn={(turn, turnIndex) => (
<ChatThreadTurnSlot
turn={turn}
isLastTurn={turnIndex === turns.length - 1}
canRetryLastUser={canRetryLastUser}
onCopyMessage={onCopyMessage}
onRetryLastUserMessage={onRetryLastUserMessage}
onResubmitUserMessage={onResubmitUserMessage}
getMessageTokens={getMessageTokens}
/>
)}
/>
) : null}

{lastError && errorPresentation ? (
<div
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import type { ChatThreadTurn } from "./chat/chat-thread-turns";
import { ChatThreadTranscriptTurn } from "./chat-thread-transcript-turn";

export interface ChatThreadTurnSlotProps {
turn: ChatThreadTurn;
isLastTurn: boolean;
canRetryLastUser: boolean;
onCopyMessage: (text: string) => void;
onRetryLastUserMessage?: (text: string) => void;
onResubmitUserMessage?: (messageId: string, nextText: string) => void;
getMessageTokens?: (messageId: string) => number | null | undefined;
}

/**
* One transcript turn with the transcript-level actions wired in: retry
* is only offered on a failed last user message, and edit/resubmit only
* on the last turn. Used as the render slot of the virtual turn list.
*/
export function ChatThreadTurnSlot({
turn,
isLastTurn,
canRetryLastUser,
onCopyMessage,
onRetryLastUserMessage,
onResubmitUserMessage,
getMessageTokens,
}: ChatThreadTurnSlotProps) {
const failedLastUser =
isLastTurn && canRetryLastUser && turn.userMessage !== null;

return (
<ChatThreadTranscriptTurn
turn={turn}
onCopyMessage={onCopyMessage}
getMessageTokens={getMessageTokens}
isFailedLastUser={failedLastUser}
onRetryLastUserMessage={
failedLastUser ? onRetryLastUserMessage : undefined
}
onResubmitUserMessage={isLastTurn ? onResubmitUserMessage : undefined}
/>
);
}
Loading
Loading