fix plugins

This commit is contained in:
Raven 2024-08-03 18:46:41 -04:00
parent fd062bae78
commit 180efb0268

View File

@ -139,7 +139,7 @@ async function scrapeWebPage(url, length) {
} }
response += `\nURL: ${url}`; response += `\nURL: ${url}`;
console.log(`${getTimestamp()} [INFO] Successfully scraped URL: ${url}`); // console.log(`${getTimestamp()} [INFO] Successfully scraped URL: ${url}`);
return response; return response;
} catch (err) { } catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${url}`, err); console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${url}`, err);
@ -183,348 +183,95 @@ app.post('/api/v1/chat', async (req, res) => {
const tolerance = 25; const tolerance = 25;
trimConversationHistory(conversationHistory[ip], maxLength, tolerance); trimConversationHistory(conversationHistory[ip], maxLength, tolerance);
// Start Plugins --- // Start Plugins ---
const ipRegex = /(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/g; const ipRegex = /(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)/g;
const ipAddresses = userMessage.match(ipRegex); const ipAddresses = userMessage.match(ipRegex);
if (ipAddresses) {
console.log(`${getTimestamp()} [INFO] Detected IP addresses in user message: ${ipAddresses}`);
for (const ipAddr of ipAddresses) {
try {
const url = new URL('https://api.abuseipdb.com/api/v2/check');
url.searchParams.append('ipAddress', ipAddr);
url.searchParams.append('maxAgeInDays', '90');
url.searchParams.append('verbose', '');
const options = {
method: 'GET',
headers: {
'Key': process.env.ABUSE_KEY,
'Accept': 'application/json'
}
};
const response = await fetch(url, options);
const data = await response.json();
let abuseResponse = `IP: ${ipAddr}\n`;
abuseResponse += `Abuse Score: ${data.data.abuseConfidenceScore}\n`;
abuseResponse += `Country: ${data.data.countryCode}\n`;
abuseResponse += `Usage Type: ${data.data.usageType}\n`;
abuseResponse += `ISP: ${data.data.isp}\n`;
abuseResponse += `Domain: ${data.data.domain}\n`;
if (data.data.totalReports) {
abuseResponse += `Total Reports: ${data.data.totalReports}\n`;
abuseResponse += `Last Reported: ${data.data.lastReportedAt}\n`;
}
const lastMessageIndex = conversationHistory[ip].length - 1;
if (lastMessageIndex >= 0) {
conversationHistory[ip][lastMessageIndex].content += "\n" + abuseResponse;
console.log(`${getTimestamp()} [INFO] Processed IP address: ${ipAddr}, response: ${abuseResponse}`);
} else {
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
}
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to process IP address: ${ipAddr}`, err);
return res.status(500).json({
message: "An error occurred while processing IP",
error: err.message
});
}
}
}
const urlRegex = /(https?:\/\/[^\s]+)/g; const urlRegex = /(https?:\/\/[^\s]+)/g;
const urls = userMessage.match(urlRegex); const urls = userMessage.match(urlRegex);
if (urls) {
console.log(`${getTimestamp()} [INFO] Detected URLs in user message: ${urls}`);
for (const url of urls) {
const scrapedContent = await scrapeWebPage(url);
if (scrapedContent) {
conversationHistory[ip].push({
role: 'assistant',
content: scrapedContent
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
}
}
const newPersonRegex = /\bnew\s+person\b/gi; const newPersonRegex = /\bnew\s+person\b/gi;
const newPersons = userMessage.match(newPersonRegex); const newPersons = userMessage.match(newPersonRegex);
if (newPersons) {
// If there are multiple occurrences of "new person", process them one by one
for (const newPerson of newPersons) {
try {
const randomUser = await fetchRandomUser();
if (randomUser) {
let response = `New Person:\n`;
response += `Name: ${randomUser.name.first} ${randomUser.name.last}\n`;
response += `Gender: ${randomUser.gender}\n`;
response += `Location: ${randomUser.location.city}, ${randomUser.location.state}, ${randomUser.location.country}\n`;
response += `Email: ${randomUser.email}\n`;
response += `Phone: ${randomUser.phone}\n`;
// Add more details as needed
// Get the index of the last message in the array
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + response
});
console.log("A request for a new person was made. Response: " + response);
} else {
console.log('Failed to fetch random user.');
}
} catch (err) {
console.error(err);
await sendRand(errorMessages);
}
}
}
async function fetchRandomUser() {
try {
const response = await fetch('https://randomuser.me/api/');
const data = await response.json();
return data.results[0]; // Assuming you only need one random user
} catch (error) {
console.error('Error fetching random user:', error);
return null;
}
}
const whatServersRegex = /\bwhat\s+servers\b/gi; const whatServersRegex = /\bwhat\s+servers\b/gi;
if (whatServersRegex.test(userMessage)) {
try {
const response = await fetch(`https://api.my-mc.link/list_all_servers/${process.env.PATH_KEY}/`, {
headers: {
'x-my-mc-auth': process.env.API_KEY
}
});
const data = await response.json();
if (data.success) {
let responseMessage = `Information provided by the system not me: The Current Minecraft Servers online within the My-MC.link P2P JUMP Node System are listed below. These servers are also listed within My-MC Realms.`;
for (const server of data.servers) {
responseMessage += `\nName: ${server.serverName}\n`;
responseMessage += `Game Version: ${server.gameVersion}\n`;
responseMessage += `MOTD: ${server.motd}\n`;
responseMessage += `Online: ${server.online}\n`;
// Add more details as needed
}
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
});
console.log("A request for server information was made. Response: " + responseMessage);
} else {
console.log('Failed to fetch server information.');
}
} catch (error) {
console.error('Error fetching server information:', error);
await sendRand(errorMessages);
}
}
const myMcRegex = /\bmy-mc\b/gi; const myMcRegex = /\bmy-mc\b/gi;
if (myMcRegex.test(userMessage)) {
try {
const response = await fetch('https://my-mc.link/wiki.json');
const data = await response.json();
if (data) {
let responseMessage = `My-MC.Link Wiki:\n`;
// Append all data fields to the response message
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
// Handle nested objects
responseMessage += `${key}:\n`;
Object.keys(data[key]).forEach(innerKey => {
responseMessage += `${innerKey}: ${data[key][innerKey]}\n`;
});
} else {
responseMessage += `${key}: ${data[key]}\n`;
}
});
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
});
console.log("A request for My-MC.Link wiki information was made.");
} else {
console.log('Failed to fetch My-MC.Link wiki information.');
}
} catch (error) {
console.error('Error fetching My-MC.Link wiki information:', error);
await sendRand(errorMessages);
}
}
const dlinuxRegex = /\bdlinux\b/gi; const dlinuxRegex = /\bdlinux\b/gi;
if (dlinuxRegex.test(userMessage)) {
try {
const response = await fetch('https://my-mc.link/dwiki.json');
const data = await response.json();
if (data) {
let responseMessage = `dlinux Wiki:\n`;
// Append all data fields to the response message
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
// Handle nested objects
responseMessage += `${key}:\n`;
Object.keys(data[key]).forEach(innerKey => {
responseMessage += `${innerKey}: ${data[key][innerKey]}\n`;
});
} else {
responseMessage += `${key}: ${data[key]}\n`;
}
});
conversationHistory[ip].push({
role: 'user',
content: "Fetched Info: " + responseMessage
});
console.log("A request for dlinux wiki information was made.");
} else {
console.log('Failed to fetch dlinux wiki information.');
}
} catch (error) {
console.error('Error fetching dlinux wiki information:', error);
await sendRand(errorMessages);
}
}
const searchRegex = /\b[Ss]earch\s+(.+)\b/; const searchRegex = /\b[Ss]earch\s+(.+)\b/;
const searchMatch = userMessage.match(searchRegex); const searchMatch = userMessage.match(searchRegex);
// Handle scraping and other plugins asynchronously
const pluginTasks = [];
if (ipAddresses) {
console.log(`${getTimestamp()} [INFO] Detected IP addresses in user message: ${ipAddresses}`);
ipAddresses.forEach(ipAddr => {
pluginTasks.push(handleIPPlugin(ipAddr, ip, conversationHistory));
});
}
if (urls) {
console.log(`${getTimestamp()} [INFO] Detected URLs in user message: ${urls}`);
urls.forEach(url => {
pluginTasks.push(handleURLPlugin(url, ip, conversationHistory));
});
}
if (newPersons) {
console.log(`${getTimestamp()} [INFO] Detected new person request in user message`);
newPersons.forEach(() => {
pluginTasks.push(handleNewPersonPlugin(ip, conversationHistory));
});
}
if (whatServersRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected what servers request in user message`);
pluginTasks.push(handleWhatServersPlugin(ip, conversationHistory));
}
if (myMcRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected My-MC.Link request in user message`);
pluginTasks.push(handleMyMcPlugin(ip, conversationHistory));
}
if (dlinuxRegex.test(userMessage)) {
console.log(`${getTimestamp()} [INFO] Detected dlinux request in user message`);
pluginTasks.push(handleDlinuxPlugin(ip, conversationHistory));
}
if (searchMatch) { if (searchMatch) {
const searchQuery = searchMatch[1]; const searchQuery = searchMatch[1];
console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`); console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`);
pluginTasks.push(handleSearchPlugin(searchQuery, ip, conversationHistory));
const options = {
query: searchQuery,
limit: 5,
disableConsole: true
};
try {
const results = await googleIt(options);
let searchResponse = `Search Query: ${searchQuery}\n`;
searchResponse += `Top Google search results:\n`;
let scrapedContent = '';
for (let i = 0; i < results.length; i++) {
const result = results[i];
searchResponse += `${i + 1}. ${result.title} - ${result.link}\n`;
try {
const scrapeResult = await scrapeWebPage(result.link, 400);
searchResponse += `Scraped Data: ${scrapeResult}\n`;
scrapedContent += `Scraped Data from ${result.link}:\n${scrapeResult}\n`;
} catch (scrapeErr) {
console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr);
searchResponse += `Failed to scrape URL: ${result.link}\n`;
scrapedContent += `Failed to scrape URL: ${result.link}\n`;
}
}
const lastMessageIndex = conversationHistory[ip].length - 1;
if (lastMessageIndex >= 0) {
conversationHistory[ip][lastMessageIndex].content += "\n" + searchResponse;
console.log(`${getTimestamp()} [INFO] Processed search query: ${searchQuery}, response: ${searchResponse}`);
} else {
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
}
if (scrapedContent) {
conversationHistory[ip].push({
role: 'assistant',
content: scrapedContent
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
// Now send the request to llama API after scraping is done
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
//console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to perform Google search: ${searchQuery}`, err);
return res.status(500).json({
message: "An error occurred while performing Google search",
error: err.message
});
}
} else {
// If no search plugin is used, send the request to llama API directly
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
} }
// Wait for all plugin tasks to complete before sending the request to llama
await Promise.all(pluginTasks);
console.log(`${getTimestamp()} [INFO] Sending request to llama API for response`);
const sent = {
model: 'model',
messages: conversationHistory[ip]
};
console.log(sent);
const llamaResponse = await fetch(`http://127.0.0.1:8002/v1/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(sent)
});
const response = await llamaResponse.json();
const assistantMessage = response.choices[0].message;
conversationHistory[ip].push(assistantMessage);
console.log(`${getTimestamp()} [INFO] Received response from llama API`);
console.log(`${getTimestamp()} [DEBUG] Finish Reason: ${response.choices[0].finish_reason}`);
console.log(`${getTimestamp()} [STATS] Usage: prompt_tokens=${response.usage.prompt_tokens}, completion_tokens=${response.usage.completion_tokens}, total_tokens=${response.usage.total_tokens}`);
res.json(assistantMessage);
} catch (error) { } catch (error) {
console.error(`${getTimestamp()} [ERROR] An error occurred while handling chat request`, error); console.error(`${getTimestamp()} [ERROR] An error occurred while handling chat request`, error);
res.status(500).json({ res.status(500).json({
@ -540,6 +287,239 @@ app.post('/api/v1/chat', async (req, res) => {
} }
}); });
// Define plugin handling functions
async function handleIPPlugin(ipAddr, ip, conversationHistory) {
try {
const url = new URL('https://api.abuseipdb.com/api/v2/check');
url.searchParams.append('ipAddress', ipAddr);
url.searchParams.append('maxAgeInDays', '90');
url.searchParams.append('verbose', '');
const options = {
method: 'GET',
headers: {
'Key': process.env.ABUSE_KEY,
'Accept': 'application/json'
}
};
const response = await fetch(url, options);
const data = await response.json();
let abuseResponse = `IP: ${ipAddr}\n`;
abuseResponse += `Abuse Score: ${data.data.abuseConfidenceScore}\n`;
abuseResponse += `Country: ${data.data.countryCode}\n`;
abuseResponse += `Usage Type: ${data.data.usageType}\n`;
abuseResponse += `ISP: ${data.data.isp}\n`;
abuseResponse += `Domain: ${data.data.domain}\n`;
if (data.data.totalReports) {
abuseResponse += `Total Reports: ${data.data.totalReports}\n`;
abuseResponse += `Last Reported: ${data.data.lastReportedAt}\n`;
}
const lastMessageIndex = conversationHistory[ip].length - 1;
if (lastMessageIndex >= 0) {
conversationHistory[ip][lastMessageIndex].content += "\n" + abuseResponse;
console.log(`${getTimestamp()} [INFO] Processed IP address: ${ipAddr}, response: ${abuseResponse}`);
} else {
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
}
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to process IP address: ${ipAddr}`, err);
}
}
async function handleURLPlugin(url, ip, conversationHistory) {
const scrapedContent = await scrapeWebPage(url);
if (scrapedContent) {
conversationHistory[ip].push({
role: 'assistant',
content: scrapedContent
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
}
async function handleNewPersonPlugin(ip, conversationHistory) {
try {
const randomUser = await fetchRandomUser();
if (randomUser) {
let response = `New Person:\n`;
response += `Name: ${randomUser.name.first} ${randomUser.name.last}\n`;
response += `Gender: ${randomUser.gender}\n`;
response += `Location: ${randomUser.location.city}, ${randomUser.location.state}, ${randomUser.location.country}\n`;
response += `Email: ${randomUser.email}\n`;
response += `Phone: ${randomUser.phone}\n`;
// Add more details as needed
conversationHistory[ip].push({
role: 'assistant',
content: response
});
console.log("A request for a new person was made. Response: " + response);
} else {
console.log('Failed to fetch random user.');
}
} catch (err) {
console.error(err);
}
}
async function handleWhatServersPlugin(ip, conversationHistory) {
try {
const response = await fetch(`https://api.my-mc.link/list_all_servers/${process.env.PATH_KEY}/`, {
headers: {
'x-my-mc-auth': process.env.API_KEY
}
});
const data = await response.json();
if (data.success) {
let responseMessage = `Information provided by the system not me: The Current Minecraft Servers online within the My-MC.link P2P JUMP Node System are listed below. These servers are also listed within My-MC Realms.`;
for (const server of data.servers) {
responseMessage += `\nName: ${server.serverName}\n`;
responseMessage += `Game Version: ${server.gameVersion}\n`;
responseMessage += `MOTD: ${server.motd}\n`;
responseMessage += `Online: ${server.online}\n`;
// Add more details as needed
}
conversationHistory[ip].push({
role: 'assistant',
content: responseMessage
});
console.log("A request for server information was made. Response: " + responseMessage);
} else {
console.log('Failed to fetch server information.');
}
} catch (error) {
console.error('Error fetching server information:', error);
}
}
async function handleMyMcPlugin(ip, conversationHistory) {
try {
const response = await fetch('https://my-mc.link/wiki.json');
const data = await response.json();
if (data) {
let responseMessage = `My-MC.Link Wiki:\n`;
// Append all data fields to the response message
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
// Handle nested objects
responseMessage += `${key}:\n`;
Object.keys(data[key]).forEach(innerKey => {
responseMessage += `${innerKey}: ${data[key][innerKey]}\n`;
});
} else {
responseMessage += `${key}: ${data[key]}\n`;
}
});
conversationHistory[ip].push({
role: 'assistant',
content: responseMessage
});
console.log("A request for My-MC.Link wiki information was made.");
} else {
console.log('Failed to fetch My-MC.Link wiki information.');
}
} catch (error) {
console.error('Error fetching My-MC.Link wiki information:', error);
}
}
async function handleDlinuxPlugin(ip, conversationHistory) {
try {
const response = await fetch('https://my-mc.link/dwiki.json');
const data = await response.json();
if (data) {
let responseMessage = `dlinux Wiki:\n`;
// Append all data fields to the response message
Object.keys(data).forEach(key => {
if (typeof data[key] === 'object') {
// Handle nested objects
responseMessage += `${key}:\n`;
Object.keys(data[key]).forEach(innerKey => {
responseMessage += `${innerKey}: ${data[key][innerKey]}\n`;
});
} else {
responseMessage += `${key}: ${data[key]}\n`;
}
});
conversationHistory[ip].push({
role: 'assistant',
content: responseMessage
});
console.log("A request for dlinux wiki information was made.");
} else {
console.log('Failed to fetch dlinux wiki information.');
}
} catch (error) {
console.error('Error fetching dlinux wiki information:', error);
}
}
async function handleSearchPlugin(searchQuery, ip, conversationHistory) {
const options = {
query: searchQuery,
limit: 5,
disableConsole: true
};
try {
const results = await googleIt(options);
let searchResponse = `Search Query: ${searchQuery}\n`;
searchResponse += `Top Google search results:\n`;
let scrapedContent = '';
for (let i = 0; i < results.length; i++) {
const result = results[i];
searchResponse += `${i + 1}. ${result.title} - ${result.link}\n`;
try {
const scrapeResult = await scrapeWebPage(result.link, 400);
searchResponse += `Scraped Data: ${scrapeResult}\n`;
scrapedContent += `Scraped Data from ${result.link}:\n${scrapeResult}\n`;
} catch (scrapeErr) {
console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr);
searchResponse += `Failed to scrape URL: ${result.link}\n`;
scrapedContent += `Failed to scrape URL: ${result.link}\n`;
}
}
const lastMessageIndex = conversationHistory[ip].length - 1;
if (lastMessageIndex >= 0) {
conversationHistory[ip][lastMessageIndex].content += "\nYou scraped these results, review them provide links: " + searchResponse;
console.log(`${getTimestamp()} [INFO] Processed search query: ${searchQuery}, response: ${searchResponse}`);
} else {
console.error(`${getTimestamp()} [ERROR] Conversation history is unexpectedly empty for: ${ip}`);
}
if (scrapedContent) {
conversationHistory[ip].push({
role: 'assistant',
content: scrapedContent
});
console.log(`${getTimestamp()} [INFO] Added scraped content to conversation history for: ${ip}`);
}
} catch (err) {
console.error(`${getTimestamp()} [ERROR] Failed to perform Google search: ${searchQuery}`, err);
}
}
app.get('/api/v1/conversation-history', (req, res) => { app.get('/api/v1/conversation-history', (req, res) => {
const ip = req.clientIp; const ip = req.clientIp;
console.log(`${getTimestamp()} [INFO] Fetching conversation history for: ${ip}`); // Log the IP address console.log(`${getTimestamp()} [INFO] Fetching conversation history for: ${ip}`); // Log the IP address