Skip to content

Commit 0953d85

Browse files
authored
Add rate limiting to websearch and title summary (#433)
1 parent ba93cf8 commit 0953d85

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/routes/conversation/[id]/summarize/+server.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1+
import { RATE_LIMIT } from "$env/static/private";
12
import { buildPrompt } from "$lib/buildPrompt";
23
import { authCondition } from "$lib/server/auth";
34
import { collections } from "$lib/server/database";
45
import { generateFromDefaultEndpoint } from "$lib/server/generateFromDefaultEndpoint";
56
import { defaultModel } from "$lib/server/models";
7+
import { ERROR_MESSAGES } from "$lib/stores/errors.js";
68
import { error } from "@sveltejs/kit";
79
import { ObjectId } from "mongodb";
810

9-
export async function POST({ params, locals }) {
11+
export async function POST({ params, locals, getClientAddress }) {
1012
const convId = new ObjectId(params.id);
1113

1214
const conversation = await collections.conversations.findOne({
@@ -18,6 +20,23 @@ export async function POST({ params, locals }) {
1820
throw error(404, "Conversation not found");
1921
}
2022

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+
2140
const firstMessage = conversation.messages.find((m) => m.from === "user");
2241

2342
const userPrompt =

src/routes/conversation/[id]/web-search/+server.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import type { WebSearch } from "$lib/types/WebSearch";
1010
import { generateQuery } from "$lib/server/websearch/generateQuery";
1111
import { parseWeb } from "$lib/server/websearch/parseWeb";
1212
import { summarizeWeb } from "$lib/server/websearch/summarizeWeb";
13+
import { RATE_LIMIT } from "$env/static/private";
14+
import { ERROR_MESSAGES } from "$lib/stores/errors.js";
1315

1416
interface GenericObject {
1517
[key: string]: GenericObject | unknown;
@@ -22,7 +24,7 @@ function removeLinks(obj: GenericObject) {
2224
}
2325
return obj;
2426
}
25-
export async function GET({ params, locals, url }) {
27+
export async function GET({ params, locals, url, getClientAddress }) {
2628
const model = defaultModel;
2729
const convId = new ObjectId(params.id);
2830
const searchId = new ObjectId();
@@ -36,6 +38,23 @@ export async function GET({ params, locals, url }) {
3638
throw error(404, "Conversation not found");
3739
}
3840

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+
3958
const prompt = z.string().trim().min(1).parse(url.searchParams.get("prompt"));
4059

4160
const messages = (() => {

0 commit comments

Comments
 (0)