Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

📖 fix: Preview Extracted Upload Text (#15946)

Danny Avila5e2a2c65a3f4
diff --git a/client/src/common/types.ts b/client/src/common/types.ts
index 2c39712cbd..6d1ec5ce2a 100644
--- a/client/src/common/types.ts
+++ b/client/src/common/types.ts
@@ -596,6 +596,7 @@ export interface ExtendedFile {
source?: FileSources;
attached?: boolean;
embedded?: boolean;
+ llmDeliveryPath?: t.TFile['llmDeliveryPath'];
tool_resource?: string;
metadata?: t.TFile['metadata'];
}
diff --git a/client/src/components/Chat/Input/ChatForm.tsx b/client/src/components/Chat/Input/ChatForm.tsx
index b15d711652..e155035d30 100644
--- a/client/src/components/Chat/Input/ChatForm.tsx
+++ b/client/src/components/Chat/Input/ChatForm.tsx
@@ -70,6 +70,26 @@ import BadgeRow from './BadgeRow';
import Mention from './Mention';
import store from '~/store';
+export function toRestoredComposerFile(
+ file: NonNullable<TMessage['files']>[number],
+): ExtendedFile | null {
+ if (!file.file_id) {
+ return null;
+ }
+ return {
+ file_id: file.file_id,
+ filename: file.filename,
+ filepath: file.filepath,
+ type: file.type ?? '',
+ height: file.height,
+ width: file.width,
+ size: file.bytes ?? 0,
+ progress: 1,
+ attached: true,
+ llmDeliveryPath: file.llmDeliveryPath,
+ };
+}
+
interface ChatFormProps {
index: number;
placeholder?: string;
@@ -346,20 +366,11 @@ const ChatForm = memo(function ChatForm({
setFiles((prev) => {
const next = new Map(prev);
for (const file of chipFiles) {
- if (!file.file_id) {
+ const restoredFile = toRestoredComposerFile(file);
+ if (restoredFile == null) {
continue;
}
- next.set(file.file_id, {
- file_id: file.file_id,
- filename: file.filename,
- filepath: file.filepath,
- type: file.type ?? '',
- height: file.height,
- width: file.width,
- size: file.bytes ?? 0,
- progress: 1,
- attached: true,
- });
+ next.set(restoredFile.file_id, restoredFile);
}
return next;
});
diff --git a/client/src/components/Chat/Input/InFlightSteers.tsx b/client/src/components/Chat/Input/InFlightSteers.tsx
index 5884663c40..12623319b0 100644
--- a/client/src/components/Chat/Input/InFlightSteers.tsx
+++ b/client/src/components/Chat/Input/InFlightSteers.tsx
@@ -15,6 +15,7 @@ import {
useDefaultToggleEntry,
useInterruptToggleEntry,
} from './SteerMenu';
+import { carriedSteerContext, cn, hydrateFileDeliveryMetadata, usesImagePreview } from '~/utils';
import FilePreviewDialog from '~/components/Chat/Messages/Content/FilePreviewDialog';
import { supportsGenerationProtocolV2, useArmSteerMutation } from '~/data-provider';
import { steerOverlayHeightFamily, escalatingSteerFamily } from '~/store/steer';
@@ -25,7 +26,7 @@ import FileContainer from '~/components/Chat/Input/Files/FileContainer';
import { useSteerCancel, useSteerReclaim, useLocalize } from '~/hooks';
import ImagePreview from '~/components/Chat/Input/Files/ImagePreview';
import SteerReceipt from '~/components/Chat/Steering/Receipt';
-import { carriedSteerContext, cn } from '~/utils';
+import { useFileMapContext } from '~/Providers';
import store from '~/store';
/** Restores a message's text into the composer, or refuses (false) when the
@@ -42,7 +43,7 @@ const splitFiles = (files?: TMessage['files']) => {
const images: NonNullable<TMessage['files']> = [];
const others: NonNullable<TMessage['files']> = [];
for (const file of files ?? []) {
- (file.type?.startsWith('image/') === true ? images : others).push(file);
+ (usesImagePreview(file) ? images : others).push(file);
}
return { images, others };
};
@@ -609,6 +610,7 @@ const InFlightSteer = memo(function InFlightSteer({
fileType={selectedFile?.type ?? undefined}
fileSource={selectedFile?.source}
fileSize={(selectedFile as TFile | null)?.bytes}
+ deliveryPath={selectedFile?.llmDeliveryPath}
/>
)}
</div>
@@ -632,7 +634,17 @@ const InFlightSteers = memo(function InFlightSteers({
}) {
const localize = useLocalize();
const steers = useRecoilValue(store.pendingSteersByConvoId(conversationId));
- const inFlight = useMemo(() => steers.filter((steer) => steer.status !== 'failed'), [steers]);
+ const fileMap = useFileMapContext();
+ const inFlight = useMemo(
+ () =>
+ steers
+ .filter((steer) => steer.status !== 'failed')
+ .map((steer) => {
+ const files = hydrateFileDeliveryMetadata(steer.files, undefined, fileMap);
+ return files === steer.files ? steer : { ...steer, files };
+ }),
+ [fileMap, steers],
+ );
/** Mirrors `PendingSteerChips`: while one interrupt is unresolved, every
* other escalation control disables rather than arming a second seal. The
* escalating flag covers an arm request's round trip, before its chip
diff --git a/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx b/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
index 18442369ad..2364da87c6 100644
--- a/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
+++ b/client/src/components/Chat/Input/__tests__/ChatForm.attachments.spec.tsx
@@ -11,9 +11,9 @@ import { QueryKeys, FileSources, EModelEndpoint } from 'librechat-data-provider'
import { render, screen, within, waitFor, fireEvent } from '@testing-library/react';
import type { TFile, TFileUpload, TConversation } from 'librechat-data-provider';
import type { ChatFormValues } from '~/common';
+import ChatForm, { toRestoredComposerFile } from '../ChatForm';
import { ChatContext, ChatFormProvider } from '~/Providers';
import { AuthContextProvider } from '~/hooks/AuthContext';
-import ChatForm from '../ChatForm';
import store from '~/store';
const mockUpload = jest.fn();
@@ -178,6 +178,23 @@ describe('ChatForm attachments', () => {
);
});
+ test('preserves extracted-text delivery when restoring a queued attachment', () => {
+ expect(
+ toRestoredComposerFile({
+ file_id: 'stored-doc',
+ filename: 'report.docx',
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ llmDeliveryPath: 'text',
+ }),
+ ).toMatchObject({
+ file_id: 'stored-doc',
+ filename: 'report.docx',
+ progress: 1,
+ attached: true,
+ llmDeliveryPath: 'text',
+ });
+ });
+
test('re-enables send once an attachment finishes uploading', async () => {
const { container } = renderComposer();
diff --git a/client/src/components/Chat/Input/__tests__/InFlightSteers.test.tsx b/client/src/components/Chat/Input/__tests__/InFlightSteers.test.tsx
index 9e97f55ca4..e0c7ab75ec 100644
--- a/client/src/components/Chat/Input/__tests__/InFlightSteers.test.tsx
+++ b/client/src/components/Chat/Input/__tests__/InFlightSteers.test.tsx
@@ -20,6 +20,11 @@ const mockRestoreToComposer = jest.fn();
let convertSteersForTest: ReturnType<typeof useSteerConvert>;
let observedQueueForTest: QueuedMessage[];
let setSteersForTest: (updater: (prev: PendingSteer[]) => PendingSteer[]) => void;
+let mockFileMap: Record<string, { llmDeliveryPath?: 'provider' | 'text' | 'none' }> = {};
+
+jest.mock('~/Providers', () => ({
+ useFileMapContext: () => mockFileMap,
+}));
jest.mock('~/hooks', () => ({
useLocalize: () => (key: string) => key,
@@ -81,8 +86,20 @@ jest.mock('~/components/Chat/Input/Files/ImagePreview', () => ({
jest.mock('~/components/Chat/Messages/Content/FilePreviewDialog', () => ({
__esModule: true,
- default: ({ open, fileName }: { open: boolean; fileName: string }) =>
- open ? <div data-testid="steer-file-preview">{fileName}</div> : null,
+ default: ({
+ open,
+ fileName,
+ deliveryPath,
+ }: {
+ open: boolean;
+ fileName: string;
+ deliveryPath?: string;
+ }) =>
+ open ? (
+ <div data-testid="steer-file-preview" data-delivery-path={deliveryPath}>
+ {fileName}
+ </div>
+ ) : null,
}));
jest.mock('~/components/Chat/Messages/Content/MarkdownLite', () => ({
@@ -173,6 +190,7 @@ async function clickMenuItem(label: string) {
describe('InFlightSteers', () => {
beforeEach(() => {
jest.clearAllMocks();
+ mockFileMap = {};
mockCancelMutateAsync.mockResolvedValue({ removed: true });
mockRestoreToComposer.mockReturnValue(true);
observedQueueForTest = [];
@@ -634,6 +652,29 @@ describe('InFlightSteers', () => {
expect(screen.getByTestId('steer-file-preview')).toHaveTextContent('notes.pdf');
});
+ it.each(['application/pdf', 'image/png'])(
+ 'hydrates %s preview metadata after the steer',
+ (type) => {
+ const steer: PendingSteer = {
+ steerId: 's1',
+ text: 'see attached',
+ status: 'pending',
+ createdAt: 1,
+ files: [{ file_id: 'f1', filename: 'notes.pdf', type }],
+ };
+ const rendered = renderSteers([steer]);
+
+ mockFileMap = { f1: { llmDeliveryPath: 'text' } };
+ rendered.rerender(steersElement([steer]));
+
+ fireEvent.click(screen.getByTestId('steer-file'));
+ expect(screen.getByTestId('steer-file-preview')).toHaveAttribute(
+ 'data-delivery-path',
+ 'text',
+ );
+ },
+ );
+
it('renders markdown the same way the applied part will, so text does not reflow on apply', () => {
renderSteers([{ steerId: 's1', text: '**bold** steer', status: 'pending', createdAt: 1 }], {
enableUserMsgMarkdown: true,
diff --git a/client/src/components/Chat/Messages/Content/FilePreviewDialog.tsx b/client/src/components/Chat/Messages/Content/FilePreviewDialog.tsx
index 958be9f4ab..7911b2fd4e 100644
--- a/client/src/components/Chat/Messages/Content/FilePreviewDialog.tsx
+++ b/client/src/components/Chat/Messages/Content/FilePreviewDialog.tsx
@@ -1,13 +1,31 @@
-import { useState, useEffect, useCallback, useMemo, useRef } from 'react';
+import { useState, useEffect, useCallback, useMemo } from 'react';
import copy from 'copy-to-clipboard';
import { Download } from 'lucide-react';
import { useRecoilValue } from 'recoil';
-import { OGDialog, OGDialogContent, OGDialogTitle, OGDialogDescription } from '@librechat/client';
+import {
+ Button,
+ OGDialog,
+ OGDialogContent,
+ OGDialogTitle,
+ OGDialogDescription,
+} from '@librechat/client';
+import type { TFile } from 'librechat-data-provider';
+import {
+ getFileExtension,
+ getPreviewKind,
+ isExtractedTextPreviewLoading,
+ shouldUseExtractedTextPreview,
+ shouldUseSharedFileDownload,
+} from './preview';
+import {
+ useFilePreview,
+ useFilePreviewBlob,
+ useFileDownload,
+ useSharedFileDownload,
+} from '~/data-provider';
import { getDownloadFilename, logger, sortPagesByRelevance, triggerDownload } from '~/utils';
-import { revokeDownloadURL, useFileDownload, useSharedFileDownload } from '~/data-provider';
-import { getFileExtension, getPreviewKind, shouldUseSharedFileDownload } from './preview';
import CopyButton from '~/components/Messages/Content/CopyButton';
-import { useShareContext } from '~/Providers';
+import { useFileMapContext, useShareContext } from '~/Providers';
import { useLocalize } from '~/hooks';
import store from '~/store';
@@ -23,6 +41,7 @@ interface FilePreviewDialogProps {
fileType?: string;
fileSource?: string;
fileSize?: number;
+ deliveryPath?: TFile['llmDeliveryPath'];
}
/** Formats bytes with unit suffix (differs from ~/utils/formatBytes which returns a raw number). */
@@ -78,88 +97,109 @@ export default function FilePreviewDialog({
fileType,
fileSource,
fileSize,
+ deliveryPath,
}: FilePreviewDialogProps) {
const localize = useLocalize();
const user = useRecoilValue(store.user);
const { shareId } = useShareContext();
- // Preview reads revoke their blob after consumption, so they need a separate
- // query identity from user-triggered downloads that may be in flight concurrently.
+ const fileMap = useFileMapContext();
+ const showExtractedText = shouldUseExtractedTextPreview(
+ deliveryPath ?? (!shareId && fileId ? fileMap?.[fileId]?.llmDeliveryPath : undefined),
+ );
+ const {
+ data: extractedPreview,
+ isInitialLoading: extractedTextLoading,
+ isFetching: extractedTextFetching,
+ isError: extractedTextError,
+ refetch: refetchExtractedPreview,
+ } = useFilePreview(
+ fileId,
+ {
+ enabled: open && showExtractedText && !!fileId,
+ },
+ shareId,
+ );
+ // Downloads own URLs; previews share bytes and create only their display URL.
const { refetch: downloadOwned } = useFileDownload(user?.id ?? '', fileId, { direct: false });
const { refetch: downloadShared } = useSharedFileDownload(shareId, fileId);
- const { refetch: previewOwned } = useFileDownload(user?.id ?? '', fileId, {
- direct: false,
- purpose: 'preview',
- });
- const { refetch: previewShared } = useSharedFileDownload(shareId, fileId, 'preview');
+ const { refetch: previewFile } = useFilePreviewBlob(user?.id, fileId, shareId);
// A shared viewer must stay inside the share-scoped authorization boundary;
// citation and retrieval previews do not carry a rewritten filepath signal.
const useShared = shouldUseSharedFileDownload(shareId, fileId);
const downloadFile = useShared ? downloadShared : downloadOwned;
- const previewFile = useShared ? previewShared : previewOwned;
const [fileContent, setFileContent] = useState<string | null>(null);
const [fileBlobUrl, setFileBlobUrl] = useState<string | null>(null);
const [loading, setLoading] = useState(false);
const [previewError, setPreviewError] = useState(false);
const [isCopied, setIsCopied] = useState(false);
- const loadingRef = useRef(false);
- const previewKind = getPreviewKind(fileName, fileType, fileSource);
+ const previewKind = showExtractedText ? false : getPreviewKind(fileName, fileType, fileSource);
const downloadFilename = getDownloadFilename(fileName, fileId, fileSource);
+ const displayedText = showExtractedText ? (extractedPreview?.text ?? null) : fileContent;
+ const isLoading =
+ (!showExtractedText && loading) ||
+ (showExtractedText &&
+ isExtractedTextPreviewLoading(
+ extractedPreview?.status,
+ extractedTextLoading,
+ extractedTextError,
+ ));
+ const hasPreviewError =
+ (!showExtractedText && previewError) ||
+ (showExtractedText &&
+ (extractedTextError ||
+ extractedPreview?.status === 'failed' ||
+ (extractedPreview?.status === 'ready' && extractedPreview.text == null)));
- const cancelledRef = useRef(false);
-
- const loadPreview = useCallback(async () => {
- if (!fileId || !previewKind || loadingRef.current) {
+ useEffect(() => {
+ let cancelled = false;
+ let objectUrl: string | undefined;
+ setFileContent(null);
+ setFileBlobUrl(null);
+ setPreviewError(false);
+ setLoading(false);
+ if (!open || !fileId || !previewKind) {
return;
}
- loadingRef.current = true;
- cancelledRef.current = false;
- setLoading(true);
- setPreviewError(false);
- try {
- const result = await previewFile();
- if (!result.data) {
- if (!cancelledRef.current) {
+ setLoading(true);
+ const load = async () => {
+ try {
+ const { data: blob } = await previewFile();
+ if (!blob) {
+ throw new Error('Preview download unavailable');
+ }
+ if (cancelled) {
+ return;
+ }
+ if (previewKind === 'text') {
+ const text = await blob.text();
+ if (!cancelled) {
+ setFileContent(text);
+ }
+ } else {
+ objectUrl = URL.createObjectURL(new Blob([blob], { type: 'application/pdf' }));
+ setFileBlobUrl(objectUrl);
+ }
+ } catch {
+ if (!cancelled) {
setPreviewError(true);
}
- return;
- }
- if (cancelledRef.current) {
- revokeDownloadURL(result.data);
- return;
- }
-
- let blob: Blob;
- try {
- const resp = await fetch(result.data);
- blob = await resp.blob();
} finally {
- revokeDownloadURL(result.data);
- }
-
- if (cancelledRef.current) {
- return;
- }
-
- if (previewKind === 'text') {
- setFileContent(await blob.text());
- } else {
- const typed = new Blob([blob], { type: 'application/pdf' });
- setFileBlobUrl(URL.createObjectURL(typed));
- }
- } catch {
- if (!cancelledRef.current) {
- setPreviewError(true);
+ if (!cancelled) {
+ setLoading(false);
+ }
}
- } finally {
- loadingRef.current = false;
- if (!cancelledRef.current) {
- setLoading(false);
+ };
+ void load();
+ return () => {
+ cancelled = true;
+ if (objectUrl) {
+ URL.revokeObjectURL(objectUrl);
}
- }
- }, [fileId, previewKind, previewFile]);
+ };
+ }, [open, fileId, previewKind, previewFile, shareId, user?.id]);
const handleDownload = useCallback(async () => {
if (!fileId) {
@@ -176,39 +216,20 @@ export default function FilePreviewDialog({
}
}, [downloadFile, downloadFilename, fileId]);
- useEffect(() => {
- if (open && previewKind && fileContent === null && !fileBlobUrl) {
- loadPreview();
- }
- }, [open, previewKind, fileContent, fileBlobUrl, loadPreview]);
-
- useEffect(() => {
- return () => {
- if (fileBlobUrl) {
- URL.revokeObjectURL(fileBlobUrl);
- }
- };
- }, [fileBlobUrl]);
-
useEffect(() => {
if (!open) {
- cancelledRef.current = true;
- setFileContent(null);
- setFileBlobUrl(null);
- setPreviewError(false);
- setLoading(false);
setIsCopied(false);
}
}, [open]);
const handleCopy = useCallback(() => {
- if (!fileContent) {
+ if (!displayedText) {
return;
}
- copy(fileContent, { format: 'text/plain' });
+ copy(displayedText, { format: 'text/plain' });
setIsCopied(true);
setTimeout(() => setIsCopied(false), 3000);
- }, [fileContent]);
+ }, [displayedText]);
const displayType = useMemo(() => getDisplayType(fileType, fileName), [fileType, fileName]);
const sortedPages = useMemo(
@@ -254,28 +275,39 @@ export default function FilePreviewDialog({
</div>
<div className="relative min-h-0 flex-1 overflow-y-auto px-6 pb-6 pt-4">
- {loading && (
+ {isLoading && (
<div className="flex h-60 items-center justify-center rounded-lg bg-surface-secondary">
<span className="shimmer text-sm text-text-secondary">
{localize('com_ui_loading')}
</span>
</div>
)}
- {previewError && (
- <div className="flex h-32 items-center justify-center rounded-lg bg-surface-secondary">
+ {hasPreviewError && !isLoading && (
+ <div className="flex h-32 flex-col items-center justify-center gap-2 rounded-lg bg-surface-secondary">
<span className="text-sm text-text-secondary">
{localize('com_ui_preview_unavailable')}
</span>
+ {showExtractedText && (
+ <Button
+ type="button"
+ variant="outline"
+ size="sm"
+ disabled={extractedTextFetching}
+ onClick={() => void refetchExtractedPreview()}
+ >
+ {localize('com_ui_retry')}
+ </Button>
+ )}
</div>
)}
- {fileBlobUrl && (
+ {fileBlobUrl && !showExtractedText && (
<iframe
src={fileBlobUrl}
title={`${localize('com_ui_preview')}: ${fileName}`}
className="h-[70vh] w-full rounded-lg border border-border-light"
/>
)}
- {fileContent !== null && (
+ {displayedText !== null && !isLoading && !hasPreviewError && (
<>
<div className="pointer-events-none sticky top-0 z-10 flex justify-end pr-1">
<CopyButton
@@ -288,12 +320,12 @@ export default function FilePreviewDialog({
</div>
<div className="-mt-8 rounded-lg bg-surface-secondary p-4">
<pre className="whitespace-pre-wrap break-words pr-8 font-mono text-sm leading-6 text-text-primary">
- {fileContent}
+ {displayedText}
</pre>
</div>
</>
)}
- {!previewKind && !loading && (
+ {!previewKind && !showExtractedText && !isLoading && (
<div className="flex h-32 items-center justify-center rounded-lg bg-surface-secondary">
<span className="text-sm text-text-secondary">
{localize('com_ui_preview_unavailable')}
diff --git a/client/src/components/Chat/Messages/Content/Files.tsx b/client/src/components/Chat/Messages/Content/Files.tsx
index 1bfa700871..0f2be00c35 100644
--- a/client/src/components/Chat/Messages/Content/Files.tsx
+++ b/client/src/components/Chat/Messages/Content/Files.tsx
@@ -1,17 +1,25 @@
import { useMemo, useState, useCallback, memo } from 'react';
import type { TFile, TMessage } from 'librechat-data-provider';
import FileContainer from '~/components/Chat/Input/Files/FileContainer';
+import { usesImagePreview, hydrateFileDeliveryMetadata } from '~/utils';
+import { useFileMapContext, useShareContext } from '~/Providers';
import FilePreviewDialog from './FilePreviewDialog';
import Image from './Image';
const Files = ({ message }: { message?: TMessage }) => {
+ const fileMap = useFileMapContext();
+ const { shareId } = useShareContext();
+ const files = useMemo(
+ () => hydrateFileDeliveryMetadata(message?.files, undefined, shareId ? undefined : fileMap),
+ [message?.files, fileMap, shareId],
+ );
const imageFiles = useMemo(() => {
- return message?.files?.filter((file) => file.type?.startsWith('image/')) || [];
- }, [message?.files]);
+ return files?.filter(usesImagePreview) || [];
+ }, [files]);
const otherFiles = useMemo(() => {
- return message?.files?.filter((file) => !file.type?.startsWith('image/')) || [];
- }, [message?.files]);
+ return files?.filter((file) => !usesImagePreview(file)) || [];
+ }, [files]);
const [selectedFile, setSelectedFile] = useState<Partial<TFile> | null>(null);
@@ -50,6 +58,7 @@ const Files = ({ message }: { message?: TMessage }) => {
fileType={selectedFile?.type ?? undefined}
fileSource={selectedFile?.source}
fileSize={(selectedFile as TFile)?.bytes}
+ deliveryPath={selectedFile?.llmDeliveryPath}
/>
</>
);
diff --git a/client/src/components/Chat/Messages/Content/Parts/SteerPart.tsx b/client/src/components/Chat/Messages/Content/Parts/SteerPart.tsx
index dcd84772b2..f00ae091c7 100644
--- a/client/src/components/Chat/Messages/Content/Parts/SteerPart.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/SteerPart.tsx
@@ -4,14 +4,14 @@ import type { TFile, TMessage } from 'librechat-data-provider';
import FilePreviewDialog from '~/components/Chat/Messages/Content/FilePreviewDialog';
import MessageTimestamp from '~/components/Chat/Messages/ui/MessageTimestamp';
import MessageQuotes from '~/components/Chat/Messages/Content/MessageQuotes';
+import { cn, hydrateFileDeliveryMetadata, usesImagePreview } from '~/utils';
import MarkdownLite from '~/components/Chat/Messages/Content/MarkdownLite';
import FileContainer from '~/components/Chat/Input/Files/FileContainer';
+import { useFileMapContext, useShareContext } from '~/Providers';
import SteerReceipt from '~/components/Chat/Steering/Receipt';
import Image from '~/components/Chat/Messages/Content/Image';
import CollapsibleText from './CollapsibleText';
-import { useShareContext } from '~/Providers';
import { useLocalize } from '~/hooks';
-import { cn } from '~/utils';
import store from '~/store';
/**
@@ -49,6 +49,7 @@ const SteerPart = memo(function SteerPart({
* user into it, and the public share route mounts outside that provider. */
const user = useRecoilValue(store.user);
const { isSharedConvo } = useShareContext();
+ const fileMap = useFileMapContext();
const usernameDisplay = useRecoilValue<boolean>(store.UsernameDisplay);
const enableUserMsgMarkdown = useRecoilValue<boolean>(store.enableUserMsgMarkdown);
const collapseLongUserMessages = useRecoilValue<boolean>(store.collapseLongUserMessages);
@@ -65,13 +66,14 @@ const SteerPart = memo(function SteerPart({
() => (createdAt != null ? new Date(createdAt).toISOString() : null),
[createdAt],
);
- const imageFiles = useMemo(
- () => files?.filter((file) => file.type?.startsWith('image/')) ?? [],
- [files],
+ const hydratedFiles = useMemo(
+ () => hydrateFileDeliveryMetadata(files, undefined, fileMap),
+ [files, fileMap],
);
+ const imageFiles = useMemo(() => hydratedFiles?.filter(usesImagePreview) ?? [], [hydratedFiles]);
const otherFiles = useMemo(
- () => files?.filter((file) => !file.type?.startsWith('image/')) ?? [],
- [files],
+ () => hydratedFiles?.filter((file) => !usesImagePreview(file)) ?? [],
+ [hydratedFiles],
);
const [selectedFile, setSelectedFile] = useState<Partial<TFile> | null>(null);
const handlePreviewClose = useCallback((open: boolean) => {
@@ -174,6 +176,7 @@ const SteerPart = memo(function SteerPart({
fileType={selectedFile?.type ?? undefined}
fileSource={selectedFile?.source}
fileSize={(selectedFile as TFile | null)?.bytes}
+ deliveryPath={selectedFile?.llmDeliveryPath}
/>
)}
</div>
diff --git a/client/src/components/Chat/Messages/Content/Parts/__tests__/SteerPart.test.tsx b/client/src/components/Chat/Messages/Content/Parts/__tests__/SteerPart.test.tsx
index d2f9d4d7ca..bf6898c8c3 100644
--- a/client/src/components/Chat/Messages/Content/Parts/__tests__/SteerPart.test.tsx
+++ b/client/src/components/Chat/Messages/Content/Parts/__tests__/SteerPart.test.tsx
@@ -7,6 +7,7 @@ import SteerPart from '../SteerPart';
import store from '~/store';
let mockShareContext: { isSharedConvo?: boolean; shareId?: string } = {};
+let mockFileMap: Record<string, { llmDeliveryPath?: 'provider' | 'text' | 'none' }> = {};
jest.mock('~/hooks', () => ({
useLocalize: () => (key: string) => key,
@@ -14,6 +15,7 @@ jest.mock('~/hooks', () => ({
jest.mock('~/Providers', () => ({
useShareContext: () => mockShareContext,
+ useFileMapContext: () => mockFileMap,
}));
jest.mock('~/components/Chat/Messages/ui/MessageTimestamp', () => ({
@@ -37,8 +39,20 @@ jest.mock('~/components/Chat/Input/Files/FileContainer', () => ({
jest.mock('~/components/Chat/Messages/Content/FilePreviewDialog', () => ({
__esModule: true,
- default: ({ open, fileName }: { open: boolean; fileName: string }) =>
- open ? <div data-testid="steer-file-preview">{fileName}</div> : null,
+ default: ({
+ open,
+ fileName,
+ deliveryPath,
+ }: {
+ open: boolean;
+ fileName: string;
+ deliveryPath?: string;
+ }) =>
+ open ? (
+ <div data-testid="steer-file-preview" data-delivery-path={deliveryPath}>
+ {fileName}
+ </div>
+ ) : null,
}));
jest.mock('~/components/Chat/Messages/Content/Image', () => ({
@@ -66,6 +80,7 @@ function renderPart(
describe('SteerPart author label', () => {
beforeEach(() => {
mockShareContext = {};
+ mockFileMap = {};
});
it('labels with the logged-in user name in the owner view (username display on)', () => {
@@ -138,6 +153,7 @@ describe('SteerPart author label', () => {
describe('SteerPart presentation', () => {
beforeEach(() => {
mockShareContext = {};
+ mockFileMap = {};
});
it('presents the steer as a compact user bubble with accessible attribution', () => {
@@ -172,13 +188,21 @@ describe('SteerPart presentation', () => {
expect(screen.getByTestId('steer-image')).toBeInTheDocument();
});
- it('opens the file preview dialog when a non-image steer attachment is clicked', () => {
- renderPart([{ file_id: 'f1', filename: 'notes.pdf', type: 'application/pdf' }]);
- expect(screen.queryByTestId('steer-file-preview')).toBeNull();
-
- fireEvent.click(screen.getByTestId('steer-file'));
- expect(screen.getByTestId('steer-file-preview')).toHaveTextContent('notes.pdf');
- });
+ it.each(['application/pdf', 'image/png'])(
+ 'opens extracted previews for %s attachments',
+ (type) => {
+ mockFileMap = { f1: { llmDeliveryPath: 'text' } };
+ renderPart([{ file_id: 'f1', filename: 'notes.pdf', type }]);
+ expect(screen.queryByTestId('steer-file-preview')).toBeNull();
+
+ fireEvent.click(screen.getByTestId('steer-file'));
+ expect(screen.getByTestId('steer-file-preview')).toHaveTextContent('notes.pdf');
+ expect(screen.getByTestId('steer-file-preview')).toHaveAttribute(
+ 'data-delivery-path',
+ 'text',
+ );
+ },
+ );
it('renders quoted excerpts as reference blocks inside the bubble', () => {
render(
diff --git a/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.lifecycle.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.lifecycle.test.tsx
new file mode 100644
index 0000000000..64757fc785
--- /dev/null
+++ b/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.lifecycle.test.tsx
@@ -0,0 +1,180 @@
+import React from 'react';
+import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
+import type { TFilePreview } from 'librechat-data-provider';
+import FilePreviewDialog from '../FilePreviewDialog';
+
+let mockFileMap: Record<string, { llmDeliveryPath: 'text' }> = {};
+let mockShareId: string | undefined;
+let mockPreview: TFilePreview | undefined;
+let mockPreviewError = false;
+const mockOwnedPreview = jest.fn();
+const mockSharedPreview = jest.fn();
+const mockDownload = jest.fn();
+const mockRetry = jest.fn();
+const mockRevoke = jest.fn();
+const mockTriggerDownload = jest.fn();
+const mockUseFilePreview = jest.fn();
+
+jest.mock('recoil', () => ({ useRecoilValue: () => ({ id: 'owner' }) }));
+jest.mock('~/store', () => ({ user: {} }));
+jest.mock('~/Providers', () => ({
+ useShareContext: () => ({ shareId: mockShareId }),
+ useFileMapContext: () => mockFileMap,
+}));
+jest.mock('~/hooks', () => ({ useLocalize: () => (key: string) => key }));
+jest.mock('~/utils', () => ({
+ getDownloadFilename: (name: string) => name,
+ logger: { error: jest.fn() },
+ sortPagesByRelevance: () => [],
+ triggerDownload: (...args: unknown[]) => mockTriggerDownload(...args),
+}));
+jest.mock('~/data-provider', () => ({
+ useFilePreview: (...args: unknown[]) => {
+ mockUseFilePreview(...args);
+ return {
+ data: mockPreview,
+ isInitialLoading: !mockPreview && !mockPreviewError,
+ isFetching: false,
+ isError: mockPreviewError,
+ refetch: mockRetry,
+ };
+ },
+ useFileDownload: (_user: string, _file: string, options: { purpose?: string }) => ({
+ refetch: options.purpose === 'preview' ? mockOwnedPreview : mockDownload,
+ }),
+ useFilePreviewBlob: (_user: string, _file: string, shareId?: string) => ({
+ refetch: shareId ? mockSharedPreview : mockOwnedPreview,
+ }),
+ useSharedFileDownload: (_share: string, _file: string, purpose?: string) => ({
+ refetch: purpose === 'preview' ? mockSharedPreview : mockDownload,
+ }),
+ revokeDownloadURL: (url: string) => mockRevoke(url),
+}));
+jest.mock('@librechat/client', () => ({
+ OGDialog: ({ open, children }: React.PropsWithChildren<{ open: boolean }>) =>
+ open ? <div>{children}</div> : null,
+ OGDialogContent: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
+ OGDialogTitle: ({ children }: React.PropsWithChildren) => <h2>{children}</h2>,
+ OGDialogDescription: ({ children }: React.PropsWithChildren) => <p>{children}</p>,
+ Button: ({ children, onClick }: React.PropsWithChildren<{ onClick: () => void }>) => (
+ <button onClick={onClick}>{children}</button>
+ ),
+}));
+jest.mock('~/components/Messages/Content/CopyButton', () => () => null);
+
+const props = {
+ open: true,
+ onOpenChange: jest.fn(),
+ fileId: 'f1',
+ fileName: 'report.pdf',
+ fileType: 'application/pdf',
+};
+
+describe('FilePreviewDialog lifecycle', () => {
+ it('renders an ordinary PDF and revokes its display URL on close', async () => {
+ const originalCreate = URL.createObjectURL;
+ const originalRevoke = URL.revokeObjectURL;
+ const revoke = jest.fn();
+ URL.createObjectURL = jest.fn(() => 'blob:display');
+ URL.revokeObjectURL = revoke;
+ try {
+ mockOwnedPreview.mockResolvedValue({ data: new Blob(['pdf']) });
+ const view = render(<FilePreviewDialog {...props} />);
+ await waitFor(() =>
+ expect(screen.getByTitle('com_ui_preview: report.pdf')).toHaveAttribute(
+ 'src',
+ 'blob:display',
+ ),
+ );
+ view.unmount();
+ expect(revoke).toHaveBeenCalledWith('blob:display');
+ } finally {
+ URL.createObjectURL = originalCreate;
+ URL.revokeObjectURL = originalRevoke;
+ }
+ });
+
+ beforeEach(() => {
+ jest.clearAllMocks();
+ mockFileMap = {};
+ mockShareId = undefined;
+ mockPreview = { file_id: 'f1', status: 'ready', text: 'extracted document' };
+ mockPreviewError = false;
+ mockOwnedPreview.mockResolvedValue({});
+ mockSharedPreview.mockResolvedValue({});
+ mockDownload.mockResolvedValue({ data: 'blob:original' });
+ });
+
+ it('renders extracted text and downloads the original object', async () => {
+ render(<FilePreviewDialog {...props} deliveryPath="text" />);
+ expect(screen.getByText('extracted document')).toBeInTheDocument();
+ expect(mockOwnedPreview).not.toHaveBeenCalled();
+ fireEvent.click(screen.getByRole('button', { name: 'com_ui_download report.pdf' }));
+ await waitFor(() =>
+ expect(mockTriggerDownload).toHaveBeenCalledWith('blob:original', 'report.pdf'),
+ );
+ });
+
+ it('handles pending, polling failure, retry, and empty success', () => {
+ mockPreview = { file_id: 'f1', status: 'pending' };
+ const view = render(<FilePreviewDialog {...props} deliveryPath="text" />);
+ expect(screen.getByText('com_ui_loading')).toBeInTheDocument();
+ mockPreviewError = true;
+ view.rerender(<FilePreviewDialog {...props} deliveryPath="text" />);
+ expect(screen.queryByText('com_ui_loading')).toBeNull();
+ fireEvent.click(screen.getByRole('button', { name: 'com_ui_retry' }));
+ expect(mockRetry).toHaveBeenCalledTimes(1);
+ mockPreviewError = false;
+ mockPreview = { file_id: 'f1', status: 'ready', text: '' };
+ view.rerender(<FilePreviewDialog {...props} deliveryPath="text" />);
+ expect(screen.queryByText('com_ui_preview_unavailable')).toBeNull();
+ });
+
+ it('recovers late metadata while open and discards the obsolete binary response', async () => {
+ let resolve!: (value: { data: Blob }) => void;
+ mockOwnedPreview.mockReturnValue(
+ new Promise((done) => {
+ resolve = done;
+ }),
+ );
+ const view = render(<FilePreviewDialog {...props} />);
+ mockFileMap = { f1: { llmDeliveryPath: 'text' } };
+ view.rerender(<FilePreviewDialog {...props} />);
+ expect(screen.getByText('extracted document')).toBeInTheDocument();
+ await act(async () => {
+ resolve({ data: new Blob(['obsolete']) });
+ });
+ expect(screen.queryByTitle('com_ui_preview: report.pdf')).toBeNull();
+ });
+
+ it('handles a deduplicated response after closing and reopening', async () => {
+ let resolve!: (value: { data: Blob }) => void;
+ mockOwnedPreview.mockReturnValue(
+ new Promise((done) => {
+ resolve = done;
+ }),
+ );
+ const textProps = { ...props, fileName: 'notes.txt', fileType: 'text/plain' };
+ const view = render(<FilePreviewDialog {...textProps} />);
+ view.rerender(<FilePreviewDialog {...textProps} open={false} />);
+ view.rerender(<FilePreviewDialog {...textProps} />);
+ expect(mockOwnedPreview).toHaveBeenCalledTimes(2);
+ await act(async () => {
+ resolve({ data: { text: async () => 'shared bytes' } as Blob });
+ });
+ expect(screen.getByText('shared bytes')).toBeInTheDocument();
+ expect(screen.queryByText('com_ui_preview_unavailable')).toBeNull();
+ });
+
+ it('uses only shared metadata and the shared authorization scope', async () => {
+ mockShareId = 'public-share';
+ mockFileMap = { f1: { llmDeliveryPath: 'text' } };
+ const view = render(<FilePreviewDialog {...props} />);
+ await waitFor(() => expect(mockSharedPreview).toHaveBeenCalledTimes(1));
+ expect(mockOwnedPreview).not.toHaveBeenCalled();
+ expect(screen.queryByText('extracted document')).toBeNull();
+ view.rerender(<FilePreviewDialog {...props} deliveryPath="text" />);
+ expect(mockUseFilePreview).toHaveBeenLastCalledWith('f1', { enabled: true }, 'public-share');
+ expect(screen.getByText('extracted document')).toBeInTheDocument();
+ });
+});
diff --git a/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.test.ts b/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.test.ts
index 6e8438dbb1..c8c0c9edb2 100644
--- a/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.test.ts
+++ b/client/src/components/Chat/Messages/Content/__tests__/FilePreviewDialog.test.ts
@@ -1,5 +1,10 @@
import { FileSources } from 'librechat-data-provider';
-import { getPreviewKind, shouldUseSharedFileDownload } from '../preview';
+import {
+ getPreviewKind,
+ isExtractedTextPreviewLoading,
+ shouldUseExtractedTextPreview,
+ shouldUseSharedFileDownload,
+} from '../preview';
import { getDownloadFilename } from '~/utils/downloadFile';
describe('FilePreviewDialog text-source behavior', () => {
@@ -17,6 +22,17 @@ describe('FilePreviewDialog text-source behavior', () => {
expect(getDownloadFilename('report.pdf', 'file-3', FileSources.local)).toBe('report.pdf');
});
+ it('uses stored extracted text when the delivery path is text', () => {
+ expect(shouldUseExtractedTextPreview('text')).toBe(true);
+ expect(shouldUseExtractedTextPreview('provider')).toBe(false);
+ expect(shouldUseExtractedTextPreview(undefined)).toBe(false);
+ });
+
+ it('stops showing a pending preview as loading when polling fails', () => {
+ expect(isExtractedTextPreviewLoading('pending', false, true)).toBe(false);
+ expect(isExtractedTextPreviewLoading('pending', false, false)).toBe(true);
+ });
+
it('routes any identified file through the share boundary in a shared view', () => {
expect(shouldUseSharedFileDownload('share-1', 'file-1')).toBe(true);
expect(shouldUseSharedFileDownload('share-1', undefined)).toBe(false);
diff --git a/client/src/components/Chat/Messages/Content/__tests__/Files.test.tsx b/client/src/components/Chat/Messages/Content/__tests__/Files.test.tsx
new file mode 100644
index 0000000000..267ecda3db
--- /dev/null
+++ b/client/src/components/Chat/Messages/Content/__tests__/Files.test.tsx
@@ -0,0 +1,40 @@
+/* eslint-disable i18next/no-literal-string */
+import React from 'react';
+import { fireEvent, render, screen } from '@testing-library/react';
+import type { TMessage } from 'librechat-data-provider';
+import Files from '../Files';
+
+jest.mock('~/Providers', () => ({
+ useFileMapContext: () => ({}),
+ useShareContext: () => ({}),
+}));
+jest.mock(
+ '~/components/Chat/Input/Files/FileContainer',
+ () =>
+ ({ onClick }: { onClick: () => void }) => <button onClick={onClick}>file action</button>,
+);
+jest.mock('../Image', () => () => <div>image preview</div>);
+jest.mock(
+ '../FilePreviewDialog',
+ () =>
+ ({ open, deliveryPath }: { open: boolean; deliveryPath?: string }) =>
+ open ? <div>preview: {deliveryPath}</div> : null,
+);
+
+it('exposes extracted image text without changing ordinary image previews', () => {
+ render(
+ <Files
+ message={
+ {
+ files: [
+ { file_id: 'text-image', type: 'image/png', llmDeliveryPath: 'text' },
+ { file_id: 'image', type: 'image/png' },
+ ],
+ } as TMessage
+ }
+ />,
+ );
+ expect(screen.getAllByText('image preview')).toHaveLength(1);
+ fireEvent.click(screen.getByText('file action'));
+ expect(screen.getByText('preview: text')).toBeInTheDocument();
+});
diff --git a/client/src/components/Chat/Messages/Content/preview.ts b/client/src/components/Chat/Messages/Content/preview.ts
index d369cbf597..45f64fa1d6 100644
--- a/client/src/components/Chat/Messages/Content/preview.ts
+++ b/client/src/components/Chat/Messages/Content/preview.ts
@@ -1,4 +1,5 @@
import { FileSources } from 'librechat-data-provider';
+import type { TFile } from 'librechat-data-provider';
type PreviewKind = 'pdf' | 'text' | false;
@@ -38,6 +39,18 @@ export function shouldUseSharedFileDownload(shareId?: string, fileId?: string):
return !!shareId && !!fileId;
}
+export function shouldUseExtractedTextPreview(deliveryPath?: TFile['llmDeliveryPath']): boolean {
+ return deliveryPath === 'text';
+}
+
+export function isExtractedTextPreviewLoading(
+ status: 'pending' | 'ready' | 'failed' | undefined,
+ isInitialLoading: boolean,
+ isError: boolean,
+): boolean {
+ return !isError && (isInitialLoading || status === 'pending');
+}
+
function getPreviewKindByMime(mime?: string): PreviewKind {
if (!mime) {
return false;
diff --git a/client/src/components/SidePanel/Files/PanelTable.tsx b/client/src/components/SidePanel/Files/PanelTable.tsx
index c46d6ba257..b526ce2ce1 100644
--- a/client/src/components/SidePanel/Files/PanelTable.tsx
+++ b/client/src/components/SidePanel/Files/PanelTable.tsx
@@ -11,6 +11,14 @@ import {
TableHeader,
useToastContext,
} from '@librechat/client';
+import {
+ megabyte,
+ mergeFileConfig,
+ checkOpenAIStorage,
+ isAssistantsEndpoint,
+ getEndpointFileConfig,
+ fileConfig as defaultFileConfig,
+} from 'librechat-data-provider';
import {
flexRender,
getCoreRowModel,
@@ -23,14 +31,6 @@ import {
type VisibilityState,
type ColumnFiltersState,
} from '@tanstack/react-table';
-import {
- megabyte,
- mergeFileConfig,
- checkOpenAIStorage,
- isAssistantsEndpoint,
- getEndpointFileConfig,
- fileConfig as defaultFileConfig,
-} from 'librechat-data-provider';
import type { TFile } from 'librechat-data-provider';
import { MyFilesModal } from '~/components/Chat/Input/Files/MyFilesModal';
import { useFileMapContext, useChatContext } from '~/Providers';
@@ -197,6 +197,7 @@ export default function DataTable<TData, TValue>({ columns, data }: DataTablePro
source: fileData.source,
size: fileData.bytes,
metadata: fileData.metadata,
+ llmDeliveryPath: fileData.llmDeliveryPath,
});
},
[addFile, files, fileMap, conversation, localize, showToast, fileConfig],
diff --git a/client/src/components/SidePanel/Files/__tests__/PanelTable.spec.tsx b/client/src/components/SidePanel/Files/__tests__/PanelTable.spec.tsx
index b7ebba77c6..3039820c3b 100644
--- a/client/src/components/SidePanel/Files/__tests__/PanelTable.spec.tsx
+++ b/client/src/components/SidePanel/Files/__tests__/PanelTable.spec.tsx
@@ -170,6 +170,7 @@ describe('PanelTable handleFileClick', () => {
filename: 'report.pdf',
context: FileContext.run_artifact,
metadata: { runFile },
+ llmDeliveryPath: 'text',
});
mockFileMap = { [file.file_id]: file };
@@ -182,6 +183,7 @@ describe('PanelTable handleFileClick', () => {
filename: 'report.pdf',
filepath: file.filepath,
metadata: { runFile },
+ llmDeliveryPath: 'text',
attached: true,
progress: 1,
}),
diff --git a/client/src/data-provider/Files/previewBlob.test.tsx b/client/src/data-provider/Files/previewBlob.test.tsx
new file mode 100644
index 0000000000..34b2dc7b02
--- /dev/null
+++ b/client/src/data-provider/Files/previewBlob.test.tsx
@@ -0,0 +1,50 @@
+import React from 'react';
+import { dataService } from 'librechat-data-provider';
+import { act, renderHook } from '@testing-library/react';
+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
+import { useFilePreviewBlob } from './queries';
+
+jest.mock('librechat-data-provider', () => ({
+ ...jest.requireActual('librechat-data-provider'),
+ dataService: { getFileDownload: jest.fn(), getSharedFileDownload: jest.fn() },
+}));
+jest.mock('~/store', () => ({}));
+jest.mock('~/utils', () => ({}));
+jest.mock('~/common', () => ({}));
+
+describe('preview blob ownership', () => {
+ afterEach(() => jest.clearAllMocks());
+
+ it.each([undefined, 'share-1'])(
+ 'shares bytes across deduplicated requests (%s)',
+ async (shareId) => {
+ let resolve!: (value: { data: Blob }) => void;
+ const pending = new Promise<{ data: Blob }>((done) => {
+ resolve = done;
+ });
+ const owned = jest.mocked(dataService.getFileDownload).mockReturnValue(pending as never);
+ const shared = jest
+ .mocked(dataService.getSharedFileDownload)
+ .mockReturnValue(pending as never);
+ const client = new QueryClient();
+ const wrapper = ({ children }: React.PropsWithChildren) => (
+ <QueryClientProvider client={client}>{children}</QueryClientProvider>
+ );
+ const first = renderHook(() => useFilePreviewBlob('owner', 'f1', shareId), { wrapper });
+ const second = renderHook(() => useFilePreviewBlob('owner', 'f1', shareId), { wrapper });
+ const blob = new Blob(['preview']);
+ await act(async () => {
+ const a = first.result.current.refetch();
+ const b = second.result.current.refetch();
+ resolve({ data: blob });
+ expect((await a).data).toBe(blob);
+ expect((await b).data).toBe(blob);
+ });
+ expect(shareId ? shared : owned).toHaveBeenCalledTimes(1);
+ expect(shareId ? owned : shared).not.toHaveBeenCalled();
+ first.unmount();
+ second.unmount();
+ client.clear();
+ },
+ );
+});
diff --git a/client/src/data-provider/Files/queries.ts b/client/src/data-provider/Files/queries.ts
index da6981571e..3c455a4bef 100644
--- a/client/src/data-provider/Files/queries.ts
+++ b/client/src/data-provider/Files/queries.ts
@@ -161,6 +161,26 @@ export const useSharedFileDownload = (
);
};
+/** Preview consumers share immutable bytes, never a revocable download URL. */
+export const useFilePreviewBlob = (
+ userId?: string,
+ fileId?: string,
+ shareId?: string,
+): QueryObserverResult<Blob> =>
+ useQuery(
+ [QueryKeys.fileDownload, 'previewBlob', shareId ? 'share' : 'owner', shareId ?? userId, fileId],
+ async () => {
+ if (!fileId || (!shareId && !userId)) {
+ throw new Error('Preview identity unavailable');
+ }
+ const response = shareId
+ ? await dataService.getSharedFileDownload(shareId, fileId)
+ : await dataService.getFileDownload(userId!, fileId);
+ return response.data;
+ },
+ { enabled: false, retry: false, cacheTime: 0 },
+ );
+
export const useCodeOutputDownload = (url = ''): QueryObserverResult<string> => {
return useQuery(
[QueryKeys.fileDownload, url],
diff --git a/client/src/hooks/Chat/__tests__/useSteerConvert.spec.tsx b/client/src/hooks/Chat/__tests__/useSteerConvert.spec.tsx
index b1eb60a204..98adf2d924 100644
--- a/client/src/hooks/Chat/__tests__/useSteerConvert.spec.tsx
+++ b/client/src/hooks/Chat/__tests__/useSteerConvert.spec.tsx
@@ -5,6 +5,8 @@ import useSteerConvert from '../useSteerConvert';
import store from '~/store';
const mockFetchStreamStatus = jest.fn();
+let mockFileMap: Record<string, { llmDeliveryPath: 'text' }> = {};
+jest.mock('~/Providers', () => ({ useFileMapContext: () => mockFileMap }));
jest.mock('~/data-provider', () => ({
fetchStreamStatus: (...args: unknown[]) => mockFetchStreamStatus(...args),
getGenerationProtocolVersion: (value: unknown) =>
@@ -45,6 +47,47 @@ function setup(initialize?: (snapshot: MutableSnapshot) => void) {
}
describe('useSteerConvert', () => {
+ beforeEach(() => {
+ mockFileMap = {};
+ });
+
+ it.each([false, true])(
+ 'hydrates late metadata during terminal recovery (queued origin: %s)',
+ (fromQueue) => {
+ const files = [{ file_id: 'f1', filename: 'notes.pdf' }];
+ const { result, rerender } = setup(({ set }) => {
+ set(store.pendingSteersByConvoId(CONVO_ID), [
+ {
+ steerId: 'legacy',
+ text: 'recover',
+ status: 'pending',
+ createdAt: 1,
+ files,
+ ...(fromQueue && {
+ queuedOrigin: {
+ item: { id: 'original', text: 'recover', createdAt: 1, files },
+ beforeIds: [],
+ afterIds: [],
+ },
+ }),
+ },
+ ]);
+ });
+ const retainedCallback = result.current.convert;
+ mockFileMap = { f1: { llmDeliveryPath: 'text' } };
+ rerender();
+ act(() => {
+ retainedCallback(CONVO_ID, [{ steerId: 'legacy', text: 'recover', files }]);
+ });
+ expect(result.current.queue[0].files).toEqual([{ ...files[0], llmDeliveryPath: 'text' }]);
+ expect(result.current.chips).toEqual([]);
+ act(() => {
+ retainedCallback(CONVO_ID, [{ steerId: 'legacy', text: 'recover', files }]);
+ });
+ expect(result.current.queue).toHaveLength(1);
+ },
+ );
+
it('converts leftover steers to queued chips and drops their pending chips', () => {
const { result } = setup(({ set }) => {
set(store.pendingSteersByConvoId(CONVO_ID), [
diff --git a/client/src/hooks/Chat/__tests__/useSteering.spec.tsx b/client/src/hooks/Chat/__tests__/useSteering.spec.tsx
index 5ad9353f15..56c9d3422a 100644
--- a/client/src/hooks/Chat/__tests__/useSteering.spec.tsx
+++ b/client/src/hooks/Chat/__tests__/useSteering.spec.tsx
@@ -2,10 +2,10 @@ import React from 'react';
import { act, renderHook, waitFor } from '@testing-library/react';
import { RecoilRoot, useRecoilValue, useSetRecoilState, type MutableSnapshot } from 'recoil';
import { Constants, ContentTypes, EModelEndpoint, LocalStorageKeys } from 'librechat-data-provider';
-import type { TConversation, TMessage } from 'librechat-data-provider';
+import type { TConversation, TFile, TMessage } from 'librechat-data-provider';
import type { QueuedMessage } from '~/store/families';
+import useSteering, { mergeQueuedTurnFileMetadata } from '../useSteering';
import useQueueDrain from '../useQueueDrain';
-import useSteering from '../useSteering';
import store from '~/store';
const CONVO_ID = 'convo-steer-ui';
@@ -20,8 +20,13 @@ const mockMarkUsage = jest.fn();
let mockMessages: TMessage[] | undefined;
let mockLatestMessage: TMessage | null | undefined;
let mockServerQueuedTurns: unknown[] | undefined;
+let mockFileMap: Record<string, Pick<TFile, 'llmDeliveryPath'>> = {};
const mockUseAgentQueuedTurns = jest.fn((..._args: unknown[]) => ({ data: mockServerQueuedTurns }));
+jest.mock('~/Providers', () => ({
+ useFileMapContext: () => mockFileMap,
+}));
+
jest.mock('~/data-provider', () => ({
useCancelSteerMutation: () => ({ mutateAsync: mockCancelSteer }),
useSteerMessageMutation: () => ({ mutate: mockMutate }),
@@ -133,6 +138,51 @@ describe('useSteering', () => {
mockMessages = undefined;
mockLatestMessage = undefined;
mockServerQueuedTurns = undefined;
+ mockFileMap = {};
+ });
+
+ it('keeps optimistic delivery metadata when a legacy receipt omits it', () => {
+ expect(
+ mergeQueuedTurnFileMetadata(
+ [{ file_id: 'stored-doc', filename: 'report.pdf', type: 'application/pdf' }],
+ [
+ {
+ file_id: 'stored-doc',
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ llmDeliveryPath: 'text',
+ },
+ ],
+ ),
+ ).toEqual([
+ {
+ file_id: 'stored-doc',
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ llmDeliveryPath: 'text',
+ },
+ ]);
+ });
+
+ it('hydrates legacy receipt delivery metadata from the stored file map', () => {
+ expect(
+ mergeQueuedTurnFileMetadata(
+ [{ file_id: 'stored-doc', filename: 'report.pdf', type: 'application/pdf' }],
+ undefined,
+ {
+ 'stored-doc': {
+ llmDeliveryPath: 'text',
+ },
+ },
+ ),
+ ).toEqual([
+ {
+ file_id: 'stored-doc',
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ llmDeliveryPath: 'text',
+ },
+ ]);
});
describe('effectiveAction', () => {
@@ -914,6 +964,11 @@ describe('useSteering', () => {
});
it('projects the authoritative server snapshot into Recoil', async () => {
+ mockFileMap = {
+ 'stored-doc': {
+ llmDeliveryPath: 'text',
+ },
+ };
mockServerQueuedTurns = [
{
queuedTurnId: 'server-snapshot-1',
@@ -921,6 +976,7 @@ describe('useSteering', () => {
conversationId: CONVO_ID,
parentMessageId: 'visible-assistant-tail',
text: 'restored after reload',
+ files: [{ file_id: 'stored-doc', filename: 'report.pdf', type: 'application/pdf' }],
status: 'queued',
revision: 3,
createdAt: new Date(200).toISOString(),
@@ -933,6 +989,14 @@ describe('useSteering', () => {
expect(rendered.result.current.queue[0]).toMatchObject({
id: 'client-snapshot-1',
text: 'restored after reload',
+ files: [
+ {
+ file_id: 'stored-doc',
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ llmDeliveryPath: 'text',
+ },
+ ],
server: { id: 'server-snapshot-1', status: 'queued', revision: 3 },
});
});
diff --git a/client/src/hooks/Chat/useChatFunctions.ts b/client/src/hooks/Chat/useChatFunctions.ts
index 80eca799dc..fb91b23feb 100644
--- a/client/src/hooks/Chat/useChatFunctions.ts
+++ b/client/src/hooks/Chat/useChatFunctions.ts
@@ -611,6 +611,7 @@ export default function useChatFunctions({
filepath: file.filepath,
filename: file.filename,
type: file.type ?? '', // Ensure type is not undefined
+ llmDeliveryPath: file.llmDeliveryPath,
height: file.height,
width: file.width,
}));
diff --git a/client/src/hooks/Chat/useSteerConvert.ts b/client/src/hooks/Chat/useSteerConvert.ts
index df6ecc22b5..2cc711f223 100644
--- a/client/src/hooks/Chat/useSteerConvert.ts
+++ b/client/src/hooks/Chat/useSteerConvert.ts
@@ -1,12 +1,18 @@
-import { useCallback } from 'react';
+import { useCallback, useRef } from 'react';
import { v4 } from 'uuid';
import { useRecoilCallback } from 'recoil';
import type { TPendingSteer } from 'librechat-data-provider';
import type { QueuedMessage, QueuedMessageOrigin } from '~/store/families';
import type { GenerationProtocolVersion } from '~/data-provider';
import type { SteerCarriedContext } from '~/utils';
-import { appendAppliedSteerIds, carriedSteerContext, insertQueuedOrigin } from '~/utils';
+import {
+ appendAppliedSteerIds,
+ carriedSteerContext,
+ insertQueuedOrigin,
+ hydrateFileDeliveryMetadata,
+} from '~/utils';
import { fetchStreamStatus, getGenerationProtocolVersion } from '~/data-provider';
+import { useFileMapContext } from '~/Providers';
import store from '~/store';
/** A server-reported steer, or a local one that carries its own client-only
@@ -47,6 +53,9 @@ interface SteerConvertOptions {
* server-side removal.
*/
export default function useSteerConvert() {
+ const fileMap = useFileMapContext();
+ const fileMapRef = useRef(fileMap);
+ fileMapRef.current = fileMap;
const convert = useRecoilCallback(
({ snapshot, set }) =>
(
@@ -129,6 +138,11 @@ export default function useSteerConvert() {
const local = localChipFor(steer);
const source = local ?? steer;
const queuedOrigin = source.queuedOrigin;
+ const files = hydrateFileDeliveryMetadata(
+ queuedOrigin?.item.files ?? steer.files,
+ local?.files,
+ fileMapRef.current,
+ );
const recoveryFields = bindRecoverySource
? {
// One UUID is stable for this queued attempt and all of
@@ -142,13 +156,13 @@ export default function useSteerConvert() {
: {};
const item =
queuedOrigin != null
- ? { ...queuedOrigin.item, ...recoveryFields }
+ ? { ...queuedOrigin.item, ...recoveryFields, ...(files && { files }) }
: ({
id: steer.steerId,
text: steer.text,
createdAt: steer.createdAt ?? Date.now(),
...recoveryFields,
- ...(steer.files && steer.files.length > 0 && { files: steer.files }),
+ ...(files && files.length > 0 && { files }),
// The chip is the usual source, but a reclaimed steer may
// have lost its chip to a competing cancel mid-round-trip.
...carriedSteerContext(source),
diff --git a/client/src/hooks/Chat/useSteering.ts b/client/src/hooks/Chat/useSteering.ts
index 9701d4d132..56e9520d19 100644
--- a/client/src/hooks/Chat/useSteering.ts
+++ b/client/src/hooks/Chat/useSteering.ts
@@ -40,11 +40,13 @@ import {
clearAllDrafts,
getPendingDraftId,
insertQueuedOrigin,
+ hydrateFileDeliveryMetadata,
mergeRestagedQuotes,
} from '~/utils';
import useSteerConvert from '~/hooks/Chat/useSteerConvert';
import { useLatestMessage } from '~/hooks/Messages';
import { useSetFilesToDelete } from '~/hooks/Files';
+import { useFileMapContext } from '~/Providers';
import useLocalize from '~/hooks/useLocalize';
import store from '~/store';
@@ -259,6 +261,7 @@ function toQueuedTurnFileRefs(files: TMessage['files']): TAgentQueuedTurnFileRef
...(file.type != null && { type: file.type }),
...(file.filepath != null && { filepath: file.filepath }),
...(file.filename != null && { filename: file.filename }),
+ ...(file.llmDeliveryPath != null && { llmDeliveryPath: file.llmDeliveryPath }),
...(file.height != null && { height: file.height }),
...(file.width != null && { width: file.width }),
...(file.bytes != null && { bytes: file.bytes }),
@@ -268,11 +271,14 @@ function toQueuedTurnFileRefs(files: TMessage['files']): TAgentQueuedTurnFileRef
return refs.length > 0 ? refs : undefined;
}
+export { hydrateFileDeliveryMetadata as mergeQueuedTurnFileMetadata } from '~/utils/files';
+
function reconcileServerQueuedTurns(
previous: QueuedMessage[],
receipts: AgentQueuedTurnReceipt[],
settledByRequestId: ReadonlyMap<string, SettledQueuedTurnReceipt>,
authoritativeSnapshot = true,
+ storedFiles?: Parameters<typeof hydrateFileDeliveryMetadata>[2],
): QueuedMessage[] {
const previousByClientRequestId = new Map(
previous.flatMap((item) =>
@@ -305,6 +311,7 @@ function reconcileServerQueuedTurns(
} else if (receipt.status === 'queued' || receipt.status === 'claimed') {
status = receipt.status;
}
+ const files = hydrateFileDeliveryMetadata(receipt.files, optimistic?.files, storedFiles);
return [
{
id: optimistic?.id ?? receipt.clientRequestId,
@@ -315,7 +322,7 @@ function reconcileServerQueuedTurns(
...(receipt.expectedPredecessorCreatedAt != null && {
expectedPredecessorCreatedAt: receipt.expectedPredecessorCreatedAt,
}),
- ...(receipt.files != null && receipt.files.length > 0 && { files: receipt.files }),
+ ...(files != null && files.length > 0 && { files }),
...(receipt.quotes != null && receipt.quotes.length > 0 && { quotes: receipt.quotes }),
...(receipt.manualSkills != null &&
receipt.manualSkills.length > 0 && {
@@ -411,6 +418,7 @@ export default function useSteering({
}: UseSteeringParams) {
const localize = useLocalize();
const { showToast } = useToastContext();
+ const fileMap = useFileMapContext();
const setFilesToDelete = useSetFilesToDelete();
const convertSteersToQueued = useSteerConvert();
/** `mutate` is a stable callback; the mutation result objects are fresh
@@ -525,11 +533,12 @@ export default function useSteering({
receipts,
terminalForReconciliation,
source === 'snapshot',
+ fileMap,
),
);
}
},
- [queueKey],
+ [fileMap, queueKey],
);
const finishQueuedTurnEnqueue = useRecoilCallback(
@@ -1095,6 +1104,7 @@ export default function useSteering({
// the attachment into the composer with its real name and size.
filename: file.filename,
bytes: file.size,
+ llmDeliveryPath: file.llmDeliveryPath,
}));
setFiles(new Map());
setFilesToDelete({});
diff --git a/client/src/hooks/Files/__tests__/useFileHandling.test.ts b/client/src/hooks/Files/__tests__/useFileHandling.test.ts
index e741324f7d..564b7279f6 100644
--- a/client/src/hooks/Files/__tests__/useFileHandling.test.ts
+++ b/client/src/hooks/Files/__tests__/useFileHandling.test.ts
@@ -17,6 +17,7 @@ type MockUploadMutationOptions = {
filename: string;
source: string;
embedded: boolean;
+ llmDeliveryPath?: string;
height?: number;
width?: number;
},
@@ -1565,6 +1566,45 @@ describe('useFileHandling', () => {
expect(onSuccess).toHaveBeenCalledWith(fileId);
});
+ it('keeps the upload delivery path on the completed attachment', async () => {
+ jest.useFakeTimers();
+ const useFileHandling = await loadHook();
+ const { result } = renderHook(() => useFileHandling());
+
+ await act(async () => {
+ await result.current.handleFiles([
+ new File(['hello'], 'notes.docx', {
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ }),
+ ]);
+ });
+
+ const uploadBody = mockMutate.mock.calls[0][0] as FormData;
+ act(() => {
+ mockUploadOptions.onSuccess?.(
+ {
+ temp_file_id: uploadBody.get('file_id') as string,
+ file_id: 'saved-file-id',
+ filepath: '/files/notes.docx',
+ type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
+ filename: 'notes.docx',
+ source: 'local',
+ embedded: false,
+ llmDeliveryPath: 'text',
+ },
+ uploadBody,
+ );
+ jest.runAllTimers();
+ });
+ jest.useRealTimers();
+
+ const [, completion] = mockUpdateFileById.mock.calls.at(-1) as [
+ string,
+ { llmDeliveryPath?: string },
+ ];
+ expect(completion.llmDeliveryPath).toBe('text');
+ });
+
it('resolves false when every file fails preprocessing', async () => {
const consoleLog = jest.spyOn(console, 'log').mockImplementation(() => undefined);
mockProcessFileForUpload.mockRejectedValue(new Error('HEIC conversion failed'));
diff --git a/client/src/hooks/Files/useFileHandling.ts b/client/src/hooks/Files/useFileHandling.ts
index 51e8608c6e..c498c7d267 100644
--- a/client/src/hooks/Files/useFileHandling.ts
+++ b/client/src/hooks/Files/useFileHandling.ts
@@ -311,6 +311,7 @@ const useFileHandlingCore = (params: UseFileHandling | undefined, fileState: Fil
filename: data.filename,
source: data.source,
embedded: data.embedded,
+ llmDeliveryPath: data.llmDeliveryPath,
},
assistant_id ? true : false,
);
diff --git a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
index 0521479130..6365f2097e 100644
--- a/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
+++ b/client/src/hooks/SSE/__tests__/useResumeOnLoad.spec.tsx
@@ -16,6 +16,11 @@ const mockUseStreamStatus = jest.fn();
const mockUseActiveJobs = jest.fn();
const mockUseAgentQueuedTurns = jest.fn();
const mockExtendActiveJobsGrace = jest.fn();
+let mockFileMap: Record<string, { llmDeliveryPath?: 'provider' | 'text' | 'none' }> = {};
+
+jest.mock('~/Providers', () => ({
+ useFileMapContext: () => mockFileMap,
+}));
jest.mock('~/data-provider', () => ({
useStreamStatus: (conversationId: string | undefined, enabled: boolean) =>
@@ -213,6 +218,7 @@ describe('useResumeOnLoad', () => {
mockUseAgentQueuedTurns.mockReset();
mockUseAgentQueuedTurns.mockReturnValue({ data: [], dataUpdatedAt: 1 });
mockExtendActiveJobsGrace.mockReset();
+ mockFileMap = {};
});
afterEach(() => {
diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.ts
index ae6a11c378..2050e2799c 100644
--- a/client/src/hooks/SSE/useResumableSSE.ts
+++ b/client/src/hooks/SSE/useResumableSSE.ts
@@ -67,6 +67,7 @@ import {
markStreamStartFailedMetadata,
findPendingActionMessageIndex,
insertQueuedOrigin,
+ hydrateFileDeliveryMetadata,
} from '~/utils';
import {
useGetUserBalance,
@@ -87,6 +88,7 @@ import useEventHandlers, {
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import useSteerConvert from '~/hooks/Chat/useSteerConvert';
import { useAuthContext } from '~/hooks/AuthContext';
+import { useFileMapContext } from '~/Providers';
import useUsageHandler from './useUsageHandler';
import store from '~/store';
@@ -762,6 +764,9 @@ export default function useResumableSSE(
const setActiveRunId = useSetRecoilState(store.activeRunFamily(runIndex));
const { token, isAuthenticated } = useAuthContext();
+ const fileMap = useFileMapContext();
+ const fileMapRef = useRef(fileMap);
+ fileMapRef.current = fileMap;
const { setMessages, getMessages, setConversation, setIsSubmitting, newConversation } =
chatHelpers;
@@ -1058,13 +1063,18 @@ export default function useResumableSSE(
const keepLocalPreempt =
(localChip?.preemptRevision ?? 0) > (steer.preemptRevision ?? 0);
const chipGenerationCreatedAt = generationCreatedAt ?? localChip?.generationCreatedAt;
+ const restoredFiles = hydrateFileDeliveryMetadata(
+ steer.files,
+ localChip?.files,
+ fileMapRef.current,
+ );
return {
steerId: steer.steerId,
...(steer.clientSteerId && { clientSteerId: steer.clientSteerId }),
text: steer.text,
status: 'pending' as const,
createdAt: steer.createdAt ?? Date.now(),
- ...(steer.files && steer.files.length > 0 && { files: steer.files }),
+ ...(restoredFiles && restoredFiles.length > 0 && { files: restoredFiles }),
...((keepLocalPreempt ? localChip?.preempt : steer.preempt) === true && {
preempt: true,
}),
diff --git a/client/src/hooks/SSE/useResumeOnLoad.ts b/client/src/hooks/SSE/useResumeOnLoad.ts
index 185bfe0e9c..9edb747fa6 100644
--- a/client/src/hooks/SSE/useResumeOnLoad.ts
+++ b/client/src/hooks/SSE/useResumeOnLoad.ts
@@ -20,6 +20,7 @@ import {
applyPendingAction,
carriedSteerContext,
getBranchSiblingIndexesForTarget,
+ hydrateFileDeliveryMetadata,
} from '~/utils';
import {
useStreamStatus,
@@ -37,6 +38,7 @@ import {
import { siblingIdxFamily, siblingKey } from '~/components/Chat/Messages/Thread/state';
import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';
import useSteerConvert from '~/hooks/Chat/useSteerConvert';
+import { useFileMapContext } from '~/Providers';
import store from '~/store';
/**
@@ -257,6 +259,7 @@ export default function useResumeOnLoad(
runIndex = 0,
messagesLoaded = true,
) {
+ const fileMap = useFileMapContext();
const jotaiStore = useStore();
const queryClient = useQueryClient();
const setSubmission = useSetRecoilState(store.submissionByIndex(runIndex));
@@ -353,13 +356,18 @@ export default function useResumeOnLoad(
const keepLocalPreempt =
(localChip?.preemptRevision ?? 0) > (steer.preemptRevision ?? 0);
const chipGenerationCreatedAt = generationCreatedAt ?? localChip?.generationCreatedAt;
+ const restoredFiles = hydrateFileDeliveryMetadata(
+ steer.files,
+ localChip?.files,
+ fileMap,
+ );
return {
steerId: steer.steerId,
...(steer.clientSteerId && { clientSteerId: steer.clientSteerId }),
text: steer.text,
status: 'pending' as const,
createdAt: steer.createdAt ?? Date.now(),
- ...(steer.files && steer.files.length > 0 && { files: steer.files }),
+ ...(restoredFiles && restoredFiles.length > 0 && { files: restoredFiles }),
...((keepLocalPreempt ? localChip?.preempt : steer.preempt) === true && {
preempt: true,
}),
@@ -384,7 +392,7 @@ export default function useResumeOnLoad(
];
});
},
- [],
+ [fileMap],
);
const settleAppliedSteerParts = useRecoilCallback(
diff --git a/client/src/utils/files.ts b/client/src/utils/files.ts
index 857742f98a..5692905d66 100644
--- a/client/src/utils/files.ts
+++ b/client/src/utils/files.ts
@@ -23,6 +23,7 @@ import {
} from 'librechat-data-provider';
import type {
TFile,
+ TMessage,
DeleteFilesResponse,
EndpointFileConfig,
FileConfig,
@@ -34,6 +35,45 @@ import type { ExtendedFile } from '~/common';
export const partialTypes = ['text/x-'];
+/** Text-routed images use the file action so their extracted preview is reachable. */
+export function usesImagePreview(file: Partial<Pick<TFile, 'type' | 'llmDeliveryPath'>>): boolean {
+ return file.type?.startsWith('image/') === true && file.llmDeliveryPath !== 'text';
+}
+
+export type FileDeliveryMetadataMap = Readonly<
+ Record<string, Pick<TFile, 'llmDeliveryPath'> | undefined>
+>;
+
+/** Restores display-only delivery metadata that an older replica may have
+ * omitted from a persisted attachment ref. The persisted ref wins, followed
+ * by the matching process-local ref, then the owner-scoped stored file map. */
+export function hydrateFileDeliveryMetadata(
+ persistedFiles: TMessage['files'],
+ localFiles?: TMessage['files'],
+ storedFiles?: FileDeliveryMetadataMap,
+): TMessage['files'] {
+ if (persistedFiles == null || persistedFiles.length === 0) {
+ return persistedFiles;
+ }
+ const localById = new Map(
+ (localFiles ?? []).flatMap((file) =>
+ file.file_id != null ? [[file.file_id, file] as const] : [],
+ ),
+ );
+ let changed = false;
+ const hydrated = persistedFiles.map((file) => {
+ const local = file.file_id == null ? undefined : localById.get(file.file_id);
+ const stored = file.file_id == null ? undefined : storedFiles?.[file.file_id];
+ const llmDeliveryPath = local?.llmDeliveryPath ?? stored?.llmDeliveryPath;
+ if (file.llmDeliveryPath != null || llmDeliveryPath == null) {
+ return file;
+ }
+ changed = true;
+ return { ...file, llmDeliveryPath };
+ });
+ return changed ? hydrated : persistedFiles;
+}
+
export function hasIncompleteFiles(files: Map<string, ExtendedFile>): boolean {
for (const file of files.values()) {
if (file.progress < 1) {
diff --git a/packages/api/src/agents/steering/__tests__/request.spec.ts b/packages/api/src/agents/steering/__tests__/request.spec.ts
index d3e694e11e..85958885a7 100644
--- a/packages/api/src/agents/steering/__tests__/request.spec.ts
+++ b/packages/api/src/agents/steering/__tests__/request.spec.ts
@@ -264,6 +264,7 @@ describe('handleSteerRequest (real in-memory job manager)', () => {
height: 10,
width: 20,
bytes: 999,
+ llmDeliveryPath: 'text',
user: 'someone-else',
embedded: true,
},
@@ -292,6 +293,7 @@ describe('handleSteerRequest (real in-memory job manager)', () => {
height: 10,
width: 20,
bytes: 999,
+ llmDeliveryPath: 'text',
},
]);
});
@@ -388,6 +390,7 @@ describe('handleSteerRequest (real in-memory job manager)', () => {
height: 4,
width: 6,
bytes: 111,
+ llmDeliveryPath: 'text',
user: 'user-1',
} as unknown as IMongoFile;
@@ -450,6 +453,7 @@ describe('handleSteerRequest (real in-memory job manager)', () => {
height: 4,
width: 6,
bytes: 111,
+ llmDeliveryPath: 'text',
},
]);
});
@@ -916,6 +920,57 @@ describe('generation protocol bridge for steering mutations', () => {
expect(plain?.requestedQuotesFingerprint).toBeUndefined();
});
+ it('keeps receipt fingerprints stable when display delivery metadata is added', async () => {
+ const streamId = 'steer-protocol-v2-delivery-path-fingerprint';
+ await GenerationJobManager.createJob(streamId, user.id, undefined, {
+ initialMetadata: { generationProtocolVersion: 2 },
+ });
+ const base = {
+ conversationId: streamId,
+ text: 'same attachment steer',
+ files: [{ file_id: 'f1', type: 'application/pdf', filename: 'report.pdf' }],
+ };
+
+ await handleSteerRequest(
+ user,
+ { ...base, clientSteerId: 'client-legacy-file-ref' },
+ {
+ generationProtocolVersion: 2,
+ },
+ );
+ await handleSteerRequest(
+ user,
+ {
+ ...base,
+ clientSteerId: 'client-current-file-ref',
+ files: [{ ...base.files[0], llmDeliveryPath: 'text' }],
+ },
+ { generationProtocolVersion: 2 },
+ );
+
+ const legacy = await GenerationJobManager.steering.getReceipt(
+ streamId,
+ 'client-legacy-file-ref',
+ );
+ const current = await GenerationJobManager.steering.getReceipt(
+ streamId,
+ 'client-current-file-ref',
+ );
+ expect(current?.fingerprint).toBe(legacy?.fingerprint);
+ for (const [clientSteerId, files] of [
+ ['client-legacy-file-ref', [{ ...base.files[0], llmDeliveryPath: 'text' }]],
+ ['client-current-file-ref', base.files],
+ ] as const) {
+ const replay = await handleSteerRequest(
+ user,
+ { ...base, clientSteerId, files },
+ { generationProtocolVersion: 2 },
+ );
+ expect(replay.body).toMatchObject({ replayed: true });
+ }
+ expect(await GenerationJobManager.steering.peek(streamId)).toHaveLength(2);
+ });
+
it('treats quotes as part of the idempotency identity', async () => {
const streamId = 'steer-protocol-v2-quote-fingerprint';
await GenerationJobManager.createJob(streamId, user.id, undefined, {
diff --git a/packages/api/src/agents/steering/refs.ts b/packages/api/src/agents/steering/refs.ts
index eb5f3d7691..e1c28f43d6 100644
--- a/packages/api/src/agents/steering/refs.ts
+++ b/packages/api/src/agents/steering/refs.ts
@@ -5,6 +5,10 @@ export interface SteerRequestUser {
tenantId?: string;
}
+function isLlmDeliveryPath(value: unknown): value is NonNullable<TFile['llmDeliveryPath']> {
+ return value === 'provider' || value === 'text' || value === 'none';
+}
+
/**
* Copies the display-metadata fields a steer attachment ref may carry,
* dropping everything else. The single source of truth for the ref shape —
@@ -29,6 +33,9 @@ export function toSteerFileRef(raw: unknown): Partial<TFile> | null {
...(typeof candidate.type === 'string' && { type: candidate.type }),
...(typeof candidate.filepath === 'string' && { filepath: candidate.filepath }),
...(typeof candidate.filename === 'string' && { filename: candidate.filename }),
+ ...(isLlmDeliveryPath(candidate.llmDeliveryPath) && {
+ llmDeliveryPath: candidate.llmDeliveryPath,
+ }),
...(typeof candidate.height === 'number' && { height: candidate.height }),
...(typeof candidate.width === 'number' && { width: candidate.width }),
...(typeof candidate.bytes === 'number' && { bytes: candidate.bytes }),
diff --git a/packages/api/src/agents/steering/request.ts b/packages/api/src/agents/steering/request.ts
index e380535ac4..90f3319ba4 100644
--- a/packages/api/src/agents/steering/request.ts
+++ b/packages/api/src/agents/steering/request.ts
@@ -235,18 +235,22 @@ function hasTenantMismatch(
return metadata?.tenantId != null && metadata.tenantId !== user.tenantId;
}
-/** DELIBERATELY quote-independent: this exact 3-field hash is what EVERY
- * deployed replica version computes, so a lost-ACK retry can replay its
- * receipt no matter which replica wrote it or reads it. Quote identity is
- * enforced separately via `SteerReceipt.requestedQuotesFingerprint`, which
- * only quote-aware readers consult. */
+/** DELIBERATELY quote-independent and display-metadata-independent: the
+ * legacy 3-field hash saw these same file refs before `llmDeliveryPath` was
+ * added, so a lost-ACK retry can replay its receipt across rollout versions.
+ * Quote identity is enforced separately via
+ * `SteerReceipt.requestedQuotesFingerprint`, which only quote-aware readers
+ * consult. */
function steerFingerprint(
text: string,
files: Partial<TFile>[] | undefined,
preempt: boolean,
): string {
+ const stableFiles = (files ?? []).map((file) =>
+ Object.fromEntries(Object.entries(file).filter(([key]) => key !== 'llmDeliveryPath')),
+ );
return createHash('sha256')
- .update(JSON.stringify({ text, files: files ?? [], preempt }))
+ .update(JSON.stringify({ text, files: stableFiles, preempt }))
.digest('base64url');
}
diff --git a/packages/data-provider/src/types/queuedTurns.spec.ts b/packages/data-provider/src/types/queuedTurns.spec.ts
index b1ab7b4dc7..82336368bc 100644
--- a/packages/data-provider/src/types/queuedTurns.spec.ts
+++ b/packages/data-provider/src/types/queuedTurns.spec.ts
@@ -11,7 +11,7 @@ const request = {
parentMessageId: 'message-1',
clientRequestId: 'request-1',
text: 'Continue with this context',
- files: [{ file_id: 'file-1', filename: 'context.txt' }],
+ files: [{ file_id: 'file-1', filename: 'context.txt', llmDeliveryPath: 'text' }],
quotes: ['quoted context'],
manualSkills: ['research'],
expectedPredecessorCreatedAt: 42,
diff --git a/packages/data-provider/src/types/queuedTurns.ts b/packages/data-provider/src/types/queuedTurns.ts
index 19fe686dce..a3d504b4fc 100644
--- a/packages/data-provider/src/types/queuedTurns.ts
+++ b/packages/data-provider/src/types/queuedTurns.ts
@@ -18,6 +18,7 @@ export const agentQueuedTurnFileRefSchema = z.object({
height: z.number().optional(),
width: z.number().optional(),
bytes: z.number().nonnegative().optional(),
+ llmDeliveryPath: z.enum(['provider', 'text', 'none']).optional(),
});
export type TAgentQueuedTurnFileRef = z.infer<typeof agentQueuedTurnFileRefSchema>;
diff --git a/packages/data-schemas/src/methods/queuedTurn.spec.ts b/packages/data-schemas/src/methods/queuedTurn.spec.ts
index b46b81f715..ef28b4752d 100644
--- a/packages/data-schemas/src/methods/queuedTurn.spec.ts
+++ b/packages/data-schemas/src/methods/queuedTurn.spec.ts
@@ -90,7 +90,7 @@ describe('agent queued turn methods', () => {
const input = enqueueInput({
text: ' follow up ',
files: [
- { file_id: ' file-1 ', filename: ' report.pdf ' },
+ { file_id: ' file-1 ', filename: ' report.pdf ', llmDeliveryPath: 'text' },
{ file_id: 'file-1', filename: 'ignored duplicate' },
],
quotes: [' quote ', '', 'second'],
@@ -102,6 +102,8 @@ describe('agent queued turn methods', () => {
const replay = await methods.enqueueAgentQueuedTurn({
...input,
text: 'follow up',
+ /** Older replicas do not know the display-only delivery marker; their retry
+ * must still replay the same accepted intent during a rolling deployment. */
files: [{ file_id: 'file-1', filename: 'report.pdf' }],
quotes: ['quote', 'second'],
manualSkills: ['skill-a'],
@@ -115,7 +117,7 @@ describe('agent queued turn methods', () => {
sequence: 1,
status: 'queued',
text: 'follow up',
- files: [{ file_id: 'file-1', filename: 'report.pdf' }],
+ files: [{ file_id: 'file-1', filename: 'report.pdf', llmDeliveryPath: 'text' }],
quotes: ['quote', 'second'],
manualSkills: ['skill-a'],
expectedPredecessorCreatedAt: 42,
diff --git a/packages/data-schemas/src/methods/queuedTurn.ts b/packages/data-schemas/src/methods/queuedTurn.ts
index 35c4dae5b4..2dfeb210e3 100644
--- a/packages/data-schemas/src/methods/queuedTurn.ts
+++ b/packages/data-schemas/src/methods/queuedTurn.ts
@@ -397,6 +397,19 @@ function normalizeOptionalString(value: string | undefined, maxLength: number):
return requireBoundedString(value, maxLength);
}
+function normalizeDeliveryPath(
+ value: AgentQueuedTurnFileRef['llmDeliveryPath'],
+): AgentQueuedTurnFileRef['llmDeliveryPath'] {
+ if (value == null) {
+ return undefined;
+ }
+ const normalized = requireBoundedString(value, 32);
+ if (normalized !== 'provider' && normalized !== 'text' && normalized !== 'none') {
+ throw new TypeError('Agent queued turn file delivery path is invalid');
+ }
+ return normalized;
+}
+
function normalizeFiles(files: readonly AgentQueuedTurnFileRef[] | undefined) {
if (files == null || files.length === 0) {
return undefined;
@@ -423,6 +436,9 @@ function normalizeFiles(files: readonly AgentQueuedTurnFileRef[] | undefined) {
...(normalizeOptionalString(file.filename, 1024) != null && {
filename: normalizeOptionalString(file.filename, 1024),
}),
+ ...(normalizeDeliveryPath(file.llmDeliveryPath) != null && {
+ llmDeliveryPath: normalizeDeliveryPath(file.llmDeliveryPath),
+ }),
...(file.height != null && {
height: requireNonnegativeNumber(file.height),
}),
@@ -525,7 +541,13 @@ function normalizeEnqueue(input: EnqueueAgentQueuedTurnInput) {
}
function fingerprint(payload: ReturnType<typeof normalizeEnqueue>): string {
- return createHash('sha256').update(JSON.stringify(payload)).digest('base64url');
+ /** Delivery routing is display metadata, not user intent. Excluding it keeps
+ * retries compatible with replicas deployed before the field was persisted. */
+ const stableIntent = {
+ ...payload,
+ files: payload.files?.map(({ llmDeliveryPath: _displayMetadata, ...file }) => file),
+ };
+ return createHash('sha256').update(JSON.stringify(stableIntent)).digest('base64url');
}
function laneKey(input: AgentQueuedTurnConversationScope): string {
diff --git a/packages/data-schemas/src/methods/share.test.ts b/packages/data-schemas/src/methods/share.test.ts
index ab4d174d15..868b276f0a 100644
--- a/packages/data-schemas/src/methods/share.test.ts
+++ b/packages/data-schemas/src/methods/share.test.ts
@@ -64,6 +64,7 @@ describe('Share Methods', () => {
textFormat: { type: String, enum: ['html', 'text'] },
status: { type: String, enum: ['pending', 'ready', 'failed'] },
previewError: String,
+ llmDeliveryPath: { type: String, enum: ['provider', 'text', 'none'] },
metadata: mongoose.Schema.Types.Mixed,
tenantId: String,
},
@@ -1112,6 +1113,7 @@ describe('Share Methods', () => {
source: 'local',
type: 'image/png',
bytes: 2048,
+ llmDeliveryPath: 'text',
});
const message = await Message.create({
@@ -1157,6 +1159,7 @@ describe('Share Methods', () => {
// share-scoped route, storage/identity internals stripped, ids anonymized.
expect(steerFile.filepath).toBe(`/api/share/${shareId}/files/steer-file-2`);
expect(steerFile).toMatchObject({ filename: 'steer.png', type: 'image/png' });
+ expect(steerFile.llmDeliveryPath).toBe('text');
expect(steerFile).not.toHaveProperty('storageKey');
expect(steerFile).not.toHaveProperty('user');
expect(steerFile.conversationId).toBe(result?.conversationId);
@@ -1164,6 +1167,7 @@ describe('Share Methods', () => {
const share = await SharedLink.findOne({ shareId }).lean();
expect(share?.fileSnapshots?.map((snapshot) => snapshot.file_id)).toContain('steer-file-2');
+ expect(share?.fileSnapshots?.[0].llmDeliveryPath).toBe('text');
});
test('leaves safe non-steer content untouched (same array reference)', () => {
@@ -3321,6 +3325,123 @@ describe('Share Methods', () => {
expect(result?.updatedAt?.getTime()).toBe(published?.updatedAt?.getTime());
});
+ test('enriches delivery metadata in existing revision-matching snapshots', async () => {
+ const userId = new mongoose.Types.ObjectId().toString();
+ const conversationId = `conv_${nanoid()}`;
+ await seedConversation(userId, conversationId);
+ const docId = await createFile(userId, { llmDeliveryPath: 'text' });
+ const message = await Message.create({
+ messageId: `msg_${nanoid()}`,
+ conversationId,
+ user: userId,
+ text: '',
+ isCreatedByUser: false,
+ content: [
+ {
+ type: 'steer',
+ steerId: 'legacy-steer',
+ steer: 'read this',
+ files: [{ file_id: docId, filename: 'report.pdf', type: 'application/pdf' }],
+ },
+ ],
+ });
+ const shareId = `share_${nanoid()}`;
+ await SharedLink.create({
+ shareId,
+ conversationId,
+ user: userId,
+ messages: [message._id],
+ fileSnapshots: [
+ {
+ file_id: docId,
+ source: 'local',
+ filepath: `/uploads/${userId}/${docId}`,
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ bytes: 1024,
+ },
+ ],
+ });
+ const published = await SharedLink.findOne({ shareId }).lean();
+
+ await expect(
+ shareMethods.getSharedMessages(shareId, undefined, {
+ preflight: async () => {
+ throw new Error('policy rejected');
+ },
+ }),
+ ).rejects.toThrow('policy rejected');
+ const afterRejectedPreflight = await SharedLink.findOne({ shareId }).lean();
+ expect(afterRejectedPreflight?.fileSnapshots?.[0].llmDeliveryPath).toBeUndefined();
+
+ const result = await shareMethods.getSharedMessages(shareId);
+ const content = result?.messages[0].content as Array<Record<string, unknown>>;
+ const file = (content[0].files as Array<Record<string, unknown>>)[0];
+ expect(file.llmDeliveryPath).toBe('text');
+
+ const saved = await SharedLink.findOne({ shareId }).lean();
+ expect(saved?.fileSnapshots?.[0].llmDeliveryPath).toBe('text');
+ expect(saved?.updatedAt?.getTime()).toBe(published?.updatedAt?.getTime());
+ const find = jest.spyOn(File, 'find');
+ await shareMethods.getSharedMessages(shareId);
+ expect(find).not.toHaveBeenCalled();
+ find.mockRestore();
+
+ await SharedLink.updateOne({ shareId }, { $unset: { 'fileSnapshots.0.llmDeliveryPath': 1 } });
+ await shareMethods.getSharedMessages(shareId, undefined, {
+ preflight: async () => {
+ await SharedLink.updateOne({ shareId }, { $set: { fileSnapshots: [] } });
+ },
+ });
+ const republished = await SharedLink.findOne({ shareId }).lean();
+ expect(republished?.fileSnapshots).toEqual([]);
+ });
+
+ test('does not enrich an existing snapshot after its file revision changes', async () => {
+ const userId = new mongoose.Types.ObjectId().toString();
+ const conversationId = `conv_${nanoid()}`;
+ await seedConversation(userId, conversationId);
+ const docId = await createFile(userId, {
+ llmDeliveryPath: 'text',
+ previewRevision: 'current-revision',
+ });
+ const message = await Message.create({
+ messageId: `msg_${nanoid()}`,
+ conversationId,
+ user: userId,
+ text: 'legacy file',
+ isCreatedByUser: true,
+ files: [{ file_id: docId, filename: 'report.pdf', type: 'application/pdf' }],
+ });
+ const shareId = `share_${nanoid()}`;
+ await SharedLink.create({
+ shareId,
+ conversationId,
+ user: userId,
+ messages: [message._id],
+ fileSnapshots: [
+ {
+ file_id: docId,
+ source: 'local',
+ filepath: `/uploads/${userId}/${docId}`,
+ filename: 'report.pdf',
+ type: 'application/pdf',
+ bytes: 1024,
+ previewRevision: 'published-revision',
+ },
+ ],
+ });
+
+ const result = await shareMethods.getSharedMessages(shareId);
+ expect(result?.messages[0].files?.[0].llmDeliveryPath).toBeUndefined();
+ const saved = await SharedLink.findOne({ shareId }).lean();
+ expect(saved?.fileSnapshots?.[0].llmDeliveryPath).toBeNull();
+ const find = jest.spyOn(File, 'find');
+ await shareMethods.getSharedMessages(shareId);
+ expect(find).not.toHaveBeenCalled();
+ find.mockRestore();
+ });
+
test('runs public projection preflight before persisting a legacy backfill', async () => {
const userId = new mongoose.Types.ObjectId().toString();
const conversationId = `conv_${nanoid()}`;
diff --git a/packages/data-schemas/src/methods/share.ts b/packages/data-schemas/src/methods/share.ts
index 0d81d816b1..216c7daadd 100644
--- a/packages/data-schemas/src/methods/share.ts
+++ b/packages/data-schemas/src/methods/share.ts
@@ -176,6 +176,12 @@ const SNAPSHOT_STREAMABLE_SOURCES = new Set<string>([
FileSources.text,
]);
+function isLlmDeliveryPath(
+ value: unknown,
+): value is NonNullable<t.SharedFileSnapshot['llmDeliveryPath']> {
+ return value === 'provider' || value === 'text' || value === 'none';
+}
+
/** Collect `file_id`s from a message's `files`/`attachments` array into `target`. */
function collectFileIds(items: unknown, target: Set<string>): void {
if (!Array.isArray(items)) {
@@ -238,12 +244,24 @@ async function buildFileSnapshots(
collectSteerFileIds(message.content, fileIds);
}
+ return readFileSnapshots(mongoose, fileIds, ownerId);
+}
+
+async function readFileSnapshots(
+ mongoose: typeof import('mongoose'),
+ fileIds: Set<string>,
+ ownerId: string,
+): Promise<t.SharedFileSnapshot[]> {
if (fileIds.size === 0) {
return [];
}
const File = mongoose.models.File as Model<t.IMongoFile>;
- const files = await File.find({ file_id: { $in: Array.from(fileIds) }, user: ownerId }).lean();
+ const files = await File.find({ file_id: { $in: Array.from(fileIds) }, user: ownerId })
+ .select(
+ 'file_id source storageKey filepath type filename bytes width height model llmDeliveryPath previewRevision metadata.sourceDispatchedAt tenantId',
+ )
+ .lean();
const snapshots: t.SharedFileSnapshot[] = [];
for (const file of files) {
@@ -262,6 +280,7 @@ async function buildFileSnapshots(
width: file.width,
height: file.height,
model: file.model,
+ llmDeliveryPath: isLlmDeliveryPath(file.llmDeliveryPath) ? file.llmDeliveryPath : 'provider',
previewRevision: file.previewRevision,
sourceDispatchedAt: file.metadata?.sourceDispatchedAt,
tenantId: file.tenantId,
@@ -394,6 +413,65 @@ async function persistBackfilledSnapshots(
return current.fileSnapshots ?? [];
}
+function snapshotMatchesCurrentVersion(
+ snapshot: t.SharedFileSnapshot,
+ current: t.SharedFileSnapshot,
+): boolean {
+ const revisionChanged = (snapshot.previewRevision ?? null) !== (current.previewRevision ?? null);
+ const sourceGenerationChanged =
+ snapshot.sourceDispatchedAt != null &&
+ snapshot.sourceDispatchedAt !== (current.sourceDispatchedAt ?? null);
+ const bytesChanged =
+ snapshot.bytes != null && current.bytes != null && snapshot.bytes !== current.bytes;
+ return !revisionChanged && !sourceGenerationChanged && !bytesChanged;
+}
+
+/**
+ * Add the delivery marker introduced after file snapshots shipped without changing
+ * what an existing link authorizes. Each marker is copied only when the live file
+ * still passes the same revision checks as the public file route. The caller persists
+ * after public-projection preflight with the full prior snapshot as a compare-and-set
+ * guard. A legacy file with no explicit marker used provider delivery, which remains
+ * the compatible fallback.
+ */
+async function enrichSnapshotDeliveryPaths(
+ mongoose: typeof import('mongoose'),
+ share: t.ISharedLink & { messages: t.IMessage[] },
+): Promise<{ snapshots: t.SharedFileSnapshot[]; changed: boolean }> {
+ const existing = share.fileSnapshots ?? [];
+ const missingIds = new Set(
+ existing
+ .filter((snapshot) => snapshot.llmDeliveryPath === undefined)
+ .map((snapshot) => snapshot.file_id),
+ );
+ if (missingIds.size === 0 || share._id == null || !share.user) {
+ return { snapshots: existing, changed: false };
+ }
+
+ const currentById = new Map(
+ (await readFileSnapshots(mongoose, missingIds, share.user)).map((snapshot) => [
+ snapshot.file_id,
+ snapshot,
+ ]),
+ );
+ let changed = false;
+ const enriched = existing.map((snapshot) => {
+ if (snapshot.llmDeliveryPath !== undefined) {
+ return snapshot;
+ }
+ const current = currentById.get(snapshot.file_id);
+ changed = true;
+ if (current == null || !snapshotMatchesCurrentVersion(snapshot, current)) {
+ return { ...snapshot, llmDeliveryPath: null };
+ }
+ return { ...snapshot, llmDeliveryPath: current.llmDeliveryPath ?? ('provider' as const) };
+ });
+ if (!changed) {
+ return { snapshots: existing, changed: false };
+ }
+ return { snapshots: enriched, changed: true };
+}
+
/** Share-scoped file route that serves a snapshotted file independent of owner ACL. */
function shareFileRoute(shareId: string, fileId: string): string {
return `/api/share/${shareId}/files/${encodeURIComponent(fileId)}`;
@@ -408,6 +486,7 @@ function applyShareFileRoute(
shareId: string,
snapshotIds: Set<string>,
textSourceIds?: Set<string>,
+ deliveryPathById?: ReadonlyMap<string, t.SharedFileSnapshot['llmDeliveryPath']>,
): t.SharedFile {
const fileId = file.file_id;
if (typeof fileId === 'string' && snapshotIds.has(fileId)) {
@@ -418,6 +497,9 @@ function applyShareFileRoute(
// General storage sources stay private, but `text` is a render semantic:
// clients must preview the database-backed payload as text, not the original MIME.
...(textSourceIds?.has(fileId) && { source: FileSources.text }),
+ ...(deliveryPathById?.get(fileId) != null && {
+ llmDeliveryPath: deliveryPathById.get(fileId),
+ }),
};
for (const key of ['preview', 'uri', 'url'] as const) {
if (file[key] !== undefined) {
@@ -450,6 +532,7 @@ export function anonymizeSharedContent(
shareId: string;
snapshotIds: Set<string>;
textSourceIds?: Set<string>;
+ deliveryPathById?: ReadonlyMap<string, t.SharedFileSnapshot['llmDeliveryPath']>;
includeFiles: boolean;
sanitizeUIResourceMarkers?: boolean;
},
@@ -481,6 +564,7 @@ export function anonymizeSharedContent(
params.shareId,
params.snapshotIds,
params.textSourceIds,
+ params.deliveryPathById,
),
)
: undefined;
@@ -518,6 +602,7 @@ function anonymizeMessages(
shareId: string,
snapshotIds: Set<string>,
textSourceIds: Set<string>,
+ deliveryPathById: ReadonlyMap<string, t.SharedFileSnapshot['llmDeliveryPath']>,
includeFiles: boolean,
anonymizeMessageId: (id: string) => string,
anonymizeAssistantId: (id: string) => string,
@@ -548,6 +633,7 @@ function anonymizeMessages(
shareId,
snapshotIds,
textSourceIds,
+ deliveryPathById,
),
)
: undefined;
@@ -564,6 +650,7 @@ function anonymizeMessages(
shareId,
snapshotIds,
textSourceIds,
+ deliveryPathById,
),
)
: undefined;
@@ -586,6 +673,7 @@ function anonymizeMessages(
shareId,
snapshotIds,
textSourceIds,
+ deliveryPathById,
includeFiles,
sanitizeUIResourceMarkers: message.isCreatedByUser !== true,
}),
@@ -904,10 +992,16 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
const perLinkEnabled = share.snapshotFiles !== false;
const includeFiles = adminEnabled && perLinkEnabled;
let fileSnapshots = share.fileSnapshots;
+ const originalFileSnapshots = share.fileSnapshots;
+ let shouldPersistEnrichedDeliveryPaths = false;
const shouldPersistFileSnapshots =
includeFiles && fileSnapshots === undefined && share._id != null;
if (shouldPersistFileSnapshots) {
fileSnapshots = await buildFileSnapshots(mongoose, messagesToShare, share.user);
+ } else if (includeFiles && fileSnapshots !== undefined) {
+ const enriched = await enrichSnapshotDeliveryPaths(mongoose, share);
+ fileSnapshots = enriched.snapshots;
+ shouldPersistEnrichedDeliveryPaths = enriched.changed;
}
const snapshotIds = includeFiles
@@ -920,6 +1014,15 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
.map((snapshot) => snapshot.file_id),
)
: new Set<string>();
+ const deliveryPathById = includeFiles
+ ? new Map(
+ (fileSnapshots ?? []).flatMap((snapshot) =>
+ snapshot.llmDeliveryPath == null
+ ? []
+ : [[snapshot.file_id, snapshot.llmDeliveryPath] as const],
+ ),
+ )
+ : new Map<string, t.SharedFileSnapshot['llmDeliveryPath']>();
/** The share view has no conversation in scope, so whether this link may reveal a
* model travels in the payload — read off the very messages being returned. */
const { messages, hasConfiguredSender } = anonymizeMessages(
@@ -928,6 +1031,7 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
resolvedShareId,
snapshotIds,
textSourceIds,
+ deliveryPathById,
includeFiles,
anonymizeMessageId,
anonymizeAssistantId,
@@ -951,6 +1055,20 @@ export function createShareMethods(mongoose: typeof import('mongoose')): {
if (shouldPersistFileSnapshots) {
await persistBackfilledSnapshots(SharedLink, { _id: share._id }, fileSnapshots ?? []);
+ } else if (
+ shouldPersistEnrichedDeliveryPaths &&
+ share._id != null &&
+ originalFileSnapshots !== undefined
+ ) {
+ await SharedLink.updateOne(
+ {
+ _id: share._id,
+ fileSnapshots: originalFileSnapshots,
+ snapshotFiles: { $ne: false },
+ },
+ { $set: { fileSnapshots } },
+ { timestamps: false },
+ );
}
return result;
diff --git a/packages/data-schemas/src/schema/queuedTurn.ts b/packages/data-schemas/src/schema/queuedTurn.ts
index be0f2001ba..e191c8d9c5 100644
--- a/packages/data-schemas/src/schema/queuedTurn.ts
+++ b/packages/data-schemas/src/schema/queuedTurn.ts
@@ -7,6 +7,7 @@ const fileRefSchema = new Schema(
type: { type: String, maxlength: 256 },
filepath: { type: String, maxlength: 2048 },
filename: { type: String, maxlength: 1024 },
+ llmDeliveryPath: { type: String, enum: ['provider', 'text', 'none'] },
height: { type: Number, min: 0 },
width: { type: Number, min: 0 },
bytes: { type: Number, min: 0 },
diff --git a/packages/data-schemas/src/schema/share.ts b/packages/data-schemas/src/schema/share.ts
index 808d977a8f..f1cf0a6b33 100644
--- a/packages/data-schemas/src/schema/share.ts
+++ b/packages/data-schemas/src/schema/share.ts
@@ -34,6 +34,7 @@ const fileSnapshotSchema = new Schema<SharedFileSnapshot>(
width: { type: Number },
height: { type: Number },
model: { type: String },
+ llmDeliveryPath: { type: String, enum: ['provider', 'text', 'none'] },
previewRevision: { type: String },
sourceDispatchedAt: { type: Number },
tenantId: { type: String },
diff --git a/packages/data-schemas/src/types/queuedTurn.ts b/packages/data-schemas/src/types/queuedTurn.ts
index 7d66a74d14..93dfc0a41e 100644
--- a/packages/data-schemas/src/types/queuedTurn.ts
+++ b/packages/data-schemas/src/types/queuedTurn.ts
@@ -20,6 +20,7 @@ export interface AgentQueuedTurnFileRef {
type?: string;
filepath?: string;
filename?: string;
+ llmDeliveryPath?: 'provider' | 'text' | 'none';
height?: number;
width?: number;
bytes?: number;
diff --git a/packages/data-schemas/src/types/share.ts b/packages/data-schemas/src/types/share.ts
index cefb456160..c0bfc4e532 100644
--- a/packages/data-schemas/src/types/share.ts
+++ b/packages/data-schemas/src/types/share.ts
@@ -19,6 +19,10 @@ export interface SharedFileSnapshot {
width?: number;
height?: number;
model?: string;
+ /** Determines whether the shared renderer previews the original object or
+ * the extracted text served by the share-scoped preview route. Null marks a
+ * legacy snapshot checked without a matching live file; do not retry on each view. */
+ llmDeliveryPath?: 'provider' | 'text' | 'none' | null;
/** Deferred-preview generation marker captured at share time. The share routes
* refuse to serve when the live file's revision no longer matches (the file_id
* was reused/overwritten by a later turn), so a link can't surface post-share