mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-11-20 15:02:02 +00:00
🔘 fix: Sync Workspace Choices When Chats Start (#16266)
Co-authored-by: Lia <lia@librechat.ai>
This commit is contained in:
parent
89effb9793
commit
7979bdbfd4
2 changed files with 290 additions and 0 deletions
267
client/src/hooks/SSE/__tests__/workspace.spec.tsx
Normal file
267
client/src/hooks/SSE/__tests__/workspace.spec.tsx
Normal file
|
|
@ -0,0 +1,267 @@
|
|||
import { useState } from 'react';
|
||||
import { RecoilRoot } from 'recoil';
|
||||
import { MemoryRouter } from 'react-router-dom';
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
|
||||
import { Constants, EModelEndpoint, QueryKeys } from 'librechat-data-provider';
|
||||
import type { EventSubmission, TConversation } from 'librechat-data-provider';
|
||||
import useCodeWorkspace from '~/hooks/Agents/useCodeWorkspace';
|
||||
import useEventHandlers from '../useEventHandlers';
|
||||
|
||||
const mockStatus = jest.fn();
|
||||
const mockStartupConfig = jest.fn();
|
||||
const mockApplyAgentTemplate = jest.fn();
|
||||
const mockAnnounce = jest.fn();
|
||||
const mockReset = jest.fn();
|
||||
|
||||
jest.mock('~/hooks/Agents', () => ({ useApplyAgentTemplate: () => mockApplyAgentTemplate }));
|
||||
jest.mock('~/hooks/AuthContext', () => ({ useAuthContext: () => ({ token: 'test' }) }));
|
||||
jest.mock('~/Providers', () => ({
|
||||
useLiveAnnouncer: () => ({ announcePolite: mockAnnounce }),
|
||||
useAgentsMapContext: () => ({}),
|
||||
}));
|
||||
jest.mock('~/hooks/SSE/useContentHandler', () => () => ({}));
|
||||
jest.mock('~/hooks/SSE/useAttachmentHandler', () => () => jest.fn());
|
||||
jest.mock('~/hooks/SSE/useStepHandler', () => () => ({
|
||||
resetSubagentAtoms: mockReset,
|
||||
resetPtcAtoms: mockReset,
|
||||
}));
|
||||
jest.mock('~/hooks/Agents/workspacePreferences', () => ({
|
||||
useWorkspacePreferences: () => ({ get: () => undefined }),
|
||||
}));
|
||||
jest.mock('~/hooks/Roles/useHasAccess', () => () => true);
|
||||
jest.mock('~/hooks/Agents/useAgentToolPermissions', () => () => ({
|
||||
agent: {
|
||||
id: 'agent_primary',
|
||||
stateful_code_sessions: true,
|
||||
code_environment_id: 'personal-vm',
|
||||
tools: ['execute_code'],
|
||||
},
|
||||
}));
|
||||
jest.mock('~/hooks/Agents/useGetAgentsConfig', () => () => ({
|
||||
agentsConfig: {
|
||||
capabilities: ['execute_code', 'stateful_code_sessions'],
|
||||
statefulCodeSessions: {
|
||||
environments: [{ id: 'personal-vm', type: 'attached', name: 'Personal VM' }],
|
||||
},
|
||||
},
|
||||
}));
|
||||
jest.mock('~/data-provider', () => ({
|
||||
...jest.requireActual('~/data-provider'),
|
||||
useCodeEnvironmentStatusQueries: () => mockStatus(),
|
||||
useGetStartupConfig: () => ({ data: mockStartupConfig() }),
|
||||
}));
|
||||
|
||||
const selection = { environmentId: 'personal-vm', workspaceId: 'project-a' };
|
||||
const initialConversation = {
|
||||
conversationId: Constants.NEW_CONVO,
|
||||
endpoint: EModelEndpoint.agents,
|
||||
agent_id: 'agent_primary',
|
||||
title: 'New Chat',
|
||||
} as TConversation;
|
||||
|
||||
function setup(current = initialConversation, isAddedRequest = false) {
|
||||
const queryClient = new QueryClient({ defaultOptions: { queries: { retry: false } } });
|
||||
queryClient.setQueryData([QueryKeys.allConversations], { pages: [], pageParams: [] });
|
||||
const wrapper = ({ children }: { children: React.ReactNode }) => (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<RecoilRoot>
|
||||
<MemoryRouter initialEntries={['/c/new']}>{children}</MemoryRouter>
|
||||
</RecoilRoot>
|
||||
</QueryClientProvider>
|
||||
);
|
||||
const hook = renderHook(
|
||||
() => {
|
||||
const [conversation, setConversation] = useState<TConversation | null>(current);
|
||||
const handlers = useEventHandlers({
|
||||
isAddedRequest,
|
||||
setConversation,
|
||||
setMessages: jest.fn(),
|
||||
getMessages: () => [],
|
||||
setCompleted: jest.fn(),
|
||||
setIsSubmitting: jest.fn(),
|
||||
setShowStopButton: jest.fn(),
|
||||
});
|
||||
return {
|
||||
conversation,
|
||||
setConversation,
|
||||
workspace: useCodeWorkspace(conversation),
|
||||
...handlers,
|
||||
};
|
||||
},
|
||||
{ wrapper },
|
||||
);
|
||||
const submission: EventSubmission = {
|
||||
isTemporary: false,
|
||||
endpointOption: { endpoint: EModelEndpoint.agents },
|
||||
conversation: current,
|
||||
messages: [],
|
||||
userMessage: {
|
||||
messageId: 'user-1',
|
||||
text: 'Hello',
|
||||
sender: 'User',
|
||||
isCreatedByUser: true,
|
||||
parentMessageId: String(Constants.NO_PARENT),
|
||||
conversationId: 'saved-chat',
|
||||
},
|
||||
initialResponse: {
|
||||
messageId: 'response-1',
|
||||
parentMessageId: 'user-1',
|
||||
conversationId: 'saved-chat',
|
||||
text: '',
|
||||
sender: 'Assistant',
|
||||
isCreatedByUser: false,
|
||||
},
|
||||
...hook.result.current.workspace.resolveSubmission(
|
||||
current.codeWorkspaces,
|
||||
current.codeEnvironmentMode,
|
||||
),
|
||||
};
|
||||
const createdData = {
|
||||
conversation: current,
|
||||
requestMessage: submission.userMessage,
|
||||
responseMessage: submission.initialResponse,
|
||||
};
|
||||
return { ...hook, submission, createdData, queryClient };
|
||||
}
|
||||
|
||||
describe('workspace decision at stream acknowledgement', () => {
|
||||
beforeEach(() => {
|
||||
mockStartupConfig.mockReturnValue({ codeEnvironmentDecisionVersion: 1 });
|
||||
mockStatus.mockReturnValue([
|
||||
{
|
||||
data: {
|
||||
environmentId: 'personal-vm',
|
||||
status: 'ready',
|
||||
workspaces: [{ id: 'project-a', name: 'Project A' }],
|
||||
},
|
||||
isLoading: false,
|
||||
isError: false,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['created', true, false],
|
||||
['sync', true, false],
|
||||
['created', false, false],
|
||||
['sync', false, false],
|
||||
['created', true, true],
|
||||
['sync', true, true],
|
||||
] as const)(
|
||||
'seals an automatic workspace on %s (decision protocol %s, added pane %s)',
|
||||
(event, decisions, isAddedRequest) => {
|
||||
mockStartupConfig.mockReturnValue(decisions ? { codeEnvironmentDecisionVersion: 1 } : {});
|
||||
const { result, submission, createdData } = setup(initialConversation, isAddedRequest);
|
||||
expect(result.current.conversation?.codeWorkspaces).toBeUndefined();
|
||||
expect(submission.codeWorkspaces).toEqual([selection]);
|
||||
expect(result.current.workspace.locked).toBe(false);
|
||||
|
||||
act(() => {
|
||||
if (event === 'created') {
|
||||
result.current.createdHandler(createdData, submission);
|
||||
} else {
|
||||
result.current.syncHandler(
|
||||
{
|
||||
sync: true,
|
||||
thread_id: 'thread',
|
||||
conversationId: 'saved-chat',
|
||||
requestMessage: submission.userMessage,
|
||||
responseMessage: submission.initialResponse,
|
||||
},
|
||||
submission,
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
expect(result.current.conversation).toMatchObject({
|
||||
conversationId: 'saved-chat',
|
||||
codeEnvironmentMode: 'attached',
|
||||
codeWorkspaces: [selection],
|
||||
});
|
||||
expect(result.current.workspace).toMatchObject({
|
||||
locked: true,
|
||||
canSubmit: true,
|
||||
state: 'ready',
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it('seals a without-attached submission while multiple workspaces remain available', () => {
|
||||
mockStatus.mockReturnValue([
|
||||
{
|
||||
data: {
|
||||
environmentId: 'personal-vm',
|
||||
status: 'ready',
|
||||
workspaces: [{ id: 'project-a' }, { id: 'project-b' }],
|
||||
},
|
||||
},
|
||||
]);
|
||||
const { result, submission, createdData } = setup();
|
||||
expect(submission.codeEnvironmentMode).toBe('without_attached');
|
||||
act(() => result.current.createdHandler(createdData, submission));
|
||||
expect(result.current.workspace).toMatchObject({
|
||||
locked: true,
|
||||
canSubmit: true,
|
||||
state: 'without_attached',
|
||||
});
|
||||
});
|
||||
|
||||
it('does not replace a saved decision with a replay of an older submission', () => {
|
||||
const { result, submission, createdData } = setup({
|
||||
...initialConversation,
|
||||
conversationId: 'saved-chat',
|
||||
codeEnvironmentMode: 'without_attached',
|
||||
});
|
||||
act(() =>
|
||||
result.current.createdHandler(createdData, {
|
||||
...submission,
|
||||
codeEnvironmentMode: 'attached',
|
||||
codeWorkspaces: [selection],
|
||||
}),
|
||||
);
|
||||
expect(result.current.conversation?.codeEnvironmentMode).toBe('without_attached');
|
||||
expect(result.current.conversation?.codeWorkspaces).toBeUndefined();
|
||||
});
|
||||
|
||||
it('records a first decision on a saved conversation that has not used a workspace', () => {
|
||||
const { result, submission, createdData } = setup({
|
||||
...initialConversation,
|
||||
conversationId: 'saved-chat',
|
||||
});
|
||||
expect(result.current.workspace.locked).toBe(false);
|
||||
act(() => result.current.createdHandler(createdData, submission));
|
||||
expect(result.current.conversation?.codeWorkspaces).toEqual([selection]);
|
||||
expect(result.current.workspace.locked).toBe(true);
|
||||
});
|
||||
|
||||
it('uses the submitted without-attached choice rather than a stale draft selection', () => {
|
||||
const { result, submission, createdData } = setup({
|
||||
...initialConversation,
|
||||
codeEnvironmentMode: 'attached',
|
||||
codeWorkspaces: [selection],
|
||||
});
|
||||
act(() =>
|
||||
result.current.createdHandler(createdData, {
|
||||
...submission,
|
||||
codeEnvironmentMode: 'without_attached',
|
||||
codeWorkspaces: undefined,
|
||||
}),
|
||||
);
|
||||
expect(result.current.conversation?.codeWorkspaces).toBeUndefined();
|
||||
expect(result.current.workspace).toMatchObject({ locked: true, state: 'without_attached' });
|
||||
});
|
||||
|
||||
it('leaves an undecided conversation editable when the submission has no decision', () => {
|
||||
const { result, submission, createdData } = setup();
|
||||
act(() =>
|
||||
result.current.createdHandler(createdData, {
|
||||
...submission,
|
||||
codeEnvironmentMode: undefined,
|
||||
codeWorkspaces: undefined,
|
||||
}),
|
||||
);
|
||||
expect(result.current.conversation?.codeEnvironmentMode).toBeUndefined();
|
||||
expect(result.current.workspace.locked).toBe(false);
|
||||
});
|
||||
});
|
||||
|
|
@ -103,6 +103,25 @@ export const buildCreatedInitialResponse = ({
|
|||
conversationId: userMessage.conversationId ?? initialResponse.conversationId,
|
||||
});
|
||||
|
||||
/** Apply the resolved request choice only after acknowledgement. A saved decision may have
|
||||
* moved since this event was emitted, so replay must never replace it. */
|
||||
function acknowledgedCodeEnvironment(
|
||||
current: TConversation | null,
|
||||
submission: EventSubmission,
|
||||
): Pick<TConversation, 'codeEnvironmentMode' | 'codeWorkspaces'> {
|
||||
const { codeEnvironmentMode, codeWorkspaces } = submission;
|
||||
if (codeEnvironmentMode == null && !codeWorkspaces?.length) return {};
|
||||
const saved =
|
||||
current?.conversationId != null &&
|
||||
current.conversationId !== Constants.NEW_CONVO &&
|
||||
current.conversationId !== Constants.PENDING_CONVO;
|
||||
if (saved && (current.codeEnvironmentMode != null || current.codeWorkspaces?.length)) return {};
|
||||
return {
|
||||
codeEnvironmentMode,
|
||||
codeWorkspaces: codeEnvironmentMode === 'without_attached' ? undefined : codeWorkspaces,
|
||||
};
|
||||
}
|
||||
|
||||
export const isInitialNewConversationSubmission = ({
|
||||
userMessage,
|
||||
}: Pick<EventSubmission, 'userMessage'>): boolean =>
|
||||
|
|
@ -718,6 +737,7 @@ export default function useEventHandlers({
|
|||
});
|
||||
update = tConvoUpdateSchema.parse({
|
||||
...prevState,
|
||||
...acknowledgedCodeEnvironment(prevState, submission),
|
||||
conversationId,
|
||||
thread_id,
|
||||
title,
|
||||
|
|
@ -742,6 +762,7 @@ export default function useEventHandlers({
|
|||
setConversation((prevState) => {
|
||||
update = tConvoUpdateSchema.parse({
|
||||
...prevState,
|
||||
...acknowledgedCodeEnvironment(prevState, submission),
|
||||
conversationId,
|
||||
thread_id,
|
||||
messages: [requestMessage.messageId, responseMessage.messageId],
|
||||
|
|
@ -801,6 +822,7 @@ export default function useEventHandlers({
|
|||
});
|
||||
update = tConvoUpdateSchema.parse({
|
||||
...prevState,
|
||||
...acknowledgedCodeEnvironment(prevState, submission),
|
||||
conversationId,
|
||||
title,
|
||||
}) as TConversation;
|
||||
|
|
@ -823,6 +845,7 @@ export default function useEventHandlers({
|
|||
setConversation((prevState) => {
|
||||
update = tConvoUpdateSchema.parse({
|
||||
...prevState,
|
||||
...acknowledgedCodeEnvironment(prevState, submission),
|
||||
conversationId,
|
||||
}) as TConversation;
|
||||
return update;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue