1
- import { MESSAGES_BEFORE_LOGIN , RATE_LIMIT } from "$env/static/private" ;
1
+ import { MESSAGES_BEFORE_LOGIN } from "$env/static/private" ;
2
2
import { authCondition , requiresUser } from "$lib/server/auth" ;
3
3
import { collections } from "$lib/server/database" ;
4
4
import { models } from "$lib/server/models" ;
@@ -19,6 +19,7 @@ import { buildSubtree } from "$lib/utils/tree/buildSubtree.js";
19
19
import { addChildren } from "$lib/utils/tree/addChildren.js" ;
20
20
import { addSibling } from "$lib/utils/tree/addSibling.js" ;
21
21
import { preprocessMessages } from "$lib/server/preprocessMessages.js" ;
22
+ import { usageLimits } from "$lib/server/usageLimits" ;
22
23
23
24
export async function POST ( { request, locals, params, getClientAddress } ) {
24
25
const id = z . string ( ) . parse ( params . id ) ;
@@ -95,14 +96,22 @@ export async function POST({ request, locals, params, getClientAddress }) {
95
96
}
96
97
}
97
98
98
- // check if the user is rate limited
99
- const nEvents = Math . max (
100
- await collections . messageEvents . countDocuments ( { userId } ) ,
101
- await collections . messageEvents . countDocuments ( { ip : getClientAddress ( ) } )
102
- ) ;
99
+ if ( usageLimits ?. messagesPerMinute ) {
100
+ // check if the user is rate limited
101
+ const nEvents = Math . max (
102
+ await collections . messageEvents . countDocuments ( { userId } ) ,
103
+ await collections . messageEvents . countDocuments ( { ip : getClientAddress ( ) } )
104
+ ) ;
105
+ if ( nEvents > usageLimits . messagesPerMinute ) {
106
+ throw error ( 429 , ERROR_MESSAGES . rateLimited ) ;
107
+ }
108
+ }
103
109
104
- if ( RATE_LIMIT != "" && nEvents > parseInt ( RATE_LIMIT ) ) {
105
- throw error ( 429 , ERROR_MESSAGES . rateLimited ) ;
110
+ if ( usageLimits ?. messages && conv . messages . length > usageLimits . messages ) {
111
+ throw error (
112
+ 429 ,
113
+ `This conversation has more than ${ usageLimits . messages } messages. Start a new one to continue`
114
+ ) ;
106
115
}
107
116
108
117
// fetch the model
@@ -125,14 +134,23 @@ export async function POST({ request, locals, params, getClientAddress }) {
125
134
} = z
126
135
. object ( {
127
136
id : z . string ( ) . uuid ( ) . refine ( isMessageId ) . optional ( ) , // parent message id to append to for a normal message, or the message id for a retry/continue
128
- inputs : z . optional ( z . string ( ) . trim ( ) . min ( 1 ) ) ,
137
+ inputs : z . optional (
138
+ z
139
+ . string ( )
140
+ . trim ( )
141
+ . min ( 1 )
142
+ . transform ( ( s ) => s . replace ( / \r \n / g, "\n" ) )
143
+ ) ,
129
144
is_retry : z . optional ( z . boolean ( ) ) ,
130
145
is_continue : z . optional ( z . boolean ( ) ) ,
131
146
web_search : z . optional ( z . boolean ( ) ) ,
132
147
files : z . optional ( z . array ( z . string ( ) ) ) ,
133
148
} )
134
149
. parse ( json ) ;
135
150
151
+ if ( usageLimits ?. messageLength && ( newPrompt ?. length ?? 0 ) > usageLimits . messageLength ) {
152
+ throw error ( 400 , "Message too long." ) ;
153
+ }
136
154
// files is an array of base64 strings encoding Blob objects
137
155
// we need to convert this array to an array of File objects
138
156
0 commit comments