2024-12-20 22:38:15 +00:00
|
|
|
<script lang="ts">
|
|
|
|
|
import { toast } from 'svelte-sonner';
|
|
|
|
|
|
|
|
|
|
import DOMPurify from 'dompurify';
|
|
|
|
|
import { marked } from 'marked';
|
|
|
|
|
|
2025-07-27 07:07:28 +00:00
|
|
|
import { getContext, tick, onDestroy } from 'svelte';
|
2024-12-20 22:38:15 +00:00
|
|
|
const i18n = getContext('i18n');
|
|
|
|
|
|
|
|
|
|
import { chatCompletion } from '$lib/apis/openai';
|
|
|
|
|
|
|
|
|
|
import ChatBubble from '$lib/components/icons/ChatBubble.svelte';
|
2025-05-19 21:12:36 +00:00
|
|
|
import LightBulb from '$lib/components/icons/LightBulb.svelte';
|
2024-12-20 22:38:15 +00:00
|
|
|
import Markdown from '../Messages/Markdown.svelte';
|
|
|
|
|
import Skeleton from '../Messages/Skeleton.svelte';
|
|
|
|
|
|
|
|
|
|
export let id = '';
|
2025-08-09 22:52:40 +00:00
|
|
|
export let messageId = '';
|
|
|
|
|
|
2024-12-20 22:38:15 +00:00
|
|
|
export let model = null;
|
|
|
|
|
export let messages = [];
|
2025-08-06 22:11:27 +00:00
|
|
|
export let actions = [];
|
2025-08-06 20:12:24 +00:00
|
|
|
export let onAdd = (e) => {};
|
2024-12-20 22:38:15 +00:00
|
|
|
|
|
|
|
|
let floatingInput = false;
|
2025-08-06 20:12:24 +00:00
|
|
|
let selectedAction = null;
|
2024-12-20 22:38:15 +00:00
|
|
|
|
|
|
|
|
let selectedText = '';
|
|
|
|
|
let floatingInputValue = '';
|
|
|
|
|
|
2025-08-06 20:12:24 +00:00
|
|
|
let content = '';
|
2024-12-20 22:38:15 +00:00
|
|
|
let responseContent = null;
|
2024-12-20 23:09:17 +00:00
|
|
|
let responseDone = false;
|
2025-07-27 07:07:28 +00:00
|
|
|
let controller = null;
|
2024-12-20 22:38:15 +00:00
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
$: if (actions.length === 0) {
|
|
|
|
|
actions = DEFAULT_ACTIONS;
|
|
|
|
|
}
|
2024-12-20 23:21:27 +00:00
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
const DEFAULT_ACTIONS = [
|
2025-08-06 20:12:24 +00:00
|
|
|
{
|
|
|
|
|
id: 'ask',
|
|
|
|
|
label: $i18n.t('Ask'),
|
|
|
|
|
icon: ChatBubble,
|
|
|
|
|
input: true,
|
|
|
|
|
prompt: `{{SELECTED_CONTENT}}\n\n\n{{INPUT_CONTENT}}`
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
id: 'explain',
|
|
|
|
|
label: $i18n.t('Explain'),
|
|
|
|
|
icon: LightBulb,
|
2025-08-06 20:14:35 +00:00
|
|
|
prompt: `{{SELECTED_CONTENT}}\n\n\n${$i18n.t('Explain')}`
|
2024-12-20 22:38:15 +00:00
|
|
|
}
|
2025-08-06 20:12:24 +00:00
|
|
|
];
|
2024-12-20 23:21:27 +00:00
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
const autoScroll = async () => {
|
|
|
|
|
const responseContainer = document.getElementById('response-container');
|
|
|
|
|
if (responseContainer) {
|
|
|
|
|
// Scroll to bottom only if the scroll is at the bottom give 50px buffer
|
|
|
|
|
if (
|
|
|
|
|
responseContainer.scrollHeight - responseContainer.clientHeight <=
|
|
|
|
|
responseContainer.scrollTop + 50
|
|
|
|
|
) {
|
|
|
|
|
responseContainer.scrollTop = responseContainer.scrollHeight;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-08-06 20:12:24 +00:00
|
|
|
const actionHandler = async (actionId) => {
|
2024-12-20 22:38:15 +00:00
|
|
|
if (!model) {
|
|
|
|
|
toast.error('Model not selected');
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-08-06 20:12:24 +00:00
|
|
|
|
|
|
|
|
let selectedContent = selectedText
|
2025-05-23 01:16:47 +00:00
|
|
|
.split('\n')
|
2025-05-23 15:22:08 +00:00
|
|
|
.map((line) => `> ${line}`)
|
2025-05-23 01:16:47 +00:00
|
|
|
.join('\n');
|
2024-12-20 22:38:15 +00:00
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
let selectedAction = actions.find((action) => action.id === actionId);
|
2025-08-06 20:12:24 +00:00
|
|
|
if (!selectedAction) {
|
|
|
|
|
toast.error('Action not found');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let prompt = selectedAction?.prompt ?? '';
|
2025-08-08 09:43:30 +00:00
|
|
|
let toolIds = [];
|
|
|
|
|
|
2025-08-08 09:50:53 +00:00
|
|
|
// Handle: {{variableId|tool:id="toolId"}} pattern
|
|
|
|
|
// This regex captures variableId and toolId from {{variableId|tool:id="toolId"}}
|
|
|
|
|
const varToolPattern = /\{\{(.*?)\|tool:id="([^"]+)"\}\}/g;
|
|
|
|
|
prompt = prompt.replace(varToolPattern, (match, variableId, toolId) => {
|
|
|
|
|
toolIds.push(toolId);
|
|
|
|
|
return variableId; // Replace with just variableId
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// legacy {{TOOL:toolId}} pattern (for backward compatibility)
|
2025-08-08 09:43:30 +00:00
|
|
|
let toolIdPattern = /\{\{TOOL:([^\}]+)\}\}/g;
|
|
|
|
|
let match;
|
|
|
|
|
while ((match = toolIdPattern.exec(prompt)) !== null) {
|
|
|
|
|
toolIds.push(match[1]);
|
|
|
|
|
}
|
2025-08-08 09:50:53 +00:00
|
|
|
|
2025-08-08 09:43:30 +00:00
|
|
|
// Remove all TOOL placeholders from the prompt
|
|
|
|
|
prompt = prompt.replace(toolIdPattern, '');
|
|
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
if (prompt.includes('{{INPUT_CONTENT}}') && !floatingInput) {
|
2025-08-06 20:12:24 +00:00
|
|
|
prompt = prompt.replace('{{INPUT_CONTENT}}', floatingInputValue);
|
|
|
|
|
floatingInputValue = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prompt = prompt.replace('{{CONTENT}}', selectedText);
|
|
|
|
|
prompt = prompt.replace('{{SELECTED_CONTENT}}', selectedContent);
|
|
|
|
|
|
|
|
|
|
content = prompt;
|
2024-12-20 22:38:15 +00:00
|
|
|
responseContent = '';
|
2025-08-06 20:12:24 +00:00
|
|
|
|
2025-07-27 07:07:28 +00:00
|
|
|
let res;
|
|
|
|
|
[res, controller] = await chatCompletion(localStorage.token, {
|
2024-12-20 22:38:15 +00:00
|
|
|
model: model,
|
|
|
|
|
messages: [
|
|
|
|
|
...messages,
|
|
|
|
|
{
|
|
|
|
|
role: 'user',
|
2025-08-06 20:12:24 +00:00
|
|
|
content: content
|
2024-12-20 22:38:15 +00:00
|
|
|
}
|
2024-12-21 03:13:17 +00:00
|
|
|
].map((message) => ({
|
|
|
|
|
role: message.role,
|
|
|
|
|
content: message.content
|
|
|
|
|
})),
|
2025-08-08 09:43:30 +00:00
|
|
|
...(toolIds.length > 0
|
|
|
|
|
? {
|
|
|
|
|
tool_ids: toolIds
|
|
|
|
|
// params: {
|
|
|
|
|
// function_calling: 'native'
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
|
2024-12-20 22:38:15 +00:00
|
|
|
stream: true // Enable streaming
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (res && res.ok) {
|
|
|
|
|
const reader = res.body.getReader();
|
|
|
|
|
const decoder = new TextDecoder();
|
|
|
|
|
|
|
|
|
|
const processStream = async () => {
|
|
|
|
|
while (true) {
|
|
|
|
|
// Read data chunks from the response stream
|
|
|
|
|
const { done, value } = await reader.read();
|
|
|
|
|
if (done) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Decode the received chunk
|
|
|
|
|
const chunk = decoder.decode(value, { stream: true });
|
|
|
|
|
|
|
|
|
|
// Process lines within the chunk
|
|
|
|
|
const lines = chunk.split('\n').filter((line) => line.trim() !== '');
|
|
|
|
|
|
|
|
|
|
for (const line of lines) {
|
|
|
|
|
if (line.startsWith('data: ')) {
|
|
|
|
|
if (line.startsWith('data: [DONE]')) {
|
2024-12-20 23:09:17 +00:00
|
|
|
responseDone = true;
|
2024-12-20 23:21:27 +00:00
|
|
|
|
|
|
|
|
await tick();
|
|
|
|
|
autoScroll();
|
2024-12-20 22:38:15 +00:00
|
|
|
continue;
|
|
|
|
|
} else {
|
|
|
|
|
// Parse the JSON chunk
|
|
|
|
|
try {
|
|
|
|
|
const data = JSON.parse(line.slice(6));
|
|
|
|
|
|
|
|
|
|
// Append the `content` field from the "choices" object
|
|
|
|
|
if (data.choices && data.choices[0]?.delta?.content) {
|
|
|
|
|
responseContent += data.choices[0].delta.content;
|
|
|
|
|
|
2024-12-20 23:21:27 +00:00
|
|
|
autoScroll();
|
2024-12-20 22:38:15 +00:00
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Process the stream in the background
|
2025-07-27 07:07:28 +00:00
|
|
|
try {
|
|
|
|
|
await processStream();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
if (e.name !== 'AbortError') {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-12-20 22:38:15 +00:00
|
|
|
} else {
|
|
|
|
|
toast.error('An error occurred while fetching the explanation');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const addHandler = async () => {
|
2024-12-20 23:09:17 +00:00
|
|
|
const messages = [
|
2024-12-20 22:38:15 +00:00
|
|
|
{
|
|
|
|
|
role: 'user',
|
2025-08-06 20:12:24 +00:00
|
|
|
content: content
|
2024-12-20 22:38:15 +00:00
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
role: 'assistant',
|
|
|
|
|
content: responseContent
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
|
2024-12-20 23:09:17 +00:00
|
|
|
onAdd({
|
|
|
|
|
modelId: model,
|
2025-08-09 22:52:40 +00:00
|
|
|
parentId: messageId,
|
2024-12-20 23:09:17 +00:00
|
|
|
messages: messages
|
|
|
|
|
});
|
2024-12-20 22:38:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const closeHandler = () => {
|
2025-08-06 20:12:24 +00:00
|
|
|
if (controller) {
|
|
|
|
|
controller.abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
selectedAction = null;
|
|
|
|
|
selectedText = '';
|
2024-12-20 22:38:15 +00:00
|
|
|
responseContent = null;
|
2024-12-20 23:09:17 +00:00
|
|
|
responseDone = false;
|
2024-12-20 22:38:15 +00:00
|
|
|
floatingInput = false;
|
|
|
|
|
floatingInputValue = '';
|
|
|
|
|
};
|
2025-07-27 07:07:28 +00:00
|
|
|
|
|
|
|
|
onDestroy(() => {
|
|
|
|
|
if (controller) {
|
|
|
|
|
controller.abort();
|
|
|
|
|
}
|
|
|
|
|
});
|
2024-12-20 22:38:15 +00:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
id={`floating-buttons-${id}`}
|
2025-02-16 03:27:25 +00:00
|
|
|
class="absolute rounded-lg mt-1 text-xs z-9999"
|
2024-12-20 22:38:15 +00:00
|
|
|
style="display: none"
|
|
|
|
|
>
|
|
|
|
|
{#if responseContent === null}
|
|
|
|
|
{#if !floatingInput}
|
|
|
|
|
<div
|
|
|
|
|
class="flex flex-row gap-0.5 shrink-0 p-1 bg-white dark:bg-gray-850 dark:text-gray-100 text-medium rounded-lg shadow-xl"
|
|
|
|
|
>
|
2025-08-06 22:11:27 +00:00
|
|
|
{#each actions as action}
|
2025-08-06 20:12:24 +00:00
|
|
|
<button
|
|
|
|
|
class="px-1 hover:bg-gray-50 dark:hover:bg-gray-800 rounded-sm flex items-center gap-1 min-w-fit"
|
|
|
|
|
on:click={async () => {
|
|
|
|
|
selectedText = window.getSelection().toString();
|
|
|
|
|
selectedAction = action;
|
|
|
|
|
|
2025-08-06 22:11:27 +00:00
|
|
|
if (action.prompt.includes('{{INPUT_CONTENT}}')) {
|
2025-08-06 20:12:24 +00:00
|
|
|
floatingInput = true;
|
|
|
|
|
floatingInputValue = '';
|
2024-12-20 22:38:15 +00:00
|
|
|
|
2025-08-06 20:12:24 +00:00
|
|
|
await tick();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
const input = document.getElementById('floating-message-input');
|
|
|
|
|
if (input) {
|
|
|
|
|
input.focus();
|
|
|
|
|
}
|
|
|
|
|
}, 0);
|
|
|
|
|
} else {
|
|
|
|
|
actionHandler(action.id);
|
|
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-08-06 22:11:27 +00:00
|
|
|
{#if action.icon}
|
|
|
|
|
<svelte:component this={action.icon} className="size-3 shrink-0" />
|
|
|
|
|
{/if}
|
2025-08-06 20:12:24 +00:00
|
|
|
<div class="shrink-0">{action.label}</div>
|
|
|
|
|
</button>
|
|
|
|
|
{/each}
|
2024-12-20 22:38:15 +00:00
|
|
|
</div>
|
|
|
|
|
{:else}
|
|
|
|
|
<div
|
2025-03-31 07:57:16 +00:00
|
|
|
class="py-1 flex dark:text-gray-100 bg-gray-50 dark:bg-gray-800 border border-gray-100 dark:border-gray-850 w-72 rounded-full shadow-xl"
|
2024-12-20 22:38:15 +00:00
|
|
|
>
|
|
|
|
|
<input
|
|
|
|
|
type="text"
|
|
|
|
|
id="floating-message-input"
|
2025-02-16 03:27:25 +00:00
|
|
|
class="ml-5 bg-transparent outline-hidden w-full flex-1 text-sm"
|
2024-12-20 22:38:15 +00:00
|
|
|
placeholder={$i18n.t('Ask a question')}
|
|
|
|
|
bind:value={floatingInputValue}
|
|
|
|
|
on:keydown={(e) => {
|
|
|
|
|
if (e.key === 'Enter') {
|
2025-08-06 20:12:24 +00:00
|
|
|
actionHandler(selectedAction?.id);
|
2024-12-20 22:38:15 +00:00
|
|
|
}
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div class="ml-1 mr-2">
|
|
|
|
|
<button
|
|
|
|
|
class="{floatingInputValue !== ''
|
|
|
|
|
? 'bg-black text-white hover:bg-gray-900 dark:bg-white dark:text-black dark:hover:bg-gray-100 '
|
|
|
|
|
: 'text-white bg-gray-200 dark:text-gray-900 dark:bg-gray-700 disabled'} transition rounded-full p-1.5 m-0.5 self-center"
|
|
|
|
|
on:click={() => {
|
2025-08-06 20:12:24 +00:00
|
|
|
actionHandler(selectedAction?.id);
|
2024-12-20 22:38:15 +00:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<svg
|
|
|
|
|
xmlns="http://www.w3.org/2000/svg"
|
|
|
|
|
viewBox="0 0 16 16"
|
|
|
|
|
fill="currentColor"
|
|
|
|
|
class="size-4"
|
|
|
|
|
>
|
|
|
|
|
<path
|
|
|
|
|
fill-rule="evenodd"
|
|
|
|
|
d="M8 14a.75.75 0 0 1-.75-.75V4.56L4.03 7.78a.75.75 0 0 1-1.06-1.06l4.5-4.5a.75.75 0 0 1 1.06 0l4.5 4.5a.75.75 0 0 1-1.06 1.06L8.75 4.56v8.69A.75.75 0 0 1 8 14Z"
|
|
|
|
|
clip-rule="evenodd"
|
|
|
|
|
/>
|
|
|
|
|
</svg>
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
{:else}
|
|
|
|
|
<div class="bg-white dark:bg-gray-850 dark:text-gray-100 rounded-xl shadow-xl w-80 max-w-full">
|
|
|
|
|
<div
|
|
|
|
|
class="bg-gray-50/50 dark:bg-gray-800 dark:text-gray-100 text-medium rounded-xl px-3.5 py-3 w-full"
|
|
|
|
|
>
|
|
|
|
|
<div class="font-medium">
|
2025-08-06 20:12:24 +00:00
|
|
|
<Markdown id={`${id}-float-prompt`} {content} />
|
2024-12-20 22:38:15 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div
|
|
|
|
|
class="bg-white dark:bg-gray-850 dark:text-gray-100 text-medium rounded-xl px-3.5 py-3 w-full"
|
|
|
|
|
>
|
|
|
|
|
<div class=" max-h-80 overflow-y-auto w-full markdown-prose-xs" id="response-container">
|
2025-08-06 20:12:24 +00:00
|
|
|
{#if !responseContent || responseContent?.trim() === ''}
|
2024-12-20 22:38:15 +00:00
|
|
|
<Skeleton size="sm" />
|
|
|
|
|
{:else}
|
|
|
|
|
<Markdown id={`${id}-float-response`} content={responseContent} />
|
|
|
|
|
{/if}
|
2024-12-20 23:09:17 +00:00
|
|
|
|
|
|
|
|
{#if responseDone}
|
|
|
|
|
<div class="flex justify-end pt-3 text-sm font-medium">
|
|
|
|
|
<button
|
|
|
|
|
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-full"
|
|
|
|
|
on:click={addHandler}
|
|
|
|
|
>
|
|
|
|
|
{$i18n.t('Add')}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2024-12-20 22:38:15 +00:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|