From e475e6d3743bf6d985a4d3537d3784e043e2a3dd Mon Sep 17 00:00:00 2001 From: Raven Date: Tue, 6 Aug 2024 20:08:24 -0400 Subject: [PATCH] edit guild, name headers for resets --- backend-server/backend-server.js | 38 ++++++++++++++++++++++++++------ bot/discord-bot.js | 10 +++++---- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/backend-server/backend-server.js b/backend-server/backend-server.js index a6ba5d3..1acf036 100644 --- a/backend-server/backend-server.js +++ b/backend-server/backend-server.js @@ -6,7 +6,8 @@ import cors from 'cors'; import cheerio from 'cheerio'; import llamaTokenizer from 'llama-tokenizer-js'; import googleIt from 'google-it'; - +let name; +let guild; const prompt = process.env.PROMPT; const app = express(); const port = 3000; @@ -31,8 +32,8 @@ const getTimestamp = () => { // Middleware to track conversation history by CF-Connecting-IP app.use((req, res, next) => { const ip = req.headers['x-forwarded-for-id'] || req.headers['cf-connecting-ip'] || req.headers['x-forwarded-for'] || req.headers['x-real-ip'] || req.ip; - const name = req.headers['x-forwarded-for-name']; - const guild = req.headers['x-forwarded-for-guild']; + name = req.headers['x-forwarded-for-name']; + guild = req.headers['x-forwarded-for-guild']; req.clientIp = ip; // Store the IP in a request property @@ -542,12 +543,35 @@ app.post('/api/v1/restart-core', (req, res) => { app.post('/api/v1/reset-conversation', (req, res) => { const ip = req.clientIp; + console.log(name) console.log(`${getTimestamp()} [INFO] Resetting conversation history for: ${ip}`); // Log the IP address - conversationHistory[ip] = [{ - role: 'system', - content: prompt - }]; + if (name) { + if (guild) { + console.log(`${getTimestamp()} [INFO] Incoming request from: ${req.clientIp} | ${name} within ${guild}`); // Log the IP address + console.log(`${getTimestamp()} [INFO] Initializing conversation history for: ${req.clientIp}`); + + conversationHistory[req.clientIp] = [{ + role: 'system', + content: `My name is: ${name}, We are chatting inside ${guild} a Discord Server. ` + prompt + }]; + + } else { + conversationHistory[req.clientIp] = [{ + role: 'system', + content: `My name is: ${name} ` + prompt + }]; + } + } else { + console.log(`${getTimestamp()} [INFO] Incoming request from: ${req.clientIp}`); // Log the IP address + console.log(`${getTimestamp()} [INFO] Initializing conversation history for new IP: ${req.clientIp}`); + + conversationHistory[req.clientIp] = [{ + role: 'system', + content: prompt + }]; + } + console.log(`${getTimestamp()} [INFO] Conversation history reset for: ${ip}`); res.json({ message: "Conversation history reset for: " + ip diff --git a/bot/discord-bot.js b/bot/discord-bot.js index 5f4cdd8..0e1e24f 100644 --- a/bot/discord-bot.js +++ b/bot/discord-bot.js @@ -98,10 +98,12 @@ const {Client, GatewayIntentBits, EmbedBuilder} = require('discord.js'); try { const response = await axios.post( `${process.env.API_PATH}/reset-conversation`, {}, { - headers: { - 'x-forwarded-for-id': message.author.id, - - } + headers: { + 'Content-Type': 'application/json', + 'x-forwarded-for-id': message.author.id, + 'x-forwarded-for-name': message.author.username, + 'x-forwarded-for-guild': message.guild.name + } } ); console.log(response.status)