From df5933cbc96ee25ada4ecd60006e77218afe88ac Mon Sep 17 00:00:00 2001 From: Index Date: Sat, 25 Oct 2025 21:30:49 -0500 Subject: [PATCH] feat: add daily message quota --- src/core.ts | 3 +++ src/env.ts | 4 ++++ src/handlers/messages.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+) diff --git a/src/core.ts b/src/core.ts index 7cce219..395e4a7 100644 --- a/src/core.ts +++ b/src/core.ts @@ -11,6 +11,9 @@ export const ai = new GoogleGenAI({ apiKey: env.GEMINI_API_KEY, }); +export const QUOTA_EXCEEDED_MESSAGE = + "You have exceeded your daily message quota (15). Please wait 24 hours before trying again."; + export const UNAUTHORIZED_MESSAGE = "I can’t make sense of your noise just yet. You’ll need to be whitelisted before I can help."; diff --git a/src/env.ts b/src/env.ts index d2281e1..38cd55d 100644 --- a/src/env.ts +++ b/src/env.ts @@ -16,6 +16,10 @@ const envSchema = z.object({ BSKY_PASSWORD: z.string(), GEMINI_API_KEY: z.string(), + DAILY_QUERY_LIMIT: z.preprocess( + (val) => (typeof val === "string" && val.trim() !== "") ? Number(val) : undefined, + z.number().int().positive().default(15), + ), }); export type Env = z.infer; diff --git a/src/handlers/messages.ts b/src/handlers/messages.ts index 0bc6a25..28a3a1f 100644 --- a/src/handlers/messages.ts +++ b/src/handlers/messages.ts @@ -4,6 +4,9 @@ import * as c from "../core"; import * as tools from "../tools"; import consola from "consola"; import { env } from "../env"; +import db from "../db"; +import { messages } from "../db/schema"; +import { and, count, eq, gte, lt } from "drizzle-orm"; import { exceedsGraphemes, multipartResponse, @@ -129,6 +132,29 @@ export async function handler(message: ChatMessage): Promise { return; } + const today = new Date(); + today.setHours(0, 0, 0, 0); + const tomorrow = new Date(today); + tomorrow.setDate(tomorrow.getDate() + 1); + + const dailyCount = await db + .select({ count: count(messages.id) }) + .from(messages) + .where( + and( + eq(messages.did, message.senderDid), + gte(messages.created_at, today), + lt(messages.created_at, tomorrow), + ), + ); + + if (dailyCount[0]!.count >= env.DAILY_QUERY_LIMIT) { + conversation.sendMessage({ + text: c.QUOTA_EXCEEDED_MESSAGE, + }); + return; + } + logger.success("Found conversation"); conversation.sendMessage({ text: "...",