@smythos/sre 1.5.1 → 1.5.4

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.
Files changed (193) hide show
  1. package/LICENSE +18 -0
  2. package/dist/index.js +22329 -4
  3. package/dist/index.js.map +1 -1
  4. package/dist/types/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.d.ts +2 -2
  5. package/dist/types/subsystems/IO/VectorDB.service/embed/BaseEmbedding.d.ts +11 -7
  6. package/dist/types/types/VectorDB.types.d.ts +13 -11
  7. package/package.json +102 -127
  8. package/src/Components/APICall/APICall.class.ts +155 -0
  9. package/src/Components/APICall/AccessTokenManager.ts +130 -0
  10. package/src/Components/APICall/ArrayBufferResponse.helper.ts +58 -0
  11. package/src/Components/APICall/OAuth.helper.ts +294 -0
  12. package/src/Components/APICall/mimeTypeCategories.ts +46 -0
  13. package/src/Components/APICall/parseData.ts +167 -0
  14. package/src/Components/APICall/parseHeaders.ts +41 -0
  15. package/src/Components/APICall/parseProxy.ts +68 -0
  16. package/src/Components/APICall/parseUrl.ts +91 -0
  17. package/src/Components/APIEndpoint.class.ts +234 -0
  18. package/src/Components/APIOutput.class.ts +58 -0
  19. package/src/Components/AgentPlugin.class.ts +102 -0
  20. package/src/Components/Async.class.ts +155 -0
  21. package/src/Components/Await.class.ts +90 -0
  22. package/src/Components/Classifier.class.ts +158 -0
  23. package/src/Components/Component.class.ts +94 -0
  24. package/src/Components/ComponentHost.class.ts +38 -0
  25. package/src/Components/DataSourceCleaner.class.ts +92 -0
  26. package/src/Components/DataSourceIndexer.class.ts +181 -0
  27. package/src/Components/DataSourceLookup.class.ts +141 -0
  28. package/src/Components/FEncDec.class.ts +29 -0
  29. package/src/Components/FHash.class.ts +33 -0
  30. package/src/Components/FSign.class.ts +80 -0
  31. package/src/Components/FSleep.class.ts +25 -0
  32. package/src/Components/FTimestamp.class.ts +25 -0
  33. package/src/Components/FileStore.class.ts +75 -0
  34. package/src/Components/ForEach.class.ts +97 -0
  35. package/src/Components/GPTPlugin.class.ts +70 -0
  36. package/src/Components/GenAILLM.class.ts +395 -0
  37. package/src/Components/HuggingFace.class.ts +314 -0
  38. package/src/Components/Image/imageSettings.config.ts +70 -0
  39. package/src/Components/ImageGenerator.class.ts +407 -0
  40. package/src/Components/JSONFilter.class.ts +54 -0
  41. package/src/Components/LLMAssistant.class.ts +213 -0
  42. package/src/Components/LogicAND.class.ts +28 -0
  43. package/src/Components/LogicAtLeast.class.ts +85 -0
  44. package/src/Components/LogicAtMost.class.ts +86 -0
  45. package/src/Components/LogicOR.class.ts +29 -0
  46. package/src/Components/LogicXOR.class.ts +34 -0
  47. package/src/Components/MCPClient.class.ts +112 -0
  48. package/src/Components/PromptGenerator.class.ts +122 -0
  49. package/src/Components/ScrapflyWebScrape.class.ts +159 -0
  50. package/src/Components/TavilyWebSearch.class.ts +98 -0
  51. package/src/Components/index.ts +77 -0
  52. package/src/Core/AgentProcess.helper.ts +240 -0
  53. package/src/Core/Connector.class.ts +123 -0
  54. package/src/Core/ConnectorsService.ts +192 -0
  55. package/src/Core/DummyConnector.ts +49 -0
  56. package/src/Core/HookService.ts +105 -0
  57. package/src/Core/SmythRuntime.class.ts +292 -0
  58. package/src/Core/SystemEvents.ts +15 -0
  59. package/src/Core/boot.ts +55 -0
  60. package/src/config.ts +15 -0
  61. package/src/constants.ts +125 -0
  62. package/src/data/hugging-face.params.json +580 -0
  63. package/src/helpers/BinaryInput.helper.ts +324 -0
  64. package/src/helpers/Conversation.helper.ts +1094 -0
  65. package/src/helpers/JsonContent.helper.ts +97 -0
  66. package/src/helpers/LocalCache.helper.ts +97 -0
  67. package/src/helpers/Log.helper.ts +234 -0
  68. package/src/helpers/OpenApiParser.helper.ts +150 -0
  69. package/src/helpers/S3Cache.helper.ts +129 -0
  70. package/src/helpers/SmythURI.helper.ts +5 -0
  71. package/src/helpers/TemplateString.helper.ts +243 -0
  72. package/src/helpers/TypeChecker.helper.ts +329 -0
  73. package/src/index.ts +179 -0
  74. package/src/index.ts.bak +179 -0
  75. package/src/subsystems/AgentManager/Agent.class.ts +1108 -0
  76. package/src/subsystems/AgentManager/Agent.helper.ts +3 -0
  77. package/src/subsystems/AgentManager/AgentData.service/AgentDataConnector.ts +230 -0
  78. package/src/subsystems/AgentManager/AgentData.service/connectors/CLIAgentDataConnector.class.ts +66 -0
  79. package/src/subsystems/AgentManager/AgentData.service/connectors/LocalAgentDataConnector.class.ts +142 -0
  80. package/src/subsystems/AgentManager/AgentData.service/connectors/NullAgentData.class.ts +39 -0
  81. package/src/subsystems/AgentManager/AgentData.service/index.ts +18 -0
  82. package/src/subsystems/AgentManager/AgentLogger.class.ts +297 -0
  83. package/src/subsystems/AgentManager/AgentRequest.class.ts +51 -0
  84. package/src/subsystems/AgentManager/AgentRuntime.class.ts +559 -0
  85. package/src/subsystems/AgentManager/AgentSSE.class.ts +101 -0
  86. package/src/subsystems/AgentManager/AgentSettings.class.ts +52 -0
  87. package/src/subsystems/AgentManager/Component.service/ComponentConnector.ts +32 -0
  88. package/src/subsystems/AgentManager/Component.service/connectors/LocalComponentConnector.class.ts +59 -0
  89. package/src/subsystems/AgentManager/Component.service/index.ts +11 -0
  90. package/src/subsystems/AgentManager/EmbodimentSettings.class.ts +47 -0
  91. package/src/subsystems/AgentManager/ForkedAgent.class.ts +153 -0
  92. package/src/subsystems/AgentManager/OSResourceMonitor.ts +77 -0
  93. package/src/subsystems/ComputeManager/Code.service/CodeConnector.ts +99 -0
  94. package/src/subsystems/ComputeManager/Code.service/connectors/AWSLambdaCode.class.ts +63 -0
  95. package/src/subsystems/ComputeManager/Code.service/index.ts +11 -0
  96. package/src/subsystems/IO/CLI.service/CLIConnector.ts +47 -0
  97. package/src/subsystems/IO/CLI.service/index.ts +9 -0
  98. package/src/subsystems/IO/Log.service/LogConnector.ts +32 -0
  99. package/src/subsystems/IO/Log.service/connectors/ConsoleLog.class.ts +28 -0
  100. package/src/subsystems/IO/Log.service/index.ts +13 -0
  101. package/src/subsystems/IO/NKV.service/NKVConnector.ts +41 -0
  102. package/src/subsystems/IO/NKV.service/connectors/NKVRAM.class.ts +204 -0
  103. package/src/subsystems/IO/NKV.service/connectors/NKVRedis.class.ts +182 -0
  104. package/src/subsystems/IO/NKV.service/index.ts +12 -0
  105. package/src/subsystems/IO/Router.service/RouterConnector.ts +21 -0
  106. package/src/subsystems/IO/Router.service/connectors/ExpressRouter.class.ts +48 -0
  107. package/src/subsystems/IO/Router.service/connectors/NullRouter.class.ts +40 -0
  108. package/src/subsystems/IO/Router.service/index.ts +11 -0
  109. package/src/subsystems/IO/Storage.service/SmythFS.class.ts +472 -0
  110. package/src/subsystems/IO/Storage.service/StorageConnector.ts +66 -0
  111. package/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts +305 -0
  112. package/src/subsystems/IO/Storage.service/connectors/S3Storage.class.ts +418 -0
  113. package/src/subsystems/IO/Storage.service/index.ts +13 -0
  114. package/src/subsystems/IO/VectorDB.service/VectorDBConnector.ts +108 -0
  115. package/src/subsystems/IO/VectorDB.service/connectors/MilvusVectorDB.class.ts +454 -0
  116. package/src/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.ts +384 -0
  117. package/src/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.ts +421 -0
  118. package/src/subsystems/IO/VectorDB.service/embed/BaseEmbedding.ts +107 -0
  119. package/src/subsystems/IO/VectorDB.service/embed/OpenAIEmbedding.ts +109 -0
  120. package/src/subsystems/IO/VectorDB.service/embed/index.ts +21 -0
  121. package/src/subsystems/IO/VectorDB.service/index.ts +14 -0
  122. package/src/subsystems/LLMManager/LLM.helper.ts +221 -0
  123. package/src/subsystems/LLMManager/LLM.inference.ts +335 -0
  124. package/src/subsystems/LLMManager/LLM.service/LLMConnector.ts +375 -0
  125. package/src/subsystems/LLMManager/LLM.service/LLMCredentials.helper.ts +145 -0
  126. package/src/subsystems/LLMManager/LLM.service/connectors/Anthropic.class.ts +632 -0
  127. package/src/subsystems/LLMManager/LLM.service/connectors/Bedrock.class.ts +405 -0
  128. package/src/subsystems/LLMManager/LLM.service/connectors/Echo.class.ts +81 -0
  129. package/src/subsystems/LLMManager/LLM.service/connectors/GoogleAI.class.ts +689 -0
  130. package/src/subsystems/LLMManager/LLM.service/connectors/Groq.class.ts +257 -0
  131. package/src/subsystems/LLMManager/LLM.service/connectors/OpenAI.class.ts +848 -0
  132. package/src/subsystems/LLMManager/LLM.service/connectors/Perplexity.class.ts +255 -0
  133. package/src/subsystems/LLMManager/LLM.service/connectors/VertexAI.class.ts +193 -0
  134. package/src/subsystems/LLMManager/LLM.service/index.ts +43 -0
  135. package/src/subsystems/LLMManager/ModelsProvider.service/ModelsProviderConnector.ts +281 -0
  136. package/src/subsystems/LLMManager/ModelsProvider.service/connectors/SmythModelsProvider.class.ts +229 -0
  137. package/src/subsystems/LLMManager/ModelsProvider.service/index.ts +11 -0
  138. package/src/subsystems/LLMManager/custom-models.ts +854 -0
  139. package/src/subsystems/LLMManager/models.ts +2539 -0
  140. package/src/subsystems/LLMManager/paramMappings.ts +69 -0
  141. package/src/subsystems/MemoryManager/Cache.service/CacheConnector.ts +86 -0
  142. package/src/subsystems/MemoryManager/Cache.service/connectors/LocalStorageCache.class.ts +297 -0
  143. package/src/subsystems/MemoryManager/Cache.service/connectors/RAMCache.class.ts +201 -0
  144. package/src/subsystems/MemoryManager/Cache.service/connectors/RedisCache.class.ts +252 -0
  145. package/src/subsystems/MemoryManager/Cache.service/connectors/S3Cache.class.ts +373 -0
  146. package/src/subsystems/MemoryManager/Cache.service/index.ts +15 -0
  147. package/src/subsystems/MemoryManager/LLMCache.ts +72 -0
  148. package/src/subsystems/MemoryManager/LLMContext.ts +125 -0
  149. package/src/subsystems/MemoryManager/RuntimeContext.ts +249 -0
  150. package/src/subsystems/Security/AccessControl/ACL.class.ts +208 -0
  151. package/src/subsystems/Security/AccessControl/AccessCandidate.class.ts +76 -0
  152. package/src/subsystems/Security/AccessControl/AccessRequest.class.ts +52 -0
  153. package/src/subsystems/Security/Account.service/AccountConnector.ts +41 -0
  154. package/src/subsystems/Security/Account.service/connectors/AWSAccount.class.ts +76 -0
  155. package/src/subsystems/Security/Account.service/connectors/DummyAccount.class.ts +130 -0
  156. package/src/subsystems/Security/Account.service/connectors/JSONFileAccount.class.ts +159 -0
  157. package/src/subsystems/Security/Account.service/index.ts +14 -0
  158. package/src/subsystems/Security/Credentials.helper.ts +62 -0
  159. package/src/subsystems/Security/ManagedVault.service/ManagedVaultConnector.ts +34 -0
  160. package/src/subsystems/Security/ManagedVault.service/connectors/NullManagedVault.class.ts +57 -0
  161. package/src/subsystems/Security/ManagedVault.service/connectors/SecretManagerManagedVault.ts +154 -0
  162. package/src/subsystems/Security/ManagedVault.service/index.ts +12 -0
  163. package/src/subsystems/Security/SecureConnector.class.ts +110 -0
  164. package/src/subsystems/Security/Vault.service/Vault.helper.ts +30 -0
  165. package/src/subsystems/Security/Vault.service/VaultConnector.ts +26 -0
  166. package/src/subsystems/Security/Vault.service/connectors/HashicorpVault.class.ts +46 -0
  167. package/src/subsystems/Security/Vault.service/connectors/JSONFileVault.class.ts +166 -0
  168. package/src/subsystems/Security/Vault.service/connectors/NullVault.class.ts +54 -0
  169. package/src/subsystems/Security/Vault.service/connectors/SecretsManager.class.ts +140 -0
  170. package/src/subsystems/Security/Vault.service/index.ts +12 -0
  171. package/src/types/ACL.types.ts +104 -0
  172. package/src/types/AWS.types.ts +9 -0
  173. package/src/types/Agent.types.ts +61 -0
  174. package/src/types/AgentLogger.types.ts +17 -0
  175. package/src/types/Cache.types.ts +1 -0
  176. package/src/types/Common.types.ts +3 -0
  177. package/src/types/LLM.types.ts +419 -0
  178. package/src/types/Redis.types.ts +8 -0
  179. package/src/types/SRE.types.ts +64 -0
  180. package/src/types/Security.types.ts +18 -0
  181. package/src/types/Storage.types.ts +5 -0
  182. package/src/types/VectorDB.types.ts +86 -0
  183. package/src/utils/base64.utils.ts +275 -0
  184. package/src/utils/cli.utils.ts +68 -0
  185. package/src/utils/data.utils.ts +263 -0
  186. package/src/utils/date-time.utils.ts +22 -0
  187. package/src/utils/general.utils.ts +238 -0
  188. package/src/utils/index.ts +12 -0
  189. package/src/utils/numbers.utils.ts +13 -0
  190. package/src/utils/oauth.utils.ts +35 -0
  191. package/src/utils/string.utils.ts +414 -0
  192. package/src/utils/url.utils.ts +19 -0
  193. package/src/utils/validation.utils.ts +74 -0
@@ -0,0 +1,559 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { Agent } from './Agent.class';
4
+ import { Component } from '@sre/Components/Component.class';
5
+
6
+ import { Logger } from '@sre/helpers/Log.helper';
7
+ import { uid } from '@sre/utils';
8
+ import { RuntimeContext } from '@sre/MemoryManager/RuntimeContext';
9
+ import { LLMCache } from '@sre/MemoryManager/LLMCache';
10
+ import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
11
+
12
+ const console = Logger('AgentRuntime');
13
+ const AgentRuntimeUnavailable = new Proxy(
14
+ {},
15
+ {
16
+ get: function (target, prop, receiver) {
17
+ // Check if the property being accessed is a function
18
+ if (typeof target[prop] === 'function') {
19
+ return target[prop];
20
+ } else {
21
+ // Return a function that logs "unavailable"
22
+ return function () {
23
+ console.warn(`AgentRuntime Unavailable tried to call : ${prop.toString()}`);
24
+ };
25
+ }
26
+ },
27
+ },
28
+ );
29
+ export class AgentRuntime {
30
+ private static processResults: any = {};
31
+ private static tagsData = {};
32
+ public static dummy = AgentRuntimeUnavailable;
33
+
34
+ private agentContext: RuntimeContext;
35
+ public llmCache: LLMCache;
36
+ //private ctxFile: string = '';
37
+ private xDebugRun: string | undefined = '';
38
+ private xDebugInject: string | undefined = '';
39
+ private xDebugRead: string | undefined = '';
40
+ private xDebugStop: string | undefined = '';
41
+ private xDebugPendingInject: any = null;
42
+ private xMockDataInject: any = null;
43
+ public xDebugId: string | undefined = '';
44
+
45
+ private xDebugCmd: string | undefined = '';
46
+ private _debugActive = false;
47
+ private _runtimeFileReady = false;
48
+ public sessionClosed = false;
49
+
50
+ private reqTagOwner = false;
51
+
52
+ //reqTag is used to identify the current running workflow including nested calls, it allows us to identify circular calls
53
+ public reqTag: any;
54
+ public processID: any; //this identifies the current processID, a process ID is the full set of runCycles that is executed by the agent.
55
+ public workflowReqId: any; //this identifies the current running workflow. a workflow starts when and agent endpoint is called, or a debug session is initiated, and ends when no more steps can be executed.
56
+
57
+ public alwaysActiveComponents: any = {};
58
+ public exclusiveComponents: any = {};
59
+
60
+ private checkRuntimeContext: any = null;
61
+
62
+ public get circularLimitReached() {
63
+ return this.agentContext?.circularLimitReached || false;
64
+ }
65
+ public set circularLimitReached(value) {
66
+ if (this.agentContext) this.agentContext.circularLimitReached = value;
67
+ }
68
+
69
+ public get debug() {
70
+ return this._debugActive;
71
+ }
72
+ public get curStep() {
73
+ return this.agentContext?.step || 0;
74
+ }
75
+
76
+ constructor(public agent: Agent) {
77
+ this.reqTag = agent.agentRequest.header('X-REQUEST-TAG');
78
+ const isNestedProcess: boolean = !!this.reqTag;
79
+
80
+ if (!this.reqTag) {
81
+ //tagged request should not be run in debug mode, this comes from a parent agent
82
+ this.xDebugStop = agent.agentRequest.header('X-DEBUG-STOP');
83
+ this.xDebugRun = agent.agentRequest.header('X-DEBUG-RUN'); //send this as header to create a session and attach it
84
+ this.xDebugInject = agent.agentRequest.header('X-DEBUG-INJ');
85
+ this.xDebugRead = agent.agentRequest.header('X-DEBUG-READ');
86
+ this.reqTag = 'xTAG-' + uid(); //if request tag is not set, set a new value, this will be used to tag nested agent calls
87
+ this.reqTagOwner = true;
88
+ } else {
89
+ this.xDebugStop = undefined;
90
+ this.xDebugRun = undefined;
91
+ this.xDebugInject = undefined;
92
+ this.xDebugRead = undefined;
93
+ }
94
+
95
+ this.xDebugId = this.xDebugStop || this.xDebugRun || this.xDebugRead;
96
+
97
+ //if (req.body) {
98
+ if (!this.xDebugId && agent.agentRequest.body) {
99
+ if (this.xDebugInject != undefined && this.xDebugInject != null) {
100
+ this.xDebugPendingInject = agent.agentRequest.body;
101
+ this.xDebugRun = this.xDebugInject || 'inj-' + uid();
102
+ } else {
103
+ if (this.xDebugRun == '') {
104
+ this.xDebugRun = 'dbg-' + uid(); //generate a random debug id
105
+ }
106
+ }
107
+ this.xDebugId = this.xDebugRun;
108
+ }
109
+
110
+ if (agent.agentRequest.header('X-MOCK-DATA-INJ') !== undefined) {
111
+ this.xMockDataInject = agent.agentRequest.body;
112
+ }
113
+
114
+ this.processID = this.xDebugId;
115
+
116
+ if (!this.xDebugId) {
117
+ //if it's not a debug session, processID is unique per request
118
+ this.xDebugId = agent.sessionId;
119
+ this.processID = this.reqTag;
120
+ }
121
+ if (isNestedProcess) {
122
+ // Need to make processID unique to run same sub-agents multiple times in parallel
123
+ this.processID += `:${Math.floor(1000 + Math.random() * 9000)}`;
124
+ }
125
+
126
+ //we need a way to identify current running workflow in a unique way
127
+ //=> In debug mode, xDebugRun holds the debug sessionID which is unique per workflow run
128
+ // if the debug session is stopped, xDebugStop holds the sessionID
129
+ // Note : We can't use reqTag in debug mode because it changes every time a new debug step is executed
130
+ //
131
+ //=> In normal mode, reqTag is unique per workflow run
132
+ this.workflowReqId = this.xDebugRun || this.xDebugStop || this.reqTag;
133
+
134
+ //tagsData can be updated from external integrations (eg. Chatbot, API Endpoint, etc.)
135
+ if (!AgentRuntime.tagsData[this.reqTag]) AgentRuntime.tagsData[this.reqTag] = {};
136
+ if (!AgentRuntime.processResults[this.processID])
137
+ AgentRuntime.processResults[this.processID] = {
138
+ timestamp: Date.now(),
139
+ errorResults: [],
140
+ sessionResults: [],
141
+ };
142
+
143
+ this.agentContext = new RuntimeContext(this);
144
+ this.agentContext.on('ready', () => {
145
+ this.alwaysActiveComponents = {};
146
+ this.exclusiveComponents = {};
147
+ for (let component of this.agent.data.components) {
148
+ const cpt: Component = this.agent.ComponentInstances[component.name];
149
+ if (!cpt) {
150
+ console.warn(`Component ${component.name} Exists in agent but has no implementation`);
151
+ continue;
152
+ }
153
+
154
+ if (cpt.alwaysActive) {
155
+ this.alwaysActiveComponents[component.id] = cpt;
156
+ this.updateComponent(component.id, { active: true, alwaysActive: true });
157
+ const runtimeData = { ...this.getRuntimeData(component.id) };
158
+ this.saveRuntimeComponentData(component.id, runtimeData);
159
+ }
160
+ if (cpt.exclusive) {
161
+ this.exclusiveComponents[component.id] = cpt;
162
+ this.updateComponent(component.id, { exclusive: true });
163
+ const runtimeData = { ...this.getRuntimeData(component.id) };
164
+ this.saveRuntimeComponentData(component.id, runtimeData);
165
+ }
166
+ }
167
+ });
168
+
169
+ //if xDebugId is equal to agent session, it means that the debugging features are not active
170
+ this._debugActive = this.xDebugId != agent.sessionId;
171
+
172
+ const xCacheId = agent.agentRequest.header('X-CACHE-ID') || '';
173
+ this.llmCache = new LLMCache(AccessCandidate.agent(this.agent.id), xCacheId);
174
+ //this.xCacheId =
175
+ }
176
+
177
+ public async ready() {
178
+ return this.agentContext.ready();
179
+ }
180
+
181
+ public destroy() {
182
+ this.sessionClosed = true;
183
+ this.sync();
184
+ }
185
+
186
+ public incTag(componentId) {
187
+ if (!AgentRuntime.tagsData[this.reqTag][componentId]) AgentRuntime.tagsData[this.reqTag][componentId] = 0;
188
+ AgentRuntime.tagsData[this.reqTag][componentId]++;
189
+
190
+ // console.log(
191
+ // `incTag agentId=${this.agent.id} componentId=${componentId} tag=${this.reqTag} ==> ${AgentRuntime.tagsData[this.reqTag][componentId]}`,
192
+ // );
193
+ //console.log('incTag tagsData', tagsData);
194
+ }
195
+
196
+ public async sync() {
197
+ //if (!this.ctxFile) return;
198
+
199
+ const deleteTag = (this.reqTagOwner && this.sessionClosed) || this.circularLimitReached;
200
+ if (deleteTag) {
201
+ console.log('>>>>>>>>>>>> deleting tagsData', this.reqTag);
202
+ delete AgentRuntime.tagsData[this.reqTag];
203
+ }
204
+
205
+ this.agentContext.sync();
206
+ }
207
+ public getWaitingComponents() {
208
+ const ctxData = this.agentContext;
209
+ const dbgComponents: any = Object.values(ctxData?.components || []).filter((c: any) => c?.ctx?.active == true);
210
+ const waitingComponents: any = dbgComponents.filter((c: any) => c?.ctx?.status && typeof c?.ctx?.output !== undefined);
211
+ return waitingComponents;
212
+ }
213
+ public getExclusiveActiveComponents() {
214
+ const ctxData = this.agentContext;
215
+ const dbgComponents: any = Object.values(ctxData?.components || []).filter((c: any) => c?.ctx?.active == true);
216
+ const exclusiveActiveComponents: any = dbgComponents.filter((c: any) => c?.ctx?.exclusive == true);
217
+ return exclusiveActiveComponents;
218
+ }
219
+ public readState(stateId: string, deltaOnly = false) {
220
+ //if (!this._debugActive || !this.xDebugRead) return null;
221
+ if (!this._debugActive || !stateId) return null;
222
+
223
+ //this.checkRuntimeContext();
224
+ const runtime = this;
225
+ const agent = this.agent;
226
+
227
+ const ctxData = runtime.agentContext;
228
+ const dbgAllComponents: any = runtime.xDebugPendingInject || Object.values(ctxData?.components || []);
229
+
230
+ //first try to find exclusive active components
231
+ let dbgActiveComponents: any;
232
+ dbgActiveComponents = dbgAllComponents.filter((c: any) => c?.ctx?.active == true && c?.ctx?.exclusive == true);
233
+ //if no exclusive active components, find all active components
234
+ if (!dbgActiveComponents || dbgActiveComponents.length == 0)
235
+ dbgActiveComponents = dbgAllComponents.filter(
236
+ (c: any) =>
237
+ c?.ctx?.active == true ||
238
+ (!c?.ctx?.output?._error && Array.isArray(c?.ctx?._job_components) && c?.ctx?._job_components.length > 0),
239
+ );
240
+ //find waiting components that was not previously run
241
+ const dbgActiveWaitingComponents: any = dbgAllComponents.filter(
242
+ (c: any) => c?.ctx?.active == true && c?.ctx?.status && typeof c?.ctx?.output !== undefined,
243
+ );
244
+
245
+ const dbgActiveReadyComponents: any = dbgAllComponents.filter((c: any) => c?.ctx?.active == true && !c?.ctx?.status);
246
+
247
+ let state = {};
248
+ for (let dbgComponent of dbgAllComponents) {
249
+ state[dbgComponent.id] = dbgComponent.ctx;
250
+ }
251
+
252
+ //let dbgSession: any = runtime.xDebugRead;
253
+ let dbgSession: any = stateId;
254
+
255
+ // let alwaysActiveComponents = 0;
256
+ // for (let activeCpt of dbgActiveComponents) {
257
+ // if (this.agent.alwaysActiveComponents[activeCpt.id]) alwaysActiveComponents++;
258
+ // }
259
+
260
+ if (!dbgActiveComponents || dbgActiveComponents.length == 0 /*|| dbgActiveComponents.length == alwaysActiveComponents*/) {
261
+ dbgSession = null;
262
+ runtime.sessionClosed = true;
263
+ }
264
+
265
+ const remainingActiveComponents: any = Object.values(ctxData?.components || []).filter(
266
+ (c: any) => c?.ctx?.active == true && !c?.ctx?.alwaysActive,
267
+ );
268
+ const activeAsyncComponents: any = Object.values(ctxData?.components || []).filter(
269
+ (c: any) => !c?.ctx?.output?._error && Array.isArray(c?.ctx?._job_components) && c?.ctx?._job_components.length > 0,
270
+ );
271
+
272
+ if (remainingActiveComponents.length == 0 && activeAsyncComponents.length == 0 /*&& awaitingInputs.length == 0*/) {
273
+ runtime.sessionClosed = true;
274
+ }
275
+
276
+ if (runtime.circularLimitReached) {
277
+ const circularLimitData = runtime.checkCircularLimit();
278
+ const error = `Circular Calls Limit Reached on ${circularLimitData}. Current agent circular limit is ${agent.circularLimit}`;
279
+ runtime.sessionClosed = true;
280
+ return { state, dbgSession, sessionClosed: runtime.sessionClosed, error };
281
+ }
282
+
283
+ const step = this.curStep >= 1 ? this.curStep - 1 : 0; //current state was executed in previous step
284
+
285
+ if (deltaOnly) {
286
+ const delta = {};
287
+ for (let cptId in state) {
288
+ const cpt = state[cptId];
289
+
290
+ //workaround, here we are supposed to test component steps that are equalt to current step
291
+ //but due to an inconsistency, the Async component has sometimes a step greater than the current step
292
+ if (cpt.step >= step) delta[cptId] = cpt;
293
+ //FIXME : identify the root cause of this issue and replace >= with ==
294
+ }
295
+ //return { state: delta, dbgSession, sessionClosed: runtime.sessionClosed, step };
296
+ state = delta;
297
+ }
298
+
299
+ return { state, dbgSession, sessionClosed: runtime.sessionClosed, step };
300
+ }
301
+
302
+ /**
303
+ * This method is called by the agent to run a process cycle, it will run all active components and return the results
304
+ * The function is called multiple times until all components are executed and no more active components are available
305
+ * @returns
306
+ */
307
+ public async runCycle() {
308
+ console.debug(
309
+ `runCycle agentId=${this.agent.id} wfReqId=${this.workflowReqId} reqTag=${this.reqTag} session=${this.xDebugRun} cycleId=${this.processID}`,
310
+ );
311
+ //this.checkRuntimeContext();
312
+
313
+ const runtime = this;
314
+ const agent = this.agent;
315
+ const ctxData = runtime.agentContext;
316
+ const dbgAllComponents: any = runtime.xDebugPendingInject || Object.values(ctxData?.components || []);
317
+
318
+ //first try to find exclusive active components
319
+ let dbgActiveComponents: any;
320
+ dbgActiveComponents = dbgAllComponents.filter((c: any) => c?.ctx?.active == true && c?.ctx?.exclusive == true);
321
+ //if no exclusive active components, find all active components
322
+ if (!dbgActiveComponents || dbgActiveComponents.length == 0)
323
+ dbgActiveComponents = dbgAllComponents.filter(
324
+ (c: any) =>
325
+ c?.ctx?.active == true ||
326
+ (!c?.ctx?.output?._error && Array.isArray(c?.ctx?._job_components) && c?.ctx?._job_components.length > 0),
327
+ );
328
+ //find waiting components that was not previously run
329
+ const dbgActiveWaitingComponents: any = dbgAllComponents.filter(
330
+ (c: any) => c?.ctx?.active == true && c?.ctx?.status && typeof c?.ctx?.output !== undefined,
331
+ );
332
+ const dbgActiveReadyComponents: any = dbgAllComponents.filter(
333
+ (c: any) =>
334
+ (c?.ctx?.active == true && !c?.ctx?.status) ||
335
+ (!c?.ctx?.output?._error && Array.isArray(c?.ctx?._job_components) && c?.ctx?._job_components.length > 0),
336
+ );
337
+ //const dbgActiveReadyComponents: any = dbgActiveComponents.filter((c: any) => c?.ctx?.active == true && !c?.ctx?.status);
338
+
339
+ let step: any;
340
+
341
+ if (!dbgActiveComponents || dbgActiveComponents.length == 0) {
342
+ runtime.sessionClosed = true;
343
+ step = {
344
+ state: { sessionClosed: true },
345
+ dbgSession: null,
346
+ //expiredDbgSession: runtime.xDebugRun || runtime.xDebugStop,
347
+ expiredDbgSession: runtime.xDebugId,
348
+ sessionClosed: true,
349
+ };
350
+ }
351
+
352
+ if (!step && dbgActiveComponents.length == dbgActiveWaitingComponents.length && ctxData.sessionResult) {
353
+ runtime.sessionClosed = true;
354
+ step = {
355
+ state: { sessionClosed: true },
356
+ dbgSession: null,
357
+ //expiredDbgSession: runtime.xDebugRun,
358
+ expiredDbgSession: runtime.xDebugId,
359
+ sessionClosed: true,
360
+ };
361
+ }
362
+ if (!step && dbgActiveReadyComponents.length > 0) {
363
+ const promises: any = [];
364
+
365
+ for (let dbgComponent of dbgActiveReadyComponents) {
366
+ const injectInput = runtime.xDebugPendingInject ? dbgComponent.ctx.input : undefined;
367
+
368
+ promises.push(agent.callComponent(dbgComponent.ctx.sourceId, dbgComponent.id, injectInput));
369
+ }
370
+ const dbgResults = await Promise.all(promises);
371
+ const state = dbgResults.length == 1 ? dbgResults[0] : dbgResults;
372
+
373
+ runtime.xDebugPendingInject = null;
374
+
375
+ const remainingActiveComponents: any = Object.values(ctxData?.components || []).filter((c: any) => c?.ctx?.active == true);
376
+ const activeAsyncComponents: any = Object.values(ctxData?.components || []).filter(
377
+ (c: any) => !c?.ctx?.output?._error && Array.isArray(c?.ctx?._job_components) && c?.ctx?._job_components.length > 0,
378
+ );
379
+ const dbgActiveWaitingComponents: any = dbgAllComponents.filter((c: any) => c?.ctx?.status && typeof c?.ctx?.output !== undefined);
380
+
381
+ if (dbgActiveWaitingComponents.length == remainingActiveComponents.length) {
382
+ ctxData.sessionResult = true;
383
+ }
384
+
385
+ //capture results
386
+ let sessionResults = dbgResults.flat().filter(
387
+ (e) =>
388
+ e.id &&
389
+ e.result &&
390
+ !e.result._missing_inputs &&
391
+ //check if this is the last component in the chain
392
+ !agent.connections.find((c) => c.sourceId == e.id),
393
+ );
394
+
395
+ let errorResults = dbgResults.flat().filter((e) => e.id && (e.error || e.result?._error));
396
+
397
+ //also filter out erroneous components that are children of a loop
398
+ //otherwise, errors inside a loop will be reported as session results and can lead to wrong results or generated noise in the final agent output
399
+ errorResults = errorResults.filter((e) => {
400
+ return !ctxData?.components?.[e.id]?.ctx?.runtimeData?._ChildLoopData;
401
+ });
402
+
403
+ if (ctxData.sessionResult && sessionResults.length == 0 && runtime.sessionClosed) {
404
+ //no result ? check if we have errors
405
+ sessionResults = errorResults;
406
+ }
407
+
408
+ ctxData.sessionResults = sessionResults;
409
+ step = {
410
+ state,
411
+ dbgSession: runtime.xDebugRun,
412
+ sessionResult: runtime.agentContext.sessionResult,
413
+ sessionResults: runtime.agentContext.sessionResults,
414
+ errorResults,
415
+ sessionClosed: remainingActiveComponents.length == 0 && activeAsyncComponents.length == 0 /*&& awaitingInputs.length == 0*/,
416
+ };
417
+ } else {
418
+ runtime.sessionClosed = true;
419
+ //return { sessionClosed: true };
420
+ step = {
421
+ state: { sessionClosed: true },
422
+ dbgSession: null,
423
+ //expiredDbgSession: runtime.xDebugRun || runtime.xDebugStop,
424
+ expiredDbgSession: runtime.xDebugId,
425
+ sessionClosed: true,
426
+ };
427
+ }
428
+
429
+ this.checkCircularLimit();
430
+ if (step.sessionResults) {
431
+ AgentRuntime.processResults[this.processID].sessionResults.push(step.sessionResults);
432
+ }
433
+ if (step.errorResults) {
434
+ AgentRuntime.processResults[this.processID].errorResults.push(step.errorResults);
435
+ }
436
+
437
+ if (step?.sessionClosed || this.circularLimitReached) {
438
+ const finalResult = this.processResults();
439
+ step.finalResult = finalResult;
440
+ runtime.sessionClosed = true;
441
+ }
442
+
443
+ this.incStep();
444
+ this.sync();
445
+ return step;
446
+ }
447
+
448
+ private processResults() {
449
+ //this.checkCircularLimit();
450
+ let result: any = { error: 'Error processing results' };
451
+ const sessionResults = AgentRuntime.processResults[this.processID].sessionResults;
452
+ const errorResults = AgentRuntime.processResults[this.processID].errorResults;
453
+ if (this.circularLimitReached) {
454
+ const circularLimitData = this.circularLimitReached;
455
+ result = { error: `Circular Calls Limit Reached on ${circularLimitData}. Current circular limit is ${this.agent.circularLimit}` };
456
+ } else {
457
+ let state = [sessionResults, errorResults].flat(Infinity);
458
+ if (!state || state.length == 0) state = errorResults.flat(Infinity);
459
+
460
+ //post process run cycle results
461
+ //deduplicating redundant entries
462
+
463
+ const data = state
464
+ .reduce(
465
+ (acc, current) => {
466
+ if (!acc.seen[current.id]) {
467
+ acc.result.push(current);
468
+ acc.seen[current.id] = true;
469
+ }
470
+ return acc;
471
+ },
472
+ { seen: {}, result: [] },
473
+ )
474
+ .result.filter((e) => !e.result?._exclude);
475
+
476
+ //data.forEach((d: any) => delete d?.result?._debug);
477
+
478
+ result = data;
479
+ /////////////
480
+ }
481
+
482
+ //cleanup
483
+ delete AgentRuntime.processResults[this.processID];
484
+
485
+ this.sync();
486
+ return result;
487
+ }
488
+
489
+ public checkCircularLimit() {
490
+ if (this.circularLimitReached) return this.agentContext.circularLimitReached;
491
+ for (let componentId in AgentRuntime.tagsData[this.reqTag]) {
492
+ if (AgentRuntime.tagsData[this.reqTag][componentId] > this.agent.circularLimit) {
493
+ this.sessionClosed = true;
494
+ this.agentContext.circularLimitReached = componentId;
495
+ return componentId;
496
+ }
497
+ }
498
+ return false;
499
+ }
500
+
501
+ public async injectDebugOutput(componentId: string) {
502
+ let component: {
503
+ ctx?: Record<string, any>;
504
+ } = {};
505
+
506
+ if (this.xDebugPendingInject) {
507
+ component = this.xDebugPendingInject?.find((c: any) => c.id == componentId);
508
+ } else if (this.xMockDataInject) {
509
+ component = this.xMockDataInject?.find((c: any) => c.id == componentId);
510
+ }
511
+
512
+ if (component?.ctx?.output) {
513
+ //if all outputs values are empty, we don't inject
514
+ let allEmpty = true;
515
+ for (let key in component.ctx.output) {
516
+ if (component.ctx.output[key] != '') {
517
+ allEmpty = false;
518
+ break;
519
+ }
520
+ }
521
+ if (allEmpty) return null;
522
+
523
+ return component.ctx.output;
524
+ }
525
+ }
526
+ public getRuntimeData(componentId) {
527
+ const componentData = this.getComponentData(componentId);
528
+ if (!componentData) return {};
529
+ const rData = componentData.runtimeData || {};
530
+
531
+ return rData;
532
+ }
533
+ public updateRuntimeData(componentId, data) {
534
+ const componentData = this.getComponentData(componentId);
535
+ if (!componentData) return;
536
+ componentData.runtimeData = { ...componentData.runtimeData, ...data };
537
+
538
+ this.sync();
539
+ }
540
+
541
+ public saveRuntimeComponentData(componentId, data) {
542
+ this.updateComponent(componentId, { runtimeData: data });
543
+ }
544
+
545
+ public incStep() {
546
+ this.agentContext.incStep();
547
+ }
548
+ public updateComponent(componentId: string, data: any) {
549
+ this.agentContext.updateComponent(componentId, data);
550
+ }
551
+
552
+ public resetComponent(componentId: string) {
553
+ this.agentContext.resetComponent(componentId);
554
+ }
555
+
556
+ public getComponentData(componentId: string) {
557
+ return this.agentContext.getComponentData(componentId);
558
+ }
559
+ }
@@ -0,0 +1,101 @@
1
+ import { Agent } from './Agent.class';
2
+
3
+ export class AgentSSE {
4
+ private connections: Map<string, any> = new Map();
5
+
6
+ /**
7
+ * Creates a new AgentSSE instance
8
+ * @param agent - The agent instance this SSE belongs to
9
+ */
10
+ constructor(private agent: Agent) {}
11
+
12
+ /**
13
+ * Updates or adds a response object with the given monitor ID
14
+ * Maintains backward compatibility by also setting the res property
15
+ * @param res - The response object to add or update
16
+ * @param monitorId - The monitor ID associated with this connection
17
+ * @returns The connection ID that was created or updated
18
+ */
19
+ public add(res: any, monitorId: string) {
20
+ // Store the connection
21
+ this.connections.set(monitorId, res);
22
+ }
23
+
24
+ public [Symbol.iterator](): IterableIterator<[string, any]> {
25
+ return this.connections.entries();
26
+ }
27
+
28
+ /**
29
+ * Sends an event to all connected clients
30
+ * @param _type - The event type
31
+ * @param _data - The event data
32
+ */
33
+ public async send(_type: string, _data: any): Promise<void> {
34
+ if (!_type || !_data) return;
35
+
36
+ // Format the data once
37
+ const data = typeof _data === 'string' ? _data : JSON.stringify(_data);
38
+ const message = `event: ${_type}\ndata: ${data}\n\n`;
39
+
40
+ // Send to all connections
41
+ for (const [id, connection] of this.connections.entries()) {
42
+ try {
43
+ if (connection && !connection.finished) {
44
+ connection.write(message);
45
+ } else {
46
+ // Remove closed connections
47
+ this.connections.delete(id);
48
+ }
49
+ } catch (error) {
50
+ // Handle errors (connection might be closed)
51
+ console.error(`Error sending SSE to connection ${id}:`, error);
52
+ this.connections.delete(id);
53
+ }
54
+ }
55
+ }
56
+
57
+ /**
58
+ * Removes a specific connection
59
+ * @param connectionId - The ID of the connection to remove
60
+ * @returns True if the connection was found and removed, false otherwise
61
+ */
62
+ public remove(connectionId: string): boolean {
63
+ const connection = this.connections.get(connectionId);
64
+ if (connection) {
65
+ try {
66
+ if (connection && !connection.finished) {
67
+ connection.end();
68
+ }
69
+ } catch (error) {
70
+ console.error(`Error closing connection ${connectionId}:`, error);
71
+ }
72
+ return this.connections.delete(connectionId);
73
+ }
74
+ return false;
75
+ }
76
+
77
+ /**
78
+ * Closes all connections
79
+ */
80
+ public async close(): Promise<void> {
81
+ for (const [id, connection] of this.connections.entries()) {
82
+ try {
83
+ if (connection && !connection.finished) {
84
+ connection.end();
85
+ console.log('Delibertly shutting down sse connection with id: ', id);
86
+ }
87
+ } catch (error) {
88
+ console.error(`Error closing connection ${id}:`, error);
89
+ }
90
+ }
91
+ this.connections.clear();
92
+ }
93
+
94
+ /**
95
+ * Gets the number of active connections
96
+ * @returns The number of active connections
97
+ */
98
+ public getConnectionCount(): number {
99
+ return this.connections.size;
100
+ }
101
+ }