Spaces:
Running
Running
[Mongo] count messages (aggregate) only when needed V2 (#866)
Browse filesfixes #863
needed
https://github.com/huggingface/chat-ui/pull/866/commits/9e67e76d75d7257abc5573ae3ec01a8a0040a784
---------
Co-authored-by: Eliott C. <[email protected]>
src/routes/+layout.server.ts
CHANGED
@@ -46,26 +46,6 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
|
|
46 |
});
|
47 |
}
|
48 |
|
49 |
-
// get the number of messages where `from === "assistant"` across all conversations.
|
50 |
-
const totalMessages =
|
51 |
-
(
|
52 |
-
await collections.conversations
|
53 |
-
.aggregate([
|
54 |
-
{ $match: authCondition(locals) },
|
55 |
-
{ $project: { messages: 1 } },
|
56 |
-
{ $unwind: "$messages" },
|
57 |
-
{ $match: { "messages.from": "assistant" } },
|
58 |
-
{ $count: "messages" },
|
59 |
-
])
|
60 |
-
.toArray()
|
61 |
-
)[0]?.messages ?? 0;
|
62 |
-
|
63 |
-
const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0;
|
64 |
-
|
65 |
-
const userHasExceededMessages = messagesBeforeLogin > 0 && totalMessages > messagesBeforeLogin;
|
66 |
-
|
67 |
-
const loginRequired = requiresUser && !locals.user && userHasExceededMessages;
|
68 |
-
|
69 |
const enableAssistants = ENABLE_ASSISTANTS === "true";
|
70 |
|
71 |
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
|
@@ -103,6 +83,33 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
|
|
103 |
|
104 |
const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
return {
|
107 |
conversations: conversations.map((conv) => {
|
108 |
if (settings?.hideEmojiOnSidebar) {
|
|
|
46 |
});
|
47 |
}
|
48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
const enableAssistants = ENABLE_ASSISTANTS === "true";
|
50 |
|
51 |
const assistantActive = !models.map(({ id }) => id).includes(settings?.activeModel ?? "");
|
|
|
83 |
|
84 |
const assistants = await collections.assistants.find({ _id: { $in: assistantIds } }).toArray();
|
85 |
|
86 |
+
const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0;
|
87 |
+
|
88 |
+
let loginRequired = false;
|
89 |
+
|
90 |
+
if (requiresUser && !locals.user && messagesBeforeLogin) {
|
91 |
+
if (conversations.length > messagesBeforeLogin) {
|
92 |
+
loginRequired = true;
|
93 |
+
} else {
|
94 |
+
// get the number of messages where `from === "assistant"` across all conversations.
|
95 |
+
const totalMessages =
|
96 |
+
(
|
97 |
+
await collections.conversations
|
98 |
+
.aggregate([
|
99 |
+
{ $match: { ...authCondition(locals), "messages.from": "assistant" } },
|
100 |
+
{ $project: { messages: 1 } },
|
101 |
+
{ $limit: messagesBeforeLogin + 1 },
|
102 |
+
{ $unwind: "$messages" },
|
103 |
+
{ $match: { "messages.from": "assistant" } },
|
104 |
+
{ $count: "messages" },
|
105 |
+
])
|
106 |
+
.toArray()
|
107 |
+
)[0]?.messages ?? 0;
|
108 |
+
|
109 |
+
loginRequired = totalMessages > messagesBeforeLogin;
|
110 |
+
}
|
111 |
+
}
|
112 |
+
|
113 |
return {
|
114 |
conversations: conversations.map((conv) => {
|
115 |
if (settings?.hideEmojiOnSidebar) {
|
src/routes/conversation/[id]/+server.ts
CHANGED
@@ -72,18 +72,17 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
72 |
ip: getClientAddress(),
|
73 |
});
|
74 |
|
|
|
|
|
75 |
// guest mode check
|
76 |
-
if (
|
77 |
-
!locals.user?._id &&
|
78 |
-
requiresUser &&
|
79 |
-
(MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0) > 0
|
80 |
-
) {
|
81 |
const totalMessages =
|
82 |
(
|
83 |
await collections.conversations
|
84 |
.aggregate([
|
85 |
-
{ $match: authCondition(locals) },
|
86 |
{ $project: { messages: 1 } },
|
|
|
87 |
{ $unwind: "$messages" },
|
88 |
{ $match: { "messages.from": "assistant" } },
|
89 |
{ $count: "messages" },
|
@@ -91,7 +90,7 @@ export async function POST({ request, locals, params, getClientAddress }) {
|
|
91 |
.toArray()
|
92 |
)[0]?.messages ?? 0;
|
93 |
|
94 |
-
if (totalMessages >
|
95 |
throw error(429, "Exceeded number of messages before login");
|
96 |
}
|
97 |
}
|
|
|
72 |
ip: getClientAddress(),
|
73 |
});
|
74 |
|
75 |
+
const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt(MESSAGES_BEFORE_LOGIN) : 0;
|
76 |
+
|
77 |
// guest mode check
|
78 |
+
if (!locals.user?._id && requiresUser && messagesBeforeLogin) {
|
|
|
|
|
|
|
|
|
79 |
const totalMessages =
|
80 |
(
|
81 |
await collections.conversations
|
82 |
.aggregate([
|
83 |
+
{ $match: { ...authCondition(locals), "messages.from": "assistant" } },
|
84 |
{ $project: { messages: 1 } },
|
85 |
+
{ $limit: messagesBeforeLogin + 1 },
|
86 |
{ $unwind: "$messages" },
|
87 |
{ $match: { "messages.from": "assistant" } },
|
88 |
{ $count: "messages" },
|
|
|
90 |
.toArray()
|
91 |
)[0]?.messages ?? 0;
|
92 |
|
93 |
+
if (totalMessages > messagesBeforeLogin) {
|
94 |
throw error(429, "Exceeded number of messages before login");
|
95 |
}
|
96 |
}
|