diff --git a/api/models/Conversation.js b/api/models/Conversation.js index 97cb8eaad1..1ff778e957 100644 --- a/api/models/Conversation.js +++ b/api/models/Conversation.js @@ -47,6 +47,15 @@ const convoSchema = mongoose.Schema({ const Conversation = mongoose.models.Conversation || mongoose.model('Conversation', convoSchema); +const getConvo = async (conversationId) => { + try { + return await Conversation.findOne({ conversationId }).exec(); + } catch (error) { + console.log(error); + return { message: 'Error getting single conversation' }; + } +}; + module.exports = { saveConvo: async ({ conversationId, title, ...convo }) => { try { @@ -95,12 +104,14 @@ module.exports = { return { message: 'Error getting conversations' }; } }, - getConvo: async (conversationId) => { + getConvo, + getConvoTitle: async (conversationId) => { try { - return await Conversation.findOne({ conversationId }).exec(); + const convo = await getConvo(conversationId); + return convo.title; } catch (error) { console.log(error); - return { message: 'Error getting single conversation' }; + return { message: 'Error getting conversation title' }; } }, deleteConvos: async (filter) => { diff --git a/api/models/index.js b/api/models/index.js index 0b195d7248..8af5a1aa9c 100644 --- a/api/models/index.js +++ b/api/models/index.js @@ -1,10 +1,11 @@ const { saveMessage, deleteMessages } = require('./Message'); const { getCustomGpts, updateCustomGpt, updateByLabel, deleteCustomGpts } = require('./CustomGpt'); -const { getConvo, saveConvo } = require('./Conversation'); +const { getConvoTitle, getConvo, saveConvo } = require('./Conversation'); module.exports = { saveMessage, deleteMessages, + getConvoTitle, getConvo, saveConvo, getCustomGpts, diff --git a/api/server/routes/askSydney.js b/api/server/routes/askSydney.js index 0b77526b8b..a928bc75fc 100644 --- a/api/server/routes/askSydney.js +++ b/api/server/routes/askSydney.js @@ -2,7 +2,7 @@ const express = require('express'); const crypto = require('crypto'); const router = express.Router(); const { titleConvo, askSydney } = require('../../app/'); -const { saveMessage, deleteMessages, saveConvo } = require('../../models'); +const { saveMessage, deleteMessages, saveConvo, getConvoTitle } = require('../../models'); const { handleError, sendMessage } = require('./handlers'); router.post('/', async (req, res) => { @@ -39,31 +39,48 @@ router.post('/', async (req, res) => { }); console.log('SYDNEY RESPONSE'); - // console.dir(response, { depth: null }); + // the usual values expected by the client are generated since only + // jailbreakConversationId and initial messageId is needed by sydney + // to continue the conversation + // if (!convo.jailbreakConversationId) { + // response.title = await titleConvo({ + // model, + // message: text, + // response: JSON.stringify(response.response) + // }); + // } - userMessage.conversationSignature = - convo.conversationSignature || response.conversationSignature; - userMessage.conversationId = convo.conversationId || response.conversationId; - userMessage.invocationId = response.invocationId; - userMessage.jailbreakConversationId = convo.jailbreakConversationId || response.jailbreakConversationId; - await saveMessage(userMessage); - - if (!convo.conversationSignature) { - response.title = await titleConvo({ - model, - message: text, - response: JSON.stringify(response.response) - }); - } - + // Save sydney response + 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 titleConvo({ + model, + message: text, + response: JSON.stringify(response.response) + }); + response.conversationId = convo.conversationId + ? convo.conversationId + : crypto.randomUUID(); + response.conversationSignature = convo.conversationSignature + ? convo.conversationSignature + : crypto.randomUUID(); response.text = response.response; - response.parentMessageId = convo.parentMessageId || response.messageId; - response.id = response.details.messageId; response.suggestions = response.details.suggestedResponses && response.details.suggestedResponses.map((s) => s.text); response.sender = model; response.final = true; + + // 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); diff --git a/client/src/components/Conversations/Conversation.jsx b/client/src/components/Conversations/Conversation.jsx index 455d331d07..745671e1cc 100644 --- a/client/src/components/Conversations/Conversation.jsx +++ b/client/src/components/Conversations/Conversation.jsx @@ -44,7 +44,7 @@ export default function Conversation({ dispatch( setConversation({ ...convo, - parentMessageId: parentMessageId || null, + parentMessageId, jailbreakConversationId, conversationSignature, clientId, diff --git a/client/src/components/Conversations/index.jsx b/client/src/components/Conversations/index.jsx index ab49d68b75..13b1e64b00 100644 --- a/client/src/components/Conversations/index.jsx +++ b/client/src/components/Conversations/index.jsx @@ -16,6 +16,7 @@ export default function Conversations({ conversations, conversationId, showMore ? { jailbreakConversationId: convo.jailbreakConversationId, conversationSignature: convo.conversationSignature, + parentMessageId: convo.parentMessageId || null, clientId: convo.clientId, invocationId: convo.invocationId } diff --git a/client/src/components/Main/TextChat.jsx b/client/src/components/Main/TextChat.jsx index a162a452d2..2350c68e5d 100644 --- a/client/src/components/Main/TextChat.jsx +++ b/client/src/components/Main/TextChat.jsx @@ -68,11 +68,30 @@ export default function TextChat({ messages }) { }) ); } else if ( - isBing && + model === 'bingai' && convo.conversationId === null && convo.invocationId === null ) { console.log('Bing data:', data) + const { + title, + conversationSignature, + clientId, + conversationId, + invocationId + } = data; + dispatch( + setConversation({ + title, + parentMessageId: null, + conversationSignature, + clientId, + conversationId, + invocationId, + }) + ); + } else if (model === 'sydney') { + console.log('Sydney data:', data) const { title, jailbreakConversationId, @@ -86,7 +105,7 @@ export default function TextChat({ messages }) { setConversation({ title, jailbreakConversationId, - parentMessageId: parentMessageId || null, + parentMessageId, conversationSignature, clientId, conversationId,