refac/pref: chat import optimization

Co-Authored-By: G30 <50341825+silentoplayz@users.noreply.github.com>
This commit is contained in:
Timothy Jaeryang Baek
2025-11-21 03:49:49 -05:00
parent 3ef7367f01
commit a51579a84b
6 changed files with 116 additions and 119 deletions

View File

@@ -16,8 +16,8 @@
deleteAllChats,
getAllChats,
getChatList,
importChat,
getPinnedChatList
getPinnedChatList,
importChats
} from '$lib/apis/chats';
import { getImportOrigin, convertOpenAIChats } from '$lib/utils';
import { onMount, getContext } from 'svelte';
@@ -52,7 +52,7 @@
console.log('Unable to import chats:', error);
}
}
importChats(chats);
importChatsHandler(chats);
};
if (importFiles.length > 0) {
@@ -60,24 +60,33 @@
}
}
const importChats = async (_chats) => {
for (const chat of _chats) {
console.log(chat);
const importChatsHandler = async (_chats) => {
const chats = _chats.map((chat) => {
if (chat.chat) {
await importChat(
localStorage.token,
chat.chat,
chat.meta ?? {},
false,
null,
chat?.created_at ?? null,
chat?.updated_at ?? null
);
return {
chat: chat.chat,
meta: chat.meta ?? {},
pinned: false,
folder_id: chat?.folder_id ?? null,
created_at: chat?.created_at ?? null,
updated_at: chat?.updated_at ?? null
};
} else {
// Legacy format
await importChat(localStorage.token, chat, {}, false, null);
return {
chat: chat,
meta: {},
pinned: false,
folder_id: null,
created_at: chat?.created_at ?? null,
updated_at: chat?.updated_at ?? null
};
}
});
const res = await importChats(localStorage.token, chats);
if (res) {
toast.success(`Successfully imported ${res.length} chats.`);
}
currentChatPage.set(1);

View File

@@ -38,7 +38,7 @@
toggleChatPinnedStatusById,
getChatById,
updateChatFolderIdById,
importChat
importChats
} from '$lib/apis/chats';
import { createNewFolder, getFolders, updateFolderParentIdById } from '$lib/apis/folders';
import { WEBUI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
@@ -227,15 +227,16 @@
for (const item of items) {
console.log(item);
if (item.chat) {
await importChat(
localStorage.token,
item.chat,
item?.meta ?? {},
pinned,
folderId,
item?.created_at ?? null,
item?.updated_at ?? null
);
await importChats(localStorage.token, [
{
chat: item.chat,
meta: item?.meta ?? {},
pinned: pinned,
folder_id: folderId,
created_at: item?.created_at ?? null,
updated_at: item?.updated_at ?? null
}
]);
}
}
@@ -999,15 +1000,16 @@
return null;
});
if (!chat && item) {
chat = await importChat(
localStorage.token,
item.chat,
item?.meta ?? {},
false,
null,
item?.created_at ?? null,
item?.updated_at ?? null
);
chat = await importChats(localStorage.token, [
{
chat: item.chat,
meta: item?.meta ?? {},
pinned: false,
folder_id: null,
created_at: item?.created_at ?? null,
updated_at: item?.updated_at ?? null
}
]);
}
if (chat) {
@@ -1064,15 +1066,16 @@
return null;
});
if (!chat && item) {
chat = await importChat(
localStorage.token,
item.chat,
item?.meta ?? {},
false,
null,
item?.created_at ?? null,
item?.updated_at ?? null
);
chat = await importChats(localStorage.token, [
{
chat: item.chat,
meta: item?.meta ?? {},
pinned: false,
folder_id: null,
created_at: item?.created_at ?? null,
updated_at: item?.updated_at ?? null
}
]);
}
if (chat) {

View File

@@ -24,8 +24,8 @@
getChatById,
getChatsByFolderId,
getChatListByFolderId,
importChat,
updateChatFolderIdById
updateChatFolderIdById,
importChats
} from '$lib/apis/chats';
import ChevronDown from '../../icons/ChevronDown.svelte';
@@ -152,15 +152,16 @@
return null;
});
if (!chat && item) {
chat = await importChat(
localStorage.token,
item.chat,
item?.meta ?? {},
false,
null,
item?.created_at ?? null,
item?.updated_at ?? null
).catch((error) => {
chat = await importChats(localStorage.token, [
{
chat: item.chat,
meta: item?.meta ?? {},
pinned: false,
folder_id: null,
created_at: item?.created_at ?? null,
updated_at: item?.updated_at ?? null
}
]).catch((error) => {
toast.error(`${error}`);
return null;
});