|
1 | | -import { type VisionContent, type ChatConversation } from '../../types' |
2 | | - |
3 | | -const MINUTE_IN_MS = 60000 // 1 minute in milliseconds |
4 | | -const INACTIVE_THRESHOLD = 5 * MINUTE_IN_MS // 5 minutes |
5 | | -const IDLE_THRESHOLD = MINUTE_IN_MS // 1 minute |
6 | | -const IDLE_MESSAGE_LIMIT = 5 |
7 | | - |
8 | | -// const HOUR_IN_MS = 3600000 // 1 hour in milliseconds |
9 | | -// const INACTIVE_THRESHOLD = 12 * HOUR_IN_MS // 12 hours |
10 | | -// const IDLE_THRESHOLD = HOUR_IN_MS // 1 hour |
11 | | -// const IDLE_MESSAGE_LIMIT = 5 |
12 | | - |
13 | | -// Utility functions |
14 | | -export const conversationManager = { |
15 | | - manageConversationWindow (conversation: ChatConversation[]): ChatConversation[] { |
16 | | - console.log('fco::::::: here', conversation.length) |
17 | | - if (conversation.length === 0) return conversation |
| 1 | +import { |
| 2 | + type VisionContent, |
| 3 | + type ChatConversation, |
| 4 | + type OnMessageContext, |
| 5 | + type OnCallBackQueryData, |
| 6 | + type ConversationManagerState, |
| 7 | + type BotSessionData, |
| 8 | + type LlmsSessionData, |
| 9 | + type ImageGenSessionData |
| 10 | +} from '../../types' |
| 11 | + |
| 12 | +// Constants for time calculations |
| 13 | +const MS_PER_DAY = 24 * 60 * 60 * 1000 |
| 14 | +const CLEANUP_HOUR = 3 // 3 AM cleanup time |
| 15 | + |
| 16 | +const getSession = (ctx: OnMessageContext | OnCallBackQueryData, sessionDataKey: string): |
| 17 | +LlmsSessionData & ImageGenSessionData => { |
| 18 | + return ctx.session[sessionDataKey as keyof BotSessionData] as LlmsSessionData & ImageGenSessionData |
| 19 | +} |
| 20 | + |
| 21 | +const conversationManager = { |
| 22 | + /** |
| 23 | + * Initialize or update cleanup timestamps |
| 24 | + */ |
| 25 | + initializeCleanupTimes (): ConversationManagerState { |
| 26 | + const now = new Date() |
| 27 | + const today3AM = new Date(now) |
| 28 | + today3AM.setHours(CLEANUP_HOUR, 0, 0, 0) |
| 29 | + |
| 30 | + if (now.getTime() >= today3AM.getTime()) { |
| 31 | + // If current time is past 3 AM, set next cleanup to tomorrow 3 AM |
| 32 | + return { |
| 33 | + nextCleanupTime: today3AM.getTime() + MS_PER_DAY, |
| 34 | + lastCleanupTime: today3AM.getTime() |
| 35 | + } |
| 36 | + } else { |
| 37 | + // If current time is before 3 AM, set next cleanup to today 3 AM |
| 38 | + return { |
| 39 | + nextCleanupTime: today3AM.getTime(), |
| 40 | + lastCleanupTime: today3AM.getTime() - MS_PER_DAY |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if cleanup is needed based on context |
| 47 | + */ |
| 48 | + needsCleanup (ctx: OnMessageContext | OnCallBackQueryData, sessionDataKey: string): boolean { |
18 | 49 | const now = Date.now() |
19 | | - const lastMessageTime = conversation[conversation.length - 1].timestamp |
20 | | - const timeDifference = now - lastMessageTime |
21 | | - // Case 1: Inactive conversation (>12 hours) - Reset |
22 | | - if (timeDifference > INACTIVE_THRESHOLD) { |
23 | | - return [] |
| 50 | + const session = getSession(ctx, sessionDataKey) |
| 51 | + |
| 52 | + // Initialize times if not set |
| 53 | + if (!session.cleanupState || session.cleanupState.nextCleanupTime === 0) { |
| 54 | + session.cleanupState = this.initializeCleanupTimes() |
24 | 55 | } |
25 | 56 |
|
26 | | - // Case 2: Idle conversation (>1 hour) - Keep last 5 messages |
27 | | - if (timeDifference > IDLE_THRESHOLD) { |
28 | | - return conversation.slice(-IDLE_MESSAGE_LIMIT) |
| 57 | + // Check if we've passed the next cleanup time |
| 58 | + if (now >= session.cleanupState.nextCleanupTime) { |
| 59 | + // Update cleanup times in session |
| 60 | + session.cleanupState = { |
| 61 | + lastCleanupTime: session.cleanupState.nextCleanupTime, |
| 62 | + nextCleanupTime: session.cleanupState.nextCleanupTime + MS_PER_DAY |
| 63 | + } |
| 64 | + return true |
| 65 | + } |
| 66 | + |
| 67 | + return false |
| 68 | + }, |
| 69 | + |
| 70 | + /** |
| 71 | + * Manage conversation window with context-aware cleanup |
| 72 | + */ |
| 73 | + manageConversationWindow (conversation: ChatConversation[], ctx: OnMessageContext | OnCallBackQueryData, sessionDataKey: string): ChatConversation[] { |
| 74 | + if (conversation.length === 0) return conversation |
| 75 | + |
| 76 | + // Only perform cleanup if needed |
| 77 | + if (this.needsCleanup(ctx, sessionDataKey)) { |
| 78 | + const session = getSession(ctx, sessionDataKey) |
| 79 | + return conversation.filter(msg => msg.timestamp >= session.cleanupState.lastCleanupTime) |
29 | 80 | } |
30 | 81 |
|
31 | | - // Case 3: Active conversation (<1 hour) - Keep full history |
32 | 82 | return conversation |
33 | 83 | }, |
34 | 84 |
|
35 | | - addMessageWithTimestamp (message: Omit<ChatConversation, 'timestamp'> | Partial<Omit<ChatConversation, 'content' | 'timestamp'>> & { content: string | VisionContent[] }): ChatConversation { |
| 85 | + /** |
| 86 | + * Add a new message to the conversation with current timestamp |
| 87 | + */ |
| 88 | + addMessageWithTimestamp ( |
| 89 | + message: Omit<ChatConversation, 'timestamp'> | |
| 90 | + Partial<Omit<ChatConversation, 'content' | 'timestamp'>> & |
| 91 | + { content: string | VisionContent[] }, |
| 92 | + ctx: OnMessageContext | OnCallBackQueryData |
| 93 | + ): ChatConversation { |
| 94 | + // Initialize times if not set |
| 95 | + if (!ctx.session.llms.cleanupState || ctx.session.llms.cleanupState.nextCleanupTime === 0) { |
| 96 | + ctx.session.llms.cleanupState = this.initializeCleanupTimes() |
| 97 | + } |
| 98 | + |
36 | 99 | return { |
37 | 100 | ...message, |
38 | 101 | timestamp: Date.now() |
39 | 102 | } |
40 | 103 | } |
41 | 104 | } |
| 105 | + |
| 106 | +export { conversationManager } |
0 commit comments