|
@@ -41,6 +41,7 @@
|
|
convertMessagesToHistory,
|
|
convertMessagesToHistory,
|
|
copyToClipboard,
|
|
copyToClipboard,
|
|
getMessageContentParts,
|
|
getMessageContentParts,
|
|
|
|
+ createMessagesList,
|
|
extractSentencesForAudio,
|
|
extractSentencesForAudio,
|
|
promptTemplate,
|
|
promptTemplate,
|
|
splitStream,
|
|
splitStream,
|
|
@@ -226,7 +227,7 @@
|
|
}
|
|
}
|
|
|
|
|
|
await tick();
|
|
await tick();
|
|
- saveChatHandler(_chatId);
|
|
|
|
|
|
+ saveChatHandler(_chatId, history);
|
|
};
|
|
};
|
|
|
|
|
|
const chatEventHandler = async (event, cb) => {
|
|
const chatEventHandler = async (event, cb) => {
|
|
@@ -826,20 +827,6 @@
|
|
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
|
|
messagesContainerElement.scrollTop = messagesContainerElement.scrollHeight;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
-
|
|
|
|
- const createMessagesList = (responseMessageId) => {
|
|
|
|
- if (responseMessageId === null) {
|
|
|
|
- return [];
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- const message = history.messages[responseMessageId];
|
|
|
|
- if (message?.parentId) {
|
|
|
|
- return [...createMessagesList(message.parentId), message];
|
|
|
|
- } else {
|
|
|
|
- return [message];
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
-
|
|
|
|
const chatCompletedHandler = async (chatId, modelId, responseMessageId, messages) => {
|
|
const chatCompletedHandler = async (chatId, modelId, responseMessageId, messages) => {
|
|
const res = await chatCompleted(localStorage.token, {
|
|
const res = await chatCompleted(localStorage.token, {
|
|
model: modelId,
|
|
model: modelId,
|
|
@@ -896,7 +883,7 @@
|
|
};
|
|
};
|
|
|
|
|
|
const chatActionHandler = async (chatId, actionId, modelId, responseMessageId, event = null) => {
|
|
const chatActionHandler = async (chatId, actionId, modelId, responseMessageId, event = null) => {
|
|
- const messages = createMessagesList(responseMessageId);
|
|
|
|
|
|
+ const messages = createMessagesList(history, responseMessageId);
|
|
|
|
|
|
const res = await chatAction(localStorage.token, actionId, {
|
|
const res = await chatAction(localStorage.token, actionId, {
|
|
model: modelId,
|
|
model: modelId,
|
|
@@ -965,7 +952,7 @@
|
|
const modelId = selectedModels[0];
|
|
const modelId = selectedModels[0];
|
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
|
|
|
|
|
- const messages = createMessagesList(history.currentId);
|
|
|
|
|
|
+ const messages = createMessagesList(history, history.currentId);
|
|
const parentMessage = messages.length !== 0 ? messages.at(-1) : null;
|
|
const parentMessage = messages.length !== 0 ? messages.at(-1) : null;
|
|
|
|
|
|
const userMessageId = uuidv4();
|
|
const userMessageId = uuidv4();
|
|
@@ -1010,9 +997,9 @@
|
|
}
|
|
}
|
|
|
|
|
|
if (messages.length === 0) {
|
|
if (messages.length === 0) {
|
|
- await initChatHandler();
|
|
|
|
|
|
+ await initChatHandler(history);
|
|
} else {
|
|
} else {
|
|
- await saveChatHandler($chatId);
|
|
|
|
|
|
+ await saveChatHandler($chatId, history);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
};
|
|
@@ -1074,9 +1061,9 @@
|
|
}
|
|
}
|
|
|
|
|
|
if (messages.length === 0) {
|
|
if (messages.length === 0) {
|
|
- await initChatHandler();
|
|
|
|
|
|
+ await initChatHandler(history);
|
|
} else {
|
|
} else {
|
|
- await saveChatHandler($chatId);
|
|
|
|
|
|
+ await saveChatHandler($chatId, history);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
@@ -1210,7 +1197,12 @@
|
|
);
|
|
);
|
|
|
|
|
|
history.messages[message.id] = message;
|
|
history.messages[message.id] = message;
|
|
- await chatCompletedHandler(chatId, message.model, message.id, createMessagesList(message.id));
|
|
|
|
|
|
+ await chatCompletedHandler(
|
|
|
|
+ chatId,
|
|
|
|
+ message.model,
|
|
|
|
+ message.id,
|
|
|
|
+ createMessagesList(history, message.id)
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
console.log(data);
|
|
console.log(data);
|
|
@@ -1226,7 +1218,7 @@
|
|
const submitPrompt = async (userPrompt, { _raw = false } = {}) => {
|
|
const submitPrompt = async (userPrompt, { _raw = false } = {}) => {
|
|
console.log('submitPrompt', userPrompt, $chatId);
|
|
console.log('submitPrompt', userPrompt, $chatId);
|
|
|
|
|
|
- const messages = createMessagesList(history.currentId);
|
|
|
|
|
|
+ const messages = createMessagesList(history, history.currentId);
|
|
const _selectedModels = selectedModels.map((modelId) =>
|
|
const _selectedModels = selectedModels.map((modelId) =>
|
|
$models.map((m) => m.id).includes(modelId) ? modelId : ''
|
|
$models.map((m) => m.id).includes(modelId) ? modelId : ''
|
|
);
|
|
);
|
|
@@ -1325,25 +1317,30 @@
|
|
|
|
|
|
saveSessionSelectedModels();
|
|
saveSessionSelectedModels();
|
|
|
|
|
|
- await sendPrompt(userPrompt, userMessageId, { newChat: true });
|
|
|
|
|
|
+ await sendPrompt(history, userPrompt, userMessageId, { newChat: true });
|
|
};
|
|
};
|
|
|
|
|
|
const sendPrompt = async (
|
|
const sendPrompt = async (
|
|
|
|
+ history,
|
|
prompt: string,
|
|
prompt: string,
|
|
parentId: string,
|
|
parentId: string,
|
|
{ modelId = null, modelIdx = null, newChat = false } = {}
|
|
{ modelId = null, modelIdx = null, newChat = false } = {}
|
|
) => {
|
|
) => {
|
|
|
|
+ let _chatId = JSON.parse(JSON.stringify($chatId));
|
|
|
|
+
|
|
// Create new chat if newChat is true and first user message
|
|
// Create new chat if newChat is true and first user message
|
|
if (
|
|
if (
|
|
newChat &&
|
|
newChat &&
|
|
history.messages[history.currentId].parentId === null &&
|
|
history.messages[history.currentId].parentId === null &&
|
|
history.messages[history.currentId].role === 'user'
|
|
history.messages[history.currentId].role === 'user'
|
|
) {
|
|
) {
|
|
- await initChatHandler();
|
|
|
|
|
|
+ _chatId = await initChatHandler(history);
|
|
} else {
|
|
} else {
|
|
- await saveChatHandler($chatId);
|
|
|
|
|
|
+ await saveChatHandler(_chatId, history);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ await tick();
|
|
|
|
+
|
|
// 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
|
|
? [modelId]
|
|
? [modelId]
|
|
@@ -1390,16 +1387,15 @@
|
|
await tick();
|
|
await tick();
|
|
|
|
|
|
// Save chat after all messages have been created
|
|
// Save chat after all messages have been created
|
|
- await saveChatHandler($chatId);
|
|
|
|
|
|
+ await saveChatHandler(_chatId, history);
|
|
|
|
|
|
- const _chatId = JSON.parse(JSON.stringify($chatId));
|
|
|
|
await Promise.all(
|
|
await Promise.all(
|
|
selectedModelIds.map(async (modelId, _modelIdx) => {
|
|
selectedModelIds.map(async (modelId, _modelIdx) => {
|
|
console.log('modelId', modelId);
|
|
console.log('modelId', modelId);
|
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
|
const model = $models.filter((m) => m.id === modelId).at(0);
|
|
|
|
|
|
if (model) {
|
|
if (model) {
|
|
- const messages = createMessagesList(parentId);
|
|
|
|
|
|
+ const messages = createMessagesList(history, parentId);
|
|
// If there are image files, check if model is vision capable
|
|
// If there are image files, check if model is vision capable
|
|
const hasImages = messages.some((message) =>
|
|
const hasImages = messages.some((message) =>
|
|
message.files?.some((file) => file.type === 'image')
|
|
message.files?.some((file) => file.type === 'image')
|
|
@@ -1444,7 +1440,7 @@
|
|
const chatEventEmitter = await getChatEventEmitter(model.id, _chatId);
|
|
const chatEventEmitter = await getChatEventEmitter(model.id, _chatId);
|
|
|
|
|
|
scrollToBottom();
|
|
scrollToBottom();
|
|
- await sendPromptSocket(model, responseMessageId, _chatId);
|
|
|
|
|
|
+ await sendPromptSocket(history, model, responseMessageId, _chatId);
|
|
|
|
|
|
if (chatEventEmitter) clearInterval(chatEventEmitter);
|
|
if (chatEventEmitter) clearInterval(chatEventEmitter);
|
|
} else {
|
|
} else {
|
|
@@ -1457,7 +1453,7 @@
|
|
chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
};
|
|
};
|
|
|
|
|
|
- const sendPromptSocket = async (model, responseMessageId, _chatId) => {
|
|
|
|
|
|
+ const sendPromptSocket = async (history, model, responseMessageId, _chatId) => {
|
|
const responseMessage = history.messages[responseMessageId];
|
|
const responseMessage = history.messages[responseMessageId];
|
|
const userMessage = history.messages[responseMessage.parentId];
|
|
const userMessage = history.messages[responseMessage.parentId];
|
|
|
|
|
|
@@ -1507,7 +1503,7 @@
|
|
}`
|
|
}`
|
|
}
|
|
}
|
|
: undefined,
|
|
: undefined,
|
|
- ...createMessagesList(responseMessageId).map((message) => ({
|
|
|
|
|
|
+ ...createMessagesList(history, responseMessageId).map((message) => ({
|
|
...message,
|
|
...message,
|
|
content: removeDetails(message.content, ['reasoning', 'code_interpreter'])
|
|
content: removeDetails(message.content, ['reasoning', 'code_interpreter'])
|
|
}))
|
|
}))
|
|
@@ -1704,7 +1700,7 @@
|
|
history.currentId = userMessageId;
|
|
history.currentId = userMessageId;
|
|
|
|
|
|
await tick();
|
|
await tick();
|
|
- await sendPrompt(userPrompt, userMessageId);
|
|
|
|
|
|
+ await sendPrompt(history, userPrompt, userMessageId);
|
|
};
|
|
};
|
|
|
|
|
|
const regenerateResponse = async (message) => {
|
|
const regenerateResponse = async (message) => {
|
|
@@ -1716,11 +1712,11 @@
|
|
|
|
|
|
if ((userMessage?.models ?? [...selectedModels]).length == 1) {
|
|
if ((userMessage?.models ?? [...selectedModels]).length == 1) {
|
|
// If user message has only one model selected, sendPrompt automatically selects it for regeneration
|
|
// If user message has only one model selected, sendPrompt automatically selects it for regeneration
|
|
- await sendPrompt(userPrompt, userMessage.id);
|
|
|
|
|
|
+ await sendPrompt(history, userPrompt, userMessage.id);
|
|
} else {
|
|
} else {
|
|
// If there are multiple models selected, use the model of the response message for regeneration
|
|
// If there are multiple models selected, use the model of the response message for regeneration
|
|
// e.g. many model chat
|
|
// e.g. many model chat
|
|
- await sendPrompt(userPrompt, userMessage.id, {
|
|
|
|
|
|
+ await sendPrompt(history, userPrompt, userMessage.id, {
|
|
modelId: message.model,
|
|
modelId: message.model,
|
|
modelIdx: message.modelIdx
|
|
modelIdx: message.modelIdx
|
|
});
|
|
});
|
|
@@ -1742,7 +1738,7 @@
|
|
.at(0);
|
|
.at(0);
|
|
|
|
|
|
if (model) {
|
|
if (model) {
|
|
- await sendPromptSocket(model, responseMessage.id, _chatId);
|
|
|
|
|
|
+ await sendPromptSocket(history, model, responseMessage.id, _chatId);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
};
|
|
};
|
|
@@ -1785,7 +1781,7 @@
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- await saveChatHandler(_chatId);
|
|
|
|
|
|
+ await saveChatHandler(_chatId, history);
|
|
} else {
|
|
} else {
|
|
console.error(res);
|
|
console.error(res);
|
|
}
|
|
}
|
|
@@ -1794,42 +1790,48 @@
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
- const initChatHandler = async () => {
|
|
|
|
|
|
+ const initChatHandler = async (history) => {
|
|
|
|
+ let _chatId = $chatId;
|
|
|
|
+
|
|
if (!$temporaryChatEnabled) {
|
|
if (!$temporaryChatEnabled) {
|
|
chat = await createNewChat(localStorage.token, {
|
|
chat = await createNewChat(localStorage.token, {
|
|
- id: $chatId,
|
|
|
|
|
|
+ id: _chatId,
|
|
title: $i18n.t('New Chat'),
|
|
title: $i18n.t('New Chat'),
|
|
models: selectedModels,
|
|
models: selectedModels,
|
|
system: $settings.system ?? undefined,
|
|
system: $settings.system ?? undefined,
|
|
params: params,
|
|
params: params,
|
|
history: history,
|
|
history: history,
|
|
- messages: createMessagesList(history.currentId),
|
|
|
|
|
|
+ messages: createMessagesList(history, history.currentId),
|
|
tags: [],
|
|
tags: [],
|
|
timestamp: Date.now()
|
|
timestamp: Date.now()
|
|
});
|
|
});
|
|
|
|
|
|
- currentChatPage.set(1);
|
|
|
|
|
|
+ _chatId = chat.id;
|
|
|
|
+ await chatId.set(_chatId);
|
|
|
|
+
|
|
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
- await chatId.set(chat.id);
|
|
|
|
|
|
+ currentChatPage.set(1);
|
|
|
|
|
|
- window.history.replaceState(history.state, '', `/c/${chat.id}`);
|
|
|
|
|
|
+ window.history.replaceState(history.state, '', `/c/${_chatId}`);
|
|
} else {
|
|
} else {
|
|
|
|
+ _chatId = 'local';
|
|
await chatId.set('local');
|
|
await chatId.set('local');
|
|
}
|
|
}
|
|
await tick();
|
|
await tick();
|
|
|
|
+
|
|
|
|
+ return _chatId;
|
|
};
|
|
};
|
|
|
|
|
|
- const saveChatHandler = async (_chatId) => {
|
|
|
|
|
|
+ const saveChatHandler = async (_chatId, history) => {
|
|
if ($chatId == _chatId) {
|
|
if ($chatId == _chatId) {
|
|
if (!$temporaryChatEnabled) {
|
|
if (!$temporaryChatEnabled) {
|
|
chat = await updateChatById(localStorage.token, _chatId, {
|
|
chat = await updateChatById(localStorage.token, _chatId, {
|
|
models: selectedModels,
|
|
models: selectedModels,
|
|
history: history,
|
|
history: history,
|
|
- messages: createMessagesList(history.currentId),
|
|
|
|
|
|
+ messages: createMessagesList(history, history.currentId),
|
|
params: params,
|
|
params: params,
|
|
files: chatFiles
|
|
files: chatFiles
|
|
});
|
|
});
|
|
-
|
|
|
|
currentChatPage.set(1);
|
|
currentChatPage.set(1);
|
|
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
await chats.set(await getChatList(localStorage.token, $currentChatPage));
|
|
}
|
|
}
|
|
@@ -1933,7 +1935,7 @@
|
|
{/if}
|
|
{/if}
|
|
|
|
|
|
<div class="flex flex-col flex-auto z-10 w-full">
|
|
<div class="flex flex-col flex-auto z-10 w-full">
|
|
- {#if $settings?.landingPageMode === 'chat' || createMessagesList(history.currentId).length > 0}
|
|
|
|
|
|
+ {#if $settings?.landingPageMode === 'chat' || createMessagesList(history, history.currentId).length > 0}
|
|
<div
|
|
<div
|
|
class=" pb-2.5 flex flex-col justify-between w-full flex-auto overflow-auto h-0 max-w-full z-10 scrollbar-hidden"
|
|
class=" pb-2.5 flex flex-col justify-between w-full flex-auto overflow-auto h-0 max-w-full z-10 scrollbar-hidden"
|
|
id="messages-container"
|
|
id="messages-container"
|