mirror of
https://github.com/open-webui/open-webui
synced 2025-04-25 08:48:21 +00:00
refac: Tidy Chat.svelte
This commit is contained in:
parent
73998a70cc
commit
d78c35c9ba
@ -3,13 +3,13 @@
|
|||||||
import { toast } from 'svelte-sonner';
|
import { toast } from 'svelte-sonner';
|
||||||
import mermaid from 'mermaid';
|
import mermaid from 'mermaid';
|
||||||
|
|
||||||
import { getContext, onMount, tick } from 'svelte';
|
import { getContext, onDestroy, onMount, tick } from 'svelte';
|
||||||
import { goto } from '$app/navigation';
|
import { goto } from '$app/navigation';
|
||||||
import { page } from '$app/stores';
|
import { page } from '$app/stores';
|
||||||
|
|
||||||
import type { Writable } from 'svelte/store';
|
import type { Unsubscriber, Writable } from 'svelte/store';
|
||||||
import type { i18n as i18nType } from 'i18next';
|
import type { i18n as i18nType } from 'i18next';
|
||||||
import { OLLAMA_API_BASE_URL, OPENAI_API_BASE_URL, WEBUI_BASE_URL } from '$lib/constants';
|
import { WEBUI_BASE_URL } from '$lib/constants';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
chatId,
|
chatId,
|
||||||
@ -19,13 +19,11 @@
|
|||||||
models,
|
models,
|
||||||
settings,
|
settings,
|
||||||
showSidebar,
|
showSidebar,
|
||||||
tags as _tags,
|
|
||||||
WEBUI_NAME,
|
WEBUI_NAME,
|
||||||
banners,
|
banners,
|
||||||
user,
|
user,
|
||||||
socket,
|
socket,
|
||||||
showCallOverlay,
|
showCallOverlay,
|
||||||
tools,
|
|
||||||
currentChatPage,
|
currentChatPage,
|
||||||
temporaryChatEnabled
|
temporaryChatEnabled
|
||||||
} from '$lib/stores';
|
} from '$lib/stores';
|
||||||
@ -33,17 +31,13 @@
|
|||||||
convertMessagesToHistory,
|
convertMessagesToHistory,
|
||||||
copyToClipboard,
|
copyToClipboard,
|
||||||
extractSentencesForAudio,
|
extractSentencesForAudio,
|
||||||
getUserPosition,
|
|
||||||
promptTemplate,
|
promptTemplate,
|
||||||
splitStream
|
splitStream
|
||||||
} from '$lib/utils';
|
} from '$lib/utils';
|
||||||
|
|
||||||
import { generateChatCompletion } from '$lib/apis/ollama';
|
import { generateChatCompletion } from '$lib/apis/ollama';
|
||||||
import {
|
import {
|
||||||
addTagById,
|
|
||||||
createNewChat,
|
createNewChat,
|
||||||
deleteTagById,
|
|
||||||
getAllChatTags,
|
|
||||||
getChatById,
|
getChatById,
|
||||||
getChatList,
|
getChatList,
|
||||||
getTagsById,
|
getTagsById,
|
||||||
@ -66,8 +60,6 @@
|
|||||||
import MessageInput from '$lib/components/chat/MessageInput.svelte';
|
import MessageInput from '$lib/components/chat/MessageInput.svelte';
|
||||||
import Messages from '$lib/components/chat/Messages.svelte';
|
import Messages from '$lib/components/chat/Messages.svelte';
|
||||||
import Navbar from '$lib/components/layout/Navbar.svelte';
|
import Navbar from '$lib/components/layout/Navbar.svelte';
|
||||||
import CallOverlay from './MessageInput/CallOverlay.svelte';
|
|
||||||
import { error } from '@sveltejs/kit';
|
|
||||||
import ChatControls from './ChatControls.svelte';
|
import ChatControls from './ChatControls.svelte';
|
||||||
import EventConfirmDialog from '../common/ConfirmDialog.svelte';
|
import EventConfirmDialog from '../common/ConfirmDialog.svelte';
|
||||||
|
|
||||||
@ -118,6 +110,8 @@
|
|||||||
|
|
||||||
let params = {};
|
let params = {};
|
||||||
|
|
||||||
|
let chatIdUnsubscriber: Unsubscriber | undefined;
|
||||||
|
|
||||||
$: if (history.currentId !== null) {
|
$: if (history.currentId !== null) {
|
||||||
let _messages = [];
|
let _messages = [];
|
||||||
|
|
||||||
@ -207,47 +201,51 @@
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
onMount(async () => {
|
const onMessageHandler = async (event: {
|
||||||
const onMessageHandler = async (event) => {
|
origin: string;
|
||||||
if (event.origin === window.origin) {
|
data: { type: string; text: string };
|
||||||
// Replace with your iframe's origin
|
}) => {
|
||||||
console.log('Message received from iframe:', event.data);
|
if (event.origin !== window.origin) {
|
||||||
if (event.data.type === 'input:prompt') {
|
return;
|
||||||
console.log(event.data.text);
|
}
|
||||||
|
|
||||||
const inputElement = document.getElementById('chat-textarea');
|
// Replace with your iframe's origin
|
||||||
|
if (event.data.type === 'input:prompt') {
|
||||||
|
console.debug(event.data.text);
|
||||||
|
|
||||||
if (inputElement) {
|
const inputElement = document.getElementById('chat-textarea');
|
||||||
prompt = event.data.text;
|
|
||||||
inputElement.focus();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.data.type === 'action:submit') {
|
if (inputElement) {
|
||||||
console.log(event.data.text);
|
prompt = event.data.text;
|
||||||
|
inputElement.focus();
|
||||||
if (prompt !== '') {
|
|
||||||
await tick();
|
|
||||||
submitPrompt(prompt);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.data.type === 'input:prompt:submit') {
|
|
||||||
console.log(event.data.text);
|
|
||||||
|
|
||||||
if (prompt !== '') {
|
|
||||||
await tick();
|
|
||||||
submitPrompt(event.data.text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
window.addEventListener('message', onMessageHandler);
|
|
||||||
|
|
||||||
$socket.on('chat-events', chatEventHandler);
|
if (event.data.type === 'action:submit') {
|
||||||
|
console.debug(event.data.text);
|
||||||
|
|
||||||
|
if (prompt !== '') {
|
||||||
|
await tick();
|
||||||
|
submitPrompt(prompt);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (event.data.type === 'input:prompt:submit') {
|
||||||
|
console.debug(event.data.text);
|
||||||
|
|
||||||
|
if (prompt !== '') {
|
||||||
|
await tick();
|
||||||
|
submitPrompt(event.data.text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
onMount(async () => {
|
||||||
|
window.addEventListener('message', onMessageHandler);
|
||||||
|
$socket?.on('chat-events', chatEventHandler);
|
||||||
|
|
||||||
if (!$chatId) {
|
if (!$chatId) {
|
||||||
chatId.subscribe(async (value) => {
|
chatIdUnsubscriber = chatId.subscribe(async (value) => {
|
||||||
if (!value) {
|
if (!value) {
|
||||||
await initNewChat();
|
await initNewChat();
|
||||||
}
|
}
|
||||||
@ -257,12 +255,12 @@
|
|||||||
await goto('/');
|
await goto('/');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return () => {
|
onDestroy(() => {
|
||||||
window.removeEventListener('message', onMessageHandler);
|
chatIdUnsubscriber?.();
|
||||||
|
window.removeEventListener('message', onMessageHandler);
|
||||||
$socket.off('chat-events');
|
$socket?.off('chat-events');
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//////////////////////////
|
//////////////////////////
|
||||||
@ -595,11 +593,11 @@
|
|||||||
};
|
};
|
||||||
|
|
||||||
const sendPrompt = async (
|
const sendPrompt = async (
|
||||||
prompt,
|
prompt: string,
|
||||||
parentId,
|
parentId: string,
|
||||||
{ modelId = null, modelIdx = null, newChat = false } = {}
|
{ modelId = null, modelIdx = null, newChat = false } = {}
|
||||||
) => {
|
) => {
|
||||||
let _responses = [];
|
let _responses: string[] = [];
|
||||||
|
|
||||||
// If modelId is provided, use it, else use selected model
|
// If modelId is provided, use it, else use selected model
|
||||||
let selectedModelIds = modelId
|
let selectedModelIds = modelId
|
||||||
@ -609,7 +607,7 @@
|
|||||||
: selectedModels;
|
: selectedModels;
|
||||||
|
|
||||||
// Create response messages for each selected model
|
// Create response messages for each selected model
|
||||||
const responseMessageIds = {};
|
const responseMessageIds: Record<PropertyKey, string> = {};
|
||||||
for (const [_modelIdx, modelId] of selectedModelIds.entries()) {
|
for (const [_modelIdx, modelId] of selectedModelIds.entries()) {
|
||||||
const model = $models.filter((m) => m.id === modelId).at(0);
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
||||||
|
|
||||||
@ -739,13 +737,13 @@
|
|||||||
);
|
);
|
||||||
|
|
||||||
currentChatPage.set(1);
|
currentChatPage.set(1);
|
||||||
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
chats.set(await getChatList(localStorage.token, $currentChatPage));
|
||||||
|
|
||||||
return _responses;
|
return _responses;
|
||||||
};
|
};
|
||||||
|
|
||||||
const sendPromptOllama = async (model, userPrompt, responseMessageId, _chatId) => {
|
const sendPromptOllama = async (model, userPrompt, responseMessageId, _chatId) => {
|
||||||
let _response = null;
|
let _response: string | null = null;
|
||||||
|
|
||||||
const responseMessage = history.messages[responseMessageId];
|
const responseMessage = history.messages[responseMessageId];
|
||||||
const userMessage = history.messages[responseMessage.parentId];
|
const userMessage = history.messages[responseMessage.parentId];
|
||||||
@ -776,7 +774,7 @@
|
|||||||
...messages
|
...messages
|
||||||
]
|
]
|
||||||
.filter((message) => message?.content?.trim())
|
.filter((message) => message?.content?.trim())
|
||||||
.map((message, idx, arr) => {
|
.map((message) => {
|
||||||
// Prepare the base message object
|
// Prepare the base message object
|
||||||
const baseMessage = {
|
const baseMessage = {
|
||||||
role: message.role,
|
role: message.role,
|
||||||
|
@ -600,7 +600,8 @@ export const extractSentencesForAudio = (text: string) => {
|
|||||||
if (lastIndex >= 0) {
|
if (lastIndex >= 0) {
|
||||||
const previousText = mergedTexts[lastIndex];
|
const previousText = mergedTexts[lastIndex];
|
||||||
const wordCount = previousText.split(/\s+/).length;
|
const wordCount = previousText.split(/\s+/).length;
|
||||||
if (wordCount < 2) {
|
const charCount = previousText.length;
|
||||||
|
if (wordCount < 4 || charCount < 50) {
|
||||||
mergedTexts[lastIndex] = previousText + ' ' + currentText;
|
mergedTexts[lastIndex] = previousText + ' ' + currentText;
|
||||||
} else {
|
} else {
|
||||||
mergedTexts.push(currentText);
|
mergedTexts.push(currentText);
|
||||||
|
Loading…
Reference in New Issue
Block a user