1- import { MESSAGES_BEFORE_LOGIN , RATE_LIMIT } from "$env/static/private" ;
1+ import { MESSAGES_BEFORE_LOGIN } from "$env/static/private" ;
22import { authCondition , requiresUser } from "$lib/server/auth" ;
33import { collections } from "$lib/server/database" ;
44import { models } from "$lib/server/models" ;
@@ -19,6 +19,7 @@ import { buildSubtree } from "$lib/utils/tree/buildSubtree.js";
1919import { addChildren } from "$lib/utils/tree/addChildren.js" ;
2020import { addSibling } from "$lib/utils/tree/addSibling.js" ;
2121import { preprocessMessages } from "$lib/server/preprocessMessages.js" ;
22+ import { usageLimits } from "$lib/server/usageLimits" ;
2223
2324export async function POST ( { request, locals, params, getClientAddress } ) {
2425const id = z . string ( ) . parse ( params . id ) ;
@@ -95,14 +96,22 @@ export async function POST({ request, locals, params, getClientAddress }) {
9596}
9697}
9798
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+ }
103109
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+ ) ;
106115}
107116
108117// fetch the model
@@ -125,14 +134,23 @@ export async function POST({ request, locals, params, getClientAddress }) {
125134} = z
126135. object ( {
127136id : 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+ ) ,
129144is_retry : z . optional ( z . boolean ( ) ) ,
130145is_continue : z . optional ( z . boolean ( ) ) ,
131146web_search : z . optional ( z . boolean ( ) ) ,
132147files : z . optional ( z . array ( z . string ( ) ) ) ,
133148} )
134149. parse ( json ) ;
135150
151+ if ( usageLimits ?. messageLength && ( newPrompt ?. length ?? 0 ) > usageLimits . messageLength ) {
152+ throw error ( 400 , "Message too long." ) ;
153+ }
136154// files is an array of base64 strings encoding Blob objects
137155// we need to convert this array to an array of File objects
138156
0 commit comments