Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use userSessionId for chat history #268

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ export const chats = sqliteTable('chats', {
title: text('title').notNull(),
createdAt: text('createdAt').notNull(),
focusMode: text('focusMode').notNull(),
userSessionId: text('userSessionId'),
});
15 changes: 11 additions & 4 deletions src/routes/chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,20 @@ import { chats, messages } from '../db/schema';

const router = express.Router();

router.get('/', async (_, res) => {
router.get('/', async (req, res) => {
try {
let chats = await db.query.chats.findMany();
let userSessionId = req.headers['user-session-id'] ? req.headers['user-session-id'].toString() : '';
if (userSessionId == '') {
return res.status(200).json({ chats: {}});
}

let chatRecords = await db.query.chats.findMany({
where: eq(chats.userSessionId, userSessionId),
});

chats = chats.reverse();
chatRecords = chatRecords.reverse();

return res.status(200).json({ chats: chats });
return res.status(200).json({ chats: chatRecords });
} catch (err) {
res.status(500).json({ message: 'An error has occurred.' });
logger.error(`Error in getting chats: ${err.message}`);
Expand Down
2 changes: 2 additions & 0 deletions src/websocket/messageHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Message = {
messageId: string;
chatId: string;
content: string;
userSessionId: string;
};

type WSMessage = {
Expand Down Expand Up @@ -154,6 +155,7 @@ export const handleMessage = async (
title: parsedMessage.content,
createdAt: new Date().toString(),
focusMode: parsedWSMessage.focusMode,
userSessionId: parsedMessage.userSessionId,
})
.execute();
}
Expand Down
8 changes: 8 additions & 0 deletions ui/app/library/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import crypto from 'crypto';
import DeleteChat from '@/components/DeleteChat';
import { formatTimeDifference } from '@/lib/utils';
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
Expand All @@ -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(`${process.env.NEXT_PUBLIC_API_URL}/chats`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'user-session-id': userSessionId!,
},
});

Expand Down
8 changes: 8 additions & 0 deletions ui/components/ChatWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ const useSocket = (
'embeddingModelProvider',
);

let userSessionId = localStorage.getItem('userSessionId');
if (!userSessionId) {
userSessionId = crypto.randomBytes(20).toString('hex');
localStorage.setItem('userSessionId', userSessionId!)
}

if (
!chatModel ||
!chatModelProvider ||
Expand Down Expand Up @@ -324,13 +330,15 @@ const ChatWindow = ({ id }: { id?: string }) => {
let added = false;

const messageId = crypto.randomBytes(7).toString('hex');
let userSessionId = localStorage.getItem('userSessionId');

ws?.send(
JSON.stringify({
type: 'message',
message: {
chatId: chatId!,
content: message,
userSessionId: userSessionId,
},
focusMode: focusMode,
history: [...chatHistory, ['human', message]],
Expand Down