diff --git a/client/src/components/Chat/Messages/Content/ContentParts.tsx b/client/src/components/Chat/Messages/Content/ContentParts.tsx index 91d7bafba0..cca17f396f 100644 --- a/client/src/components/Chat/Messages/Content/ContentParts.tsx +++ b/client/src/components/Chat/Messages/Content/ContentParts.tsx @@ -1,7 +1,7 @@ import { memo, useRef, useMemo, useEffect, useCallback, useContext, Fragment } from 'react'; import { useStore } from 'jotai'; -import { Constants, ContentTypes, Tools } from 'librechat-data-provider'; -import { PendingSkillCall, Container } from '@librechat/chat/components'; +import { Constants, ContentTypes, Tools, imageGenTools } from 'librechat-data-provider'; +import { PendingSkillCall, Container, HiddenAttachmentsContext } from '@librechat/chat/components'; import { hasParallelLanes, parallelLaneGroups, @@ -50,10 +50,11 @@ import { isPreviewedToolCallPart, PreviewedToolCallPart } from './hydration'; import { MediaContext, MessageContext, SearchContext } from '~/Providers'; import MemoryArtifacts, { hasMemoryArtifacts } from './MemoryArtifacts'; import { useMCPAppsPolicy } from '~/Providers/MCPAppsPolicyContext'; +import { EmptyText, AgentUpdate, AttachmentGroup } from './Parts'; import { selectToolCallUIResources } from '~/utils/mcpApps'; +import { AGENT_STYLE_TOOLS } from './Parts/OpenAIImageGen'; import ActivityPhaseGroup from './ActivityPhaseGroup'; import EditContentParts from './EditContentParts'; -import { EmptyText, AgentUpdate } from './Parts'; import ApprovalProvider from './ApprovalContext'; import Sources from '~/components/Web/Sources'; import ToolCallGroup from './ToolCallGroup'; @@ -73,6 +74,9 @@ const isEmptyTextPart = (part: TMessageContentParts | undefined): boolean => { const getToolCallId = (part: TMessageContentParts): string => (part?.[ContentTypes.TOOL_CALL] as Agents.ToolCall | undefined)?.id ?? ''; +const isFileOutput = (file: TAttachment): boolean => + Boolean(file.filepath) && file.type !== Tools.ui_resources && file.type !== Tools.web_search; + const getPartAgentId = (part: TMessageContentParts): string | undefined => (part as { agentId?: string })?.agentId ?? (part?.[ContentTypes.TOOL_CALL] as { agentId?: string } | undefined)?.agentId; @@ -399,6 +403,8 @@ type ContentPartsProps = { authorHeader?: ReactNode; conversationId?: string | null; attachments?: TAttachment[]; + /** One mounted response, retained across optimistic and durable message ids. */ + mediaScope?: number; searchResults?: { [key: string]: SearchResultData }; isCreatedByUser: boolean; showThinking: boolean; @@ -464,6 +470,7 @@ const ContentPartsBody = memo(function ContentPartsBody({ isLast, content, manualSkills, + mediaScope = 0, messageId, enterEdit, siblingIdx, @@ -630,6 +637,91 @@ const ContentPartsBody = memo(function ContentPartsBody({ }, [attachmentsForPart], ); + const inheritedHiddenFiles = useContext(HiddenAttachmentsContext); + const fileRows = useMemo(() => { + type FileRow = { key: string; files: TAttachment[]; source: TAttachment[] }; + const rows = new Map(); + const ownerByFile = new Map(); + if ( + nestedActivityPhase || + hideAttachments || + edit || + hasParallelLanes(content, messageLaneGroups) + ) { + return rows; + } + for (const [index, part] of (content ?? []).entries()) { + if (part?.type !== ContentTypes.TOOL_CALL) continue; + const call = part[ContentTypes.TOOL_CALL]; + const name = call != null && 'name' in call ? call.name : ''; + // Image-generation tools own a specialized preview with generation details. + if (imageGenTools.has(name) || AGENT_STYLE_TOOLS.has(name)) { + continue; + } + const files = collectPhaseAttachments([part])?.filter( + (file) => isFileOutput(file) && !inheritedHiddenFiles?.has(file), + ); + if (!files?.length) continue; + const row: FileRow = { + key: `files-${mediaScope}-${getToolCallId(part)}-${getPartKeyIndex(part, absoluteIndexAt(index))}`, + files: [], + source: attachmentsForPart(part) ?? files, + }; + for (const file of files) { + const identity = attachmentRenderKey(file); + const owner = identity == null ? undefined : ownerByFile.get(identity); + if (owner != null) { + owner.row.files[owner.index] = file; + continue; + } + if (identity != null) ownerByFile.set(identity, { row, index: row.files.length }); + row.files.push(file); + } + // Keep source-only entries so legacy duplicates are suppressed at every matching part. + rows.set(part, row); + } + return rows; + }, [ + content, + messageLaneGroups, + nestedActivityPhase, + hideAttachments, + edit, + collectPhaseAttachments, + attachmentsForPart, + inheritedHiddenFiles, + mediaScope, + absoluteIndexAt, + ]); + const hiddenFilesRef = useRef | null>(null); + const nextHiddenFiles = new Set(inheritedHiddenFiles); + for (const { source } of fileRows.values()) { + for (const file of source) { + // Hide superseded records too; the stable row renders only the latest version. + if (isFileOutput(file)) nextHiddenFiles.add(file); + } + } + if ( + hiddenFilesRef.current == null || + hiddenFilesRef.current.size !== nextHiddenFiles.size || + [...nextHiddenFiles].some((file) => !hiddenFilesRef.current?.has(file)) + ) { + hiddenFilesRef.current = nextHiddenFiles; + } + const hiddenFiles = hiddenFilesRef.current; + const renderFiles = (parts: ReadonlyArray): ReactElement[] => { + const rows: ReactElement[] = []; + for (const part of parts) { + const row = part == null ? undefined : fileRows.get(part); + if (row == null || row.files.length === 0) continue; + rows.push( + + + , + ); + } + return rows; + }; const completedPhaseKeys = useMemo(() => { const indices = new Set(); const labels = new Map(); @@ -975,7 +1067,7 @@ const ContentPartsBody = memo(function ContentPartsBody({ } if (group.type === 'single') { const { part, idx } = group.part; - nodes.push(renderPart(part, idx, idx === lastContentIdx)); + nodes.push(renderPart(part, idx, idx === lastContentIdx), ...renderFiles([part])); return nodes; } const { groupId } = group; @@ -1008,6 +1100,7 @@ const ContentPartsBody = memo(function ContentPartsBody({ parentPhaseOwnsFailurePill={parentPhaseOwnsFailurePill} />, ); + nodes.push(...renderFiles(group.parts.map(({ part }) => part))); return nodes; }); }; @@ -1106,6 +1199,7 @@ const ContentPartsBody = memo(function ContentPartsBody({ key={key} content={segmentContent} messageId={messageId} + mediaScope={mediaScope} createdAt={createdAt} authorHeader={authorHeader} conversationId={conversationId} @@ -1219,14 +1313,17 @@ const ContentPartsBody = memo(function ContentPartsBody({ ? keepsThinkingOpen || segment.content.some(blocksLiveFold) : hasPendingApproval; if (synthesized && awaitsReader) { - return renderSegment( - segment.content, - absoluteIndexAt(segment.startIndex), - segmentIndices, - `phase-awaiting-${cardKey}`, - ); + return [ + renderSegment( + segment.content, + absoluteIndexAt(segment.startIndex), + segmentIndices, + `phase-awaiting-${cardKey}`, + ), + ...renderFiles(segment.content), + ]; } - return ( + return [ - - ); + , + ...renderFiles(segment.content), + ]; }); } @@ -1333,23 +1431,25 @@ const ContentPartsBody = memo(function ContentPartsBody({ // Sequential content: render parts in order (90% of cases) const sequentialContent = ( - - {!nestedActivityPhase && !continuation && } - {!nestedActivityPhase && !continuation && renderPendingSkills()} - {showEmptyCursor && ( - - {/** Nudge only when the dot is truly first under the header — leading - * memory/skill rows and nested phases keep it flush. */} - - - )} - {!showEmptyCursor && (phaseContent ?? renderGroups(groupedParts, lastContentIdx))} - {!nestedActivityPhase && } - + + + {!nestedActivityPhase && !continuation && } + {!nestedActivityPhase && !continuation && renderPendingSkills()} + {showEmptyCursor && ( + + {/** Nudge only when the dot is truly first under the header — leading + * memory/skill rows and nested phases keep it flush. */} + + + )} + {!showEmptyCursor && (phaseContent ?? renderGroups(groupedParts, lastContentIdx))} + {!nestedActivityPhase && } + + ); if (nestedActivityPhase) { return sequentialContent; @@ -1574,6 +1674,7 @@ const ContentParts = memo(function ContentParts(props: ContentPartsProps) { {appContent ?? ( - + diff --git a/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx b/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx index 609c365d07..57fe60d49a 100644 --- a/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx +++ b/client/src/components/Chat/Messages/Content/MemoryArtifacts.tsx @@ -1,4 +1,4 @@ -import { useId, useMemo, useState } from 'react'; +import { memo, useId, useMemo, useState } from 'react'; import { ChevronDown } from 'lucide-react'; import { Tools } from 'librechat-data-provider'; import { toolPanelSpacingClassName } from '@librechat/chat/react'; @@ -27,7 +27,7 @@ const isUnlinkedMemoryArtifact = ( export const hasMemoryArtifacts = (attachments?: TAttachment[]): boolean => attachments?.some(isUnlinkedMemoryArtifact) ?? false; -export default function MemoryArtifacts({ attachments }: { attachments?: TAttachment[] }) { +export default memo(function MemoryArtifacts({ attachments }: { attachments?: TAttachment[] }) { const contentId = useId(); const localize = useLocalize(); const [showInfo, setShowInfo] = useState(false); @@ -126,4 +126,4 @@ export default function MemoryArtifacts({ attachments }: { attachments?: TAttach ); -} +}); diff --git a/client/src/components/Chat/Messages/Content/Parts/WorkspaceChanges.tsx b/client/src/components/Chat/Messages/Content/Parts/WorkspaceChanges.tsx index e93d848ea5..5ca17a5a80 100644 --- a/client/src/components/Chat/Messages/Content/Parts/WorkspaceChanges.tsx +++ b/client/src/components/Chat/Messages/Content/Parts/WorkspaceChanges.tsx @@ -80,7 +80,7 @@ const WorkspaceChange = memo(({ attachment }: { attachment: StatefulWorkspaceAtt WorkspaceChange.displayName = 'WorkspaceChange'; -export default function WorkspaceChanges({ +export default memo(function WorkspaceChanges({ attachments, }: { attachments: StatefulWorkspaceAttachment[]; @@ -140,4 +140,4 @@ export default function WorkspaceChanges({ ); -} +}); diff --git a/client/src/components/Chat/Messages/Content/ToolOutput/ToolIcon.tsx b/client/src/components/Chat/Messages/Content/ToolOutput/ToolIcon.tsx index f79d42b96c..e00a7cf89f 100644 --- a/client/src/components/Chat/Messages/Content/ToolOutput/ToolIcon.tsx +++ b/client/src/components/Chat/Messages/Content/ToolOutput/ToolIcon.tsx @@ -1,3 +1,4 @@ +import { memo } from 'react'; import { LangIcon } from '@librechat/chat/components'; import { Constants, isActionTool, splitToolCallName } from 'librechat-data-provider'; import { @@ -134,7 +135,12 @@ interface ToolIconProps { className?: string; } -export default function ToolIcon({ type, iconUrl, isAnimating = false, className }: ToolIconProps) { +export default memo(function ToolIcon({ + type, + iconUrl, + isAnimating = false, + className, +}: ToolIconProps) { if (iconUrl) { return (