Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

fix: ratelimiting

Marc Seitz6e70364d8820
diff --git a/components/chat/chat-message.tsx b/components/chat/chat-message.tsx
index 6218922e..8d1655fc 100644
--- a/components/chat/chat-message.tsx
+++ b/components/chat/chat-message.tsx
@@ -4,6 +4,15 @@ import { cn } from "@/lib/utils";
import { ChatMessageActions } from "./chat-message-actions";
import UserRound from "../shared/icons/user-round";
import PapermarkSparkle from "../shared/icons/papermark-sparkle";
+import AlertCircle from "../shared/icons/alert-circle";
+
+// map role to icon and name
+const mapMessageRole = {
+ user: { icon: <UserRound />, name: "You" },
+ system: { icon: <AlertCircle />, name: "System" },
+ assistant: { icon: <PapermarkSparkle />, name: "Papermark Assistant" },
+ function: { icon: <PapermarkSparkle />, name: "Papermark Assistant" },
+};
export interface ChatMessageProps {
message: Message;
@@ -23,15 +32,17 @@ export function ChatMessage({ message, ...props }: ChatMessageProps) {
"flex h-8 w-8 shrink-0 select-none items-center justify-center rounded-md border shadow",
message.role === "user"
? "bg-background"
- : "bg-primary text-primary-foreground",
+ : message.role === "system"
+ ? "bg-destructive text-destructive-foreground"
+ : "bg-primary text-primary-foreground",
)}
>
- {message.role === "user" ? <UserRound /> : <PapermarkSparkle />}
+ {mapMessageRole[message.role].icon}
</div>
<div className="flex-1 px-1 ml-4 space-y-2 overflow-hidden">
<div className="relative flex w-[calc(100%-50px)] flex-col group">
<div className="font-semibold select-none">
- {message.role === "user" ? "You" : "Papermark Assistant"}
+ {mapMessageRole[message.role].name}
</div>
<div className="prose break-words dark:prose-invert prose-p:leading-relaxed prose-pre:p-0 font-light">
<p className="mb-2 last:mb-0">{message.content}</p>
diff --git a/components/chat/chat.tsx b/components/chat/chat.tsx
index 7ac7c0bb..6a263804 100644
--- a/components/chat/chat.tsx
+++ b/components/chat/chat.tsx
@@ -11,6 +11,7 @@ import { ChatInput } from "./chat-input";
import { ChatScrollAnchor } from "./chat-scroll-anchor";
import { EmptyScreen } from "./empty-screen";
import { useEffect, useState } from "react";
+import { nanoid } from "nanoid";
export interface ChatProps extends React.ComponentProps<"div"> {
initialMessages: Message[];
@@ -43,6 +44,19 @@ export function Chat({
const [combinedMessages, setCombinedMessages] = useState<Message[]>([]);
+ useEffect(() => {
+ if (error instanceof Error) {
+ 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.",
+ id: nanoid(),
+ };
+
+ setCombinedMessages((prev) => [...prev, message]);
+ }
+ }, [error]);
+
useEffect(() => {
// Concatenate existing messages with messages from the hook
// and reverse the order so that the newest messages are at the bottom:
diff --git a/lib/redis.ts b/lib/redis.ts
index 3a1dd024..27def91b 100644
--- a/lib/redis.ts
+++ b/lib/redis.ts
@@ -1,6 +1,6 @@
import { Redis } from "@upstash/redis";
export const redis = new Redis({
- url: process.env.UPSTASH_REDIS_REST_TOKEN as string,
+ url: process.env.UPSTASH_REDIS_REST_URL as string,
token: process.env.UPSTASH_REDIS_REST_TOKEN as string,
});
diff --git a/pages/api/assistants/chat.ts b/pages/api/assistants/chat.ts
index a9bbdf4f..d2ca9de0 100644
--- a/pages/api/assistants/chat.ts
+++ b/pages/api/assistants/chat.ts
@@ -49,14 +49,13 @@ export default async function POST(req: Request) {
redis: redis,
// rate limit to 5 requests per hour
limiter: Ratelimit.slidingWindow(5, "3600s"),
+ analytics: true,
});
const { success, limit, reset, remaining } = await ratelimit.limit(
`ratelimit_${ip}`,
);
- console.log("ratelimit", { success, limit, reset, remaining });
-
if (!success) {
return new Response("You have reached your request limit for the day.", {
status: 429,