@@ -147,6 +147,25 @@ export class RuntimeContext extends EventEmitter {
147147
148148 const deleteSession = this . runtime . sessionClosed ;
149149
150+ //TODO : Do we need to cache the context if not in debug mode ?
151+
152+ const data = this . serialize ( ) ;
153+ //if (data) fs.writeFileSync(this.ctxFile, JSON.stringify(data, null, 2));
154+ if ( data ) {
155+ let serializedData = JSON . stringify ( data ) ;
156+ console . debug ( 'Agent Context Size' , this . ctxFile , serializedData . length , AccessCandidate . agent ( this . runtime . agent . id ) ) ;
157+
158+ await this . _cacheConnector
159+ . requester ( AccessCandidate . agent ( this . runtime . agent . id ) )
160+ . set ( this . ctxFile , serializedData , null , null , 3 * 60 * 60 ) ; //expires in 3 hours max
161+
162+ const mb = serializedData . length / 1024 / 1024 ;
163+ const cooldown = ( mb / 10 ) * 1000 ;
164+ serializedData = null ;
165+
166+ await delay ( cooldown ) ;
167+ }
168+
150169 if ( deleteSession ) {
151170 const exists = await this . _cacheConnector . requester ( AccessCandidate . agent ( this . runtime . agent . id ) ) . exists ( this . ctxFile ) ;
152171
@@ -159,33 +178,18 @@ export class RuntimeContext extends EventEmitter {
159178 //if (fs.existsSync(this.ctxFile)) fs.unlinkSync(this.ctxFile);
160179 this . ctxFile = null ;
161180 }
162- } else {
163- //TODO : Do we need to cache the context if not in debug mode ?
164-
165- const data = this . serialize ( ) ;
166- //if (data) fs.writeFileSync(this.ctxFile, JSON.stringify(data, null, 2));
167- if ( data ) {
168- let serializedData = JSON . stringify ( data ) ;
169- console . debug ( 'Agent Context Size' , this . ctxFile , serializedData . length , AccessCandidate . agent ( this . runtime . agent . id ) ) ;
170-
171- await this . _cacheConnector
172- . requester ( AccessCandidate . agent ( this . runtime . agent . id ) )
173- . set ( this . ctxFile , serializedData , null , null , 3 * 60 * 60 ) ; //expires in 3 hours max
174-
175- const mb = serializedData . length / 1024 / 1024 ;
176- const cooldown = ( mb / 10 ) * 1000 ;
177- serializedData = null ;
178-
179- await delay ( cooldown ) ;
180- }
181181 }
182182 }
183183 private _syncQueue = Promise . resolve ( ) ;
184184
185185 public enqueueSync ( ) {
186186 if ( ! this . ctxFile ) return ;
187187 console . log ( 'ENQUEUE SYNC' ) ;
188- this . _syncQueue = this . _syncQueue . then ( ( ) => this . sync ( ) ) . catch ( ( ) => { } ) ; // avoid unhandled rejections
188+ this . _syncQueue = this . _syncQueue
189+ . then ( ( ) => this . sync ( ) )
190+ . catch ( ( err ) => {
191+ console . error ( 'Error syncing context' , err ) ;
192+ } ) ; // avoid unhandled rejections
189193 }
190194 public incStep ( ) {
191195 this . step ++ ;
0 commit comments