2023-02-25 09:04:32 -05:00
|
|
|
const express = require('express');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const router = express.Router();
|
2023-03-15 15:21:04 -04:00
|
|
|
const { titleConvo, getCitations, citeText, askBing } = require('../../app/');
|
2023-03-13 14:04:47 +08:00
|
|
|
const { saveMessage, getConvoTitle, saveConvo } = require('../../models');
|
2023-03-15 15:21:04 -04:00
|
|
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
2023-03-09 18:42:36 -05:00
|
|
|
const citationRegex = /\[\^\d+?\^]/g;
|
2023-02-25 09:04:32 -05:00
|
|
|
|
|
|
|
|
router.post('/', async (req, res) => {
|
2023-03-14 20:21:41 -04:00
|
|
|
const {
|
|
|
|
|
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-25 09:04:32 -05:00
|
|
|
}
|
2023-03-13 14:04:47 +08:00
|
|
|
|
2023-03-13 13:11:53 +08:00
|
|
|
const conversationId = oldConversationId || crypto.randomUUID();
|
2023-03-14 20:21:41 -04:00
|
|
|
const isNewConversation = !oldConversationId;
|
2023-02-25 09:04:32 -05:00
|
|
|
|
2023-03-13 21:59:25 -04: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-13 12:35:55 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-03-14 20:21:41 -04:00
|
|
|
conversationId,
|
|
|
|
|
isCreatedByUser: true
|
|
|
|
|
};
|
2023-02-25 09:04:32 -05:00
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
console.log('ask log', {
|
2023-03-13 12:35:55 +08:00
|
|
|
model,
|
|
|
|
|
...userMessage,
|
|
|
|
|
...convo
|
|
|
|
|
});
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
await saveMessage(userMessage);
|
|
|
|
|
await saveConvo({ ...userMessage, model, ...convo });
|
|
|
|
|
|
|
|
|
|
return await ask({
|
|
|
|
|
isNewConversation,
|
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
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
const ask = async ({
|
2023-03-15 00:54:50 +08:00
|
|
|
isNewConversation,
|
|
|
|
|
overrideParentMessageId = null,
|
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
|
|
|
let {
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId: userParentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
messageId: userMessageId
|
|
|
|
|
} = userMessage;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-02-25 09:04:32 -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-25 09:04:32 -05:00
|
|
|
try {
|
2023-03-14 15:42:59 -04:00
|
|
|
const progressCallback = createOnProgress();
|
2023-02-25 09:04:32 -05:00
|
|
|
let response = await askBing({
|
|
|
|
|
text,
|
2023-03-14 20:21:41 -04:00
|
|
|
onProgress: progressCallback.call(null, model, {
|
|
|
|
|
res,
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId: overrideParentMessageId || userMessageId
|
|
|
|
|
}),
|
2023-03-13 12:35:55 +08:00
|
|
|
convo: {
|
2023-03-15 00:54:50 +08:00
|
|
|
...convo,
|
2023-03-13 12:35:55 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
2023-03-14 20:21:41 -04:00
|
|
|
conversationId
|
|
|
|
|
}
|
2023-02-25 09:04:32 -05:00
|
|
|
});
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
console.log('BING RESPONSE', response);
|
2023-03-09 18:42:36 -05:00
|
|
|
// console.dir(response, { depth: null });
|
2023-03-09 20:29:44 -05:00
|
|
|
const hasCitations = response.response.match(citationRegex)?.length > 0;
|
2023-02-25 09:04:32 -05:00
|
|
|
|
|
|
|
|
userMessage.conversationSignature =
|
|
|
|
|
convo.conversationSignature || response.conversationSignature;
|
2023-03-15 00:54:50 +08:00
|
|
|
userMessage.conversationId = response.conversationId || conversationId;
|
2023-02-25 09:04:32 -05:00
|
|
|
userMessage.invocationId = response.invocationId;
|
|
|
|
|
await saveMessage(userMessage);
|
2023-03-14 20:21:41 -04:00
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
// Bing API will not use our conversationId at the first time,
|
|
|
|
|
// so change the placeholder conversationId to the real one.
|
|
|
|
|
// Attition: the api will also create new conversationId while using invalid userMessage.parentMessageId,
|
|
|
|
|
// but in this situation, don't change the conversationId, but create new convo.
|
2023-03-14 20:21:41 -04:00
|
|
|
if (conversationId != userMessage.conversationId && isNewConversation)
|
|
|
|
|
await saveConvo({
|
|
|
|
|
conversationId: conversationId,
|
|
|
|
|
newConversationId: userMessage.conversationId
|
|
|
|
|
});
|
2023-03-15 00:54:50 +08:00
|
|
|
conversationId = userMessage.conversationId;
|
2023-02-25 09:04:32 -05:00
|
|
|
|
|
|
|
|
response.text = response.response;
|
2023-03-09 18:42:36 -05:00
|
|
|
delete response.response;
|
2023-03-13 12:35:55 +08:00
|
|
|
// response.id = response.details.messageId;
|
2023-02-25 09:04:32 -05:00
|
|
|
response.suggestions =
|
|
|
|
|
response.details.suggestedResponses &&
|
|
|
|
|
response.details.suggestedResponses.map((s) => s.text);
|
|
|
|
|
response.sender = model;
|
2023-03-13 12:35:55 +08:00
|
|
|
// response.final = true;
|
2023-03-09 18:42:36 -05:00
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
// override the parentMessageId, for the regeneration.
|
2023-03-14 20:21:41 -04:00
|
|
|
response.parentMessageId =
|
|
|
|
|
overrideParentMessageId || response.parentMessageId || userMessageId;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-03-09 18:42:36 -05:00
|
|
|
const links = getCitations(response);
|
|
|
|
|
response.text =
|
|
|
|
|
citeText(response) +
|
|
|
|
|
(links?.length > 0 && hasCitations ? `\n<small>${links}</small>` : '');
|
2023-03-14 16:05:46 -04:00
|
|
|
response.text = await handleText(response.text);
|
2023-03-09 18:42:36 -05:00
|
|
|
|
2023-02-25 09:04:32 -05:00
|
|
|
await saveMessage(response);
|
2023-03-14 20:21:41 -04:00
|
|
|
await saveConvo({ ...response, model, chatGptLabel: null, promptPrefix: null, ...convo });
|
2023-03-13 12:35:55 +08:00
|
|
|
sendMessage(res, {
|
2023-03-13 14:04:47 +08:00
|
|
|
title: await getConvoTitle(conversationId),
|
2023-03-14 20:21:41 -04:00
|
|
|
final: true,
|
|
|
|
|
requestMessage: userMessage,
|
2023-03-15 00:54:50 +08:00
|
|
|
responseMessage: response
|
2023-03-13 12:35:55 +08:00
|
|
|
});
|
2023-02-25 09:04:32 -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 });
|
2023-03-14 20:21:41 -04:00
|
|
|
|
2023-03-14 11:42:35 +08:00
|
|
|
await saveConvo({
|
|
|
|
|
conversationId,
|
|
|
|
|
title
|
2023-03-14 20:21:41 -04:00
|
|
|
});
|
2023-03-14 11:42:35 +08:00
|
|
|
}
|
2023-02-25 09:04:32 -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-25 09:04:32 -05:00
|
|
|
}
|
2023-03-15 00:54:50 +08:00
|
|
|
};
|
2023-02-25 09:04:32 -05:00
|
|
|
|
|
|
|
|
module.exports = router;
|