mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 10:24:02 +00:00
feat(bot): add topic history-aware assistant replies
This commit is contained in:
@@ -4,3 +4,4 @@ export { createDbHouseholdConfigurationRepository } from './household-config-rep
|
||||
export { createDbProcessedBotMessageRepository } from './processed-bot-message-repository'
|
||||
export { createDbReminderDispatchRepository } from './reminder-dispatch-repository'
|
||||
export { createDbTelegramPendingActionRepository } from './telegram-pending-action-repository'
|
||||
export { createDbTopicMessageHistoryRepository } from './topic-message-history-repository'
|
||||
|
||||
99
packages/adapters-db/src/topic-message-history-repository.ts
Normal file
99
packages/adapters-db/src/topic-message-history-repository.ts
Normal file
@@ -0,0 +1,99 @@
|
||||
import { and, asc, desc, eq, gte, isNotNull } from 'drizzle-orm'
|
||||
|
||||
import { instantFromDatabaseValue, instantToDate } from '@household/domain'
|
||||
import { createDbClient, schema } from '@household/db'
|
||||
import type { TopicMessageHistoryRepository } from '@household/ports'
|
||||
|
||||
export function createDbTopicMessageHistoryRepository(databaseUrl: string): {
|
||||
repository: TopicMessageHistoryRepository
|
||||
close: () => Promise<void>
|
||||
} {
|
||||
const { db, queryClient } = createDbClient(databaseUrl, {
|
||||
max: 3,
|
||||
prepare: false
|
||||
})
|
||||
|
||||
const repository: TopicMessageHistoryRepository = {
|
||||
async saveMessage(input) {
|
||||
await db
|
||||
.insert(schema.topicMessages)
|
||||
.values({
|
||||
householdId: input.householdId,
|
||||
telegramChatId: input.telegramChatId,
|
||||
telegramThreadId: input.telegramThreadId,
|
||||
telegramMessageId: input.telegramMessageId,
|
||||
telegramUpdateId: input.telegramUpdateId,
|
||||
senderTelegramUserId: input.senderTelegramUserId,
|
||||
senderDisplayName: input.senderDisplayName,
|
||||
isBot: input.isBot ? 1 : 0,
|
||||
rawText: input.rawText,
|
||||
messageSentAt: input.messageSentAt ? instantToDate(input.messageSentAt) : null
|
||||
})
|
||||
.onConflictDoNothing()
|
||||
},
|
||||
|
||||
async listRecentThreadMessages(input) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(schema.topicMessages)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.topicMessages.householdId, input.householdId),
|
||||
eq(schema.topicMessages.telegramChatId, input.telegramChatId),
|
||||
eq(schema.topicMessages.telegramThreadId, input.telegramThreadId)
|
||||
)
|
||||
)
|
||||
.orderBy(desc(schema.topicMessages.messageSentAt), desc(schema.topicMessages.createdAt))
|
||||
.limit(input.limit)
|
||||
|
||||
return rows.reverse().map((row) => ({
|
||||
householdId: row.householdId,
|
||||
telegramChatId: row.telegramChatId,
|
||||
telegramThreadId: row.telegramThreadId,
|
||||
telegramMessageId: row.telegramMessageId,
|
||||
telegramUpdateId: row.telegramUpdateId,
|
||||
senderTelegramUserId: row.senderTelegramUserId,
|
||||
senderDisplayName: row.senderDisplayName,
|
||||
isBot: row.isBot === 1,
|
||||
rawText: row.rawText,
|
||||
messageSentAt: instantFromDatabaseValue(row.messageSentAt)
|
||||
}))
|
||||
},
|
||||
|
||||
async listRecentChatMessages(input) {
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(schema.topicMessages)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.topicMessages.householdId, input.householdId),
|
||||
eq(schema.topicMessages.telegramChatId, input.telegramChatId),
|
||||
isNotNull(schema.topicMessages.messageSentAt),
|
||||
gte(schema.topicMessages.messageSentAt, instantToDate(input.sentAtOrAfter))
|
||||
)
|
||||
)
|
||||
.orderBy(asc(schema.topicMessages.messageSentAt), asc(schema.topicMessages.createdAt))
|
||||
.limit(input.limit)
|
||||
|
||||
return rows.map((row) => ({
|
||||
householdId: row.householdId,
|
||||
telegramChatId: row.telegramChatId,
|
||||
telegramThreadId: row.telegramThreadId,
|
||||
telegramMessageId: row.telegramMessageId,
|
||||
telegramUpdateId: row.telegramUpdateId,
|
||||
senderTelegramUserId: row.senderTelegramUserId,
|
||||
senderDisplayName: row.senderDisplayName,
|
||||
isBot: row.isBot === 1,
|
||||
rawText: row.rawText,
|
||||
messageSentAt: instantFromDatabaseValue(row.messageSentAt)
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
repository,
|
||||
close: async () => {
|
||||
await queryClient.end({ timeout: 5 })
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -19,6 +19,7 @@
|
||||
"0015_white_owl.sql": "a9dec4c536c660d7eb0fcea42a3bedb1301408551977d098dff8324d7d5b26bd",
|
||||
"0016_equal_susan_delgado.sql": "1698bf0516d16d2d7929dcb1bd2bb76d5a629eaba3d0bb2533c1ae926408de7a",
|
||||
"0017_gigantic_selene.sql": "232d61b979675ddb97c9d69d14406dc15dd095ee6a332d3fa71d10416204fade",
|
||||
"0018_nimble_kojori.sql": "818738e729119c6de8049dcfca562926a5dc6e321ecbbf9cf38e02bc70b5a0dc"
|
||||
"0018_nimble_kojori.sql": "818738e729119c6de8049dcfca562926a5dc6e321ecbbf9cf38e02bc70b5a0dc",
|
||||
"0019_faithful_madame_masque.sql": "38711341799b04a7c47fcc64fd19faf5b26e6f183d6a4c01d492b9929cd63641"
|
||||
}
|
||||
}
|
||||
|
||||
20
packages/db/drizzle/0019_faithful_madame_masque.sql
Normal file
20
packages/db/drizzle/0019_faithful_madame_masque.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE "topic_messages" (
|
||||
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
||||
"household_id" uuid NOT NULL,
|
||||
"telegram_chat_id" text NOT NULL,
|
||||
"telegram_thread_id" text,
|
||||
"telegram_message_id" text,
|
||||
"telegram_update_id" text,
|
||||
"sender_telegram_user_id" text,
|
||||
"sender_display_name" text,
|
||||
"is_bot" integer DEFAULT 0 NOT NULL,
|
||||
"raw_text" text NOT NULL,
|
||||
"message_sent_at" timestamp with time zone,
|
||||
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
||||
);
|
||||
--> statement-breakpoint
|
||||
ALTER TABLE "topic_messages" ADD CONSTRAINT "topic_messages_household_id_households_id_fk" FOREIGN KEY ("household_id") REFERENCES "public"."households"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
|
||||
CREATE INDEX "topic_messages_household_thread_sent_idx" ON "topic_messages" USING btree ("household_id","telegram_chat_id","telegram_thread_id","message_sent_at");--> statement-breakpoint
|
||||
CREATE INDEX "topic_messages_household_chat_sent_idx" ON "topic_messages" USING btree ("household_id","telegram_chat_id","message_sent_at");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "topic_messages_household_tg_message_unique" ON "topic_messages" USING btree ("household_id","telegram_chat_id","telegram_message_id");--> statement-breakpoint
|
||||
CREATE UNIQUE INDEX "topic_messages_household_tg_update_unique" ON "topic_messages" USING btree ("household_id","telegram_update_id");
|
||||
3441
packages/db/drizzle/meta/0019_snapshot.json
Normal file
3441
packages/db/drizzle/meta/0019_snapshot.json
Normal file
File diff suppressed because it is too large
Load Diff
@@ -134,6 +134,13 @@
|
||||
"when": 1773252000000,
|
||||
"tag": "0018_nimble_kojori",
|
||||
"breakpoints": true
|
||||
},
|
||||
{
|
||||
"idx": 19,
|
||||
"version": "7",
|
||||
"when": 1773327708167,
|
||||
"tag": "0019_faithful_madame_masque",
|
||||
"breakpoints": true
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -499,6 +499,48 @@ export const processedBotMessages = pgTable(
|
||||
})
|
||||
)
|
||||
|
||||
export const topicMessages = pgTable(
|
||||
'topic_messages',
|
||||
{
|
||||
id: uuid('id').defaultRandom().primaryKey(),
|
||||
householdId: uuid('household_id')
|
||||
.notNull()
|
||||
.references(() => households.id, { onDelete: 'cascade' }),
|
||||
telegramChatId: text('telegram_chat_id').notNull(),
|
||||
telegramThreadId: text('telegram_thread_id'),
|
||||
telegramMessageId: text('telegram_message_id'),
|
||||
telegramUpdateId: text('telegram_update_id'),
|
||||
senderTelegramUserId: text('sender_telegram_user_id'),
|
||||
senderDisplayName: text('sender_display_name'),
|
||||
isBot: integer('is_bot').default(0).notNull(),
|
||||
rawText: text('raw_text').notNull(),
|
||||
messageSentAt: timestamp('message_sent_at', { withTimezone: true }),
|
||||
createdAt: timestamp('created_at', { withTimezone: true }).defaultNow().notNull()
|
||||
},
|
||||
(table) => ({
|
||||
householdThreadSentIdx: index('topic_messages_household_thread_sent_idx').on(
|
||||
table.householdId,
|
||||
table.telegramChatId,
|
||||
table.telegramThreadId,
|
||||
table.messageSentAt
|
||||
),
|
||||
householdChatSentIdx: index('topic_messages_household_chat_sent_idx').on(
|
||||
table.householdId,
|
||||
table.telegramChatId,
|
||||
table.messageSentAt
|
||||
),
|
||||
householdMessageUnique: uniqueIndex('topic_messages_household_tg_message_unique').on(
|
||||
table.householdId,
|
||||
table.telegramChatId,
|
||||
table.telegramMessageId
|
||||
),
|
||||
householdUpdateUnique: uniqueIndex('topic_messages_household_tg_update_unique').on(
|
||||
table.householdId,
|
||||
table.telegramUpdateId
|
||||
)
|
||||
})
|
||||
)
|
||||
|
||||
export const anonymousMessages = pgTable(
|
||||
'anonymous_messages',
|
||||
{
|
||||
@@ -682,6 +724,7 @@ export type BillingCycleExchangeRate = typeof billingCycleExchangeRates.$inferSe
|
||||
export type UtilityBill = typeof utilityBills.$inferSelect
|
||||
export type PurchaseEntry = typeof purchaseEntries.$inferSelect
|
||||
export type PurchaseMessage = typeof purchaseMessages.$inferSelect
|
||||
export type TopicMessage = typeof topicMessages.$inferSelect
|
||||
export type AnonymousMessage = typeof anonymousMessages.$inferSelect
|
||||
export type PaymentConfirmation = typeof paymentConfirmations.$inferSelect
|
||||
export type PaymentRecord = typeof paymentRecords.$inferSelect
|
||||
|
||||
@@ -66,3 +66,9 @@ export {
|
||||
type TelegramPendingActionRepository,
|
||||
type TelegramPendingActionType
|
||||
} from './telegram-pending-actions'
|
||||
export type {
|
||||
ListRecentChatTopicMessagesInput,
|
||||
ListRecentThreadTopicMessagesInput,
|
||||
TopicMessageHistoryRecord,
|
||||
TopicMessageHistoryRepository
|
||||
} from './topic-message-history'
|
||||
|
||||
38
packages/ports/src/topic-message-history.ts
Normal file
38
packages/ports/src/topic-message-history.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import type { Instant } from '@household/domain'
|
||||
|
||||
export interface TopicMessageHistoryRecord {
|
||||
householdId: string
|
||||
telegramChatId: string
|
||||
telegramThreadId: string | null
|
||||
telegramMessageId: string | null
|
||||
telegramUpdateId: string | null
|
||||
senderTelegramUserId: string | null
|
||||
senderDisplayName: string | null
|
||||
isBot: boolean
|
||||
rawText: string
|
||||
messageSentAt: Instant | null
|
||||
}
|
||||
|
||||
export interface ListRecentThreadTopicMessagesInput {
|
||||
householdId: string
|
||||
telegramChatId: string
|
||||
telegramThreadId: string
|
||||
limit: number
|
||||
}
|
||||
|
||||
export interface ListRecentChatTopicMessagesInput {
|
||||
householdId: string
|
||||
telegramChatId: string
|
||||
sentAtOrAfter: Instant
|
||||
limit: number
|
||||
}
|
||||
|
||||
export interface TopicMessageHistoryRepository {
|
||||
saveMessage(input: TopicMessageHistoryRecord): Promise<void>
|
||||
listRecentThreadMessages(
|
||||
input: ListRecentThreadTopicMessagesInput
|
||||
): Promise<readonly TopicMessageHistoryRecord[]>
|
||||
listRecentChatMessages(
|
||||
input: ListRecentChatTopicMessagesInput
|
||||
): Promise<readonly TopicMessageHistoryRecord[]>
|
||||
}
|
||||
Reference in New Issue
Block a user