-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistory.ts
More file actions
35 lines (28 loc) · 1.28 KB
/
history.ts
File metadata and controls
35 lines (28 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { settings } from '$lib/config'
import type { Message } from '$lib/types'
import { queryMessagesDb } from './iMessages'
import { logger } from './logger'
import { formatMessagesAsText } from './utils'
export const fetchRelevantHistory = async (messages: Message[]): Promise<string> => {
try {
const lookbackHours = Number.parseInt(settings.HISTORY_LOOKBACK_HOURS || '0')
if (!lookbackHours || messages.length === 0) {
logger.warn('No messages or invalid lookbackHours; skipping history fetch')
return ''
}
const earliestTimestampMs = new Date(messages[0].timestamp).getTime()
const end = new Date(earliestTimestampMs - 1) // exclude the current message
const start = new Date(end.getTime() - lookbackHours * 60 * 60 * 1000)
const { messages: history } = await queryMessagesDb(start.toISOString(), end.toISOString())
if (!history.length) {
logger.debug('No messages found in history window')
return ''
}
const historyText = formatMessagesAsText(history)
logger.debug({ count: history.length }, 'History messages found')
return historyText
} catch (error) {
logger.error({ error }, 'Failed to fetch history context')
return ''
}
}