fix(webui): restore session-scoped file previews

This commit is contained in:
Xubin Ren committed 2026-09-24 15:17:05 +08:00
1 parent 1457904e8d
commit af5acabb33
15 files changed
+564 -70

No files matched your search

+17 -3
View File
@@ -13,6 +13,7 @@ interface FilePreviewPanelProps {
sessionKey: string;
path: string;
token: string;
loadPreview?: (path: string) => Promise<FilePreviewPayload>;
desktopWidth?: number;
isClosing?: boolean;
onResizeStart?: (event: ReactPointerEvent<HTMLButtonElement>) => void;
@@ -28,6 +29,7 @@ export function FilePreviewPanel({
sessionKey,
path,
token,
loadPreview,
desktopWidth = 544,
isClosing = false,
onResizeStart,
@@ -38,6 +40,8 @@ export function FilePreviewPanel({
const [entered, setEntered] = useState(false);
const tokenRef = useRef(token);
tokenRef.current = token;
const loadPreviewRef = useRef(loadPreview);
loadPreviewRef.current = loadPreview;
useEffect(() => {
const frame = window.requestAnimationFrame(() => setEntered(true));
@@ -47,7 +51,7 @@ export function FilePreviewPanel({
useEffect(() => {
let cancelled = false;
setState({ status: "loading" });
fetchFilePreview(tokenRef.current, sessionKey, path)
(loadPreviewRef.current?.(path) ?? fetchFilePreview(tokenRef.current, sessionKey, path))
.then((payload) => {
if (!cancelled) setState({ status: "ready", payload });
})
@@ -102,9 +106,10 @@ export function FilePreviewPanel({
style={{
"--file-preview-width": `${desktopWidth}px`,
"--file-preview-slot-width": !entered || isClosing ? "0px" : `${desktopWidth}px`,
"--file-preview-mobile-width": !entered || isClosing ? "0px" : "100%",
} as CSSProperties}
className={cn(
"absolute inset-y-0 right-0 z-30 w-[min(100vw,var(--file-preview-slot-width))] overflow-hidden",
"absolute inset-y-0 right-0 z-30 w-[var(--file-preview-mobile-width)] overflow-hidden",
"transition-[width] duration-300 ease-out will-change-[width]",
"md:relative md:z-auto md:w-[var(--file-preview-slot-width)] md:min-w-0 md:shrink-0",
isClosing && "pointer-events-none",
@@ -114,7 +119,7 @@ export function FilePreviewPanel({
>
<div
className={cn(
"absolute inset-y-0 right-0 flex w-[min(100vw,var(--file-preview-width))] flex-col overflow-hidden pb-[env(safe-area-inset-bottom)] md:w-[var(--file-preview-width)] md:pb-0",
"absolute inset-y-0 right-0 flex w-screen flex-col overflow-hidden pb-[env(safe-area-inset-bottom)] md:w-[var(--file-preview-width)] md:pb-0",
"border-l border-border/70 bg-background shadow-2xl md:shadow-none",
"transition-[opacity,transform] duration-300 ease-out will-change-transform",
!entered || isClosing ? "translate-x-full opacity-0" : "translate-x-0 opacity-100",
@@ -228,6 +233,15 @@ export function FilePreviewPanel({
<p>{errorMessage}</p>
</div>
</div>
) : state.payload.kind === "image" ? (
<div className="flex min-h-full items-center justify-center p-4">
<img
src={state.payload.data_url}
alt={fileName}
className="max-h-full max-w-full object-contain"
onError={() => setState({ status: "error", error: new Error("Invalid image") })}
/>
</div>
) : (
<div className="min-h-full">
{state.payload.truncated ? (
+9 -8
View File
@@ -75,15 +75,16 @@ export function FileReferenceChip({
onKeyDown={interactive ? onKeyDown : undefined}
className={cn(
"inline-flex max-w-full items-baseline gap-[0.28em] font-medium leading-[inherit]",
"text-sky-600 transition-colors hover:text-sky-700",
"dark:text-sky-300 dark:hover:text-sky-200",
interactive
? "text-sky-600 transition-colors hover:text-sky-700 dark:text-sky-300 dark:hover:text-sky-200"
: "text-muted-foreground",
interactive && [
"cursor-pointer rounded-compact",
"focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-sky-400/45",
],
)}
>
<FileReferenceIcon kind={kind} />
<FileReferenceIcon kind={kind} interactive={interactive} />
<span
data-sheen-text={active ? displayText : undefined}
className={cn(
@@ -95,7 +96,7 @@ export function FileReferenceChip({
{display === "path" && directory ? (
<>
<span className="text-muted-foreground/65">{directory}</span>
<span className="font-semibold text-sky-700 dark:text-sky-200">{name}</span>
<span className={cn("font-semibold", interactive && "text-sky-700 dark:text-sky-200")}>{name}</span>
</>
) : (
displayText
@@ -193,7 +194,7 @@ function fileKindForPath(path: string): FileReferenceKind {
}
}
function FileReferenceIcon({ kind }: { kind: FileReferenceKind }) {
function FileReferenceIcon({ kind, interactive }: { kind: FileReferenceKind; interactive: boolean }) {
if (kind === "python") {
return (
<svg
@@ -218,7 +219,7 @@ function FileReferenceIcon({ kind }: { kind: FileReferenceKind }) {
return (
<svg
aria-hidden
className="h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em] text-sky-500 dark:text-sky-300"
className={cn("h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em]", interactive && "text-sky-500 dark:text-sky-300")}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
@@ -237,7 +238,7 @@ function FileReferenceIcon({ kind }: { kind: FileReferenceKind }) {
return (
<svg
aria-hidden
className="h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em] text-sky-500 dark:text-sky-300"
className={cn("h-[0.92em] w-[0.92em] shrink-0 translate-y-[0.11em]", interactive && "text-sky-500 dark:text-sky-300")}
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
@@ -254,7 +255,7 @@ function FileReferenceIcon({ kind }: { kind: FileReferenceKind }) {
return (
<svg
aria-hidden
className="h-[0.96em] w-[0.96em] shrink-0 translate-y-[0.12em] text-sky-500 dark:text-sky-300"
className={cn("h-[0.96em] w-[0.96em] shrink-0 translate-y-[0.12em]", interactive && "text-sky-500 dark:text-sky-300")}
viewBox="0 0 24 24"
fill="none"
>
+100 -35
View File
@@ -24,6 +24,7 @@ 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 FilePreviewStore } from "@/hooks/useFilePreviewState";
import {
ApiError,
fetchFilePreviewAvailability,
@@ -46,6 +47,7 @@ import {
import type { CanonicalRunSnapshot, StreamError } from "@/lib/nanobot-client";
import type {
ChatSummary,
FilePreviewPayload,
RoundUsage,
SettingsPayload,
SlashCommand,
@@ -383,9 +385,12 @@ function clampFilePreviewWidth(width: number, maxWidth: number): number {
}
function maxFilePreviewWidth(containerWidth: number): number {
return Math.max(
FILE_PREVIEW_MIN_WIDTH,
Math.min(FILE_PREVIEW_MAX_WIDTH, containerWidth - FILE_PREVIEW_MIN_MAIN_WIDTH),
return Math.min(
containerWidth > 0 ? containerWidth : FILE_PREVIEW_MIN_WIDTH,
Math.max(
FILE_PREVIEW_MIN_WIDTH,
Math.min(FILE_PREVIEW_MAX_WIDTH, containerWidth - FILE_PREVIEW_MIN_MAIN_WIDTH),
),
);
}
@@ -396,6 +401,7 @@ interface ThreadShellProps {
temporary?: boolean;
temporaryChatIds?: readonly string[];
messageCache?: ThreadMessageCache;
filePreviewStore?: FilePreviewStore;
temporaryChatEnabled?: boolean;
onTemporaryChatEnabledChange?: (enabled: boolean) => void;
onToggleSidebar: () => void;
@@ -611,6 +617,7 @@ export function ThreadShell({
temporary = false,
temporaryChatIds = [],
messageCache,
filePreviewStore,
temporaryChatEnabled = false,
onTemporaryChatEnabledChange,
onToggleSidebar,
@@ -647,6 +654,7 @@ export function ThreadShell({
const { t } = useTranslation();
const chatId = session?.chatId ?? null;
const historyKey = temporary ? null : session?.key ?? null;
const previewSessionKey = session?.key ?? null;
const mentionSessions = useMemo(
() => sessions.filter((candidate) => candidate.key !== historyKey),
[historyKey, sessions],
@@ -702,15 +710,21 @@ export function ThreadShell({
} | null>(null);
const [heroGreetingKey, setHeroGreetingKey] = useState(randomHeroGreetingKey);
const [submittedViewportTurnId, setSubmittedViewportTurnId] = useState<string | null>(null);
const [filePreviewPath, setFilePreviewPath] = useState<string | null>(null);
const [filePreviewClosing, setFilePreviewClosing] = useState(false);
const [filePreviewWidth, setFilePreviewWidth] = useState(FILE_PREVIEW_DEFAULT_WIDTH);
const { state: previewState, setPath: setFilePreviewPath, setWidth: setFilePreviewWidth } =
useFilePreviewState(previewSessionKey, filePreviewStore);
const [closingPreview, setClosingPreview] = useState<{ key: string; path: string } | null>(null);
const filePreviewClosing = closingPreview?.key === previewSessionKey;
const filePreviewPath = previewState.path
?? (filePreviewClosing ? closingPreview?.path ?? null : null);
const [filePreviewMaxWidth, setFilePreviewMaxWidth] = useState(FILE_PREVIEW_MAX_WIDTH);
const filePreviewWidth = clampFilePreviewWidth(previewState.width, filePreviewMaxWidth);
const [quotedContext, setQuotedContext] = useState<string | null>(null);
const [composerFocusSignal, setComposerFocusSignal] = useState(0);
const shellRef = useRef<HTMLElement | null>(null);
const composerSurfaceRef = useRef<HTMLDivElement | null>(null);
const filePreviewWidthRef = useRef(FILE_PREVIEW_DEFAULT_WIDTH);
const filePreviewCloseTimerRef = useRef<number | null>(null);
const filePreviewResizeCleanupRef = useRef<(() => void) | null>(null);
const pendingFirstRef = useRef<PendingFirstMessage | null>(null);
const [pendingFirstTargetChatId, setPendingFirstTargetChatId] = useState<string | null>(null);
const consumedPendingFirstMessageIdRef = useRef<string | null>(null);
@@ -835,15 +849,15 @@ export function ThreadShell({
}, [filePreviewWidth]);
useEffect(() => {
filePreviewResizeCleanupRef.current?.();
if (filePreviewCloseTimerRef.current !== null) {
window.clearTimeout(filePreviewCloseTimerRef.current);
filePreviewCloseTimerRef.current = null;
}
setFilePreviewClosing(false);
setFilePreviewPath(null);
setClosingPreview(null);
setQuotedContext(null);
setSubmittedViewportTurnId(null);
}, [historyKey]);
}, [previewSessionKey]);
useEffect(() => {
const retained = new Set(temporaryChatIds);
@@ -865,6 +879,7 @@ export function ThreadShell({
useEffect(() => {
return () => {
filePreviewResizeCleanupRef.current?.();
if (filePreviewCloseTimerRef.current !== null) {
window.clearTimeout(filePreviewCloseTimerRef.current);
}
@@ -917,11 +932,11 @@ export function ThreadShell({
}, [chatId, messagesReady, rememberedViewportTurnId, turnActive]);
const filePreviewAvailabilityCache = useMemo(
() => new Map<string, FilePreviewAvailabilityCacheEntry>(),
[historyKey],
[previewSessionKey],
);
const filePreviewAvailabilityRevision = displayMessages.length;
const resolveFilePreviewAvailability = useCallback((path: string) => {
if (!historyKey) return Promise.resolve(false);
if (!previewSessionKey) return Promise.resolve(false);
const cached = filePreviewAvailabilityCache.get(path);
if (
cached
@@ -929,13 +944,18 @@ export function ThreadShell({
) {
return cached.promise;
}
const pending = fetchFilePreviewAvailability(getToken(), historyKey, path).catch(
const request = temporary
? client.requestMutation<{ available: boolean }>("temporary_chat.file_preview", {
chat_id: chatId, path, probe: true,
}).then((result) => result.available)
: fetchFilePreviewAvailability(getToken(), previewSessionKey, path);
const pending = request.catch(
(error: unknown) => {
if (error instanceof ApiError) {
if (error.status === 404 && /API route not found/i.test(error.message)) {
return true;
}
if ([400, 403, 404, 415].includes(error.status)) return false;
if ([400, 403, 404, 413, 415].includes(error.status)) return false;
}
return false;
},
@@ -955,7 +975,10 @@ export function ThreadShell({
filePreviewAvailabilityCache,
filePreviewAvailabilityRevision,
getToken,
historyKey,
previewSessionKey,
temporary,
client,
chatId,
]);
const showHeroComposer = displayMessages.length === 0 && !loading;
@@ -1462,26 +1485,62 @@ export function ThreadShell({
[chatId, send, withWorkspaceScope],
);
const loadTemporaryFilePreview = useCallback((path: string) =>
client.requestMutation<FilePreviewPayload>("temporary_chat.file_preview", {
chat_id: chatId, path,
}).catch((error: unknown) => {
if (error instanceof Error && "status" in error && typeof error.status === "number") {
throw new ApiError(error.status, error.message);
}
throw error;
}), [client, chatId]);
const handleCloseFilePreview = useCallback(() => {
if (!previewSessionKey || !filePreviewPath || filePreviewClosing) return;
filePreviewResizeCleanupRef.current?.();
setClosingPreview({ key: previewSessionKey, path: filePreviewPath });
// Record the closed state immediately, even if navigation interrupts the animation.
setFilePreviewPath(null);
filePreviewCloseTimerRef.current = window.setTimeout(() => {
filePreviewCloseTimerRef.current = null;
setClosingPreview(null);
}, FILE_PREVIEW_CLOSE_ANIMATION_MS);
}, [previewSessionKey, filePreviewClosing, filePreviewPath, setFilePreviewPath]);
const handleOpenFilePreview = useCallback((path: string) => {
if (path === filePreviewPath && !filePreviewClosing) {
handleCloseFilePreview();
return;
}
if (filePreviewCloseTimerRef.current !== null) {
window.clearTimeout(filePreviewCloseTimerRef.current);
filePreviewCloseTimerRef.current = null;
}
setFilePreviewClosing(false);
setClosingPreview(null);
setFilePreviewPath(path);
}, []);
}, [filePreviewPath, filePreviewClosing, handleCloseFilePreview, setFilePreviewPath]);
const handleCloseFilePreview = useCallback(() => {
if (!filePreviewPath || filePreviewClosing) return;
setFilePreviewClosing(true);
filePreviewCloseTimerRef.current = window.setTimeout(() => {
filePreviewCloseTimerRef.current = null;
setFilePreviewPath(null);
setFilePreviewClosing(false);
}, FILE_PREVIEW_CLOSE_ANIMATION_MS);
}, [filePreviewClosing, filePreviewPath]);
// Markdown blocks can retain rendered links while their text is unchanged.
// Keep their callback stable, but always act on the current pane/session state.
const openFilePreviewRef = useRef(handleOpenFilePreview);
openFilePreviewRef.current = handleOpenFilePreview;
const openFilePreview = useCallback((path: string) => openFilePreviewRef.current(path), []);
useEffect(() => {
if (!filePreviewPath || !headerActive) return;
const onKeyDown = (event: KeyboardEvent) => {
if (event.key !== "Escape" || event.defaultPrevented || event.isComposing) return;
// Let a dialog/menu above the pane consume Escape without closing this preview.
if (document.querySelector('[role="dialog"], [role="alertdialog"], [role="menu"], [role="listbox"], [data-radix-popper-content-wrapper]')) return;
event.preventDefault();
handleCloseFilePreview();
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
}, [filePreviewPath, headerActive, handleCloseFilePreview]);
const handleFilePreviewResizeStart = useCallback((event: ReactPointerEvent<HTMLButtonElement>) => {
filePreviewResizeCleanupRef.current?.();
event.preventDefault();
event.stopPropagation();
const panel = event.currentTarget.closest<HTMLElement>("[data-file-preview-panel]");
@@ -1512,7 +1571,7 @@ export function ThreadShell({
moveEvent.preventDefault();
applyWidth(moveEvent.clientX);
};
const handlePointerUp = () => {
const stopResize = (commit: boolean) => {
if (frame !== null) {
window.cancelAnimationFrame(frame);
frame = null;
@@ -1520,32 +1579,36 @@ export function ThreadShell({
panel?.style.setProperty("--file-preview-width", `${nextWidth}px`);
panel?.style.setProperty("--file-preview-slot-width", `${nextWidth}px`);
if (panel) panel.style.transition = originalPanelTransition;
setFilePreviewWidth(nextWidth);
if (commit) setFilePreviewWidth(nextWidth);
document.body.style.cursor = originalBodyCursor;
document.body.style.userSelect = originalBodyUserSelect;
window.removeEventListener("pointermove", handlePointerMove);
window.removeEventListener("pointerup", handlePointerUp);
window.removeEventListener("pointercancel", handlePointerUp);
filePreviewResizeCleanupRef.current = null;
};
const handlePointerUp = () => stopResize(true);
filePreviewResizeCleanupRef.current = () => stopResize(false);
applyWidth(event.clientX);
window.addEventListener("pointermove", handlePointerMove);
window.addEventListener("pointerup", handlePointerUp);
window.addEventListener("pointercancel", handlePointerUp);
}, []);
}, [setFilePreviewWidth]);
useEffect(() => {
if (!filePreviewPath) return;
const clampToShell = () => {
const shellWidth = shellRef.current?.getBoundingClientRect().width ?? window.innerWidth;
const maxWidth = maxFilePreviewWidth(shellWidth);
const nextWidth = clampFilePreviewWidth(filePreviewWidthRef.current, maxWidth);
filePreviewWidthRef.current = nextWidth;
setFilePreviewWidth(nextWidth);
setFilePreviewMaxWidth(maxWidth);
};
clampToShell();
const observer = typeof ResizeObserver === "undefined" ? null : new ResizeObserver(clampToShell);
if (shellRef.current) observer?.observe(shellRef.current);
window.addEventListener("resize", clampToShell);
return () => {
observer?.disconnect();
window.removeEventListener("resize", clampToShell);
};
}, [filePreviewPath]);
@@ -1749,7 +1812,7 @@ export function ThreadShell({
) : null}
{headerPortalTarget === undefined ? threadHeader : null}
<FilePreviewAvailabilityProvider
resolve={historyKey ? resolveFilePreviewAvailability : undefined}
resolve={previewSessionKey ? resolveFilePreviewAvailability : undefined}
>
<ThreadViewport
ref={viewportRef}
@@ -1775,7 +1838,7 @@ export function ThreadShell({
onLoadOlder={loadOlder}
traceDetailScope={historyKey}
onLoadTraceDetails={messagesReady ? loadTraceDetails : undefined}
onOpenFilePreview={historyKey ? handleOpenFilePreview : undefined}
onOpenFilePreview={previewSessionKey ? openFilePreview : undefined}
onForkFromMessage={onForkChat ? handleForkFromMessage : undefined}
onQuoteSelection={session ? handleQuoteSelection : undefined}
/>
@@ -1794,11 +1857,13 @@ export function ThreadShell({
</div>,
composerPortalTarget,
) : null}
{filePreviewPath && historyKey ? (
{filePreviewPath && previewSessionKey ? (
<FilePreviewPanel
sessionKey={historyKey}
key={`${previewSessionKey}:${filePreviewPath}`}
sessionKey={previewSessionKey}
path={filePreviewPath}
token={token}
loadPreview={temporary ? loadTemporaryFilePreview : undefined}
desktopWidth={filePreviewWidth}
isClosing={filePreviewClosing}
onResizeStart={handleFilePreviewResizeStart}