From 8097610baffaefc8d2b07516bccfc18574ce5297 Mon Sep 17 00:00:00 2001 From: sjiampojamarn <18257803+sjiampojamarn@users.noreply.github.com> Date: Sat, 5 Apr 2025 15:14:25 -0700 Subject: [PATCH 1/2] Adding user session for history --- src/app/api/chat/route.ts | 4 ++++ src/app/api/chats/route.ts | 43 ++++++++++++++++++++++++++++++++--- src/app/library/page.tsx | 8 +++++++ src/components/ChatWindow.tsx | 8 +++++++ src/lib/db/schema.ts | 2 ++ 5 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts index e566edb..c5cc345 100644 --- a/src/app/api/chat/route.ts +++ b/src/app/api/chat/route.ts @@ -29,6 +29,7 @@ type Message = { messageId: string; chatId: string; content: string; + userSessionId: string; }; type ChatModel = { @@ -138,6 +139,7 @@ const handleHistorySave = async ( where: eq(chats.id, message.chatId), }); + let currentDate = new Date(); if (!chat) { await db .insert(chats) @@ -147,6 +149,8 @@ const handleHistorySave = async ( createdAt: new Date().toString(), focusMode: focusMode, files: files.map(getFileDetails), + userSessionId: message.userSessionId, + timestamp: currentDate.toISOString(), }) .execute(); } diff --git a/src/app/api/chats/route.ts b/src/app/api/chats/route.ts index 986a192..94f2b36 100644 --- a/src/app/api/chats/route.ts +++ b/src/app/api/chats/route.ts @@ -1,10 +1,47 @@ import db from '@/lib/db'; +import { chats } from '@/lib/db/schema'; +import { eq, sql} from 'drizzle-orm'; export const GET = async (req: Request) => { try { - let chats = await db.query.chats.findMany(); - chats = chats.reverse(); - return Response.json({ chats: chats }, { status: 200 }); + // get header from request + const headers = await req.headers; + let userSessionId = headers.get('user-session-id')?.toString() ?? ''; + + if (userSessionId == '') { + return Response.json({ chats: {} }, { status: 200 }); + } + + let chatsRes = await db.query.chats.findMany({ + where: eq(chats.userSessionId, userSessionId), + }); + + chatsRes = chatsRes.reverse(); + // Keep only the latest 20 records in the database. Delete older records. + let maxRecordLimit = 20; + if (chatsRes.length > maxRecordLimit) { + const deleteChatsQuery = sql`DELETE FROM chats + WHERE userSessionId = ${userSessionId} AND ( + timestamp IS NULL OR + timestamp NOT in ( + SELECT timestamp FROM chats + WHERE userSessionId = ${userSessionId} + ORDER BY timestamp DESC + LIMIT ${maxRecordLimit} + ) + ) + `; + await db.run(deleteChatsQuery); + // Delete messages that no longer link with the chat from the database. + const deleteMessagesQuery = sql`DELETE FROM messages + WHERE chatId NOT IN ( + SELECT id FROM chats + ) + `; + await db.run(deleteMessagesQuery); + } + + return Response.json({ chats: chatsRes }, { status: 200 }); } catch (err) { console.error('Error in getting chats: ', err); return Response.json( diff --git a/src/app/library/page.tsx b/src/app/library/page.tsx index 9c40b2b..ff0b0e1 100644 --- a/src/app/library/page.tsx +++ b/src/app/library/page.tsx @@ -1,5 +1,6 @@ 'use client'; +import crypto from 'crypto'; import DeleteChat from '@/components/DeleteChat'; import { cn, formatTimeDifference } from '@/lib/utils'; import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react'; @@ -21,10 +22,17 @@ const Page = () => { const fetchChats = async () => { setLoading(true); + let userSessionId = localStorage.getItem('userSessionId'); + if (!userSessionId) { + userSessionId = crypto.randomBytes(20).toString('hex'); + localStorage.setItem('userSessionId', userSessionId) + } + const res = await fetch(`/api/chats`, { method: 'GET', headers: { 'Content-Type': 'application/json', + 'user-session-id': userSessionId!, }, }); diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx index 93c8a0c..15b402d 100644 --- a/src/components/ChatWindow.tsx +++ b/src/components/ChatWindow.tsx @@ -95,6 +95,12 @@ const checkConfig = async ( if (!embeddingModel || !embeddingModelProvider) { const embeddingModelProviders = providers.embeddingModelProviders; + let userSessionId = localStorage.getItem('userSessionId'); + if (!userSessionId) { + userSessionId = crypto.randomBytes(20).toString('hex'); + localStorage.setItem('userSessionId', userSessionId!) + } + if ( !embeddingModelProviders || Object.keys(embeddingModelProviders).length === 0 @@ -342,6 +348,7 @@ const ChatWindow = ({ id }: { id?: string }) => { let added = false; messageId = messageId ?? crypto.randomBytes(7).toString('hex'); + let userSessionId = localStorage.getItem('userSessionId'); setMessages((prevMessages) => [ ...prevMessages, @@ -466,6 +473,7 @@ const ChatWindow = ({ id }: { id?: string }) => { messageId: messageId, chatId: chatId!, content: message, + userSessionId: userSessionId, }, chatId: chatId!, files: fileIds, diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index cee9660..8da7e5d 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -25,4 +25,6 @@ export const chats = sqliteTable('chats', { files: text('files', { mode: 'json' }) .$type() .default(sql`'[]'`), + userSessionId: text('userSessionId'), + timestamp: text('timestamp'), }); From 02f573907022fbac168d45e67ca0fbe08c6924a0 Mon Sep 17 00:00:00 2001 From: sjiampojamarn <18257803+sjiampojamarn@users.noreply.github.com> Date: Sun, 6 Apr 2025 11:42:12 -0700 Subject: [PATCH 2/2] Adding max record limt in settings --- src/app/api/chats/route.ts | 6 ++--- src/app/library/page.tsx | 17 ++++++++++++++ src/app/settings/page.tsx | 44 +++++++++++++++++++++++++++++++++++ src/components/ChatWindow.tsx | 6 +++++ 4 files changed, 70 insertions(+), 3 deletions(-) diff --git a/src/app/api/chats/route.ts b/src/app/api/chats/route.ts index 94f2b36..2395df7 100644 --- a/src/app/api/chats/route.ts +++ b/src/app/api/chats/route.ts @@ -6,7 +6,8 @@ export const GET = async (req: Request) => { try { // get header from request const headers = await req.headers; - let userSessionId = headers.get('user-session-id')?.toString() ?? ''; + const userSessionId = headers.get('user-session-id')?.toString() ?? ''; + const maxRecordLimit = parseInt(headers.get('max-record-limit') || '20', 10); if (userSessionId == '') { return Response.json({ chats: {} }, { status: 200 }); @@ -17,8 +18,7 @@ export const GET = async (req: Request) => { }); chatsRes = chatsRes.reverse(); - // Keep only the latest 20 records in the database. Delete older records. - let maxRecordLimit = 20; + // Keep only the latest records in the database. Delete older records. if (chatsRes.length > maxRecordLimit) { const deleteChatsQuery = sql`DELETE FROM chats WHERE userSessionId = ${userSessionId} AND ( diff --git a/src/app/library/page.tsx b/src/app/library/page.tsx index ff0b0e1..845bcc1 100644 --- a/src/app/library/page.tsx +++ b/src/app/library/page.tsx @@ -28,11 +28,28 @@ const Page = () => { localStorage.setItem('userSessionId', userSessionId) } + // Get maxRecordLimit from localStorage or set default + let maxRecordLimit = localStorage.getItem('maxRecordLimit'); + if (!maxRecordLimit) { + maxRecordLimit = '20'; + localStorage.setItem('maxRecordLimit', maxRecordLimit); + } else { + let valueInt = parseInt(maxRecordLimit, 10) || 20; + if (valueInt < 1) { + valueInt = 1; + } else if (valueInt > 100) { + valueInt = 100; + } + maxRecordLimit = valueInt.toString(); + localStorage.setItem('maxRecordLimit', maxRecordLimit); + } + const res = await fetch(`/api/chats`, { method: 'GET', headers: { 'Content-Type': 'application/json', 'user-session-id': userSessionId!, + 'max-record-limit': maxRecordLimit, }, }); diff --git a/src/app/settings/page.tsx b/src/app/settings/page.tsx index 8e1c45a..8dbefdf 100644 --- a/src/app/settings/page.tsx +++ b/src/app/settings/page.tsx @@ -23,6 +23,7 @@ interface SettingsType { customOpenaiApiKey: string; customOpenaiApiUrl: string; customOpenaiModelName: string; + maxRecordLimit: string; } interface InputProps extends React.InputHTMLAttributes { @@ -145,6 +146,7 @@ const Page = () => { const [automaticVideoSearch, setAutomaticVideoSearch] = useState(false); const [systemInstructions, setSystemInstructions] = useState(''); const [savingStates, setSavingStates] = useState>({}); + const [maxRecordLimit, setMaxRecordLimit] = useState('20'); useEffect(() => { const fetchConfig = async () => { @@ -207,6 +209,8 @@ const Page = () => { setSystemInstructions(localStorage.getItem('systemInstructions')!); + setMaxRecordLimit(localStorage.getItem('maxRecordLimit') || data.maxRecordLimit || '20'); + setIsLoading(false); }; @@ -365,6 +369,15 @@ const Page = () => { localStorage.setItem('embeddingModel', value); } else if (key === 'systemInstructions') { localStorage.setItem('systemInstructions', value); + } else if (key === 'maxRecordLimit') { + let valueInt = parseInt(value, 10) || 20; + if (valueInt < 1) { + valueInt = 1; + } else if (valueInt > 100) { + valueInt = 100; + } + setMaxRecordLimit(valueInt.toString()); + localStorage.setItem('maxRecordLimit', valueInt.toString()); } } catch (err) { console.error('Failed to save:', err); @@ -840,6 +853,37 @@ const Page = () => { + + +
+
+

+ Maximum Chat History Records +

+
+ { + setMaxRecordLimit(e.target.value); + }} + onSave={(value) => saveConfig('maxRecordLimit', value)} + /> + + records + +
+

+ Maximum number of chat records to keep in history. Older records will be automatically deleted. +

+
+
+
) )} diff --git a/src/components/ChatWindow.tsx b/src/components/ChatWindow.tsx index 15b402d..cc63139 100644 --- a/src/components/ChatWindow.tsx +++ b/src/components/ChatWindow.tsx @@ -101,6 +101,12 @@ const checkConfig = async ( localStorage.setItem('userSessionId', userSessionId!) } + let maxRecordLimit = localStorage.getItem('maxRecordLimit'); + if (!maxRecordLimit) { + maxRecordLimit = '20'; + localStorage.setItem('maxRecordLimit', maxRecordLimit); + } + if ( !embeddingModelProviders || Object.keys(embeddingModelProviders).length === 0