@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,375 @@
|
|
|
1
|
+
import { Connector } from '@sre/Core/Connector.class';
|
|
2
|
+
import { ConnectorService } from '@sre/Core/ConnectorsService';
|
|
3
|
+
import { Logger } from '@sre/helpers/Log.helper';
|
|
4
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
5
|
+
import { JSONContent } from '@sre/helpers/JsonContent.helper';
|
|
6
|
+
import {
|
|
7
|
+
TLLMParams,
|
|
8
|
+
TLLMConnectorParams,
|
|
9
|
+
TLLMMessageBlock,
|
|
10
|
+
TLLMToolResultMessageBlock,
|
|
11
|
+
ToolData,
|
|
12
|
+
APIKeySource,
|
|
13
|
+
TLLMModel,
|
|
14
|
+
TLLMCredentials,
|
|
15
|
+
TBedrockSettings,
|
|
16
|
+
TVertexAISettings,
|
|
17
|
+
ILLMRequestFuncParams,
|
|
18
|
+
TLLMChatResponse,
|
|
19
|
+
TLLMRequestBody,
|
|
20
|
+
} from '@sre/types/LLM.types';
|
|
21
|
+
import EventEmitter from 'events';
|
|
22
|
+
import { Readable } from 'stream';
|
|
23
|
+
import { AccountConnector } from '@sre/Security/Account.service/AccountConnector';
|
|
24
|
+
import { VaultConnector } from '@sre/Security/Vault.service/VaultConnector';
|
|
25
|
+
import { TCustomLLMModel } from '@sre/types/LLM.types';
|
|
26
|
+
import config from '@sre/config';
|
|
27
|
+
import { ModelsProviderConnector } from '@sre/LLMManager/ModelsProvider.service/ModelsProviderConnector';
|
|
28
|
+
import { getLLMCredentials } from './LLMCredentials.helper';
|
|
29
|
+
|
|
30
|
+
const console = Logger('LLMConnector');
|
|
31
|
+
|
|
32
|
+
export interface ILLMConnectorRequest {
|
|
33
|
+
// chatRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
34
|
+
// visionRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
35
|
+
// multimodalRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
36
|
+
// toolRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
37
|
+
|
|
38
|
+
request(params: TLLMConnectorParams): Promise<TLLMChatResponse>;
|
|
39
|
+
streamRequest(params: TLLMConnectorParams): Promise<EventEmitter>;
|
|
40
|
+
|
|
41
|
+
imageGenRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
42
|
+
imageEditRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any>;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export class LLMStream extends Readable {
|
|
46
|
+
private dataQueue: any[];
|
|
47
|
+
private toolsData: any[];
|
|
48
|
+
private hasData: boolean;
|
|
49
|
+
isReading: boolean;
|
|
50
|
+
constructor(options?) {
|
|
51
|
+
super(options);
|
|
52
|
+
this.dataQueue = [];
|
|
53
|
+
this.toolsData = [];
|
|
54
|
+
this.isReading = true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
_read(size) {
|
|
58
|
+
if (this.dataQueue.length > 0) {
|
|
59
|
+
while (this.dataQueue.length > 0) {
|
|
60
|
+
const chunk = this.dataQueue.shift();
|
|
61
|
+
if (!this.push(chunk)) {
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
this.push(null); // No more data
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
enqueueData(data) {
|
|
71
|
+
this.dataQueue.push(data);
|
|
72
|
+
this.read(0); // Trigger the _read method
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
endStream() {
|
|
76
|
+
this.isReading = false;
|
|
77
|
+
this.push(null); // End the stream
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export abstract class LLMConnector extends Connector {
|
|
82
|
+
public abstract name: string;
|
|
83
|
+
|
|
84
|
+
protected abstract request({ acRequest, body, context }: ILLMRequestFuncParams): Promise<TLLMChatResponse>;
|
|
85
|
+
protected abstract streamRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<EventEmitter>;
|
|
86
|
+
protected abstract webSearchRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<EventEmitter>;
|
|
87
|
+
|
|
88
|
+
protected abstract reqBodyAdapter(params: TLLMConnectorParams): Promise<TLLMRequestBody>;
|
|
89
|
+
protected abstract reportUsage(usage: any, metadata: { modelEntryName: string; keySource: APIKeySource; agentId: string; teamId: string }): any;
|
|
90
|
+
|
|
91
|
+
// Optional method - default implementation throws error. (It's a workaround. We will move image related methods to another subsystem.)
|
|
92
|
+
protected imageGenRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any> {
|
|
93
|
+
return Promise.reject(new Error('Image edit not supported by this model'));
|
|
94
|
+
}
|
|
95
|
+
protected imageEditRequest({ acRequest, body, context }: ILLMRequestFuncParams): Promise<any> {
|
|
96
|
+
return Promise.reject(new Error('Image edit not supported by this model'));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
private vaultConnector: VaultConnector;
|
|
100
|
+
|
|
101
|
+
public requester(candidate: AccessCandidate): ILLMConnectorRequest {
|
|
102
|
+
//if (candidate.role !== 'agent') throw new Error('Only agents can use LLM connector');
|
|
103
|
+
|
|
104
|
+
this.vaultConnector = ConnectorService.getVaultConnector();
|
|
105
|
+
|
|
106
|
+
if (!this.vaultConnector || !this.vaultConnector.valid) {
|
|
107
|
+
console.warn(`Vault Connector unavailable for ${candidate.id} `);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
const _request: ILLMConnectorRequest = {
|
|
111
|
+
request: async (params: TLLMConnectorParams) => {
|
|
112
|
+
const preparedParams = await this.prepareParams(candidate, params);
|
|
113
|
+
|
|
114
|
+
const response = await this.request({
|
|
115
|
+
acRequest: candidate.readRequest,
|
|
116
|
+
body: preparedParams.body,
|
|
117
|
+
context: {
|
|
118
|
+
modelEntryName: preparedParams.modelEntryName,
|
|
119
|
+
agentId: preparedParams.agentId,
|
|
120
|
+
teamId: preparedParams.teamId,
|
|
121
|
+
isUserKey: preparedParams.isUserKey,
|
|
122
|
+
hasFiles: preparedParams.files?.length > 0,
|
|
123
|
+
modelInfo: preparedParams.modelInfo,
|
|
124
|
+
credentials: preparedParams.credentials,
|
|
125
|
+
},
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
return response;
|
|
129
|
+
},
|
|
130
|
+
streamRequest: async (params: TLLMConnectorParams) => {
|
|
131
|
+
const preparedParams = await this.prepareParams(candidate, params);
|
|
132
|
+
|
|
133
|
+
const requestParams = {
|
|
134
|
+
acRequest: candidate.readRequest,
|
|
135
|
+
body: preparedParams.body,
|
|
136
|
+
context: {
|
|
137
|
+
modelEntryName: preparedParams.modelEntryName,
|
|
138
|
+
agentId: preparedParams.agentId,
|
|
139
|
+
teamId: preparedParams.teamId,
|
|
140
|
+
isUserKey: preparedParams.isUserKey,
|
|
141
|
+
hasFiles: preparedParams.files?.length > 0,
|
|
142
|
+
modelInfo: preparedParams.modelInfo,
|
|
143
|
+
credentials: preparedParams.credentials,
|
|
144
|
+
},
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
let response;
|
|
148
|
+
|
|
149
|
+
if (preparedParams.capabilities?.search === true && preparedParams.useWebSearch === true) {
|
|
150
|
+
response = await this.webSearchRequest(requestParams);
|
|
151
|
+
} else {
|
|
152
|
+
response = await this.streamRequest(requestParams);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return response;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
imageGenRequest: async (params: any) => {
|
|
159
|
+
const preparedParams = await this.prepareParams(candidate, params);
|
|
160
|
+
|
|
161
|
+
const response = await this.imageGenRequest({
|
|
162
|
+
acRequest: candidate.readRequest,
|
|
163
|
+
body: preparedParams.body,
|
|
164
|
+
context: {
|
|
165
|
+
modelEntryName: preparedParams.modelEntryName,
|
|
166
|
+
isUserKey: preparedParams.isUserKey,
|
|
167
|
+
agentId: preparedParams.agentId,
|
|
168
|
+
teamId: preparedParams.teamId,
|
|
169
|
+
hasFiles: preparedParams.files?.length > 0,
|
|
170
|
+
modelInfo: preparedParams.modelInfo,
|
|
171
|
+
credentials: preparedParams.credentials,
|
|
172
|
+
},
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
return response;
|
|
176
|
+
},
|
|
177
|
+
imageEditRequest: async (params: any) => {
|
|
178
|
+
const preparedParams = await this.prepareParams(candidate, params);
|
|
179
|
+
|
|
180
|
+
const response = await this.imageEditRequest({
|
|
181
|
+
acRequest: candidate.readRequest,
|
|
182
|
+
body: preparedParams.body,
|
|
183
|
+
context: {
|
|
184
|
+
modelEntryName: preparedParams.modelEntryName,
|
|
185
|
+
isUserKey: preparedParams.isUserKey,
|
|
186
|
+
agentId: preparedParams.agentId,
|
|
187
|
+
teamId: preparedParams.teamId,
|
|
188
|
+
hasFiles: preparedParams.files?.length > 0,
|
|
189
|
+
modelInfo: preparedParams.modelInfo,
|
|
190
|
+
credentials: preparedParams.credentials,
|
|
191
|
+
},
|
|
192
|
+
});
|
|
193
|
+
|
|
194
|
+
return response;
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return _request;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
public enhancePrompt(prompt: string, config: any) {
|
|
202
|
+
if (!prompt) return prompt;
|
|
203
|
+
let newPrompt = prompt;
|
|
204
|
+
const outputs = {};
|
|
205
|
+
|
|
206
|
+
if (config?.outputs) {
|
|
207
|
+
for (let con of config.outputs) {
|
|
208
|
+
if (con.default) continue;
|
|
209
|
+
outputs[con.name] = con?.description ? ` (${con?.description})` : '';
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const excludedKeys = ['_debug', '_error'];
|
|
214
|
+
const outputKeys = Object.keys(outputs).filter((key) => !excludedKeys.includes(key));
|
|
215
|
+
|
|
216
|
+
if (outputKeys.length > 0) {
|
|
217
|
+
const outputFormat = {};
|
|
218
|
+
outputKeys.forEach((key) => (outputFormat[key] = (config.name === 'Classifier' ? '<Boolean|String>' : '<value>') + (outputs[key] || '')));
|
|
219
|
+
|
|
220
|
+
newPrompt +=
|
|
221
|
+
'\n##\nExpected output format = ' +
|
|
222
|
+
JSON.stringify(outputFormat) +
|
|
223
|
+
'\nThe output JSON should only use the entries from the output format.';
|
|
224
|
+
|
|
225
|
+
//console.debug(` Enhanced prompt \n`, prompt, '\n');
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return newPrompt;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
public postProcess(response: string) {
|
|
232
|
+
try {
|
|
233
|
+
return JSONContent(response).tryParse();
|
|
234
|
+
} catch (error) {
|
|
235
|
+
return {
|
|
236
|
+
error: 'Invalid JSON response',
|
|
237
|
+
data: response,
|
|
238
|
+
details: 'The response from the model is not a valid JSON object. Please check the model output and try again.',
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
}
|
|
242
|
+
public formatToolsConfig({ type = 'function', toolDefinitions, toolChoice = 'auto' }) {
|
|
243
|
+
throw new Error('This model does not support tools');
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
public transformToolMessageBlocks({
|
|
247
|
+
messageBlock,
|
|
248
|
+
toolsData,
|
|
249
|
+
}: {
|
|
250
|
+
messageBlock: TLLMMessageBlock;
|
|
251
|
+
toolsData: ToolData[];
|
|
252
|
+
}): TLLMToolResultMessageBlock[] {
|
|
253
|
+
throw new Error('This model does not support tools');
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
public getConsistentMessages(messages: TLLMMessageBlock[]) {
|
|
257
|
+
return messages; // if a LLM connector does not implement this method, the messages will not be modified
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
private async prepareParams(candidate: AccessCandidate, params: TLLMConnectorParams): Promise<TLLMConnectorParams & { body: any }> {
|
|
261
|
+
const modelsProvider: ModelsProviderConnector = ConnectorService.getModelsProviderConnector();
|
|
262
|
+
// Assign file from the original parameters to avoid overwriting the original constructor
|
|
263
|
+
const files = params?.files;
|
|
264
|
+
delete params?.files; // need to remove files to avoid any issues during JSON.stringify() especially when we have large files
|
|
265
|
+
|
|
266
|
+
const clonedParams = JSON.parse(JSON.stringify(params)); // Avoid mutation of the original params
|
|
267
|
+
|
|
268
|
+
// Format the parameters to ensure proper type of values
|
|
269
|
+
const _params: TLLMConnectorParams = this.formatParamValues(clonedParams);
|
|
270
|
+
|
|
271
|
+
const model = _params.model;
|
|
272
|
+
const teamId = await this.getTeamId(candidate);
|
|
273
|
+
|
|
274
|
+
// We need the model entry name for usage reporting
|
|
275
|
+
_params.modelEntryName = typeof model === 'string' ? model : (model as TLLMModel).modelId;
|
|
276
|
+
_params.teamId = teamId;
|
|
277
|
+
|
|
278
|
+
const modelProviderCandidate = modelsProvider.requester(candidate);
|
|
279
|
+
const modelInfo: TLLMModel | TCustomLLMModel = await modelProviderCandidate.getModelInfo(model);
|
|
280
|
+
|
|
281
|
+
//if the model has default params make sure to set them if they are not present
|
|
282
|
+
if (modelInfo.params) {
|
|
283
|
+
for (let key in modelInfo.params) {
|
|
284
|
+
if (typeof _params[key] === 'undefined') {
|
|
285
|
+
_params[key] = modelInfo.params[key];
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const isStandardLLM = await modelProviderCandidate.isStandardLLM(model);
|
|
291
|
+
|
|
292
|
+
const llmProvider = await modelProviderCandidate.getProvider(model);
|
|
293
|
+
|
|
294
|
+
_params.credentials = await getLLMCredentials(candidate, modelInfo);
|
|
295
|
+
|
|
296
|
+
//_params.model = (await modelProviderCandidate.getModelId(model)) || model;
|
|
297
|
+
|
|
298
|
+
_params.baseURL = modelInfo?.baseURL;
|
|
299
|
+
// if (!isStandardLLM) _params.modelInfo = modelInfo as TCustomLLMModel; //only if custom LLM ?
|
|
300
|
+
_params.modelInfo = modelInfo as TCustomLLMModel; // We need model info for both standard and custom LLMs
|
|
301
|
+
|
|
302
|
+
if (_params.maxTokens) {
|
|
303
|
+
_params.maxTokens = await modelProviderCandidate.adjustMaxCompletionTokens(model, _params.maxTokens, _params?.isUserKey as boolean);
|
|
304
|
+
} else {
|
|
305
|
+
// max output tokens is mandatory for Anthropic models
|
|
306
|
+
// We provide the default max output tokens here to avoid some complexity in reqBodyAdapter()
|
|
307
|
+
_params.maxTokens = await modelProviderCandidate.getMaxCompletionTokens(model, _params?.isUserKey as boolean);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
_params.model = await modelProviderCandidate.getModelId(model);
|
|
311
|
+
// Attach the files again after formatting the parameters
|
|
312
|
+
_params.files = files;
|
|
313
|
+
|
|
314
|
+
const features = modelInfo?.features || [];
|
|
315
|
+
|
|
316
|
+
_params.capabilities = {
|
|
317
|
+
search: features.includes('search'),
|
|
318
|
+
reasoning: features.includes('reasoning'),
|
|
319
|
+
imageGeneration: features.includes('image-generation'),
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// The input adapter transforms the standardized parameters into the specific format required by the target LLM provider
|
|
323
|
+
_params.agentId = candidate.id;
|
|
324
|
+
const body = await this.reqBodyAdapter(_params);
|
|
325
|
+
|
|
326
|
+
return { ..._params, body };
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// TODO [Forhad]: apply proper typing for _value and return value
|
|
330
|
+
private formatParamValues(params: Record<string, string | number | TLLMMessageBlock[]>): any {
|
|
331
|
+
let _params = {};
|
|
332
|
+
|
|
333
|
+
for (const [key, value] of Object.entries(params)) {
|
|
334
|
+
let _value: any = value;
|
|
335
|
+
|
|
336
|
+
// When we have stopSequences, we need to split it into an array
|
|
337
|
+
if (key === 'stopSequences') {
|
|
338
|
+
_value = _value ? _value?.split(',') : null;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
// When we have a string that is a number, we need to convert it to a number
|
|
342
|
+
if (typeof _value === 'string' && !isNaN(Number(_value))) {
|
|
343
|
+
_value = +_value;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
//FIXME: to revisit by Alaa-eddine
|
|
347
|
+
if (key === 'messages') {
|
|
348
|
+
_value = this.getConsistentMessages(_value);
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
_params[key] = _value;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
return _params;
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Retrieves the team ID associated with the given access candidate
|
|
359
|
+
* @param candidate - The access candidate whose team ID needs to be retrieved
|
|
360
|
+
* @returns Promise<string> - The unique identifier of the team associated with the candidate
|
|
361
|
+
* @throws {Error} If the Account Connector service is unavailable or cannot be accessed
|
|
362
|
+
* @throws {Error} If the candidate's team cannot be retrieved
|
|
363
|
+
* @private
|
|
364
|
+
* @remarks This method is used internally to determine the team context for custom LLM operations
|
|
365
|
+
*/
|
|
366
|
+
private async getTeamId(candidate: AccessCandidate): Promise<string> {
|
|
367
|
+
const accountConnector: AccountConnector = ConnectorService.getAccountConnector();
|
|
368
|
+
|
|
369
|
+
if (!accountConnector) throw new Error('Account Connector unavailable, cannot proceed');
|
|
370
|
+
|
|
371
|
+
const teamId = await accountConnector.getCandidateTeam(candidate);
|
|
372
|
+
|
|
373
|
+
return teamId;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import config from '@sre/config';
|
|
2
|
+
import { ConnectorService } from '@sre/Core/ConnectorsService';
|
|
3
|
+
import { AccessCandidate } from '@sre/Security/AccessControl/AccessCandidate.class';
|
|
4
|
+
import { TBedrockSettings, TCustomLLMModel, TLLMCredentials, TLLMModel, TVertexAISettings } from '@sre/types/LLM.types';
|
|
5
|
+
|
|
6
|
+
export async function getLLMCredentials(candidate: AccessCandidate, modelInfo: TLLMModel | TCustomLLMModel) {
|
|
7
|
+
//create a credentials list that we can iterate over
|
|
8
|
+
//if the credentials are not provided, we will use None as a default in order to return empty credentials
|
|
9
|
+
const credentialsList: any[] = !Array.isArray(modelInfo.credentials) ? [modelInfo.credentials] : modelInfo.credentials || [TLLMCredentials.None];
|
|
10
|
+
|
|
11
|
+
for (let credentialsMode of credentialsList) {
|
|
12
|
+
if (typeof credentialsMode === 'object') {
|
|
13
|
+
//credentials passed directly
|
|
14
|
+
return credentialsMode;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
switch (credentialsMode) {
|
|
18
|
+
case TLLMCredentials.None: {
|
|
19
|
+
return { apiKey: '' };
|
|
20
|
+
}
|
|
21
|
+
case TLLMCredentials.Internal:
|
|
22
|
+
case TLLMCredentials.Vault: {
|
|
23
|
+
const credentials = await getStandardLLMCredentials(candidate, modelInfo as TLLMModel);
|
|
24
|
+
if (credentials) return credentials;
|
|
25
|
+
break;
|
|
26
|
+
}
|
|
27
|
+
case TLLMCredentials.BedrockVault: {
|
|
28
|
+
const credentials = await getBedrockCredentials(candidate, modelInfo as TCustomLLMModel);
|
|
29
|
+
if (credentials) return credentials;
|
|
30
|
+
break;
|
|
31
|
+
}
|
|
32
|
+
case TLLMCredentials.VertexAIVault: {
|
|
33
|
+
const credentials = await getVertexAICredentials(candidate, modelInfo as TCustomLLMModel);
|
|
34
|
+
if (credentials) return credentials;
|
|
35
|
+
break;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Retrieves API key credentials for standard LLM providers from the vault
|
|
45
|
+
* @param candidate - The access candidate requesting the credentials
|
|
46
|
+
* @param provider - The LLM provider name (e.g., 'openai', 'anthropic')
|
|
47
|
+
* @returns Promise resolving to an object containing the provider's API key
|
|
48
|
+
* @throws {Error} If vault connector is unavailable (handled in parent method)
|
|
49
|
+
* @remarks Returns an empty string as API key if vault access fails
|
|
50
|
+
* @private
|
|
51
|
+
*/
|
|
52
|
+
async function getStandardLLMCredentials(candidate: AccessCandidate, modelInfo: TLLMModel): Promise<{ apiKey: string; isUserKey: boolean }> {
|
|
53
|
+
const provider = (modelInfo.provider || modelInfo.llm)?.toLowerCase();
|
|
54
|
+
const vaultConnector = ConnectorService.getVaultConnector();
|
|
55
|
+
|
|
56
|
+
const apiKey = await vaultConnector
|
|
57
|
+
.requester(candidate)
|
|
58
|
+
.get(provider)
|
|
59
|
+
.catch(() => '');
|
|
60
|
+
|
|
61
|
+
if (!apiKey) return null;
|
|
62
|
+
return { apiKey, isUserKey: true };
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Retrieves AWS Bedrock credentials from the vault for authentication
|
|
67
|
+
* @param candidate - The access candidate requesting the credentials
|
|
68
|
+
* @param modelInfo - The Bedrock model information containing credential key names in settings
|
|
69
|
+
* @returns Promise resolving to AWS credentials object
|
|
70
|
+
* @returns {Promise<Object>} credentials
|
|
71
|
+
* @returns {string} credentials.accessKeyId - AWS access key ID
|
|
72
|
+
* @returns {string} credentials.secretAccessKey - AWS secret access key
|
|
73
|
+
* @returns {string} [credentials.sessionToken] - Optional AWS session token
|
|
74
|
+
* @throws {Error} If vault connector is unavailable (handled in parent method)
|
|
75
|
+
* @private
|
|
76
|
+
*/
|
|
77
|
+
async function getBedrockCredentials(
|
|
78
|
+
candidate: AccessCandidate,
|
|
79
|
+
modelInfo: TCustomLLMModel
|
|
80
|
+
): Promise<{ accessKeyId: string; secretAccessKey: string; sessionToken?: string; isUserKey: boolean }> {
|
|
81
|
+
const keyIdName = (modelInfo.settings as TBedrockSettings)?.keyIDName;
|
|
82
|
+
const secretKeyName = (modelInfo.settings as TBedrockSettings)?.secretKeyName;
|
|
83
|
+
const sessionKeyName = (modelInfo.settings as TBedrockSettings)?.sessionKeyName;
|
|
84
|
+
|
|
85
|
+
const vaultConnector = ConnectorService.getVaultConnector();
|
|
86
|
+
|
|
87
|
+
const [accessKeyId, secretAccessKey, sessionToken] = await Promise.all([
|
|
88
|
+
vaultConnector
|
|
89
|
+
.requester(candidate)
|
|
90
|
+
.get(keyIdName)
|
|
91
|
+
.catch(() => ''),
|
|
92
|
+
vaultConnector
|
|
93
|
+
.requester(candidate)
|
|
94
|
+
.get(secretKeyName)
|
|
95
|
+
.catch(() => ''),
|
|
96
|
+
vaultConnector
|
|
97
|
+
.requester(candidate)
|
|
98
|
+
.get(sessionKeyName)
|
|
99
|
+
.catch(() => ''),
|
|
100
|
+
]);
|
|
101
|
+
|
|
102
|
+
let credentials: {
|
|
103
|
+
accessKeyId: string;
|
|
104
|
+
secretAccessKey: string;
|
|
105
|
+
sessionToken?: string;
|
|
106
|
+
isUserKey: boolean;
|
|
107
|
+
} = {
|
|
108
|
+
accessKeyId,
|
|
109
|
+
secretAccessKey,
|
|
110
|
+
isUserKey: true,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
if (sessionToken) {
|
|
114
|
+
credentials.sessionToken = sessionToken;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!accessKeyId || !secretAccessKey) return null;
|
|
118
|
+
return credentials;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Retrieves the credentials required for VertexAI authentication from the vault
|
|
123
|
+
* @param candidate - The access candidate requesting the credentials
|
|
124
|
+
* @param modelInfo - The VertexAI model information containing settings
|
|
125
|
+
* @returns Promise resolving to the parsed JSON credentials for VertexAI
|
|
126
|
+
* @throws {Error} If vault connector is unavailable (handled in parent method)
|
|
127
|
+
* @throws {Error} If JSON parsing fails or credentials are malformed
|
|
128
|
+
* @remarks Returns empty credentials if vault access fails
|
|
129
|
+
* @private
|
|
130
|
+
*/
|
|
131
|
+
async function getVertexAICredentials(candidate: AccessCandidate, modelInfo: TCustomLLMModel): Promise<any> {
|
|
132
|
+
const jsonCredentialsName = (modelInfo.settings as TVertexAISettings)?.jsonCredentialsName;
|
|
133
|
+
|
|
134
|
+
const vaultConnector = ConnectorService.getVaultConnector();
|
|
135
|
+
|
|
136
|
+
let jsonCredentials = await vaultConnector
|
|
137
|
+
.requester(candidate)
|
|
138
|
+
.get(jsonCredentialsName)
|
|
139
|
+
.catch(() => '');
|
|
140
|
+
|
|
141
|
+
const credentials = JSON.parse(jsonCredentials);
|
|
142
|
+
|
|
143
|
+
if (!credentials) return null;
|
|
144
|
+
return { ...credentials, isUserKey: true };
|
|
145
|
+
}
|