From ae82fda49f7a83fe7cc46b1079aff342d44715d0 Mon Sep 17 00:00:00 2001 From: Raven Date: Sat, 3 Aug 2024 16:27:17 -0400 Subject: [PATCH] Limit Search Results to 5 --- backend-server/backend-server.js | 100 ++++++++++++++++--------------- 1 file changed, 53 insertions(+), 47 deletions(-) diff --git a/backend-server/backend-server.js b/backend-server/backend-server.js index 41a982f..0e12277 100644 --- a/backend-server/backend-server.js +++ b/backend-server/backend-server.js @@ -425,53 +425,59 @@ app.post('/api/v1/chat', async (req, res) => { } const searchRegex = /\b[Ss]earch\s+(.+)\b/; -const searchMatch = userMessage.match(searchRegex); - -if (searchMatch) { - const searchQuery = searchMatch[1]; - console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`); - - try { - googleIt({ 'query': searchQuery }).then(async results => { - let searchResponse = `Search Query: ${searchQuery}\n`; - searchResponse += `Top Google search results:\n`; - - 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, 200); - searchResponse += `Scraped Data: ${scrapeResult}\n`; - } catch (scrapeErr) { - console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr); - searchResponse += `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}`); - } - }).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 - }); - }); - } catch (err) { - console.error(`${getTimestamp()} [ERROR] An unexpected error occurred:`, err); - return res.status(500).json({ - message: "An unexpected error occurred", - error: err.message - }); - } -} - + const searchMatch = userMessage.match(searchRegex); + + if (searchMatch) { + const searchQuery = searchMatch[1]; + console.log(`${getTimestamp()} [INFO] Detected search query in user message: ${searchQuery}`); + + const options = { + query: searchQuery, + limit: 5, + disableConsole: true + }; + + try { + googleIt(options).then(async results => { + let searchResponse = `Search Query: ${searchQuery}\n`; + searchResponse += `Top Google search results:\n`; + + 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, 200); + searchResponse += `Scraped Data: ${scrapeResult}\n`; + } catch (scrapeErr) { + console.error(`${getTimestamp()} [ERROR] Failed to scrape URL: ${result.link}`, scrapeErr); + searchResponse += `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}`); + } + }).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 + }); + }); + } catch (err) { + console.error(`${getTimestamp()} [ERROR] An unexpected error occurred:`, err); + return res.status(500).json({ + message: "An unexpected error occurred", + error: err.message + }); + } + } + // End Plugins ---