@@ -91,6 +91,20 @@ let cpuMetrics = {
91
91
lastUpdate : Date . now ( )
92
92
} ;
93
93
94
+ const LOG_BUFFER_SIZE = 100 ; // Keep last 100 log entries
95
+ const logBuffer : string [ ] = [ ] ;
96
+
97
+ // Add this function to maintain the log buffer
98
+ function addToLogBuffer ( logData : string ) {
99
+ logBuffer . push ( logData ) ;
100
+ if ( logBuffer . length > LOG_BUFFER_SIZE ) {
101
+ logBuffer . shift ( ) ; // Remove oldest entry
102
+ }
103
+ }
104
+
105
+ // Subscribe to logs at the application level to maintain the buffer
106
+ elizaLogger . subscribe ( addToLogBuffer ) ;
107
+
94
108
async function verifyTokenMiddleware ( req : any , res : any , next ) {
95
109
// if JWT is not enabled, skip verification
96
110
if ( ! ( settings . JWT_ENABLED && settings . JWT_ENABLED . toLowerCase ( ) === 'true' ) ) {
@@ -562,6 +576,15 @@ export function createManageApiRouter(
562
576
563
577
// Send initial connection message
564
578
res . write ( `data: {"level":30,"time":${ now } ,"msg":"${ clientId } connected"}\n\n` ) ;
579
+
580
+ // Send buffered logs to the new client
581
+ logBuffer . forEach ( logData => {
582
+ try {
583
+ res . write ( `data: ${ logData } \n\n` ) ;
584
+ } catch ( err ) {
585
+ // Ignore errors when sending buffer
586
+ }
587
+ } ) ;
565
588
566
589
// Setup heartbeat
567
590
const heartbeatInterval = setInterval ( ( ) => {
@@ -573,7 +596,7 @@ export function createManageApiRouter(
573
596
}
574
597
} , 30000 ) ;
575
598
576
- // Subscribe to logs
599
+ // Subscribe to new logs
577
600
const unsubscribe = elizaLogger . subscribe ( ( logData ) => {
578
601
try {
579
602
res . write ( `data: ${ logData } \n\n` ) ;
@@ -586,6 +609,7 @@ export function createManageApiRouter(
586
609
const cleanup = ( ) => {
587
610
clearInterval ( heartbeatInterval ) ;
588
611
unsubscribe ( ) ;
612
+ elizaLogger . debug ( `Log client ${ clientId } disconnected` ) ;
589
613
} ;
590
614
591
615
// Handle client disconnect
@@ -604,6 +628,9 @@ export function createManageApiRouter(
604
628
req . on ( 'close' , ( ) => {
605
629
clearTimeout ( connectionTimeout ) ;
606
630
} ) ;
631
+
632
+ // Log that a new client connected
633
+ elizaLogger . debug ( `New log client ${ clientId } connected` ) ;
607
634
} ) ;
608
635
609
636
router . get ( "/system/metrics" , async ( req , res ) => {
0 commit comments