Skip to content

Commit 6e9e25e

Browse files
authored
Persist chat config (#2367)
Closes #2224
1 parent 491765f commit 6e9e25e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

website/src/components/Chat/ChatSection.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { FormProvider, useForm } from "react-hook-form";
2+
import { getCachedChatForm, useCacheChatForm } from "src/hooks/chat/useCacheConfig";
23
import { ChatConfigForm } from "src/types/Chat";
34

45
import { ChatConfigSummary } from "./ChatConfigSummary";
@@ -10,12 +11,16 @@ export const ChatSection = ({ chatId }: { chatId: string }) => {
1011

1112
console.assert(modelInfos.length > 0, "No model config was found");
1213
const form = useForm<ChatConfigForm>({
13-
defaultValues: {
14+
//NOTE: we should at some point validate the cache, in case models were added or deleted
15+
// or certain parameters disabled
16+
defaultValues: getCachedChatForm() ?? {
1417
...modelInfos[0].parameter_configs[0].sampling_parameters,
1518
model_config_name: modelInfos[0].name,
1619
},
1720
});
1821

22+
useCacheChatForm(form);
23+
1924
return (
2025
<FormProvider {...form}>
2126
<ChatConversation chatId={chatId} />
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { useEffect } from "react";
2+
import { UseFormReturn } from "react-hook-form";
3+
import { ChatConfigForm } from "src/types/Chat";
4+
5+
const CHAT_CONFIG_KEY = "CHAT_CONFIG";
6+
7+
export const getCachedChatForm = (): ChatConfigForm | null => {
8+
if (typeof localStorage !== "undefined") {
9+
const oldConfig = localStorage.getItem(CHAT_CONFIG_KEY);
10+
if (oldConfig) {
11+
return JSON.parse(oldConfig);
12+
}
13+
}
14+
return null;
15+
};
16+
17+
export const useCacheChatForm = (form: UseFormReturn<ChatConfigForm>) => {
18+
const config = form.watch();
19+
20+
useEffect(() => {
21+
if (typeof localStorage !== "undefined") {
22+
localStorage.setItem(CHAT_CONFIG_KEY, JSON.stringify(config));
23+
}
24+
}, [config]);
25+
};

0 commit comments

Comments
 (0)