rai-serge-discord-bot/commands/system/view-session-history.js

59 lines
2.1 KiB
JavaScript
Raw Normal View History

2023-03-25 13:09:44 -04:00
const axios = require('axios');
const jsonfile = require('jsonfile');
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.');
sessionID = session.id;
2023-03-25 13:09:44 -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
// The Auth stuff here is internal to my set up only and may be removed if you use this bot.
// My chat GUI for Serge is locked down by basic auth in apache to safe guard from
// Users issuing mulitple jobs at a time until there is some built in way to do so.
const response = await axios.get(`http://${process.env.INTERNAL_IP}:8008/api/chat/${sessionID}`, {
2023-03-25 13:09:44 -04:00
headers,
auth: {
2023-03-25 13:46:43 -04:00
username: process.env.USERNAME,
2023-03-25 13:40:50 -04:00
password: process.env.PASS
2023-03-25 13:09:44 -04:00
}
});
console.log(response.data)
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);
})
if (err) console.error("woo" + err)
},
};