feat: airdrop chats between windows

This commit is contained in:
Timothy J. Baek 2024-10-26 21:25:48 -07:00
parent e6ca994c92
commit effcbd6301
4 changed files with 70 additions and 44 deletions

View File

@ -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 getEmbeddings = async (text: string) => {
const tokens = await tokenizer(text); const tokens = await tokenizer(text);
const output = await model(tokens); 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 () => { onMount(async () => {
feedbacks = await getAllFeedbacks(localStorage.token); feedbacks = await getAllFeedbacks(localStorage.token);
loaded = true; loaded = true;

View File

@ -571,10 +571,15 @@
importChatHandler(e.detail, true); importChatHandler(e.detail, true);
}} }}
on:drop={async (e) => { on:drop={async (e) => {
const { type, id } = e.detail; const { type, id, item } = e.detail;
if (type === 'chat') { 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) { if (chat) {
console.log(chat); console.log(chat);
@ -587,21 +592,15 @@
toast.error(error); toast.error(error);
return null; return null;
}); });
if (res) {
initChatList();
}
} }
if (!chat.pinned) { if (!chat.pinned) {
const res = await toggleChatPinnedStatusById(localStorage.token, id); const res = await toggleChatPinnedStatusById(localStorage.token, id);
}
if (res) {
initChatList(); initChatList();
} }
} }
}
}
}} }}
name={$i18n.t('Pinned')} name={$i18n.t('Pinned')}
> >
@ -660,10 +659,15 @@
importChatHandler(e.detail); importChatHandler(e.detail);
}} }}
on:drop={async (e) => { on:drop={async (e) => {
const { type, id } = e.detail; const { type, id, item } = e.detail;
if (type === 'chat') { 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) { if (chat) {
console.log(chat); console.log(chat);
@ -674,20 +678,14 @@
return null; return null;
} }
); );
if (res) {
initChatList();
}
} }
if (chat.pinned) { if (chat.pinned) {
const res = await toggleChatPinnedStatusById(localStorage.token, id); const res = await toggleChatPinnedStatusById(localStorage.token, id);
}
if (res) {
initChatList(); initChatList();
} }
}
}
} else if (type === 'folder') { } else if (type === 'folder') {
if (folders[id].parent_id === null) { if (folders[id].parent_id === null) {
return; return;

View File

@ -11,6 +11,7 @@
cloneChatById, cloneChatById,
deleteChatById, deleteChatById,
getAllTags, getAllTags,
getChatById,
getChatList, getChatList,
getChatListByTagName, getChatListByTagName,
getPinnedChatList, getPinnedChatList,
@ -46,7 +47,21 @@
export let selected = false; export let selected = false;
export let shiftKey = false; export let shiftKey = false;
let chat = null;
let mouseOver = false; 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 showShareChatModal = false;
let confirmEdit = false; let confirmEdit = false;
@ -133,7 +148,8 @@
'text/plain', 'text/plain',
JSON.stringify({ JSON.stringify({
type: 'chat', type: 'chat',
id: id id: id,
item: chat
}) })
); );
@ -204,7 +220,7 @@
</DragGhost> </DragGhost>
{/if} {/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} {#if confirmEdit}
<div <div
class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId || class=" w-full flex justify-between rounded-lg px-[11px] py-[6px] {id === $chatId ||

View File

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