From 24823cb5e2cea1f5cba5ac8f79a2fb5a0b58adfa Mon Sep 17 00:00:00 2001
From: timothycarambat <rambat1010@gmail.com>
Date: Wed, 1 Nov 2023 14:12:27 -0700
Subject: [PATCH] patch workspace chat history windows to persist most recent
 chats, not the top n

---
 server/models/workspaceChats.js | 17 +++++++++--------
 server/utils/chats/index.js     | 32 +++++++++++++++++++++++++-------
 2 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/server/models/workspaceChats.js b/server/models/workspaceChats.js
index 686dada98..2e3a98a5d 100644
--- a/server/models/workspaceChats.js
+++ b/server/models/workspaceChats.js
@@ -21,7 +21,8 @@ const WorkspaceChats = {
   forWorkspaceByUser: async function (
     workspaceId = null,
     userId = null,
-    limit = null
+    limit = null,
+    orderBy = null
   ) {
     if (!workspaceId || !userId) return [];
     try {
@@ -32,9 +33,7 @@ const WorkspaceChats = {
           include: true,
         },
         ...(limit !== null ? { take: limit } : {}),
-        orderBy: {
-          id: "asc",
-        },
+        ...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
       });
       return chats;
     } catch (error) {
@@ -43,7 +42,11 @@ const WorkspaceChats = {
     }
   },
 
-  forWorkspace: async function (workspaceId = null, limit = null) {
+  forWorkspace: async function (
+    workspaceId = null,
+    limit = null,
+    orderBy = null
+  ) {
     if (!workspaceId) return [];
     try {
       const chats = await prisma.workspace_chats.findMany({
@@ -52,9 +55,7 @@ const WorkspaceChats = {
           include: true,
         },
         ...(limit !== null ? { take: limit } : {}),
-        orderBy: {
-          id: "asc",
-        },
+        ...(orderBy !== null ? { orderBy } : { orderBy: { id: "asc" } }),
       });
       return chats;
     } catch (error) {
diff --git a/server/utils/chats/index.js b/server/utils/chats/index.js
index 3873c3ac0..77d413323 100644
--- a/server/utils/chats/index.js
+++ b/server/utils/chats/index.js
@@ -87,10 +87,22 @@ async function chatWithWorkspace(
     };
   }
 
+  const messageLimit = workspace?.openAiHistory || 20;
   const hasVectorizedSpace = await VectorDb.hasNamespace(workspace.slug);
   const embeddingsCount = await VectorDb.namespaceCount(workspace.slug);
   if (!hasVectorizedSpace || embeddingsCount === 0) {
-    const rawHistory = await WorkspaceChats.forWorkspace(workspace.id);
+    const rawHistory = (
+      user
+        ? await WorkspaceChats.forWorkspaceByUser(
+            workspace.id,
+            user.id,
+            messageLimit,
+            { id: "desc" }
+          )
+        : await WorkspaceChats.forWorkspace(workspace.id, messageLimit, {
+            id: "desc",
+          })
+    ).reverse();
     const chatHistory = convertToPromptHistory(rawHistory);
     const response = await LLMConnector.sendChat(
       chatHistory,
@@ -114,12 +126,18 @@ async function chatWithWorkspace(
       error: null,
     };
   } else {
-    var messageLimit = workspace?.openAiHistory;
-
-    const rawHistory = await WorkspaceChats.forWorkspace(
-      workspace.id,
-      messageLimit
-    );
+    const rawHistory = (
+      user
+        ? await WorkspaceChats.forWorkspaceByUser(
+            workspace.id,
+            user.id,
+            messageLimit,
+            { id: "desc" }
+          )
+        : await WorkspaceChats.forWorkspace(workspace.id, messageLimit, {
+            id: "desc",
+          })
+    ).reverse();
     const chatHistory = convertToPromptHistory(rawHistory);
     const {
       response,
-- 
GitLab