@@ -37,6 +37,7 @@ import {
3737 type LLMModelsManager ,
3838 type ModelVersion
3939} from './utils/llmModelsManager'
40+ import { conversationManager } from './utils/conversationManager'
4041
4142export abstract class LlmsBase implements PayableBot {
4243 public module : string
@@ -205,7 +206,8 @@ export abstract class LlmsBase implements PayableBot {
205206 id : ctx . message ?. message_id ,
206207 model,
207208 content : await preparePrompt ( ctx , prompt as string ) ,
208- numSubAgents : 0
209+ numSubAgents : 0 ,
210+ timestamp : Date . now ( )
209211 } )
210212 if ( ! session . isProcessingQueue ) {
211213 session . isProcessingQueue = true
@@ -218,7 +220,8 @@ export abstract class LlmsBase implements PayableBot {
218220 id : ctx . message ?. message_id ?? ctx . message ?. message_thread_id ?? 0 ,
219221 model,
220222 content : prompt as string ?? '' , // await preparePrompt(ctx, prompt as string),
221- numSubAgents : supportedAgents
223+ numSubAgents : supportedAgents ,
224+ timestamp : Date . now ( )
222225 }
223226 await this . runSubagents ( ctx , msg , stream , usesTools ) // prompt as string)
224227 }
@@ -230,6 +233,7 @@ export abstract class LlmsBase implements PayableBot {
230233
231234 async onChatRequestHandler ( ctx : OnMessageContext | OnCallBackQueryData , stream : boolean , usesTools : boolean ) : Promise < void > {
232235 const session = this . getSession ( ctx )
236+ session . chatConversation = conversationManager . manageConversationWindow ( session . chatConversation , ctx , this . sessionDataKey )
233237 while ( session . requestQueue . length > 0 ) {
234238 try {
235239 const msg = session . requestQueue . shift ( )
@@ -272,7 +276,8 @@ export abstract class LlmsBase implements PayableBot {
272276 const chat : ChatConversation = {
273277 content : enhancedPrompt || prompt ,
274278 role : 'user' ,
275- model : modelVersion
279+ model : modelVersion ,
280+ timestamp : Date . now ( )
276281 }
277282 chatConversation . push ( chat )
278283 const payload = {
@@ -358,7 +363,8 @@ export abstract class LlmsBase implements PayableBot {
358363 conversation . push ( {
359364 role : 'assistant' ,
360365 content : completion . completion ?. content ?? '' ,
361- model
366+ model,
367+ timestamp : Date . now ( )
362368 } )
363369 return {
364370 price : price . price ,
@@ -371,7 +377,8 @@ export abstract class LlmsBase implements PayableBot {
371377 conversation . push ( {
372378 role : 'assistant' ,
373379 content : response . completion ?. content ?? '' ,
374- model
380+ model,
381+ timestamp : Date . now ( )
375382 } )
376383 return {
377384 price : response . price ,
@@ -470,6 +477,49 @@ export abstract class LlmsBase implements PayableBot {
470477 session . price = 0
471478 }
472479
480+ async testCleanup ( ctx : OnMessageContext | OnCallBackQueryData ) : Promise < void > {
481+ const session = this . getSession ( ctx )
482+ // Force cleanup times for testing
483+ const now = new Date ( )
484+ const forcedCleanupTime = new Date ( now )
485+ forcedCleanupTime . setHours ( 2 , 59 , 0 , 0 ) // Set to 2:59 AM
486+ session . cleanupState = {
487+ nextCleanupTime : forcedCleanupTime . getTime ( ) + ( 60 * 1000 ) , // 3 AM
488+ lastCleanupTime : forcedCleanupTime . getTime ( ) - ( 24 * 60 * 60 * 1000 ) // Yesterday 2:59 AM
489+ }
490+ console . log ( 'Testing cleanup with forced times:' , {
491+ nextCleanup : new Date ( session . cleanupState . nextCleanupTime ) . toLocaleString ( ) ,
492+ lastCleanup : new Date ( session . cleanupState . lastCleanupTime ) . toLocaleString ( ) ,
493+ currentTime : now . toLocaleString ( )
494+ } )
495+ // Add some test messages with various timestamps
496+ if ( session . chatConversation . length === 0 ) {
497+ const yesterday = new Date ( now )
498+ yesterday . setDate ( yesterday . getDate ( ) - 1 )
499+ session . chatConversation = [
500+ {
501+ role : 'user' ,
502+ content : 'Message from 2 days ago' ,
503+ model : 'test' ,
504+ timestamp : yesterday . getTime ( ) - ( 24 * 60 * 60 * 1000 )
505+ } ,
506+ {
507+ role : 'assistant' ,
508+ content : 'Message from yesterday' ,
509+ model : 'test' ,
510+ timestamp : yesterday . getTime ( )
511+ } ,
512+ {
513+ role : 'user' ,
514+ content : 'Message from today' ,
515+ model : 'test' ,
516+ timestamp : now . getTime ( )
517+ }
518+ ]
519+ }
520+ await this . onChatRequestHandler ( ctx , false , false )
521+ }
522+
473523 async onError (
474524 ctx : OnMessageContext | OnCallBackQueryData ,
475525 e : any ,
0 commit comments