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

142 lines
3.4 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
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;
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-23 12:24:50 +00:00
});
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-10-23 05:55:34 +00:00
{chatId}
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}
2024-10-05 08:37:39 +00:00
on:submit={async (e) => {
dispatch('submit', e.detail);
}}
2024-09-22 22:55:13 +00:00
on:action={async (e) => {
2024-09-26 18:59:25 +00:00
dispatch('action', e.detail);
2024-09-22 22:55:13 +00:00
}}
2024-09-22 23:36:46 +00:00
on:update={async (e) => {
2024-09-26 18:59:25 +00:00
dispatch('update');
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;
2024-09-26 18:59:25 +00:00
dispatch('update');
2024-09-22 22:55:13 +00:00
} else {
2024-09-26 18:59:25 +00:00
dispatch('update');
2024-09-22 22:55:13 +00:00
}
}}
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-10-05 08:37:39 +00:00
on:submit={async (e) => {
dispatch('submit', e.detail);
}}
2024-09-22 22:55:13 +00:00
on:action={async (e) => {
2024-09-26 18:59:25 +00:00
dispatch('action', e.detail);
2024-09-22 22:55:13 +00:00
}}
2024-09-23 21:20:27 +00:00
on:update={async (e) => {
2024-09-26 18:59:25 +00:00
dispatch('update');
2024-09-23 21:20:27 +00:00
}}
on:save={async (e) => {
console.log('save', e);
const message = e.detail;
if (message) {
history.messages[message.id] = message;
2024-09-26 18:59:25 +00:00
dispatch('update');
2024-09-23 21:20:27 +00:00
} else {
2024-09-26 18:59:25 +00:00
dispatch('update');
2024-09-23 21:20:27 +00:00
}
}}
2024-09-22 22:55:13 +00:00
on:change={async () => {
2024-09-23 21:20:27 +00:00
await tick();
2024-09-26 18:59:25 +00:00
dispatch('update');
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>