mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
feat: return error as a error message, not only text
This commit is contained in:
parent
7168498543
commit
d73375958b
5 changed files with 30 additions and 25 deletions
|
|
@ -19,7 +19,7 @@ router.use('/sydney', askSydney);
|
|||
router.post('/', async (req, res) => {
|
||||
let { model, text, parentMessageId, conversationId: oldConversationId , chatGptLabel, promptPrefix } = req.body;
|
||||
if (text.length === 0) {
|
||||
return handleError(res, 'Prompt empty or too short');
|
||||
return handleError(res, { text: 'Prompt empty or too short' });
|
||||
}
|
||||
|
||||
const conversationId = oldConversationId || crypto.randomUUID();
|
||||
|
|
@ -135,7 +135,7 @@ router.post('/', async (req, res) => {
|
|||
messageId: crypto.randomUUID(), sender: model,
|
||||
conversationId, parentMessageId: userMessageId,
|
||||
error: true, text: 'Prompt empty or too short'});
|
||||
return handleError(res, 'Prompt empty or too short');
|
||||
return handleError(res, { text: 'Prompt empty or too short' });
|
||||
}
|
||||
|
||||
gptResponse.sender = model === 'chatgptCustom' ? chatGptLabel : model;
|
||||
|
|
@ -175,11 +175,12 @@ router.post('/', async (req, res) => {
|
|||
} catch (error) {
|
||||
console.log(error);
|
||||
// await deleteMessages({ messageId: userMessageId });
|
||||
await saveMessage({
|
||||
const errorMessage = {
|
||||
messageId: crypto.randomUUID(), sender: model,
|
||||
conversationId, parentMessageId: userMessageId,
|
||||
error: true, text: error.message});
|
||||
handleError(res, error.message);
|
||||
conversationId, parentMessageId: overrideParentMessageId || userMessageId,
|
||||
error: true, text: error.message}
|
||||
await saveMessage(errorMessage);
|
||||
handleError(res, errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ const citationRegex = /\[\^\d+?\^]/g;
|
|||
router.post('/', async (req, res) => {
|
||||
const { model, text, parentMessageId, conversationId: oldConversationId, ...convo } = req.body;
|
||||
if (text.length === 0) {
|
||||
return handleError(res, 'Prompt empty or too short');
|
||||
return handleError(res, { text: 'Prompt empty or too short' });
|
||||
}
|
||||
|
||||
const conversationId = oldConversationId || crypto.randomUUID();
|
||||
|
|
@ -112,11 +112,12 @@ router.post('/', async (req, res) => {
|
|||
} catch (error) {
|
||||
console.log(error);
|
||||
// await deleteMessages({ messageId: userMessageId });
|
||||
await saveMessage({
|
||||
const errorMessage = {
|
||||
messageId: crypto.randomUUID(), sender: model,
|
||||
conversationId, parentMessageId: userMessageId,
|
||||
error: true, text: error.message});
|
||||
handleError(res, error.message);
|
||||
conversationId, parentMessageId: overrideParentMessageId || userMessageId,
|
||||
error: true, text: error.message}
|
||||
await saveMessage(errorMessage);
|
||||
handleError(res, errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@ const citationRegex = /\[\^\d+?\^]/g;
|
|||
router.post('/', async (req, res) => {
|
||||
const { model, text, parentMessageId, conversationId: oldConversationId, ...convo } = req.body;
|
||||
if (text.length === 0) {
|
||||
return handleError(res, 'Prompt empty or too short');
|
||||
return handleError(res, { text: 'Prompt empty or too short' });
|
||||
}
|
||||
|
||||
const conversationId = oldConversationId || crypto.randomUUID();
|
||||
|
|
@ -123,11 +123,12 @@ router.post('/', async (req, res) => {
|
|||
} catch (error) {
|
||||
console.log(error);
|
||||
// await deleteMessages({ messageId: userMessageId });
|
||||
await saveMessage({
|
||||
const errorMessage = {
|
||||
messageId: crypto.randomUUID(), sender: model,
|
||||
conversationId, parentMessageId: userMessageId,
|
||||
error: true, text: error.message});
|
||||
handleError(res, error.message);
|
||||
conversationId, parentMessageId: overrideParentMessageId || userMessageId,
|
||||
error: true, text: error.message}
|
||||
await saveMessage(errorMessage);
|
||||
handleError(res, errorMessage);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
const handleError = (res, errorMessage) => {
|
||||
res.status(500).write(`event: error\ndata: ${errorMessage}`);
|
||||
const handleError = (res, message) => {
|
||||
res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`);
|
||||
res.end();
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -128,18 +128,17 @@ export default function TextChat({ messages }) {
|
|||
dispatch(setSubmitState(false));
|
||||
};
|
||||
|
||||
const errorHandler = (event, currentState, currentMsg) => {
|
||||
const errorHandler = (data, currentState, currentMsg) => {
|
||||
const { initialResponse, messages, _currentMsg, message } = currentState;
|
||||
console.log('Error:', event);
|
||||
console.log('Error:', data);
|
||||
const errorResponse = {
|
||||
...initialResponse,
|
||||
text: `${event.data}`,
|
||||
...data,
|
||||
error: true,
|
||||
parentMessageId: currentMsg?.messageId,
|
||||
};
|
||||
setErrorMessage(event.data);
|
||||
setErrorMessage(data?.text);
|
||||
dispatch(setSubmitState(false));
|
||||
dispatch(setMessages([...messages.slice(0, -2), currentMsg, errorResponse]));
|
||||
dispatch(setMessages([...messages, currentMsg, errorResponse]));
|
||||
dispatch(setText(message?.text));
|
||||
dispatch(setError(true));
|
||||
return;
|
||||
|
|
@ -265,7 +264,10 @@ export default function TextChat({ messages }) {
|
|||
events.onerror = function (e) {
|
||||
console.log('error in opening conn.');
|
||||
events.close();
|
||||
errorHandler(e, currentState, currentMsg);
|
||||
|
||||
const data = JSON.parse(e.data);
|
||||
|
||||
errorHandler(data, currentState, currentMsg);
|
||||
};
|
||||
|
||||
events.stream();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue