2023-02-12 16:38:33 -05:00
|
|
|
const express = require('express');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const router = express.Router();
|
2023-02-25 09:04:32 -05:00
|
|
|
const askBing = require('./askBing');
|
2023-03-08 19:47:23 -05:00
|
|
|
const askSydney = require('./askSydney');
|
2023-03-15 15:21:04 -04:00
|
|
|
const { titleConvo, askClient, browserClient, customClient } = require('../../app/');
|
2023-03-13 14:04:47 +08:00
|
|
|
const { getConvo, saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
2023-03-15 15:21:04 -04:00
|
|
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
2023-03-15 00:54:50 +08:00
|
|
|
const { getMessages } = require('../../models/Message');
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-08 21:06:58 -05:00
|
|
|
router.use('/bing', askBing);
|
|
|
|
|
router.use('/sydney', askSydney);
|
2023-02-15 12:29:56 -05:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
router.post('/', async (req, res) => {
|
2023-03-14 20:21:41 -04:00
|
|
|
let { model, text, parentMessageId, conversationId: oldConversationId, ...convo } = req.body;
|
2023-03-11 12:10:00 -05:00
|
|
|
if (text.length === 0) {
|
2023-03-15 00:50:27 +08:00
|
|
|
return handleError(res, { text: 'Prompt empty or too short' });
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-13 13:11:53 +08:00
|
|
|
const conversationId = oldConversationId || crypto.randomUUID();
|
|
|
|
|
|
2023-03-13 12:35:55 +08:00
|
|
|
const userMessageId = crypto.randomUUID();
|
2023-03-14 20:21:41 -04:00
|
|
|
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000';
|
2023-03-13 12:35:55 +08:00
|
|
|
let userMessage = {
|
2023-03-14 20:21:41 -04:00
|
|
|
messageId: userMessageId,
|
|
|
|
|
sender: 'User',
|
|
|
|
|
text,
|
2023-03-15 00:54:50 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-03-14 20:21:41 -04:00
|
|
|
conversationId,
|
|
|
|
|
isCreatedByUser: true
|
2023-03-13 12:35:55 +08:00
|
|
|
};
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
console.log('ask log', {
|
|
|
|
|
model,
|
|
|
|
|
...userMessage,
|
2023-03-15 00:54:50 +08:00
|
|
|
...convo
|
2023-03-03 15:52:06 -05:00
|
|
|
});
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
await saveMessage(userMessage);
|
2023-03-15 16:56:47 +08:00
|
|
|
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
|
2023-03-15 00:54:50 +08:00
|
|
|
|
|
|
|
|
return await ask({
|
2023-03-14 20:21:41 -04:00
|
|
|
userMessage,
|
2023-03-15 00:54:50 +08:00
|
|
|
model,
|
|
|
|
|
convo,
|
|
|
|
|
preSendRequest: true,
|
2023-03-14 20:21:41 -04:00
|
|
|
req,
|
|
|
|
|
res
|
2023-03-15 00:54:50 +08:00
|
|
|
});
|
2023-03-14 20:21:41 -04:00
|
|
|
});
|
2023-03-15 00:54:50 +08:00
|
|
|
|
|
|
|
|
router.post('/regenerate', async (req, res) => {
|
2023-03-14 20:21:41 -04:00
|
|
|
const { model } = req.body;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
const oldUserMessage = await getMessages({ messageId: req.body });
|
2023-03-15 00:54:50 +08:00
|
|
|
|
|
|
|
|
if (oldUserMessage) {
|
2023-03-14 20:21:41 -04:00
|
|
|
const convo = await getConvo(userMessage?.conversationId);
|
2023-03-15 00:54:50 +08:00
|
|
|
|
|
|
|
|
const userMessageId = crypto.randomUUID();
|
|
|
|
|
|
|
|
|
|
let userMessage = {
|
|
|
|
|
...userMessage,
|
2023-03-14 20:21:41 -04:00
|
|
|
messageId: userMessageId
|
2023-03-15 00:54:50 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
console.log('ask log for regeneration', {
|
|
|
|
|
model,
|
|
|
|
|
...userMessage,
|
|
|
|
|
...convo
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return await ask({
|
2023-03-14 20:21:41 -04:00
|
|
|
userMessage,
|
2023-03-15 00:54:50 +08:00
|
|
|
model,
|
|
|
|
|
convo,
|
|
|
|
|
preSendRequest: false,
|
2023-03-14 20:21:41 -04:00
|
|
|
req,
|
|
|
|
|
res
|
2023-03-15 00:54:50 +08:00
|
|
|
});
|
2023-03-14 20:21:41 -04:00
|
|
|
} else return handleError(res, { text: 'Parent message not found' });
|
2023-03-15 00:54:50 +08:00
|
|
|
});
|
|
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
const ask = async ({
|
|
|
|
|
userMessage,
|
2023-03-15 00:54:50 +08:00
|
|
|
overrideParentMessageId = null,
|
|
|
|
|
model,
|
|
|
|
|
convo,
|
|
|
|
|
preSendRequest = true,
|
2023-03-14 20:21:41 -04:00
|
|
|
req,
|
|
|
|
|
res
|
2023-03-15 00:54:50 +08:00
|
|
|
}) => {
|
2023-03-14 20:21:41 -04:00
|
|
|
let {
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId: userParentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
messageId: userMessageId
|
|
|
|
|
} = userMessage;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
let client;
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-03-03 15:52:06 -05:00
|
|
|
if (model === 'chatgpt') {
|
|
|
|
|
client = askClient;
|
|
|
|
|
} else if (model === 'chatgptCustom') {
|
|
|
|
|
client = customClient;
|
|
|
|
|
} else {
|
|
|
|
|
client = browserClient;
|
|
|
|
|
}
|
2023-03-01 21:39:57 -05:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
res.writeHead(200, {
|
|
|
|
|
Connection: 'keep-alive',
|
|
|
|
|
'Content-Type': 'text/event-stream',
|
|
|
|
|
'Cache-Control': 'no-cache, no-transform',
|
|
|
|
|
'Access-Control-Allow-Origin': '*',
|
|
|
|
|
'X-Accel-Buffering': 'no'
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
if (preSendRequest) sendMessage(res, { message: userMessage, created: true });
|
2023-03-13 13:11:53 +08:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
try {
|
2023-03-14 15:42:59 -04:00
|
|
|
const progressCallback = createOnProgress();
|
2023-03-01 21:39:57 -05:00
|
|
|
let gptResponse = await client({
|
2023-02-13 21:15:28 -05:00
|
|
|
text,
|
2023-03-14 20:21:41 -04:00
|
|
|
onProgress: progressCallback.call(null, model, { res, text }),
|
2023-02-13 21:15:28 -05:00
|
|
|
convo: {
|
2023-03-13 12:35:55 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-03-15 00:54:50 +08:00
|
|
|
conversationId,
|
|
|
|
|
...convo
|
2023-03-03 15:52:06 -05:00
|
|
|
},
|
2023-03-15 00:54:50 +08:00
|
|
|
...convo
|
2023-02-13 13:32:54 -05:00
|
|
|
});
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-03-01 21:39:57 -05:00
|
|
|
console.log('CLIENT RESPONSE', gptResponse);
|
2023-02-12 22:24:36 -05:00
|
|
|
|
2023-02-13 13:32:54 -05:00
|
|
|
if (!gptResponse.parentMessageId) {
|
2023-02-12 22:24:36 -05:00
|
|
|
gptResponse.text = gptResponse.response;
|
2023-03-13 12:35:55 +08:00
|
|
|
// gptResponse.id = gptResponse.messageId;
|
2023-03-15 00:54:50 +08:00
|
|
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
2023-02-13 21:15:28 -05:00
|
|
|
userMessage.conversationId = conversationId
|
|
|
|
|
? conversationId
|
|
|
|
|
: gptResponse.conversationId;
|
2023-02-13 13:32:54 -05:00
|
|
|
await saveMessage(userMessage);
|
2023-02-12 22:24:36 -05:00
|
|
|
delete gptResponse.response;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
if (
|
2023-02-13 13:32:54 -05:00
|
|
|
(gptResponse.text.includes('2023') && !gptResponse.text.trim().includes(' ')) ||
|
|
|
|
|
gptResponse.text.toLowerCase().includes('no response') ||
|
|
|
|
|
gptResponse.text.toLowerCase().includes('no answer')
|
2023-02-12 16:38:33 -05:00
|
|
|
) {
|
2023-03-14 20:21:41 -04:00
|
|
|
await saveMessage({
|
|
|
|
|
messageId: crypto.randomUUID(),
|
|
|
|
|
sender: model,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId: overrideParentMessageId || userMessageId,
|
|
|
|
|
error: true,
|
|
|
|
|
text: 'Prompt empty or too short'
|
|
|
|
|
});
|
2023-03-15 00:50:27 +08:00
|
|
|
return handleError(res, { text: 'Prompt empty or too short' });
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
gptResponse.sender = model === 'chatgptCustom' ? convo.chatGptLabel : model;
|
2023-03-14 20:21:41 -04:00
|
|
|
gptResponse.model = model;
|
2023-03-13 12:35:55 +08:00
|
|
|
// gptResponse.final = true;
|
2023-03-15 15:44:48 -04:00
|
|
|
gptResponse.text = await handleText(gptResponse);
|
2023-03-04 17:39:06 -05:00
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
if (convo.chatGptLabel?.length > 0 && model === 'chatgptCustom') {
|
|
|
|
|
gptResponse.chatGptLabel = convo.chatGptLabel;
|
2023-03-04 17:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
if (convo.promptPrefix?.length > 0 && model === 'chatgptCustom') {
|
|
|
|
|
gptResponse.promptPrefix = convo.promptPrefix;
|
2023-03-04 17:39:06 -05:00
|
|
|
}
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
// override the parentMessageId, for the regeneration.
|
2023-03-14 20:21:41 -04:00
|
|
|
gptResponse.parentMessageId = overrideParentMessageId || userMessageId;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
await saveMessage(gptResponse);
|
2023-03-14 01:24:43 +08:00
|
|
|
await saveConvo(req?.session?.user?.username, gptResponse);
|
2023-03-13 12:35:55 +08:00
|
|
|
sendMessage(res, {
|
2023-03-15 16:56:47 +08:00
|
|
|
title: await getConvoTitle(req?.session?.user?.username, conversationId),
|
2023-03-14 20:21:41 -04:00
|
|
|
final: true,
|
|
|
|
|
requestMessage: userMessage,
|
2023-03-13 12:35:55 +08:00
|
|
|
responseMessage: gptResponse
|
|
|
|
|
});
|
2023-02-12 16:38:33 -05:00
|
|
|
res.end();
|
2023-03-14 11:42:35 +08:00
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
if (userParentMessageId == '00000000-0000-0000-0000-000000000000') {
|
2023-03-15 15:21:04 -04:00
|
|
|
const title = await titleConvo({ model, text, response: gptResponse });
|
2023-03-15 00:53:15 +08:00
|
|
|
|
2023-03-15 16:56:47 +08:00
|
|
|
await saveConvo(
|
|
|
|
|
req?.session?.user?.username,
|
|
|
|
|
{
|
|
|
|
|
conversationId,
|
|
|
|
|
title
|
|
|
|
|
}
|
|
|
|
|
);
|
2023-03-14 11:42:35 +08:00
|
|
|
}
|
2023-02-12 16:38:33 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2023-03-13 13:11:53 +08:00
|
|
|
// await deleteMessages({ messageId: userMessageId });
|
2023-03-14 20:21:41 -04:00
|
|
|
const errorMessage = {
|
|
|
|
|
messageId: crypto.randomUUID(),
|
|
|
|
|
sender: model,
|
|
|
|
|
conversationId,
|
|
|
|
|
parentMessageId: overrideParentMessageId || userMessageId,
|
|
|
|
|
error: true,
|
|
|
|
|
text: error.message
|
|
|
|
|
};
|
2023-03-15 00:50:27 +08:00
|
|
|
await saveMessage(errorMessage);
|
|
|
|
|
handleError(res, errorMessage);
|
2023-02-12 16:38:33 -05:00
|
|
|
}
|
2023-03-15 00:54:50 +08:00
|
|
|
};
|
2023-02-12 16:38:33 -05:00
|
|
|
|
2023-02-21 21:30:56 -05:00
|
|
|
module.exports = router;
|