2023-03-25 13:09:44 -04:00
|
|
|
const axios = require('axios');
|
|
|
|
const jsonfile = require('jsonfile');
|
2023-03-25 20:27:33 -04:00
|
|
|
let sessionID;
|
2023-03-25 13:09:44 -04:00
|
|
|
|
|
|
|
function parseQuestionsAndAnswers(jsonStr) {
|
|
|
|
const obj = JSON.parse(jsonStr);
|
|
|
|
const questions = obj.questions;
|
|
|
|
const answers = questions.map(q => q.answer);
|
|
|
|
return { questions, answers };
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
name: "view-session-history",
|
|
|
|
description: "View your current conversation.",
|
|
|
|
run: async (client, interaction) => {
|
|
|
|
const file = './cache/' + interaction.user.id;
|
|
|
|
|
|
|
|
await jsonfile.readFile(file, async function (err, session) {
|
2023-03-25 13:50:48 -04:00
|
|
|
|
2023-03-25 13:09:44 -04:00
|
|
|
if (err) return interaction.editReply('Please create a session using /create-session.');
|
|
|
|
|
2023-03-25 20:27:33 -04:00
|
|
|
sessionID = session.id;
|
2023-03-25 13:09:44 -04:00
|
|
|
|
2023-03-25 20:27:33 -04:00
|
|
|
if (!sessionID) {
|
2023-03-25 13:09:44 -04:00
|
|
|
console.log("No Session!");
|
|
|
|
isProcessing = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const headers = {
|
2023-03-25 13:40:50 -04:00
|
|
|
'authority': process.env.PUBLIC_URL,
|
2023-03-25 13:09:44 -04:00
|
|
|
'accept': 'text/event-stream',
|
|
|
|
};
|
2023-03-26 23:02:55 -04:00
|
|
|
|
2023-04-07 15:18:40 -04:00
|
|
|
const response = await axios.get(`http://${process.env.INTERNAL_IP}:${process.env.SERGE_PORT}/api/chat/${sessionID}`, {
|
2023-04-07 14:54:46 -04:00
|
|
|
headers
|
2023-03-25 13:09:44 -04:00
|
|
|
});
|
2023-04-02 21:21:30 -04:00
|
|
|
|
2023-03-25 13:09:44 -04:00
|
|
|
if (!response.data.questions) return interaction.editReply("You have no history in this session yet :) ");
|
|
|
|
|
|
|
|
const result = parseQuestionsAndAnswers(JSON.stringify(response.data));
|
|
|
|
let pairsString = "";
|
|
|
|
for (const question of result.questions) {
|
|
|
|
pairsString += `***Question:*** ${question.question}\n***Answer:*** ${question.answer}\n`;
|
|
|
|
}
|
|
|
|
interaction.editReply(pairsString);
|
|
|
|
})
|
|
|
|
},
|
|
|
|
};
|