open-webui/src/lib/components/chat/Messages/Message.svelte

167 lines
4.3 KiB
Svelte
Raw Normal View History

2024-09-22 22:55:13 +00:00
<script lang="ts">
import { toast } from 'svelte-sonner';
2024-09-23 21:20:27 +00:00
import { tick, getContext, onMount, createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();
2024-09-22 22:55:13 +00:00
const i18n = getContext('i18n');
import { settings } from '$lib/stores';
import { copyToClipboard } from '$lib/utils';
import MultiResponseMessages from './MultiResponseMessages.svelte';
import ResponseMessage from './ResponseMessage.svelte';
import UserMessage from './UserMessage.svelte';
2024-09-22 23:36:46 +00:00
import { updateChatById } from '$lib/apis/chats';
2024-09-22 22:55:13 +00:00
export let chatId;
2024-09-23 12:24:50 +00:00
export let idx = 0;
2024-09-22 22:55:13 +00:00
export let history;
export let messageId;
export let user;
2024-09-23 21:20:27 +00:00
export let updateChatHistory;
2024-09-22 22:55:13 +00:00
export let chatActionHandler;
export let showPreviousMessage;
export let showNextMessage;
2024-09-22 23:36:46 +00:00
export let editMessage;
2024-09-22 22:55:13 +00:00
export let deleteMessage;
2024-09-22 23:36:46 +00:00
export let rateMessage;
2024-09-22 22:55:13 +00:00
export let regenerateResponse;
2024-09-22 23:36:46 +00:00
export let continueResponse;
2024-09-22 22:55:13 +00:00
// MultiResponseMessages
export let mergeResponses;
2024-09-23 21:20:27 +00:00
export let autoScroll = false;
2024-09-22 22:55:13 +00:00
export let readOnly = false;
2024-09-23 12:24:50 +00:00
onMount(() => {
console.log('message', idx);
});
2024-09-22 22:55:13 +00:00
</script>
2024-09-22 23:36:46 +00:00
<div
class="flex flex-col justify-between px-5 mb-3 w-full {($settings?.widescreenMode ?? null)
? 'max-w-full'
: 'max-w-5xl'} mx-auto rounded-lg group"
>
2024-09-23 21:20:27 +00:00
{#if history.messages[messageId]}
{#if history.messages[messageId].role === 'user'}
2024-09-22 22:55:13 +00:00
<UserMessage
{user}
2024-09-23 21:20:27 +00:00
{history}
{messageId}
2024-09-23 12:24:50 +00:00
isFirstMessage={idx === 0}
2024-09-23 21:20:27 +00:00
siblings={history.messages[messageId].parentId !== null
? (history.messages[history.messages[messageId].parentId]?.childrenIds ?? [])
2024-09-22 22:55:13 +00:00
: (Object.values(history.messages)
.filter((message) => message.parentId === null)
.map((message) => message.id) ?? [])}
{showPreviousMessage}
{showNextMessage}
2024-09-22 23:36:46 +00:00
{editMessage}
2024-09-23 21:20:27 +00:00
on:delete={() => deleteMessage(messageId)}
2024-09-22 22:55:13 +00:00
{readOnly}
/>
2024-09-23 21:20:27 +00:00
{:else if (history.messages[history.messages[messageId].parentId]?.models?.length ?? 1) === 1}
2024-09-22 22:55:13 +00:00
<ResponseMessage
2024-09-23 21:20:27 +00:00
{history}
{messageId}
2024-09-22 23:36:46 +00:00
isLastMessage={messageId === history.currentId}
2024-09-23 21:20:27 +00:00
siblings={history.messages[history.messages[messageId].parentId]?.childrenIds ?? []}
2024-09-22 22:55:13 +00:00
{showPreviousMessage}
{showNextMessage}
2024-09-22 23:36:46 +00:00
{editMessage}
2024-09-22 22:55:13 +00:00
{rateMessage}
2024-09-22 23:36:46 +00:00
{continueResponse}
2024-09-22 22:55:13 +00:00
{regenerateResponse}
on:action={async (e) => {
console.log('action', e);
2024-09-23 21:20:27 +00:00
const message = history.messages[messageId];
2024-09-22 22:55:13 +00:00
if (typeof e.detail === 'string') {
await chatActionHandler(chatId, e.detail, message.model, message.id);
} else {
const { id, event } = e.detail;
await chatActionHandler(chatId, id, message.model, message.id, event);
}
}}
2024-09-22 23:36:46 +00:00
on:update={async (e) => {
console.log('update', e);
2024-09-23 21:20:27 +00:00
updateChatHistory();
2024-09-22 23:36:46 +00:00
}}
2024-09-22 22:55:13 +00:00
on:save={async (e) => {
console.log('save', e);
const message = e.detail;
if (message) {
history.messages[message.id] = message;
await updateChatById(localStorage.token, chatId, {
history: history
});
} else {
await updateChatById(localStorage.token, chatId, {
history: history
});
}
}}
2024-09-22 23:36:46 +00:00
{readOnly}
2024-09-22 22:55:13 +00:00
/>
{:else}
<MultiResponseMessages
bind:history
{chatId}
2024-09-23 21:20:27 +00:00
{messageId}
isLastMessage={messageId === history?.currentId}
2024-09-22 22:55:13 +00:00
{rateMessage}
2024-09-23 21:20:27 +00:00
{editMessage}
2024-09-22 23:36:46 +00:00
{continueResponse}
2024-09-22 22:55:13 +00:00
{regenerateResponse}
2024-09-23 21:20:27 +00:00
{mergeResponses}
2024-09-22 22:55:13 +00:00
on:action={async (e) => {
console.log('action', e);
2024-09-23 21:20:27 +00:00
const message = history.messages[messageId];
2024-09-22 22:55:13 +00:00
if (typeof e.detail === 'string') {
await chatActionHandler(chatId, e.detail, message.model, message.id);
} else {
const { id, event } = e.detail;
await chatActionHandler(chatId, id, message.model, message.id, event);
}
}}
2024-09-23 21:20:27 +00:00
on:update={async (e) => {
console.log('update', e);
updateChatHistory();
}}
on:save={async (e) => {
console.log('save', e);
const message = e.detail;
if (message) {
history.messages[message.id] = message;
await updateChatById(localStorage.token, chatId, {
history: history
});
} else {
await updateChatById(localStorage.token, chatId, {
history: history
});
}
}}
2024-09-22 22:55:13 +00:00
on:change={async () => {
2024-09-23 21:20:27 +00:00
await tick();
2024-09-22 22:55:13 +00:00
await updateChatById(localStorage.token, chatId, {
history: history
});
2024-09-23 21:20:27 +00:00
dispatch('scroll');
2024-09-22 22:55:13 +00:00
}}
2024-09-23 21:20:27 +00:00
{readOnly}
2024-09-22 22:55:13 +00:00
/>
{/if}
2024-09-22 23:36:46 +00:00
{/if}
2024-09-22 22:55:13 +00:00
</div>