@smythos/sre 1.5.0 → 1.5.2

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 (189) hide show
  1. package/CHANGELOG +62 -0
  2. package/LICENSE +18 -0
  3. package/package.json +127 -115
  4. package/src/Components/APICall/APICall.class.ts +155 -0
  5. package/src/Components/APICall/AccessTokenManager.ts +130 -0
  6. package/src/Components/APICall/ArrayBufferResponse.helper.ts +58 -0
  7. package/src/Components/APICall/OAuth.helper.ts +294 -0
  8. package/src/Components/APICall/mimeTypeCategories.ts +46 -0
  9. package/src/Components/APICall/parseData.ts +167 -0
  10. package/src/Components/APICall/parseHeaders.ts +41 -0
  11. package/src/Components/APICall/parseProxy.ts +68 -0
  12. package/src/Components/APICall/parseUrl.ts +91 -0
  13. package/src/Components/APIEndpoint.class.ts +234 -0
  14. package/src/Components/APIOutput.class.ts +58 -0
  15. package/src/Components/AgentPlugin.class.ts +102 -0
  16. package/src/Components/Async.class.ts +155 -0
  17. package/src/Components/Await.class.ts +90 -0
  18. package/src/Components/Classifier.class.ts +158 -0
  19. package/src/Components/Component.class.ts +94 -0
  20. package/src/Components/ComponentHost.class.ts +38 -0
  21. package/src/Components/DataSourceCleaner.class.ts +92 -0
  22. package/src/Components/DataSourceIndexer.class.ts +181 -0
  23. package/src/Components/DataSourceLookup.class.ts +141 -0
  24. package/src/Components/FEncDec.class.ts +29 -0
  25. package/src/Components/FHash.class.ts +33 -0
  26. package/src/Components/FSign.class.ts +80 -0
  27. package/src/Components/FSleep.class.ts +25 -0
  28. package/src/Components/FTimestamp.class.ts +25 -0
  29. package/src/Components/FileStore.class.ts +75 -0
  30. package/src/Components/ForEach.class.ts +97 -0
  31. package/src/Components/GPTPlugin.class.ts +70 -0
  32. package/src/Components/GenAILLM.class.ts +395 -0
  33. package/src/Components/HuggingFace.class.ts +314 -0
  34. package/src/Components/Image/imageSettings.config.ts +70 -0
  35. package/src/Components/ImageGenerator.class.ts +407 -0
  36. package/src/Components/JSONFilter.class.ts +54 -0
  37. package/src/Components/LLMAssistant.class.ts +213 -0
  38. package/src/Components/LogicAND.class.ts +28 -0
  39. package/src/Components/LogicAtLeast.class.ts +85 -0
  40. package/src/Components/LogicAtMost.class.ts +86 -0
  41. package/src/Components/LogicOR.class.ts +29 -0
  42. package/src/Components/LogicXOR.class.ts +34 -0
  43. package/src/Components/MCPClient.class.ts +112 -0
  44. package/src/Components/PromptGenerator.class.ts +122 -0
  45. package/src/Components/ScrapflyWebScrape.class.ts +159 -0
  46. package/src/Components/TavilyWebSearch.class.ts +98 -0
  47. package/src/Components/index.ts +77 -0
  48. package/src/Core/AgentProcess.helper.ts +240 -0
  49. package/src/Core/Connector.class.ts +123 -0
  50. package/src/Core/ConnectorsService.ts +192 -0
  51. package/src/Core/DummyConnector.ts +49 -0
  52. package/src/Core/HookService.ts +105 -0
  53. package/src/Core/SmythRuntime.class.ts +292 -0
  54. package/src/Core/SystemEvents.ts +15 -0
  55. package/src/Core/boot.ts +55 -0
  56. package/src/config.ts +15 -0
  57. package/src/constants.ts +125 -0
  58. package/src/data/hugging-face.params.json +580 -0
  59. package/src/helpers/BinaryInput.helper.ts +324 -0
  60. package/src/helpers/Conversation.helper.ts +1094 -0
  61. package/src/helpers/JsonContent.helper.ts +97 -0
  62. package/src/helpers/LocalCache.helper.ts +97 -0
  63. package/src/helpers/Log.helper.ts +234 -0
  64. package/src/helpers/OpenApiParser.helper.ts +150 -0
  65. package/src/helpers/S3Cache.helper.ts +129 -0
  66. package/src/helpers/SmythURI.helper.ts +5 -0
  67. package/src/helpers/TemplateString.helper.ts +243 -0
  68. package/src/helpers/TypeChecker.helper.ts +329 -0
  69. package/src/index.ts +179 -0
  70. package/src/index.ts.bak +179 -0
  71. package/src/subsystems/AgentManager/Agent.class.ts +1108 -0
  72. package/src/subsystems/AgentManager/Agent.helper.ts +3 -0
  73. package/src/subsystems/AgentManager/AgentData.service/AgentDataConnector.ts +230 -0
  74. package/src/subsystems/AgentManager/AgentData.service/connectors/CLIAgentDataConnector.class.ts +66 -0
  75. package/src/subsystems/AgentManager/AgentData.service/connectors/LocalAgentDataConnector.class.ts +142 -0
  76. package/src/subsystems/AgentManager/AgentData.service/connectors/NullAgentData.class.ts +39 -0
  77. package/src/subsystems/AgentManager/AgentData.service/index.ts +18 -0
  78. package/src/subsystems/AgentManager/AgentLogger.class.ts +297 -0
  79. package/src/subsystems/AgentManager/AgentRequest.class.ts +51 -0
  80. package/src/subsystems/AgentManager/AgentRuntime.class.ts +559 -0
  81. package/src/subsystems/AgentManager/AgentSSE.class.ts +101 -0
  82. package/src/subsystems/AgentManager/AgentSettings.class.ts +52 -0
  83. package/src/subsystems/AgentManager/Component.service/ComponentConnector.ts +32 -0
  84. package/src/subsystems/AgentManager/Component.service/connectors/LocalComponentConnector.class.ts +59 -0
  85. package/src/subsystems/AgentManager/Component.service/index.ts +11 -0
  86. package/src/subsystems/AgentManager/EmbodimentSettings.class.ts +47 -0
  87. package/src/subsystems/AgentManager/ForkedAgent.class.ts +153 -0
  88. package/src/subsystems/AgentManager/OSResourceMonitor.ts +77 -0
  89. package/src/subsystems/ComputeManager/Code.service/CodeConnector.ts +99 -0
  90. package/src/subsystems/ComputeManager/Code.service/connectors/AWSLambdaCode.class.ts +63 -0
  91. package/src/subsystems/ComputeManager/Code.service/index.ts +11 -0
  92. package/src/subsystems/IO/CLI.service/CLIConnector.ts +47 -0
  93. package/src/subsystems/IO/CLI.service/index.ts +9 -0
  94. package/src/subsystems/IO/Log.service/LogConnector.ts +32 -0
  95. package/src/subsystems/IO/Log.service/connectors/ConsoleLog.class.ts +28 -0
  96. package/src/subsystems/IO/Log.service/index.ts +13 -0
  97. package/src/subsystems/IO/NKV.service/NKVConnector.ts +41 -0
  98. package/src/subsystems/IO/NKV.service/connectors/NKVRAM.class.ts +204 -0
  99. package/src/subsystems/IO/NKV.service/connectors/NKVRedis.class.ts +182 -0
  100. package/src/subsystems/IO/NKV.service/index.ts +12 -0
  101. package/src/subsystems/IO/Router.service/RouterConnector.ts +21 -0
  102. package/src/subsystems/IO/Router.service/connectors/ExpressRouter.class.ts +48 -0
  103. package/src/subsystems/IO/Router.service/connectors/NullRouter.class.ts +40 -0
  104. package/src/subsystems/IO/Router.service/index.ts +11 -0
  105. package/src/subsystems/IO/Storage.service/SmythFS.class.ts +472 -0
  106. package/src/subsystems/IO/Storage.service/StorageConnector.ts +66 -0
  107. package/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts +305 -0
  108. package/src/subsystems/IO/Storage.service/connectors/S3Storage.class.ts +418 -0
  109. package/src/subsystems/IO/Storage.service/index.ts +13 -0
  110. package/src/subsystems/IO/VectorDB.service/VectorDBConnector.ts +108 -0
  111. package/src/subsystems/IO/VectorDB.service/connectors/MilvusVectorDB.class.ts +450 -0
  112. package/src/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.ts +373 -0
  113. package/src/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.ts +420 -0
  114. package/src/subsystems/IO/VectorDB.service/embed/BaseEmbedding.ts +106 -0
  115. package/src/subsystems/IO/VectorDB.service/embed/OpenAIEmbedding.ts +109 -0
  116. package/src/subsystems/IO/VectorDB.service/embed/index.ts +21 -0
  117. package/src/subsystems/IO/VectorDB.service/index.ts +14 -0
  118. package/src/subsystems/LLMManager/LLM.helper.ts +221 -0
  119. package/src/subsystems/LLMManager/LLM.inference.ts +335 -0
  120. package/src/subsystems/LLMManager/LLM.service/LLMConnector.ts +374 -0
  121. package/src/subsystems/LLMManager/LLM.service/LLMCredentials.helper.ts +145 -0
  122. package/src/subsystems/LLMManager/LLM.service/connectors/Anthropic.class.ts +632 -0
  123. package/src/subsystems/LLMManager/LLM.service/connectors/Bedrock.class.ts +405 -0
  124. package/src/subsystems/LLMManager/LLM.service/connectors/Echo.class.ts +81 -0
  125. package/src/subsystems/LLMManager/LLM.service/connectors/GoogleAI.class.ts +689 -0
  126. package/src/subsystems/LLMManager/LLM.service/connectors/Groq.class.ts +257 -0
  127. package/src/subsystems/LLMManager/LLM.service/connectors/OpenAI.class.ts +848 -0
  128. package/src/subsystems/LLMManager/LLM.service/connectors/Perplexity.class.ts +255 -0
  129. package/src/subsystems/LLMManager/LLM.service/connectors/VertexAI.class.ts +193 -0
  130. package/src/subsystems/LLMManager/LLM.service/index.ts +43 -0
  131. package/src/subsystems/LLMManager/ModelsProvider.service/ModelsProviderConnector.ts +281 -0
  132. package/src/subsystems/LLMManager/ModelsProvider.service/connectors/SmythModelsProvider.class.ts +229 -0
  133. package/src/subsystems/LLMManager/ModelsProvider.service/index.ts +11 -0
  134. package/src/subsystems/LLMManager/custom-models.ts +854 -0
  135. package/src/subsystems/LLMManager/models.ts +2539 -0
  136. package/src/subsystems/LLMManager/paramMappings.ts +69 -0
  137. package/src/subsystems/MemoryManager/Cache.service/CacheConnector.ts +86 -0
  138. package/src/subsystems/MemoryManager/Cache.service/connectors/LocalStorageCache.class.ts +297 -0
  139. package/src/subsystems/MemoryManager/Cache.service/connectors/RAMCache.class.ts +201 -0
  140. package/src/subsystems/MemoryManager/Cache.service/connectors/RedisCache.class.ts +252 -0
  141. package/src/subsystems/MemoryManager/Cache.service/connectors/S3Cache.class.ts +373 -0
  142. package/src/subsystems/MemoryManager/Cache.service/index.ts +15 -0
  143. package/src/subsystems/MemoryManager/LLMCache.ts +72 -0
  144. package/src/subsystems/MemoryManager/LLMContext.ts +125 -0
  145. package/src/subsystems/MemoryManager/RuntimeContext.ts +249 -0
  146. package/src/subsystems/Security/AccessControl/ACL.class.ts +208 -0
  147. package/src/subsystems/Security/AccessControl/AccessCandidate.class.ts +76 -0
  148. package/src/subsystems/Security/AccessControl/AccessRequest.class.ts +52 -0
  149. package/src/subsystems/Security/Account.service/AccountConnector.ts +41 -0
  150. package/src/subsystems/Security/Account.service/connectors/AWSAccount.class.ts +76 -0
  151. package/src/subsystems/Security/Account.service/connectors/DummyAccount.class.ts +130 -0
  152. package/src/subsystems/Security/Account.service/connectors/JSONFileAccount.class.ts +159 -0
  153. package/src/subsystems/Security/Account.service/index.ts +14 -0
  154. package/src/subsystems/Security/Credentials.helper.ts +62 -0
  155. package/src/subsystems/Security/ManagedVault.service/ManagedVaultConnector.ts +34 -0
  156. package/src/subsystems/Security/ManagedVault.service/connectors/NullManagedVault.class.ts +57 -0
  157. package/src/subsystems/Security/ManagedVault.service/connectors/SecretManagerManagedVault.ts +154 -0
  158. package/src/subsystems/Security/ManagedVault.service/index.ts +12 -0
  159. package/src/subsystems/Security/SecureConnector.class.ts +110 -0
  160. package/src/subsystems/Security/Vault.service/Vault.helper.ts +30 -0
  161. package/src/subsystems/Security/Vault.service/VaultConnector.ts +26 -0
  162. package/src/subsystems/Security/Vault.service/connectors/HashicorpVault.class.ts +46 -0
  163. package/src/subsystems/Security/Vault.service/connectors/JSONFileVault.class.ts +166 -0
  164. package/src/subsystems/Security/Vault.service/connectors/NullVault.class.ts +54 -0
  165. package/src/subsystems/Security/Vault.service/connectors/SecretsManager.class.ts +140 -0
  166. package/src/subsystems/Security/Vault.service/index.ts +12 -0
  167. package/src/types/ACL.types.ts +104 -0
  168. package/src/types/AWS.types.ts +9 -0
  169. package/src/types/Agent.types.ts +61 -0
  170. package/src/types/AgentLogger.types.ts +17 -0
  171. package/src/types/Cache.types.ts +1 -0
  172. package/src/types/Common.types.ts +3 -0
  173. package/src/types/LLM.types.ts +419 -0
  174. package/src/types/Redis.types.ts +8 -0
  175. package/src/types/SRE.types.ts +64 -0
  176. package/src/types/Security.types.ts +18 -0
  177. package/src/types/Storage.types.ts +5 -0
  178. package/src/types/VectorDB.types.ts +78 -0
  179. package/src/utils/base64.utils.ts +275 -0
  180. package/src/utils/cli.utils.ts +68 -0
  181. package/src/utils/data.utils.ts +263 -0
  182. package/src/utils/date-time.utils.ts +22 -0
  183. package/src/utils/general.utils.ts +238 -0
  184. package/src/utils/index.ts +12 -0
  185. package/src/utils/numbers.utils.ts +13 -0
  186. package/src/utils/oauth.utils.ts +35 -0
  187. package/src/utils/string.utils.ts +414 -0
  188. package/src/utils/url.utils.ts +19 -0
  189. package/src/utils/validation.utils.ts +74 -0
@@ -0,0 +1,98 @@
1
+ import { IAgent as Agent } from '@sre/types/Agent.types';
2
+ import { Component } from './Component.class';
3
+ import Joi from 'joi';
4
+ import SREConfig from '@sre/config';
5
+ import axios from 'axios';
6
+ import { SystemEvents } from '@sre/Core/SystemEvents';
7
+ import { getCredentials } from '../subsystems/Security/Credentials.helper';
8
+ import { AccessCandidate } from '../subsystems/Security/AccessControl/AccessCandidate.class';
9
+
10
+ export class TavilyWebSearch extends Component {
11
+ protected schema = {
12
+ name: 'TavilyWebSearch',
13
+ description: 'Use this component to generate a responses from an LLM',
14
+
15
+ inputs: {
16
+ SearchQuery: {
17
+ type: 'Text',
18
+ description: 'The search query to get the web search results of',
19
+ default: true,
20
+ },
21
+ },
22
+ outputs: {
23
+ Results: {
24
+ type: 'Array',
25
+ description: 'The web search results',
26
+ default: true,
27
+ },
28
+ },
29
+ };
30
+
31
+ protected configSchema = Joi.object({
32
+ includeImages: Joi.boolean().default(false).label('Include Image Results'),
33
+ sourcesLimit: Joi.number().integer().default(10).label('Sources Limit'),
34
+ searchTopic: Joi.string().valid('general', 'news').label('Search Topic'),
35
+ includeQAs: Joi.boolean().default(false).label('Include QAs'),
36
+ timeRange: Joi.string().valid('None', 'day', 'week', 'month', 'year').label('Time Range'),
37
+ includeRawContent: Joi.boolean().default(false).label('Include Raw Content'),
38
+ excludeDomains: Joi.string().allow('').label('Exclude Domains'),
39
+ });
40
+ constructor() {
41
+ super();
42
+ }
43
+ init() {}
44
+ async process(input, config, agent: Agent) {
45
+ await super.process(input, config, agent);
46
+
47
+ const logger = this.createComponentLogger(agent, config);
48
+ try {
49
+ logger.debug(`=== Web Scrape Log ===`);
50
+ let Output: any = {};
51
+ let _error = undefined;
52
+ let searchQuery = input['SearchQuery'];
53
+
54
+ const teamId = agent.teamId;
55
+ const api_key = await getCredentials(AccessCandidate.team(teamId), 'tavily');
56
+
57
+ logger.debug('Payload:', JSON.stringify(config.data));
58
+ const response = await axios({
59
+ method: 'post',
60
+ url: 'https://api.tavily.com/search',
61
+ data: {
62
+ api_key,
63
+ query: searchQuery,
64
+ topic: config.data.searchTopic,
65
+ exclude_domains: config.data.excludeDomains?.length ? config.data.excludeDomains.split(',') : [],
66
+ max_results: config.data.sourcesLimit,
67
+ ...(config.data.timeRange !== 'None' ? { time_range: config.data.timeRange } : {}),
68
+ ...(config.data.includeImages ? { include_images: true } : {}),
69
+ ...(config.data.includeQAs ? { include_answer: true } : {}),
70
+ ...(config.data.includeRawContent ? { include_raw_content: true } : {}),
71
+ },
72
+ });
73
+ Output = {
74
+ Results: response.data.results,
75
+ ...(config.data.includeImages ? { Images: response.data.images } : {}),
76
+ ...(config.data.includeQAs ? { Answer: response.data.answer } : {}),
77
+ };
78
+ this.reportUsage({
79
+ agentId: agent.id,
80
+ teamId: agent.teamId,
81
+ });
82
+ return { ...Output, _error, _debug: logger.output };
83
+ } catch (err: any) {
84
+ const _error = err?.message || err?.response?.data || err.toString();
85
+ logger.error(` Error scraping web \n${JSON.stringify(_error)}\n`);
86
+ return { Output: undefined, _error, _debug: logger.output };
87
+ }
88
+ }
89
+
90
+ protected reportUsage({ agentId, teamId }: { agentId: string; teamId: string }) {
91
+ SystemEvents.emit('USAGE:API', {
92
+ sourceId: 'api:websearch.smyth',
93
+ credits: 1,
94
+ agentId,
95
+ teamId,
96
+ });
97
+ }
98
+ }
@@ -0,0 +1,77 @@
1
+ import { Component } from './Component.class';
2
+ import { APIEndpoint } from './APIEndpoint.class';
3
+ import { APIOutput } from './APIOutput.class';
4
+ import { PromptGenerator } from './PromptGenerator.class';
5
+ import { APICall } from './APICall/APICall.class';
6
+ import { FSleep } from './FSleep.class';
7
+ import { FHash } from './FHash.class';
8
+ import { FEncDec } from './FEncDec.class';
9
+ import { FTimestamp } from './FTimestamp.class';
10
+ import { DataSourceLookup } from './DataSourceLookup.class';
11
+ import { DataSourceIndexer } from './DataSourceIndexer.class';
12
+ import { DataSourceCleaner } from './DataSourceCleaner.class';
13
+ import { JSONFilter } from './JSONFilter.class';
14
+ import { LogicAND } from './LogicAND.class';
15
+ import { LogicOR } from './LogicOR.class';
16
+ import { LogicXOR } from './LogicXOR.class';
17
+ import { LogicAtLeast } from './LogicAtLeast.class';
18
+ import { LogicAtMost } from './LogicAtMost.class';
19
+ import { AgentPlugin } from './AgentPlugin.class';
20
+ import { LLMAssistant } from './LLMAssistant.class';
21
+ import { Async } from './Async.class';
22
+ import { Await } from './Await.class';
23
+ import { ForEach } from './ForEach.class';
24
+ import { HuggingFace } from './HuggingFace.class';
25
+ import { GPTPlugin } from './GPTPlugin.class';
26
+ import { Classifier } from './Classifier.class';
27
+ import { FSign } from './FSign.class';
28
+ import { GenAILLM } from './GenAILLM.class';
29
+ import { FileStore } from './FileStore.class';
30
+ import { ScrapflyWebScrape } from './ScrapflyWebScrape.class';
31
+ import { TavilyWebSearch } from './TavilyWebSearch.class';
32
+ import { ComponentHost } from './ComponentHost.class';
33
+ import { ImageGenerator } from './ImageGenerator.class'; // Legacy
34
+ import { MCPClient } from './MCPClient.class';
35
+
36
+ const components = {
37
+ Component: new Component(),
38
+ Note: new Component(), //this is a fake component
39
+ APIEndpoint: new APIEndpoint(),
40
+ APIOutput: new APIOutput(),
41
+ PromptGenerator: new PromptGenerator(),
42
+ LLMPrompt: new PromptGenerator(),
43
+ APICall: new APICall(),
44
+ FSleep: new FSleep(),
45
+ FHash: new FHash(),
46
+ FEncDec: new FEncDec(),
47
+ FSign: new FSign(),
48
+ FTimestamp: new FTimestamp(),
49
+ DataSourceLookup: new DataSourceLookup(),
50
+ DataSourceIndexer: new DataSourceIndexer(),
51
+ DataSourceCleaner: new DataSourceCleaner(),
52
+ JSONFilter: new JSONFilter(),
53
+ LogicAND: new LogicAND(),
54
+ LogicOR: new LogicOR(),
55
+ LogicXOR: new LogicXOR(),
56
+ LogicAtLeast: new LogicAtLeast(),
57
+ LogicAtMost: new LogicAtMost(),
58
+ AgentPlugin: new AgentPlugin(),
59
+ LLMAssistant: new LLMAssistant(),
60
+ Async: new Async(),
61
+ Await: new Await(),
62
+ ForEach: new ForEach(),
63
+ HuggingFace: new HuggingFace(),
64
+ GPTPlugin: new GPTPlugin(),
65
+ Classifier: new Classifier(),
66
+ GenAILLM: new GenAILLM(),
67
+ FileStore: new FileStore(),
68
+ WebSearch: new TavilyWebSearch(),
69
+ WebScrape: new ScrapflyWebScrape(),
70
+ TavilyWebSearch: new TavilyWebSearch(),
71
+ ScrapflyWebScrape: new ScrapflyWebScrape(),
72
+ ComponentHost: new ComponentHost(),
73
+ ImageGenerator: new ImageGenerator(),
74
+ MCPClient: new MCPClient(),
75
+ };
76
+
77
+ export const ComponentInstances = components;
@@ -0,0 +1,240 @@
1
+ //import { SmythRuntime } from '@sre/Core/SmythRuntime.class';
2
+ import { Agent } from '@sre/AgentManager/Agent.class';
3
+ import { AgentRequest } from '@sre/AgentManager/AgentRequest.class';
4
+ import { AgentSettings } from '@sre/AgentManager/AgentSettings.class';
5
+ import { TAgentProcessParams } from '@sre/types/Agent.types';
6
+ import { uid } from '../utils';
7
+
8
+ import { CLIConnector } from '@sre/IO/CLI.service/CLIConnector';
9
+ import * as FileType from 'file-type';
10
+ import fs from 'fs';
11
+ import mime from 'mime';
12
+ import path from 'path';
13
+ import { ConnectorService } from './ConnectorsService';
14
+
15
+ export class AgentProcess {
16
+ public agent: Agent;
17
+
18
+ private _loadPromise: Promise<any>;
19
+
20
+ private constructor(
21
+ private agentData: any,
22
+ private agentVersion?: string,
23
+ ) {
24
+ this.initAgent(agentData, agentVersion);
25
+ }
26
+ private async initAgent(agentData: any, agentVersion?: string) {
27
+ //await SmythRuntime.Instance.ready();
28
+ let data;
29
+ let agentId;
30
+
31
+ if (typeof agentData === 'object') {
32
+ data = agentData;
33
+ if (data.components && data.connections) {
34
+ data = { data, version: '1.0' };
35
+ }
36
+
37
+ agentId = data.data.id || 'tmp-' + uid();
38
+ } else {
39
+ const jsonRegex = /^{.*}$/g;
40
+ const jsonData = agentData.match(jsonRegex)?.[0];
41
+
42
+ const idRegex = /^[a-zA-Z0-9\-]+$/g;
43
+ agentId = agentData.match(idRegex)?.[0];
44
+
45
+ //We are loading from an agentId
46
+ if (agentId) {
47
+ const agentDataConnector = ConnectorService.getAgentDataConnector();
48
+
49
+ data = await agentDataConnector.getAgentData(agentId, agentVersion);
50
+ }
51
+
52
+ //we are loading an agent from provided data
53
+ if (!data && jsonData) {
54
+ data = JSON.parse(jsonData);
55
+ //generate an agentId if not provided
56
+ agentId = data.id || 'tmp-' + uid();
57
+
58
+ if (data.components && data.connections) {
59
+ data = { data, version: '1.0' };
60
+ }
61
+ }
62
+ }
63
+
64
+ const agentSettings = new AgentSettings(agentId);
65
+ this.agent = new Agent(agentId, data, agentSettings);
66
+ }
67
+
68
+ public async ready() {
69
+ if (this._loadPromise) {
70
+ return this._loadPromise;
71
+ }
72
+
73
+ return (this._loadPromise = new Promise((resolve) => {
74
+ let maxWait = 10000;
75
+ //wait for agent to be set
76
+ const itv = setInterval(() => {
77
+ if (this.agent) {
78
+ clearInterval(itv);
79
+ resolve(true);
80
+ }
81
+ maxWait -= 100;
82
+ if (maxWait <= 0) {
83
+ clearInterval(itv);
84
+ resolve(false);
85
+ }
86
+ }, 100);
87
+ }));
88
+ }
89
+
90
+ public static load(agentData: any, agentVersion?: string) {
91
+ const agentProcess = new AgentProcess(agentData, agentVersion);
92
+ return agentProcess;
93
+ }
94
+
95
+ /**
96
+ * Run the agent process
97
+ * @param reqConfig - The request configuration
98
+ * @param callback - The callback function is used if we want to send status data, meta information, or stream response progressively.
99
+ * Note: even if the response is streamed through the callback, the response is still returned as a single object in the response.data field.
100
+ * @returns The result of the agent process
101
+ */
102
+ public async run(
103
+ reqConfig: TAgentProcessParams | Array<string> | AgentRequest,
104
+ callback?: (data: any) => void,
105
+ ): Promise<{
106
+ status?: number;
107
+ data: any;
108
+ }> {
109
+ await this.ready();
110
+ if (!this.agent) throw new Error('Failed to load agent');
111
+ let request: AgentRequest = this.parseReqConfig(reqConfig);
112
+
113
+ this.agent.setRequest(request);
114
+
115
+ if (typeof callback === 'function') {
116
+ this.agent.setCallback(callback);
117
+ }
118
+
119
+ const pathMatches = request.path.match(/(^\/v[0-9]+\.[0-9]+?)?(\/api\/(.+)?)/);
120
+ if (!pathMatches || !pathMatches[2]) {
121
+ return { status: 404, data: { error: 'Endpoint not found' } };
122
+ }
123
+ const endpointPath = pathMatches[2];
124
+ const input = request.method == 'GET' ? request.query : request.body;
125
+ const result: any = await this.agent.process(endpointPath, input).catch((error) => ({ error: error.message }));
126
+
127
+ return { data: result };
128
+ }
129
+
130
+ public reset() {
131
+ //the current version of agent cannot be used to run multiple requests
132
+ //as a workaround we provide this function to reset AgentProcess state by generating a new Agent
133
+ //TODO: refactor Agent.class in order to allow multiple consecutive requests running
134
+ this.initAgent(this.agentData);
135
+ }
136
+
137
+ private parseReqConfig(reqConfig: TAgentProcessParams | Array<string> | AgentRequest): AgentRequest {
138
+ if (reqConfig instanceof AgentRequest) return reqConfig;
139
+ if (Array.isArray(reqConfig)) return this.parseCLI(reqConfig);
140
+ return new AgentRequest(reqConfig);
141
+ }
142
+
143
+ private parseCLI(argList: Array<string>): AgentRequest {
144
+ const cliConnector: CLIConnector = ConnectorService.getCLIConnector();
145
+ const methods = ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'];
146
+ const cli: any = cliConnector.parse(argList, ['endpoint', 'post', 'get', 'put', 'delete', 'patch', 'head', 'options', 'headers', 'session']);
147
+
148
+ const usedMethod = methods.find((method) => cli[method]);
149
+
150
+ const req: AgentRequest = new AgentRequest();
151
+
152
+ req.method = usedMethod?.toUpperCase() || 'GET';
153
+ req.body = {};
154
+ req.query = {};
155
+
156
+ switch (usedMethod) {
157
+ case 'get':
158
+ case 'delete':
159
+ case 'head':
160
+ case 'options':
161
+ req.query = cli[usedMethod];
162
+ break;
163
+ case 'post':
164
+ case 'put':
165
+ case 'patch':
166
+ req.body = cli[usedMethod];
167
+ break;
168
+ }
169
+
170
+ req.path = `/api/${cli.endpoint}`;
171
+ req.params = cli.endpoint?.split('/');
172
+
173
+ req.headers = cli.headers || {};
174
+ //convert all keys to lowercase
175
+ for (let key in req.headers) {
176
+ req.headers[key.toLowerCase()] = req.headers[key];
177
+ delete req.headers[key];
178
+ }
179
+
180
+ req.sessionID = cli.session || uid();
181
+
182
+ req.files = [];
183
+ if (req.body) {
184
+ for (let entry in req.body) {
185
+ let value = req.body[entry];
186
+ const filePath = path.join(process.cwd(), value);
187
+ const fileName = path.basename(filePath);
188
+ if (!fs.existsSync(filePath)) continue;
189
+
190
+ //read the file and create a file object
191
+
192
+ try {
193
+ // Read the file content
194
+ const fileBuffer = fs.readFileSync(filePath);
195
+ const ext: any = fileName.split('.').pop();
196
+
197
+ const fileObj = {
198
+ fieldname: entry,
199
+ originalname: fileName,
200
+ buffer: fileBuffer,
201
+ mimetype: mime.getType(ext) || 'application/octet-stream',
202
+ };
203
+
204
+ delete req.body[entry];
205
+ req.files.push(fileObj);
206
+
207
+ // Try to determine the MIME type from the file content
208
+ FileType.fileTypeFromBuffer(fileBuffer).then((fileType) => {
209
+ if (fileType) {
210
+ fileObj.mimetype = fileType.mime;
211
+ }
212
+ });
213
+ } catch (error) {
214
+ console.warn('Coud not read file', filePath, error.message);
215
+ }
216
+ }
217
+ }
218
+
219
+ return req;
220
+ }
221
+
222
+ public async post(path: string, input?: any, headers?: any) {
223
+ return this.run({ method: 'POST', path, body: input || {}, headers });
224
+ }
225
+
226
+ public async get(path: string, query?: any, headers?: any) {
227
+ return this.run({ method: 'GET', path, query, headers });
228
+ }
229
+
230
+ public async readDebugState(stateId: string, reqConfig: TAgentProcessParams | Array<string> | AgentRequest) {
231
+ await this.ready();
232
+ if (!this.agent) throw new Error('Failed to load agent');
233
+ let request: AgentRequest = this.parseReqConfig(reqConfig);
234
+
235
+ this.agent.setRequest(request);
236
+ await this.agent.agentRuntime.ready();
237
+
238
+ return this.agent.agentRuntime.readState(stateId, true);
239
+ }
240
+ }
@@ -0,0 +1,123 @@
1
+ import { LocalCache } from '@sre/helpers/LocalCache.helper';
2
+ import { Logger } from '../helpers/Log.helper';
3
+ import { createHash } from 'crypto';
4
+ import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
5
+
6
+ const console = Logger('Connector');
7
+ //const lCache = new LocalCache();
8
+
9
+ export class Connector<TRequest = any> {
10
+ public name: string;
11
+ public started = false;
12
+ private _interactionHandler: () => void;
13
+ private _readyPromise: Promise<boolean>;
14
+ private static lCache = new LocalCache();
15
+
16
+ public get settings() {
17
+ return this._settings;
18
+ }
19
+
20
+ public get interactionHandler() {
21
+ return this._interactionHandler;
22
+ }
23
+
24
+ //this flag is used to check if the connector is valid
25
+ //when a connector fails to load, it is replaced by a DummyConnector instance which returns false for this flag
26
+ public get valid() {
27
+ return true;
28
+ }
29
+
30
+ constructor(protected _settings?: any) {
31
+ //TODO : check if smyth runtime is initialized and throw an error if it is not
32
+ }
33
+
34
+ /**
35
+ * If the connector is interactive, The connector initializer will wait for start() method to complete before loading the next connector
36
+ */
37
+ protected setInteraction(handler: () => void) {
38
+ this._interactionHandler = handler;
39
+ }
40
+ /**
41
+ * Creates a new instance of the current class using the provided settings.
42
+ * This method can be called on both Connector instances and its subclasses.
43
+ * This is used when we need to create a connector instance with a specific configuration (for example with user provided keys)
44
+ *
45
+ * @param settings - Configuration settings for the new instance.
46
+ * @returns A new instance of the current class.
47
+ */
48
+ public instance(settings: any): this {
49
+ const configHash = createHash('sha256')
50
+ .update(JSON.stringify(settings || {}))
51
+ .digest('hex');
52
+ const key = `${this.name}-${configHash}`;
53
+
54
+ if (Connector.lCache.has(key)) {
55
+ return Connector.lCache.get(key) as this;
56
+ }
57
+
58
+ // if not in cache, create a new instance from the concrete class
59
+ const constructor = this.constructor as { new (config: any): any };
60
+ const instance = new constructor(settings);
61
+ Connector.lCache.set(key, instance, 60 * 60 * 1000); // cache for 1 hour
62
+
63
+ return instance;
64
+ }
65
+
66
+ static isValid(connector: Connector): boolean {
67
+ return connector.name !== undefined && connector.name !== null && connector.name !== '';
68
+ }
69
+
70
+ public async start() {
71
+ console.info(`Starting ${this.name} connector ...`);
72
+ this.started = true;
73
+ }
74
+
75
+ public async stop() {
76
+ console.info(`Stopping ${this.name} connector ...`);
77
+ }
78
+
79
+ public ready() {
80
+ if (!this._readyPromise) {
81
+ this._readyPromise = new Promise((resolve) => {
82
+ let maxWait = 60000;
83
+ const tick = 100;
84
+ if (this.started) {
85
+ resolve(true);
86
+ } else {
87
+ const interval = setInterval(() => {
88
+ if (this.started) {
89
+ clearInterval(interval);
90
+ resolve(true);
91
+ }
92
+
93
+ maxWait -= tick;
94
+ if (maxWait <= 0) {
95
+ clearInterval(interval);
96
+ resolve(false);
97
+ }
98
+ }, tick);
99
+ }
100
+ });
101
+ }
102
+ return this._readyPromise;
103
+ }
104
+
105
+ public requester(candidate: AccessCandidate): TRequest {
106
+ return null as TRequest;
107
+ }
108
+
109
+ public user(candidate: AccessCandidate | string): TRequest {
110
+ if (typeof candidate === 'string') {
111
+ return this.requester(AccessCandidate.user(candidate));
112
+ }
113
+ return this.requester(candidate);
114
+ }
115
+
116
+ public team(teamId: string): TRequest {
117
+ return this.requester(AccessCandidate.team(teamId));
118
+ }
119
+
120
+ public agent(agentId: string): TRequest {
121
+ return this.requester(AccessCandidate.agent(agentId));
122
+ }
123
+ }