@smythos/sre 1.6.1 → 1.6.9
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/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/types/Components/Triggers/Gmail.trigger.d.ts +58 -0
- package/dist/types/Components/Triggers/GmailTrigger.class.d.ts +40 -9
- package/dist/types/Components/Triggers/Trigger.class.d.ts +18 -0
- package/dist/types/Components/Triggers/WhatsApp.trigger.d.ts +22 -0
- package/dist/types/subsystems/Security/Credentials/Credentials.class.d.ts +2 -0
- package/dist/types/subsystems/Security/Credentials/ManagedOAuth2Credentials.class.d.ts +18 -0
- package/dist/types/subsystems/Security/Credentials/OAuth2Credentials.class.d.ts +14 -0
- package/package.json +1 -1
- package/src/helpers/AWSLambdaCode.helper.ts +40 -15
- package/src/index.ts +195 -195
- package/src/index.ts.bak +195 -195
- package/src/subsystems/ComputeManager/Code.service/connectors/AWSLambdaCode.class.ts +1 -2
- package/src/subsystems/LLMManager/ModelsProvider.service/connectors/JSONModelsProvider.class.ts +8 -2
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { IAgent as Agent } from '@sre/types/Agent.types';
|
|
2
|
+
import { Trigger } from './Trigger.class';
|
|
3
|
+
export type GmailTriggerAttachment = {
|
|
4
|
+
filename: string;
|
|
5
|
+
mimeType: string;
|
|
6
|
+
size: number;
|
|
7
|
+
attachmentId: string;
|
|
8
|
+
};
|
|
9
|
+
export type GmailTriggerMessage = {
|
|
10
|
+
id: string;
|
|
11
|
+
threadId: string;
|
|
12
|
+
labelIds: string[];
|
|
13
|
+
snippet: string;
|
|
14
|
+
sizeEstimate: number;
|
|
15
|
+
internalDate: string;
|
|
16
|
+
headers: {
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
cc: string;
|
|
20
|
+
bcc: string;
|
|
21
|
+
subject: string;
|
|
22
|
+
date: string;
|
|
23
|
+
messageId: string;
|
|
24
|
+
};
|
|
25
|
+
body: {
|
|
26
|
+
text: string;
|
|
27
|
+
html: string;
|
|
28
|
+
};
|
|
29
|
+
attachments: GmailTriggerAttachment[];
|
|
30
|
+
isUnread: true;
|
|
31
|
+
};
|
|
32
|
+
export declare class GmailTrigger extends Trigger {
|
|
33
|
+
collectPayload(input: any, config: any, agent: Agent): Promise<{
|
|
34
|
+
email: {
|
|
35
|
+
id: any;
|
|
36
|
+
threadId: any;
|
|
37
|
+
labelIds: any;
|
|
38
|
+
snippet: any;
|
|
39
|
+
sizeEstimate: any;
|
|
40
|
+
internalDate: string;
|
|
41
|
+
headers: {
|
|
42
|
+
from: any;
|
|
43
|
+
to: any;
|
|
44
|
+
cc: any;
|
|
45
|
+
bcc: any;
|
|
46
|
+
subject: any;
|
|
47
|
+
date: any;
|
|
48
|
+
messageId: any;
|
|
49
|
+
};
|
|
50
|
+
body: {
|
|
51
|
+
text: string;
|
|
52
|
+
html: string;
|
|
53
|
+
};
|
|
54
|
+
attachments: any[];
|
|
55
|
+
isUnread: any;
|
|
56
|
+
};
|
|
57
|
+
}[]>;
|
|
58
|
+
}
|
|
@@ -1,13 +1,44 @@
|
|
|
1
1
|
import { IAgent as Agent } from '@sre/types/Agent.types';
|
|
2
2
|
import { Trigger } from './Trigger.class';
|
|
3
|
+
export type GmailTriggerAttachment = {
|
|
4
|
+
filename: string;
|
|
5
|
+
mimeType: string;
|
|
6
|
+
size: number;
|
|
7
|
+
attachmentId: string;
|
|
8
|
+
};
|
|
9
|
+
export type GmailTriggerMessage = {
|
|
10
|
+
id: string;
|
|
11
|
+
threadId: string;
|
|
12
|
+
labelIds: string[];
|
|
13
|
+
snippet: string;
|
|
14
|
+
sizeEstimate: number;
|
|
15
|
+
internalDate: string;
|
|
16
|
+
headers: {
|
|
17
|
+
from: string;
|
|
18
|
+
to: string;
|
|
19
|
+
cc: string;
|
|
20
|
+
bcc: string;
|
|
21
|
+
subject: string;
|
|
22
|
+
date: string;
|
|
23
|
+
messageId: string;
|
|
24
|
+
};
|
|
25
|
+
body: {
|
|
26
|
+
text: string;
|
|
27
|
+
html: string;
|
|
28
|
+
};
|
|
29
|
+
attachments: GmailTriggerAttachment[];
|
|
30
|
+
isUnread: true;
|
|
31
|
+
};
|
|
3
32
|
export declare class GmailTrigger extends Trigger {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
33
|
+
collectPayload(input: any, config: any, agent: Agent): Promise<{
|
|
34
|
+
message: {
|
|
35
|
+
id: string;
|
|
36
|
+
subject: string;
|
|
37
|
+
body: string;
|
|
38
|
+
from: string;
|
|
39
|
+
to: string;
|
|
40
|
+
date: string;
|
|
41
|
+
threadId: string;
|
|
42
|
+
};
|
|
43
|
+
}[]>;
|
|
13
44
|
}
|
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
import { Agent } from '@sre/AgentManager/Agent.class';
|
|
1
2
|
import { Component } from '../Component.class';
|
|
3
|
+
import { LogHelper } from '@sre/helpers/Log.helper';
|
|
2
4
|
export declare class Trigger extends Component {
|
|
5
|
+
protected logger: LogHelper;
|
|
6
|
+
process(input: any, settings: any, agent: Agent): Promise<any>;
|
|
7
|
+
protected collectPayload(input: any, settings: any, agent: Agent): Promise<any[]>;
|
|
8
|
+
requestHandler(input: any, settings: any, agent: Agent): Promise<any>;
|
|
9
|
+
protected processIteration(inputArray: any, settings: any, agent: any): Promise<{
|
|
10
|
+
Payload: {};
|
|
11
|
+
Result: any;
|
|
12
|
+
_temp_result: any;
|
|
13
|
+
_error: any;
|
|
14
|
+
_in_progress: boolean;
|
|
15
|
+
_debug: string;
|
|
16
|
+
}>;
|
|
17
|
+
postProcess(output: any, settings: any, agent: Agent): Promise<any>;
|
|
18
|
+
register(componentId: string, componentSettings: any, payload: {
|
|
19
|
+
triggerUrl: string;
|
|
20
|
+
}): Promise<void>;
|
|
3
21
|
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { IAgent as Agent } from '@sre/types/Agent.types';
|
|
2
|
+
import { Trigger } from './Trigger.class';
|
|
3
|
+
type WhatsAppIncomingMessage = {
|
|
4
|
+
id?: string;
|
|
5
|
+
from?: string;
|
|
6
|
+
profileName?: string;
|
|
7
|
+
text?: string;
|
|
8
|
+
type?: string;
|
|
9
|
+
timestamp?: string;
|
|
10
|
+
phoneNumberId?: string;
|
|
11
|
+
};
|
|
12
|
+
export declare class WhatsAppTrigger extends Trigger {
|
|
13
|
+
collectPayload(input: any, settings: any, agent: Agent): Promise<{
|
|
14
|
+
message: WhatsAppIncomingMessage;
|
|
15
|
+
}[]>;
|
|
16
|
+
requestHandler(input: any, settings: any, agent: Agent): Promise<boolean>;
|
|
17
|
+
register(componentId: string, componentSettings: any, payload?: {
|
|
18
|
+
triggerUrl: string;
|
|
19
|
+
}): Promise<void>;
|
|
20
|
+
unregister(componentId: string, agent: Agent, payload?: any): Promise<void>;
|
|
21
|
+
}
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { Credentials } from './Credentials.class';
|
|
2
|
+
import { AccessCandidate } from '../AccessControl/AccessCandidate.class';
|
|
3
|
+
export declare class ManagedOAuth2Credentials extends Credentials {
|
|
4
|
+
#private;
|
|
5
|
+
get accessToken(): string;
|
|
6
|
+
get refreshToken(): string;
|
|
7
|
+
get expiresIn(): number;
|
|
8
|
+
get scope(): string;
|
|
9
|
+
get tokenUrl(): string;
|
|
10
|
+
get service(): string;
|
|
11
|
+
private constructor();
|
|
12
|
+
static load(keyId: string, candidate: AccessCandidate): Promise<ManagedOAuth2Credentials>;
|
|
13
|
+
private parseData;
|
|
14
|
+
/**
|
|
15
|
+
* Get a fresh access token using the refresh token
|
|
16
|
+
*/
|
|
17
|
+
refreshAccessToken(): Promise<string>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Credentials } from './Credentials.class';
|
|
2
|
+
import { AccessCandidate } from '../AccessControl/AccessCandidate.class';
|
|
3
|
+
export declare class OAuth2Credentials extends Credentials {
|
|
4
|
+
#private;
|
|
5
|
+
accessToken: string;
|
|
6
|
+
refreshToken: string;
|
|
7
|
+
expiresIn: number;
|
|
8
|
+
scope: string;
|
|
9
|
+
tokenUrl: string;
|
|
10
|
+
service: string;
|
|
11
|
+
constructor(data: any);
|
|
12
|
+
static fromManagedVault(keyId: string, candidate: AccessCandidate): Promise<OAuth2Credentials>;
|
|
13
|
+
private parseData;
|
|
14
|
+
}
|
package/package.json
CHANGED
|
@@ -187,6 +187,7 @@ export async function createOrUpdateLambdaFunction(functionName, zipFilePath, aw
|
|
|
187
187
|
AssumeRolePolicyDocument: getLambdaRolePolicy(),
|
|
188
188
|
});
|
|
189
189
|
const roleResponse: CreateRoleCommandOutput = await iamClient.send(createRoleCommand);
|
|
190
|
+
|
|
190
191
|
await waitForRoleDeploymentStatus(`smyth-${functionName}-role`, iamClient);
|
|
191
192
|
roleArn = roleResponse.Role.Arn;
|
|
192
193
|
} else {
|
|
@@ -208,14 +209,9 @@ export async function createOrUpdateLambdaFunction(functionName, zipFilePath, aw
|
|
|
208
209
|
MemorySize: 256,
|
|
209
210
|
...(envVariables && Object.keys(envVariables).length ? { Environment: { Variables: envVariables } } : {}),
|
|
210
211
|
};
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
await client.send(functionCreateCommand);
|
|
214
|
-
// console.log('Function ARN:', functionResponse.FunctionArn);
|
|
212
|
+
// Retry mechanism for Lambda function creation with exponential backoff
|
|
213
|
+
await createLambdaFunction(client, functionParams);
|
|
215
214
|
await verifyFunctionDeploymentStatus(functionName, client);
|
|
216
|
-
// wait 500 ms to let the function trust policy be applied
|
|
217
|
-
// it will only occur when the function is created for the first time
|
|
218
|
-
await new Promise((resolve) => setTimeout(resolve, 500));
|
|
219
215
|
}
|
|
220
216
|
} catch (error) {
|
|
221
217
|
throw error;
|
|
@@ -233,20 +229,49 @@ function updateLambdaFunctionConfiguration(client: LambdaClient, functionName: s
|
|
|
233
229
|
return client.send(updateFunctionConfigurationCommand);
|
|
234
230
|
}
|
|
235
231
|
|
|
232
|
+
async function createLambdaFunction(client: LambdaClient, functionParams: any, maxRetries: number = 5): Promise<void> {
|
|
233
|
+
let lastError: Error | null = null;
|
|
234
|
+
|
|
235
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
236
|
+
try {
|
|
237
|
+
const functionCreateCommand = new CreateFunctionCommand(functionParams);
|
|
238
|
+
await client.send(functionCreateCommand);
|
|
239
|
+
return; // Success, exit the retry loop
|
|
240
|
+
} catch (error) {
|
|
241
|
+
lastError = error;
|
|
242
|
+
// Check if this is a role trust policy error
|
|
243
|
+
if (error?.message?.includes('cannot be assumed by Lambda')) {
|
|
244
|
+
|
|
245
|
+
if (attempt < maxRetries) {
|
|
246
|
+
// Exponential backoff: 2^attempt seconds (2, 4, 8, 16, 32 seconds)
|
|
247
|
+
const waitTime = Math.pow(2, attempt) * 1000;
|
|
248
|
+
await new Promise(resolve => setTimeout(resolve, waitTime));
|
|
249
|
+
continue;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
// For other errors or if we've exhausted retries, throw immediately
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// If we get here, all retries failed
|
|
259
|
+
throw lastError || new Error('Lambda function creation failed after all retry attempts');
|
|
260
|
+
}
|
|
261
|
+
|
|
236
262
|
export async function waitForRoleDeploymentStatus(roleName, client): Promise<boolean> {
|
|
237
263
|
return new Promise((resolve, reject) => {
|
|
238
|
-
|
|
239
|
-
let interval = setInterval(async () => {
|
|
264
|
+
const interval = setInterval(async () => {
|
|
240
265
|
const getRoleCommand = new GetRoleCommand({ RoleName: roleName });
|
|
241
266
|
const roleResponse = await client.send(getRoleCommand);
|
|
242
|
-
|
|
267
|
+
|
|
268
|
+
// Check if role exists and has assume role policy document
|
|
269
|
+
if (roleResponse.Role && roleResponse.Role.AssumeRolePolicyDocument) {
|
|
243
270
|
clearInterval(interval);
|
|
244
|
-
|
|
271
|
+
setTimeout(() => resolve(true), 2000);
|
|
272
|
+
return;
|
|
245
273
|
}
|
|
246
|
-
|
|
247
|
-
} catch (error) {
|
|
248
|
-
return false;
|
|
249
|
-
}
|
|
274
|
+
}, 2000); // Check every 2 seconds
|
|
250
275
|
});
|
|
251
276
|
}
|
|
252
277
|
|