mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 06:51:59 +00:00
🖼️ fix: Preserve Generated Outputs During Streaming (#18272)
* fix: preserve generated output identity during streaming * fix: deduplicate shared output rows across activity phases
This commit is contained in:
parent
90304e34b9
commit
cff8d595f1
10 changed files with 418 additions and 86 deletions
|
|
@ -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<TMessageContentParts, FileRow>();
|
||||
const ownerByFile = new Map<string, { row: FileRow; index: number }>();
|
||||
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<ReadonlySet<TAttachment> | 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<TMessageContentParts | undefined>): 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(
|
||||
<HiddenAttachmentsContext.Provider key={row.key} value={inheritedHiddenFiles}>
|
||||
<AttachmentGroup attachments={row.files} />
|
||||
</HiddenAttachmentsContext.Provider>,
|
||||
);
|
||||
}
|
||||
return rows;
|
||||
};
|
||||
const completedPhaseKeys = useMemo(() => {
|
||||
const indices = new Set<number>();
|
||||
const labels = new Map<string, number>();
|
||||
|
|
@ -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 [
|
||||
<ActivityPhaseGroup
|
||||
key={`activity-phase-${cardKey}`}
|
||||
labelPart={segment.labelPart}
|
||||
|
|
@ -1295,8 +1392,9 @@ const ContentPartsBody = memo(function ContentPartsBody({
|
|||
true,
|
||||
)}
|
||||
</SoleToolContext.Provider>
|
||||
</ActivityPhaseGroup>
|
||||
);
|
||||
</ActivityPhaseGroup>,
|
||||
...renderFiles(segment.content),
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
|
|
@ -1333,23 +1431,25 @@ const ContentPartsBody = memo(function ContentPartsBody({
|
|||
|
||||
// Sequential content: render parts in order (90% of cases)
|
||||
const sequentialContent = (
|
||||
<SearchContext.Provider value={{ searchResults }}>
|
||||
{!nestedActivityPhase && !continuation && <MemoryArtifacts attachments={attachments} />}
|
||||
{!nestedActivityPhase && !continuation && renderPendingSkills()}
|
||||
{showEmptyCursor && (
|
||||
<Container>
|
||||
{/** Nudge only when the dot is truly first under the header — leading
|
||||
* memory/skill rows and nested phases keep it flush. */}
|
||||
<EmptyText
|
||||
underHeaderIcon={
|
||||
!nestedActivityPhase && !hasPendingSkills && !hasMemoryArtifacts(attachments)
|
||||
}
|
||||
/>
|
||||
</Container>
|
||||
)}
|
||||
{!showEmptyCursor && (phaseContent ?? renderGroups(groupedParts, lastContentIdx))}
|
||||
{!nestedActivityPhase && <WorkspaceChanges attachments={workspaceChanges} />}
|
||||
</SearchContext.Provider>
|
||||
<HiddenAttachmentsContext.Provider value={hiddenFiles}>
|
||||
<SearchContext.Provider value={{ searchResults }}>
|
||||
{!nestedActivityPhase && !continuation && <MemoryArtifacts attachments={attachments} />}
|
||||
{!nestedActivityPhase && !continuation && renderPendingSkills()}
|
||||
{showEmptyCursor && (
|
||||
<Container>
|
||||
{/** Nudge only when the dot is truly first under the header — leading
|
||||
* memory/skill rows and nested phases keep it flush. */}
|
||||
<EmptyText
|
||||
underHeaderIcon={
|
||||
!nestedActivityPhase && !hasPendingSkills && !hasMemoryArtifacts(attachments)
|
||||
}
|
||||
/>
|
||||
</Container>
|
||||
)}
|
||||
{!showEmptyCursor && (phaseContent ?? renderGroups(groupedParts, lastContentIdx))}
|
||||
{!nestedActivityPhase && <WorkspaceChanges attachments={workspaceChanges} />}
|
||||
</SearchContext.Provider>
|
||||
</HiddenAttachmentsContext.Provider>
|
||||
);
|
||||
if (nestedActivityPhase) {
|
||||
return sequentialContent;
|
||||
|
|
@ -1574,6 +1674,7 @@ const ContentParts = memo(function ContentParts(props: ContentPartsProps) {
|
|||
<SoleToolContext.Provider value={undefined}>
|
||||
<ContentPartsBody
|
||||
{...props}
|
||||
mediaScope={appRenderScope}
|
||||
content={slice}
|
||||
attachments={inlineAttachments}
|
||||
workspaceAttachmentsPartitioned
|
||||
|
|
@ -1633,10 +1734,11 @@ const ContentParts = memo(function ContentParts(props: ContentPartsProps) {
|
|||
)}
|
||||
<SubagentAttachmentsContext.Provider value={nestedAttachments}>
|
||||
{appContent ?? (
|
||||
<MCPAppSuppressionContext.Provider value={suppressedAppAttachments}>
|
||||
<MCPAppSuppressionContext.Provider key="body-0" value={suppressedAppAttachments}>
|
||||
<SoleToolContext.Provider value={undefined}>
|
||||
<ContentPartsBody
|
||||
{...props}
|
||||
mediaScope={appRenderScope}
|
||||
resumeAuthors={props.resumeAuthors ?? messageContent.resumeAuthors}
|
||||
/>
|
||||
</SoleToolContext.Provider>
|
||||
|
|
|
|||
|
|
@ -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
|
|||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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({
|
|||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 (
|
||||
<CustomIcon
|
||||
|
|
@ -160,4 +166,4 @@ export default function ToolIcon({ type, iconUrl, isAnimating = false, className
|
|||
aria-hidden="true"
|
||||
/>
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -83,7 +83,7 @@ jest.mock('~/components/MCPUIResource', () => {
|
|||
ui_resources: [resource],
|
||||
})) ?? attachments;
|
||||
const visible = (selectedAttachments ?? []).filter(
|
||||
(attachment) => !suppressed?.has(attachment),
|
||||
(attachment) => attachment.ui_resources != null && !suppressed?.has(attachment),
|
||||
);
|
||||
if (visible.length === 0) {
|
||||
return null;
|
||||
|
|
@ -159,10 +159,19 @@ jest.mock('../Parts', () => ({
|
|||
StreamingThoughtPeek: ({ text }: { text: string }) => (
|
||||
<div data-testid="streaming-thought-peek">{text}</div>
|
||||
),
|
||||
AttachmentGroup: ({ attachments }: { attachments?: TAttachment[] }) => {
|
||||
AttachmentGroup: ({ attachments: source }: { attachments?: TAttachment[] }) => {
|
||||
const { useVisibleAttachments } = jest.requireActual<
|
||||
typeof import('@librechat/chat/components')
|
||||
>('@librechat/chat/components');
|
||||
const { Tools } =
|
||||
jest.requireActual<typeof import('librechat-data-provider')>('librechat-data-provider');
|
||||
const attachments = useVisibleAttachments(source)?.filter(
|
||||
(file) => file.type !== Tools.ui_resources,
|
||||
);
|
||||
const { isSubmitting } = jest
|
||||
.requireActual<typeof import('~/Providers/MessageContext')>('~/Providers/MessageContext')
|
||||
.useMessageContext();
|
||||
if (!attachments?.length) return null;
|
||||
return (
|
||||
<div
|
||||
data-testid="attachment-group"
|
||||
|
|
@ -443,9 +452,9 @@ describe('ContentParts integration: MCP image hoist and grouping', () => {
|
|||
});
|
||||
|
||||
const groups = screen.getAllByTestId('attachment-group');
|
||||
// One AttachmentGroup hoisted at the group level — inner ToolCalls skip rendering theirs.
|
||||
expect(groups).toHaveLength(1);
|
||||
expect(groups[0].getAttribute('data-count')).toBe('2');
|
||||
// Output ownership stays with each producing call when the activity group changes.
|
||||
expect(groups).toHaveLength(2);
|
||||
expect(groups.map((row) => row.getAttribute('data-count'))).toEqual(['1', '1']);
|
||||
});
|
||||
|
||||
it('marks grouped artifacts as history while another response regenerates', () => {
|
||||
|
|
@ -459,17 +468,25 @@ describe('ContentParts integration: MCP image hoist and grouping', () => {
|
|||
content,
|
||||
attachments,
|
||||
});
|
||||
expect(screen.getByTestId('attachment-group')).toHaveAttribute('data-submitting', 'false');
|
||||
expect(
|
||||
screen
|
||||
.getAllByTestId('attachment-group')
|
||||
.every((row) => row.getAttribute('data-submitting') === 'false'),
|
||||
).toBe(true);
|
||||
|
||||
rerender(
|
||||
<RecoilRoot>
|
||||
<ContentParts {...baseProps} isSubmitting content={content} attachments={attachments} />
|
||||
</RecoilRoot>,
|
||||
);
|
||||
expect(screen.getByTestId('attachment-group')).toHaveAttribute('data-submitting', 'true');
|
||||
expect(
|
||||
screen
|
||||
.getAllByTestId('attachment-group')
|
||||
.every((row) => row.getAttribute('data-submitting') === 'true'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('does not group a single tool call — image renders inline (no hoist)', () => {
|
||||
it('keeps a single call output beside the tool without adding a tool group', () => {
|
||||
const content = [makeMcpToolCall('t1')];
|
||||
const attachments = [imageAttachment('t1', 'a.png')];
|
||||
|
||||
|
|
@ -479,7 +496,7 @@ describe('ContentParts integration: MCP image hoist and grouping', () => {
|
|||
attachments,
|
||||
});
|
||||
|
||||
// Single tool call: AttachmentGroup is rendered by ToolCall, not hoisted.
|
||||
// A single tool still has one output row and no activity-group label.
|
||||
const groups = screen.queryAllByTestId('attachment-group');
|
||||
expect(groups).toHaveLength(1);
|
||||
expect(groups[0].getAttribute('data-count')).toBe('1');
|
||||
|
|
@ -506,9 +523,11 @@ describe('ContentParts integration: MCP image hoist and grouping', () => {
|
|||
});
|
||||
|
||||
const groups = screen.getAllByTestId('attachment-group');
|
||||
expect(groups).toHaveLength(1);
|
||||
// Both image and file are in the hoisted group.
|
||||
expect(groups[0].getAttribute('data-count')).toBe('2');
|
||||
expect(groups).toHaveLength(2);
|
||||
expect(groups.map((row) => row.getAttribute('data-paths'))).toEqual([
|
||||
'/files/a.png',
|
||||
'/files/doc.pdf',
|
||||
]);
|
||||
});
|
||||
|
||||
it('renders no AttachmentGroup when grouped tool calls have no attachments', () => {
|
||||
|
|
@ -1817,26 +1836,26 @@ describe('ContentParts integration: phase media row', () => {
|
|||
];
|
||||
const phaseAttachments = [imageAttachment('t1', 'a.png'), imageAttachment('t2', 'b.png')];
|
||||
|
||||
it("lifts a phase's files into a media row the fold cannot hide", () => {
|
||||
it("lifts a phase's files into stable rows the fold cannot hide", () => {
|
||||
renderContentParts({ ...baseProps, content: phaseContent(), attachments: phaseAttachments });
|
||||
|
||||
const rows = screen.getAllByTestId('attachment-group');
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].getAttribute('data-count')).toBe('2');
|
||||
expect(rows).toHaveLength(2);
|
||||
expect(rows.map((row) => row.getAttribute('data-count'))).toEqual(['1', '1']);
|
||||
// The row is a sibling of the collapsed card, not a child of its panel.
|
||||
expect(
|
||||
within(screen.getByTestId('activity-phase-panel')).queryByTestId('attachment-group'),
|
||||
).toBeNull();
|
||||
});
|
||||
|
||||
it('still shows one row once the reader expands the phase', () => {
|
||||
it('still shows each output once the reader expands the phase', () => {
|
||||
renderContentParts({ ...baseProps, content: phaseContent(), attachments: phaseAttachments });
|
||||
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Generated five mortgage charts' }));
|
||||
|
||||
const rows = screen.getAllByTestId('attachment-group');
|
||||
expect(rows).toHaveLength(1);
|
||||
expect(rows[0].getAttribute('data-count')).toBe('2');
|
||||
expect(rows).toHaveLength(2);
|
||||
expect(rows.map((row) => row.getAttribute('data-count'))).toEqual(['1', '1']);
|
||||
});
|
||||
|
||||
it('lifts the file of a lone tool call too, not just a grouped batch', () => {
|
||||
|
|
@ -1854,21 +1873,143 @@ describe('ContentParts integration: phase media row', () => {
|
|||
).toBeNull();
|
||||
});
|
||||
|
||||
it('shows the latest record when a tool writes the same file twice', () => {
|
||||
// Both records name one stored file; the later carries the fresher
|
||||
// lifecycle fields. Hoisting makes this row the only surface for it, so a
|
||||
// first-seen dedup would pin the stale record on screen.
|
||||
const stale = { ...imageAttachment('t1', 'a.png'), file_id: 'f1' } as TAttachment;
|
||||
const fresh = { ...stale, filepath: '/files/a.png?v=2' } as TAttachment;
|
||||
const content = [makeMcpToolCall('t1'), makePhasePart(0, 1, 'Redrew the chart')];
|
||||
|
||||
renderContentParts({ ...baseProps, content, attachments: [stale, fresh] });
|
||||
|
||||
it('keeps a generated file mounted when the same tool also publishes an MCP App', async () => {
|
||||
const call = makeMcpToolCall('chart');
|
||||
const image = imageAttachment('chart', 'chart.png');
|
||||
const app = {
|
||||
type: Tools.ui_resources,
|
||||
toolCallId: 'chart',
|
||||
[Tools.ui_resources]: [
|
||||
{
|
||||
resourceId: 'app',
|
||||
uri: 'ui://demo/view',
|
||||
mimeType: 'text/html;profile=mcp-app',
|
||||
toolName: 'show_app',
|
||||
serverName: 'demo',
|
||||
},
|
||||
],
|
||||
} as TAttachment;
|
||||
const frame = (attachments: TAttachment[]) => (
|
||||
<RecoilRoot>
|
||||
<ContentParts {...baseProps} content={[call]} attachments={attachments} />
|
||||
</RecoilRoot>
|
||||
);
|
||||
const { rerender } = render(frame([image]));
|
||||
const row = screen.getByTestId('attachment-group');
|
||||
expect(row.getAttribute('data-count')).toBe('1');
|
||||
expect(row.getAttribute('data-paths')).toBe('/files/a.png?v=2');
|
||||
rerender(frame([image, app]));
|
||||
expect(screen.getByTestId('attachment-group')).toBe(row);
|
||||
expect(screen.getByTestId('mcp-app-attachments')).toBeInTheDocument();
|
||||
await act(async () => {});
|
||||
});
|
||||
|
||||
it.each([false, true])(
|
||||
'deduplicates legacy shared files while retaining step-scoped outputs (scoped %s)',
|
||||
(scoped) => {
|
||||
const first = makeMcpToolCall('call_0', true, 'step-a');
|
||||
const second = makeMcpToolCall('call_0', true, 'step-b');
|
||||
const shared = {
|
||||
...imageAttachment('call_0', 'shared.png'),
|
||||
file_id: 'shared',
|
||||
} as TAttachment;
|
||||
const attachments = scoped
|
||||
? [
|
||||
{ ...shared, stepId: 'step-a' },
|
||||
{ ...shared, stepId: 'step-b' },
|
||||
]
|
||||
: [shared];
|
||||
renderContentParts({
|
||||
...baseProps,
|
||||
content: [first, second, makePhasePart(0, 2, 'Generated charts')],
|
||||
attachments,
|
||||
});
|
||||
const rows = screen.getAllByTestId('attachment-group');
|
||||
expect(rows).toHaveLength(scoped ? 2 : 1);
|
||||
expect(rows.map((row) => row.getAttribute('data-count'))).toEqual(
|
||||
scoped ? ['1', '1'] : ['1'],
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
it.each([true, false])(
|
||||
'shows the latest record when a tool writes the same file twice (phase %s)',
|
||||
(phase) => {
|
||||
// Both records name one stored file; the later carries the fresher
|
||||
// lifecycle fields. Hoisting makes this row the only surface for it, so a
|
||||
// first-seen dedup would pin the stale record on screen.
|
||||
const stale = { ...imageAttachment('t1', 'a.png'), file_id: 'f1' } as TAttachment;
|
||||
const fresh = { ...stale, filepath: '/files/a.png?v=2' } as TAttachment;
|
||||
const content = [
|
||||
makeMcpToolCall('t1'),
|
||||
...(phase ? [makePhasePart(0, 1, 'Redrew the chart')] : []),
|
||||
];
|
||||
|
||||
renderContentParts({ ...baseProps, content, attachments: [stale, fresh] });
|
||||
|
||||
const row = screen.getByTestId('attachment-group');
|
||||
expect(row.getAttribute('data-count')).toBe('1');
|
||||
expect(row.getAttribute('data-paths')).toBe('/files/a.png?v=2');
|
||||
},
|
||||
);
|
||||
|
||||
it.each([true, false])(
|
||||
'keeps the output row mounted through the exported chat phases (live folding %s)',
|
||||
async (foldLiveActivity) => {
|
||||
const batch = (id: string, label: string): TMessageContentParts => ({
|
||||
type: ContentTypes.ACTIVITY_LABEL,
|
||||
activity_label: label,
|
||||
tool_call_ids: [id],
|
||||
status: 'ok',
|
||||
pending: false,
|
||||
});
|
||||
// The exported response: a skill phase, query, prose, chart, answer, then summary.
|
||||
const content: TMessageContentParts[] = [
|
||||
{ type: ContentTypes.THINK, think: 'Load the warehouse guidance.' },
|
||||
makeMcpToolCall('skill'),
|
||||
batch('skill', 'Loaded guidance'),
|
||||
{ type: ContentTypes.THINK, think: 'Query annual prices.' },
|
||||
makeTextPart('Let us query the annual price trend.'),
|
||||
makePhasePart(0, 4, 'Prepared the query'),
|
||||
makeMcpToolCall('query'),
|
||||
batch('query', 'Queried annual prices'),
|
||||
makeTextPart('Now let us chart the query results.'),
|
||||
makeMcpToolCall('chart'),
|
||||
batch('chart', 'Created the chart'),
|
||||
makeTextPart('Here is the chart and CSV.'),
|
||||
makePhasePart(6, 11, 'Charted query results'),
|
||||
];
|
||||
const files = [imageAttachment('chart', 'chart.png')];
|
||||
const frame = (parts: TMessageContentParts[], settled = false, sibling = false) => {
|
||||
const messageId = settled ? 'response' : 'request_';
|
||||
return (
|
||||
<RecoilRoot>
|
||||
<ContentParts
|
||||
{...baseProps}
|
||||
messageId={sibling ? 'sibling' : messageId}
|
||||
conversationId="conversation"
|
||||
renderOwnerId={settled ? undefined : 'request'}
|
||||
isSubmitting={!settled}
|
||||
foldLiveActivity={foldLiveActivity}
|
||||
content={parts}
|
||||
attachments={files}
|
||||
/>
|
||||
</RecoilRoot>
|
||||
);
|
||||
};
|
||||
const { rerender } = render(frame(content.slice(0, 11)));
|
||||
const row = screen.getByTestId('attachment-group');
|
||||
rerender(frame(content.slice(0, 12)));
|
||||
expect(screen.getByTestId('attachment-group')).toBe(row);
|
||||
rerender(frame(content));
|
||||
expect(screen.getByTestId('attachment-group')).toBe(row);
|
||||
rerender(frame(content, true));
|
||||
expect(screen.getByTestId('attachment-group')).toBe(row);
|
||||
// A real sibling switch starts a fresh output surface, even when it reused a file.
|
||||
rerender(frame(content, true, true));
|
||||
expect(screen.getByTestId('attachment-group')).not.toBe(row);
|
||||
await act(async () => {});
|
||||
},
|
||||
);
|
||||
|
||||
it('renders no row for a phase that produced nothing', () => {
|
||||
renderContentParts({ ...baseProps, content: phaseContent() });
|
||||
|
||||
|
|
|
|||
|
|
@ -377,3 +377,5 @@ export {
|
|||
getSourceDomains,
|
||||
getUniqueDomainSources,
|
||||
} from './components/parts/sources';
|
||||
|
||||
export { HiddenAttachmentsContext, useVisibleAttachments } from './components/parts/media';
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ import { Tools } from 'librechat-data-provider';
|
|||
import { Button, useLocalize, useRemScale } from '@librechat/client';
|
||||
import { Loader2, AlertCircle, Download, ChevronDown, Files as FilesIcon } from 'lucide-react';
|
||||
import type { TAttachment, TFile, TAttachmentMetadata } from 'librechat-data-provider';
|
||||
import type { ReactNode } from 'react';
|
||||
import type { ReactNode, MemoExoticComponent } from 'react';
|
||||
import type { AttachmentArtifact } from '../../react/files';
|
||||
import {
|
||||
bySalience,
|
||||
|
|
@ -19,6 +19,7 @@ import FileContainer from '../composer/files/FileContainer';
|
|||
import { useMessagePartsHost } from '../../react/host';
|
||||
import useExpandCollapse from './useExpandCollapse';
|
||||
import { useFilesHost } from '../../react/files';
|
||||
import { useVisibleAttachments } from './media';
|
||||
|
||||
const PLACEHOLDER_ROW_CLASS = `${TOOL_ROW_CLASSES} ${ARTIFACT_ROW_NAV_SPACING} text-text-secondary text-sm`;
|
||||
|
||||
|
|
@ -383,24 +384,11 @@ const TextAttachment = memo(
|
|||
);
|
||||
|
||||
const ImageAttachment = memo(({ attachment }: { attachment: TAttachment }) => {
|
||||
const [isLoaded, setIsLoaded] = useState(false);
|
||||
const renderImage = useMessagePartsHost().renderImage;
|
||||
const { width, height, filepath = null } = attachment as TFile & TAttachmentMetadata;
|
||||
|
||||
useEffect(() => {
|
||||
setIsLoaded(false);
|
||||
const timer = setTimeout(() => setIsLoaded(true), 100);
|
||||
return () => clearTimeout(timer);
|
||||
}, [attachment]);
|
||||
|
||||
return (
|
||||
<div
|
||||
className={
|
||||
isLoaded
|
||||
? 'attachment-settle scale-100 subpixel-antialiased opacity-100 transition-all duration-500 ease-out'
|
||||
: 'attachment-settle scale-[0.98] subpixel-antialiased opacity-0 transition-all duration-500 ease-out'
|
||||
}
|
||||
>
|
||||
<div>
|
||||
{renderImage({
|
||||
altText: attachment.filename ?? '',
|
||||
imagePath: filepath ?? '',
|
||||
|
|
@ -424,7 +412,8 @@ function HostArtifact({
|
|||
|
||||
export default function Attachment({ attachment }: { attachment?: TAttachment }): ReactNode {
|
||||
const { classifyAttachment } = useFilesHost();
|
||||
if (!attachment) {
|
||||
const visible = useVisibleAttachments(attachment ? [attachment] : undefined);
|
||||
if (!attachment || visible?.length === 0) {
|
||||
return null;
|
||||
}
|
||||
if (attachment.type === Tools.web_search) {
|
||||
|
|
@ -453,8 +442,9 @@ export default function Attachment({ attachment }: { attachment?: TAttachment })
|
|||
return <FileAttachment attachment={attachment} />;
|
||||
}
|
||||
|
||||
export function AttachmentGroup({ attachments }: { attachments?: TAttachment[] }): ReactNode {
|
||||
function AttachmentGroupBody({ attachments: source }: { attachments?: TAttachment[] }): ReactNode {
|
||||
const { classifyAttachment } = useFilesHost();
|
||||
const attachments = useVisibleAttachments(source);
|
||||
if (!attachments || attachments.length === 0) {
|
||||
return null;
|
||||
}
|
||||
|
|
@ -590,3 +580,18 @@ export function AttachmentGroup({ attachments }: { attachments?: TAttachment[] }
|
|||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export const AttachmentGroup: MemoExoticComponent<typeof AttachmentGroupBody> = memo(
|
||||
AttachmentGroupBody,
|
||||
(previous, next) => {
|
||||
const left = previous.attachments;
|
||||
const right = next.attachments;
|
||||
return (
|
||||
left === right ||
|
||||
(left != null &&
|
||||
right != null &&
|
||||
left.length === right.length &&
|
||||
left.every((file, index) => file === right[index]))
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ import { createMessagePartsHost } from '../../../__fixtures__/partsHost';
|
|||
import { MessagePartsHostProvider } from '../../../react/host';
|
||||
import Attachment, { AttachmentGroup } from '../Attachment';
|
||||
import { FilesHostProvider } from '../../../react/files';
|
||||
import { HiddenAttachmentsContext } from '../media';
|
||||
|
||||
const TRANSLATIONS: Record<string, string> = {
|
||||
com_ui_download: 'Download',
|
||||
|
|
@ -173,3 +174,55 @@ describe('AttachmentGroup', () => {
|
|||
expect(screen.getByRole('button', { name: 'results.zip' })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
||||
describe('stable tool outputs', () => {
|
||||
it('keeps an unchanged file row out of streaming parent renders', () => {
|
||||
const attachment = file();
|
||||
const classifyAttachment = jest.fn(() => null);
|
||||
const host = { ...baseHost, classifyAttachment };
|
||||
const frame = (files: TAttachment[]) => (
|
||||
<MessagePartsHostProvider host={createMessagePartsHost()}>
|
||||
<FilesHostProvider host={host}>
|
||||
<AttachmentGroup attachments={files} />
|
||||
</FilesHostProvider>
|
||||
</MessagePartsHostProvider>
|
||||
);
|
||||
const { rerender } = render(frame([attachment]));
|
||||
const calls = classifyAttachment.mock.calls.length;
|
||||
rerender(frame([attachment]));
|
||||
expect(classifyAttachment).toHaveBeenCalledTimes(calls);
|
||||
rerender(frame([{ ...attachment, filename: 'updated.zip' }]));
|
||||
expect(screen.getByRole('button', { name: 'updated.zip' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('does not hide a painted image when its attachment metadata is replaced', () => {
|
||||
const attachment = file({ filename: 'chart.png', type: 'image/png', width: 100, height: 100 });
|
||||
const host = createMessagePartsHost({
|
||||
renderImage: ({ imagePath, altText }) => <img src={imagePath} alt={altText} />,
|
||||
});
|
||||
const frame = (value: TAttachment) => (
|
||||
<MessagePartsHostProvider host={host}>
|
||||
<AttachmentGroup attachments={[value]} />
|
||||
</MessagePartsHostProvider>
|
||||
);
|
||||
const { rerender } = render(frame(attachment));
|
||||
const image = screen.getByRole('img', { name: 'chart.png' });
|
||||
expect(image.parentElement).not.toHaveClass('opacity-0');
|
||||
rerender(frame({ ...attachment, bytes: 4000 }));
|
||||
expect(screen.getByRole('img', { name: 'chart.png' })).toBe(image);
|
||||
expect(image.parentElement).not.toHaveClass('opacity-0');
|
||||
});
|
||||
|
||||
it('hides only files already rendered at the stable parent', () => {
|
||||
const first = file();
|
||||
const second = file({ file_id: 'second', filename: 'second.zip' });
|
||||
renderRow(
|
||||
<HiddenAttachmentsContext.Provider value={new Set([first])}>
|
||||
<AttachmentGroup attachments={[first, second]} />
|
||||
<Attachment attachment={first} />
|
||||
</HiddenAttachmentsContext.Provider>,
|
||||
);
|
||||
expect(screen.queryByRole('button', { name: 'results.zip' })).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'second.zip' })).toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
|
|
|
|||
18
packages/chat/src/components/parts/media.ts
Normal file
18
packages/chat/src/components/parts/media.ts
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
import { createContext, useContext, useMemo } from 'react';
|
||||
import type { TAttachment } from 'librechat-data-provider';
|
||||
import type { Context } from 'react';
|
||||
|
||||
/** Files rendered beside the activity tree rather than inside its changing folds. */
|
||||
export const HiddenAttachmentsContext: Context<ReadonlySet<TAttachment> | null> =
|
||||
createContext<ReadonlySet<TAttachment> | null>(null);
|
||||
|
||||
export function useVisibleAttachments(attachments?: TAttachment[]): TAttachment[] | undefined {
|
||||
const hidden = useContext(HiddenAttachmentsContext);
|
||||
return useMemo(
|
||||
() =>
|
||||
hidden == null || hidden.size === 0
|
||||
? attachments
|
||||
: attachments?.filter((file) => !hidden.has(file)),
|
||||
[attachments, hidden],
|
||||
);
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
import {
|
||||
createContext,
|
||||
memo,
|
||||
useCallback,
|
||||
useContext,
|
||||
useEffect,
|
||||
|
|
@ -10,6 +11,7 @@ import {
|
|||
import { CircleX } from 'lucide-react';
|
||||
import { Button, cn, useLocalize } from '@librechat/client';
|
||||
import type { TranslationKeys } from '@librechat/client';
|
||||
import type { MemoExoticComponent } from 'react';
|
||||
|
||||
/**
|
||||
* A request to show the failed calls under a header, travelling DOWN the fold
|
||||
|
|
@ -104,7 +106,7 @@ export function useFailedReveal(
|
|||
* button, and its name says what it does rather than repeating the count the
|
||||
* header already carries.
|
||||
*/
|
||||
export function FailedRevealPill({
|
||||
function FailedRevealPillBody({
|
||||
count,
|
||||
total,
|
||||
onReveal,
|
||||
|
|
@ -144,3 +146,6 @@ export function FailedRevealPill({
|
|||
</Button>
|
||||
);
|
||||
}
|
||||
|
||||
export const FailedRevealPill: MemoExoticComponent<typeof FailedRevealPillBody> =
|
||||
memo(FailedRevealPillBody);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue