2023-02-12 16:38:33 -05:00
|
|
|
const express = require('express');
|
|
|
|
|
const router = express.Router();
|
2023-03-13 14:04:47 +08:00
|
|
|
const { titleConvo } = require('../../app/');
|
|
|
|
|
const { getConvo, saveConvo, getConvoTitle } = require('../../models');
|
2023-02-12 16:38:33 -05:00
|
|
|
const { getConvos, deleteConvos, updateConvo } = require('../../models/Conversation');
|
2023-03-13 14:04:47 +08:00
|
|
|
const { getMessages } = require('../../models/Message');
|
2023-02-12 16:38:33 -05:00
|
|
|
|
|
|
|
|
router.get('/', async (req, res) => {
|
2023-03-05 14:41:50 -05:00
|
|
|
const pageNumber = req.query.pageNumber || 1;
|
|
|
|
|
res.status(200).send(await getConvos(pageNumber));
|
2023-02-12 16:38:33 -05:00
|
|
|
});
|
|
|
|
|
|
2023-03-13 14:04:47 +08:00
|
|
|
router.post('/gen_title', async (req, res) => {
|
|
|
|
|
const { conversationId } = req.body.arg;
|
|
|
|
|
|
|
|
|
|
const convo = await getConvo(conversationId)
|
|
|
|
|
const firstMessage = (await getMessages({ conversationId }))[0]
|
|
|
|
|
const secondMessage = (await getMessages({ conversationId }))[1]
|
|
|
|
|
|
|
|
|
|
const title = convo.jailbreakConversationId
|
|
|
|
|
? await getConvoTitle(conversationId)
|
|
|
|
|
: await titleConvo({
|
|
|
|
|
model: convo?.model,
|
|
|
|
|
message: firstMessage?.text,
|
2023-03-13 21:44:30 +08:00
|
|
|
response: JSON.stringify(secondMessage?.text || '')
|
2023-03-13 14:04:47 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await saveConvo({
|
|
|
|
|
conversationId,
|
|
|
|
|
title
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
res.status(200).send(title);
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-12 16:38:33 -05:00
|
|
|
router.post('/clear', async (req, res) => {
|
|
|
|
|
let filter = {};
|
|
|
|
|
const { conversationId } = req.body.arg;
|
2023-02-21 21:30:56 -05:00
|
|
|
if (conversationId) {
|
2023-02-12 16:38:33 -05:00
|
|
|
filter = { conversationId };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const dbResponse = await deleteConvos(filter);
|
|
|
|
|
res.status(201).send(dbResponse);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).send(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.post('/update', async (req, res) => {
|
|
|
|
|
const update = req.body.arg;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const dbResponse = await updateConvo(update);
|
|
|
|
|
res.status(201).send(dbResponse);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error(error);
|
|
|
|
|
res.status(500).send(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|