nsarrazin HF staff commited on
Commit
0953d85
1 Parent(s): ba93cf8

Add rate limiting to websearch and title summary (#433)

Browse files
src/routes/conversation/[id]/summarize/+server.ts CHANGED
@@ -1,12 +1,14 @@
 
1
  import { buildPrompt } from "$lib/buildPrompt";
2
  import { authCondition } from "$lib/server/auth";
3
  import { collections } from "$lib/server/database";
4
  import { generateFromDefaultEndpoint } from "$lib/server/generateFromDefaultEndpoint";
5
  import { defaultModel } from "$lib/server/models";
 
6
  import { error } from "@sveltejs/kit";
7
  import { ObjectId } from "mongodb";
8
 
9
- export async function POST({ params, locals }) {
10
  const convId = new ObjectId(params.id);
11
 
12
  const conversation = await collections.conversations.findOne({
@@ -18,6 +20,23 @@ export async function POST({ params, locals }) {
18
  throw error(404, "Conversation not found");
19
  }
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  const firstMessage = conversation.messages.find((m) => m.from === "user");
22
 
23
  const userPrompt =
 
1
+ import { RATE_LIMIT } from "$env/static/private";
2
  import { buildPrompt } from "$lib/buildPrompt";
3
  import { authCondition } from "$lib/server/auth";
4
  import { collections } from "$lib/server/database";
5
  import { generateFromDefaultEndpoint } from "$lib/server/generateFromDefaultEndpoint";
6
  import { defaultModel } from "$lib/server/models";
7
+ import { ERROR_MESSAGES } from "$lib/stores/errors.js";
8
  import { error } from "@sveltejs/kit";
9
  import { ObjectId } from "mongodb";
10
 
11
+ export async function POST({ params, locals, getClientAddress }) {
12
  const convId = new ObjectId(params.id);
13
 
14
  const conversation = await collections.conversations.findOne({
 
20
  throw error(404, "Conversation not found");
21
  }
22
 
23
+ const userId = locals.user?._id ?? locals.sessionId;
24
+
25
+ await collections.messageEvents.insertOne({
26
+ userId: userId,
27
+ createdAt: new Date(),
28
+ ip: getClientAddress(),
29
+ });
30
+
31
+ const nEvents = Math.max(
32
+ await collections.messageEvents.countDocuments({ userId }),
33
+ await collections.messageEvents.countDocuments({ ip: getClientAddress() })
34
+ );
35
+
36
+ if (RATE_LIMIT != "" && nEvents > parseInt(RATE_LIMIT)) {
37
+ throw error(429, ERROR_MESSAGES.rateLimited);
38
+ }
39
+
40
  const firstMessage = conversation.messages.find((m) => m.from === "user");
41
 
42
  const userPrompt =
src/routes/conversation/[id]/web-search/+server.ts CHANGED
@@ -10,6 +10,8 @@ import type { WebSearch } from "$lib/types/WebSearch";
10
  import { generateQuery } from "$lib/server/websearch/generateQuery";
11
  import { parseWeb } from "$lib/server/websearch/parseWeb";
12
  import { summarizeWeb } from "$lib/server/websearch/summarizeWeb";
 
 
13
 
14
  interface GenericObject {
15
  [key: string]: GenericObject | unknown;
@@ -22,7 +24,7 @@ function removeLinks(obj: GenericObject) {
22
  }
23
  return obj;
24
  }
25
- export async function GET({ params, locals, url }) {
26
  const model = defaultModel;
27
  const convId = new ObjectId(params.id);
28
  const searchId = new ObjectId();
@@ -36,6 +38,23 @@ export async function GET({ params, locals, url }) {
36
  throw error(404, "Conversation not found");
37
  }
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  const prompt = z.string().trim().min(1).parse(url.searchParams.get("prompt"));
40
 
41
  const messages = (() => {
 
10
  import { generateQuery } from "$lib/server/websearch/generateQuery";
11
  import { parseWeb } from "$lib/server/websearch/parseWeb";
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";
15
 
16
  interface GenericObject {
17
  [key: string]: GenericObject | unknown;
 
24
  }
25
  return obj;
26
  }
27
+ export async function GET({ params, locals, url, getClientAddress }) {
28
  const model = defaultModel;
29
  const convId = new ObjectId(params.id);
30
  const searchId = new ObjectId();
 
38
  throw error(404, "Conversation not found");
39
  }
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
+
58
  const prompt = z.string().trim().min(1).parse(url.searchParams.get("prompt"));
59
 
60
  const messages = (() => {