@@ -46,6 +46,26 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
46
46
} ) ;
47
47
}
48
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
+
49
69
const enableAssistants = ENABLE_ASSISTANTS === "true" ;
50
70
51
71
const assistantActive = ! models . map ( ( { id } ) => id ) . includes ( settings ?. activeModel ?? "" ) ;
@@ -83,29 +103,6 @@ export const load: LayoutServerLoad = async ({ locals, depends }) => {
83
103
84
104
const assistants = await collections . assistants . find ( { _id : { $in : assistantIds } } ) . toArray ( ) ;
85
105
86
- const messagesBeforeLogin = MESSAGES_BEFORE_LOGIN ? parseInt ( MESSAGES_BEFORE_LOGIN ) : 0 ;
87
-
88
- let loginRequired = conversations . length > messagesBeforeLogin ;
89
-
90
- if ( ! loginRequired && messagesBeforeLogin && requiresUser && ! locals . user ) {
91
- // get the number of messages where `from === "assistant"` across all conversations.
92
- const totalMessages =
93
- (
94
- await collections . conversations
95
- . aggregate ( [
96
- { $match : authCondition ( locals ) , "messages.from" : "assistant" } ,
97
- { $project : { messages : 1 } } ,
98
- { $limit : messagesBeforeLogin + 1 } ,
99
- { $unwind : "$messages" } ,
100
- { $match : { "messages.from" : "assistant" } } ,
101
- { $count : "messages" } ,
102
- ] )
103
- . toArray ( )
104
- ) [ 0 ] ?. messages ?? 0 ;
105
-
106
- loginRequired = totalMessages > messagesBeforeLogin ;
107
- }
108
-
109
106
return {
110
107
conversations : conversations . map ( ( conv ) => {
111
108
if ( settings ?. hideEmojiOnSidebar ) {
0 commit comments