This commit is contained in:
Timothy Jaeryang Baek 2025-02-03 18:36:49 -08:00
parent aa7184ae0d
commit 8fdd3024f7
3 changed files with 31 additions and 39 deletions

View File

@ -901,7 +901,7 @@ async def process_chat_response(
if message: if message:
messages = get_message_list(message_map, message.get("id")) messages = get_message_list(message_map, message.get("id"))
if tasks: if tasks and messages:
if TASKS.TITLE_GENERATION in tasks: if TASKS.TITLE_GENERATION in tasks:
if tasks[TASKS.TITLE_GENERATION]: if tasks[TASKS.TITLE_GENERATION]:
res = await generate_title( res = await generate_title(

View File

@ -20,7 +20,7 @@ def get_message_list(messages, message_id):
current_message = messages.get(message_id) current_message = messages.get(message_id)
if not current_message: if not current_message:
return f"Message ID {message_id} not found in the history." return None
# Reconstruct the chain by following the parentId links # Reconstruct the chain by following the parentId links
message_list = [] message_list = []

View File

@ -827,14 +827,14 @@
} }
}; };
const createMessagesList = (history, responseMessageId) => { const createMessagesList = (responseMessageId) => {
if (responseMessageId === null) { if (responseMessageId === null) {
return []; return [];
} }
const message = history.messages[responseMessageId]; const message = history.messages[responseMessageId];
if (message?.parentId) { if (message?.parentId) {
return [...createMessagesList(history, message.parentId), message]; return [...createMessagesList(message.parentId), message];
} else { } else {
return [message]; return [message];
} }
@ -896,7 +896,7 @@
}; };
const chatActionHandler = async (chatId, actionId, modelId, responseMessageId, event = null) => { const chatActionHandler = async (chatId, actionId, modelId, responseMessageId, event = null) => {
const messages = createMessagesList(history, responseMessageId); const messages = createMessagesList(responseMessageId);
const res = await chatAction(localStorage.token, actionId, { const res = await chatAction(localStorage.token, actionId, {
model: modelId, model: modelId,
@ -965,7 +965,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, history.currentId); const messages = createMessagesList(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();
@ -1210,12 +1210,7 @@
); );
history.messages[message.id] = message; history.messages[message.id] = message;
await chatCompletedHandler( await chatCompletedHandler(chatId, message.model, message.id, createMessagesList(message.id));
chatId,
message.model,
message.id,
createMessagesList(history, message.id)
);
} }
console.log(data); console.log(data);
@ -1231,7 +1226,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, history.currentId); const messages = createMessagesList(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 : ''
); );
@ -1330,7 +1325,7 @@
saveSessionSelectedModels(); saveSessionSelectedModels();
sendPrompt(userPrompt, userMessageId, { newChat: true }); await sendPrompt(userPrompt, userMessageId, { newChat: true });
}; };
const sendPrompt = async ( const sendPrompt = async (
@ -1338,6 +1333,17 @@
parentId: string, parentId: string,
{ modelId = null, modelIdx = null, newChat = false } = {} { modelId = null, modelIdx = null, newChat = false } = {}
) => { ) => {
// Create new chat if newChat is true and first user message
if (
newChat &&
history.messages[history.currentId].parentId === null &&
history.messages[history.currentId].role === 'user'
) {
await initChatHandler();
} else {
await saveChatHandler($chatId);
}
// 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]
@ -1347,19 +1353,6 @@
// Create response messages for each selected model // Create response messages for each selected model
const responseMessageIds: Record<PropertyKey, string> = {}; const responseMessageIds: Record<PropertyKey, string> = {};
const _chatId = JSON.parse(JSON.stringify($chatId));
// Create new chat if newChat is true and first user message
if (
newChat &&
history.messages[history.currentId].parentId === null &&
history.messages[history.currentId].role === 'user'
) {
await initChatHandler();
} else {
await saveChatHandler(_chatId);
}
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);
@ -1397,20 +1390,19 @@
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);
await tick();
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(history, parentId); const messages = createMessagesList(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')
); );
if (hasImages && !(model.info?.meta?.capabilities?.vision ?? true)) { if (hasImages && !(model.info?.meta?.capabilities?.vision ?? true)) {
@ -1452,7 +1444,7 @@
const chatEventEmitter = await getChatEventEmitter(model.id, _chatId); const chatEventEmitter = await getChatEventEmitter(model.id, _chatId);
scrollToBottom(); scrollToBottom();
await sendPromptSocket(history, model, responseMessageId, _chatId); await sendPromptSocket(model, responseMessageId, _chatId);
if (chatEventEmitter) clearInterval(chatEventEmitter); if (chatEventEmitter) clearInterval(chatEventEmitter);
} else { } else {
@ -1465,7 +1457,7 @@
chats.set(await getChatList(localStorage.token, $currentChatPage)); chats.set(await getChatList(localStorage.token, $currentChatPage));
}; };
const sendPromptSocket = async (history, model, responseMessageId, _chatId) => { const sendPromptSocket = async (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];
@ -1515,7 +1507,7 @@
}` }`
} }
: undefined, : undefined,
...createMessagesList(history, responseMessageId).map((message) => ({ ...createMessagesList(responseMessageId).map((message) => ({
...message, ...message,
content: removeDetails(message.content, ['reasoning', 'code_interpreter']) content: removeDetails(message.content, ['reasoning', 'code_interpreter'])
})) }))
@ -1750,7 +1742,7 @@
.at(0); .at(0);
if (model) { if (model) {
await sendPromptSocket(history, model, responseMessage.id, _chatId); await sendPromptSocket(model, responseMessage.id, _chatId);
} }
} }
}; };
@ -1811,7 +1803,7 @@
system: $settings.system ?? undefined, system: $settings.system ?? undefined,
params: params, params: params,
history: history, history: history,
messages: createMessagesList(history, history.currentId), messages: createMessagesList(history.currentId),
tags: [], tags: [],
timestamp: Date.now() timestamp: Date.now()
}); });
@ -1833,7 +1825,7 @@
chat = await updateChatById(localStorage.token, _chatId, { chat = await updateChatById(localStorage.token, _chatId, {
models: selectedModels, models: selectedModels,
history: history, history: history,
messages: createMessagesList(history, history.currentId), messages: createMessagesList(history.currentId),
params: params, params: params,
files: chatFiles files: chatFiles
}); });
@ -1941,7 +1933,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, history.currentId).length > 0} {#if $settings?.landingPageMode === 'chat' || createMessagesList(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"