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

205 lines
6.0 KiB
Svelte
Raw Normal View History

2024-05-19 11:46:35 +00:00
<script lang="ts">
2024-08-18 15:16:22 +00:00
import { onMount, tick, getContext } from 'svelte';
2024-05-19 11:46:35 +00:00
import { createEventDispatcher } from 'svelte';
import { updateChatById } from '$lib/apis/chats';
import ResponseMessage from './ResponseMessage.svelte';
2024-08-18 15:16:22 +00:00
import Tooltip from '$lib/components/common/Tooltip.svelte';
import Merge from '$lib/components/icons/Merge.svelte';
const i18n = getContext('i18n');
2024-05-19 11:46:35 +00:00
export let chatId;
export let history;
export let messages = [];
export let messageIdx;
export let parentMessage;
2024-05-31 21:19:46 +00:00
export let readOnly = false;
2024-05-19 11:46:35 +00:00
export let updateChatMessages: Function;
export let confirmEditResponseMessage: Function;
export let rateMessage: Function;
export let copyToClipboard: Function;
export let continueGeneration: Function;
export let regenerateResponse: Function;
const dispatch = createEventDispatcher();
let currentMessageId;
let groupedMessages = {};
2024-08-16 16:54:30 +00:00
let groupedMessagesIdx = {};
2024-05-19 11:46:35 +00:00
$: if (parentMessage) {
initHandler();
}
2024-05-19 11:46:35 +00:00
2024-08-16 16:54:30 +00:00
const showPreviousMessage = (modelIdx) => {
groupedMessagesIdx[modelIdx] = Math.max(0, groupedMessagesIdx[modelIdx] - 1);
let messageId = groupedMessages[modelIdx].messages[groupedMessagesIdx[modelIdx]].id;
2024-05-19 20:11:56 +00:00
console.log(messageId);
let messageChildrenIds = history.messages[messageId].childrenIds;
while (messageChildrenIds.length !== 0) {
messageId = messageChildrenIds.at(-1);
messageChildrenIds = history.messages[messageId].childrenIds;
}
history.currentId = messageId;
dispatch('change');
};
2024-08-16 16:54:30 +00:00
const showNextMessage = (modelIdx) => {
groupedMessagesIdx[modelIdx] = Math.min(
groupedMessages[modelIdx].messages.length - 1,
groupedMessagesIdx[modelIdx] + 1
2024-05-19 20:11:56 +00:00
);
2024-08-16 16:54:30 +00:00
let messageId = groupedMessages[modelIdx].messages[groupedMessagesIdx[modelIdx]].id;
2024-05-19 20:11:56 +00:00
console.log(messageId);
let messageChildrenIds = history.messages[messageId].childrenIds;
while (messageChildrenIds.length !== 0) {
messageId = messageChildrenIds.at(-1);
messageChildrenIds = history.messages[messageId].childrenIds;
}
history.currentId = messageId;
dispatch('change');
};
const initHandler = async () => {
2024-05-19 11:46:35 +00:00
await tick();
currentMessageId = messages[messageIdx].id;
groupedMessages = parentMessage?.models.reduce((a, model, modelIdx) => {
// Find all messages that are children of the parent message and have the same model
const modelMessages = parentMessage?.childrenIds
.map((id) => history.messages[id])
.filter((m) => m.modelIdx === modelIdx);
return {
...a,
[modelIdx]: { messages: modelMessages }
};
}, {});
groupedMessagesIdx = parentMessage?.models.reduce((a, model, modelIdx) => {
2024-08-16 16:54:30 +00:00
const idx = groupedMessages[modelIdx].messages.findIndex((m) => m.id === currentMessageId);
2024-05-19 11:46:35 +00:00
if (idx !== -1) {
return {
...a,
[modelIdx]: idx
};
2024-05-19 11:46:35 +00:00
} else {
return {
...a,
[modelIdx]: 0
};
2024-05-19 11:46:35 +00:00
}
}, {});
};
onMount(async () => {
initHandler();
2024-05-19 11:46:35 +00:00
});
</script>
<div>
2024-05-19 12:01:29 +00:00
<div
2024-05-19 12:06:13 +00:00
class="flex snap-x snap-mandatory overflow-x-auto scrollbar-hidden"
id="responses-container-{chatId}-{parentMessage.id}"
2024-05-19 12:01:29 +00:00
>
2024-07-01 00:45:28 +00:00
{#key currentMessageId}
2024-08-16 16:54:30 +00:00
{#each Object.keys(groupedMessages) as modelIdx}
{#if groupedMessagesIdx[modelIdx] !== undefined && groupedMessages[modelIdx].messages.length > 0}
2024-07-01 00:45:28 +00:00
<!-- svelte-ignore a11y-no-static-element-interactions -->
<!-- svelte-ignore a11y-click-events-have-key-events -->
2024-08-16 16:54:30 +00:00
{@const message = groupedMessages[modelIdx].messages[groupedMessagesIdx[modelIdx]]}
2024-07-01 00:45:28 +00:00
<div
class=" snap-center min-w-80 w-full max-w-full m-1 border {history.messages[
currentMessageId
2024-08-16 17:00:10 +00:00
]?.modelIdx == modelIdx
2024-07-01 00:45:28 +00:00
? 'border-gray-100 dark:border-gray-800 border-[1.5px]'
2024-08-18 15:16:22 +00:00
: 'border-gray-50 dark:border-gray-850 border-dashed '} transition p-5 rounded-2xl"
2024-07-01 00:45:28 +00:00
on:click={() => {
if (currentMessageId != message.id) {
currentMessageId = message.id;
let messageId = message.id;
console.log(messageId);
//
let messageChildrenIds = history.messages[messageId].childrenIds;
while (messageChildrenIds.length !== 0) {
messageId = messageChildrenIds.at(-1);
messageChildrenIds = history.messages[messageId].childrenIds;
}
history.currentId = messageId;
dispatch('change');
}
2024-05-19 11:46:35 +00:00
}}
2024-07-01 00:45:28 +00:00
>
{#key history.currentId}
{#if message}
<ResponseMessage
{message}
siblings={groupedMessages[modelIdx].messages.map((m) => m.id)}
isLastMessage={true}
{updateChatMessages}
{confirmEditResponseMessage}
showPreviousMessage={() => showPreviousMessage(modelIdx)}
showNextMessage={() => showNextMessage(modelIdx)}
{readOnly}
{rateMessage}
{copyToClipboard}
{continueGeneration}
regenerateResponse={async (message) => {
regenerateResponse(message);
await tick();
groupedMessagesIdx[modelIdx] = groupedMessages[modelIdx].messages.length - 1;
}}
on:save={async (e) => {
console.log('save', e);
const message = e.detail;
history.messages[message.id] = message;
await updateChatById(localStorage.token, chatId, {
messages: messages,
history: history
});
}}
/>
{/if}
{/key}
2024-07-01 00:45:28 +00:00
</div>
{/if}
{/each}
{/key}
2024-05-19 11:46:35 +00:00
</div>
2024-08-18 15:16:22 +00:00
{#if !readOnly}
{#if !parentMessage?.childrenIds.map((id) => history.messages[id]).find((m) => !m.done)}
<div class=" flex justify-end overflow-x-auto buttons text-gray-500 dark:text-gray-400 mt-1">
<Tooltip content={$i18n.t('Merge Responses')} placement="bottom">
<button
type="button"
id="merge-response-button"
class="{true
? 'visible'
: 'invisible group-hover:visible'} p-1 hover:bg-black/5 dark:hover:bg-white/5 rounded-lg dark:hover:text-white hover:text-black transition regenerate-response-button"
on:click={() => {
// continueGeneration();
}}
>
<Merge className=" size-5 " />
</button>
</Tooltip>
</div>
{/if}
{/if}
2024-05-19 11:46:35 +00:00
</div>