LibreChat/client/src/utils/handleSubmit.js

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-03-06 10:15:07 -05:00
import { SSE } from './sse';
2023-02-23 16:32:08 -05:00
// const newLineRegex = /^\n+/;
2023-02-06 21:17:46 -05:00
export default function handleSubmit({
model,
text,
convo,
messageHandler,
convoHandler,
2023-03-03 15:52:06 -05:00
errorHandler,
chatGptLabel,
promptPrefix
}) {
const endpoint = `/api/ask`;
2023-03-03 15:52:06 -05:00
let payload = { model, text, chatGptLabel, promptPrefix };
2023-02-06 21:17:46 -05:00
if (convo.conversationId && convo.parentMessageId) {
payload = {
...payload,
conversationId: convo.conversationId,
parentMessageId: convo.parentMessageId
};
}
const isBing = model === 'bingai' || model === 'sydney';
if (isBing && convo.conversationId) {
2023-02-20 21:52:08 -05:00
payload = {
...payload,
jailbreakConversationId: convo.jailbreakConversationId,
2023-02-20 21:52:08 -05:00
conversationId: convo.conversationId,
conversationSignature: convo.conversationSignature,
clientId: convo.clientId,
invocationId: convo.invocationId,
};
}
let server = endpoint;
server = model === 'bingai' ? server + '/bing' : server;
server = model === 'sydney' ? server + '/sydney' : server;
const events = new SSE(server, {
2023-02-06 21:17:46 -05:00
payload: JSON.stringify(payload),
headers: { 'Content-Type': 'application/json' }
});
events.onopen = function () {
console.log('connection is opened');
};
events.onmessage = function (e) {
const data = JSON.parse(e.data);
2023-02-23 16:32:08 -05:00
let text = data.text || data.response;
if (data.message) {
messageHandler(text);
}
if (data.final) {
2023-02-06 21:17:46 -05:00
convoHandler(data);
console.log('final', data);
2023-02-06 21:17:46 -05:00
} else {
// console.log('dataStream', data);
2023-02-06 21:17:46 -05:00
}
};
events.onerror = function (e) {
console.log('error in opening conn.');
2023-02-06 21:17:46 -05:00
events.close();
errorHandler(e);
2023-02-06 21:17:46 -05:00
};
events.stream();
}