2023-02-25 09:04:32 -05:00
|
|
|
const express = require('express');
|
|
|
|
|
const crypto = require('crypto');
|
|
|
|
|
const router = express.Router();
|
2023-03-31 03:22:57 +08:00
|
|
|
const { titleConvo, askBing } = require('../../app');
|
|
|
|
|
const { saveBingMessage, getConvoTitle, saveConvo, getConvo } = require('../../models');
|
2023-03-15 15:21:04 -04:00
|
|
|
const { handleError, sendMessage, createOnProgress, handleText } = require('./handlers');
|
2023-02-25 09:04:32 -05:00
|
|
|
|
|
|
|
|
router.post('/', async (req, res) => {
|
2023-03-14 20:21:41 -04:00
|
|
|
const {
|
2023-03-31 03:22:57 +08:00
|
|
|
endpoint,
|
2023-03-14 20:21:41 -04:00
|
|
|
text,
|
2023-03-31 03:22:57 +08:00
|
|
|
messageId,
|
|
|
|
|
overrideParentMessageId = null,
|
2023-03-14 20:21:41 -04:00
|
|
|
parentMessageId,
|
2023-03-31 03:22:57 +08:00
|
|
|
conversationId: oldConversationId
|
2023-03-14 20:21:41 -04:00
|
|
|
} = req.body;
|
2023-03-31 03:22:57 +08:00
|
|
|
if (text.length === 0) return handleError(res, { text: 'Prompt empty or too short' });
|
|
|
|
|
if (endpoint !== 'bingAI') return handleError(res, { text: 'Illegal request' });
|
2023-03-13 14:04:47 +08:00
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
// build user message
|
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-03-31 03:22:57 +08:00
|
|
|
const userMessageId = messageId;
|
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-31 03:22:57 +08:00
|
|
|
// build endpoint option
|
|
|
|
|
const endpointOption = {
|
|
|
|
|
jailbreak: req.body?.jailbreak || false,
|
|
|
|
|
jailbreakConversationId: req.body?.jailbreakConversationId || null,
|
|
|
|
|
conversationSignature: req.body?.conversationSignature || null,
|
|
|
|
|
clientId: req.body?.clientId || null,
|
|
|
|
|
invocationId: req.body?.invocationId || null,
|
|
|
|
|
toneStyle: req.body?.toneStyle || 'fast',
|
|
|
|
|
suggestions: req.body?.suggestions || []
|
|
|
|
|
};
|
|
|
|
|
|
2023-03-14 20:21:41 -04:00
|
|
|
console.log('ask log', {
|
2023-03-31 03:22:57 +08:00
|
|
|
userMessage,
|
|
|
|
|
endpointOption,
|
|
|
|
|
conversationId
|
2023-03-13 12:35:55 +08:00
|
|
|
});
|
|
|
|
|
|
2023-03-17 02:08:03 +08:00
|
|
|
if (!overrideParentMessageId) {
|
2023-03-20 17:02:37 -04:00
|
|
|
await saveBingMessage(userMessage);
|
2023-03-31 04:22:16 +08:00
|
|
|
await saveConvo(req?.session?.user?.username, {
|
|
|
|
|
...userMessage,
|
|
|
|
|
...endpointOption,
|
|
|
|
|
conversationId,
|
|
|
|
|
endpoint
|
|
|
|
|
});
|
2023-03-17 02:08:03 +08:00
|
|
|
}
|
2023-03-15 00:54:50 +08:00
|
|
|
|
|
|
|
|
return await ask({
|
|
|
|
|
isNewConversation,
|
2023-03-14 20:21:41 -04:00
|
|
|
userMessage,
|
2023-03-31 03:22:57 +08:00
|
|
|
endpointOption,
|
|
|
|
|
conversationId,
|
2023-03-15 00:54:50 +08:00
|
|
|
preSendRequest: true,
|
2023-03-17 02:08:03 +08:00
|
|
|
overrideParentMessageId,
|
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,
|
2023-03-14 20:21:41 -04:00
|
|
|
userMessage,
|
2023-03-31 03:22:57 +08:00
|
|
|
endpointOption,
|
|
|
|
|
conversationId,
|
2023-03-15 00:54:50 +08:00
|
|
|
preSendRequest = true,
|
2023-03-31 03:22:57 +08:00
|
|
|
overrideParentMessageId = null,
|
2023-03-14 20:21:41 -04:00
|
|
|
req,
|
|
|
|
|
res
|
2023-03-15 00:54:50 +08:00
|
|
|
}) => {
|
2023-03-31 03:22:57 +08:00
|
|
|
let { text, parentMessageId: userParentMessageId, 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-03-17 03:13:42 +08:00
|
|
|
const abortController = new AbortController();
|
2023-03-31 03:22:57 +08:00
|
|
|
res.on('close', () => abortController.abort());
|
2023-02-25 09:04:32 -05:00
|
|
|
let response = await askBing({
|
|
|
|
|
text,
|
2023-03-31 03:22:57 +08:00
|
|
|
parentMessageId: userParentMessageId,
|
|
|
|
|
conversationId,
|
|
|
|
|
...endpointOption,
|
|
|
|
|
onProgress: progressCallback.call(null, {
|
2023-03-14 20:21:41 -04:00
|
|
|
res,
|
|
|
|
|
text,
|
|
|
|
|
parentMessageId: overrideParentMessageId || userMessageId
|
|
|
|
|
}),
|
2023-03-17 03:13:42 +08:00
|
|
|
abortController
|
2023-02-25 09:04:32 -05:00
|
|
|
});
|
|
|
|
|
|
2023-03-15 00:54:50 +08:00
|
|
|
console.log('BING RESPONSE', response);
|
2023-02-25 09:04:32 -05:00
|
|
|
|
2023-04-04 01:10:50 +08:00
|
|
|
// STEP1 update the convosation
|
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-04-04 01:10:50 +08:00
|
|
|
if (conversationId != response.conversationId && isNewConversation)
|
2023-03-31 03:22:57 +08:00
|
|
|
await saveConvo(req?.session?.user?.username, {
|
|
|
|
|
conversationId: conversationId,
|
2023-04-04 01:10:50 +08:00
|
|
|
newConversationId: response.conversationId || conversationId
|
2023-03-31 03:22:57 +08:00
|
|
|
});
|
2023-04-04 01:10:50 +08:00
|
|
|
conversationId = response.conversationId || conversationId;
|
|
|
|
|
|
|
|
|
|
// STEP2 update the user message
|
|
|
|
|
userMessage.conversationSignature =
|
|
|
|
|
endpointOption.conversationSignature || response.conversationSignature;
|
|
|
|
|
userMessage.conversationId = conversationId;
|
|
|
|
|
userMessage.invocationId = endpointOption.invocationId;
|
|
|
|
|
userMessage.messageId = response.details.requestId || userMessageId;
|
|
|
|
|
|
|
|
|
|
// If response has parentMessageId, the fake userMessage.messageId should be updated to the real one.
|
|
|
|
|
if (!overrideParentMessageId) {
|
|
|
|
|
const oldUserMessageId = userMessageId;
|
|
|
|
|
userMessageId = response.details.requestId;
|
|
|
|
|
await saveBingMessage({ ...userMessage, messageId: oldUserMessageId, newMessageId: userMessageId });
|
|
|
|
|
}
|
2023-02-25 09:04:32 -05:00
|
|
|
|
2023-03-17 09:11:45 -04:00
|
|
|
response.text = response.response || response.details.spokenText || '**Bing refused to answer.**';
|
|
|
|
|
// 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 =
|
2023-03-31 03:22:57 +08:00
|
|
|
response.details.suggestedResponses && response.details.suggestedResponses.map(s => s.text);
|
|
|
|
|
response.sender = endpointOption?.jailbreak ? 'Sydney' : 'BingAI';
|
2023-03-13 12:35:55 +08:00
|
|
|
// response.final = true;
|
2023-03-09 18:42:36 -05:00
|
|
|
|
2023-03-20 17:02:37 -04:00
|
|
|
response.messageId = response.details.messageId;
|
2023-03-15 00:54:50 +08:00
|
|
|
// override the parentMessageId, for the regeneration.
|
2023-03-31 03:22:57 +08:00
|
|
|
response.parentMessageId = overrideParentMessageId || response.details.requestId || userMessageId;
|
2023-03-15 00:54:50 +08:00
|
|
|
|
2023-03-15 15:44:48 -04:00
|
|
|
response.text = await handleText(response, true);
|
2023-03-20 17:02:37 -04:00
|
|
|
await saveBingMessage(response);
|
2023-03-31 03:22:57 +08:00
|
|
|
await saveConvo(req?.session?.user?.username, {
|
|
|
|
|
...endpointOption,
|
|
|
|
|
...response
|
|
|
|
|
});
|
2023-03-14 01:24:43 +08:00
|
|
|
|
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,
|
2023-03-31 03:22:57 +08:00
|
|
|
conversation: await getConvo(req?.session?.user?.username, conversationId),
|
2023-03-14 20:21:41 -04:00
|
|
|
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-31 03:22:57 +08:00
|
|
|
const title = await titleConvo({ endpoint: endpointOption?.endpoint, text, response });
|
|
|
|
|
|
|
|
|
|
await saveConvo(req?.session?.user?.username, {
|
|
|
|
|
conversationId: conversationId,
|
|
|
|
|
title
|
|
|
|
|
});
|
2023-03-14 11:42:35 +08:00
|
|
|
}
|
2023-02-25 09:04:32 -05:00
|
|
|
} catch (error) {
|
|
|
|
|
console.log(error);
|
2023-03-14 20:21:41 -04:00
|
|
|
const errorMessage = {
|
|
|
|
|
messageId: crypto.randomUUID(),
|
2023-03-31 03:22:57 +08:00
|
|
|
sender: endpointOption?.jailbreak ? 'Sydney' : 'BingAI',
|
2023-03-14 20:21:41 -04:00
|
|
|
conversationId,
|
|
|
|
|
parentMessageId: overrideParentMessageId || userMessageId,
|
|
|
|
|
error: true,
|
|
|
|
|
text: error.message
|
|
|
|
|
};
|
2023-03-20 17:02:37 -04:00
|
|
|
await saveBingMessage(errorMessage);
|
2023-03-15 00:50:27 +08:00
|
|
|
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
|
|
|
|
2023-03-31 03:22:57 +08:00
|
|
|
module.exports = router;
|