feat: add daily message quota
This commit is contained in:
parent
abb6b8b719
commit
df5933cbc9
3 changed files with 33 additions and 0 deletions
|
|
@ -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.";
|
||||
|
||||
|
|
|
|||
|
|
@ -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<typeof envSchema>;
|
||||
|
|
|
|||
|
|
@ -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<void> {
|
|||
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: "...",
|
||||
|
|
|
|||
Loading…
Reference in a new issue