forked from open-webui/open-webui
		
	refac: share chat model
This commit is contained in:
		
							parent
							
								
									4e9ca31146
								
							
						
					
					
						commit
						88c5b7f495
					
				
					 2 changed files with 74 additions and 65 deletions
				
			
		| 
						 | 
				
			
			@ -1,8 +1,6 @@
 | 
			
		|||
<script lang="ts">
 | 
			
		||||
	import { getContext } from 'svelte';
 | 
			
		||||
	import { toast } from 'svelte-sonner';
 | 
			
		||||
	import fileSaver from 'file-saver';
 | 
			
		||||
	const { saveAs } = fileSaver;
 | 
			
		||||
 | 
			
		||||
	import { Separator } from 'bits-ui';
 | 
			
		||||
	import { getChatById, shareChatById } from '$lib/apis/chats';
 | 
			
		||||
| 
						 | 
				
			
			@ -19,7 +17,6 @@
 | 
			
		|||
	import ChevronUpDown from '../icons/ChevronUpDown.svelte';
 | 
			
		||||
	import Menu from './Navbar/Menu.svelte';
 | 
			
		||||
	import TagChatModal from '../chat/TagChatModal.svelte';
 | 
			
		||||
	import { copyToClipboard } from '$lib/utils';
 | 
			
		||||
 | 
			
		||||
	const i18n = getContext('i18n');
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -37,66 +34,9 @@
 | 
			
		|||
 | 
			
		||||
	let showShareChatModal = false;
 | 
			
		||||
	let showTagChatModal = false;
 | 
			
		||||
 | 
			
		||||
	const shareChat = async () => {
 | 
			
		||||
		const chat = (await getChatById(localStorage.token, $chatId)).chat;
 | 
			
		||||
		console.log('share', chat);
 | 
			
		||||
 | 
			
		||||
		toast.success($i18n.t('Redirecting you to OpenWebUI Community'));
 | 
			
		||||
		const url = 'https://openwebui.com';
 | 
			
		||||
		// const url = 'http://localhost:5173';
 | 
			
		||||
 | 
			
		||||
		const tab = await window.open(`${url}/chats/upload`, '_blank');
 | 
			
		||||
		window.addEventListener(
 | 
			
		||||
			'message',
 | 
			
		||||
			(event) => {
 | 
			
		||||
				if (event.origin !== url) return;
 | 
			
		||||
				if (event.data === 'loaded') {
 | 
			
		||||
					tab.postMessage(
 | 
			
		||||
						JSON.stringify({
 | 
			
		||||
							chat: chat,
 | 
			
		||||
							modelfiles: $modelfiles.filter((modelfile) => chat.models.includes(modelfile.tagName))
 | 
			
		||||
						}),
 | 
			
		||||
						'*'
 | 
			
		||||
					);
 | 
			
		||||
				}
 | 
			
		||||
			},
 | 
			
		||||
			false
 | 
			
		||||
		);
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const shareLocalChat = async () => {
 | 
			
		||||
		const chat = await getChatById(localStorage.token, $chatId);
 | 
			
		||||
 | 
			
		||||
		let chatShareUrl = '';
 | 
			
		||||
		if (chat.share_id) {
 | 
			
		||||
			chatShareUrl = `${window.location.origin}/s/${chat.share_id}`;
 | 
			
		||||
		} else {
 | 
			
		||||
			const sharedChat = await shareChatById(localStorage.token, $chatId);
 | 
			
		||||
			chatShareUrl = `${window.location.origin}/s/${sharedChat.id}`;
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		toast.success($i18n.t('Copied shared conversation URL to clipboard!'));
 | 
			
		||||
		copyToClipboard(chatShareUrl);
 | 
			
		||||
	};
 | 
			
		||||
 | 
			
		||||
	const downloadChat = async () => {
 | 
			
		||||
		const chat = (await getChatById(localStorage.token, $chatId)).chat;
 | 
			
		||||
		console.log('download', chat);
 | 
			
		||||
 | 
			
		||||
		const chatText = chat.messages.reduce((a, message, i, arr) => {
 | 
			
		||||
			return `${a}### ${message.role.toUpperCase()}\n${message.content}\n\n`;
 | 
			
		||||
		}, '');
 | 
			
		||||
 | 
			
		||||
		let blob = new Blob([chatText], {
 | 
			
		||||
			type: 'text/plain'
 | 
			
		||||
		});
 | 
			
		||||
 | 
			
		||||
		saveAs(blob, `chat-${chat.title}.txt`);
 | 
			
		||||
	};
 | 
			
		||||
</script>
 | 
			
		||||
 | 
			
		||||
<ShareChatModal bind:show={showShareChatModal} {downloadChat} {shareChat} {shareLocalChat} />
 | 
			
		||||
<ShareChatModal bind:show={showShareChatModal} />
 | 
			
		||||
<!-- <TagChatModal bind:show={showTagChatModal} {tags} {deleteTag} {addTag} /> -->
 | 
			
		||||
<nav id="nav" class=" sticky py-2.5 top-0 flex flex-row justify-center z-30">
 | 
			
		||||
	<div
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue