2023-03-14 15:42:59 -04:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
const sanitizeHtml = require('sanitize-html');
|
2023-03-15 14:36:17 -04:00
|
|
|
const { titleConvo, citeText, detectCode } = require('../../app/');
|
|
|
|
|
const htmlTagRegex = /(<\/?\s*[a-zA-Z]*\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?)>|<\s*[a-zA-Z]+\s*(?:\s+[a-zA-Z]+\s*=\s*(?:"[^"]*"|'[^']*'))*\s*(?:\/?>|<\/?>))/g;
|
2023-03-14 15:42:59 -04:00
|
|
|
|
2023-03-15 00:50:27 +08:00
|
|
|
const handleError = (res, message) => {
|
|
|
|
|
res.write(`event: error\ndata: ${JSON.stringify(message)}\n\n`);
|
2023-02-25 09:04:32 -05:00
|
|
|
res.end();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sendMessage = (res, message) => {
|
|
|
|
|
if (message.length === 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
res.write(`event: message\ndata: ${JSON.stringify(message)}\n\n`);
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-15 12:47:30 -04:00
|
|
|
const genTitle = async ({ model, text, response }) => {
|
|
|
|
|
let title = 'New Chat';
|
|
|
|
|
try {
|
|
|
|
|
title = await titleConvo({
|
|
|
|
|
model,
|
|
|
|
|
message: text,
|
|
|
|
|
response: JSON.stringify(response?.text)
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
console.log('There was an issue generating title, see error above');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('CONVERSATION TITLE', title);
|
|
|
|
|
return title;
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-14 15:42:59 -04:00
|
|
|
const createOnProgress = () => {
|
|
|
|
|
let i = 0;
|
|
|
|
|
let tokens = '';
|
|
|
|
|
|
|
|
|
|
const progressCallback = async (partial, { res, text, bing = false, ...rest }) => {
|
|
|
|
|
tokens += partial === text ? '' : partial;
|
|
|
|
|
tokens = tokens.replaceAll('[DONE]', '');
|
2023-03-14 16:05:46 -04:00
|
|
|
|
|
|
|
|
if (tokens.match(/^\n/)) {
|
|
|
|
|
tokens = tokens.replace(/^\n/, '');
|
2023-03-14 15:42:59 -04:00
|
|
|
}
|
2023-03-15 14:36:17 -04:00
|
|
|
|
|
|
|
|
// const htmlTags = tokens.match(htmlTagRegex);
|
|
|
|
|
// if (tokens.includes('```') && htmlTags && htmlTags.length > 0) {
|
|
|
|
|
// htmlTags.forEach((tag) => {
|
|
|
|
|
// const sanitizedTag = sanitizeHtml(tag);
|
|
|
|
|
// tokens = tokens.replaceAll(tag, sanitizedTag);
|
|
|
|
|
// });
|
2023-03-14 16:05:46 -04:00
|
|
|
// }
|
2023-03-14 15:42:59 -04:00
|
|
|
|
|
|
|
|
if (bing) {
|
|
|
|
|
tokens = citeText(tokens, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendMessage(res, { text: tokens, message: true, initial: i === 0, ...rest });
|
|
|
|
|
i++;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const onProgress = (model, opts) => {
|
|
|
|
|
const bingModels = new Set(['bingai', 'sydney']);
|
|
|
|
|
return _.partialRight(progressCallback, { ...opts, bing: bingModels.has(model) });
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return onProgress;
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-14 16:05:46 -04:00
|
|
|
const handleText = async (input) => {
|
|
|
|
|
let text = input;
|
|
|
|
|
text = await detectCode(text);
|
2023-03-15 14:36:17 -04:00
|
|
|
// const htmlTags = text.match(htmlTagRegex);
|
|
|
|
|
// if (text.includes('```') && htmlTags && htmlTags.length > 0) {
|
|
|
|
|
// htmlTags.forEach((tag) => {
|
|
|
|
|
// const sanitizedTag = sanitizeHtml(tag);
|
|
|
|
|
// text = text.replaceAll(tag, sanitizedTag);
|
|
|
|
|
// });
|
2023-03-14 16:05:46 -04:00
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
return text;
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-15 12:47:30 -04:00
|
|
|
module.exports = { handleError, sendMessage, createOnProgress, genTitle, handleText };
|