@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.
- package/CHANGELOG +62 -0
- package/LICENSE +18 -0
- package/package.json +127 -115
- 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 +450 -0
- package/src/subsystems/IO/VectorDB.service/connectors/PineconeVectorDB.class.ts +373 -0
- package/src/subsystems/IO/VectorDB.service/connectors/RAMVecrtorDB.class.ts +420 -0
- package/src/subsystems/IO/VectorDB.service/embed/BaseEmbedding.ts +106 -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 +374 -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 +78 -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,418 @@
|
|
|
1
|
+
//==[ SRE: S3Storage ]======================
|
|
2
|
+
|
|
3
|
+
//#region = [Polyfill for CommonJS] =================================
|
|
4
|
+
|
|
5
|
+
//S3 Methods fail in CommonJS build because they expect a global 'crypto' object with a 'getRandomValues' method
|
|
6
|
+
//getRandomValues is supposed to be for browser environments, but it seems that CommonJS build leaks some browser related code to the packaged AWS-SDK
|
|
7
|
+
import crypto from 'crypto';
|
|
8
|
+
|
|
9
|
+
Object.defineProperty(global, 'crypto', {
|
|
10
|
+
value: {
|
|
11
|
+
getRandomValues: (arr: any) => crypto.randomBytes(arr.length),
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
//#endregion
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
DeleteObjectCommand,
|
|
18
|
+
GetObjectCommand,
|
|
19
|
+
GetObjectCommandOutput,
|
|
20
|
+
HeadObjectCommand,
|
|
21
|
+
HeadObjectCommandOutput,
|
|
22
|
+
PutObjectCommand,
|
|
23
|
+
PutObjectTaggingCommand,
|
|
24
|
+
S3Client,
|
|
25
|
+
S3ClientConfig,
|
|
26
|
+
} from '@aws-sdk/client-s3';
|
|
27
|
+
|
|
28
|
+
import { Logger } from '@sre/helpers/Log.helper';
|
|
29
|
+
import { IStorageRequest, StorageConnector } from '@sre/IO/Storage.service/StorageConnector';
|
|
30
|
+
import { ACL } from '@sre/Security/AccessControl/ACL.class';
|
|
31
|
+
import { IAccessCandidate, IACL, TAccessLevel, TAccessResult, TAccessRole } from '@sre/types/ACL.types';
|
|
32
|
+
import { AWSRegionConfig, AWSCredentials } from '@sre/types/AWS.types';
|
|
33
|
+
import { StorageData, StorageMetadata } from '@sre/types/Storage.types';
|
|
34
|
+
import { streamToBuffer } from '@sre/utils';
|
|
35
|
+
import type { Readable } from 'stream';
|
|
36
|
+
|
|
37
|
+
//import { SmythRuntime } from '@sre/Core/SmythRuntime.class';
|
|
38
|
+
import { AccessRequest } from '@sre/Security/AccessControl/AccessRequest.class';
|
|
39
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
40
|
+
import { SecureConnector } from '@sre/Security/SecureConnector.class';
|
|
41
|
+
import { checkAndInstallLifecycleRules, generateExpiryMetadata, ttlToExpiryDays } from '@sre/helpers/S3Cache.helper';
|
|
42
|
+
import { ConnectorService } from '@sre/Core/ConnectorsService';
|
|
43
|
+
|
|
44
|
+
const console = Logger('S3Storage');
|
|
45
|
+
|
|
46
|
+
export type S3Config = AWSCredentials & AWSRegionConfig & { bucket: string };
|
|
47
|
+
|
|
48
|
+
export class S3Storage extends StorageConnector {
|
|
49
|
+
public name = 'S3Storage';
|
|
50
|
+
private client: S3Client;
|
|
51
|
+
private bucket: string;
|
|
52
|
+
private isInitialized: boolean = false;
|
|
53
|
+
|
|
54
|
+
constructor(protected _settings: S3Config) {
|
|
55
|
+
super(_settings);
|
|
56
|
+
//if (!SmythRuntime.Instance) throw new Error('SRE not initialized');
|
|
57
|
+
this.bucket = _settings.bucket;
|
|
58
|
+
const clientConfig: any = {};
|
|
59
|
+
if (_settings.region) clientConfig.region = _settings.region;
|
|
60
|
+
if (_settings.accessKeyId && _settings.secretAccessKey) {
|
|
61
|
+
clientConfig.credentials = {
|
|
62
|
+
accessKeyId: _settings.accessKeyId,
|
|
63
|
+
secretAccessKey: _settings.secretAccessKey,
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
this.client = new S3Client(clientConfig);
|
|
68
|
+
this.initialize();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private async initialize() {
|
|
72
|
+
await checkAndInstallLifecycleRules(this.bucket, this.client);
|
|
73
|
+
this.isInitialized = true;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Reads an object from the S3 bucket.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} resourceId - The key of the object to be read.
|
|
80
|
+
* @returns {Promise<any>} - A promise that resolves with the object data.
|
|
81
|
+
*/
|
|
82
|
+
|
|
83
|
+
@SecureConnector.AccessControl
|
|
84
|
+
public async read(acRequest: AccessRequest, resourceId: string) {
|
|
85
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
86
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
87
|
+
const params = {
|
|
88
|
+
Bucket: this.bucket,
|
|
89
|
+
Key: resourceId,
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
const s3HeadCommand = new HeadObjectCommand(params);
|
|
93
|
+
const s3HeadData: HeadObjectCommandOutput = await this.client.send(s3HeadCommand);
|
|
94
|
+
|
|
95
|
+
const expirationHeader = s3HeadData?.Expiration;
|
|
96
|
+
if (expirationHeader) {
|
|
97
|
+
const expirationDateMatch = expirationHeader.match(/expiry-date="([^"]+)"/);
|
|
98
|
+
if (expirationDateMatch) {
|
|
99
|
+
const expirationDate = new Date(expirationDateMatch[1]);
|
|
100
|
+
const currentDate = new Date();
|
|
101
|
+
|
|
102
|
+
if (currentDate > expirationDate) {
|
|
103
|
+
const s3DeleteCommand = new DeleteObjectCommand(params);
|
|
104
|
+
await this.client.send(s3DeleteCommand);
|
|
105
|
+
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const command = new GetObjectCommand(params);
|
|
112
|
+
|
|
113
|
+
try {
|
|
114
|
+
const response: GetObjectCommandOutput = await this.client.send(command);
|
|
115
|
+
//const metadata = response.Metadata;
|
|
116
|
+
return await streamToBuffer(response.Body as Readable);
|
|
117
|
+
} catch (error) {
|
|
118
|
+
if (error.name === 'NotFound' || error.name === 'NoSuchKey') {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
console.error(`Error reading object from S3`, error.name, error.message);
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@SecureConnector.AccessControl
|
|
127
|
+
async getMetadata(acRequest: AccessRequest, resourceId: string): Promise<StorageMetadata | undefined> {
|
|
128
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
129
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
130
|
+
|
|
131
|
+
try {
|
|
132
|
+
const s3Metadata = await this.getS3Metadata(resourceId);
|
|
133
|
+
return s3Metadata as StorageMetadata;
|
|
134
|
+
} catch (error) {
|
|
135
|
+
console.error(`Error getting access rights in S3`, error.name, error.message);
|
|
136
|
+
throw error;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
@SecureConnector.AccessControl
|
|
141
|
+
async setMetadata(acRequest: AccessRequest, resourceId: string, metadata: StorageMetadata) {
|
|
142
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
143
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
144
|
+
|
|
145
|
+
try {
|
|
146
|
+
let s3Metadata = await this.getS3Metadata(resourceId);
|
|
147
|
+
if (!s3Metadata) s3Metadata = {};
|
|
148
|
+
//s3Metadata['x-amz-meta-data'] = metadata;
|
|
149
|
+
s3Metadata = { ...s3Metadata, ...metadata };
|
|
150
|
+
await this.setS3Metadata(resourceId, s3Metadata);
|
|
151
|
+
} catch (error) {
|
|
152
|
+
console.error(`Error setting access rights in S3`, error);
|
|
153
|
+
throw error;
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Writes an object to the S3 bucket.
|
|
158
|
+
*
|
|
159
|
+
* @param {string} resourceId - The key of the object to be written.
|
|
160
|
+
* @param {any} value - The value of the object to be written.
|
|
161
|
+
* @param {Metadata} metadata - Optional metadata to be associated with the object.
|
|
162
|
+
* @returns {Promise<void>} - A promise that resolves when the object has been written.
|
|
163
|
+
*/
|
|
164
|
+
@SecureConnector.AccessControl
|
|
165
|
+
async write(acRequest: AccessRequest, resourceId: string, value: StorageData, acl?: IACL, metadata?: StorageMetadata): Promise<void> {
|
|
166
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
167
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
168
|
+
if (!this.isInitialized) await this.initialize();
|
|
169
|
+
const accessCandidate = acRequest.candidate;
|
|
170
|
+
|
|
171
|
+
let amzACL = ACL.from(acl).addAccess(accessCandidate.role, accessCandidate.id, TAccessLevel.Owner).ACL;
|
|
172
|
+
let s3Metadata = {
|
|
173
|
+
...metadata,
|
|
174
|
+
'x-amz-meta-acl': amzACL,
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const command = new PutObjectCommand({
|
|
178
|
+
Bucket: this.bucket,
|
|
179
|
+
Key: resourceId,
|
|
180
|
+
Body: value,
|
|
181
|
+
Metadata: this.serializeS3Metadata(s3Metadata),
|
|
182
|
+
ContentType: s3Metadata['ContentType'],
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
const result: any = await this.client.send(command);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
console.error(`Error writing object to S3`, error.name, error.message);
|
|
189
|
+
//console.error(error);
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Deletes an object from the S3 bucket.
|
|
196
|
+
*
|
|
197
|
+
* @param {string} resourceId - The key of the object to be deleted.
|
|
198
|
+
* @returns {Promise<void>} - A promise that resolves when the object has been deleted.
|
|
199
|
+
*/
|
|
200
|
+
@SecureConnector.AccessControl
|
|
201
|
+
async delete(acRequest: AccessRequest, resourceId: string): Promise<void> {
|
|
202
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
203
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
204
|
+
|
|
205
|
+
const command = new DeleteObjectCommand({
|
|
206
|
+
Bucket: this.bucket,
|
|
207
|
+
Key: resourceId,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
try {
|
|
211
|
+
await this.client.send(command);
|
|
212
|
+
} catch (error) {
|
|
213
|
+
console.error(`Error deleting object from S3`, error.name, error.message);
|
|
214
|
+
throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
@SecureConnector.AccessControl
|
|
219
|
+
async exists(acRequest: AccessRequest, resourceId: string): Promise<boolean> {
|
|
220
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
221
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
222
|
+
const command = new HeadObjectCommand({
|
|
223
|
+
Bucket: this.bucket,
|
|
224
|
+
Key: resourceId,
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
try {
|
|
228
|
+
await this.client.send(command);
|
|
229
|
+
return true;
|
|
230
|
+
} catch (error) {
|
|
231
|
+
if (error.name === 'NotFound' || error.name === 'NoSuchKey') {
|
|
232
|
+
return false;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
console.error(`Error checking object existence in S3`, error.name, error.message);
|
|
236
|
+
throw error;
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
//this determines the access rights for the requested resource
|
|
241
|
+
//the connector should check if the resource exists or not
|
|
242
|
+
//if the resource exists we read it's ACL and return it
|
|
243
|
+
//if the resource does not exist we return an write access ACL for the candidate
|
|
244
|
+
public async getResourceACL(resourceId: string, candidate: IAccessCandidate) {
|
|
245
|
+
const s3Metadata = await this.getS3Metadata(resourceId);
|
|
246
|
+
const exists = s3Metadata !== undefined; //undefined metadata means the resource does not exist
|
|
247
|
+
//let acl: ACL = ACL.from(s3Metadata?.['x-amz-meta-acl'] as IACL);
|
|
248
|
+
|
|
249
|
+
if (!exists) {
|
|
250
|
+
//the resource does not exist yet, we grant write access to the candidate in order to allow the resource creation
|
|
251
|
+
return new ACL().addAccess(candidate.role, candidate.id, TAccessLevel.Owner);
|
|
252
|
+
}
|
|
253
|
+
return ACL.from(s3Metadata?.['x-amz-meta-acl'] as IACL);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@SecureConnector.AccessControl
|
|
257
|
+
async getACL(acRequest: AccessRequest, resourceId: string): Promise<ACL | undefined> {
|
|
258
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
259
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
260
|
+
|
|
261
|
+
try {
|
|
262
|
+
const s3Metadata = await this.getS3Metadata(resourceId);
|
|
263
|
+
return ACL.from(s3Metadata?.['x-amz-meta-acl'] as IACL);
|
|
264
|
+
} catch (error) {
|
|
265
|
+
console.error(`Error getting access rights in S3`, error.name, error.message);
|
|
266
|
+
throw error;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
@SecureConnector.AccessControl
|
|
271
|
+
async setACL(acRequest: AccessRequest, resourceId: string, acl: IACL) {
|
|
272
|
+
// const accessTicket = await this.getAccessTicket(resourceId, acRequest);
|
|
273
|
+
// if (accessTicket.access !== TAccessResult.Granted) throw new Error('Access Denied');
|
|
274
|
+
|
|
275
|
+
try {
|
|
276
|
+
let s3Metadata = await this.getS3Metadata(resourceId);
|
|
277
|
+
if (!s3Metadata) s3Metadata = {};
|
|
278
|
+
//when setting ACL make sure to not lose ownership
|
|
279
|
+
s3Metadata['x-amz-meta-acl'] = ACL.from(acl).addAccess(acRequest.candidate.role, acRequest.candidate.id, TAccessLevel.Owner).ACL;
|
|
280
|
+
await this.setS3Metadata(resourceId, s3Metadata);
|
|
281
|
+
} catch (error) {
|
|
282
|
+
console.error(`Error setting access rights in S3`, error);
|
|
283
|
+
throw error;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
@SecureConnector.AccessControl
|
|
288
|
+
async expire(acRequest: AccessRequest, resourceId: string, ttl: number) {
|
|
289
|
+
const expiryMetadata = generateExpiryMetadata(ttlToExpiryDays(ttl)); // seconds to days
|
|
290
|
+
const s3PutObjectTaggingCommand = new PutObjectTaggingCommand({
|
|
291
|
+
Bucket: this.bucket,
|
|
292
|
+
Key: resourceId,
|
|
293
|
+
Tagging: { TagSet: [{ Key: expiryMetadata.Key, Value: expiryMetadata.Value }] },
|
|
294
|
+
});
|
|
295
|
+
await this.client.send(s3PutObjectTaggingCommand);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
private migrateMetadata(metadata: Record<string, string>): Record<string, any> {
|
|
299
|
+
if (!metadata.agentid && !metadata.teamid && !metadata.userid) return metadata as Record<string, any>;
|
|
300
|
+
else {
|
|
301
|
+
const convertibleItems = ['agentid', 'teamid', 'userid'];
|
|
302
|
+
const aclHelper = new ACL();
|
|
303
|
+
|
|
304
|
+
for (let key of convertibleItems) {
|
|
305
|
+
if (!metadata[key]) continue;
|
|
306
|
+
const role = key === 'agentid' ? TAccessRole.Agent : key === 'teamid' ? TAccessRole.Team : TAccessRole.User;
|
|
307
|
+
aclHelper.addAccess(role, metadata[key].toString(), [TAccessLevel.Owner, TAccessLevel.Read, TAccessLevel.Write]);
|
|
308
|
+
delete metadata[key];
|
|
309
|
+
}
|
|
310
|
+
aclHelper.migrated = true;
|
|
311
|
+
const newMetadata: Record<string, any> = {
|
|
312
|
+
'x-amz-meta-acl': aclHelper.ACL,
|
|
313
|
+
};
|
|
314
|
+
//copy remaining metadata
|
|
315
|
+
for (let key in metadata) {
|
|
316
|
+
newMetadata[key] = metadata[key];
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
return newMetadata;
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private serializeS3Metadata(s3Metadata: Record<string, any>): Record<string, string> {
|
|
324
|
+
let amzMetadata = {};
|
|
325
|
+
if (s3Metadata['x-amz-meta-acl']) {
|
|
326
|
+
//const acl: TACL = s3Metadata['x-amz-meta-acl'];
|
|
327
|
+
if (s3Metadata['x-amz-meta-acl']) {
|
|
328
|
+
amzMetadata['x-amz-meta-acl'] =
|
|
329
|
+
typeof s3Metadata['x-amz-meta-acl'] == 'string'
|
|
330
|
+
? s3Metadata['x-amz-meta-acl']
|
|
331
|
+
: ACL.from(s3Metadata['x-amz-meta-acl']).serializedACL;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
delete s3Metadata['x-amz-meta-acl'];
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
for (let key in s3Metadata) {
|
|
338
|
+
if (key == 'ContentType') continue; //skip ContentType as it can only be set when writing the object
|
|
339
|
+
amzMetadata[key] = typeof s3Metadata[key] === 'string' ? s3Metadata[key] : JSON.stringify(s3Metadata[key]);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
return amzMetadata;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private deserializeS3Metadata(amzMetadata: Record<string, string>): Record<string, any> {
|
|
346
|
+
let metadata: Record<string, any> = {};
|
|
347
|
+
|
|
348
|
+
for (let key in amzMetadata) {
|
|
349
|
+
if (key === 'x-amz-meta-acl') {
|
|
350
|
+
metadata[key] = ACL.from(amzMetadata[key]).ACL;
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
try {
|
|
355
|
+
metadata[key] = JSON.parse(amzMetadata[key]);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
metadata[key] = amzMetadata[key];
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
//TODO : Remove this migration code after all metadata is migrated
|
|
361
|
+
// Context : an old ACL metadata format was used in initial implementation of Smyth Storage
|
|
362
|
+
// We need to ensure compatibility with legacy format and seamlessly convert it when reading
|
|
363
|
+
metadata = this.migrateMetadata(metadata) as Record<string, any>;
|
|
364
|
+
|
|
365
|
+
return metadata;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
private async getS3Metadata(resourceId: string): Promise<Record<string, any> | undefined> {
|
|
369
|
+
try {
|
|
370
|
+
const command = new HeadObjectCommand({
|
|
371
|
+
Bucket: this.bucket,
|
|
372
|
+
Key: resourceId,
|
|
373
|
+
});
|
|
374
|
+
const response: HeadObjectCommandOutput = await this.client.send(command);
|
|
375
|
+
const s3RawMetadata = response.Metadata;
|
|
376
|
+
if (!s3RawMetadata || Object.keys(s3RawMetadata).length === 0) return {};
|
|
377
|
+
|
|
378
|
+
let metadata: Record<string, any> = this.deserializeS3Metadata(s3RawMetadata);
|
|
379
|
+
|
|
380
|
+
if (!metadata['ContentType']) metadata['ContentType'] = response.ContentType ? response.ContentType : 'application/octet-stream';
|
|
381
|
+
return metadata;
|
|
382
|
+
} catch (error) {
|
|
383
|
+
if (error.name === 'NotFound' || error.name === 'NoSuchKey') {
|
|
384
|
+
return undefined;
|
|
385
|
+
}
|
|
386
|
+
console.error(`Error reading object metadata from S3`, error.name, error.message);
|
|
387
|
+
throw error;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
private async setS3Metadata(resourceId: string, metadata: Record<string, any>): Promise<void> {
|
|
392
|
+
try {
|
|
393
|
+
// Get the current object content
|
|
394
|
+
const getObjectCommand = new GetObjectCommand({
|
|
395
|
+
Bucket: this.bucket,
|
|
396
|
+
Key: resourceId,
|
|
397
|
+
});
|
|
398
|
+
const objectData: GetObjectCommandOutput = await this.client.send(getObjectCommand);
|
|
399
|
+
|
|
400
|
+
// Read the object's content
|
|
401
|
+
const bufferBody = await streamToBuffer(objectData.Body as Readable);
|
|
402
|
+
|
|
403
|
+
const amzMetadata = this.serializeS3Metadata(metadata);
|
|
404
|
+
// Put the object back with the new metadata and the same content
|
|
405
|
+
const putObjectCommand = new PutObjectCommand({
|
|
406
|
+
Bucket: this.bucket,
|
|
407
|
+
Key: resourceId,
|
|
408
|
+
Body: bufferBody,
|
|
409
|
+
Metadata: amzMetadata,
|
|
410
|
+
});
|
|
411
|
+
|
|
412
|
+
await this.client.send(putObjectCommand);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
console.error(`Error setting object metadata in S3`, error.name, error.message);
|
|
415
|
+
throw error;
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//==[ SRE: Storage ]======================
|
|
2
|
+
|
|
3
|
+
import { ConnectorService, ConnectorServiceProvider } from '@sre/Core/ConnectorsService';
|
|
4
|
+
import { TConnectorService } from '@sre/types/SRE.types';
|
|
5
|
+
import { S3Storage } from './connectors/S3Storage.class';
|
|
6
|
+
import { LocalStorage } from './connectors/LocalStorage.class';
|
|
7
|
+
|
|
8
|
+
export class StorageService extends ConnectorServiceProvider {
|
|
9
|
+
public register() {
|
|
10
|
+
ConnectorService.register(TConnectorService.Storage, 'S3', S3Storage);
|
|
11
|
+
ConnectorService.register(TConnectorService.Storage, 'LocalStorage', LocalStorage);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { ACL } from '@sre/Security/AccessControl/ACL.class';
|
|
2
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
3
|
+
import { AccessRequest } from '@sre/Security/AccessControl/AccessRequest.class';
|
|
4
|
+
import { SecureConnector } from '@sre/Security/SecureConnector.class';
|
|
5
|
+
import { IAccessCandidate, TAccessRole } from '@sre/types/ACL.types';
|
|
6
|
+
import { DatasourceDto, IStorageVectorDataSource, IVectorDataSourceDto, QueryOptions, VectorsResultData } from '@sre/types/VectorDB.types';
|
|
7
|
+
|
|
8
|
+
export type DeleteFilterOptions = {
|
|
9
|
+
datasourceId?: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type DeleteTarget = string | string[] | DeleteFilterOptions;
|
|
13
|
+
|
|
14
|
+
export interface IVectorDBRequest {
|
|
15
|
+
search(namespace: string, query: string | number[], options?: QueryOptions): Promise<VectorsResultData>;
|
|
16
|
+
// insert(namespace: string, source: IVectorDataSourceDto | IVectorDataSourceDto[]): Promise<string[]>;
|
|
17
|
+
// delete(namespace: string, id: string | string[]): Promise<void>;
|
|
18
|
+
|
|
19
|
+
createDatasource(namespace: string, datasource: DatasourceDto): Promise<IStorageVectorDataSource>;
|
|
20
|
+
deleteDatasource(namespace: string, datasourceId: string): Promise<void>;
|
|
21
|
+
listDatasources(namespace: string): Promise<IStorageVectorDataSource[]>;
|
|
22
|
+
getDatasource(namespace: string, datasourceId: string): Promise<IStorageVectorDataSource>;
|
|
23
|
+
|
|
24
|
+
createNamespace(namespace: string, metadata?: { [key: string]: any }): Promise<void>;
|
|
25
|
+
deleteNamespace(namespace: string): Promise<void>;
|
|
26
|
+
namespaceExists(namespace: string): Promise<boolean>;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export abstract class VectorDBConnector extends SecureConnector<IVectorDBRequest> {
|
|
30
|
+
protected readonly USER_METADATA_KEY = 'user_metadata';
|
|
31
|
+
|
|
32
|
+
public abstract id: string;
|
|
33
|
+
public abstract getResourceACL(resourceId: string, candidate: IAccessCandidate): Promise<ACL>;
|
|
34
|
+
|
|
35
|
+
public requester(candidate: AccessCandidate): IVectorDBRequest {
|
|
36
|
+
return {
|
|
37
|
+
search: async (namespace: string, query: string | number[], options: QueryOptions) => {
|
|
38
|
+
return await this.search(candidate.readRequest, namespace, query, options);
|
|
39
|
+
},
|
|
40
|
+
|
|
41
|
+
createDatasource: async (namespace: string, datasource: DatasourceDto) => {
|
|
42
|
+
return await this.createDatasource(candidate.writeRequest, namespace, datasource);
|
|
43
|
+
},
|
|
44
|
+
deleteDatasource: async (namespace: string, datasourceId: string) => {
|
|
45
|
+
await this.deleteDatasource(candidate.writeRequest, namespace, datasourceId);
|
|
46
|
+
},
|
|
47
|
+
listDatasources: async (namespace: string) => {
|
|
48
|
+
return await this.listDatasources(candidate.readRequest, namespace);
|
|
49
|
+
},
|
|
50
|
+
getDatasource: async (namespace: string, datasourceId: string) => {
|
|
51
|
+
return await this.getDatasource(candidate.readRequest, namespace, datasourceId);
|
|
52
|
+
},
|
|
53
|
+
|
|
54
|
+
createNamespace: async (namespace: string, metadata?: { [key: string]: any }) => {
|
|
55
|
+
await this.createNamespace(candidate.writeRequest, namespace, metadata);
|
|
56
|
+
},
|
|
57
|
+
deleteNamespace: async (namespace: string) => {
|
|
58
|
+
await this.deleteNamespace(candidate.writeRequest, namespace);
|
|
59
|
+
},
|
|
60
|
+
namespaceExists: async (namespace: string) => {
|
|
61
|
+
return await this.namespaceExists(candidate.readRequest, namespace);
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
protected abstract search(
|
|
67
|
+
acRequest: AccessRequest,
|
|
68
|
+
namespace: string,
|
|
69
|
+
query: string | number[],
|
|
70
|
+
options: QueryOptions
|
|
71
|
+
): Promise<VectorsResultData>;
|
|
72
|
+
|
|
73
|
+
protected abstract insert(acRequest: AccessRequest, namespace: string, source: IVectorDataSourceDto | IVectorDataSourceDto[]): Promise<string[]>;
|
|
74
|
+
|
|
75
|
+
protected abstract delete(acRequest: AccessRequest, namespace: string, deleteTarget: DeleteTarget): Promise<void>;
|
|
76
|
+
|
|
77
|
+
protected abstract createDatasource(acRequest: AccessRequest, namespace: string, datasource: DatasourceDto): Promise<IStorageVectorDataSource>;
|
|
78
|
+
|
|
79
|
+
protected abstract deleteDatasource(acRequest: AccessRequest, namespace: string, datasourceId: string): Promise<void>;
|
|
80
|
+
|
|
81
|
+
protected abstract listDatasources(acRequest: AccessRequest, namespace: string): Promise<IStorageVectorDataSource[]>;
|
|
82
|
+
|
|
83
|
+
protected abstract getDatasource(
|
|
84
|
+
acRequest: AccessRequest,
|
|
85
|
+
namespace: string,
|
|
86
|
+
datasourceId: string
|
|
87
|
+
): Promise<IStorageVectorDataSource | undefined>;
|
|
88
|
+
|
|
89
|
+
protected abstract createNamespace(
|
|
90
|
+
acRequest: AccessRequest,
|
|
91
|
+
namespace: string,
|
|
92
|
+
|
|
93
|
+
metadata?: { [key: string]: any }
|
|
94
|
+
): Promise<void>;
|
|
95
|
+
|
|
96
|
+
protected abstract deleteNamespace(acRequest: AccessRequest, namespace: string): Promise<void>;
|
|
97
|
+
|
|
98
|
+
protected abstract namespaceExists(acRequest: AccessRequest, namespace: string): Promise<boolean>;
|
|
99
|
+
|
|
100
|
+
public constructNsName(candidate: AccessCandidate, name: string) {
|
|
101
|
+
//normalise name
|
|
102
|
+
const joinedName = name.trim().replace(/\s/g, '_').toLowerCase();
|
|
103
|
+
//add prefix = first letter of role + id
|
|
104
|
+
let prefix = candidate.role[0] + '_' + candidate.id;
|
|
105
|
+
|
|
106
|
+
return `${prefix}_${joinedName}`;
|
|
107
|
+
}
|
|
108
|
+
}
|