Enforce chat session limit to a maximum of 10 per user.
This commit is contained in:
@@ -167,6 +167,12 @@ const listChatSessionsStmt = db.prepare(`
|
|||||||
ORDER BY datetime(updated_at) DESC
|
ORDER BY datetime(updated_at) DESC
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
const countChatSessionsStmt = db.prepare(`
|
||||||
|
SELECT COUNT(*) as count
|
||||||
|
FROM chat_sessions
|
||||||
|
WHERE username = ?
|
||||||
|
`);
|
||||||
|
|
||||||
const getChatSessionStmt = db.prepare(
|
const getChatSessionStmt = db.prepare(
|
||||||
"SELECT id, title FROM chat_sessions WHERE id = ? AND username = ?"
|
"SELECT id, title FROM chat_sessions WHERE id = ? AND username = ?"
|
||||||
);
|
);
|
||||||
@@ -291,6 +297,12 @@ app.get("/chats", requireLogin, (req, res) => {
|
|||||||
|
|
||||||
app.post("/chats", requireLogin, (req, res) => {
|
app.post("/chats", requireLogin, (req, res) => {
|
||||||
const user = req.session.user;
|
const user = req.session.user;
|
||||||
|
|
||||||
|
const sessionCount = countChatSessionsStmt.get(user).count;
|
||||||
|
if (sessionCount >= 10) {
|
||||||
|
return res.status(400).json({error: "Maximum of 10 chats reached. Please delete an old chat to create a new one."});
|
||||||
|
}
|
||||||
|
|
||||||
const title = (req.body?.title || "New chat").trim() || "New chat";
|
const title = (req.body?.title || "New chat").trim() || "New chat";
|
||||||
const chatId = insertChatSessionStmt.run(user, title).lastInsertRowid;
|
const chatId = insertChatSessionStmt.run(user, title).lastInsertRowid;
|
||||||
res.json({chat: {id: chatId, title}});
|
res.json({chat: {id: chatId, title}});
|
||||||
@@ -388,31 +400,22 @@ app.post("/chat", requireLogin, async (req, res) => {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error(`AI HTTP ${response.status}`);
|
throw new Error(`AI HTTP ${response.status}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
const reader = response.body.getReader();
|
const reader = response.body.getReader();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
|
|
||||||
let buffer = "";
|
let buffer = "";
|
||||||
let fullReply = "";
|
let fullReply = "";
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
const {value, done} = await reader.read();
|
const {value, done} = await reader.read();
|
||||||
if (done) break;
|
if (done) break;
|
||||||
|
|
||||||
buffer += decoder.decode(value, {stream: true});
|
buffer += decoder.decode(value, {stream: true});
|
||||||
|
|
||||||
let lines = buffer.split("\n");
|
let lines = buffer.split("\n");
|
||||||
buffer = lines.pop();
|
buffer = lines.pop();
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
if (!line.trim()) continue;
|
if (!line.trim()) continue;
|
||||||
|
|
||||||
const json = JSON.parse(line);
|
const json = JSON.parse(line);
|
||||||
|
|
||||||
if (json.done) {
|
if (json.done) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (json.message?.content) {
|
if (json.message?.content) {
|
||||||
fullReply += json.message.content;
|
fullReply += json.message.content;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user