Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions app/(chat)/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { checkBotId } from "botid/server";
import { geolocation, ipAddress } from "@vercel/functions";
import {
convertToModelMessages,
Expand Down Expand Up @@ -64,7 +65,10 @@ export async function POST(request: Request) {
const { id, message, messages, selectedChatModel, selectedVisibilityType } =
requestBody;

const session = await auth();
const [, session] = await Promise.all([
checkBotId().catch(() => null),
auth(),
]);

if (!session?.user) {
return new ChatbotError("unauthorized:chat").toResponse();
Expand All @@ -80,10 +84,10 @@ export async function POST(request: Request) {

const messageCount = await getMessageCountByUserId({
id: session.user.id,
differenceInHours: 24,
differenceInHours: 1,
});

if (messageCount > entitlementsByUserType[userType].maxMessagesPerDay) {
if (messageCount > entitlementsByUserType[userType].maxMessagesPerHour) {
return new ChatbotError("rate_limit:chat").toResponse();
}

Expand Down
11 changes: 10 additions & 1 deletion instrumentation-client.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export {};
import { initBotId } from "botid/client/core";

initBotId({
protect: [
{
path: "/api/chat",
method: "POST",
},
],
});
6 changes: 3 additions & 3 deletions lib/ai/entitlements.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import type { UserType } from "@/app/(auth)/auth";

type Entitlements = {
maxMessagesPerDay: number;
maxMessagesPerHour: number;
};

export const entitlementsByUserType: Record<UserType, Entitlements> = {
/*
* For users without an account
*/
guest: {
maxMessagesPerDay: 10,
maxMessagesPerHour: 10,
},

/*
* For users with an account
*/
regular: {
maxMessagesPerDay: 10,
maxMessagesPerHour: 10,
},

/*
Expand Down
6 changes: 3 additions & 3 deletions lib/ratelimit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { createClient } from "redis";
import { isProductionEnvironment } from "@/lib/constants";
import { ChatbotError } from "@/lib/errors";

const MAX_MESSAGES_PER_DAY = 10;
const TTL_SECONDS = 60 * 60 * 24;
const MAX_MESSAGES = 10;
const TTL_SECONDS = 60 * 60;

let client: ReturnType<typeof createClient> | null = null;

Expand Down Expand Up @@ -37,7 +37,7 @@ export async function checkIpRateLimit(ip: string | undefined) {
.expire(key, TTL_SECONDS, "NX")
.exec();

if (typeof count === "number" && count > MAX_MESSAGES_PER_DAY) {
if (typeof count === "number" && count > MAX_MESSAGES) {
throw new ChatbotError("rate_limit:chat");
}
} catch (error) {
Expand Down
3 changes: 2 additions & 1 deletion next.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { withBotId } from "botid/next/config";
import type { NextConfig } from "next";

const basePath = "/demo";
Expand All @@ -24,4 +25,4 @@ const nextConfig: NextConfig = {
},
};

export default nextConfig;
export default withBotId(nextConfig);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"@xyflow/react": "^12.10.0",
"ai": "6.0.37",
"bcrypt-ts": "^5.0.2",
"botid": "^1.5.11",
"class-variance-authority": "^0.7.1",
"classnames": "^2.5.1",
"clsx": "^2.1.1",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading