mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-04-07 00:15:23 +02:00
* refactor: Update Image Component to Remove Lazy Loading and Enhance Rendering - Removed the react-lazy-load-image-component dependency from the Image component, simplifying the image loading process. - Updated the Image component to use a standard <img> tag with async decoding for improved performance and user experience. - Adjusted related tests to reflect changes in image rendering behavior and ensure proper functionality without lazy loading. * refactor: Enhance Image Handling and Caching Across Components - Introduced a new previewCache utility for managing local blob preview URLs, improving image loading efficiency. - Updated the Image component and related parts (FileRow, Files, Part, ImageAttachment, LogContent) to utilize cached previews, enhancing rendering performance and user experience. - Added width and height properties to the Image component for better layout management and consistency across different usages. - Improved file handling logic in useFileHandling to cache previews during file uploads, ensuring quick access to image data. - Enhanced overall code clarity and maintainability by streamlining image rendering logic and reducing redundancy. * refactor: Enhance OpenAIImageGen Component with Image Dimensions - Added width and height properties to the OpenAIImageGen component for improved image rendering and layout management. - Updated the Image component usage within OpenAIImageGen to utilize the new dimensions, enhancing visual consistency and performance. - Improved code clarity by destructuring additional properties from the attachment object, streamlining the component's logic. * refactor: Implement Image Size Caching in DialogImage Component - Introduced an imageSizeCache to store and retrieve image sizes, enhancing performance by reducing redundant fetch requests. - Updated the getImageSize function to first check the cache before making network requests, improving efficiency in image handling. - Added decoding attribute to the image element for optimized rendering behavior. * refactor: Enhance UserAvatar Component with Avatar Caching and Error Handling - Introduced avatar caching logic to optimize avatar resolution based on user ID and avatar source, improving performance and reducing redundant image loads. - Implemented error handling for failed image loads, allowing for fallback to a default avatar when necessary. - Updated UserAvatar props to streamline the interface by removing the user object and directly accepting avatar-related properties. - Enhanced overall code clarity and maintainability by refactoring the component structure and logic. * fix: Layout Shift in Message and Placeholder Components for Consistent Height Management - Adjusted the height of the PlaceholderRow and related message components to ensure consistent rendering with a minimum height of 31px. - Updated the MessageParts and ContentRender components to utilize a minimum height for better layout stability. - Enhanced overall code clarity by standardizing the structure of message-related components. * tests: Update FileRow Component to Prefer Cached Previews for Image Rendering - Modified the image URL selection logic in the FileRow component to prioritize cached previews over file paths when uploads are complete, enhancing rendering performance and user experience. - Updated related tests to reflect changes in image URL handling, ensuring accurate assertions for both preview and file path scenarios. - Introduced a fallback mechanism to use file paths when no preview exists, improving robustness in file handling. * fix: Image cache lifecycle and dialog decoding - Add deletePreview/clearPreviewCache to previewCache.ts for blob URL cleanup - Wire deletePreview into useFileDeletion to revoke blobs on file delete - Move dimensionCache.set into useMemo to avoid side effects during render - Extract IMAGE_MAX_W_PX constant (512) to document coupling with max-w-lg - Export _resetImageCaches for test isolation - Change DialogImage decoding from "sync" to "async" to avoid blocking main thread * fix: Avatar cache invalidation and cleanup - Include avatarSrc in cache invalidation to prevent stale avatars - Remove unused username parameter from resolveAvatar - Skip caching when userId is empty to prevent cache key collisions * test: Fix test isolation and type safety - Reset module-level dimensionCache/paintedUrls in beforeEach via _resetImageCaches - Replace any[] with typed mock signature in cn mock for both test files * chore: Code quality improvements from review - Use barrel imports for previewCache in Files.tsx and Part.tsx - Single Map.get with truthy check instead of has+get in useEventHandlers - Add JSDoc comments explaining EmptyText margin removal and PlaceholderRow height - Fix FileRow toast showing "Deleting file" when file isn't actually deleted (progress < 1) * fix: Address remaining review findings (R1-R3) - Add deletePreview calls to deleteFiles batch path to prevent blob URL leaks - Change useFileDeletion import from deep path to barrel (~/utils) - Change useMemo to useEffect for dimensionCache.set (side effect, not derived value) * fix: Address audit comments 2, 5, and 7 - Fix files preservation to distinguish null (missing) from [] (empty) in finalHandler - Add auto-revoke on overwrite in cachePreview to prevent leaked blobs - Add removePreviewEntry for key transfer without revoke - Clean up stale temp_file_id cache entry after promotion to permanent file_id
241 lines
7 KiB
TypeScript
241 lines
7 KiB
TypeScript
import {
|
|
Tools,
|
|
Constants,
|
|
ContentTypes,
|
|
ToolCallTypes,
|
|
imageGenTools,
|
|
isImageVisionTool,
|
|
} from 'librechat-data-provider';
|
|
import { memo } from 'react';
|
|
import type { TMessageContentParts, TAttachment } from 'librechat-data-provider';
|
|
import { OpenAIImageGen, EmptyText, Reasoning, ExecuteCode, AgentUpdate, Text } from './Parts';
|
|
import { ErrorMessage } from './MessageContent';
|
|
import RetrievalCall from './RetrievalCall';
|
|
import { getCachedPreview } from '~/utils';
|
|
import AgentHandoff from './AgentHandoff';
|
|
import CodeAnalyze from './CodeAnalyze';
|
|
import Container from './Container';
|
|
import WebSearch from './WebSearch';
|
|
import ToolCall from './ToolCall';
|
|
import ImageGen from './ImageGen';
|
|
import Image from './Image';
|
|
|
|
type PartProps = {
|
|
part?: TMessageContentParts;
|
|
isLast?: boolean;
|
|
isSubmitting: boolean;
|
|
showCursor: boolean;
|
|
isCreatedByUser: boolean;
|
|
attachments?: TAttachment[];
|
|
};
|
|
|
|
const Part = memo(function Part({
|
|
part,
|
|
isSubmitting,
|
|
attachments,
|
|
isLast,
|
|
showCursor,
|
|
isCreatedByUser,
|
|
}: PartProps) {
|
|
if (!part) {
|
|
return null;
|
|
}
|
|
|
|
if (part.type === ContentTypes.ERROR) {
|
|
return (
|
|
<ErrorMessage
|
|
text={
|
|
part[ContentTypes.ERROR] ??
|
|
(typeof part[ContentTypes.TEXT] === 'string'
|
|
? part[ContentTypes.TEXT]
|
|
: part.text?.value) ??
|
|
''
|
|
}
|
|
className="my-2"
|
|
/>
|
|
);
|
|
} else if (part.type === ContentTypes.AGENT_UPDATE) {
|
|
return (
|
|
<>
|
|
<AgentUpdate currentAgentId={part[ContentTypes.AGENT_UPDATE]?.agentId} />
|
|
{isLast && showCursor && (
|
|
<Container>
|
|
<EmptyText />
|
|
</Container>
|
|
)}
|
|
</>
|
|
);
|
|
} else if (part.type === ContentTypes.TEXT) {
|
|
const text = typeof part.text === 'string' ? part.text : part.text?.value;
|
|
|
|
if (typeof text !== 'string') {
|
|
return null;
|
|
}
|
|
if (part.tool_call_ids != null && !text) {
|
|
return null;
|
|
}
|
|
/** Handle whitespace-only text to avoid layout shift */
|
|
if (text.length > 0 && /^\s*$/.test(text)) {
|
|
/** Show placeholder for whitespace-only last part during streaming */
|
|
if (isLast && showCursor) {
|
|
return (
|
|
<Container>
|
|
<EmptyText />
|
|
</Container>
|
|
);
|
|
}
|
|
/** Skip rendering non-last whitespace-only parts to avoid empty Container */
|
|
if (!isLast) {
|
|
return null;
|
|
}
|
|
}
|
|
return (
|
|
<Container>
|
|
<Text text={text} isCreatedByUser={isCreatedByUser} showCursor={showCursor} />
|
|
</Container>
|
|
);
|
|
} else if (part.type === ContentTypes.THINK) {
|
|
const reasoning = typeof part.think === 'string' ? part.think : part.think?.value;
|
|
if (typeof reasoning !== 'string') {
|
|
return null;
|
|
}
|
|
return <Reasoning reasoning={reasoning} isLast={isLast ?? false} />;
|
|
} else if (part.type === ContentTypes.TOOL_CALL) {
|
|
const toolCall = part[ContentTypes.TOOL_CALL];
|
|
|
|
if (!toolCall) {
|
|
return null;
|
|
}
|
|
|
|
const isToolCall =
|
|
'args' in toolCall && (!toolCall.type || toolCall.type === ToolCallTypes.TOOL_CALL);
|
|
if (
|
|
isToolCall &&
|
|
(toolCall.name === Tools.execute_code ||
|
|
toolCall.name === Constants.PROGRAMMATIC_TOOL_CALLING)
|
|
) {
|
|
return (
|
|
<ExecuteCode
|
|
attachments={attachments}
|
|
isSubmitting={isSubmitting}
|
|
output={toolCall.output ?? ''}
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
args={typeof toolCall.args === 'string' ? toolCall.args : ''}
|
|
/>
|
|
);
|
|
} else if (
|
|
isToolCall &&
|
|
(toolCall.name === 'image_gen_oai' ||
|
|
toolCall.name === 'image_edit_oai' ||
|
|
toolCall.name === 'gemini_image_gen')
|
|
) {
|
|
return (
|
|
<OpenAIImageGen
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
isSubmitting={isSubmitting}
|
|
toolName={toolCall.name}
|
|
args={typeof toolCall.args === 'string' ? toolCall.args : ''}
|
|
output={toolCall.output ?? ''}
|
|
attachments={attachments}
|
|
/>
|
|
);
|
|
} else if (isToolCall && toolCall.name === Tools.web_search) {
|
|
return (
|
|
<WebSearch
|
|
output={toolCall.output ?? ''}
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
isSubmitting={isSubmitting}
|
|
attachments={attachments}
|
|
isLast={isLast}
|
|
/>
|
|
);
|
|
} else if (isToolCall && toolCall.name?.startsWith(Constants.LC_TRANSFER_TO_)) {
|
|
return (
|
|
<AgentHandoff
|
|
args={toolCall.args ?? ''}
|
|
name={toolCall.name || ''}
|
|
output={toolCall.output ?? ''}
|
|
/>
|
|
);
|
|
} else if (isToolCall) {
|
|
return (
|
|
<ToolCall
|
|
args={toolCall.args ?? ''}
|
|
name={toolCall.name || ''}
|
|
output={toolCall.output ?? ''}
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
isSubmitting={isSubmitting}
|
|
attachments={attachments}
|
|
auth={toolCall.auth}
|
|
expires_at={toolCall.expires_at}
|
|
isLast={isLast}
|
|
/>
|
|
);
|
|
} else if (toolCall.type === ToolCallTypes.CODE_INTERPRETER) {
|
|
const code_interpreter = toolCall[ToolCallTypes.CODE_INTERPRETER];
|
|
return (
|
|
<CodeAnalyze
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
code={code_interpreter.input}
|
|
outputs={code_interpreter.outputs ?? []}
|
|
/>
|
|
);
|
|
} else if (
|
|
toolCall.type === ToolCallTypes.RETRIEVAL ||
|
|
toolCall.type === ToolCallTypes.FILE_SEARCH
|
|
) {
|
|
return (
|
|
<RetrievalCall initialProgress={toolCall.progress ?? 0.1} isSubmitting={isSubmitting} />
|
|
);
|
|
} else if (
|
|
toolCall.type === ToolCallTypes.FUNCTION &&
|
|
ToolCallTypes.FUNCTION in toolCall &&
|
|
imageGenTools.has(toolCall.function.name)
|
|
) {
|
|
return (
|
|
<ImageGen
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
args={toolCall.function.arguments as string}
|
|
/>
|
|
);
|
|
} else if (toolCall.type === ToolCallTypes.FUNCTION && ToolCallTypes.FUNCTION in toolCall) {
|
|
if (isImageVisionTool(toolCall)) {
|
|
if (isSubmitting && showCursor) {
|
|
return (
|
|
<Container>
|
|
<Text text={''} isCreatedByUser={isCreatedByUser} showCursor={showCursor} />
|
|
</Container>
|
|
);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<ToolCall
|
|
initialProgress={toolCall.progress ?? 0.1}
|
|
isSubmitting={isSubmitting}
|
|
args={toolCall.function.arguments as string}
|
|
name={toolCall.function.name}
|
|
output={toolCall.function.output}
|
|
isLast={isLast}
|
|
/>
|
|
);
|
|
}
|
|
} else if (part.type === ContentTypes.IMAGE_FILE) {
|
|
const imageFile = part[ContentTypes.IMAGE_FILE];
|
|
const cached = imageFile.file_id ? getCachedPreview(imageFile.file_id) : undefined;
|
|
return (
|
|
<Image
|
|
imagePath={cached ?? imageFile.filepath}
|
|
altText={imageFile.filename ?? 'Uploaded Image'}
|
|
width={imageFile.width}
|
|
height={imageFile.height}
|
|
/>
|
|
);
|
|
}
|
|
|
|
return null;
|
|
});
|
|
Part.displayName = 'Part';
|
|
|
|
export default Part;
|