@@ -10,6 +10,8 @@ import type { WebSearch } from "$lib/types/WebSearch";
10
10
import { generateQuery } from "$lib/server/websearch/generateQuery" ;
11
11
import { parseWeb } from "$lib/server/websearch/parseWeb" ;
12
12
import { summarizeWeb } from "$lib/server/websearch/summarizeWeb" ;
13
+ import { RATE_LIMIT } from "$env/static/private" ;
14
+ import { ERROR_MESSAGES } from "$lib/stores/errors.js" ;
13
15
14
16
interface GenericObject {
15
17
[ key : string ] : GenericObject | unknown ;
@@ -22,7 +24,7 @@ function removeLinks(obj: GenericObject) {
22
24
}
23
25
return obj ;
24
26
}
25
- export async function GET ( { params, locals, url } ) {
27
+ export async function GET ( { params, locals, url, getClientAddress } ) {
26
28
const model = defaultModel ;
27
29
const convId = new ObjectId ( params . id ) ;
28
30
const searchId = new ObjectId ( ) ;
@@ -36,6 +38,23 @@ export async function GET({ params, locals, url }) {
36
38
throw error ( 404 , "Conversation not found" ) ;
37
39
}
38
40
41
+ const userId = locals . user ?. _id ?? locals . sessionId ;
42
+
43
+ await collections . messageEvents . insertOne ( {
44
+ userId : userId ,
45
+ createdAt : new Date ( ) ,
46
+ ip : getClientAddress ( ) ,
47
+ } ) ;
48
+
49
+ const nEvents = Math . max (
50
+ await collections . messageEvents . countDocuments ( { userId } ) ,
51
+ await collections . messageEvents . countDocuments ( { ip : getClientAddress ( ) } )
52
+ ) ;
53
+
54
+ if ( RATE_LIMIT != "" && nEvents > parseInt ( RATE_LIMIT ) ) {
55
+ throw error ( 429 , ERROR_MESSAGES . rateLimited ) ;
56
+ }
57
+
39
58
const prompt = z . string ( ) . trim ( ) . min ( 1 ) . parse ( url . searchParams . get ( "prompt" ) ) ;
40
59
41
60
const messages = ( ( ) => {
0 commit comments