2026-07-28 15:00:29 +07:00
|
|
|
import { pgChatbotMessagesTable, pgMessagesTable } from "@bete/shared";
|
2026-06-09 10:16:04 +07:00
|
|
|
import { createChildLogger } from "@bete/shared/logger";
|
2026-07-27 21:54:31 +07:00
|
|
|
import { and, desc, eq, type SQL, sql } from "drizzle-orm";
|
|
|
|
|
import { getDatabase } from "../../shared/database/index.js";
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
const logger = createChildLogger("chatbot.repository");
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
export interface ChatbotContext {
|
2026-06-09 10:16:04 +07:00
|
|
|
messageCount?: number;
|
|
|
|
|
activeParticipants?: number;
|
|
|
|
|
lastActivity?: string;
|
|
|
|
|
topicsDiscussed?: string[];
|
|
|
|
|
guildId?: string;
|
|
|
|
|
channelId?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface SaveConversationInput {
|
|
|
|
|
userId: string;
|
|
|
|
|
userMessage: string;
|
2026-07-28 15:00:29 +07:00
|
|
|
botResponse: string;
|
|
|
|
|
context?: ChatbotContext;
|
2026-06-09 10:16:04 +07:00
|
|
|
timestamp: Date;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
export interface ChatbotHistoryRow {
|
2026-06-09 10:16:04 +07:00
|
|
|
id: string;
|
|
|
|
|
user_id: string;
|
|
|
|
|
user_message: string;
|
2026-07-28 15:00:29 +07:00
|
|
|
bot_response: string;
|
|
|
|
|
context: ChatbotContext | null;
|
2026-06-09 10:16:04 +07:00
|
|
|
created_at: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface ServerInsights {
|
|
|
|
|
total_messages: number;
|
|
|
|
|
active_users: number;
|
|
|
|
|
flagged: number;
|
|
|
|
|
warned: number;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
export class ChatbotRepository {
|
2026-06-09 10:16:04 +07:00
|
|
|
async saveConversation(input: SaveConversationInput): Promise<void> {
|
2026-07-27 21:54:31 +07:00
|
|
|
const db = getDatabase();
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
await db.insert(pgChatbotMessagesTable).values({
|
2026-07-27 21:54:31 +07:00
|
|
|
user_id: input.userId,
|
|
|
|
|
user_message: input.userMessage,
|
2026-07-28 15:00:29 +07:00
|
|
|
bot_response: input.botResponse,
|
2026-07-27 21:54:31 +07:00
|
|
|
context: (input.context ?? {}) as Record<string, unknown>,
|
|
|
|
|
created_at: input.timestamp,
|
|
|
|
|
});
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
logger.debug({ userId: input.userId }, "Conversation saved");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getChatHistory(
|
|
|
|
|
userId: string,
|
|
|
|
|
limit: number,
|
2026-07-28 15:00:29 +07:00
|
|
|
): Promise<ChatbotHistoryRow[]> {
|
2026-07-27 21:54:31 +07:00
|
|
|
const db = getDatabase();
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const rows = await db
|
|
|
|
|
.select()
|
2026-07-28 15:00:29 +07:00
|
|
|
.from(pgChatbotMessagesTable)
|
|
|
|
|
.where(eq(pgChatbotMessagesTable.user_id, userId))
|
|
|
|
|
.orderBy(desc(pgChatbotMessagesTable.created_at))
|
2026-07-27 21:54:31 +07:00
|
|
|
.limit(limit);
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
logger.debug({ userId, count: rows.length }, "Chat history fetched");
|
2026-07-28 15:00:29 +07:00
|
|
|
return rows.reverse() as unknown as ChatbotHistoryRow[];
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clearChatHistory(userId: string): Promise<void> {
|
2026-07-27 21:54:31 +07:00
|
|
|
const db = getDatabase();
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const deleted = await db
|
2026-07-28 15:00:29 +07:00
|
|
|
.delete(pgChatbotMessagesTable)
|
|
|
|
|
.where(eq(pgChatbotMessagesTable.user_id, userId))
|
|
|
|
|
.returning({ id: pgChatbotMessagesTable.id });
|
2026-07-27 21:54:31 +07:00
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
|
{ userId, deletedRows: deleted.length },
|
|
|
|
|
"Chat history cleared",
|
2026-06-09 10:16:04 +07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getServerInsights(
|
|
|
|
|
guildId?: string,
|
|
|
|
|
channelId?: string,
|
|
|
|
|
): Promise<ServerInsights> {
|
|
|
|
|
try {
|
2026-07-27 21:54:31 +07:00
|
|
|
const db = getDatabase();
|
|
|
|
|
const conditions: SQL[] = [];
|
2026-06-09 10:16:04 +07:00
|
|
|
|
|
|
|
|
if (guildId) {
|
2026-07-27 21:54:31 +07:00
|
|
|
conditions.push(eq(pgMessagesTable.guild_id, guildId));
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
if (channelId) {
|
2026-07-27 21:54:31 +07:00
|
|
|
conditions.push(eq(pgMessagesTable.channel_id, channelId));
|
2026-06-09 10:16:04 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const where = conditions.length > 0 ? and(...conditions) : undefined;
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const [result] = await db
|
|
|
|
|
.select({
|
|
|
|
|
total_messages: sql<number>`COUNT(*)::int`,
|
|
|
|
|
active_users: sql<number>`COUNT(DISTINCT ${pgMessagesTable.user_id})::int`,
|
|
|
|
|
flagged: sql<number>`COUNT(*) FILTER (WHERE ${pgMessagesTable.ai_status} = 'flagged')::int`,
|
|
|
|
|
warned: sql<number>`COUNT(*) FILTER (WHERE ${pgMessagesTable.ai_status} = 'warn')::int`,
|
|
|
|
|
})
|
|
|
|
|
.from(pgMessagesTable)
|
|
|
|
|
.where(where);
|
2026-06-09 10:16:04 +07:00
|
|
|
|
2026-07-27 21:54:31 +07:00
|
|
|
const insights = result ?? {
|
2026-06-09 10:16:04 +07:00
|
|
|
total_messages: 0,
|
|
|
|
|
active_users: 0,
|
|
|
|
|
flagged: 0,
|
|
|
|
|
warned: 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
logger.debug({ guildId, channelId, insights }, "Server insights fetched");
|
|
|
|
|
return insights;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.warn(
|
|
|
|
|
{ error, guildId, channelId },
|
|
|
|
|
"Failed to load server insights",
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
total_messages: 0,
|
|
|
|
|
active_users: 0,
|
|
|
|
|
flagged: 0,
|
|
|
|
|
warned: 0,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-28 15:00:29 +07:00
|
|
|
export const chatbotRepository = new ChatbotRepository();
|