@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.
- package/LICENSE +18 -0
- package/dist/index.js +22329 -4
- package/dist/index.js.map +1 -1
- package/dist/types/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.d.ts +2 -2
- package/dist/types/subsystems/IO/VectorDB.service/embed/BaseEmbedding.d.ts +11 -7
- package/dist/types/types/VectorDB.types.d.ts +13 -11
- package/package.json +102 -127
- package/src/Components/APICall/APICall.class.ts +155 -0
- package/src/Components/APICall/AccessTokenManager.ts +130 -0
- package/src/Components/APICall/ArrayBufferResponse.helper.ts +58 -0
- package/src/Components/APICall/OAuth.helper.ts +294 -0
- package/src/Components/APICall/mimeTypeCategories.ts +46 -0
- package/src/Components/APICall/parseData.ts +167 -0
- package/src/Components/APICall/parseHeaders.ts +41 -0
- package/src/Components/APICall/parseProxy.ts +68 -0
- package/src/Components/APICall/parseUrl.ts +91 -0
- package/src/Components/APIEndpoint.class.ts +234 -0
- package/src/Components/APIOutput.class.ts +58 -0
- package/src/Components/AgentPlugin.class.ts +102 -0
- package/src/Components/Async.class.ts +155 -0
- package/src/Components/Await.class.ts +90 -0
- package/src/Components/Classifier.class.ts +158 -0
- package/src/Components/Component.class.ts +94 -0
- package/src/Components/ComponentHost.class.ts +38 -0
- package/src/Components/DataSourceCleaner.class.ts +92 -0
- package/src/Components/DataSourceIndexer.class.ts +181 -0
- package/src/Components/DataSourceLookup.class.ts +141 -0
- package/src/Components/FEncDec.class.ts +29 -0
- package/src/Components/FHash.class.ts +33 -0
- package/src/Components/FSign.class.ts +80 -0
- package/src/Components/FSleep.class.ts +25 -0
- package/src/Components/FTimestamp.class.ts +25 -0
- package/src/Components/FileStore.class.ts +75 -0
- package/src/Components/ForEach.class.ts +97 -0
- package/src/Components/GPTPlugin.class.ts +70 -0
- package/src/Components/GenAILLM.class.ts +395 -0
- package/src/Components/HuggingFace.class.ts +314 -0
- package/src/Components/Image/imageSettings.config.ts +70 -0
- package/src/Components/ImageGenerator.class.ts +407 -0
- package/src/Components/JSONFilter.class.ts +54 -0
- package/src/Components/LLMAssistant.class.ts +213 -0
- package/src/Components/LogicAND.class.ts +28 -0
- package/src/Components/LogicAtLeast.class.ts +85 -0
- package/src/Components/LogicAtMost.class.ts +86 -0
- package/src/Components/LogicOR.class.ts +29 -0
- package/src/Components/LogicXOR.class.ts +34 -0
- package/src/Components/MCPClient.class.ts +112 -0
- package/src/Components/PromptGenerator.class.ts +122 -0
- package/src/Components/ScrapflyWebScrape.class.ts +159 -0
- package/src/Components/TavilyWebSearch.class.ts +98 -0
- package/src/Components/index.ts +77 -0
- package/src/Core/AgentProcess.helper.ts +240 -0
- package/src/Core/Connector.class.ts +123 -0
- package/src/Core/ConnectorsService.ts +192 -0
- package/src/Core/DummyConnector.ts +49 -0
- package/src/Core/HookService.ts +105 -0
- package/src/Core/SmythRuntime.class.ts +292 -0
- package/src/Core/SystemEvents.ts +15 -0
- package/src/Core/boot.ts +55 -0
- package/src/config.ts +15 -0
- package/src/constants.ts +125 -0
- package/src/data/hugging-face.params.json +580 -0
- package/src/helpers/BinaryInput.helper.ts +324 -0
- package/src/helpers/Conversation.helper.ts +1094 -0
- package/src/helpers/JsonContent.helper.ts +97 -0
- package/src/helpers/LocalCache.helper.ts +97 -0
- package/src/helpers/Log.helper.ts +234 -0
- package/src/helpers/OpenApiParser.helper.ts +150 -0
- package/src/helpers/S3Cache.helper.ts +129 -0
- package/src/helpers/SmythURI.helper.ts +5 -0
- package/src/helpers/TemplateString.helper.ts +243 -0
- package/src/helpers/TypeChecker.helper.ts +329 -0
- package/src/index.ts +179 -0
- package/src/index.ts.bak +179 -0
- package/src/subsystems/AgentManager/Agent.class.ts +1108 -0
- package/src/subsystems/AgentManager/Agent.helper.ts +3 -0
- package/src/subsystems/AgentManager/AgentData.service/AgentDataConnector.ts +230 -0
- package/src/subsystems/AgentManager/AgentData.service/connectors/CLIAgentDataConnector.class.ts +66 -0
- package/src/subsystems/AgentManager/AgentData.service/connectors/LocalAgentDataConnector.class.ts +142 -0
- package/src/subsystems/AgentManager/AgentData.service/connectors/NullAgentData.class.ts +39 -0
- package/src/subsystems/AgentManager/AgentData.service/index.ts +18 -0
- package/src/subsystems/AgentManager/AgentLogger.class.ts +297 -0
- package/src/subsystems/AgentManager/AgentRequest.class.ts +51 -0
- package/src/subsystems/AgentManager/AgentRuntime.class.ts +559 -0
- package/src/subsystems/AgentManager/AgentSSE.class.ts +101 -0
- package/src/subsystems/AgentManager/AgentSettings.class.ts +52 -0
- package/src/subsystems/AgentManager/Component.service/ComponentConnector.ts +32 -0
- package/src/subsystems/AgentManager/Component.service/connectors/LocalComponentConnector.class.ts +59 -0
- package/src/subsystems/AgentManager/Component.service/index.ts +11 -0
- package/src/subsystems/AgentManager/EmbodimentSettings.class.ts +47 -0
- package/src/subsystems/AgentManager/ForkedAgent.class.ts +153 -0
- package/src/subsystems/AgentManager/OSResourceMonitor.ts +77 -0
- package/src/subsystems/ComputeManager/Code.service/CodeConnector.ts +99 -0
- package/src/subsystems/ComputeManager/Code.service/connectors/AWSLambdaCode.class.ts +63 -0
- package/src/subsystems/ComputeManager/Code.service/index.ts +11 -0
- package/src/subsystems/IO/CLI.service/CLIConnector.ts +47 -0
- package/src/subsystems/IO/CLI.service/index.ts +9 -0
- package/src/subsystems/IO/Log.service/LogConnector.ts +32 -0
- package/src/subsystems/IO/Log.service/connectors/ConsoleLog.class.ts +28 -0
- package/src/subsystems/IO/Log.service/index.ts +13 -0
- package/src/subsystems/IO/NKV.service/NKVConnector.ts +41 -0
- package/src/subsystems/IO/NKV.service/connectors/NKVRAM.class.ts +204 -0
- package/src/subsystems/IO/NKV.service/connectors/NKVRedis.class.ts +182 -0
- package/src/subsystems/IO/NKV.service/index.ts +12 -0
- package/src/subsystems/IO/Router.service/RouterConnector.ts +21 -0
- package/src/subsystems/IO/Router.service/connectors/ExpressRouter.class.ts +48 -0
- package/src/subsystems/IO/Router.service/connectors/NullRouter.class.ts +40 -0
- package/src/subsystems/IO/Router.service/index.ts +11 -0
- package/src/subsystems/IO/Storage.service/SmythFS.class.ts +472 -0
- package/src/subsystems/IO/Storage.service/StorageConnector.ts +66 -0
- package/src/subsystems/IO/Storage.service/connectors/LocalStorage.class.ts +305 -0
- package/src/subsystems/IO/Storage.service/connectors/S3Storage.class.ts +418 -0
- package/src/subsystems/IO/Storage.service/index.ts +13 -0
- package/src/subsystems/IO/VectorDB.service/VectorDBConnector.ts +108 -0
- package/src/subsystems/IO/VectorDB.service/connectors/MilvusVectorDB.class.ts +454 -0
- package/src/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.ts +384 -0
- package/src/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.ts +421 -0
- package/src/subsystems/IO/VectorDB.service/embed/BaseEmbedding.ts +107 -0
- package/src/subsystems/IO/VectorDB.service/embed/OpenAIEmbedding.ts +109 -0
- package/src/subsystems/IO/VectorDB.service/embed/index.ts +21 -0
- package/src/subsystems/IO/VectorDB.service/index.ts +14 -0
- package/src/subsystems/LLMManager/LLM.helper.ts +221 -0
- package/src/subsystems/LLMManager/LLM.inference.ts +335 -0
- package/src/subsystems/LLMManager/LLM.service/LLMConnector.ts +375 -0
- package/src/subsystems/LLMManager/LLM.service/LLMCredentials.helper.ts +145 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/Anthropic.class.ts +632 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/Bedrock.class.ts +405 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/Echo.class.ts +81 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/GoogleAI.class.ts +689 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/Groq.class.ts +257 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/OpenAI.class.ts +848 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/Perplexity.class.ts +255 -0
- package/src/subsystems/LLMManager/LLM.service/connectors/VertexAI.class.ts +193 -0
- package/src/subsystems/LLMManager/LLM.service/index.ts +43 -0
- package/src/subsystems/LLMManager/ModelsProvider.service/ModelsProviderConnector.ts +281 -0
- package/src/subsystems/LLMManager/ModelsProvider.service/connectors/SmythModelsProvider.class.ts +229 -0
- package/src/subsystems/LLMManager/ModelsProvider.service/index.ts +11 -0
- package/src/subsystems/LLMManager/custom-models.ts +854 -0
- package/src/subsystems/LLMManager/models.ts +2539 -0
- package/src/subsystems/LLMManager/paramMappings.ts +69 -0
- package/src/subsystems/MemoryManager/Cache.service/CacheConnector.ts +86 -0
- package/src/subsystems/MemoryManager/Cache.service/connectors/LocalStorageCache.class.ts +297 -0
- package/src/subsystems/MemoryManager/Cache.service/connectors/RAMCache.class.ts +201 -0
- package/src/subsystems/MemoryManager/Cache.service/connectors/RedisCache.class.ts +252 -0
- package/src/subsystems/MemoryManager/Cache.service/connectors/S3Cache.class.ts +373 -0
- package/src/subsystems/MemoryManager/Cache.service/index.ts +15 -0
- package/src/subsystems/MemoryManager/LLMCache.ts +72 -0
- package/src/subsystems/MemoryManager/LLMContext.ts +125 -0
- package/src/subsystems/MemoryManager/RuntimeContext.ts +249 -0
- package/src/subsystems/Security/AccessControl/ACL.class.ts +208 -0
- package/src/subsystems/Security/AccessControl/AccessCandidate.class.ts +76 -0
- package/src/subsystems/Security/AccessControl/AccessRequest.class.ts +52 -0
- package/src/subsystems/Security/Account.service/AccountConnector.ts +41 -0
- package/src/subsystems/Security/Account.service/connectors/AWSAccount.class.ts +76 -0
- package/src/subsystems/Security/Account.service/connectors/DummyAccount.class.ts +130 -0
- package/src/subsystems/Security/Account.service/connectors/JSONFileAccount.class.ts +159 -0
- package/src/subsystems/Security/Account.service/index.ts +14 -0
- package/src/subsystems/Security/Credentials.helper.ts +62 -0
- package/src/subsystems/Security/ManagedVault.service/ManagedVaultConnector.ts +34 -0
- package/src/subsystems/Security/ManagedVault.service/connectors/NullManagedVault.class.ts +57 -0
- package/src/subsystems/Security/ManagedVault.service/connectors/SecretManagerManagedVault.ts +154 -0
- package/src/subsystems/Security/ManagedVault.service/index.ts +12 -0
- package/src/subsystems/Security/SecureConnector.class.ts +110 -0
- package/src/subsystems/Security/Vault.service/Vault.helper.ts +30 -0
- package/src/subsystems/Security/Vault.service/VaultConnector.ts +26 -0
- package/src/subsystems/Security/Vault.service/connectors/HashicorpVault.class.ts +46 -0
- package/src/subsystems/Security/Vault.service/connectors/JSONFileVault.class.ts +166 -0
- package/src/subsystems/Security/Vault.service/connectors/NullVault.class.ts +54 -0
- package/src/subsystems/Security/Vault.service/connectors/SecretsManager.class.ts +140 -0
- package/src/subsystems/Security/Vault.service/index.ts +12 -0
- package/src/types/ACL.types.ts +104 -0
- package/src/types/AWS.types.ts +9 -0
- package/src/types/Agent.types.ts +61 -0
- package/src/types/AgentLogger.types.ts +17 -0
- package/src/types/Cache.types.ts +1 -0
- package/src/types/Common.types.ts +3 -0
- package/src/types/LLM.types.ts +419 -0
- package/src/types/Redis.types.ts +8 -0
- package/src/types/SRE.types.ts +64 -0
- package/src/types/Security.types.ts +18 -0
- package/src/types/Storage.types.ts +5 -0
- package/src/types/VectorDB.types.ts +86 -0
- package/src/utils/base64.utils.ts +275 -0
- package/src/utils/cli.utils.ts +68 -0
- package/src/utils/data.utils.ts +263 -0
- package/src/utils/date-time.utils.ts +22 -0
- package/src/utils/general.utils.ts +238 -0
- package/src/utils/index.ts +12 -0
- package/src/utils/numbers.utils.ts +13 -0
- package/src/utils/oauth.utils.ts +35 -0
- package/src/utils/string.utils.ts +414 -0
- package/src/utils/url.utils.ts +19 -0
- package/src/utils/validation.utils.ts +74 -0
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { ConnectorService } from '@sre/Core/ConnectorsService';
|
|
2
|
+
import { CacheConnector } from './Cache.service/CacheConnector';
|
|
3
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
4
|
+
import { uid } from '@sre/utils/index';
|
|
5
|
+
|
|
6
|
+
export type LLMCacheObject = {
|
|
7
|
+
ttl: number;
|
|
8
|
+
data: any;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export class LLMCache {
|
|
12
|
+
private _cacheConnector: CacheConnector;
|
|
13
|
+
private _cacheId: string;
|
|
14
|
+
private _ttl: number;
|
|
15
|
+
private _candidate: AccessCandidate;
|
|
16
|
+
|
|
17
|
+
public get id() {
|
|
18
|
+
return this._cacheId;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Creates a new LLMCache instace for a smythOS actor, the actor can be an agent, a user or a team
|
|
23
|
+
* This is mainly use with agent to maintain a cache of the current LLM context
|
|
24
|
+
*
|
|
25
|
+
* This class can be used to share llm contexts data accross multiple instances of an agent, or between a ConversationHelper and a remote agent
|
|
26
|
+
*
|
|
27
|
+
* @param candidate
|
|
28
|
+
* @param cacheId
|
|
29
|
+
* @param ttl
|
|
30
|
+
*/
|
|
31
|
+
constructor(candidate: AccessCandidate, cacheId?: string, ttl: number = 1 * 60 * 60) {
|
|
32
|
+
this._cacheConnector = ConnectorService.getCacheConnector();
|
|
33
|
+
this._cacheId = cacheId || 'llm_cache_' + uid();
|
|
34
|
+
this._ttl = ttl;
|
|
35
|
+
this._candidate = candidate;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async set(key: string, data: any) {
|
|
39
|
+
if (!this._cacheConnector.valid) return;
|
|
40
|
+
|
|
41
|
+
await this._cacheConnector
|
|
42
|
+
.requester(this._candidate)
|
|
43
|
+
.set(`${this._cacheId}:${key}`, typeof data === 'object' ? JSON.stringify(data) : data, null, null, this._ttl);
|
|
44
|
+
}
|
|
45
|
+
async get(key: string, format: 'json' | 'text' = 'json') {
|
|
46
|
+
if (!this._cacheConnector.valid) return;
|
|
47
|
+
|
|
48
|
+
const obj = await this._cacheConnector.requester(this._candidate).get(`${this._cacheId}:${key}`);
|
|
49
|
+
let result;
|
|
50
|
+
if (format === 'json') {
|
|
51
|
+
try {
|
|
52
|
+
result = JSON.parse(obj);
|
|
53
|
+
} catch (e) {
|
|
54
|
+
console.warn(`Invalid JSON data for key ${key}`);
|
|
55
|
+
result = null;
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
result = obj;
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
async delete(key: string) {
|
|
63
|
+
if (!this._cacheConnector.valid) return;
|
|
64
|
+
|
|
65
|
+
await this._cacheConnector.requester(this._candidate).delete(`${this._cacheId}:${key}`);
|
|
66
|
+
}
|
|
67
|
+
async clear() {
|
|
68
|
+
if (!this._cacheConnector.valid) return;
|
|
69
|
+
|
|
70
|
+
await this._cacheConnector.requester(this._candidate).delete(this._cacheId);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { encode, encodeChat } from 'gpt-tokenizer';
|
|
2
|
+
import { ChatMessage } from 'gpt-tokenizer/esm/GptEncoding';
|
|
3
|
+
import { ILLMContextStore } from '@sre/types/LLM.types';
|
|
4
|
+
import { LLMCache } from './LLMCache';
|
|
5
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
6
|
+
|
|
7
|
+
//content, name, role, tool_call_id, tool_calls, function_call
|
|
8
|
+
export class LLMContext {
|
|
9
|
+
private _systemPrompt: string = '';
|
|
10
|
+
private _llmContextStore: ILLMContextStore;
|
|
11
|
+
private _llmCache: LLMCache;
|
|
12
|
+
|
|
13
|
+
public get systemPrompt() {
|
|
14
|
+
return this._systemPrompt;
|
|
15
|
+
}
|
|
16
|
+
public set systemPrompt(systemPrompt) {
|
|
17
|
+
this._systemPrompt = systemPrompt;
|
|
18
|
+
this._llmCache?.set('systemPrompt', this.systemPrompt);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
public get llmCache() {
|
|
22
|
+
return this._llmCache;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
public contextLength: number;
|
|
26
|
+
|
|
27
|
+
private _messages: any[] = [];
|
|
28
|
+
public get messages() {
|
|
29
|
+
//TODO : check if the store is ready
|
|
30
|
+
return this._messages;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
public get model() {
|
|
34
|
+
return this.llmInference.model;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
* @param source a messages[] object, or smyth file system uri (smythfs://...)
|
|
39
|
+
*/
|
|
40
|
+
constructor(
|
|
41
|
+
private llmInference,
|
|
42
|
+
_systemPrompt: string = '',
|
|
43
|
+
/*private _messages: any[] = [],*/ llmContextStore?: ILLMContextStore,
|
|
44
|
+
) {
|
|
45
|
+
this._llmCache = new LLMCache(AccessCandidate.team(this.llmInference.teamId));
|
|
46
|
+
//this._systemPrompt = _systemPrompt;
|
|
47
|
+
this.systemPrompt = _systemPrompt;
|
|
48
|
+
|
|
49
|
+
//TODO:allow configuring a storage service
|
|
50
|
+
if (llmContextStore) {
|
|
51
|
+
this._llmContextStore = llmContextStore;
|
|
52
|
+
this._llmContextStore.load().then((messages) => {
|
|
53
|
+
this._messages = messages;
|
|
54
|
+
this._llmCache.set('messages', this._messages);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private push(...message: any[]) {
|
|
60
|
+
this._messages.push(...message);
|
|
61
|
+
//TODO: persist to storage
|
|
62
|
+
if (this._llmContextStore) {
|
|
63
|
+
this._llmContextStore.save(this._messages);
|
|
64
|
+
}
|
|
65
|
+
this._llmCache.set('messages', this._messages);
|
|
66
|
+
}
|
|
67
|
+
public addUserMessage(content: string, message_id: string, metadata?: any) {
|
|
68
|
+
//in the current implementation, we do not support forked conversations
|
|
69
|
+
//we always attatch to the last message in the queue
|
|
70
|
+
|
|
71
|
+
//TODO: implement forked conversations ==> might require updating the interfaces in order to support passing previous message_id explicitly
|
|
72
|
+
const lastMessage = this._messages[this._messages.length - 1];
|
|
73
|
+
|
|
74
|
+
if (lastMessage) {
|
|
75
|
+
if (!lastMessage.__smyth_data__?.next) {
|
|
76
|
+
lastMessage.__smyth_data__.next = [];
|
|
77
|
+
}
|
|
78
|
+
lastMessage.__smyth_data__.next.push(message_id);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const prev = lastMessage?.__smyth_data__?.message_id;
|
|
82
|
+
const next = [];
|
|
83
|
+
|
|
84
|
+
this.push({ role: 'user', content, __smyth_data__: { message_id, ...metadata, prev, next } });
|
|
85
|
+
}
|
|
86
|
+
public addAssistantMessage(content: string, message_id: string, metadata?: any) {
|
|
87
|
+
const lastMessage = this._messages[this._messages.length - 1];
|
|
88
|
+
|
|
89
|
+
if (lastMessage) {
|
|
90
|
+
if (!lastMessage.__smyth_data__?.next) {
|
|
91
|
+
lastMessage.__smyth_data__.next = [];
|
|
92
|
+
}
|
|
93
|
+
lastMessage.__smyth_data__.next.push(message_id);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const prev = lastMessage?.__smyth_data__?.message_id;
|
|
97
|
+
const next = [];
|
|
98
|
+
this.push({ role: 'assistant', content, __smyth_data__: { message_id, ...metadata, prev, next } });
|
|
99
|
+
}
|
|
100
|
+
public addToolMessage(messageBlock: any, toolsData: any, message_id: string, metadata?: any) {
|
|
101
|
+
const lastMessage = this._messages[this._messages.length - 1];
|
|
102
|
+
|
|
103
|
+
if (lastMessage) {
|
|
104
|
+
if (!lastMessage.__smyth_data__?.next) {
|
|
105
|
+
lastMessage.__smyth_data__.next = [];
|
|
106
|
+
}
|
|
107
|
+
lastMessage.__smyth_data__.next.push(message_id);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const prev = lastMessage?.__smyth_data__?.message_id;
|
|
111
|
+
const next = [];
|
|
112
|
+
this.push({ messageBlock, toolsData, __smyth_data__: { message_id, ...metadata, prev, next } });
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
public async getContextWindow(maxTokens: number, maxOutputTokens: number = 1024): Promise<any[]> {
|
|
116
|
+
const messages = JSON.parse(JSON.stringify(this._messages));
|
|
117
|
+
// if (messages[0]?.role === 'system') {
|
|
118
|
+
// messages[0].content = this.systemPrompt;
|
|
119
|
+
// } else {
|
|
120
|
+
// messages.unshift({ role: 'system', content: this.systemPrompt });
|
|
121
|
+
// }
|
|
122
|
+
|
|
123
|
+
return this.llmInference.getContextWindow(this.systemPrompt, messages, maxTokens, maxOutputTokens);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
import EventEmitter from 'events';
|
|
2
|
+
import { delay, uid } from '@sre/utils';
|
|
3
|
+
import { AgentRuntime } from '@sre/AgentManager/AgentRuntime.class';
|
|
4
|
+
import { Logger } from '@sre/helpers/Log.helper';
|
|
5
|
+
import { ConnectorService } from '@sre/Core/ConnectorsService';
|
|
6
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
7
|
+
import { CacheConnector } from './Cache.service/CacheConnector';
|
|
8
|
+
|
|
9
|
+
const console = Logger('RuntimeContext');
|
|
10
|
+
|
|
11
|
+
type TRuntimeData = {
|
|
12
|
+
input?: { [key: string]: any };
|
|
13
|
+
_LoopData?: any;
|
|
14
|
+
_ChildLoopData?: any;
|
|
15
|
+
};
|
|
16
|
+
type TComponentContext = {
|
|
17
|
+
active: boolean;
|
|
18
|
+
name: string;
|
|
19
|
+
runtimeData?: TRuntimeData;
|
|
20
|
+
step: number;
|
|
21
|
+
input?: { [key: string]: any };
|
|
22
|
+
output?: { [key: string]: any };
|
|
23
|
+
};
|
|
24
|
+
export class RuntimeContext extends EventEmitter {
|
|
25
|
+
public circularLimitReached: string | boolean = false;
|
|
26
|
+
public step: number = 0;
|
|
27
|
+
public sessionResult: boolean = false;
|
|
28
|
+
public sessionResults: any;
|
|
29
|
+
public components: { [id: string]: { ctx: TComponentContext } } = {};
|
|
30
|
+
|
|
31
|
+
public checkRuntimeContext: any = null;
|
|
32
|
+
|
|
33
|
+
private ctxFile: string = '';
|
|
34
|
+
private _runtimeFileReady: any;
|
|
35
|
+
private _cacheConnector: CacheConnector;
|
|
36
|
+
|
|
37
|
+
private _readyPromise: Promise<boolean>;
|
|
38
|
+
|
|
39
|
+
constructor(private runtime: AgentRuntime) {
|
|
40
|
+
super();
|
|
41
|
+
const agent = runtime.agent;
|
|
42
|
+
|
|
43
|
+
this._cacheConnector = ConnectorService.getCacheConnector();
|
|
44
|
+
|
|
45
|
+
const processRootID = runtime.processID?.split(':')[0] || '';
|
|
46
|
+
const reqId = processRootID == runtime.xDebugId ? '' : '.' + uid() + runtime.reqTag;
|
|
47
|
+
|
|
48
|
+
this.ctxFile = `${runtime.xDebugId}${reqId}${agent.jobID ? `-job-${agent.jobID}` : ''}`;
|
|
49
|
+
|
|
50
|
+
this._readyPromise = new Promise((resolve, reject) => {
|
|
51
|
+
let resolved = false;
|
|
52
|
+
this.on('ready', () => {
|
|
53
|
+
resolved = true;
|
|
54
|
+
resolve(true);
|
|
55
|
+
});
|
|
56
|
+
const timer = setTimeout(() => {
|
|
57
|
+
if (!resolved) {
|
|
58
|
+
reject(new Error('Agent Runtime context initialization timeout'));
|
|
59
|
+
}
|
|
60
|
+
}, 5 * 60 * 1000);
|
|
61
|
+
timer.unref(); //unblock the event loop
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
this.initRuntimeContext();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private serialize() {
|
|
68
|
+
const data = {
|
|
69
|
+
step: this.step,
|
|
70
|
+
sessionResult: this.sessionResult,
|
|
71
|
+
sessionResults: this.sessionResults,
|
|
72
|
+
components: this.components,
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return data;
|
|
76
|
+
}
|
|
77
|
+
private deserialize(data: any) {
|
|
78
|
+
this.step = data.step;
|
|
79
|
+
this.sessionResult = data.sessionResult;
|
|
80
|
+
this.sessionResults = data.sessionResults;
|
|
81
|
+
this.components = data.components;
|
|
82
|
+
}
|
|
83
|
+
private reset() {
|
|
84
|
+
this.step = 0;
|
|
85
|
+
this.sessionResult = false;
|
|
86
|
+
this.sessionResults = null;
|
|
87
|
+
this.components = {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
private initRuntimeContext() {
|
|
91
|
+
if (this._runtimeFileReady) return;
|
|
92
|
+
|
|
93
|
+
const endpointDBGCall = this.runtime.xDebugId?.startsWith('dbg-'); //weak check for debug session, we need to improve this
|
|
94
|
+
console.debug('Init ctxFile', this.ctxFile);
|
|
95
|
+
const agent = this.runtime.agent;
|
|
96
|
+
let method = (agent.agentRequest.method || 'POST').toUpperCase();
|
|
97
|
+
const endpoint = agent.endpoints?.[agent.agentRequest.path]?.[method];
|
|
98
|
+
|
|
99
|
+
let ctxData: any = {};
|
|
100
|
+
|
|
101
|
+
this._cacheConnector
|
|
102
|
+
.requester(AccessCandidate.agent(this.runtime.agent.id))
|
|
103
|
+
.get(this.ctxFile)
|
|
104
|
+
.then(async (data) => {
|
|
105
|
+
if (!data) {
|
|
106
|
+
ctxData = JSON.parse(JSON.stringify({ components: agent.components, connections: agent.connections, timestamp: Date.now() }));
|
|
107
|
+
if (!ctxData.step) ctxData.step = 0;
|
|
108
|
+
for (let cptId in ctxData.components) {
|
|
109
|
+
ctxData.components[cptId] = {
|
|
110
|
+
id: cptId,
|
|
111
|
+
name: ctxData.components[cptId].name,
|
|
112
|
+
//dbg: { active: false, name: ctxData.components[cptId].name },
|
|
113
|
+
ctx: { active: false, name: ctxData.components[cptId].name },
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
const cpt = ctxData.components[cptId];
|
|
117
|
+
//if this debug session was initiated from an endpoint, we mark the endpoint component as active
|
|
118
|
+
if (endpoint && endpoint.id != undefined && cpt.id == endpoint.id && endpointDBGCall) {
|
|
119
|
+
//cpt.dbg.active = true;
|
|
120
|
+
cpt.ctx.active = true;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
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
|
|
127
|
+
} else {
|
|
128
|
+
ctxData = JSON.parse(data);
|
|
129
|
+
if (!ctxData.step) ctxData.step = 0;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
this.deserialize(ctxData);
|
|
133
|
+
this._runtimeFileReady = true;
|
|
134
|
+
this.emit('ready');
|
|
135
|
+
});
|
|
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
|
+
}
|
|
165
|
+
|
|
166
|
+
public async ready() {
|
|
167
|
+
if (this._runtimeFileReady) return true;
|
|
168
|
+
return this._readyPromise;
|
|
169
|
+
}
|
|
170
|
+
public async sync() {
|
|
171
|
+
if (!this.ctxFile) return;
|
|
172
|
+
this.emit('syncing');
|
|
173
|
+
|
|
174
|
+
const deleteSession = this.runtime.sessionClosed;
|
|
175
|
+
|
|
176
|
+
if (deleteSession) {
|
|
177
|
+
const exists = await this._cacheConnector.requester(AccessCandidate.agent(this.runtime.agent.id)).exists(this.ctxFile);
|
|
178
|
+
|
|
179
|
+
if (exists) {
|
|
180
|
+
if (this.runtime.debug)
|
|
181
|
+
this._cacheConnector
|
|
182
|
+
.requester(AccessCandidate.agent(this.runtime.agent.id))
|
|
183
|
+
.updateTTL(this.ctxFile, 5 * 60); //expires in 5 minute
|
|
184
|
+
else this._cacheConnector.requester(AccessCandidate.agent(this.runtime.agent.id)).delete(this.ctxFile);
|
|
185
|
+
//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
|
|
186
|
+
//if (fs.existsSync(this.ctxFile)) fs.unlinkSync(this.ctxFile);
|
|
187
|
+
}
|
|
188
|
+
} else {
|
|
189
|
+
const data = this.serialize();
|
|
190
|
+
//if (data) fs.writeFileSync(this.ctxFile, JSON.stringify(data, null, 2));
|
|
191
|
+
if (data)
|
|
192
|
+
await this._cacheConnector
|
|
193
|
+
.requester(AccessCandidate.agent(this.runtime.agent.id))
|
|
194
|
+
.set(this.ctxFile, JSON.stringify(data, null, 2), null, null, 6 * 60 * 60); //expires in 6 hours max
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
public incStep() {
|
|
199
|
+
this.step++;
|
|
200
|
+
this.sync();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public updateComponent(componentId: string, data: any) {
|
|
204
|
+
const ctxData = this;
|
|
205
|
+
if (!ctxData) return;
|
|
206
|
+
const component = ctxData.components[componentId];
|
|
207
|
+
|
|
208
|
+
if (!component) {
|
|
209
|
+
console.log('>>>>>>> updateComponent Component debug data not found', componentId, component);
|
|
210
|
+
console.log('>>> ctxFile', this.ctxFile);
|
|
211
|
+
console.log('>>> ctxData', ctxData);
|
|
212
|
+
}
|
|
213
|
+
component.ctx = { ...component.ctx, ...data, step: this.step };
|
|
214
|
+
|
|
215
|
+
//if (this.debug) component.dbg = { ...component.dbg, ...data };
|
|
216
|
+
|
|
217
|
+
this.sync();
|
|
218
|
+
}
|
|
219
|
+
public resetComponent(componentId: string) {
|
|
220
|
+
const ctxData = this;
|
|
221
|
+
const component = ctxData.components[componentId];
|
|
222
|
+
if (!component) {
|
|
223
|
+
console.log('>>>>>>> resetComponent Component debug data not found', componentId, component);
|
|
224
|
+
console.log('>>> ctxFile', this.ctxFile);
|
|
225
|
+
console.log('>>> ctxData', ctxData);
|
|
226
|
+
}
|
|
227
|
+
//component.dbg.active = false;
|
|
228
|
+
//component.dbg.runtimeData = {};
|
|
229
|
+
component.ctx.runtimeData = {};
|
|
230
|
+
component.ctx.active = false;
|
|
231
|
+
|
|
232
|
+
this.sync();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
public getComponentData(componentId: string) {
|
|
236
|
+
const ctxData = this;
|
|
237
|
+
if (!ctxData) return null;
|
|
238
|
+
const component = ctxData.components[componentId];
|
|
239
|
+
if (!component) {
|
|
240
|
+
console.log('>>>>>>> getComponentData Component debug data not found', componentId, component);
|
|
241
|
+
console.log('>>> ctxFile', this.ctxFile);
|
|
242
|
+
console.log('>>> ctxData', ctxData);
|
|
243
|
+
}
|
|
244
|
+
//const data = this.debug ? component.dbg : component.ctx;
|
|
245
|
+
const data = component.ctx;
|
|
246
|
+
|
|
247
|
+
return data;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
//import { xxh3 } from '@node-rs/xxhash';
|
|
2
|
+
import xxhash from 'xxhashjs';
|
|
3
|
+
import { IACL, IAccessRequest, LevelMap, ReverseLevelMap, ReverseRoleMap, RoleMap, TACLEntry, TAccessLevel, TAccessRole } from '@sre/types/ACL.types';
|
|
4
|
+
|
|
5
|
+
const ACLHashAlgo = {
|
|
6
|
+
none: (source) => source,
|
|
7
|
+
//xxh3: (source) => xxh3.xxh64(source.toString()).toString(16),
|
|
8
|
+
xxh3: (source) => {
|
|
9
|
+
const h64 = xxhash.h64(); // Use xxhashjs's h64 function
|
|
10
|
+
return source ? h64.update(source.toString()).digest().toString(16) : null;
|
|
11
|
+
},
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export class ACL implements IACL {
|
|
15
|
+
public hashAlgorithm?: string | undefined;
|
|
16
|
+
public entries?: {
|
|
17
|
+
[key in TAccessRole]?: TACLEntry | undefined;
|
|
18
|
+
};
|
|
19
|
+
public migrated?: boolean | undefined;
|
|
20
|
+
//private acl: TACL;
|
|
21
|
+
public get ACL(): IACL {
|
|
22
|
+
return {
|
|
23
|
+
hashAlgorithm: this.hashAlgorithm,
|
|
24
|
+
entries: JSON.parse(JSON.stringify(this.entries)),
|
|
25
|
+
migrated: this.migrated,
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
public get serializedACL(): string {
|
|
29
|
+
return this.serializeACL(this);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
constructor(acl?: IACL | string) {
|
|
33
|
+
if (typeof acl === 'string') {
|
|
34
|
+
this.deserializeACL(acl);
|
|
35
|
+
} else {
|
|
36
|
+
this.hashAlgorithm = acl?.hashAlgorithm;
|
|
37
|
+
this.entries = acl?.entries ? JSON.parse(JSON.stringify(acl?.entries)) : {};
|
|
38
|
+
this.migrated = acl?.migrated;
|
|
39
|
+
}
|
|
40
|
+
if (!this.hashAlgorithm) this.hashAlgorithm = 'xxh3';
|
|
41
|
+
if (!this.entries) this.entries = {};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
static from(acl?: IACL | string): ACL {
|
|
45
|
+
return new ACL(acl);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* This function checks if the candidate has access to the requested level
|
|
50
|
+
* it only checks the exact access level, not the higher levels
|
|
51
|
+
* Examples :
|
|
52
|
+
* - if the candidate has read access, it will return true only if the requested level is read
|
|
53
|
+
* - if the current ACL has team access but the candidate is an agent, it will not match the team access
|
|
54
|
+
* @param acRequest
|
|
55
|
+
* @returns
|
|
56
|
+
*/
|
|
57
|
+
public checkExactAccess(acRequest: IAccessRequest): boolean {
|
|
58
|
+
if (!this?.entries) return false; // cannot determine the access rights, prefer to deny access
|
|
59
|
+
|
|
60
|
+
const role = this?.entries[acRequest.candidate.role];
|
|
61
|
+
if (!role) return false;
|
|
62
|
+
let entryId = acRequest.candidate.id;
|
|
63
|
+
|
|
64
|
+
if (!ACLHashAlgo[this.hashAlgorithm]) {
|
|
65
|
+
throw new Error(`Hash algorithm ${this.hashAlgorithm} not supported`);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
entryId = ACLHashAlgo[this.hashAlgorithm](entryId);
|
|
69
|
+
|
|
70
|
+
const access = role[entryId];
|
|
71
|
+
if (!access) return false;
|
|
72
|
+
|
|
73
|
+
const levels = Array.isArray(acRequest.level) ? acRequest.level : [acRequest.level];
|
|
74
|
+
|
|
75
|
+
return levels.every((level) => access.includes(level));
|
|
76
|
+
//return access.includes(req.level);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
public addPublicAccess(level: TAccessLevel | TAccessLevel[]): ACL {
|
|
80
|
+
if (!this?.entries[TAccessRole.Public]) this.entries[TAccessRole.Public] = {};
|
|
81
|
+
if (!ACLHashAlgo[this.hashAlgorithm]) {
|
|
82
|
+
throw new Error(`Hash algorithm ${this.hashAlgorithm} not supported`);
|
|
83
|
+
}
|
|
84
|
+
const ownerId = TAccessRole.Public; //public is a special case we use the role as the owner id because public access does not have specific candidate IDs
|
|
85
|
+
const hashedOwner = ACLHashAlgo[this.hashAlgorithm](ownerId);
|
|
86
|
+
|
|
87
|
+
if (!this?.entries[TAccessRole.Public]![hashedOwner]) this.entries[TAccessRole.Public]![hashedOwner] = [];
|
|
88
|
+
//acl[TAccessRole.Public]![hashedOwner]!.push(level);
|
|
89
|
+
//concatenate the levels
|
|
90
|
+
const curLevel: any = this.entries[TAccessRole.Public]![hashedOwner]!;
|
|
91
|
+
this.entries[TAccessRole.Public]![hashedOwner] = [...curLevel, ...level];
|
|
92
|
+
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
public removePublicAccess(level: TAccessLevel | TAccessLevel[]): ACL {
|
|
96
|
+
if (!this?.entries[TAccessRole.Public]) return this;
|
|
97
|
+
const ownerId = TAccessRole.Public; //public is a special case we use the role as the owner id because public access does not have specific candidate IDs
|
|
98
|
+
const hashedOwner = ACLHashAlgo[this.hashAlgorithm](ownerId);
|
|
99
|
+
|
|
100
|
+
//remove the levels
|
|
101
|
+
const curLevel = this[TAccessRole.Public]![hashedOwner]!;
|
|
102
|
+
this[TAccessRole.Public]![hashedOwner] = curLevel.filter((l) => !level.includes(l));
|
|
103
|
+
|
|
104
|
+
return this;
|
|
105
|
+
}
|
|
106
|
+
public addAccess(role: TAccessRole, ownerId: string, level: TAccessLevel | TAccessLevel[]): ACL {
|
|
107
|
+
if (role === TAccessRole.Public) {
|
|
108
|
+
throw new Error('Adding public access using addAccess method is not allowed. Use addPublicAccess method instead.');
|
|
109
|
+
}
|
|
110
|
+
const _level = Array.isArray(level) ? level : [level];
|
|
111
|
+
if (!this?.entries[role]) this.entries[role] = {};
|
|
112
|
+
if (!ACLHashAlgo[this.hashAlgorithm]) {
|
|
113
|
+
throw new Error(`Hash algorithm ${this.hashAlgorithm} not supported`);
|
|
114
|
+
}
|
|
115
|
+
const hashedOwner = ACLHashAlgo[this.hashAlgorithm](ownerId);
|
|
116
|
+
|
|
117
|
+
if (!hashedOwner) {
|
|
118
|
+
throw new Error(`Invalid ownerId: ${role}:${ownerId}`);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if (!this?.entries[role]![hashedOwner]) this.entries[role]![hashedOwner] = [];
|
|
122
|
+
//acl[role]![ownerId]!.push(level);
|
|
123
|
+
//concatenate the levels
|
|
124
|
+
const curLevel = this.entries[role]![hashedOwner]!;
|
|
125
|
+
this.entries[role]![hashedOwner] = [...curLevel, ..._level];
|
|
126
|
+
|
|
127
|
+
return this;
|
|
128
|
+
}
|
|
129
|
+
public static addAccess(role: TAccessRole, ownerId: string, level: TAccessLevel | TAccessLevel[]): ACL {
|
|
130
|
+
return ACL.from().addAccess(role, ownerId, level);
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
public removeAccess(role: TAccessRole, ownerId: string, level: TAccessLevel | TAccessLevel[]): ACL {
|
|
134
|
+
const _level = Array.isArray(level) ? level : [level];
|
|
135
|
+
if (!this[role]) return this;
|
|
136
|
+
if (!this[role]![ownerId]) return this;
|
|
137
|
+
//acl[role]![ownerId] = acl[role]![ownerId]!.filter((l) => l !== level);
|
|
138
|
+
//remove the levels
|
|
139
|
+
const curLevel = this[role]![ownerId]!;
|
|
140
|
+
this[role]![ownerId] = curLevel.filter((l) => !_level.includes(l));
|
|
141
|
+
|
|
142
|
+
return this;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
private serializeACL(tacl: IACL): string {
|
|
146
|
+
let compressed = '';
|
|
147
|
+
|
|
148
|
+
if (tacl.hashAlgorithm) {
|
|
149
|
+
compressed += `h:${tacl.hashAlgorithm}|`;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (tacl.entries) {
|
|
153
|
+
for (const [role, entries] of Object.entries(tacl.entries)) {
|
|
154
|
+
const roleShort = RoleMap[role]; // Use the mapping for role
|
|
155
|
+
const entriesArray: any[] = [];
|
|
156
|
+
|
|
157
|
+
for (const [hashedOwnerKey, accessLevels] of Object.entries(entries || {})) {
|
|
158
|
+
if (accessLevels) {
|
|
159
|
+
const accessLevelsShort = accessLevels.map((level) => LevelMap[level]).join('');
|
|
160
|
+
entriesArray.push(`${hashedOwnerKey}/${accessLevelsShort}`);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (entriesArray.length > 0) {
|
|
165
|
+
compressed += `${roleShort}:${entriesArray.join(',')}|`;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// Remove the trailing '|'
|
|
171
|
+
if (compressed.endsWith('|')) {
|
|
172
|
+
compressed = compressed.slice(0, -1);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
return compressed;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
private deserializeACL(compressed: string) {
|
|
179
|
+
const parts = compressed.split('|');
|
|
180
|
+
this.hashAlgorithm = '';
|
|
181
|
+
this.entries = {};
|
|
182
|
+
|
|
183
|
+
for (const part of parts) {
|
|
184
|
+
if (part.startsWith('h:')) {
|
|
185
|
+
this.hashAlgorithm = part.substring(2);
|
|
186
|
+
} else {
|
|
187
|
+
const [roleShort, entries] = part.split(':');
|
|
188
|
+
const role = ReverseRoleMap[roleShort]; // Use the reverse mapping for role
|
|
189
|
+
|
|
190
|
+
if (role) {
|
|
191
|
+
const entriesObj = {};
|
|
192
|
+
const entriesArray = entries.split(',');
|
|
193
|
+
|
|
194
|
+
for (const entry of entriesArray) {
|
|
195
|
+
const [hashedOwnerKey, accessLevelsShort] = entry.split('/');
|
|
196
|
+
const accessLevels = accessLevelsShort.split('').map((short) => ReverseLevelMap[short]);
|
|
197
|
+
|
|
198
|
+
entriesObj[hashedOwnerKey] = accessLevels;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
this.entries[role] = entriesObj;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
//return tacl;
|
|
207
|
+
}
|
|
208
|
+
}
|