@smythos/sre 1.5.55 → 1.5.56

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,7 +41,9 @@ export declare class RuntimeContext extends EventEmitter {
41
41
  private reset;
42
42
  private initRuntimeContext;
43
43
  ready(): Promise<boolean>;
44
- sync(): Promise<void>;
44
+ private sync;
45
+ private _syncQueue;
46
+ enqueueSync(): void;
45
47
  incStep(): void;
46
48
  updateComponent(componentId: string, data: any): void;
47
49
  resetComponent(componentId: string): void;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@smythos/sre",
3
- "version": "1.5.55",
3
+ "version": "1.5.56",
4
4
  "description": "Smyth Runtime Environment",
5
5
  "author": "Alaa-eddine KADDOURI",
6
6
  "license": "MIT",
@@ -1,13 +1,11 @@
1
- import fs from 'fs';
2
- import path from 'path';
3
- import { Agent } from './Agent.class';
4
1
  import { Component } from '@sre/Components/Component.class';
2
+ import { Agent } from './Agent.class';
5
3
 
6
4
  import { Logger } from '@sre/helpers/Log.helper';
7
- import { uid } from '@sre/utils';
8
- import { RuntimeContext } from '@sre/MemoryManager/RuntimeContext';
9
5
  import { LLMCache } from '@sre/MemoryManager/LLMCache';
6
+ import { RuntimeContext } from '@sre/MemoryManager/RuntimeContext';
10
7
  import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
8
+ import { uid } from '@sre/utils';
11
9
 
12
10
  const console = Logger('AgentRuntime');
13
11
  const AgentRuntimeUnavailable = new Proxy(
@@ -201,7 +199,7 @@ export class AgentRuntime {
201
199
  delete AgentRuntime.tagsData[this.reqTag];
202
200
  }
203
201
 
204
- this.agentContext.sync();
202
+ this.agentContext.enqueueSync();
205
203
  }
206
204
  public getWaitingComponents() {
207
205
  const ctxData = this.agentContext;
@@ -21,6 +21,7 @@ type TComponentContext = {
21
21
  input?: { [key: string]: any };
22
22
  output?: { [key: string]: any };
23
23
  };
24
+
24
25
  export class RuntimeContext extends EventEmitter {
25
26
  public circularLimitReached: string | boolean = false;
26
27
  public step: number = 0;
@@ -120,10 +121,12 @@ export class RuntimeContext extends EventEmitter {
120
121
  cpt.ctx.active = true;
121
122
  }
122
123
  }
123
- //fs.writeFileSync(this.ctxFile, JSON.stringify(ctxData, null, 2));
124
- await this._cacheConnector
125
- .requester(AccessCandidate.agent(this.runtime.agent.id))
126
- .set(this.ctxFile, JSON.stringify(ctxData, null, 2), null, null, 6 * 60 * 60); //expires in 6 hours max
124
+
125
+ if (this.runtime.debug) {
126
+ await this._cacheConnector
127
+ .requester(AccessCandidate.agent(this.runtime.agent.id))
128
+ .set(this.ctxFile, JSON.stringify(ctxData), null, null, 1 * 60 * 60); //expires in 1 hour
129
+ }
127
130
  } else {
128
131
  ctxData = JSON.parse(data);
129
132
  if (!ctxData.step) ctxData.step = 0;
@@ -133,42 +136,15 @@ export class RuntimeContext extends EventEmitter {
133
136
  this._runtimeFileReady = true;
134
137
  this.emit('ready');
135
138
  });
136
-
137
- // if (!fs.existsSync(this.ctxFile)) {
138
- // ctxData = JSON.parse(JSON.stringify({ components: agent.components, connections: agent.connections, timestamp: Date.now() }));
139
- // if (!ctxData.step) ctxData.step = 0;
140
- // for (let cptId in ctxData.components) {
141
- // ctxData.components[cptId] = {
142
- // id: cptId,
143
- // name: ctxData.components[cptId].name,
144
- // //dbg: { active: false, name: ctxData.components[cptId].name },
145
- // ctx: { active: false, name: ctxData.components[cptId].name },
146
- // };
147
-
148
- // const cpt = ctxData.components[cptId];
149
- // //if this debug session was initiated from an endpoint, we mark the endpoint component as active
150
- // if (endpoint && endpoint.id != undefined && cpt.id == endpoint.id && endpointDBGCall) {
151
- // //cpt.dbg.active = true;
152
- // cpt.ctx.active = true;
153
- // }
154
- // }
155
- // fs.writeFileSync(this.ctxFile, JSON.stringify(ctxData, null, 2));
156
- // } else {
157
- // ctxData = JSON.parse(fs.readFileSync(this.ctxFile, 'utf8'));
158
- // if (!ctxData.step) ctxData.step = 0;
159
- // }
160
-
161
- // this.deserialize(ctxData);
162
- // this._runtimeFileReady = true;
163
- // this.emit('ready');
164
139
  }
165
140
 
166
141
  public async ready() {
167
142
  if (this._runtimeFileReady) return true;
168
143
  return this._readyPromise;
169
144
  }
170
- public async sync() {
171
- if (!this.ctxFile) return;
145
+ private async sync() {
146
+ if (!this.ctxFile || this.runtime.debug) return;
147
+
172
148
  this.emit('syncing');
173
149
 
174
150
  const deleteSession = this.runtime.sessionClosed;
@@ -183,23 +159,36 @@ export class RuntimeContext extends EventEmitter {
183
159
  else this._cacheConnector.requester(AccessCandidate.agent(this.runtime.agent.id)).delete(this.ctxFile);
184
160
  //if (this.runtime.debug && fs.existsSync(this.ctxFile)) await delay(1000 * 60); //if we're in debug mode, we keep the file for a while to allow final state read
185
161
  //if (fs.existsSync(this.ctxFile)) fs.unlinkSync(this.ctxFile);
162
+ this.ctxFile = null;
186
163
  }
187
164
  } else {
188
165
  const data = this.serialize();
189
166
  //if (data) fs.writeFileSync(this.ctxFile, JSON.stringify(data, null, 2));
190
167
  if (data) {
191
- const serializedData = JSON.stringify(data, null, 2);
168
+ let serializedData = JSON.stringify(data);
192
169
  console.debug('Agent Context Size', this.ctxFile, serializedData.length, AccessCandidate.agent(this.runtime.agent.id));
193
170
  await this._cacheConnector
194
171
  .requester(AccessCandidate.agent(this.runtime.agent.id))
195
172
  .set(this.ctxFile, serializedData, null, null, 3 * 60 * 60); //expires in 3 hours max
173
+
174
+ const mb = serializedData.length / 1024 / 1024;
175
+ const cooldown = (mb / 10) * 1000;
176
+ serializedData = null;
177
+
178
+ await delay(cooldown);
196
179
  }
197
180
  }
198
181
  }
182
+ private _syncQueue = Promise.resolve();
199
183
 
184
+ public enqueueSync() {
185
+ if (!this.ctxFile) return;
186
+ console.log('ENQUEUE SYNC');
187
+ this._syncQueue = this._syncQueue.then(() => this.sync()).catch(() => {}); // avoid unhandled rejections
188
+ }
200
189
  public incStep() {
201
190
  this.step++;
202
- this.sync();
191
+ //this.sync();
203
192
  }
204
193
 
205
194
  public updateComponent(componentId: string, data: any) {
@@ -217,11 +206,16 @@ export class RuntimeContext extends EventEmitter {
217
206
  console.debug('>>> ctxFile', this.ctxFile, AccessCandidate.agent(this.runtime.agent.id));
218
207
  console.debug('>>> ctxData', ctxData, AccessCandidate.agent(this.runtime.agent.id));
219
208
  }
220
- component.ctx = { ...component.ctx, ...data, step: this.step };
209
+ //component.ctx = { ...component.ctx, ...data, step: this.step };
210
+ // minimal allocations
211
+
212
+ if (!component.ctx) component.ctx = { active: false, name: '', step: 0 };
213
+ Object.assign(component.ctx, data);
214
+ component.ctx.step = this.step;
221
215
 
222
216
  //if (this.debug) component.dbg = { ...component.dbg, ...data };
223
217
 
224
- this.sync();
218
+ this.enqueueSync();
225
219
  }
226
220
  public resetComponent(componentId: string) {
227
221
  const ctxData = this;
@@ -240,8 +234,13 @@ export class RuntimeContext extends EventEmitter {
240
234
  //component.dbg.runtimeData = {};
241
235
  component.ctx.runtimeData = {};
242
236
  component.ctx.active = false;
237
+ if (!this.runtime.debug) {
238
+ //console.debug('NOT in debug mode, clearing context input/output');
239
+ component.ctx.input = undefined;
240
+ component.ctx.output = undefined;
241
+ }
243
242
 
244
- this.sync();
243
+ this.enqueueSync();
245
244
  }
246
245
 
247
246
  public getComponentData(componentId: string) {