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

57 lines
2.1 KiB
JavaScript

const axios = require('axios');
const jsonfile = require('jsonfile');
let sessionID;
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) {
if (err) return interaction.editReply('Please create a session using /create-session.');
sessionID = session.id;
if (!sessionID) {
console.log("No Session!");
isProcessing = false;
return;
}
const headers = {
'authority': process.env.PUBLIC_URL,
'accept': 'text/event-stream',
};
// 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}`, {
headers,
auth: {
username: process.env.USERNAME,
password: process.env.PASS
}
});
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)
},
};