Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 55 additions & 1 deletion client/src/components/Chat/Presentation.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
sideChatAvailableAtom,
} from '@librechat/chat/react';
import type { TConversation, TStartupConfig } from 'librechat-data-provider';
import type { SideChat } from '@librechat/chat/react';
import type { JotaiStore } from 'test/harness';
import type { Artifact } from '~/common';
import { prepareUndockedDocument } from '~/components/Artifacts/undockedWindow';
Expand Down Expand Up @@ -39,6 +40,11 @@ const mockOpenChildLabel = 'Open Child Activity';
const mockSideChatLabel = 'Side chat loaded';
const mockOpenSideChatLabel = 'Open Side Chat';
const mockAskSideChatLabel = 'Ask for a side chat';
let mockSideChatProps: {
chat: SideChat;
onClose: () => void;
onHandedTextTaken: (expected: SideChat) => void;
};
const mockSelectAgentConversationLabel = 'Select Agent Conversation';
const mockStartAgentRunLabel = 'Start Agent Run';
/**
Expand Down Expand Up @@ -86,7 +92,8 @@ jest.mock('@librechat/chat/components', () => {
),
loadSideChatPanel: () =>
Promise.resolve({
default: function MockSideChatPanel() {
default: function MockSideChatPanel(props: typeof mockSideChatProps) {
mockSideChatProps = props;
const parent = useParentSubagents();
return (
<aside>
Expand Down Expand Up @@ -430,6 +437,53 @@ describe('Presentation Artifact loading', () => {
);
});

it.each(['same', 'different'] as const)(
'an old pane callback cannot close or consume a newer %s destination handoff',
async (destination) => {
let atoms!: JotaiStore;
renderPresentation(
<ChatSurfaceHarness
seed={(store) => {
atoms = store;
}}
>
<RecoilRoot>
<Presentation>
<OpenSideChat />
</Presentation>
</RecoilRoot>
</ChatSurfaceHarness>,
);
fireEvent.click(screen.getByRole('button', { name: mockOpenSideChatLabel }));
await screen.findByText(mockSideChatLabel);
const conversation = atoms.get(sideChatAtom)!.conversation;
const captured = mockSideChatProps.chat;
const consume = mockSideChatProps.onHandedTextTaken;
const close = mockSideChatProps.onClose;
act(() => {
atoms.set(sideChatAtom, {
conversation: {
...conversation,
conversationId: destination === 'same' ? conversation.conversationId : 'new-side',
},
text: 'Same question.',
});
});
const replacement = atoms.get(sideChatAtom)!;
expect(replacement).not.toBe(captured);
act(() => {
consume(captured);
close();
});
expect(atoms.get(sideChatAtom)).toBe(replacement);
expect(atoms.get(sideChatAtom)?.text).toBe('Same question.');
act(() => mockSideChatProps.onHandedTextTaken(replacement));
expect(atoms.get(sideChatAtom)?.text).toBeUndefined();
act(() => mockSideChatProps.onClose());
expect(atoms.get(sideChatAtom)).toBeNull();
},
);

it('shows a side chat beside the conversation, with child activity read for its own conversation', async () => {
renderPresentation(
<ChatSurfaceHarness>
Expand Down
20 changes: 15 additions & 5 deletions client/src/components/Chat/Presentation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { EModelEndpoint } from 'librechat-data-provider';
import { useRecoilValue, useSetRecoilState, useResetRecoilState } from 'recoil';
import {
sideChatAtom,
sideChatDraftTakenAtom,
SIDE_CHAT_PANE,
sideChatBesideAtom,
sideChatRequestAtom,
Expand All @@ -12,6 +13,7 @@ import {
SideChatHostProvider,
ParentSubagentsProvider,
} from '@librechat/chat/react';
import type { SideChat } from '@librechat/chat/react';
import useArtifactsRegistryLifetime from '~/hooks/Artifacts/useArtifactsRegistryLifetime';
import { MessageSurfaceContext } from '~/components/Chat/Messages/ui/surface';
import DragDropWrapper from '~/components/Chat/Input/Files/DragDropWrapper';
Expand Down Expand Up @@ -70,9 +72,16 @@ export default function Presentation({
const sideChat = useAtomValue(sideChatAtom);
const isSideChatSubmitting = useIsSubmitting(SIDE_CHAT_PANE);
const setSideChat = useSetAtom(sideChatAtom);
const closeSideChat = useCallback(() => setSideChat(null), [setSideChat]);
const takeSideChatDraft = useSetAtom(sideChatDraftTakenAtom);
const closeSideChat = useCallback(
() => setSideChat((current) => (current === sideChat ? null : current)),
[setSideChat, sideChat],
);
const takeSideChatText = useCallback(
() => setSideChat((current) => current && { conversation: current.conversation }),
(expected: SideChat) =>
setSideChat((current) =>
current === expected ? { conversation: current.conversation } : current,
),
[setSideChat],
);
const previousConversationIdRef = useRef<string | null>(null);
Expand Down Expand Up @@ -187,9 +196,9 @@ export default function Presentation({
useEffect(() => {
if (artifactShown) {
withdrawSideChatRequest(null);
closeSideChat();
setSideChat(null);
}
}, [artifactShown, currentArtifactId, closeSideChat, withdrawSideChatRequest]);
}, [artifactShown, currentArtifactId, setSideChat, withdrawSideChatRequest]);

const subagentElement = useMemo(() => {
if (
Expand Down Expand Up @@ -227,12 +236,13 @@ export default function Presentation({
chat={sideChat}
onClose={closeSideChat}
onHandedTextTaken={takeSideChatText}
onDraftTaken={takeSideChatDraft}
/>
</Suspense>
</ParentSubagentsProvider>
</SideChatHostProvider>
);
}, [closeSideChat, isSideChatSubmitting, sideChat, takeSideChatText]);
}, [closeSideChat, isSideChatSubmitting, sideChat, takeSideChatDraft, takeSideChatText]);

/* Undocked, the pane renders into its own window: the side panel gives its
* width back to the conversation instead of holding an empty column. */
Expand Down
Loading
Loading