From 29ea0550f4e3c18599d89c2bfb57453ff6c37e16 Mon Sep 17 00:00:00 2001 From: Shamoil Arsiwala Date: Sun, 14 Jul 2024 16:45:22 +0530 Subject: [PATCH 1/4] feat: split answer in chunks --- index.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/index.js b/index.js index 2954b91..dc556df 100644 --- a/index.js +++ b/index.js @@ -5,7 +5,7 @@ require("dotenv").config(); // Secrets const MENDABLE_KEY = process.env["MENDABLE_API_KEY"]; const DISCORD_TOKEN = process.env["DISCORD_TOKEN"]; -const BOT_ID = process.env["BOT_ID"]; // +const BOT_ID = process.env["BOT_ID"]; // const client = new Client({ intents: [ @@ -18,7 +18,6 @@ const client = new Client({ const historyMap = new Map(); const threadToChannelMap = new Map(); - async function createConversation() { const url = "https://api.mendable.ai/v0/newConversation"; @@ -58,9 +57,7 @@ async function getAnswerAndSources(question, history = []) { const response = await fetch(url, { method: "POST", - headers: { - "Content-Type": "application/json", - }, + headers: { "Content-Type": "application/json" }, body: JSON.stringify(data), }); @@ -118,9 +115,9 @@ client.on("messageCreate", async (message) => { const responseJSON = await response.json(); const answer = await responseJSON["answer"]["text"]; - const sources = await responseJSON["sources"] - .map((source) => source["link"]) - .join("\n"); + const sources = await responseJSON["sources"].map( + (source) => source["link"] + ); history.push({ prompt: formattedMessage.trim(), @@ -129,18 +126,31 @@ client.on("messageCreate", async (message) => { }); historyMap.set(threadId, history); // Use thread ID to store history instead of channel ID - if (message.channel.isThread()) { - await message.reply( - `${message.author}\n\n${answer}` - ); - if (sources) { - await message.reply(`Sources:\n${sources}`); - } - } else { - await thread.send(`${message.author}\n\n${answer}\n`); - if (sources) { - await thread.send(`\n\n- Verified Sources:\n${sources}`); + const answersList = []; + let tempString = ""; + let answerIndex = 0; + + const splittedAnswers = answer.split("\n"); + + while (answerIndex < splittedAnswers.length) { + // update 1999 to (n-1) where n is discords per message limit + while (tempString.length + splittedAnswers[answerIndex].length <= 1999) { + tempString += splittedAnswers[answerIndex++] + "\n"; + if (answerIndex === splittedAnswers.length) break; } + answersList.push(tempString); + tempString = ""; + } + + const firstMessage = `${message.author}\n\n${answersList[0]}`; + if (message.channel.isThread()) await message.reply(firstMessage); + else await thread.send(firstMessage); + + for (let i = 1; i < answersList.length; i++) + await thread.send(answersList[i]); + + if (sources) { + await thread.send(`\n\nVerified Sources:\n- ${sources.join("\n- ")}`); } } catch (error) { console.log(error); From bf4e53a4fcfceb0625306cf05b26f567bd375e10 Mon Sep 17 00:00:00 2001 From: Shamoil Arsiwala Date: Sun, 14 Jul 2024 16:50:39 +0530 Subject: [PATCH 2/4] feat: added typing indicator --- index.js | 1 + 1 file changed, 1 insertion(+) diff --git a/index.js b/index.js index dc556df..ea47fed 100644 --- a/index.js +++ b/index.js @@ -104,6 +104,7 @@ client.on("messageCreate", async (message) => { channelId = message.channel.id; // Set the parent channel ID to the current channel ID } + await thread.sendTyping(); // https://discord.com/developers/docs/resources/channel#trigger-typing-indicator threadToChannelMap.set(threadId, channelId); // Add the thread-to-channel mapping let history = historyMap.get(threadId) || []; // Use thread ID to look up history instead of channel ID From 04646fa460dea41f467bc2504474bf13843c2bf1 Mon Sep 17 00:00:00 2001 From: Shamoil Arsiwala Date: Sun, 14 Jul 2024 17:02:03 +0530 Subject: [PATCH 3/4] chore: cleanup --- index.js | 41 +++++++++++++++++++++++------------------ 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/index.js b/index.js index ea47fed..0f0b5e4 100644 --- a/index.js +++ b/index.js @@ -18,6 +18,24 @@ const client = new Client({ const historyMap = new Map(); const threadToChannelMap = new Map(); +function splitInto(str, maxChunkLen) { + const parts = []; + let tempPart = ""; + let i = 0; + + const splittedStr = str.split("\n"); + + while (i < splittedStr.length) { + while (tempPart.length + splittedStr[i].length <= maxChunkLen - 1) { + tempPart += splittedStr[i++] + "\n"; + if (i === splittedStr.length) break; + } + parts.push(tempPart); + tempPart = ""; + } + return parts; +} + async function createConversation() { const url = "https://api.mendable.ai/v0/newConversation"; @@ -127,28 +145,15 @@ client.on("messageCreate", async (message) => { }); historyMap.set(threadId, history); // Use thread ID to store history instead of channel ID - const answersList = []; - let tempString = ""; - let answerIndex = 0; - - const splittedAnswers = answer.split("\n"); - - while (answerIndex < splittedAnswers.length) { - // update 1999 to (n-1) where n is discords per message limit - while (tempString.length + splittedAnswers[answerIndex].length <= 1999) { - tempString += splittedAnswers[answerIndex++] + "\n"; - if (answerIndex === splittedAnswers.length) break; - } - answersList.push(tempString); - tempString = ""; - } + // 2000 is discords current message limit + const answersParts = splitInto(answer, 2000); - const firstMessage = `${message.author}\n\n${answersList[0]}`; + const firstMessage = `${message.author}\n\n${answersParts[0]}`; if (message.channel.isThread()) await message.reply(firstMessage); else await thread.send(firstMessage); - for (let i = 1; i < answersList.length; i++) - await thread.send(answersList[i]); + for (let i = 1; i < answersParts.length; i++) + await thread.send(answersParts[i]); if (sources) { await thread.send(`\n\nVerified Sources:\n- ${sources.join("\n- ")}`); From 299dbc213956cf8e05e345e8e0db687ba7da1041 Mon Sep 17 00:00:00 2001 From: Shamoil Arsiwala Date: Sun, 14 Jul 2024 17:07:24 +0530 Subject: [PATCH 4/4] chore: fix typo --- index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0f0b5e4..ff65d6e 100644 --- a/index.js +++ b/index.js @@ -146,14 +146,14 @@ client.on("messageCreate", async (message) => { historyMap.set(threadId, history); // Use thread ID to store history instead of channel ID // 2000 is discords current message limit - const answersParts = splitInto(answer, 2000); + const answerParts = splitInto(answer, 2000); - const firstMessage = `${message.author}\n\n${answersParts[0]}`; + const firstMessage = `${message.author}\n\n${answerParts[0]}`; if (message.channel.isThread()) await message.reply(firstMessage); else await thread.send(firstMessage); - for (let i = 1; i < answersParts.length; i++) - await thread.send(answersParts[i]); + for (let i = 1; i < answerParts.length; i++) + await thread.send(answerParts[i]); if (sources) { await thread.send(`\n\nVerified Sources:\n- ${sources.join("\n- ")}`);