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

@ -92,6 +92,10 @@ class ChatImportForm(ChatForm):
updated_at: Optional[int] = None updated_at: Optional[int] = None
class ChatsImportForm(BaseModel):
chats: list[ChatImportForm]
class ChatTitleMessagesForm(BaseModel): class ChatTitleMessagesForm(BaseModel):
title: str title: str
messages: list[dict] messages: list[dict]
@ -148,42 +152,44 @@ class ChatTable:
db.refresh(result) db.refresh(result)
return ChatModel.model_validate(result) if result else None return ChatModel.model_validate(result) if result else None
def import_chat( def _chat_import_form_to_chat_model(
self, user_id: str, form_data: ChatImportForm self, user_id: str, form_data: ChatImportForm
) -> Optional[ChatModel]: ) -> ChatModel:
with get_db() as db: id = str(uuid.uuid4())
id = str(uuid.uuid4()) chat = ChatModel(
chat = ChatModel( **{
**{ "id": id,
"id": id, "user_id": user_id,
"user_id": user_id, "title": (
"title": ( form_data.chat["title"] if "title" in form_data.chat else "New Chat"
form_data.chat["title"] ),
if "title" in form_data.chat "chat": form_data.chat,
else "New Chat" "meta": form_data.meta,
), "pinned": form_data.pinned,
"chat": form_data.chat, "folder_id": form_data.folder_id,
"meta": form_data.meta, "created_at": (
"pinned": form_data.pinned, form_data.created_at if form_data.created_at else int(time.time())
"folder_id": form_data.folder_id, ),
"created_at": ( "updated_at": (
form_data.created_at form_data.updated_at if form_data.updated_at else int(time.time())
if form_data.created_at ),
else int(time.time()) }
), )
"updated_at": ( return chat
form_data.updated_at
if form_data.updated_at
else int(time.time())
),
}
)
result = Chat(**chat.model_dump()) def import_chats(
db.add(result) self, user_id: str, chats: list[ChatImportForm]
) -> list[ChatModel]:
with get_db() as db:
chats = []
for form_data in chats:
chat = self._chat_import_form_to_chat_model(user_id, form_data)
chats.append(Chat(**chat.model_dump()))
db.add_all(chats)
db.commit() db.commit()
db.refresh(result) return [ChatModel.model_validate(chat) for chat in chats]
return ChatModel.model_validate(result) if result else None
def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]: def update_chat_by_id(self, id: str, chat: dict) -> Optional[ChatModel]:
try: try:

View file

@ -7,9 +7,11 @@ from open_webui.socket.main import get_event_emitter
from open_webui.models.chats import ( from open_webui.models.chats import (
ChatForm, ChatForm,
ChatImportForm, ChatImportForm,
ChatBulkImportForm,
ChatResponse, ChatResponse,
Chats, Chats,
ChatTitleIdResponse, ChatTitleIdResponse,
ChatsImportForm,
) )
from open_webui.models.tags import TagModel, Tags from open_webui.models.tags import TagModel, Tags
from open_webui.models.folders import Folders from open_webui.models.folders import Folders
@ -142,26 +144,15 @@ async def create_new_chat(form_data: ChatForm, user=Depends(get_verified_user)):
############################ ############################
# ImportChat # ImportChats
############################ ############################
@router.post("/import", response_model=Optional[ChatResponse]) @router.post("/import", response_model=Optional[ChatResponse])
async def import_chat(form_data: ChatImportForm, user=Depends(get_verified_user)): async def import_chats(form_data: ChatsImportForm, user=Depends(get_verified_user)):
try: try:
chat = Chats.import_chat(user.id, form_data) chats = Chats.import_chats(user.id, form_data.chats)
if chat: return [ChatResponse(**chat.model_dump()) for chat in chats]
tags = chat.meta.get("tags", [])
for tag_id in tags:
tag_id = tag_id.replace(" ", "_").lower()
tag_name = " ".join([word.capitalize() for word in tag_id.split("_")])
if (
tag_id != "none"
and Tags.get_tag_by_name_and_user_id(tag_name, user.id) is None
):
Tags.insert_new_tag(tag_name, user.id)
return ChatResponse(**chat.model_dump())
except Exception as e: except Exception as e:
log.exception(e) log.exception(e)
raise HTTPException( raise HTTPException(

View file

@ -65,15 +65,7 @@ export const unarchiveAllChats = async (token: string) => {
return res; return res;
}; };
export const importChat = async ( export const importChats = async (token: string, chats: object[]) => {
token: string,
chat: object,
meta: object | null,
pinned?: boolean,
folderId?: string | null,
createdAt: number | null = null,
updatedAt: number | null = null
) => {
let error = null; let error = null;
const res = await fetch(`${WEBUI_API_BASE_URL}/chats/import`, { const res = await fetch(`${WEBUI_API_BASE_URL}/chats/import`, {
@ -84,12 +76,7 @@ export const importChat = async (
authorization: `Bearer ${token}` authorization: `Bearer ${token}`
}, },
body: JSON.stringify({ body: JSON.stringify({
chat: chat, chats
meta: meta ?? {},
pinned: pinned,
folder_id: folderId,
created_at: createdAt ?? null,
updated_at: updatedAt ?? null
}) })
}) })
.then(async (res) => { .then(async (res) => {

View file

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

View file

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

View file

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