perf(webui): reduce repeated work and bound history rendering (#5703)
* perf(webui): reduce repeated work and bound history rendering * fix(webui): keep continuation reasoning with its answer * refactor(webui): reuse answer buffers and prepared copy text * perf(webui): restore conditional replay buffer initialization * fix(webui): remove code block pagination
This commit is contained in:
1 parent
1c451b5b5f
commit
1469d4811c
32 files changed
+1156
-361
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { ChevronDown } from "lucide-react";
|
||||
import type { ReactNode, Ref } from "react";
|
||||
import { useState, type ReactNode, type Ref } from "react";
|
||||
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
@@ -30,6 +30,8 @@ export function ThinkingReasoningShell({
|
||||
onToggle,
|
||||
onScroll,
|
||||
}: ThinkingReasoningShellProps) {
|
||||
const [hasExpanded, setHasExpanded] = useState(expanded);
|
||||
if (expanded && !hasExpanded) setHasExpanded(true);
|
||||
return (
|
||||
<div
|
||||
className="flex w-full max-w-[45rem] animate-in flex-col fade-in duration-300 motion-reduce:animate-none"
|
||||
@@ -109,7 +111,7 @@ export function ThinkingReasoningShell({
|
||||
className="mt-1.5 max-h-[180px] overflow-y-auto pr-1 [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"
|
||||
>
|
||||
<div ref={contentRef} className="flex flex-col gap-0.5">
|
||||
{children}
|
||||
{hasExpanded ? children : null}
|
||||
</div>
|
||||
</div>
|
||||
{fadeTop ? (
|
||||
|
||||
@@ -13,33 +13,48 @@ import type { UIMediaAttachment, UIMessage } from "@/lib/types";
|
||||
*/
|
||||
export function coalesceActivityMessages(messages: UIMessage[]): UIMessage[] {
|
||||
const normalized: UIMessage[] = [];
|
||||
const calls = new Map<string, { latest: number; byTurn: Map<string, number> }>();
|
||||
|
||||
for (const message of messages) {
|
||||
const targetIndex = findMergeTarget(normalized, message);
|
||||
let targetIndex = -1;
|
||||
if (message.kind === "trace") {
|
||||
for (const event of message.toolEvents ?? []) {
|
||||
if (!event.call_id) continue;
|
||||
const call = calls.get(event.call_id);
|
||||
if (!call) continue;
|
||||
const index = message.turnId
|
||||
? Math.max(call.byTurn.get(message.turnId) ?? -1, call.byTurn.get("") ?? -1)
|
||||
: call.latest;
|
||||
targetIndex = Math.max(targetIndex, index);
|
||||
}
|
||||
if (targetIndex < 0 && canMergeAdjacentProgress(normalized.at(-1), message)) {
|
||||
targetIndex = normalized.length - 1;
|
||||
}
|
||||
}
|
||||
if (targetIndex < 0) {
|
||||
targetIndex = normalized.length;
|
||||
normalized.push(message);
|
||||
continue;
|
||||
} else {
|
||||
normalized[targetIndex] = mergeTraceMessages(normalized[targetIndex], message);
|
||||
}
|
||||
if (message.kind !== "trace") continue;
|
||||
// Merging preserves the target's turn. Legacy rows without a turn match any turn.
|
||||
const turn = normalized[targetIndex].turnId || "";
|
||||
for (const event of message.toolEvents ?? []) {
|
||||
if (!event.call_id) continue;
|
||||
let call = calls.get(event.call_id);
|
||||
if (!call) {
|
||||
call = { latest: targetIndex, byTurn: new Map() };
|
||||
calls.set(event.call_id, call);
|
||||
}
|
||||
call.latest = Math.max(call.latest, targetIndex);
|
||||
call.byTurn.set(turn, Math.max(call.byTurn.get(turn) ?? -1, targetIndex));
|
||||
}
|
||||
normalized[targetIndex] = mergeTraceMessages(normalized[targetIndex], message);
|
||||
}
|
||||
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function findMergeTarget(messages: UIMessage[], incoming: UIMessage): number {
|
||||
if (incoming.kind !== "trace") return -1;
|
||||
|
||||
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
||||
const previous = messages[index];
|
||||
if (previous.kind !== "trace") continue;
|
||||
if (hasSharedToolCall(previous, incoming) && sameTurn(previous, incoming)) return index;
|
||||
}
|
||||
|
||||
const adjacentIndex = messages.length - 1;
|
||||
const adjacent = messages[adjacentIndex];
|
||||
return canMergeAdjacentProgress(adjacent, incoming) ? adjacentIndex : -1;
|
||||
}
|
||||
|
||||
function canMergeAdjacentProgress(
|
||||
previous: UIMessage | undefined,
|
||||
incoming: UIMessage,
|
||||
@@ -87,15 +102,6 @@ function messageTraces(message: UIMessage): string[] {
|
||||
return message.content.trim() ? [message.content] : [];
|
||||
}
|
||||
|
||||
function hasSharedToolCall(previous: UIMessage, incoming: UIMessage): boolean {
|
||||
const previousCallIds = new Set(
|
||||
(previous.toolEvents ?? []).map((event) => event.call_id).filter(Boolean),
|
||||
);
|
||||
return (incoming.toolEvents ?? []).some((event) => (
|
||||
!!event.call_id && previousCallIds.has(event.call_id)
|
||||
));
|
||||
}
|
||||
|
||||
function hasSharedTrace(previous: UIMessage, incoming: UIMessage): boolean {
|
||||
const previousTraces = new Set(messageTraces(previous).map(canonicalToolTrace));
|
||||
return messageTraces(incoming).some((trace) => previousTraces.has(canonicalToolTrace(trace)));
|
||||
|
||||
Reference in new issue
Block a user