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

213 lines
5.4 KiB
Svelte
Raw Normal View History

2024-10-05 16:37:39 +08:00
<script>
2025-05-17 01:47:43 +08:00
import { onDestroy, onMount, tick, getContext } from 'svelte';
2024-10-07 07:14:12 +08:00
const i18n = getContext('i18n');
2024-10-05 16:37:39 +08:00
import Markdown from './Markdown.svelte';
2025-05-17 01:47:43 +08:00
import {
artifactCode,
chatId,
mobile,
settings,
showArtifacts,
showControls,
showOverview
} from '$lib/stores';
2024-12-21 06:38:15 +08:00
import FloatingButtons from '../ContentRenderer/FloatingButtons.svelte';
import { createMessagesList } from '$lib/utils';
2024-10-05 16:37:39 +08:00
export let id;
export let content;
2024-12-21 06:38:15 +08:00
export let history;
2025-06-27 20:29:04 +08:00
export let selectedModels = [];
2025-07-18 20:38:43 +08:00
export let done = true;
2024-10-05 16:37:39 +08:00
export let model = null;
2024-11-22 11:46:09 +08:00
export let sources = null;
2024-10-05 16:37:39 +08:00
2024-10-06 03:07:45 +08:00
export let save = false;
2025-05-17 01:47:43 +08:00
export let preview = false;
2024-10-05 16:37:39 +08:00
export let floatingButtons = true;
2025-08-10 05:03:02 +08:00
export let topPadding = false;
2024-12-21 07:09:17 +08:00
2025-06-27 20:29:04 +08:00
export let onSave = (e) => {};
export let onSourceClick = (e) => {};
export let onTaskClick = (e) => {};
export let onAddMessages = (e) => {};
2024-10-05 16:37:39 +08:00
let contentContainerElement;
2024-12-21 06:38:15 +08:00
let floatingButtonsElement;
2024-10-07 07:14:12 +08:00
2024-10-06 13:23:06 +08:00
const updateButtonPosition = (event) => {
2024-12-21 06:38:15 +08:00
const buttonsContainerElement = document.getElementById(`floating-buttons-${id}`);
2024-10-07 13:55:10 +08:00
if (
!contentContainerElement?.contains(event.target) &&
!buttonsContainerElement?.contains(event.target)
) {
closeFloatingButtons();
return;
}
2024-10-05 16:37:39 +08:00
setTimeout(async () => {
await tick();
2024-10-06 13:23:06 +08:00
2024-10-07 13:55:10 +08:00
if (!contentContainerElement?.contains(event.target)) return;
2024-10-06 13:23:06 +08:00
2024-10-05 16:37:39 +08:00
let selection = window.getSelection();
if (selection.toString().trim().length > 0) {
const range = selection.getRangeAt(0);
const rect = range.getBoundingClientRect();
2024-10-07 11:26:35 +08:00
const parentRect = contentContainerElement.getBoundingClientRect();
// Adjust based on parent rect
const top = rect.bottom - parentRect.top;
const left = rect.left - parentRect.left;
2024-10-07 07:14:12 +08:00
2024-10-05 18:21:22 +08:00
if (buttonsContainerElement) {
buttonsContainerElement.style.display = 'block';
2024-10-07 11:26:35 +08:00
// Calculate space available on the right
2024-12-21 06:38:15 +08:00
const spaceOnRight = parentRect.width - left;
2024-12-22 00:41:49 +08:00
let halfScreenWidth = $mobile ? window.innerWidth / 2 : window.innerWidth / 3;
2024-10-07 11:26:35 +08:00
2024-12-21 06:38:15 +08:00
if (spaceOnRight < halfScreenWidth) {
2024-10-07 11:26:35 +08:00
const right = parentRect.right - rect.right;
buttonsContainerElement.style.right = `${right}px`;
buttonsContainerElement.style.left = 'auto'; // Reset left
} else {
// Enough space, position using 'left'
buttonsContainerElement.style.left = `${left}px`;
buttonsContainerElement.style.right = 'auto'; // Reset right
}
2024-10-05 18:21:22 +08:00
buttonsContainerElement.style.top = `${top + 5}px`; // +5 to add some spacing
}
2024-10-05 16:37:39 +08:00
} else {
2024-10-07 11:29:30 +08:00
closeFloatingButtons();
2024-10-05 16:37:39 +08:00
}
}, 0);
};
2024-10-07 11:29:30 +08:00
const closeFloatingButtons = () => {
2024-12-21 06:38:15 +08:00
const buttonsContainerElement = document.getElementById(`floating-buttons-${id}`);
2024-10-07 11:29:30 +08:00
if (buttonsContainerElement) {
buttonsContainerElement.style.display = 'none';
}
2024-10-07 07:14:12 +08:00
2024-12-21 06:38:15 +08:00
if (floatingButtonsElement) {
2025-03-30 05:59:03 +08:00
// check if closeHandler is defined
if (typeof floatingButtonsElement?.closeHandler === 'function') {
// call the closeHandler function
floatingButtonsElement?.closeHandler();
}
2024-12-21 06:38:15 +08:00
}
2024-10-07 07:14:12 +08:00
};
2024-10-07 11:29:30 +08:00
const keydownHandler = (e) => {
if (e.key === 'Escape') {
closeFloatingButtons();
}
};
2024-10-05 16:37:39 +08:00
onMount(() => {
if (floatingButtons) {
contentContainerElement?.addEventListener('mouseup', updateButtonPosition);
2024-10-05 18:19:55 +08:00
document.addEventListener('mouseup', updateButtonPosition);
2024-10-07 11:29:30 +08:00
document.addEventListener('keydown', keydownHandler);
2024-10-05 16:37:39 +08:00
}
});
onDestroy(() => {
if (floatingButtons) {
contentContainerElement?.removeEventListener('mouseup', updateButtonPosition);
2024-10-05 18:19:55 +08:00
document.removeEventListener('mouseup', updateButtonPosition);
2024-10-07 11:29:30 +08:00
document.removeEventListener('keydown', keydownHandler);
2024-10-05 16:37:39 +08:00
}
});
</script>
<div bind:this={contentContainerElement}>
2024-10-06 03:04:36 +08:00
<Markdown
{id}
{content}
{model}
2024-10-06 03:07:45 +08:00
{save}
2025-05-17 01:47:43 +08:00
{preview}
2025-07-18 20:38:43 +08:00
{done}
2025-08-10 05:03:02 +08:00
{topPadding}
2024-11-22 11:46:09 +08:00
sourceIds={(sources ?? []).reduce((acc, s) => {
let ids = [];
s.document.forEach((document, index) => {
if (model?.info?.meta?.capabilities?.citations == false) {
ids.push('N/A');
return ids;
}
2024-11-22 11:46:09 +08:00
const metadata = s.metadata?.[index];
const id = metadata?.source ?? 'N/A';
if (metadata?.name) {
ids.push(metadata.name);
return ids;
}
if (id.startsWith('http://') || id.startsWith('https://')) {
ids.push(id);
} else {
ids.push(s?.source?.name ?? id);
}
return ids;
});
acc = [...acc, ...ids];
// remove duplicates
return acc.filter((item, index) => acc.indexOf(item) === index);
}, [])}
2024-11-22 09:58:29 +08:00
{onSourceClick}
2025-02-14 14:37:01 +08:00
{onTaskClick}
2025-05-17 07:14:26 +08:00
{onSave}
onUpdate={(token) => {
const { lang, text: code } = token;
2024-10-07 03:51:29 +08:00
if (
2025-04-11 01:36:57 +08:00
($settings?.detectArtifacts ?? true) &&
2024-10-07 03:51:29 +08:00
(['html', 'svg'].includes(lang) || (lang === 'xml' && code.includes('svg'))) &&
2024-10-07 07:39:46 +08:00
!$mobile &&
$chatId
2024-10-07 03:51:29 +08:00
) {
2024-10-06 15:14:05 +08:00
showArtifacts.set(true);
showControls.set(true);
}
}}
2025-05-17 07:14:26 +08:00
onPreview={async (value) => {
console.log('Preview', value);
await artifactCode.set(value);
await showControls.set(true);
await showArtifacts.set(true);
await showOverview.set(false);
}}
2024-10-06 03:04:36 +08:00
/>
2024-10-05 16:37:39 +08:00
</div>
2024-12-21 06:38:15 +08:00
{#if floatingButtons && model}
<FloatingButtons
bind:this={floatingButtonsElement}
{id}
2025-08-07 06:11:27 +08:00
actions={$settings?.floatingActionButtons ?? []}
2025-06-27 20:29:04 +08:00
model={(selectedModels ?? []).includes(model?.id)
? model?.id
: (selectedModels ?? []).length > 0
? selectedModels.at(0)
: model?.id}
2024-12-21 06:38:15 +08:00
messages={createMessagesList(history, id)}
2024-12-21 07:19:54 +08:00
onAdd={({ modelId, parentId, messages }) => {
console.log(modelId, parentId, messages);
onAddMessages({ modelId, parentId, messages });
2024-12-21 06:38:15 +08:00
closeFloatingButtons();
}}
/>
2024-10-05 16:37:39 +08:00
{/if}