From ae9b818c1e202fe96d981a98503460a45852f06c Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 00:01:18 +0300 Subject: [PATCH 1/7] refactor: Update index.js to log AI response text --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index a6a2954..53750ab 100644 --- a/index.js +++ b/index.js @@ -67,7 +67,7 @@ export default async ({ req, res, log, error }) => { log(`userId: ${req.body.to}`); const aiResponse = await handleInteraction(userMessage); - log(JSON.stringify(aiResponse)); + log(aiResponse.text()); const correction = aiResponse.text(); const correctionObj = JSON.parse(correction); From 0ffd7d625c2de53d21c844d92f3c1ed535aa2ad6 Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 00:08:06 +0300 Subject: [PATCH 2/7] fix: Update aiHandler.spec.js to fix response assignment bug --- copilot/aiHandler.spec.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/copilot/aiHandler.spec.js b/copilot/aiHandler.spec.js index 00bf6bc..99e7a85 100644 --- a/copilot/aiHandler.spec.js +++ b/copilot/aiHandler.spec.js @@ -7,7 +7,8 @@ describe("handleInteraction function", () => { const userMessage = "I have an apple."; // Call the handleInteraction function with the user message and chat history - const response = await handleInteraction(userMessage, chatHistory); + let response = await handleInteraction(userMessage, chatHistory); + response = JSON.parse(response.text()); // Check that the response object has 'correction' and 'explanation' properties expect(response).toHaveProperty("correction"); @@ -23,7 +24,8 @@ describe("handleInteraction function", () => { const userMessage = "I have a apple"; // Call the handleInteraction function with the user message and chat history - const response = await handleInteraction(userMessage, chatHistory); + let response = await handleInteraction(userMessage, chatHistory); + response = JSON.parse(response.text()); // Check that the response object has 'correction' and 'explanation' properties expect(response).toHaveProperty("correction"); From 63dad073e5b539cfcdc11c3ac42a3c7cd209d395 Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 00:34:06 +0300 Subject: [PATCH 3/7] refactor: Improve error handling and logging in aiHandler.js and index.js --- copilot/aiHandler.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/copilot/aiHandler.js b/copilot/aiHandler.js index 46dd09f..f259352 100644 --- a/copilot/aiHandler.js +++ b/copilot/aiHandler.js @@ -5,7 +5,7 @@ import { genAI, safetySettings } from "../utils/common.js"; dotenv.config(); const systemInstruction = decodeURIComponent(process.env.SYSTEM_INSTRUCTION); -const chatHistory = JSON.parse(process.env.CHAT_HISTORY); +const chatHistory = JSON.parse(process.env.CHAT_HISTORY) || []; const model = genAI.getGenerativeModel({ model: "gemini-1.5-pro-latest", @@ -20,15 +20,21 @@ const generationConfig = { responseMimeType: "application/json", }; -export async function handleInteraction(userMessage) { - const chatSession = model.startChat({ - generationConfig, - safetySettings, - history: chatHistory, - }); - - const result = await chatSession.sendMessage(userMessage); - - // Print the response text to the console - return result.response; +async function handleInteraction(userMessage) { + try { + const chatSession = model.startChat({ + generationConfig, + safetySettings, + history: chatHistory, + }); + + // Send user message + const result = await chatSession.sendMessage(userMessage); + return result.response; + } catch (error) { + console.error("Error in handleInteraction:", error); + throw error; + } } + +export { handleInteraction }; From 6451404b114a3543a1ef5169aa0b20ec6b406eb2 Mon Sep 17 00:00:00 2001 From: xue Date: Sun, 2 Jun 2024 19:23:25 +0300 Subject: [PATCH 4/7] refactor: Improve error handling and logging in aiHandler.js and index.js --- copilot/aiHandler.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/copilot/aiHandler.js b/copilot/aiHandler.js index f259352..8c66f38 100644 --- a/copilot/aiHandler.js +++ b/copilot/aiHandler.js @@ -22,6 +22,15 @@ const generationConfig = { async function handleInteraction(userMessage) { try { + if ( + !userMessage || + typeof userMessage !== "string" || + userMessage.trim() === "" + ) { + console.error("Invalid user message:", userMessage); + throw new Error("Invalid user message"); + } + const chatSession = model.startChat({ generationConfig, safetySettings, @@ -30,6 +39,12 @@ async function handleInteraction(userMessage) { // Send user message const result = await chatSession.sendMessage(userMessage); + if (!result || !result.response) { + console.error("Invalid response from generative model:", result); + throw new Error("Invalid response from generative model"); + } + + // Return the corrected message return result.response; } catch (error) { console.error("Error in handleInteraction:", error); From 792b18dc18d43068983a16f0e834be9ee097c7e8 Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 03:36:36 +0300 Subject: [PATCH 5/7] refactor: Add write permission for sender in index.js --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 53750ab..7f7f007 100644 --- a/index.js +++ b/index.js @@ -88,7 +88,10 @@ export default async ({ req, res, log, error }) => { roomId: roomId, messageId: req.body.$id, }, - [`read("user:${req.body.sender}")`] + [ + `read("user:${req.body.sender}")`, + `write("user:${req.body.sender}")`, + ] ); log(`Successfully created copilot document: ${copilotDoc.$id}`); From 627d4903705cf353719823b420d8d9c33aac91c4 Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 06:23:54 +0300 Subject: [PATCH 6/7] refactor: Parse response text as JSON in bot.js --- discord/bot.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/discord/bot.js b/discord/bot.js index 1b4258c..61a1bc9 100644 --- a/discord/bot.js +++ b/discord/bot.js @@ -58,7 +58,8 @@ client.on("interactionCreate", async (interaction) => { // Acknowledge the interaction to avoid timing out await interaction.deferReply(); - const response = await handleInteraction(userMessage); + let response = await handleInteraction(userMessage); + response = JSON.parse(response.text()); if (response.correction) { await interaction.editReply( From 4d2340e3f09afc3866d6ac7481edbc224f10304b Mon Sep 17 00:00:00 2001 From: xue Date: Mon, 3 Jun 2024 06:24:38 +0300 Subject: [PATCH 7/7] Bump version to 0.1.1 in package-lock.json and package.json --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 962d4d9..7a1b463 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@langx/copilot", - "version": "0.1.0", + "version": "0.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@langx/copilot", - "version": "0.1.0", + "version": "0.1.1", "license": "BSD-3-Clause", "dependencies": { "@discordjs/rest": "^2.3.0", diff --git a/package.json b/package.json index d7e5819..bedc17b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@langx/copilot", - "version": "0.1.0", + "version": "0.1.1", "type": "module", "description": "An introduction to LangX Copilot, an AI-powered tool designed to enhance your language learning experience with personalized feedback and privacy-focused features.", "scripts": {