enh/refac: debounce chat draft save

This commit is contained in:
Timothy Jaeryang Baek 2025-07-31 16:02:43 +04:00
parent 05895d9657
commit 73b07df28f
2 changed files with 28 additions and 18 deletions

View File

@ -2038,6 +2038,25 @@
}
}
};
const MAX_DRAFT_LENGTH = 5000;
let saveDraftTimeout = null;
const saveDraft = async (draft, chatId = null) => {
if (saveDraftTimeout) {
clearTimeout(saveDraftTimeout);
}
if (draft.prompt !== null && draft.prompt.length < MAX_DRAFT_LENGTH) {
saveDraftTimeout = setTimeout(async () => {
await sessionStorage.setItem(
`chat-input${chatId ? `-${chatId}` : ''}`,
JSON.stringify(draft)
);
}, 500);
} else {
sessionStorage.removeItem(`chat-input${chatId ? `-${chatId}` : ''}`);
}
};
</script>
<svelte:head>
@ -2173,16 +2192,9 @@
false}
{stopResponse}
{createMessagePair}
onChange={(input) => {
onChange={(data) => {
if (!$temporaryChatEnabled) {
if (input.prompt !== null) {
sessionStorage.setItem(
`chat-input${$chatId ? `-${$chatId}` : ''}`,
JSON.stringify(input)
);
} else {
sessionStorage.removeItem(`chat-input${$chatId ? `-${$chatId}` : ''}`);
}
saveDraft(data, $chatId);
}
}}
on:upload={async (e) => {
@ -2237,6 +2249,11 @@
{stopResponse}
{createMessagePair}
{onSelect}
onChange={(data) => {
if (!$temporaryChatEnabled) {
saveDraft(data);
}
}}
on:upload={async (e) => {
const { type, data } = e.detail;

View File

@ -55,6 +55,7 @@
export let webSearchEnabled = false;
export let onSelect = (e) => {};
export let onChange = (e) => {};
export let toolServers = [];
@ -224,15 +225,7 @@
{stopResponse}
{createMessagePair}
placeholder={$i18n.t('How can I help you today?')}
onChange={(input) => {
if (!$temporaryChatEnabled) {
if (input.prompt !== null) {
sessionStorage.setItem(`chat-input`, JSON.stringify(input));
} else {
sessionStorage.removeItem(`chat-input`);
}
}
}}
{onChange}
on:upload={(e) => {
dispatch('upload', e.detail);
}}