From effcbd6301f0d58654aa49f42f9f99e707df79be Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Sat, 26 Oct 2024 21:25:48 -0700 Subject: [PATCH] feat: airdrop chats between windows --- src/lib/components/admin/Evaluations.svelte | 38 +++++++++---------- src/lib/components/layout/Sidebar.svelte | 38 +++++++++---------- .../components/layout/Sidebar/ChatItem.svelte | 20 +++++++++- .../layout/Sidebar/RecursiveFolder.svelte | 18 +++++++-- 4 files changed, 70 insertions(+), 44 deletions(-) diff --git a/src/lib/components/admin/Evaluations.svelte b/src/lib/components/admin/Evaluations.svelte index 4632238f0..088b157da 100644 --- a/src/lib/components/admin/Evaluations.svelte +++ b/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; diff --git a/src/lib/components/layout/Sidebar.svelte b/src/lib/components/layout/Sidebar.svelte index 8b133bcae..d3502b056 100644 --- a/src/lib/components/layout/Sidebar.svelte +++ b/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) { diff --git a/src/lib/components/layout/Sidebar/ChatItem.svelte b/src/lib/components/layout/Sidebar/ChatItem.svelte index 2ca8e6409..110233bf8 100644 --- a/src/lib/components/layout/Sidebar/ChatItem.svelte +++ b/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 @@ {/if} -
+
{#if confirmEdit}