2023-02-06 21:17:46 -05:00
|
|
|
import { SSE } from '../../app/sse';
|
2023-02-15 12:29:56 -05:00
|
|
|
const endpoint = 'http://localhost:3050/ask';
|
2023-02-06 21:17:46 -05:00
|
|
|
|
2023-02-08 00:02:29 -05:00
|
|
|
export default function handleSubmit({
|
2023-02-13 21:15:28 -05:00
|
|
|
model,
|
2023-02-08 00:02:29 -05:00
|
|
|
text,
|
|
|
|
|
convo,
|
|
|
|
|
messageHandler,
|
|
|
|
|
convoHandler,
|
|
|
|
|
errorHandler
|
|
|
|
|
}) {
|
2023-02-13 21:15:28 -05:00
|
|
|
let payload = { model, text };
|
2023-02-06 21:17:46 -05:00
|
|
|
if (convo.conversationId && convo.parentMessageId) {
|
|
|
|
|
payload = {
|
|
|
|
|
...payload,
|
|
|
|
|
conversationId: convo.conversationId,
|
|
|
|
|
parentMessageId: convo.parentMessageId
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-15 12:29:56 -05:00
|
|
|
const server = model === 'bingai' ? endpoint + '/bing' : endpoint;
|
|
|
|
|
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-15 12:29:56 -05:00
|
|
|
const text = data.text || data.response;
|
2023-02-06 21:17:46 -05:00
|
|
|
if (!!data.message) {
|
2023-02-15 12:29:56 -05:00
|
|
|
messageHandler(text.replace(/^\n/, ''));
|
2023-02-12 22:57:59 -05:00
|
|
|
} else if (!!data.final) {
|
2023-02-06 21:17:46 -05:00
|
|
|
console.log(data);
|
|
|
|
|
convoHandler(data);
|
|
|
|
|
} else {
|
|
|
|
|
console.log('initial', data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
events.onerror = function (e) {
|
2023-02-08 09:15:47 -05:00
|
|
|
console.log('error in opening conn.');
|
2023-02-06 21:17:46 -05:00
|
|
|
events.close();
|
2023-02-08 00:02:29 -05:00
|
|
|
errorHandler(e);
|
2023-02-06 21:17:46 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
events.stream();
|
2023-02-08 00:02:29 -05:00
|
|
|
}
|