mirror of
https://github.com/whekin/household-bot.git
synced 2026-03-31 17:44:03 +00:00
Fix topic message history persistence
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { nowInstant, Temporal, type Instant } from '@household/domain'
|
||||
import type { TopicMessageHistoryRecord } from '@household/ports'
|
||||
import { instantFromEpochSeconds, nowInstant, Temporal, type Instant } from '@household/domain'
|
||||
import type { TopicMessageHistoryRecord, TopicMessageHistoryRepository } from '@household/ports'
|
||||
|
||||
export interface TopicHistoryTurn {
|
||||
role: 'user' | 'assistant'
|
||||
@@ -45,6 +45,50 @@ export function historyRecordToTurn(record: TopicMessageHistoryRecord): TopicHis
|
||||
}
|
||||
}
|
||||
|
||||
export function telegramMessageIdFromMessage(
|
||||
message: { message_id?: number } | null | undefined
|
||||
): string | null {
|
||||
return typeof message?.message_id === 'number' ? message.message_id.toString() : null
|
||||
}
|
||||
|
||||
export function telegramMessageSentAtFromMessage(
|
||||
message: { date?: number } | null | undefined
|
||||
): Instant | null {
|
||||
return typeof message?.date === 'number' ? instantFromEpochSeconds(message.date) : null
|
||||
}
|
||||
|
||||
export async function persistTopicHistoryMessage(input: {
|
||||
repository: TopicMessageHistoryRepository | undefined
|
||||
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
|
||||
}) {
|
||||
const normalizedText = input.rawText.trim()
|
||||
if (!input.repository || normalizedText.length === 0) {
|
||||
return
|
||||
}
|
||||
|
||||
await input.repository.saveMessage({
|
||||
householdId: input.householdId,
|
||||
telegramChatId: input.telegramChatId,
|
||||
telegramThreadId: input.telegramThreadId,
|
||||
telegramMessageId: input.telegramMessageId,
|
||||
telegramUpdateId: input.telegramUpdateId,
|
||||
senderTelegramUserId: input.senderTelegramUserId,
|
||||
senderDisplayName: input.senderDisplayName,
|
||||
isBot: input.isBot,
|
||||
rawText: normalizedText,
|
||||
messageSentAt: input.messageSentAt
|
||||
})
|
||||
}
|
||||
|
||||
export function formatThreadHistory(turns: readonly TopicHistoryTurn[]): string | null {
|
||||
const lines = turns
|
||||
.map((turn) => `${turn.speaker} (${turn.role}): ${turn.text}`)
|
||||
|
||||
Reference in New Issue
Block a user