Merge pull request #80 from wtlyu/feat-regenerate-and-cancel

Feat regenerate and cancel
This commit is contained in:
Danny Avila 2023-03-17 10:40:45 -04:00 committed by GitHub
commit ce3f03267a
18 changed files with 281 additions and 190 deletions

View file

@ -9,6 +9,7 @@ router.post('/', async (req, res) => {
const {
model,
text,
overrideParentMessageId=null,
parentMessageId,
conversationId: oldConversationId,
...convo
@ -37,8 +38,10 @@ router.post('/', async (req, res) => {
...convo
});
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
if (!overrideParentMessageId) {
await saveMessage(userMessage);
await saveConvo(req?.session?.user?.username, { ...userMessage, model, ...convo });
}
return await ask({
isNewConversation,
@ -46,6 +49,7 @@ router.post('/', async (req, res) => {
model,
convo,
preSendRequest: true,
overrideParentMessageId,
req,
res
});
@ -80,6 +84,14 @@ const ask = async ({
try {
const progressCallback = createOnProgress();
const abortController = new AbortController();
res.on('close', () => {
console.log('The client has disconnected.');
// 执行其他操作
abortController.abort();
})
let response = await askSydney({
text,
onProgress: progressCallback.call(null, model, {
@ -91,7 +103,8 @@ const ask = async ({
parentMessageId: userParentMessageId,
conversationId,
...convo
}
},
abortController
});
console.log('SYDNEY RESPONSE', response);
@ -102,7 +115,8 @@ const ask = async ({
userMessage.conversationId = response.conversationId || conversationId;
userMessage.invocationId = response.invocationId;
// Unlike gpt and bing, Sydney will never accept our given userMessage.messageId, it will generate its own one.
await saveMessage(userMessage);
if (!overrideParentMessageId)
await saveMessage(userMessage);
// Save sydney response
// response.id = response.messageId;
@ -125,7 +139,8 @@ const ask = async ({
// Save user message
userMessage.conversationId = response.conversationId || conversationId;
await saveMessage(userMessage);
if (!overrideParentMessageId)
await saveMessage(userMessage);
// Bing API will not use our conversationId at the first time,
// so change the placeholder conversationId to the real one.