Use userSessionId for chat history
This commit is contained in:
parent
27e6f5b9e1
commit
1eee511aac
5 changed files with 30 additions and 4 deletions
|
@ -16,4 +16,5 @@ export const chats = sqliteTable('chats', {
|
||||||
title: text('title').notNull(),
|
title: text('title').notNull(),
|
||||||
createdAt: text('createdAt').notNull(),
|
createdAt: text('createdAt').notNull(),
|
||||||
focusMode: text('focusMode').notNull(),
|
focusMode: text('focusMode').notNull(),
|
||||||
|
userSessionId: text('userSessionId'),
|
||||||
});
|
});
|
||||||
|
|
|
@ -6,13 +6,20 @@ import { chats, messages } from '../db/schema';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
router.get('/', async (_, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
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) {
|
} catch (err) {
|
||||||
res.status(500).json({ message: 'An error has occurred.' });
|
res.status(500).json({ message: 'An error has occurred.' });
|
||||||
logger.error(`Error in getting chats: ${err.message}`);
|
logger.error(`Error in getting chats: ${err.message}`);
|
||||||
|
|
|
@ -18,6 +18,7 @@ type Message = {
|
||||||
messageId: string;
|
messageId: string;
|
||||||
chatId: string;
|
chatId: string;
|
||||||
content: string;
|
content: string;
|
||||||
|
userSessionId: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
type WSMessage = {
|
type WSMessage = {
|
||||||
|
@ -154,6 +155,7 @@ export const handleMessage = async (
|
||||||
title: parsedMessage.content,
|
title: parsedMessage.content,
|
||||||
createdAt: new Date().toString(),
|
createdAt: new Date().toString(),
|
||||||
focusMode: parsedWSMessage.focusMode,
|
focusMode: parsedWSMessage.focusMode,
|
||||||
|
userSessionId: parsedMessage.userSessionId,
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
import crypto from 'crypto';
|
||||||
import DeleteChat from '@/components/DeleteChat';
|
import DeleteChat from '@/components/DeleteChat';
|
||||||
import { formatTimeDifference } from '@/lib/utils';
|
import { formatTimeDifference } from '@/lib/utils';
|
||||||
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
|
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
|
||||||
|
@ -21,10 +22,17 @@ const Page = () => {
|
||||||
const fetchChats = async () => {
|
const fetchChats = async () => {
|
||||||
setLoading(true);
|
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`, {
|
const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/chats`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
|
'user-session-id': userSessionId!,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
@ -38,6 +38,12 @@ const useSocket = (
|
||||||
'embeddingModelProvider',
|
'embeddingModelProvider',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let userSessionId = localStorage.getItem('userSessionId');
|
||||||
|
if (!userSessionId) {
|
||||||
|
userSessionId = crypto.randomBytes(20).toString('hex');
|
||||||
|
localStorage.setItem('userSessionId', userSessionId!)
|
||||||
|
}
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!chatModel ||
|
!chatModel ||
|
||||||
!chatModelProvider ||
|
!chatModelProvider ||
|
||||||
|
@ -324,6 +330,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
||||||
let added = false;
|
let added = false;
|
||||||
|
|
||||||
const messageId = crypto.randomBytes(7).toString('hex');
|
const messageId = crypto.randomBytes(7).toString('hex');
|
||||||
|
let userSessionId = localStorage.getItem('userSessionId');
|
||||||
|
|
||||||
ws?.send(
|
ws?.send(
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
@ -331,6 +338,7 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
||||||
message: {
|
message: {
|
||||||
chatId: chatId!,
|
chatId: chatId!,
|
||||||
content: message,
|
content: message,
|
||||||
|
userSessionId: userSessionId,
|
||||||
},
|
},
|
||||||
focusMode: focusMode,
|
focusMode: focusMode,
|
||||||
history: [...chatHistory, ['human', message]],
|
history: [...chatHistory, ['human', message]],
|
||||||
|
|
Loading…
Add table
Reference in a new issue