@memberjunction/actions 2.50.0 → 2.52.0
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/generic/ActionEngine.d.ts +2 -6
- package/dist/generic/ActionEngine.d.ts.map +1 -1
- package/dist/generic/ActionEngine.js +2 -20
- package/dist/generic/ActionEngine.js.map +1 -1
- package/dist/generic/BaseAction.d.ts +38 -0
- package/dist/generic/BaseAction.d.ts.map +1 -1
- package/dist/generic/BaseAction.js +31 -0
- package/dist/generic/BaseAction.js.map +1 -1
- package/package.json +8 -8
- package/readme.md +141 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ActionExecutionLogEntity, ActionFilterEntity } from "@memberjunction/core-entities";
|
|
2
|
-
import { ActionEngineBase, ActionEntityExtended, ActionParam, ActionResult,
|
|
2
|
+
import { ActionEngineBase, ActionEntityExtended, ActionParam, ActionResult, RunActionParams } from "@memberjunction/actions-base";
|
|
3
3
|
/**
|
|
4
4
|
* Base class for executing actions. This class can be sub-classed if desired if you would like to modify the logic across ALL actions. To do so, sub-class this class and use the
|
|
5
5
|
* @RegisterClass decorator from the @memberjunction/global package to register your sub-class with the ClassFactory. This will cause your sub-class to be used instead of this base class when the Metadata object insantiates the ActionEngine.
|
|
@@ -7,11 +7,6 @@ import { ActionEngineBase, ActionEntityExtended, ActionParam, ActionResult, RunA
|
|
|
7
7
|
export declare class ActionEngineServer extends ActionEngineBase {
|
|
8
8
|
static get Instance(): ActionEngineServer;
|
|
9
9
|
RunAction(params: RunActionParams): Promise<ActionResult>;
|
|
10
|
-
/**
|
|
11
|
-
* Finds an action by the provided ID and runs it. This is a convenience method that can be used to run an action without having to first find/create an action entity object.
|
|
12
|
-
* Note that if no ActionParams are provided, the function will use the action params defined in the metadata.
|
|
13
|
-
*/
|
|
14
|
-
RunActionByID(params: RunActionByNameParams): Promise<ActionResult>;
|
|
15
10
|
protected GetActionParamsForAction(action: ActionEntityExtended): ActionParam[];
|
|
16
11
|
/**
|
|
17
12
|
* This method handles input validation. Subclasses can override this method to provide custom input validation.
|
|
@@ -23,6 +18,7 @@ export declare class ActionEngineServer extends ActionEngineBase {
|
|
|
23
18
|
protected RunFilters(params: RunActionParams): Promise<boolean>;
|
|
24
19
|
/**
|
|
25
20
|
* This method runs a single filter. Subclasses can override this method to provide custom filter logic.
|
|
21
|
+
*
|
|
26
22
|
* @param filter
|
|
27
23
|
*/
|
|
28
24
|
protected RunSingleFilter(params: RunActionParams, filter: ActionFilterEntity): Promise<boolean>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActionEngine.d.ts","sourceRoot":"","sources":["../../src/generic/ActionEngine.ts"],"names":[],"mappings":"AACA,OAAO,
|
|
1
|
+
{"version":3,"file":"ActionEngine.d.ts","sourceRoot":"","sources":["../../src/generic/ActionEngine.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,wBAAwB,EAAE,kBAAkB,EAA6C,MAAM,+BAA+B,CAAC;AAGxI,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,WAAW,EAAE,YAAY,EAAsB,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAItJ;;;GAGG;AACH,qBAAa,kBAAmB,SAAQ,gBAAgB;IAGrD,WAAkB,QAAQ,IAAI,kBAAkB,CAE/C;IAEY,SAAS,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC;IAuCtE,SAAS,CAAC,wBAAwB,CAAC,MAAM,EAAE,oBAAoB,GAAG,WAAW,EAAE;IAqC/E;;OAEG;cACa,cAAc,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC;IAIzE;;OAEG;cACa,UAAU,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,OAAO,CAAC;IAWrE;;;;OAIG;cACa,eAAe,CAAC,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,OAAO,CAAC;cAKtF,iBAAiB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,YAAY,CAAC;cAmCjE,cAAc,CAAC,MAAM,EAAE,eAAe,EAAE,UAAU,GAAE,OAAc,GAAG,OAAO,CAAC,wBAAwB,CAAC;cAqBtG,YAAY,CAAC,SAAS,EAAE,wBAAwB,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,YAAY;cAc/F,oBAAoB,CAAC,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,YAAY,GAAG,OAAO,CAAC,wBAAwB,CAAC;CAMxH"}
|
|
@@ -45,26 +45,6 @@ class ActionEngineServer extends actions_base_1.ActionEngineBase {
|
|
|
45
45
|
const runActionResult = await this.InternalRunAction(params);
|
|
46
46
|
return runActionResult;
|
|
47
47
|
}
|
|
48
|
-
/**
|
|
49
|
-
* Finds an action by the provided ID and runs it. This is a convenience method that can be used to run an action without having to first find/create an action entity object.
|
|
50
|
-
* Note that if no ActionParams are provided, the function will use the action params defined in the metadata.
|
|
51
|
-
*/
|
|
52
|
-
async RunActionByID(params) {
|
|
53
|
-
const action = this.Actions.find(a => a.ID === params.ActionID);
|
|
54
|
-
if (!action) {
|
|
55
|
-
throw new Error(`Action with ID ${params.ActionID} not found in Metadata`);
|
|
56
|
-
}
|
|
57
|
-
const actionParams = params.Params || this.GetActionParamsForAction(action);
|
|
58
|
-
const runParams = {
|
|
59
|
-
Action: action,
|
|
60
|
-
ContextUser: params.ContextUser,
|
|
61
|
-
SkipActionLog: params.SkipActionLog,
|
|
62
|
-
Params: actionParams,
|
|
63
|
-
Filters: []
|
|
64
|
-
};
|
|
65
|
-
const actionResult = await this.RunAction(runParams);
|
|
66
|
-
return actionResult;
|
|
67
|
-
}
|
|
68
48
|
GetActionParamsForAction(action) {
|
|
69
49
|
const params = action.Params.map((param) => {
|
|
70
50
|
let value = null;
|
|
@@ -119,6 +99,7 @@ class ActionEngineServer extends actions_base_1.ActionEngineBase {
|
|
|
119
99
|
}
|
|
120
100
|
/**
|
|
121
101
|
* This method runs a single filter. Subclasses can override this method to provide custom filter logic.
|
|
102
|
+
*
|
|
122
103
|
* @param filter
|
|
123
104
|
*/
|
|
124
105
|
async RunSingleFilter(params, filter) {
|
|
@@ -178,6 +159,7 @@ class ActionEngineServer extends actions_base_1.ActionEngineBase {
|
|
|
178
159
|
logEntity.EndedAt = new Date();
|
|
179
160
|
logEntity.Params = JSON.stringify(params.Params);
|
|
180
161
|
logEntity.ResultCode = result.Result?.ResultCode;
|
|
162
|
+
logEntity.Message = result.Message;
|
|
181
163
|
// save a second time to record the action ending
|
|
182
164
|
const saveResult = await logEntity.Save();
|
|
183
165
|
if (!saveResult) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ActionEngine.js","sourceRoot":"","sources":["../../src/generic/ActionEngine.ts"],"names":[],"mappings":";;;AAAA,+
|
|
1
|
+
{"version":3,"file":"ActionEngine.js","sourceRoot":"","sources":["../../src/generic/ActionEngine.ts"],"names":[],"mappings":";;;AAAA,+CAA0D;AAE1D,mDAAiE;AACjE,6CAA0C;AAC1C,+DAAsJ;AAItJ;;;GAGG;AACH,MAAa,kBAAmB,SAAQ,+BAAgB;IAG9C,MAAM,KAAK,QAAQ;QACvB,OAAO,KAAK,CAAC,WAAW,CAAqB,kBAAkB,CAAC,CAAC;IACpE,CAAC;IAEM,KAAK,CAAC,SAAS,CAAC,MAAuB;QAC3C,MAAM,WAAW,GAAY,MAAM,IAAI,CAAC,cAAc,CAAC,MAAM,CAAC,CAAC;QAC/D,IAAG,CAAC,WAAW,EAAC,CAAC;YACd,MAAM,MAAM,GAAiB;gBAC1B,OAAO,EAAE,KAAK;gBACd,OAAO,EAAE,uDAAuD;gBAChE,QAAQ,EAAE,IAAI;gBACd,MAAM,EAAE,MAAM,CAAC,MAAM;gBACrB,SAAS,EAAE,MAAM;aACnB,CAAC;YAEF,IAAG,CAAC,MAAM,CAAC,aAAa,EAAC,CAAC;gBACvB,MAAM,CAAC,QAAQ,GAAG,M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IAAA,eAAQ,EAAC,kCAAkC,MAAM,CAAC,MAAM,CAAC,IAAI,GAAG,EAAE,SAAS,EAAE,SAAS,CAAC,YAAY,CAAC,CAAC;QACxG,CAAC;IACJ,CAAC;IAES,KAAK,CAAC,oBAAoB,CAAC,MAAuB,EAAE,MAAoB;QAC/E,4BAA4B;QAC5B,MAAM,SAAS,GAA6B,MAAM,IAAI,CAAC,cAAc,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;QACrF,MAAM,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,CAAC;QACnD,OAAO,SAAS,CAAC;IACpB,CAAC;CACH;AA9LD,gDA8LC"}
|
|
@@ -1,9 +1,47 @@
|
|
|
1
1
|
import { ActionResultSimple, RunActionParams } from "@memberjunction/actions-base";
|
|
2
2
|
/**
|
|
3
3
|
* Base class for all actions. All actions will derive from this class and be instantiated by the ClassFactory within the @memberjunctin/global package.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```typescript
|
|
7
|
+
* // Define a typed context
|
|
8
|
+
* interface MyActionContext {
|
|
9
|
+
* apiEndpoint: string;
|
|
10
|
+
* authToken: string;
|
|
11
|
+
* }
|
|
12
|
+
*
|
|
13
|
+
* // Create an action with typed params
|
|
14
|
+
* export class MyAction extends BaseAction {
|
|
15
|
+
* protected async InternalRunAction(params: RunActionParams<MyActionContext>): Promise<ActionResultSimple> {
|
|
16
|
+
* // Access typed context
|
|
17
|
+
* const endpoint = params.Context?.apiEndpoint;
|
|
18
|
+
* const token = params.Context?.authToken;
|
|
19
|
+
*
|
|
20
|
+
* // Implement action logic
|
|
21
|
+
* return {
|
|
22
|
+
* Success: true,
|
|
23
|
+
* ResultCode: 'SUCCESS',
|
|
24
|
+
* Message: 'Action completed'
|
|
25
|
+
* };
|
|
26
|
+
* }
|
|
27
|
+
* }
|
|
28
|
+
* ```
|
|
4
29
|
*/
|
|
5
30
|
export declare abstract class BaseAction {
|
|
31
|
+
/**
|
|
32
|
+
* Executes the action with the provided parameters.
|
|
33
|
+
*
|
|
34
|
+
* @param params - The action execution parameters including context
|
|
35
|
+
* @returns Promise resolving to the action result
|
|
36
|
+
*/
|
|
6
37
|
Run(params: RunActionParams): Promise<ActionResultSimple>;
|
|
38
|
+
/**
|
|
39
|
+
* Internal method that must be implemented by derived action classes.
|
|
40
|
+
* This is where the actual action logic should be implemented.
|
|
41
|
+
*
|
|
42
|
+
* @param params - The action execution parameters including typed context
|
|
43
|
+
* @returns Promise resolving to the action result
|
|
44
|
+
*/
|
|
7
45
|
protected abstract InternalRunAction(params: RunActionParams): Promise<ActionResultSimple>;
|
|
8
46
|
}
|
|
9
47
|
//# sourceMappingURL=BaseAction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseAction.d.ts","sourceRoot":"","sources":["../../src/generic/BaseAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEnF
|
|
1
|
+
{"version":3,"file":"BaseAction.d.ts","sourceRoot":"","sources":["../../src/generic/BaseAction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,8BAA8B,CAAC;AAEnF;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,8BAAsB,UAAU;IAC7B;;;;;OAKG;IACU,GAAG,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAItE;;;;;;OAMG;IACH,SAAS,CAAC,QAAQ,CAAC,iBAAiB,CAAC,MAAM,EAAE,eAAe,GAAG,OAAO,CAAC,kBAAkB,CAAC;CAC5F"}
|
|
@@ -3,8 +3,39 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseAction = void 0;
|
|
4
4
|
/**
|
|
5
5
|
* Base class for all actions. All actions will derive from this class and be instantiated by the ClassFactory within the @memberjunctin/global package.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Define a typed context
|
|
10
|
+
* interface MyActionContext {
|
|
11
|
+
* apiEndpoint: string;
|
|
12
|
+
* authToken: string;
|
|
13
|
+
* }
|
|
14
|
+
*
|
|
15
|
+
* // Create an action with typed params
|
|
16
|
+
* export class MyAction extends BaseAction {
|
|
17
|
+
* protected async InternalRunAction(params: RunActionParams<MyActionContext>): Promise<ActionResultSimple> {
|
|
18
|
+
* // Access typed context
|
|
19
|
+
* const endpoint = params.Context?.apiEndpoint;
|
|
20
|
+
* const token = params.Context?.authToken;
|
|
21
|
+
*
|
|
22
|
+
* // Implement action logic
|
|
23
|
+
* return {
|
|
24
|
+
* Success: true,
|
|
25
|
+
* ResultCode: 'SUCCESS',
|
|
26
|
+
* Message: 'Action completed'
|
|
27
|
+
* };
|
|
28
|
+
* }
|
|
29
|
+
* }
|
|
30
|
+
* ```
|
|
6
31
|
*/
|
|
7
32
|
class BaseAction {
|
|
33
|
+
/**
|
|
34
|
+
* Executes the action with the provided parameters.
|
|
35
|
+
*
|
|
36
|
+
* @param params - The action execution parameters including context
|
|
37
|
+
* @returns Promise resolving to the action result
|
|
38
|
+
*/
|
|
8
39
|
async Run(params) {
|
|
9
40
|
return await this.InternalRunAction(params);
|
|
10
41
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseAction.js","sourceRoot":"","sources":["../../src/generic/BaseAction.ts"],"names":[],"mappings":";;;AAEA
|
|
1
|
+
{"version":3,"file":"BaseAction.js","sourceRoot":"","sources":["../../src/generic/BaseAction.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AACH,MAAsB,UAAU;IAC7B;;;;;OAKG;IACI,KAAK,CAAC,GAAG,CAAC,MAAuB;QACrC,OAAO,MAAM,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAC;IAC/C,CAAC;CAUH;AAnBD,gCAmBC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@memberjunction/actions",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.52.0",
|
|
4
4
|
"description": "Main library for MemberJunction Actions. This library is only intended to be imported on the server side.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -19,12 +19,12 @@
|
|
|
19
19
|
"typescript": "^5.4.5"
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@memberjunction/global": "2.
|
|
23
|
-
"@memberjunction/core": "2.
|
|
24
|
-
"@memberjunction/actions-base": "2.
|
|
25
|
-
"@memberjunction/core-entities": "2.
|
|
26
|
-
"@memberjunction/ai": "2.
|
|
27
|
-
"@memberjunction/aiengine": "2.
|
|
28
|
-
"@memberjunction/doc-utils": "2.
|
|
22
|
+
"@memberjunction/global": "2.52.0",
|
|
23
|
+
"@memberjunction/core": "2.52.0",
|
|
24
|
+
"@memberjunction/actions-base": "2.52.0",
|
|
25
|
+
"@memberjunction/core-entities": "2.52.0",
|
|
26
|
+
"@memberjunction/ai": "2.52.0",
|
|
27
|
+
"@memberjunction/aiengine": "2.52.0",
|
|
28
|
+
"@memberjunction/doc-utils": "2.52.0"
|
|
29
29
|
}
|
|
30
30
|
}
|
package/readme.md
CHANGED
|
@@ -106,6 +106,146 @@ const params: RunActionParams = {
|
|
|
106
106
|
const result = await engine.RunAction(params);
|
|
107
107
|
```
|
|
108
108
|
|
|
109
|
+
### Using Context in Actions (New in v2.51.0)
|
|
110
|
+
|
|
111
|
+
Actions now support type-safe context propagation for runtime-specific information:
|
|
112
|
+
|
|
113
|
+
```typescript
|
|
114
|
+
import { BaseAction } from '@memberjunction/actions';
|
|
115
|
+
import { ActionResultSimple, RunActionParams } from '@memberjunction/actions-base';
|
|
116
|
+
import { RegisterClass } from '@memberjunction/global';
|
|
117
|
+
|
|
118
|
+
// Define your context type
|
|
119
|
+
interface APIContext {
|
|
120
|
+
apiEndpoint: string;
|
|
121
|
+
apiKey: string;
|
|
122
|
+
timeout: number;
|
|
123
|
+
retryCount: number;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Note: BaseAction does not have generics - context is typed through params
|
|
127
|
+
@RegisterClass(BaseAction, 'APICallAction')
|
|
128
|
+
export class APICallAction extends BaseAction {
|
|
129
|
+
protected async InternalRunAction(params: RunActionParams<APIContext>): Promise<ActionResultSimple> {
|
|
130
|
+
// Access typed context through params
|
|
131
|
+
const endpoint = params.Context?.apiEndpoint;
|
|
132
|
+
const apiKey = params.Context?.apiKey;
|
|
133
|
+
const timeout = params.Context?.timeout || 30000;
|
|
134
|
+
|
|
135
|
+
if (!endpoint || !apiKey) {
|
|
136
|
+
return {
|
|
137
|
+
Success: false,
|
|
138
|
+
ResultCode: 'MISSING_CONTEXT',
|
|
139
|
+
Message: 'API endpoint and key are required in context'
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Use context for API call
|
|
144
|
+
const requestData = params.Params.find(p => p.Name === 'RequestData')?.Value;
|
|
145
|
+
|
|
146
|
+
try {
|
|
147
|
+
const response = await this.callAPI(endpoint, apiKey, requestData, timeout);
|
|
148
|
+
|
|
149
|
+
// Set output parameter
|
|
150
|
+
const outputParam = params.Params.find(p => p.Name === 'ResponseData');
|
|
151
|
+
if (outputParam) {
|
|
152
|
+
outputParam.Value = response;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
return {
|
|
156
|
+
Success: true,
|
|
157
|
+
ResultCode: 'SUCCESS',
|
|
158
|
+
Message: 'API call completed successfully',
|
|
159
|
+
Params: params.Params
|
|
160
|
+
};
|
|
161
|
+
} catch (error) {
|
|
162
|
+
return {
|
|
163
|
+
Success: false,
|
|
164
|
+
ResultCode: 'API_ERROR',
|
|
165
|
+
Message: error.message
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
private async callAPI(endpoint: string, apiKey: string, data: any, timeout: number): Promise<any> {
|
|
171
|
+
// Implementation details
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const response = await fetch(endpoint, {
|
|
177
|
+
method: 'POST',
|
|
178
|
+
headers: {
|
|
179
|
+
'Authorization': `Bearer ${apiKey}`,
|
|
180
|
+
'Content-Type': 'application/json'
|
|
181
|
+
},
|
|
182
|
+
body: JSON.stringify(data),
|
|
183
|
+
signal: controller.signal
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
clearTimeout(timeoutId);
|
|
187
|
+
return await response.json();
|
|
188
|
+
} catch (error) {
|
|
189
|
+
clearTimeout(timeoutId);
|
|
190
|
+
throw error;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Running Actions with Context
|
|
197
|
+
|
|
198
|
+
```typescript
|
|
199
|
+
import { ActionEngineServer } from '@memberjunction/actions';
|
|
200
|
+
import { RunActionParams } from '@memberjunction/actions-base';
|
|
201
|
+
|
|
202
|
+
// Define context type
|
|
203
|
+
interface APIContext {
|
|
204
|
+
apiEndpoint: string;
|
|
205
|
+
apiKey: string;
|
|
206
|
+
timeout: number;
|
|
207
|
+
retryCount: number;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Configure parameters with context
|
|
211
|
+
const params = new RunActionParams<APIContext>();
|
|
212
|
+
params.Action = apiCallAction;
|
|
213
|
+
params.ContextUser = currentUser;
|
|
214
|
+
params.Params = [
|
|
215
|
+
{ Name: 'RequestData', Value: { orderId: '12345' }, Type: 'Input' },
|
|
216
|
+
{ Name: 'ResponseData', Value: null, Type: 'Output' }
|
|
217
|
+
];
|
|
218
|
+
|
|
219
|
+
// Set runtime context
|
|
220
|
+
params.Context = {
|
|
221
|
+
apiEndpoint: process.env.API_ENDPOINT,
|
|
222
|
+
apiKey: process.env.API_KEY,
|
|
223
|
+
timeout: 10000,
|
|
224
|
+
retryCount: 3
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
// Execute with typed context
|
|
228
|
+
const result = await ActionEngineServer.Instance.RunAction(params);
|
|
229
|
+
```
|
|
230
|
+
|
|
231
|
+
#### Context vs Parameters
|
|
232
|
+
|
|
233
|
+
**Use Context for:**
|
|
234
|
+
- Environment-specific configuration (API endpoints, service URLs)
|
|
235
|
+
- Runtime credentials (API keys, tokens)
|
|
236
|
+
- Session information (user preferences, correlation IDs)
|
|
237
|
+
- Feature flags and toggles
|
|
238
|
+
- Timeout and retry policies
|
|
239
|
+
|
|
240
|
+
**Use Parameters for:**
|
|
241
|
+
- Business data (customer ID, order details)
|
|
242
|
+
- Action-specific inputs (email content, calculation values)
|
|
243
|
+
- Data that should be logged and audited
|
|
244
|
+
- Values that need to be stored in the database
|
|
245
|
+
- Output values that other actions may depend on
|
|
246
|
+
|
|
247
|
+
The context is particularly useful when actions are executed from AI agents, as it allows the agent to pass runtime information down through the entire execution hierarchy without modifying the action's formal parameter structure.
|
|
248
|
+
|
|
109
249
|
### Entity Actions
|
|
110
250
|
|
|
111
251
|
Entity Actions are triggered automatically during entity lifecycle events. To work with entity actions, use the `EntityActionEngineServer`:
|
|
@@ -165,7 +305,7 @@ The main engine for executing actions.
|
|
|
165
305
|
|
|
166
306
|
#### BaseAction
|
|
167
307
|
|
|
168
|
-
Abstract base class for all actions.
|
|
308
|
+
Abstract base class for all actions. Note that BaseAction does not use generics - context typing is achieved through the RunActionParams parameter.
|
|
169
309
|
|
|
170
310
|
**Methods:**
|
|
171
311
|
- `Run(params: RunActionParams): Promise<ActionResultSimple>` - Public method called by the engine
|