mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-08-04 14:57:42 +00:00
fix: Restore drafts on active agent changes
This commit is contained in:
parent
897cc50187
commit
0156310b26
2 changed files with 46 additions and 9 deletions
|
|
@ -229,6 +229,11 @@ const isPersistableDraftField = (name?: string): boolean => {
|
|||
);
|
||||
};
|
||||
|
||||
const getDraftFormValues = (draftValues?: Partial<AgentForm>): AgentForm => ({
|
||||
...getDefaultAgentFormValues(),
|
||||
...draftValues,
|
||||
});
|
||||
|
||||
export default function AgentPanel() {
|
||||
const localize = useLocalize();
|
||||
const { user } = useAuthContext();
|
||||
|
|
@ -262,13 +267,7 @@ export default function AgentPanel() {
|
|||
|
||||
const models = useMemo(() => modelsQuery.data ?? {}, [modelsQuery.data]);
|
||||
const draftValues = useMemo(() => getAgentDraft(current_agent_id), [current_agent_id]);
|
||||
const defaultValues = useMemo(
|
||||
() => ({
|
||||
...getDefaultAgentFormValues(),
|
||||
...draftValues,
|
||||
}),
|
||||
[draftValues],
|
||||
);
|
||||
const defaultValues = useMemo(() => getDraftFormValues(draftValues), [draftValues]);
|
||||
const methods = useForm<AgentForm>({
|
||||
defaultValues,
|
||||
mode: 'onChange',
|
||||
|
|
@ -338,10 +337,14 @@ export default function AgentPanel() {
|
|||
}
|
||||
|
||||
currentAgentIdRef.current = current_agent_id;
|
||||
const nextHasDraft = getAgentDraft(current_agent_id) != null;
|
||||
const nextDraft = getAgentDraft(current_agent_id);
|
||||
const nextHasDraft = nextDraft != null;
|
||||
shouldPersistDraftRef.current = nextHasDraft;
|
||||
setHasDraft(nextHasDraft);
|
||||
}, [current_agent_id, getValues]);
|
||||
if (nextDraft) {
|
||||
reset(getDraftFormValues(nextDraft));
|
||||
}
|
||||
}, [current_agent_id, getValues, reset]);
|
||||
|
||||
useEffect(() => {
|
||||
if (hasDraft) {
|
||||
|
|
|
|||
|
|
@ -313,4 +313,38 @@ describe('AgentPanel draft preservation', () => {
|
|||
expect(getAgentDraft('agent-123')).toBeUndefined();
|
||||
expect(mockLastAgentSelectHasDraft).toBe(false);
|
||||
});
|
||||
|
||||
it('restores a draft when the current agent changes while mounted', async () => {
|
||||
mockCurrentAgentId = 'agent-123';
|
||||
const { rerender } = render(<Harness />);
|
||||
|
||||
fireEvent.change(screen.getByLabelText('Draft name'), {
|
||||
target: { value: 'Previous agent draft' },
|
||||
});
|
||||
saveAgentDraft('agent-456', {
|
||||
id: 'agent-456',
|
||||
name: 'Mounted switch draft',
|
||||
description: '',
|
||||
instructions: 'Draft for the switched agent',
|
||||
model: 'gpt-4o',
|
||||
model_parameters: {},
|
||||
provider: { label: 'OpenAI', value: 'openAI' },
|
||||
tools: [],
|
||||
tool_options: {},
|
||||
category: 'general',
|
||||
execute_code: false,
|
||||
file_search: false,
|
||||
web_search: false,
|
||||
} as AgentForm);
|
||||
|
||||
mockCurrentAgentId = 'agent-456';
|
||||
rerender(<Harness />);
|
||||
|
||||
await waitFor(() => {
|
||||
expect(screen.getByLabelText('Draft name')).toHaveValue('Mounted switch draft');
|
||||
});
|
||||
expect(screen.getByLabelText('Draft instructions')).toHaveValue('Draft for the switched agent');
|
||||
expect(getAgentDraft('agent-123')?.name).toBe('Previous agent draft');
|
||||
expect(mockLastAgentSelectHasDraft).toBe(true);
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue