mirror of
https://github.com/open-webui/open-webui
synced 2025-05-17 20:05:08 +00:00
add functionality to clone shared chats
This commit is contained in:
parent
87ba39df57
commit
2444327a47
@ -469,6 +469,8 @@ class ChatTable:
|
|||||||
def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]:
|
def get_chat_by_share_id(self, id: str) -> Optional[ChatModel]:
|
||||||
try:
|
try:
|
||||||
with get_db() as db:
|
with get_db() as db:
|
||||||
|
# it is possible that the shared link was deleted. hence,
|
||||||
|
# we check if the chat is still shared by checkng if a chat with the share_id exists
|
||||||
chat = db.query(Chat).filter_by(share_id=id).first()
|
chat = db.query(Chat).filter_by(share_id=id).first()
|
||||||
|
|
||||||
if chat:
|
if chat:
|
||||||
|
@ -463,6 +463,31 @@ async def clone_chat_by_id(id: str, user=Depends(get_verified_user)):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
############################
|
||||||
|
# CloneChatByShareId
|
||||||
|
############################
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/{share_id}/clone_shared", response_model=Optional[ChatResponse])
|
||||||
|
async def clone_chat_by_share_id(share_id: str, user=Depends(get_verified_user)):
|
||||||
|
chat = Chats.get_chat_by_share_id(share_id)
|
||||||
|
if chat:
|
||||||
|
updated_chat = {
|
||||||
|
**chat.chat,
|
||||||
|
"originalChatId": chat.id,
|
||||||
|
"branchPointMessageId": chat.chat["history"]["currentId"],
|
||||||
|
"title": f"Clone of {chat.title}",
|
||||||
|
}
|
||||||
|
|
||||||
|
chat = Chats.insert_new_chat(user.id, ChatForm(**{"chat": updated_chat}))
|
||||||
|
return ChatResponse(**chat.model_dump())
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.DEFAULT()
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
############################
|
############################
|
||||||
# ArchiveChat
|
# ArchiveChat
|
||||||
############################
|
############################
|
||||||
|
@ -618,6 +618,44 @@ export const cloneChatById = async (token: string, id: string) => {
|
|||||||
return res;
|
return res;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const cloneChatByShareId = async (token: string, share_id: string) => {
|
||||||
|
let error = null;
|
||||||
|
|
||||||
|
const res = await fetch(`${WEBUI_API_BASE_URL}/chats/${share_id}/clone_shared`, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
Accept: 'application/json',
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
...(token && { authorization: `Bearer ${token}` })
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.then(async (res) => {
|
||||||
|
if (!res.ok) throw await res.json();
|
||||||
|
return res.json();
|
||||||
|
})
|
||||||
|
.then((json) => {
|
||||||
|
return json;
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
error = err;
|
||||||
|
|
||||||
|
if ('detail' in err) {
|
||||||
|
error = err.detail;
|
||||||
|
} else {
|
||||||
|
error = err;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(err);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
export const shareChatById = async (token: string, id: string) => {
|
export const shareChatById = async (token: string, id: string) => {
|
||||||
let error = null;
|
let error = null;
|
||||||
|
|
||||||
|
@ -8,13 +8,14 @@
|
|||||||
import { settings, chatId, WEBUI_NAME, models } from '$lib/stores';
|
import { settings, chatId, WEBUI_NAME, models } from '$lib/stores';
|
||||||
import { convertMessagesToHistory, createMessagesList } from '$lib/utils';
|
import { convertMessagesToHistory, createMessagesList } from '$lib/utils';
|
||||||
|
|
||||||
import { getChatByShareId } from '$lib/apis/chats';
|
import { getChatByShareId, cloneChatByShareId } from '$lib/apis/chats';
|
||||||
|
|
||||||
import Messages from '$lib/components/chat/Messages.svelte';
|
import Messages from '$lib/components/chat/Messages.svelte';
|
||||||
import Navbar from '$lib/components/layout/Navbar.svelte';
|
import Navbar from '$lib/components/layout/Navbar.svelte';
|
||||||
import { getUserById } from '$lib/apis/users';
|
import { getUserById } from '$lib/apis/users';
|
||||||
import { error } from '@sveltejs/kit';
|
import { error } from '@sveltejs/kit';
|
||||||
import { getModels } from '$lib/apis';
|
import { getModels } from '$lib/apis';
|
||||||
|
import { toast } from 'svelte-sonner';
|
||||||
|
|
||||||
const i18n = getContext('i18n');
|
const i18n = getContext('i18n');
|
||||||
|
|
||||||
@ -100,6 +101,19 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const cloneSharedChat = async () => {
|
||||||
|
if (!chat) return;
|
||||||
|
|
||||||
|
const res = await cloneChatByShareId(localStorage.token, chat.id).catch((error) => {
|
||||||
|
toast.error(error);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
|
||||||
|
if (res) {
|
||||||
|
goto(`/c/${res.id}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<svelte:head>
|
<svelte:head>
|
||||||
@ -121,8 +135,16 @@
|
|||||||
{title}
|
{title}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class=" mt-1 text-gray-400">
|
<div class="flex justify-between items-center mt-1">
|
||||||
{dayjs(chat.chat.timestamp).format($i18n.t('MMMM DD, YYYY'))}
|
<div class="text-gray-400">
|
||||||
|
{dayjs(chat.chat.timestamp).format($i18n.t('MMMM DD, YYYY'))}
|
||||||
|
</div>
|
||||||
|
<button
|
||||||
|
class="px-4 py-2 text-sm font-medium bg-emerald-600 hover:bg-emerald-500 text-white rounded-xl transition"
|
||||||
|
on:click={cloneSharedChat}
|
||||||
|
>
|
||||||
|
{$i18n.t('Clone in OpenWebUI')}
|
||||||
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user