fix(webui): admit history without dropping live output (NAN-311)

This commit is contained in:
chengyongru authored and chengyongru committed 2026-10-22 01:22:49 +08:00
1 parent df63526ddf
commit b37a23698d
21 files changed
+3026 -1607

No files matched your search

+40 -661
View File
@@ -33,7 +33,6 @@ import { ThreadLoadingSkeleton } from "@/components/thread/ThreadLoadingSkeleton
import { StreamErrorNotice } from "@/components/thread/StreamErrorNotice";
import { ThreadViewport, type ThreadViewportHandle } from "@/components/thread/ThreadViewport";
import { useNanobotStream, type SendAttachment, type SendOptions } from "@/hooks/useNanobotStream";
import { useSessionHistory } from "@/hooks/useSessions";
import { useFilePreviewState, type FilePreviewState, type FilePreviewStore } from "@/hooks/useFilePreviewState";
import {
ApiError,
@@ -45,7 +44,6 @@ import {
fetchSettings,
fetchWorkspaces,
fetchWorkspaceDirectories,
fetchWebuiThreadTraceDetail,
listSlashCommands,
} from "@/lib/api";
import {
@@ -58,7 +56,7 @@ import {
installedMcpPresetsFromPayload,
isMcpPresetsPayload,
} from "@/lib/mcp-preset-events";
import type { CanonicalRunSnapshot, StreamError } from "@/lib/nanobot-client";
import type { StreamError } from "@/lib/nanobot-client";
import type {
ChatSummary,
FilePreviewPayload,
@@ -75,49 +73,10 @@ import type {
import { projectThreadEvents } from "@/lib/thread-event-projection";
import { projectWebuiThreadMessages } from "@/lib/thread-display-projection";
import { ThreadMessageCache } from "@/lib/thread-message-cache";
import { copyThreadCheckpoint, createThreadCheckpoint, type ThreadCheckpoint } from "@/lib/thread-checkpoint";
import { canonicalRunSnapshot, completedAssistantTurnIds } from "@/lib/canonical-run-snapshot";
import { providerDisplayLabel } from "@/lib/provider-brand";
import { cn } from "@/lib/utils";
import { useClient } from "@/providers/ClientProvider";
type MessageShape = Pick<UIMessage, "role" | "kind" | "content" | "isStreaming" | "turnId">;
interface PendingCanonicalHydrate {
source: "initial" | "refresh";
historyLineage: number;
historyVersion: number;
runGeneration: number;
uiBaseline: MessageShape[];
uiLineage: number | null;
uiRevision: number;
}
interface PendingHistoryLineageCommit {
lineage: number;
messages: UIMessage[];
}
interface PendingCanonicalCommit {
canonicalSnapshot: CanonicalRunSnapshot;
completedTurnIds: string[];
expectedUiRevision: number;
historyLineage: number;
historyVersion: number;
hydrate: PendingCanonicalHydrate;
messages: UIMessage[];
previousMessages: UIMessage[];
}
function sameMessageShape(a: MessageShape, b: MessageShape): boolean {
return (
a.role === b.role
&& (a.kind ?? "") === (b.kind ?? "")
&& a.content === b.content
&& (!a.turnId || !b.turnId || a.turnId === b.turnId)
);
}
function latestComposerContextUsage(messages: UIMessage[]): ComposerContextUsage | null {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
@@ -199,140 +158,6 @@ function recentComposerRoundUsage(messages: UIMessage[]): ComposerRoundUsage[] {
return recent.reverse();
}
function snapshotPreservesMessage(
current: MessageShape,
candidate: MessageShape,
allowCompletedTurnReplacement: boolean,
): boolean {
if (sameMessageShape(current, candidate)) return true;
if (
allowCompletedTurnReplacement
&& current.role === "assistant"
&& candidate.role === current.role
&& (candidate.kind ?? "") === (current.kind ?? "")
&& !!current.turnId
&& candidate.turnId === current.turnId
) {
return true;
}
return (
current.role === "assistant"
&& current.isStreaming === true
&& candidate.role === current.role
&& (candidate.kind ?? "") === (current.kind ?? "")
&& (!current.turnId || !candidate.turnId || candidate.turnId === current.turnId)
&& candidate.content.startsWith(current.content)
);
}
function durableMessageShape(message: UIMessage): MessageShape | null {
if (message.kind === "trace") return null;
if (message.role !== "user" && message.role !== "assistant") return null;
if (message.role === "assistant" && !message.content.trim() && !message.media?.length) {
return null;
}
return {
role: message.role,
kind: message.kind,
content: message.content,
isStreaming: message.isStreaming,
turnId: message.turnId,
};
}
function durableMessageShapes(messages: UIMessage[]): MessageShape[] {
return messages
.map(durableMessageShape)
.filter((message): message is MessageShape => message !== null);
}
function preservesMessageShapes(
expected: MessageShape[],
candidates: MessageShape[],
allowCompletedTurnReplacement: boolean,
): boolean {
let cursor = 0;
let previousCandidate: MessageShape | null = null;
for (const message of expected) {
if (
allowCompletedTurnReplacement
&& previousCandidate?.role === "assistant"
&& message.role === "assistant"
&& !!message.turnId
&& message.turnId === previousCandidate.turnId
) {
// A delayed websocket delta can briefly create a second bubble after an
// HTTP completion snapshot. The completed replay is authoritative for
// that turn, so both local fragments may map to its single assistant row.
continue;
}
let found = false;
while (cursor < candidates.length) {
const candidate = candidates[cursor];
cursor += 1;
if (snapshotPreservesMessage(message, candidate, allowCompletedTurnReplacement)) {
found = true;
previousCandidate = candidate;
break;
}
}
if (!found) return false;
}
return true;
}
function preservesDurableMessages(
current: UIMessage[],
snapshot: UIMessage[],
allowCompletedTurnReplacement = false,
): boolean {
// Canonical history refreshes can race with live websocket messages after fork/send.
// Never accept a refreshed snapshot that drops a user/assistant message already shown.
const expected = durableMessageShapes(current);
if (expected.length === 0) return true;
return preservesMessageShapes(
expected,
durableMessageShapes(snapshot),
allowCompletedTurnReplacement,
);
}
function resetDropsPostRequestDurableTail(
baseline: MessageShape[],
current: UIMessage[],
snapshot: UIMessage[],
): boolean {
const currentDurable = durableMessageShapes(current);
let stablePrefixLength = 0;
while (
stablePrefixLength < baseline.length
&& stablePrefixLength < currentDurable.length
&& sameMessageShape(baseline[stablePrefixLength], currentDurable[stablePrefixLength])
) {
stablePrefixLength += 1;
}
const postRequestTail = currentDurable.slice(stablePrefixLength);
if (postRequestTail.length === 0) return false;
return !preservesMessageShapes(
postRequestTail,
durableMessageShapes(snapshot),
true,
);
}
function isStaleThreadSnapshot(
current: UIMessage[],
snapshot: UIMessage[],
allowCompletedTurnReplacement = false,
): boolean {
if (current.length === 0) return false;
if (snapshot.length === 0) return true;
if (!preservesDurableMessages(current, snapshot, allowCompletedTurnReplacement)) return true;
if (snapshot.length >= current.length) return false;
if (allowCompletedTurnReplacement) return false;
return snapshot.every((message, index) => sameMessageShape(current[index], message));
}
function latestActiveTurnId(messages: UIMessage[], runStartedAt: number | null): string | null {
for (let index = messages.length - 1; index >= 0; index -= 1) {
const message = messages[index];
@@ -363,122 +188,6 @@ function hasInlineDeliveryError(
));
}
function replaceCanonicalCompletedTurns(
current: UIMessage[],
canonical: UIMessage[],
completedTurnIds: readonly string[],
): UIMessage[] {
const completed = new Set(completedTurnIds);
const represented = new Set(canonical.flatMap((message) => (
message.turnId && completed.has(message.turnId) ? [message.turnId] : []
)));
if (!current.some((message) => message.turnId && represented.has(message.turnId))) {
return current;
}
const retained: UIMessage[] = [];
const originalPositions = new Map<string, number>();
for (const message of current) {
if (message.turnId && represented.has(message.turnId)) {
if (!originalPositions.has(message.turnId)) {
originalPositions.set(message.turnId, retained.length);
}
} else {
retained.push(message);
}
}
// Supplemental inputs may have their own turn identity between two answer
// segments. Match those retained rows as anchors so completed rows keep the
// canonical chronology instead of being grouped at the first turn row.
type AnchorQueue = { indices: number[]; offset: number };
type ShapeAnchors = {
all: AnchorQueue;
byTurnId: Map<string | undefined, AnchorQueue>;
};
const anchorIndex = new Map<string, Map<string, ShapeAnchors>>();
for (let index = 0; index < retained.length; index += 1) {
const message = retained[index];
const shape = JSON.stringify([message.role, message.kind ?? ""]);
let byContent = anchorIndex.get(shape);
if (!byContent) {
byContent = new Map();
anchorIndex.set(shape, byContent);
}
let positions = byContent.get(message.content);
if (!positions) {
positions = {
all: { indices: [], offset: 0 },
byTurnId: new Map(),
};
byContent.set(message.content, positions);
}
positions.all.indices.push(index);
const turnId = message.turnId || undefined;
const queue = positions.byTurnId.get(turnId) ?? { indices: [], offset: 0 };
queue.indices.push(index);
positions.byTurnId.set(turnId, queue);
}
let cursor = 0;
let fallbackBoundary = 0;
const firstAnchor = (queue: AnchorQueue | undefined, minimumIndex: number): number => {
if (!queue) return retained.length;
while (queue.offset < queue.indices.length && queue.indices[queue.offset] < minimumIndex) {
queue.offset += 1;
}
return queue.indices[queue.offset] ?? retained.length;
};
const anchors = canonical.map((message) => {
if (message.turnId && represented.has(message.turnId)) {
// Keep anonymous matches after this turn's retained pagination prefix.
fallbackBoundary = Math.max(fallbackBoundary, originalPositions.get(message.turnId) ?? 0);
return -1;
}
const positions = anchorIndex
.get(JSON.stringify([message.role, message.kind ?? ""]))
?.get(message.content);
const exactIndex = message.turnId
? firstAnchor(positions?.byTurnId.get(message.turnId), cursor)
: retained.length;
const index = exactIndex < retained.length
? exactIndex
: firstAnchor(
message.turnId ? positions?.byTurnId.get(undefined) : positions?.all,
Math.max(cursor, fallbackBoundary),
);
if (index === retained.length) return -1;
cursor = index + 1;
return index;
});
const nextAnchors = new Array<number | undefined>(anchors.length);
let nextAnchor: number | undefined;
for (let index = anchors.length - 1; index >= 0; index -= 1) {
nextAnchors[index] = nextAnchor;
if (anchors[index] >= 0) nextAnchor = anchors[index];
}
const insertions = new Map<number, UIMessage[]>();
let precedingAnchor = 0;
let previousInsertionPosition = 0;
for (let index = 0; index < canonical.length; index += 1) {
if (anchors[index] >= 0) precedingAnchor = anchors[index] + 1;
const message = canonical[index];
if (!message.turnId || !represented.has(message.turnId)) continue;
const position = Math.max(
previousInsertionPosition,
nextAnchors[index] ?? Math.max(
precedingAnchor,
originalPositions.get(message.turnId) ?? precedingAnchor,
),
);
previousInsertionPosition = position;
const rows = insertions.get(position) ?? [];
rows.push(message);
insertions.set(position, rows);
}
return retained.flatMap((message, index) => [
...insertions.get(index) ?? [],
message,
]).concat(insertions.get(retained.length) ?? []);
}
const FILE_PREVIEW_DEFAULT_WIDTH = 544;
const FILE_PREVIEW_MIN_WIDTH = 360;
const FILE_PREVIEW_MAX_WIDTH = 860;
@@ -782,25 +491,6 @@ export function ThreadShell({
() => sessions.filter((candidate) => candidate.key !== historyKey),
[historyKey, sessions],
);
const {
messages: historical,
loading,
error: historyError,
loadingOlder,
olderError,
loadOlder,
hasMoreBefore,
userMessageOffset,
hasPendingToolCalls,
completedTurnIds,
completionIdsProvided,
continuity: historyContinuity,
lineage: historyLineage,
activeTurnId: historyActiveTurnId,
refresh: refreshHistory,
version: historyVersion,
forkBoundaryMessageCount,
} = useSessionHistory(historyKey);
const { client, getToken, ingressLimits, modelName, token, webuiCapabilities } = useClient();
const favoriteWorkspaceProject = useCallback(
async (path: string, pinned: boolean) => {
@@ -884,37 +574,12 @@ export function ThreadShell({
));
const messageCacheRef = useRef(messageCache ?? localMessageCacheRef.current);
messageCacheRef.current = messageCache ?? localMessageCacheRef.current;
const pendingCanonicalHydrateRef = useRef<Map<string, PendingCanonicalHydrate>>(new Map());
const pendingCanonicalCommitRef = useRef<Map<string, PendingCanonicalCommit>>(new Map());
const pendingHistoryLineageCommitRef = useRef<Map<string, PendingHistoryLineageCommit>>(
new Map(),
);
const completedCanonicalHydrateVersionRef = useRef<Map<string, number>>(new Map());
const committedHistoryLineageRef = useRef<Map<string, number>>(new Map());
const sessionKeyByChatIdRef = useRef<Map<string, string>>(new Map());
const traceDetailRequestsRef = useRef<Map<string, Promise<void>>>(new Map());
const activeHistoryKeyRef = useRef(historyKey);
activeHistoryKeyRef.current = historyKey;
const currentUiMessagesRef = useRef<UIMessage[] | null>(null);
const uiRevisionRef = useRef(0);
const olderPageStartRef = useRef<{ historyKey: string; client: typeof client; visibleFirstId: string | null } | null>(null);
const showTemporaryChatControl =
!hideHeader && !session && !loading && !!onTemporaryChatEnabledChange;
const initial = useMemo(() => {
const cached = chatId ? messageCacheRef.current.get(chatId) : undefined;
return cached
? copyThreadCheckpoint(cached, replaceCanonicalCompletedTurns(cached.projection.messages, historical, completedTurnIds))
: createThreadCheckpoint(historical);
}, [chatId, completedTurnIds, historical]);
const handleTurnEnd = useCallback(() => {
if (chatId) activeViewportTurnByChatIdRef.current.delete(chatId);
setSubmittedViewportTurnId(null);
onTurnEnd?.();
}, [chatId, onTurnEnd]);
const handleStreamDetach = useCallback((checkpoint: ThreadCheckpoint) => {
if (chatId) messageCacheRef.current.set(chatId, checkpoint);
}, [chatId]);
const {
messages,
messagesReady,
@@ -928,104 +593,46 @@ export function ThreadShell({
send,
transcribeAudio,
stop,
reconcileTurnComplete,
setMessages,
getCheckpoint,
history: {
loading,
error: historyError,
loadingOlder,
olderError,
hasMoreBefore,
userMessageOffset,
hasPendingToolCalls,
completedTurnIds,
activeTurnId: historyActiveTurnId,
version: historyVersion,
forkBoundaryMessageCount,
},
refreshHistory,
loadOlder,
loadTraceDetails: loadBodyTraceDetails,
streamError,
dismissStreamError,
} = useNanobotStream(
chatId, initial.projection.messages, hasPendingToolCalls, handleTurnEnd, handleStreamDetach, completedTurnIds, initial,
chatId, undefined, false, handleTurnEnd, undefined, undefined,
{ key: historyKey, cache: messageCacheRef.current },
);
const displayMessages = useMemo(() => projectWebuiThreadMessages(messages), [messages]);
const normalizedHistory = useMemo(() => projectWebuiThreadMessages(historical), [historical]);
// The response can arrive one commit before its messages reach the viewport.
const initialHistoryLoading = loading || (normalizedHistory.length > 0 && displayMessages.length === 0);
const initialHistoryLoading = loading;
const showTemporaryChatControl =
!hideHeader && !session && !loading && !!onTemporaryChatEnabledChange;
const promptNavigation = usePromptHistory(historyKey, historyVersion, displayMessages);
const updateNavigationMessages = promptNavigation.updateMessages;
const olderPageProjectionPending = Boolean(
olderPageStartRef.current?.historyKey === historyKey
&& olderPageStartRef.current.client === client
&& !loadingOlder
&& !olderError
&& normalizedHistory[0]?.id !== olderPageStartRef.current.visibleFirstId
&& displayMessages[0]?.id !== normalizedHistory[0]?.id,
);
useLayoutEffect(() => {
const pending = olderPageStartRef.current;
if (pending && (pending.historyKey !== historyKey || pending.client !== client)) {
olderPageStartRef.current = null;
}
if (!historyKey) {
olderPageStartRef.current = null;
} else if (loadingOlder) {
// The request may finish before this shell commits the rendered projection.
if (!olderPageStartRef.current) {
olderPageStartRef.current = {
historyKey,
client,
visibleFirstId: normalizedHistory[0]?.id ?? null,
};
}
} else if (olderError || !olderPageProjectionPending) {
olderPageStartRef.current = null;
}
}, [client, displayMessages, historyKey, loadingOlder, olderError, olderPageProjectionPending, normalizedHistory]);
const loadTraceDetails = useCallback(async (refs: string[]) => {
const requestKey = historyKey;
if (!requestKey) return;
const requests: Promise<void>[] = [];
for (const ref of refs) {
const requestId = `${requestKey}:${ref}`;
const existing = traceDetailRequestsRef.current.get(requestId);
if (existing) {
requests.push(existing);
continue;
}
const request = fetchWebuiThreadTraceDetail(getToken(), requestKey, ref)
.then((detail) => {
if (activeHistoryKeyRef.current !== requestKey) return;
const projected = projectThreadEvents(detail.events);
const hydrate = (current: UIMessage[]) => current.flatMap((message) => {
if (message.traceDetail?.ref !== ref) return [message];
return projected.map((replacement) => ({
...replacement,
activitySegmentId: message.activitySegmentId ?? replacement.activitySegmentId,
}));
});
setMessages(hydrate);
updateNavigationMessages(hydrate);
})
.catch((error: unknown) => {
if (activeHistoryKeyRef.current !== requestKey) return;
throw error;
})
.finally(() => {
traceDetailRequestsRef.current.delete(requestId);
});
traceDetailRequestsRef.current.set(requestId, request);
requests.push(request);
const details = await loadBodyTraceDetails(refs);
for (const detail of details) {
const projected = projectThreadEvents(detail.events);
updateNavigationMessages((current) => current.flatMap((message) => (
message.traceDetail?.ref === detail.ref
? projected.map((row) => ({ ...row, activitySegmentId: message.activitySegmentId }))
: [message]
)));
}
await Promise.all(requests);
}, [getToken, historyKey, setMessages, updateNavigationMessages]);
useEffect(() => () => {
activeHistoryKeyRef.current = null;
}, []);
useLayoutEffect(() => {
if (currentUiMessagesRef.current === messages) return;
currentUiMessagesRef.current = messages;
uiRevisionRef.current += 1;
if (!chatId) return;
const lineageCommit = pendingHistoryLineageCommitRef.current.get(chatId);
if (!lineageCommit) return;
pendingHistoryLineageCommitRef.current.delete(chatId);
if (lineageCommit.messages === messages) {
committedHistoryLineageRef.current.set(chatId, lineageCommit.lineage);
}
}, [chatId, messages]);
}, [loadBodyTraceDetails, updateNavigationMessages]);
useEffect(() => {
if (chatId && historyKey) sessionKeyByChatIdRef.current.set(chatId, historyKey);
@@ -1282,231 +889,15 @@ export function ThreadShell({
});
}, [client, refreshModelSettings]);
useEffect(() => {
if (!historyKey || !chatId) return;
pendingCanonicalHydrateRef.current.set(chatId, {
source: "initial",
historyLineage,
historyVersion,
runGeneration: client.getRunGeneration(chatId),
uiBaseline: durableMessageShapes(initial.projection.messages),
uiLineage: committedHistoryLineageRef.current.get(chatId) ?? null,
uiRevision: uiRevisionRef.current,
});
// Capture the request's run generation when entering a conversation, before
// its history response can race with another send or running event.
}, [chatId, client, historyKey]);
const canonicalCompletedTurnIds = useMemo(() => Array.from(new Set([
...completedTurnIds,
...(!completionIdsProvided && !hasPendingToolCalls && historyActiveTurnId === null
? completedAssistantTurnIds(normalizedHistory)
: []),
])), [completedTurnIds, completionIdsProvided, hasPendingToolCalls, normalizedHistory, historyActiveTurnId]);
const lastRunTurnId = chatId ? client.getRunTurnId(chatId) : null;
const completedRunTurnId = chatId && lastRunTurnId && client.isRunCompleted(chatId, lastRunTurnId)
? lastRunTurnId : null;
const queueCompletedTurnIds = useMemo(() => {
const canonical = hasPendingToolCalls ? [] : canonicalCompletedTurnIds;
const canonical = hasPendingToolCalls ? [] : completedTurnIds;
// Live terminal facts can release the queue before the history refresh finishes.
return completedRunTurnId && !canonical.includes(completedRunTurnId)
? [...canonical, completedRunTurnId] : canonical;
}, [canonicalCompletedTurnIds, completedRunTurnId, hasPendingToolCalls]);
useEffect(() => {
if (!historyKey || !chatId || loading) return;
const pendingCanonicalHydrate = pendingCanonicalHydrateRef.current.get(chatId);
const shouldReconcileRun = (
pendingCanonicalHydrate !== undefined
&& historyVersion > pendingCanonicalHydrate.historyVersion
&& !hasPendingToolCalls
&& (
pendingCanonicalHydrate.source === "refresh"
|| (
(historical.length > 0 || completedTurnIds.length > 0)
&& (isStreaming || runStartedAt !== null || client.hasUnsettledRun(chatId))
)
)
);
// Completion is a fact about a turn, even when a newer local turn prevents
// this snapshot from settling the conversation's current run.
client.fenceCanonicalCompletedTurns(chatId, canonicalCompletedTurnIds);
const keepLiveMessages = (current: UIMessage[]) => {
const reconciled = projectWebuiThreadMessages(
replaceCanonicalCompletedTurns(current, normalizedHistory, canonicalCompletedTurnIds),
);
const firstId = reconciled[0]?.id;
const prefixLength = normalizedHistory.findIndex((message) => message.id === firstId);
return prefixLength > 0
? [...normalizedHistory.slice(0, prefixLength), ...reconciled]
: reconciled;
};
if (shouldReconcileRun && pendingCanonicalHydrate) {
const authoritativeReset = (
pendingCanonicalHydrate.uiLineage !== null
&& historyLineage !== pendingCanonicalHydrate.uiLineage
&& (
historyContinuity === "reset"
|| (
pendingCanonicalHydrate.source === "refresh"
&& historyContinuity === "overlap"
&& historyLineage === pendingCanonicalHydrate.historyLineage
)
)
);
const responseUiRevision = uiRevisionRef.current;
const resetDropsRenderedTail = (
authoritativeReset
&& responseUiRevision !== pendingCanonicalHydrate.uiRevision
&& resetDropsPostRequestDurableTail(
pendingCanonicalHydrate.uiBaseline,
messages,
normalizedHistory,
)
);
if (
authoritativeReset
? resetDropsRenderedTail
: isStaleThreadSnapshot(messages, normalizedHistory, true)
) {
setMessages((current) => keepLiveMessages(current));
return;
}
const canonicalSnapshot = canonicalRunSnapshot(
normalizedHistory,
hasPendingToolCalls,
historyActiveTurnId,
completionIdsProvided,
);
if (!client.canReconcileCanonicalCompletion(
chatId,
pendingCanonicalHydrate.runGeneration,
canonicalCompletedTurnIds,
canonicalSnapshot,
)) {
setMessages((current) => keepLiveMessages(current));
return;
}
pendingCanonicalCommitRef.current.set(chatId, {
canonicalSnapshot,
completedTurnIds: canonicalCompletedTurnIds,
expectedUiRevision: responseUiRevision + 1,
historyLineage,
historyVersion,
hydrate: pendingCanonicalHydrate,
messages: normalizedHistory,
previousMessages: keepLiveMessages(messages),
});
setMessages((current) => {
if (current !== messages) return current;
if (
authoritativeReset
? resetDropsRenderedTail
: isStaleThreadSnapshot(current, normalizedHistory, true)
) {
return keepLiveMessages(current);
}
return normalizedHistory;
});
return;
}
const reconciledMessages = keepLiveMessages(messages);
const adoptsNormalizedHistory = (
(!hasPendingToolCalls || messages.length === 0)
&& !isStaleThreadSnapshot(reconciledMessages, normalizedHistory, true)
);
if (adoptsNormalizedHistory) {
pendingHistoryLineageCommitRef.current.set(chatId, {
lineage: historyLineage,
messages: normalizedHistory,
});
}
setMessages((current) => {
const reconciled = keepLiveMessages(current);
return (hasPendingToolCalls && current.length > 0)
|| isStaleThreadSnapshot(reconciled, normalizedHistory, true)
? reconciled
: normalizedHistory;
});
}, [
loading,
chatId,
client,
completedTurnIds,
canonicalCompletedTurnIds,
completionIdsProvided,
historical,
normalizedHistory,
historyVersion,
historyContinuity,
historyLineage,
historyActiveTurnId,
hasPendingToolCalls,
historyKey,
]);
useLayoutEffect(() => {
if (!historyKey || !chatId) return;
const commit = pendingCanonicalCommitRef.current.get(chatId);
if (!commit) return;
if (
commit.historyVersion !== historyVersion
|| commit.historyLineage !== historyLineage
|| commit.messages !== messages
) {
pendingCanonicalCommitRef.current.delete(chatId);
return;
}
if (pendingCanonicalHydrateRef.current.get(chatId) !== commit.hydrate) {
pendingCanonicalCommitRef.current.delete(chatId);
return;
}
if (
uiRevisionRef.current !== commit.expectedUiRevision
|| !client.reconcileCanonicalCompletion(
chatId,
commit.hydrate.runGeneration,
commit.completedTurnIds,
commit.canonicalSnapshot,
)
) {
pendingCanonicalCommitRef.current.delete(chatId);
const fallback = replaceCanonicalCompletedTurns(
messageCacheRef.current.get(chatId)?.projection.messages ?? commit.previousMessages,
commit.messages,
commit.completedTurnIds,
);
messageCacheRef.current.set(chatId, copyThreadCheckpoint(getCheckpoint(), fallback));
setMessages((current) => current === commit.messages ? fallback : current);
return;
}
pendingCanonicalHydrateRef.current.delete(chatId);
pendingCanonicalCommitRef.current.delete(chatId);
committedHistoryLineageRef.current.set(chatId, historyLineage);
completedCanonicalHydrateVersionRef.current.set(chatId, historyVersion);
}, [chatId, client, getCheckpoint, historyKey, historyLineage, historyVersion, messages, setMessages]);
useEffect(() => {
if (!historyKey || !chatId || hasPendingToolCalls) return;
if (completedCanonicalHydrateVersionRef.current.get(chatId) !== historyVersion) return;
completedCanonicalHydrateVersionRef.current.delete(chatId);
reconcileTurnComplete();
}, [chatId, hasPendingToolCalls, historyKey, historyVersion, messages, reconcileTurnComplete]);
const refreshCanonicalHistory = useCallback(() => {
if (!historyKey || !chatId) return;
pendingCanonicalHydrateRef.current.set(chatId, {
source: "refresh",
historyLineage,
historyVersion,
runGeneration: client.getRunGeneration(chatId),
uiBaseline: durableMessageShapes(currentUiMessagesRef.current ?? []),
uiLineage: committedHistoryLineageRef.current.get(chatId) ?? null,
uiRevision: uiRevisionRef.current,
});
refreshHistory();
}, [chatId, client, historyKey, historyLineage, historyVersion, refreshHistory]);
}, [completedTurnIds, completedRunTurnId, hasPendingToolCalls]);
useEffect(() => {
if (!historyKey || !chatId) return;
@@ -1516,9 +907,9 @@ export function ThreadShell({
// A turn-end thread refresh can arrive while the viewport is easing the
// final layout change. User-driven scrolling already disables following,
// so keep an active programmatic follow alive across canonical hydration.
refreshCanonicalHistory();
refreshHistory();
});
}, [chatId, client, historyKey, refreshCanonicalHistory]);
}, [chatId, client, historyKey, refreshHistory]);
const wasPageHiddenRef = useRef(document.visibilityState === "hidden");
useEffect(() => {
@@ -1538,7 +929,7 @@ export function ThreadShell({
) {
return;
}
refreshCanonicalHistory();
refreshHistory();
};
document.addEventListener("visibilitychange", refreshOnReturn);
return () => document.removeEventListener("visibilitychange", refreshOnReturn);
@@ -1549,7 +940,7 @@ export function ThreadShell({
historyKey,
historyError,
loading,
refreshCanonicalHistory,
refreshHistory,
turnActive,
]);
@@ -1560,20 +951,10 @@ export function ThreadShell({
refreshOnNextOpen = true;
return;
}
if (refreshOnNextOpen) refreshCanonicalHistory();
if (refreshOnNextOpen) refreshHistory();
refreshOnNextOpen = false;
});
}, [client, refreshCanonicalHistory]);
useEffect(() => {
if (chatId) return;
setMessages(projectWebuiThreadMessages(historical));
}, [chatId, historical, setMessages]);
useEffect(() => {
if (!chatId || !messagesReady || loading) return;
messageCacheRef.current.set(chatId, getCheckpoint());
}, [chatId, getCheckpoint, loading, messages, messagesReady]);
}, [client, refreshHistory]);
// The landing composer queues the first message while `new_chat` is in flight.
// Only the chat created for that send may consume it; selecting another chat
@@ -1850,8 +1231,6 @@ export function ThreadShell({
const forkedChatId = await onForkChat(chatId, beforeUserIndex);
if (!forkedChatId) return;
messageCacheRef.current.delete(forkedChatId);
pendingCanonicalHydrateRef.current.delete(forkedChatId);
completedCanonicalHydrateVersionRef.current.delete(forkedChatId);
},
[chatId, onForkChat],
);
@@ -2096,7 +1475,7 @@ export function ThreadShell({
slashCommands={availableSlashCommands}
forkBoundaryMessageCount={forkBoundaryMessageCount}
hasMoreBefore={hasMoreBefore}
loadingOlder={loadingOlder || olderPageProjectionPending}
loadingOlder={loadingOlder}
olderError={olderError}
userMessageOffset={userMessageOffset}
onLoadOlder={loadOlder}