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