소스 검색

feat: airdrop chats between windows

Timothy J. Baek 6 달 전
부모
커밋
effcbd6301

+ 19 - 19
src/lib/components/admin/Evaluations.svelte

@@ -216,6 +216,25 @@
 	//
 	//////////////////////
 
+	const loadEmbeddingModel = async () => {
+		// Check if the tokenizer and model are already loaded and stored in the window object
+		if (!window.tokenizer) {
+			window.tokenizer = await AutoTokenizer.from_pretrained(EMBEDDING_MODEL);
+		}
+
+		if (!window.model) {
+			window.model = await AutoModel.from_pretrained(EMBEDDING_MODEL);
+		}
+
+		// Use the tokenizer and model from the window object
+		tokenizer = window.tokenizer;
+		model = window.model;
+
+		// Pre-compute embeddings for all unique tags
+		const allTags = new Set(feedbacks.flatMap((feedback) => feedback.data.tags || []));
+		await getTagEmbeddings(Array.from(allTags));
+	};
+
 	const getEmbeddings = async (text: string) => {
 		const tokens = await tokenizer(text);
 		const output = await model(tokens);
@@ -320,25 +339,6 @@
 		}
 	};
 
-	const loadEmbeddingModel = async () => {
-		// Check if the tokenizer and model are already loaded and stored in the window object
-		if (!window.tokenizer) {
-			window.tokenizer = await AutoTokenizer.from_pretrained(EMBEDDING_MODEL);
-		}
-
-		if (!window.model) {
-			window.model = await AutoModel.from_pretrained(EMBEDDING_MODEL);
-		}
-
-		// Use the tokenizer and model from the window object
-		tokenizer = window.tokenizer;
-		model = window.model;
-
-		// Pre-compute embeddings for all unique tags
-		const allTags = new Set(feedbacks.flatMap((feedback) => feedback.data.tags || []));
-		await getTagEmbeddings(Array.from(allTags));
-	};
-
 	onMount(async () => {
 		feedbacks = await getAllFeedbacks(localStorage.token);
 		loaded = true;

+ 18 - 20
src/lib/components/layout/Sidebar.svelte

@@ -571,10 +571,15 @@
 							importChatHandler(e.detail, true);
 						}}
 						on:drop={async (e) => {
-							const { type, id } = e.detail;
+							const { type, id, item } = e.detail;
 
 							if (type === 'chat') {
-								const chat = await getChatById(localStorage.token, id);
+								let chat = await getChatById(localStorage.token, id).catch((error) => {
+									return null;
+								});
+								if (!chat && item) {
+									chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
+								}
 
 								if (chat) {
 									console.log(chat);
@@ -587,19 +592,13 @@
 											toast.error(error);
 											return null;
 										});
-
-										if (res) {
-											initChatList();
-										}
 									}
 
 									if (!chat.pinned) {
 										const res = await toggleChatPinnedStatusById(localStorage.token, id);
-
-										if (res) {
-											initChatList();
-										}
 									}
+
+									initChatList();
 								}
 							}
 						}}
@@ -660,10 +659,15 @@
 						importChatHandler(e.detail);
 					}}
 					on:drop={async (e) => {
-						const { type, id } = e.detail;
+						const { type, id, item } = e.detail;
 
 						if (type === 'chat') {
-							const chat = await getChatById(localStorage.token, id);
+							let chat = await getChatById(localStorage.token, id).catch((error) => {
+								return null;
+							});
+							if (!chat && item) {
+								chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
+							}
 
 							if (chat) {
 								console.log(chat);
@@ -674,19 +678,13 @@
 											return null;
 										}
 									);
-
-									if (res) {
-										initChatList();
-									}
 								}
 
 								if (chat.pinned) {
 									const res = await toggleChatPinnedStatusById(localStorage.token, id);
-
-									if (res) {
-										initChatList();
-									}
 								}
+
+								initChatList();
 							}
 						} else if (type === 'folder') {
 							if (folders[id].parent_id === null) {

+ 18 - 2
src/lib/components/layout/Sidebar/ChatItem.svelte

@@ -11,6 +11,7 @@
 		cloneChatById,
 		deleteChatById,
 		getAllTags,
+		getChatById,
 		getChatList,
 		getChatListByTagName,
 		getPinnedChatList,
@@ -46,7 +47,21 @@
 	export let selected = false;
 	export let shiftKey = false;
 
+	let chat = null;
+
 	let mouseOver = false;
+	let draggable = false;
+	$: if (mouseOver) {
+		loadChat();
+	}
+
+	const loadChat = async () => {
+		if (!chat) {
+			draggable = false;
+			chat = await getChatById(localStorage.token, id);
+			draggable = true;
+		}
+	};
 
 	let showShareChatModal = false;
 	let confirmEdit = false;
@@ -133,7 +148,8 @@
 			'text/plain',
 			JSON.stringify({
 				type: 'chat',
-				id: id
+				id: id,
+				item: chat
 			})
 		);
 
@@ -204,7 +220,7 @@
 	</DragGhost>
 {/if}
 
-<div bind:this={itemElement} class=" w-full {className} relative group" draggable="true">
+<div bind:this={itemElement} class=" w-full {className} relative group" {draggable}>
 	{#if confirmEdit}
 		<div
 			class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||

+ 15 - 3
src/lib/components/layout/Sidebar/RecursiveFolder.svelte

@@ -22,7 +22,12 @@
 		updateFolderParentIdById
 	} from '$lib/apis/folders';
 	import { toast } from 'svelte-sonner';
-	import { getChatsByFolderId, updateChatFolderIdById } from '$lib/apis/chats';
+	import {
+		getChatById,
+		getChatsByFolderId,
+		importChat,
+		updateChatFolderIdById
+	} from '$lib/apis/chats';
 	import ChatItem from './ChatItem.svelte';
 	import FolderMenu from './Folders/FolderMenu.svelte';
 	import DeleteConfirmDialog from '$lib/components/common/ConfirmDialog.svelte';
@@ -101,7 +106,7 @@
 						const data = JSON.parse(dataTransfer);
 						console.log(data);
 
-						const { type, id } = data;
+						const { type, id, item } = data;
 
 						if (type === 'folder') {
 							open = true;
@@ -122,8 +127,15 @@
 						} else if (type === 'chat') {
 							open = true;
 
+							let chat = await getChatById(localStorage.token, id).catch((error) => {
+								return null;
+							});
+							if (!chat && item) {
+								chat = await importChat(localStorage.token, item.chat, item?.meta ?? {});
+							}
+
 							// Move the chat
-							const res = await updateChatFolderIdById(localStorage.token, id, folderId).catch(
+							const res = await updateChatFolderIdById(localStorage.token, chat.id, folderId).catch(
 								(error) => {
 									toast.error(error);
 									return null;