feat: merge all message.id into message.messageId

feat: the first message will have a parentMessageId as 00000000-0000-0000-0000-000000000000 (in order not to create new convo when resubmit)
feat: ask will return the userMessage as well, to send back the messageId

TODO: comment out the title generation.
TODO: bing version need to be test

fix: never use the same messageId
fix: never delete exist messages
fix: connect response.parentMessageId to the userMessage.messageId
fix: set default convo title as new Chat
This commit is contained in:
Wentao Lyu 2023-03-13 12:35:55 +08:00
parent be71140dd4
commit 0ed8a40a41
7 changed files with 159 additions and 85 deletions

View file

@ -7,22 +7,37 @@ const { handleError, sendMessage } = require('./handlers');
const citationRegex = /\[\^\d+?\^]/g;
router.post('/', async (req, res) => {
const { id, model, text, ...convo } = req.body;
const { model, text, parentMessageId, conversationId, ...convo } = req.body;
if (text.length === 0) {
return handleError(res, 'Prompt empty or too short');
}
const userMessageId = id || crypto.randomUUID();
let userMessage = { id: userMessageId, sender: 'User', text, isCreatedByUser: true };
const userMessageId = messageId;
const userParentMessageId = parentMessageId || '00000000-0000-0000-0000-000000000000'
let userMessage = {
messageId: userMessageId,
sender: 'User',
text,
parentMessageId: userParentMessageId,
conversationId,
isCreatedByUser: true
};
console.log('ask log', { model, ...userMessage, ...convo });
if (id) {
// existing conversation
await saveMessage(userMessage);
await deleteMessagesSince(userMessage);
} else {}
console.log('ask log', {
model,
...userMessage,
parentMessageId: userParentMessageId,
conversationId,
...convo
});
// if (messageId) {
// // existing conversation
// await saveMessage(userMessage);
// await deleteMessagesSince(userMessage);
// } else {}
res.writeHead(200, {
Connection: 'keep-alive',
'Content-Type': 'text/event-stream',
@ -43,7 +58,11 @@ router.post('/', async (req, res) => {
let response = await askSydney({
text,
progressCallback,
convo
convo: {
parentMessageId: userParentMessageId,
conversationId,
...convo
},
});
console.log('SYDNEY RESPONSE');
@ -52,19 +71,19 @@ router.post('/', async (req, res) => {
const hasCitations = response.response.match(citationRegex)?.length > 0;
// Save sydney response
response.id = response.messageId;
// response.id = response.messageId;
// response.parentMessageId = convo.parentMessageId ? convo.parentMessageId : response.messageId;
response.parentMessageId = response.messageId;
response.invocationId = convo.invocationId ? convo.invocationId + 1 : 1;
response.title = convo.jailbreakConversationId
? await getConvoTitle(convo.conversationId)
? await getConvoTitle(conversationId)
: await titleConvo({
model,
message: text,
response: JSON.stringify(response.response)
});
response.conversationId = convo.conversationId
? convo.conversationId
response.conversationId = conversationId
? conversationId
: crypto.randomUUID();
response.conversationSignature = convo.conversationSignature
? convo.conversationSignature
@ -75,7 +94,8 @@ router.post('/', async (req, res) => {
response.details.suggestedResponses &&
response.details.suggestedResponses.map((s) => s.text);
response.sender = model;
response.final = true;
response.parentMessageId = gptResponse.parentMessageId || userMessage.messageId
// response.final = true;
const links = getCitations(response);
response.text =
@ -84,17 +104,20 @@ router.post('/', async (req, res) => {
// Save user message
userMessage.conversationId = response.conversationId;
userMessage.parentMessageId = response.parentMessageId;
await saveMessage(userMessage);
// Save sydney response & convo, then send
await saveMessage(response);
await saveConvo(response);
sendMessage(res, response);
sendMessage(res, {
final: true,
requestMessage: userMessage,
responseMessage: gptResponse
});
res.end();
} catch (error) {
console.log(error);
await deleteMessages({ id: userMessageId });
await deleteMessages({ messageId: userMessageId });
handleError(res, error.message);
}
});