Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
add ratelimiting to chat
Marc Seitzd01e5d1845e0
diff --git a/components/chat/chat.tsx b/components/chat/chat.tsxindex 6a263804..a0f64fb9 100644--- a/components/chat/chat.tsx+++ b/components/chat/chat.tsx@@ -18,6 +18,8 @@ export interface ChatProps extends React.ComponentProps<"div"> {threadId?: string;firstPage?: string;isPublic?: boolean;+ userId?: string;+ plan?: string;}export function Chat({@@ -26,6 +28,8 @@ export function Chat({firstPage,className,isPublic,+ userId,+ plan,}: ChatProps) {const {status,@@ -39,6 +43,8 @@ export function Chat({threadId: threadId,body: {isPublic: isPublic,+ userId: userId,+ plan: plan,},});@@ -46,10 +52,19 @@ export function Chat({useEffect(() => {if (error instanceof Error) {+ let content: string = "";+ if (isPublic) {+ content =+ "You have reached your request limit for the day. Sign up for a free account to continue using Papermark Assistant.";+ }+ if (userId && plan !== "pro") {+ content =+ "You have reached your request limit for the day. Upgrade to a paid account to continue using Papermark Assistant.";+ }+const message: Message = {role: "system",- content:- "You have reached your request limit for the day. Sign up for a free account to continue using Papermark Assistant.",+ content: content,id: nanoid(),};diff --git a/pages/api/assistants/chat.ts b/pages/api/assistants/chat.tsindex 0f97b037..13992394 100644--- a/pages/api/assistants/chat.ts+++ b/pages/api/assistants/chat.ts@@ -10,13 +10,15 @@ const ratelimit = {redis,analytics: true,prefix: "ratelimit:public",- limiter: Ratelimit.slidingWindow(1, "3600s"),+ // rate limit public to 3 request per hour+ limiter: Ratelimit.slidingWindow(3, "1h"),}),free: new Ratelimit({redis,analytics: true,prefix: "ratelimit:free",- limiter: Ratelimit.slidingWindow(60, "10s"),+ // rate limit to 3 request per day+ limiter: Ratelimit.fixedWindow(3, "24h"),}),paid: new Ratelimit({redis,@@ -24,6 +26,13 @@ const ratelimit = {prefix: "ratelimit:paid",limiter: Ratelimit.slidingWindow(60, "10s"),}),+ pro: new Ratelimit({+ redis,+ analytics: true,+ prefix: "ratelimit:pro",+ // rate limit to 1000 request per 30 days+ limiter: Ratelimit.fixedWindow(1000, "30d"),+ }),};// IMPORTANT! Set the runtime to edge@@ -37,18 +46,14 @@ export default async function POST(req: Request) {threadId: string | null;message: string;isPublic: boolean | null;+ userId: string | null;+ plan: string | null;} = await req.json();if (input.isPublic) {const ip = req.headers.get("x-forwarded-for");- const ratelimit = new Ratelimit({- redis: redis,- // rate limit to 5 requests per hour- limiter: Ratelimit.slidingWindow(5, "3600s"),- analytics: true,- });-- const { success, limit, reset, remaining } = await ratelimit.limit(++ const { success, limit, reset, remaining } = await ratelimit.public.limit(`ratelimit_${ip}`,);@@ -64,6 +69,23 @@ export default async function POST(req: Request) {}}+ if (input.userId && input.plan !== "pro") {+ const { success, limit, reset, remaining } = await ratelimit.free.limit(+ `ratelimit_${input.userId}`,+ );++ if (!success) {+ return new Response("You have reached your request limit for the day.", {+ status: 429,+ headers: {+ "X-RateLimit-Limit": limit.toString(),+ "X-RateLimit-Remaining": remaining.toString(),+ "X-RateLimit-Reset": reset.toString(),+ },+ });+ }+ }+// create a threadId if one wasn't providedconst threadId = input.threadId ?? (await openai.beta.threads.create()).id;diff --git a/pages/api/teams/[teamId]/billing/plan.ts b/pages/api/teams/[teamId]/billing/plan.tsindex 15086c0f..e7f5f5bb 100644--- a/pages/api/teams/[teamId]/billing/plan.ts+++ b/pages/api/teams/[teamId]/billing/plan.ts@@ -3,10 +3,11 @@ import { getServerSession } from "next-auth/next";import prisma from "@/lib/prisma";import { authOptions } from "../../../auth/[...nextauth]";import { errorhandler } from "@/lib/errorHandler";+import { CustomUser } from "@/lib/types";export default async function handle(req: NextApiRequest,- res: NextApiResponse+ res: NextApiResponse,) {if (req.method === "GET") {// GET /api/teams/:teamId/billing/plan@@ -16,11 +17,17 @@ export default async function handle(}const { teamId } = req.query as { teamId: string };+ const userId = (session.user as CustomUser).id;try {const team = await prisma.team.findUnique({where: {id: teamId,+ users: {+ some: {+ userId: userId,+ },+ },},select: {plan: true,diff --git a/pages/documents/[id]/chat.tsx b/pages/documents/[id]/chat.tsxindex b533b0a4..dcf17ae3 100644--- a/pages/documents/[id]/chat.tsx+++ b/pages/documents/[id]/chat.tsx@@ -5,6 +5,7 @@ import { authOptions } from "@/pages/api/auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { Chat } from "@/components/chat/chat";import Sparkle from "@/components/shared/icons/sparkle";+import { usePlan } from "@/lib/swr/use-billing";export const getServerSideProps = async (context: any) => {const { id } = context.params;@@ -43,6 +44,8 @@ export const getServerSideProps = async (context: any) => {};}+ const userId = (session.user as CustomUser).id;+// create or fetch threadIdconst res = await fetch(`${process.env.NEXTAUTH_URL}/api/assistants/threads`,@@ -53,7 +56,7 @@ export const getServerSideProps = async (context: any) => {},body: JSON.stringify({documentId: document.id,- userId: (session.user as CustomUser).id,+ userId: userId,}),},);@@ -71,6 +74,7 @@ export const getServerSideProps = async (context: any) => {threadId,messages: messages || [],firstPage: document.versions[0].pages[0].file,+ userId,},};};@@ -79,11 +83,14 @@ export default function ChatPage({threadId,messages,firstPage,+ userId,}: {threadId: string;messages: Message[];firstPage: string;+ userId: string;}) {+ const { plan } = usePlan();return (<><Nav />@@ -91,6 +98,8 @@ export default function ChatPage({initialMessages={messages}threadId={threadId}firstPage={firstPage}+ userId={userId}+ plan={plan?.plan}/></>);diff --git a/pages/view/[linkId]/chat.tsx b/pages/view/[linkId]/chat.tsxindex 08780b70..17316a5e 100644--- a/pages/view/[linkId]/chat.tsx+++ b/pages/view/[linkId]/chat.tsx@@ -5,6 +5,7 @@ import { authOptions } from "@/pages/api/auth/[...nextauth]";import { CustomUser } from "@/lib/types";import { Chat } from "@/components/chat/chat";import Sparkle from "@/components/shared/icons/sparkle";+import { usePlan } from "@/lib/swr/use-billing";export const getServerSideProps = async (context: any) => {const { linkId } = context.params;@@ -47,6 +48,8 @@ export const getServerSideProps = async (context: any) => {};}+ const userId = (session.user as CustomUser).id;+// create or fetch threadIdconst res = await fetch(`${process.env.NEXTAUTH_URL}/api/assistants/threads`,@@ -57,7 +60,7 @@ export const getServerSideProps = async (context: any) => {},body: JSON.stringify({documentId: link!.document.id,- userId: (session.user as CustomUser).id,+ userId: userId,}),},);@@ -75,6 +78,7 @@ export const getServerSideProps = async (context: any) => {threadId,messages: messages || [],firstPage: link.document.versions[0].pages[0].file,+ userId: userId,},};};@@ -83,11 +87,15 @@ export default function ChatPage({threadId,messages,firstPage,+ userId,}: {threadId: string;messages: Message[];firstPage: string;+ userId: string;}) {+ const { plan } = usePlan();+return (<><Nav />@@ -95,6 +103,8 @@ export default function ChatPage({initialMessages={messages}threadId={threadId}firstPage={firstPage}+ userId={userId}+ plan={plan?.plan}/></>);