mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 17:00:15 +01:00
adds server and client error handling, clear convos in progress
This commit is contained in:
parent
511ac948b4
commit
51d93da3f8
9 changed files with 98 additions and 48 deletions
|
|
@ -27,25 +27,25 @@ const Conversation =
|
|||
|
||||
module.exports = {
|
||||
saveConvo: async ({ conversationId, parentMessageId, title }) => {
|
||||
try {
|
||||
const messages = await getMessages({ conversationId });
|
||||
const update = { parentMessageId, messages };
|
||||
if (title) {
|
||||
update.title = title;
|
||||
}
|
||||
|
||||
await Conversation.findOneAndUpdate(
|
||||
return await Conversation.findOneAndUpdate(
|
||||
{ conversationId },
|
||||
{ $set: update },
|
||||
{ new: true, upsert: true }
|
||||
).exec();
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
return { message: 'Error saving conversation' };
|
||||
}
|
||||
},
|
||||
getConvos: async () => await Conversation.find({}).exec(),
|
||||
deleteConvos: async (filter) => {
|
||||
// const filter = {};
|
||||
|
||||
// if (!!conversationId) {
|
||||
// filter = conversationId;
|
||||
// }
|
||||
|
||||
let deleteCount = await Conversation.deleteMany(filter).exec();
|
||||
deleteCount.messages = await deleteMessages(filter);
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ const Message = mongoose.models.Message || mongoose.model('Message', messageSche
|
|||
|
||||
module.exports = {
|
||||
saveMessage: async ({ id, conversationId, parentMessageId, sender, text }) => {
|
||||
try {
|
||||
await Message.create({
|
||||
id,
|
||||
conversationId,
|
||||
|
|
@ -39,6 +40,11 @@ module.exports = {
|
|||
sender,
|
||||
text
|
||||
});
|
||||
return { id, conversationId, parentMessageId, sender, text };
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return { message: 'Error saving message' };
|
||||
}
|
||||
},
|
||||
getMessages: async (filter) => await Message.find(filter).exec(),
|
||||
deleteMessages: async (filter) => await Message.deleteMany(filter).exec()
|
||||
|
|
|
|||
|
|
@ -52,6 +52,8 @@ app.post('/ask', async (req, res) => {
|
|||
const userMessageId = crypto.randomUUID();
|
||||
let userMessage = { id: userMessageId, sender: 'User', text };
|
||||
|
||||
console.log(userMessage, req.body);
|
||||
|
||||
res.writeHead(200, {
|
||||
Connection: 'keep-alive',
|
||||
'Content-Type': 'text/event-stream',
|
||||
|
|
@ -61,7 +63,7 @@ app.post('/ask', async (req, res) => {
|
|||
});
|
||||
|
||||
// res.write(`event: message\ndata: ${JSON.stringify('')}\n\n`);
|
||||
|
||||
try {
|
||||
let i = 0;
|
||||
const progressCallback = async (partial) => {
|
||||
// console.log('partial', partial);
|
||||
|
|
@ -69,14 +71,15 @@ app.post('/ask', async (req, res) => {
|
|||
userMessage.parentMessageId = parentMessageId ? parentMessageId : partial.id;
|
||||
userMessage.conversationId = conversationId ? conversationId : partial.conversationId;
|
||||
await saveMessage(userMessage);
|
||||
res.write(`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`);
|
||||
res.write(
|
||||
`event: message\ndata: ${JSON.stringify({ ...partial, initial: true })}\n\n`
|
||||
);
|
||||
i++;
|
||||
}
|
||||
const data = JSON.stringify({ ...partial, message: true });
|
||||
res.write(`event: message\ndata: ${data}\n\n`);
|
||||
};
|
||||
|
||||
try {
|
||||
let gptResponse = await ask(text, progressCallback, { parentMessageId, conversationId });
|
||||
if (!!parentMessageId) {
|
||||
gptResponse = { ...gptResponse, parentMessageId };
|
||||
|
|
@ -84,6 +87,12 @@ app.post('/ask', async (req, res) => {
|
|||
gptResponse.title = await titleConversation(text, gptResponse.text);
|
||||
}
|
||||
|
||||
if (gptResponse.text.includes('2023')) {
|
||||
res.status(500).write('event: error\ndata: empty string error?');
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
gptResponse.sender = 'GPT';
|
||||
await saveMessage(gptResponse);
|
||||
await saveConvo(gptResponse);
|
||||
|
|
@ -91,8 +100,9 @@ app.post('/ask', async (req, res) => {
|
|||
res.write(`event: message\ndata: ${JSON.stringify(gptResponse)}\n\n`);
|
||||
res.end();
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).send(error);
|
||||
console.log(error);
|
||||
res.status(500).write('event: error\ndata: ' + error.message);
|
||||
res.end();
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -18,7 +18,10 @@ export default function ClearConvos() {
|
|||
}
|
||||
);
|
||||
|
||||
const clickHandler = () => trigger({});
|
||||
const clickHandler = () => {
|
||||
console.log('Clearing conversations...');
|
||||
trigger({});
|
||||
};
|
||||
|
||||
return (
|
||||
<NavLink
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ export default function NavLink({ svg, text, clickHandler }) {
|
|||
|
||||
if (clickHandler) {
|
||||
props.onClick = clickHandler;
|
||||
console.log('clickHandler: ', clickHandler);
|
||||
}
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import React from 'react';
|
||||
import { useSelector } from 'react-redux';
|
||||
|
||||
export default function Message({ sender, text, last = false}) {
|
||||
export default function Message({ sender, text, last = false, error = false }) {
|
||||
const { isSubmitting } = useSelector((state) => state.submit);
|
||||
const props = {
|
||||
className:
|
||||
|
|
@ -18,10 +18,20 @@ export default function Message({ sender, text, last = false}) {
|
|||
<div className="m-auto flex gap-4 p-4 text-base md:max-w-2xl md:gap-6 md:py-6 lg:max-w-2xl lg:px-0 xl:max-w-3xl">
|
||||
<strong className="relative flex w-[30px] flex-col items-end">{sender}:</strong>
|
||||
<div className="relative flex w-[calc(100%-50px)] flex-col gap-1 whitespace-pre-wrap md:gap-3 lg:w-[calc(100%-115px)]">
|
||||
<div className="flex flex-grow flex-col gap-3">
|
||||
{!!error ? (
|
||||
<div className="flex flex min-h-[20px] flex-row flex-col items-start gap-4 gap-2 whitespace-pre-wrap text-red-500">
|
||||
<div className="rounded-md border border-red-500 bg-red-500/10 py-2 px-3 text-sm text-gray-600 dark:text-gray-100">
|
||||
{text}
|
||||
</div>
|
||||
</div>
|
||||
) : (
|
||||
<span>
|
||||
{text}
|
||||
{isSubmitting && last && sender === 'GPT' && <span className="blink">█</span>}
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ export default function Messages({ messages }) {
|
|||
sender={message.sender}
|
||||
text={message.text}
|
||||
last={i === messages.length - 1}
|
||||
error={!!message.error ? true : false}
|
||||
/>
|
||||
))}
|
||||
<div ref={messagesEndRef} />
|
||||
|
|
|
|||
|
|
@ -35,8 +35,20 @@ export default function TextChat({ messages, reloadConvos }) {
|
|||
reloadConvos();
|
||||
dispatch(setSubmitState(false));
|
||||
};
|
||||
|
||||
const errorHandler = (data) => {
|
||||
console.log('Error:', data);
|
||||
const errorResponse = {
|
||||
...initialResponse,
|
||||
text: 'An error occurred. Please try again in a few moments.',
|
||||
error: true
|
||||
};
|
||||
dispatch(setSubmitState(false));
|
||||
dispatch(setMessages([...messages, currentMsg, errorResponse]));
|
||||
return;
|
||||
};
|
||||
console.log('User Input:', payload);
|
||||
handleSubmit(payload, messageHandler, convo, convoHandler);
|
||||
handleSubmit({ text: payload, messageHandler, convo, convoHandler, errorHandler });
|
||||
};
|
||||
|
||||
const handleKeyPress = (e) => {
|
||||
|
|
|
|||
|
|
@ -1,6 +1,12 @@
|
|||
import { SSE } from '../../app/sse';
|
||||
|
||||
export default function handleSubmit(text, messageHandler, convo, convoHandler) {
|
||||
export default function handleSubmit({
|
||||
text,
|
||||
convo,
|
||||
messageHandler,
|
||||
convoHandler,
|
||||
errorHandler
|
||||
}) {
|
||||
let payload = { text };
|
||||
if (convo.conversationId && convo.parentMessageId) {
|
||||
payload = {
|
||||
|
|
@ -34,7 +40,8 @@ export default function handleSubmit(text, messageHandler, convo, convoHandler)
|
|||
events.onerror = function (e) {
|
||||
console.log(e, 'error in opening conn.');
|
||||
events.close();
|
||||
errorHandler(e);
|
||||
};
|
||||
|
||||
events.stream();
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue