2024-05-19 11:46:35 +00:00
|
|
|
<script lang="ts">
|
2024-08-18 18:59:59 +00:00
|
|
|
import dayjs from 'dayjs';
|
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';
|
2024-08-18 18:59:59 +00:00
|
|
|
|
|
|
|
import { mobile, settings } from '$lib/stores';
|
|
|
|
|
|
|
|
import { generateMoACompletion } from '$lib/apis';
|
2024-05-19 11:46:35 +00:00
|
|
|
import { updateChatById } from '$lib/apis/chats';
|
2024-08-18 18:59:59 +00:00
|
|
|
import { createOpenAITextStream } from '$lib/apis/streaming';
|
|
|
|
|
2024-05-19 11:46:35 +00:00
|
|
|
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';
|
2024-08-18 18:59:59 +00:00
|
|
|
|
|
|
|
import Markdown from './Markdown.svelte';
|
|
|
|
import Name from './Name.svelte';
|
|
|
|
import Skeleton from './Skeleton.svelte';
|
2024-08-18 15:16:22 +00:00
|
|
|
|
|
|
|
const i18n = getContext('i18n');
|
2024-05-19 11:46:35 +00:00
|
|
|
|
|
|
|
export let chatId;
|
|
|
|
|
|
|
|
export let history;
|
|
|
|
export let messageIdx;
|
|
|
|
|
|
|
|
export let parentMessage;
|
2024-08-18 15:40:26 +00:00
|
|
|
export let isLastMessage;
|
2024-05-19 11:46:35 +00:00
|
|
|
|
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;
|
2024-08-18 18:59:59 +00:00
|
|
|
export let mergeResponses: Function;
|
2024-05-19 11:46:35 +00:00
|
|
|
export let regenerateResponse: Function;
|
2024-09-22 18:16:45 +00:00
|
|
|
export let saveNewResponseMessage: Function;
|
2024-05-19 11:46:35 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2024-09-22 22:55:13 +00:00
|
|
|
$: if (history.messages) {
|
|
|
|
console.log('history.messages', history.messages);
|
|
|
|
|
2024-08-18 14:47:12 +00:00
|
|
|
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');
|
|
|
|
};
|
|
|
|
|
2024-08-18 14:47:12 +00:00
|
|
|
const initHandler = async () => {
|
2024-09-22 22:55:13 +00:00
|
|
|
console.log('multiresponse:initHandler');
|
2024-05-19 11:46:35 +00:00
|
|
|
await tick();
|
|
|
|
currentMessageId = messages[messageIdx].id;
|
|
|
|
|
2024-08-18 14:47:12 +00:00
|
|
|
groupedMessages = parentMessage?.models.reduce((a, model, modelIdx) => {
|
|
|
|
// Find all messages that are children of the parent message and have the same model
|
2024-08-21 22:22:40 +00:00
|
|
|
let modelMessages = parentMessage?.childrenIds
|
2024-08-18 14:47:12 +00:00
|
|
|
.map((id) => history.messages[id])
|
2024-08-21 22:22:40 +00:00
|
|
|
.filter((m) => m?.modelIdx === modelIdx);
|
|
|
|
|
|
|
|
if (modelMessages.length === 0) {
|
|
|
|
modelMessages = parentMessage?.childrenIds
|
|
|
|
.map((id) => history.messages[id])
|
|
|
|
.filter((m) => m?.model === model);
|
|
|
|
|
|
|
|
modelMessages.forEach((m) => {
|
|
|
|
m.modelIdx = modelIdx;
|
|
|
|
});
|
|
|
|
}
|
2024-08-18 14:47:12 +00:00
|
|
|
|
|
|
|
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) {
|
2024-08-18 14:47:12 +00:00
|
|
|
return {
|
|
|
|
...a,
|
|
|
|
[modelIdx]: idx
|
|
|
|
};
|
2024-05-19 11:46:35 +00:00
|
|
|
} else {
|
2024-08-18 14:47:12 +00:00
|
|
|
return {
|
|
|
|
...a,
|
|
|
|
[modelIdx]: 0
|
|
|
|
};
|
2024-05-19 11:46:35 +00:00
|
|
|
}
|
2024-08-18 14:47:12 +00:00
|
|
|
}, {});
|
|
|
|
};
|
|
|
|
|
2024-08-18 18:59:59 +00:00
|
|
|
const mergeResponsesHandler = async () => {
|
|
|
|
const responses = Object.keys(groupedMessages).map((modelIdx) => {
|
|
|
|
const { messages } = groupedMessages[modelIdx];
|
|
|
|
return messages[groupedMessagesIdx[modelIdx]].content;
|
|
|
|
});
|
2024-08-18 19:11:59 +00:00
|
|
|
mergeResponses(currentMessageId, responses, chatId);
|
2024-08-18 18:59:59 +00:00
|
|
|
};
|
|
|
|
|
2024-08-18 14:47:12 +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"
|
2024-08-18 14:47:12 +00:00
|
|
|
id="responses-container-{chatId}-{parentMessage.id}"
|
2024-05-19 12:01:29 +00:00
|
|
|
>
|
2024-09-22 22:55:13 +00:00
|
|
|
{#each Object.keys(groupedMessages) as modelIdx}
|
|
|
|
{#if groupedMessagesIdx[modelIdx] !== undefined && groupedMessages[modelIdx].messages.length > 0}
|
|
|
|
<!-- svelte-ignore a11y-no-static-element-interactions -->
|
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
|
|
|
{@const message = groupedMessages[modelIdx].messages[groupedMessagesIdx[modelIdx]]}
|
|
|
|
|
|
|
|
<div
|
|
|
|
class=" snap-center w-full max-w-full m-1 border {history.messages[currentMessageId]
|
|
|
|
?.modelIdx == modelIdx
|
|
|
|
? `border-gray-100 dark:border-gray-800 border-[1.5px] ${
|
|
|
|
$mobile ? 'min-w-full' : 'min-w-[32rem]'
|
|
|
|
}`
|
|
|
|
: `border-gray-50 dark:border-gray-850 border-dashed ${
|
|
|
|
$mobile ? 'min-w-full' : 'min-w-80'
|
|
|
|
}`} transition-all p-5 rounded-2xl"
|
|
|
|
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;
|
2024-07-01 00:45:28 +00:00
|
|
|
}
|
2024-09-22 22:55:13 +00:00
|
|
|
history.currentId = messageId;
|
|
|
|
dispatch('change');
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{#key history.currentId}
|
|
|
|
{#if message}
|
|
|
|
<ResponseMessage
|
|
|
|
{message}
|
|
|
|
siblings={groupedMessages[modelIdx].messages.map((m) => m.id)}
|
|
|
|
isLastMessage={true}
|
|
|
|
{updateChatMessages}
|
|
|
|
{saveNewResponseMessage}
|
|
|
|
{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:action={async (e) => {
|
|
|
|
dispatch('action', e.detail);
|
|
|
|
}}
|
|
|
|
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}
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/each}
|
2024-05-19 11:46:35 +00:00
|
|
|
</div>
|
2024-08-18 15:16:22 +00:00
|
|
|
|
2024-08-18 15:40:26 +00:00
|
|
|
{#if !readOnly && isLastMessage}
|
2024-08-18 18:59:59 +00:00
|
|
|
{#if !Object.keys(groupedMessages).find((modelIdx) => {
|
|
|
|
const { messages } = groupedMessages[modelIdx];
|
2024-08-21 22:03:58 +00:00
|
|
|
return !messages[groupedMessagesIdx[modelIdx]]?.done ?? false;
|
2024-08-18 18:59:59 +00:00
|
|
|
})}
|
|
|
|
<div class="flex justify-end">
|
|
|
|
<div class="w-full">
|
|
|
|
{#if history.messages[currentMessageId]?.merged?.status}
|
|
|
|
{@const message = history.messages[currentMessageId]?.merged}
|
|
|
|
|
|
|
|
<div class="w-full rounded-xl pl-5 pr-2 py-2">
|
|
|
|
<Name>
|
|
|
|
Merged Response
|
|
|
|
|
|
|
|
{#if message.timestamp}
|
|
|
|
<span
|
|
|
|
class=" self-center invisible group-hover:visible text-gray-400 text-xs font-medium uppercase ml-0.5 -mt-0.5"
|
|
|
|
>
|
|
|
|
{dayjs(message.timestamp * 1000).format($i18n.t('h:mm a'))}
|
|
|
|
</span>
|
|
|
|
{/if}
|
|
|
|
</Name>
|
|
|
|
|
|
|
|
<div class="mt-1 markdown-prose w-full min-w-full">
|
|
|
|
{#if (message?.content ?? '') === ''}
|
|
|
|
<Skeleton />
|
|
|
|
{:else}
|
|
|
|
<Markdown id={`merged`} content={message.content ?? ''} />
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class=" flex-shrink-0 text-gray-600 dark:text-gray-500 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={() => {
|
|
|
|
mergeResponsesHandler();
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Merge className=" size-5 " />
|
|
|
|
</button>
|
|
|
|
</Tooltip>
|
|
|
|
</div>
|
2024-08-18 15:16:22 +00:00
|
|
|
</div>
|
|
|
|
{/if}
|
|
|
|
{/if}
|
2024-05-19 11:46:35 +00:00
|
|
|
</div>
|