@outputai/core 0.10.0 → 0.10.1-dev.b7b2fbe.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/package.json +1 -1
- package/src/consts.js +2 -1
- package/src/helpers/object.js +45 -36
- package/src/helpers/object.spec.js +118 -75
- package/src/interface/workflow.js +92 -85
- package/src/interface/workflow.spec.js +135 -142
- package/src/sdk/helpers/objects.d.ts +24 -19
- package/src/worker/interceptors/workflow.js +5 -5
- package/src/worker/interceptors/workflow.spec.js +7 -7
- package/src/worker/loader/activities.js +56 -40
- package/src/worker/loader/activities.spec.js +21 -22
- package/src/worker/loader/workflows.spec.js +1 -2
- package/src/worker/webpack_loaders/consts.js +6 -0
- package/src/worker/webpack_loaders/tools.js +1 -146
- package/src/worker/webpack_loaders/tools.spec.js +0 -23
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.js +24 -32
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.spec.js +82 -279
- package/src/worker/webpack_loaders/workflow_rewriter/index.mjs +6 -7
- package/src/worker/webpack_loaders/workflow_rewriter/index.spec.js +57 -119
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_activity_calls.js +88 -0
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_activity_calls.spec.js +165 -0
- package/src/worker/webpack_loaders/workflow_validator/index.mjs +96 -29
- package/src/worker/webpack_loaders/workflow_validator/index.spec.js +110 -583
- package/src/worker/webpack_loaders/{npm_workflow_export_resolve.js → workflow_validator/npm_workflow_export_resolve.js} +1 -1
- package/src/worker/webpack_loaders/{npm_workflow_export_resolve.spec.js → workflow_validator/npm_workflow_export_resolve.spec.js} +1 -1
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.js +0 -193
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.spec.js +0 -123
|
@@ -2,74 +2,90 @@ import { dirname } from 'node:path';
|
|
|
2
2
|
import { getTraceDestinations, sendHttpRequest } from '#internal_activities';
|
|
3
3
|
import { findSharedActivitiesFromWorkflows, importComponents, matchFiles, writeFileInTempDir } from './tools.js';
|
|
4
4
|
import { buildActivityMatcher, staticMatchers } from './matchers.js';
|
|
5
|
-
import { ACTIVITY_SEND_HTTP_REQUEST, ACTIVITY_OPTIONS_FILENAME,
|
|
5
|
+
import { ACTIVITY_SEND_HTTP_REQUEST, ACTIVITY_OPTIONS_FILENAME, ACTIVITY_GET_TRACE_DESTINATIONS } from '#consts';
|
|
6
6
|
import { createChildLogger } from '#logger';
|
|
7
7
|
import { ValidationError } from '#errors';
|
|
8
8
|
|
|
9
9
|
const log = createChildLogger( 'Activities Loader' );
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* Load activities
|
|
13
|
-
* -
|
|
14
|
-
* -
|
|
15
|
-
* -
|
|
12
|
+
* Load activities
|
|
13
|
+
* - Scan local project and external workflow npm packages and look for shared activities.
|
|
14
|
+
* - For each workflow, register shared activities under the workflow namespace.
|
|
15
|
+
* - For each workflow, load activities declared relative to that workflow directory.
|
|
16
16
|
*
|
|
17
|
-
* Builds a map of activities,
|
|
18
|
-
* - Shared activity keys have a common prefix followed by the activity name;
|
|
19
|
-
* - Internal activities are registered with a fixed key;
|
|
20
|
-
* - Workflow activities keys are composed using the workflow name and the activity name;
|
|
17
|
+
* Builds a map of activities, key is workflowType#activityType, value is the component wrapper function.
|
|
21
18
|
*
|
|
22
19
|
* @param {string} rootDir
|
|
23
20
|
* @param {import('./workflows.js').Workflow[]} workflows
|
|
24
21
|
* @returns {object}
|
|
25
22
|
*/
|
|
26
23
|
export async function loadActivities( rootDir, workflows ) {
|
|
27
|
-
const activities =
|
|
28
|
-
const
|
|
24
|
+
const activities = new Map();
|
|
25
|
+
const activityOptions = new Map();
|
|
29
26
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const dir = dirname( workflowPath );
|
|
33
|
-
for await ( const { fn, metadata, path } of importComponents( matchFiles( dir, [ buildActivityMatcher( dir ) ] ) ) ) {
|
|
34
|
-
// Activities loaded from a workflow path will use the workflow name as a namespace, which is unique across the platform, avoiding collision
|
|
35
|
-
const activityKey = `${workflowName}#${metadata.name}`;
|
|
36
|
-
|
|
37
|
-
log.info( metadata.name, { workflow: workflowName, type: metadata.type, ...( external && { external } ), path } );
|
|
38
|
-
|
|
39
|
-
if ( activities[activityKey] ) {
|
|
40
|
-
throw new ValidationError( `Activity "${metadata.name}" in workflow "${workflowName}" conflicts with another \
|
|
41
|
-
activity in the same workflow. Activity names must be unique within a workflow.` );
|
|
42
|
-
}
|
|
43
|
-
activities[activityKey] = fn;
|
|
44
|
-
// propagate the custom options set on the step()/evaluator() constructor
|
|
45
|
-
activityOptionsMap[activityKey] = metadata.options?.activityOptions ?? undefined;
|
|
46
|
-
}
|
|
47
|
-
}
|
|
27
|
+
const sharedActivities = new Map();
|
|
28
|
+
const sharedActivitiesOptions = new Map();
|
|
48
29
|
|
|
49
30
|
// Load shared activities/evaluators from local and external npm modules
|
|
50
31
|
const localSharedActivities = matchFiles( rootDir, [ staticMatchers.sharedStepsDir, staticMatchers.sharedEvaluatorsDir ] );
|
|
51
32
|
const externalSharedActivities = findSharedActivitiesFromWorkflows( workflows.filter( w => w.external ) );
|
|
52
33
|
for await ( const { fn, metadata, path } of importComponents( [ ...localSharedActivities, ...externalSharedActivities ] ) ) {
|
|
53
34
|
const external = externalSharedActivities.some( a => a.path === path );
|
|
54
|
-
// Uses a global namespace for shared activities
|
|
55
|
-
const activityKey = `${SHARED_STEP_PREFIX}#${metadata.name}`;
|
|
56
35
|
|
|
36
|
+
if ( sharedActivities.has( metadata.name ) ) {
|
|
37
|
+
throw new ValidationError( `Shared activity "${metadata.name}" conflicts with another shared activity.` +
|
|
38
|
+
' Shared activity names must be unique.' );
|
|
39
|
+
}
|
|
57
40
|
log.info( metadata.name, { shared: true, type: metadata.type, ...( external && { external } ), path } );
|
|
58
41
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
42
|
+
sharedActivities.set( metadata.name, fn );
|
|
43
|
+
if ( metadata.options?.activityOptions ) {
|
|
44
|
+
sharedActivitiesOptions.set( metadata.name, metadata.options.activityOptions );
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// Discover and load workflow activities
|
|
49
|
+
for ( const { path: workflowPath, name: workflowName, external } of workflows ) {
|
|
50
|
+
const dir = dirname( workflowPath );
|
|
51
|
+
|
|
52
|
+
// Add shared activities to this workflow namespace
|
|
53
|
+
for ( const [ name, fn ] of sharedActivities ) {
|
|
54
|
+
const id = `${workflowName}#${name}`;
|
|
55
|
+
activities.set( id, fn );
|
|
56
|
+
if ( sharedActivitiesOptions.has( name ) ) {
|
|
57
|
+
activityOptions.set( id, sharedActivitiesOptions.get( name ) );
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
for await ( const { fn, metadata, path } of importComponents( matchFiles( dir, [ buildActivityMatcher( dir ) ] ) ) ) {
|
|
62
|
+
// Activities loaded from a workflow path will use the workflow name as a namespace, which is unique across the platform, avoiding collision
|
|
63
|
+
const id = `${workflowName}#${metadata.name}`;
|
|
64
|
+
|
|
65
|
+
if ( sharedActivities.has( metadata.name ) ) {
|
|
66
|
+
throw new ValidationError( `Activity "${metadata.name}" in workflow "${workflowName}" conflicts with a shared activity.` +
|
|
67
|
+
' Workflow activity names must not overlap with shared activity names.' );
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if ( activities.has( id ) ) {
|
|
71
|
+
throw new ValidationError( `Activity "${metadata.name}" in workflow "${workflowName}" conflicts with another activity in the same workflow.` +
|
|
72
|
+
' Activity names must be unique within a workflow.' );
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
log.info( metadata.name, { workflow: workflowName, type: metadata.type, ...( external && { external } ), path } );
|
|
76
|
+
activities.set( id, fn );
|
|
77
|
+
if ( metadata.options?.activityOptions ) {
|
|
78
|
+
activityOptions.set( id, metadata.options.activityOptions );
|
|
79
|
+
}
|
|
62
80
|
}
|
|
63
|
-
activities[activityKey] = fn;
|
|
64
|
-
activityOptionsMap[activityKey] = metadata.options?.activityOptions ?? undefined;
|
|
65
81
|
}
|
|
66
82
|
|
|
67
83
|
// writes down the activity option overrides
|
|
68
|
-
const optionsContent = `export default ${JSON.stringify(
|
|
84
|
+
const optionsContent = `export default ${JSON.stringify( Object.fromEntries( activityOptions ), undefined, 2 )};`;
|
|
69
85
|
const optionsFile = writeFileInTempDir( optionsContent, ACTIVITY_OPTIONS_FILENAME );
|
|
70
86
|
|
|
71
87
|
// system activities
|
|
72
|
-
activities
|
|
73
|
-
activities
|
|
74
|
-
return { activities, optionsFile };
|
|
88
|
+
activities.set( ACTIVITY_SEND_HTTP_REQUEST, sendHttpRequest );
|
|
89
|
+
activities.set( ACTIVITY_GET_TRACE_DESTINATIONS, getTraceDestinations );
|
|
90
|
+
return { activities: Object.fromEntries( activities ), optionsFile };
|
|
75
91
|
};
|
|
@@ -5,8 +5,7 @@ vi.mock( '#consts', () => ( {
|
|
|
5
5
|
ACTIVITY_GET_TRACE_DESTINATIONS: '__internal#getTraceDestinations',
|
|
6
6
|
WORKFLOWS_INDEX_FILENAME: '__workflows_entrypoint.js',
|
|
7
7
|
WORKFLOW_CATALOG: 'catalog',
|
|
8
|
-
ACTIVITY_OPTIONS_FILENAME: '__activity_options.js'
|
|
9
|
-
SHARED_STEP_PREFIX: '$shared'
|
|
8
|
+
ACTIVITY_OPTIONS_FILENAME: '__activity_options.js'
|
|
10
9
|
} ) );
|
|
11
10
|
|
|
12
11
|
const sendHttpRequestMock = vi.fn();
|
|
@@ -63,6 +62,7 @@ describe( 'loadActivities', () => {
|
|
|
63
62
|
it( 'loadActivities returns map including system activity and writes options file', async () => {
|
|
64
63
|
const { loadActivities } = await import( './activities.js' );
|
|
65
64
|
|
|
65
|
+
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
66
66
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
67
67
|
yield {
|
|
68
68
|
fn: () => {},
|
|
@@ -70,7 +70,6 @@ describe( 'loadActivities', () => {
|
|
|
70
70
|
path: '/a/steps.js'
|
|
71
71
|
};
|
|
72
72
|
} );
|
|
73
|
-
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
74
73
|
|
|
75
74
|
const workflows = [ { name: 'A', path: '/a/workflow.js' } ];
|
|
76
75
|
const { activities, optionsFile } = await loadActivities( '/root', workflows );
|
|
@@ -89,11 +88,11 @@ describe( 'loadActivities', () => {
|
|
|
89
88
|
|
|
90
89
|
it( 'loadActivities omits activity options when component has no options or no activityOptions', async () => {
|
|
91
90
|
const { loadActivities } = await import( './activities.js' );
|
|
91
|
+
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
92
92
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
93
93
|
yield { fn: () => {}, metadata: { name: 'NoOptions' }, path: '/a/steps.js' };
|
|
94
94
|
yield { fn: () => {}, metadata: { name: 'EmptyOptions', options: {} }, path: '/a/steps2.js' };
|
|
95
95
|
} );
|
|
96
|
-
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
97
96
|
|
|
98
97
|
await loadActivities( '/root', [ { name: 'A', path: '/a/workflow.js' } ] );
|
|
99
98
|
const written = JSON.parse(
|
|
@@ -105,6 +104,7 @@ describe( 'loadActivities', () => {
|
|
|
105
104
|
|
|
106
105
|
it( 'loadActivities throws when two activities in the same workflow share a name', async () => {
|
|
107
106
|
const { loadActivities } = await import( './activities.js' );
|
|
107
|
+
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
108
108
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
109
109
|
yield { fn: () => {}, metadata: { name: 'DuplicateActivity' }, path: '/a/steps.js' };
|
|
110
110
|
yield { fn: () => {}, metadata: { name: 'DuplicateActivity' }, path: '/a/evaluators.js' };
|
|
@@ -118,7 +118,6 @@ Activity names must be unique within a workflow.'
|
|
|
118
118
|
|
|
119
119
|
it( 'loadActivities throws when two shared activities share a name', async () => {
|
|
120
120
|
const { loadActivities } = await import( './activities.js' );
|
|
121
|
-
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
122
121
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
123
122
|
yield { fn: () => {}, metadata: { name: 'DuplicateShared' }, path: '/root/shared/steps/a.js' };
|
|
124
123
|
yield { fn: () => {}, metadata: { name: 'DuplicateShared' }, path: '/root/shared/evaluators/a.js' };
|
|
@@ -133,8 +132,8 @@ Activity names must be unique within a workflow.'
|
|
|
133
132
|
const { loadActivities } = await import( './activities.js' );
|
|
134
133
|
const workflowFiles = [ { path: '/a/steps/foo.js' } ];
|
|
135
134
|
const sharedFiles = [ { path: '/root/shared/steps/baz.js' } ];
|
|
136
|
-
matchFilesMock.mockReturnValueOnce( workflowFiles );
|
|
137
135
|
matchFilesMock.mockReturnValueOnce( sharedFiles );
|
|
136
|
+
matchFilesMock.mockReturnValueOnce( workflowFiles );
|
|
138
137
|
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
139
138
|
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
140
139
|
|
|
@@ -143,12 +142,12 @@ Activity names must be unique within a workflow.'
|
|
|
143
142
|
|
|
144
143
|
expect( matchFilesMock ).toHaveBeenCalledTimes( 2 );
|
|
145
144
|
expect( buildActivityMatcherMock ).toHaveBeenCalledWith( '/a' );
|
|
146
|
-
expect( matchFilesMock ).toHaveBeenNthCalledWith( 1, '/
|
|
147
|
-
expect( matchFilesMock ).toHaveBeenNthCalledWith( 2, '/
|
|
145
|
+
expect( matchFilesMock ).toHaveBeenNthCalledWith( 1, '/root', [ sharedStepsDirMock, sharedEvaluatorsDirMock ] );
|
|
146
|
+
expect( matchFilesMock ).toHaveBeenNthCalledWith( 2, '/a', [ activityMatcherMock ] );
|
|
148
147
|
|
|
149
148
|
expect( importComponentsMock ).toHaveBeenCalledTimes( 2 );
|
|
150
|
-
expect( importComponentsMock ).toHaveBeenNthCalledWith( 1,
|
|
151
|
-
expect( importComponentsMock ).toHaveBeenNthCalledWith( 2,
|
|
149
|
+
expect( importComponentsMock ).toHaveBeenNthCalledWith( 1, sharedFiles );
|
|
150
|
+
expect( importComponentsMock ).toHaveBeenNthCalledWith( 2, workflowFiles );
|
|
152
151
|
} );
|
|
153
152
|
|
|
154
153
|
it( 'loads shared activities from external workflow packages', async () => {
|
|
@@ -157,8 +156,6 @@ Activity names must be unique within a workflow.'
|
|
|
157
156
|
const localWorkflow = { name: 'Local', path: '/root/workflows/local/workflow.js' };
|
|
158
157
|
const externalWorkflow = { name: 'External', path: '/root/node_modules/pkg/workflows/a/workflow.js', external: true };
|
|
159
158
|
findSharedActivitiesFromWorkflowsMock.mockReturnValue( externalSharedFiles );
|
|
160
|
-
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
161
|
-
importComponentsMock.mockImplementationOnce( async function *() {} );
|
|
162
159
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
163
160
|
yield {
|
|
164
161
|
fn: () => {},
|
|
@@ -170,27 +167,29 @@ Activity names must be unique within a workflow.'
|
|
|
170
167
|
const { activities } = await loadActivities( '/root', [ localWorkflow, externalWorkflow ] );
|
|
171
168
|
|
|
172
169
|
expect( findSharedActivitiesFromWorkflowsMock ).toHaveBeenCalledWith( [ externalWorkflow ] );
|
|
173
|
-
expect( importComponentsMock ).toHaveBeenNthCalledWith(
|
|
174
|
-
expect( activities['
|
|
170
|
+
expect( importComponentsMock ).toHaveBeenNthCalledWith( 1, externalSharedFiles );
|
|
171
|
+
expect( activities['Local#ExternalShared'] ).toBeTypeOf( 'function' );
|
|
172
|
+
expect( activities['External#ExternalShared'] ).toBeTypeOf( 'function' );
|
|
175
173
|
const written = JSON.parse(
|
|
176
174
|
writeFileInTempDirMock.mock.calls[0][0].replace( /^export default\s*/, '' ).replace( /;\s*$/, '' )
|
|
177
175
|
);
|
|
178
|
-
expect( written['
|
|
176
|
+
expect( written['Local#ExternalShared'] ).toEqual( { retry: { maximumAttempts: 2 } } );
|
|
177
|
+
expect( written['External#ExternalShared'] ).toEqual( { retry: { maximumAttempts: 2 } } );
|
|
179
178
|
} );
|
|
180
179
|
|
|
181
180
|
it( 'loadActivities includes nested workflow steps and shared evaluators', async () => {
|
|
182
181
|
const { loadActivities } = await import( './activities.js' );
|
|
183
182
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
184
|
-
yield { fn: () => {}, metadata: { name: '
|
|
183
|
+
yield { fn: () => {}, metadata: { name: 'SharedEval' }, path: '/root/shared/evaluators/bar.js' };
|
|
185
184
|
} );
|
|
186
185
|
importComponentsMock.mockImplementationOnce( async function *() {
|
|
187
|
-
yield { fn: () => {}, metadata: { name: '
|
|
186
|
+
yield { fn: () => {}, metadata: { name: 'ActNested' }, path: '/a/steps/foo.js' };
|
|
188
187
|
} );
|
|
189
188
|
|
|
190
189
|
const workflows = [ { name: 'A', path: '/a/workflow.js' } ];
|
|
191
190
|
const { activities } = await loadActivities( '/root', workflows );
|
|
192
191
|
expect( activities['A#ActNested'] ).toBeTypeOf( 'function' );
|
|
193
|
-
expect( activities['
|
|
192
|
+
expect( activities['A#SharedEval'] ).toBeTypeOf( 'function' );
|
|
194
193
|
} );
|
|
195
194
|
|
|
196
195
|
it( 'collects shared nested steps and evaluators across multiple subfolders', async () => {
|
|
@@ -205,9 +204,9 @@ Activity names must be unique within a workflow.'
|
|
|
205
204
|
|
|
206
205
|
const workflows = [ { name: 'A', path: '/a/workflow.js' } ];
|
|
207
206
|
const { activities } = await loadActivities( '/root', workflows );
|
|
208
|
-
expect( activities['
|
|
209
|
-
expect( activities['
|
|
210
|
-
expect( activities['
|
|
211
|
-
expect( activities['
|
|
207
|
+
expect( activities['A#SharedStepPrimary'] ).toBeTypeOf( 'function' );
|
|
208
|
+
expect( activities['A#SharedStepSecondary'] ).toBeTypeOf( 'function' );
|
|
209
|
+
expect( activities['A#SharedEvalPrimary'] ).toBeTypeOf( 'function' );
|
|
210
|
+
expect( activities['A#SharedEvalSecondary'] ).toBeTypeOf( 'function' );
|
|
212
211
|
} );
|
|
213
212
|
} );
|
|
@@ -5,8 +5,7 @@ vi.mock( '#consts', () => ( {
|
|
|
5
5
|
ACTIVITY_GET_TRACE_DESTINATIONS: '__internal#getTraceDestinations',
|
|
6
6
|
WORKFLOWS_INDEX_FILENAME: '__workflows_entrypoint.js',
|
|
7
7
|
WORKFLOW_CATALOG: 'catalog',
|
|
8
|
-
ACTIVITY_OPTIONS_FILENAME: '__activity_options.js'
|
|
9
|
-
SHARED_STEP_PREFIX: '$shared'
|
|
8
|
+
ACTIVITY_OPTIONS_FILENAME: '__activity_options.js'
|
|
10
9
|
} ) );
|
|
11
10
|
|
|
12
11
|
const { importComponentsMock, findWorkflowsInNodeModulesMock, matchFilesMock, writeFileInTempDirMock } = vi.hoisted( () => ( {
|
|
@@ -2,27 +2,14 @@ import parser from '@babel/parser';
|
|
|
2
2
|
import { resolve as resolvePath } from 'node:path';
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
4
|
import {
|
|
5
|
-
blockStatement,
|
|
6
|
-
callExpression,
|
|
7
|
-
functionExpression,
|
|
8
|
-
identifier,
|
|
9
|
-
isArrowFunctionExpression,
|
|
10
5
|
isAssignmentPattern,
|
|
11
|
-
isBlockStatement,
|
|
12
6
|
isCallExpression,
|
|
13
7
|
isExportNamedDeclaration,
|
|
14
|
-
isFunctionExpression,
|
|
15
8
|
isIdentifier,
|
|
16
|
-
isVariableDeclarator,
|
|
17
9
|
isStringLiteral,
|
|
18
10
|
isVariableDeclaration,
|
|
19
11
|
isObjectExpression,
|
|
20
|
-
|
|
21
|
-
returnStatement,
|
|
22
|
-
stringLiteral,
|
|
23
|
-
thisExpression,
|
|
24
|
-
isExportDefaultDeclaration,
|
|
25
|
-
isFunctionDeclaration
|
|
12
|
+
isExportDefaultDeclaration
|
|
26
13
|
} from '@babel/types';
|
|
27
14
|
import { ComponentFile, NodeType } from './consts.js';
|
|
28
15
|
|
|
@@ -100,17 +87,6 @@ export const getLocalNameFromDestructuredProperty = prop => {
|
|
|
100
87
|
return null;
|
|
101
88
|
};
|
|
102
89
|
|
|
103
|
-
/**
|
|
104
|
-
* Convert an ArrowFunctionExpression to a FunctionExpression.
|
|
105
|
-
* Wraps expression bodies in a block with a return statement.
|
|
106
|
-
* @param {import('@babel/types').ArrowFunctionExpression} arrow - Arrow function.
|
|
107
|
-
* @returns {import('@babel/types').FunctionExpression} Function expression.
|
|
108
|
-
*/
|
|
109
|
-
export const toFunctionExpression = arrow => {
|
|
110
|
-
const body = isBlockStatement( arrow.body ) ? arrow.body : blockStatement( [ returnStatement( arrow.body ) ] );
|
|
111
|
-
return functionExpression( null, arrow.params, body, arrow.generator ?? false, arrow.async ?? false );
|
|
112
|
-
};
|
|
113
|
-
|
|
114
90
|
/**
|
|
115
91
|
* Check if a module specifier or request string points to steps.js or is in a steps folder.
|
|
116
92
|
* Matches: steps.js, /steps.js, /steps/*.js
|
|
@@ -231,38 +207,6 @@ export const getFileKind = path => {
|
|
|
231
207
|
return null;
|
|
232
208
|
};
|
|
233
209
|
|
|
234
|
-
/**
|
|
235
|
-
* Create a `this.method(literalName, ...args)` CallExpression.
|
|
236
|
-
* @param {string} method - Method name on `this`.
|
|
237
|
-
* @param {string} literalName - First string literal argument.
|
|
238
|
-
* @param {import('@babel/types').Expression[]} args - Remaining call arguments.
|
|
239
|
-
* @returns {import('@babel/types').CallExpression} Call expression node.
|
|
240
|
-
*/
|
|
241
|
-
export const createThisMethodCall = ( method, literalName, args ) =>
|
|
242
|
-
callExpression( memberExpression( thisExpression(), identifier( method ) ), [ stringLiteral( literalName ), ...args ] );
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* Build a CallExpression that binds `this` at the call site:
|
|
246
|
-
* fn(arg1, arg2) -> fn.call(this, arg1, arg2)
|
|
247
|
-
*
|
|
248
|
-
* When to use:
|
|
249
|
-
* - Inside workflow `fn` rewriting, local call-chain functions must receive the dynamic `this`
|
|
250
|
-
* so that emitted `this.invokeStep(...)` and similar calls inside them operate correctly.
|
|
251
|
-
*
|
|
252
|
-
* Example:
|
|
253
|
-
* // Input AST intent:
|
|
254
|
-
* foo(a, b);
|
|
255
|
-
*
|
|
256
|
-
* // Rewritten AST:
|
|
257
|
-
* foo.call(this, a, b);
|
|
258
|
-
*
|
|
259
|
-
* @param {string} calleeName - Identifier name of the function being called (e.g., 'foo').
|
|
260
|
-
* @param {import('@babel/types').Expression[]} args - Original call arguments.
|
|
261
|
-
* @returns {import('@babel/types').CallExpression} CallExpression node representing `callee.call(this, ...args)`.
|
|
262
|
-
*/
|
|
263
|
-
export const bindThisAtCallSite = ( calleeName, args ) =>
|
|
264
|
-
callExpression( memberExpression( identifier( calleeName ), identifier( 'call' ) ), [ thisExpression(), ...args ] );
|
|
265
|
-
|
|
266
210
|
/**
|
|
267
211
|
* Resolve an options object's name property to a string.
|
|
268
212
|
* Accepts literal strings or top-level const string identifiers.
|
|
@@ -466,92 +410,3 @@ export const buildWorkflowNameMap = ( path, cache ) => {
|
|
|
466
410
|
return result;
|
|
467
411
|
};
|
|
468
412
|
|
|
469
|
-
/**
|
|
470
|
-
* Determine whether a node represents a function body usable as a workflow `fn`.
|
|
471
|
-
*
|
|
472
|
-
* Why this matters:
|
|
473
|
-
* - Workflow `fn` needs a dynamic `this` so the rewriter can emit calls like `this.invokeStep(...)`.
|
|
474
|
-
* - Arrow functions do not have their own `this`; they capture `this` lexically, which breaks the runtime contract.
|
|
475
|
-
*
|
|
476
|
-
* Accepts:
|
|
477
|
-
* - FunctionExpression (possibly async/generator), e.g.:
|
|
478
|
-
* const obj = {
|
|
479
|
-
* fn: async function (input) {
|
|
480
|
-
* return input;
|
|
481
|
-
* }
|
|
482
|
-
* };
|
|
483
|
-
*
|
|
484
|
-
* Rejects:
|
|
485
|
-
* - ArrowFunctionExpression, e.g.:
|
|
486
|
-
* const obj = {
|
|
487
|
-
* fn: async (input) => input
|
|
488
|
-
* };
|
|
489
|
-
*
|
|
490
|
-
* - Any other non-function expression.
|
|
491
|
-
*
|
|
492
|
-
* Notes:
|
|
493
|
-
* - The rewriter will proactively convert arrow `fn` to a FunctionExpression before further processing.
|
|
494
|
-
*
|
|
495
|
-
* @param {import('@babel/types').Expression} v - Candidate node for `fn` value.
|
|
496
|
-
* @returns {boolean} True if `v` is a FunctionExpression and not an arrow function.
|
|
497
|
-
*/
|
|
498
|
-
export const isFunction = v => isFunctionExpression( v ) && !isArrowFunctionExpression( v );
|
|
499
|
-
|
|
500
|
-
/**
|
|
501
|
-
* Determine whether a variable declarator represents a function-like value.
|
|
502
|
-
*
|
|
503
|
-
* Use case:
|
|
504
|
-
* - When `fn` calls a locally-declared function (directly or transitively), we need to:
|
|
505
|
-
* - propagate `this` to that function call (`callee.call(this, ...)`)
|
|
506
|
-
* - traverse into that function's body to rewrite imported step/workflow/evaluator calls.
|
|
507
|
-
*
|
|
508
|
-
* Matches patterns like:
|
|
509
|
-
* - Function expression:
|
|
510
|
-
* const foo = function (x) { return x + 1; };
|
|
511
|
-
*
|
|
512
|
-
* - Async/generator function expression:
|
|
513
|
-
* const foo = async function (x) { return await work(x); };
|
|
514
|
-
*
|
|
515
|
-
* - Arrow function (will be normalized to FunctionExpression by the rewriter):
|
|
516
|
-
* const foo = (x) => x + 1;
|
|
517
|
-
* const foo = async (x) => await work(x);
|
|
518
|
-
*
|
|
519
|
-
* Does not match:
|
|
520
|
-
* - Non-function initializers:
|
|
521
|
-
* const foo = 42;
|
|
522
|
-
* const foo = someIdentifier;
|
|
523
|
-
*
|
|
524
|
-
* @param {import('@babel/types').Node} v - AST node (typically a VariableDeclarator).
|
|
525
|
-
* @returns {boolean} True if the declarator's initializer is a function (arrow or function expression).
|
|
526
|
-
*/
|
|
527
|
-
export const isVarFunction = v =>
|
|
528
|
-
isVariableDeclarator( v ) && ( isFunctionExpression( v.init ) || isArrowFunctionExpression( v.init ) );
|
|
529
|
-
|
|
530
|
-
/**
|
|
531
|
-
* Determine whether a binding node corresponds to a function-like declaration usable
|
|
532
|
-
* as a call-chain function target during workflow rewriting.
|
|
533
|
-
*
|
|
534
|
-
* Matches:
|
|
535
|
-
* - FunctionDeclaration:
|
|
536
|
-
* function foo(x) { return x + 1; }
|
|
537
|
-
*
|
|
538
|
-
* - VariableDeclarator initialized with a function or arrow (normalized later):
|
|
539
|
-
* const foo = function (x) { return x + 1; };
|
|
540
|
-
* const foo = (x) => x + 1;
|
|
541
|
-
*
|
|
542
|
-
* Non-matches:
|
|
543
|
-
* - Any binding that is not a function declaration nor a variable declarator with a function initializer.
|
|
544
|
-
*
|
|
545
|
-
* Why this matters:
|
|
546
|
-
* - The rewriter traverses call chains from the workflow `fn`. It must recognize which local
|
|
547
|
-
* callees are valid function bodies to rewrite and into which it can propagate `this`.
|
|
548
|
-
*
|
|
549
|
-
* @param {import('@babel/types').Node} node - Binding path node (FunctionDeclaration or VariableDeclarator).
|
|
550
|
-
* @returns {boolean} True if the node represents a function-like binding.
|
|
551
|
-
*/
|
|
552
|
-
export const isFunctionLikeBinding = node =>
|
|
553
|
-
isFunctionDeclaration( node ) ||
|
|
554
|
-
(
|
|
555
|
-
isVariableDeclarator( node ) &&
|
|
556
|
-
( isFunctionExpression( node.init ) || isArrowFunctionExpression( node.init ) )
|
|
557
|
-
);
|
|
@@ -9,7 +9,6 @@ import {
|
|
|
9
9
|
extractTopLevelStringConsts,
|
|
10
10
|
getObjectKeyName,
|
|
11
11
|
getLocalNameFromDestructuredProperty,
|
|
12
|
-
toFunctionExpression,
|
|
13
12
|
isStepsPath,
|
|
14
13
|
isSharedStepsPath,
|
|
15
14
|
isAnyStepsPath,
|
|
@@ -17,7 +16,6 @@ import {
|
|
|
17
16
|
isSharedEvaluatorsPath,
|
|
18
17
|
isWorkflowPath,
|
|
19
18
|
isAbsoluteWorkflowJsResource,
|
|
20
|
-
createThisMethodCall,
|
|
21
19
|
resolveNameFromArg,
|
|
22
20
|
resolveNameFromOptions,
|
|
23
21
|
buildStepsNameMap,
|
|
@@ -130,17 +128,6 @@ describe( 'workflow_rewriter tools', () => {
|
|
|
130
128
|
rmSync( dir, { recursive: true, force: true } );
|
|
131
129
|
} );
|
|
132
130
|
|
|
133
|
-
it( 'toFunctionExpression: converts arrow, wraps expression bodies', () => {
|
|
134
|
-
const arrowExprBody = t.arrowFunctionExpression( [ t.identifier( 'x' ) ], t.identifier( 'x' ) );
|
|
135
|
-
const arrowBlockBody = t.arrowFunctionExpression( [], t.blockStatement( [ t.returnStatement( t.numericLiteral( 1 ) ) ] ) );
|
|
136
|
-
const fn1 = toFunctionExpression( arrowExprBody );
|
|
137
|
-
const fn2 = toFunctionExpression( arrowBlockBody );
|
|
138
|
-
expect( t.isFunctionExpression( fn1 ) ).toBe( true );
|
|
139
|
-
expect( t.isBlockStatement( fn1.body ) ).toBe( true );
|
|
140
|
-
expect( t.isReturnStatement( fn1.body.body[0] ) ).toBe( true );
|
|
141
|
-
expect( t.isFunctionExpression( fn2 ) ).toBe( true );
|
|
142
|
-
} );
|
|
143
|
-
|
|
144
131
|
it( 'isStepsPath: matches LOCAL steps.js (no path traversal)', () => {
|
|
145
132
|
// Local steps (without ../ or /shared/)
|
|
146
133
|
expect( isStepsPath( 'steps.js' ) ).toBe( true );
|
|
@@ -226,16 +213,6 @@ describe( 'workflow_rewriter tools', () => {
|
|
|
226
213
|
expect( isSharedEvaluatorsPath( 'steps.js' ) ).toBe( false );
|
|
227
214
|
} );
|
|
228
215
|
|
|
229
|
-
it( 'createThisMethodCall: builds this.method(\'name\', ...args) call', () => {
|
|
230
|
-
const call = createThisMethodCall( 'invoke', 'n', [ t.numericLiteral( 1 ), t.identifier( 'x' ) ] );
|
|
231
|
-
expect( t.isCallExpression( call ) ).toBe( true );
|
|
232
|
-
expect( t.isMemberExpression( call.callee ) ).toBe( true );
|
|
233
|
-
expect( t.isThisExpression( call.callee.object ) ).toBe( true );
|
|
234
|
-
expect( t.isIdentifier( call.callee.property, { name: 'invoke' } ) ).toBe( true );
|
|
235
|
-
expect( t.isStringLiteral( call.arguments[0], { value: 'n' } ) ).toBe( true );
|
|
236
|
-
expect( call.arguments.length ).toBe( 3 );
|
|
237
|
-
} );
|
|
238
|
-
|
|
239
216
|
it( 'buildSharedStepsNameMap: reads names from shared steps module and caches result', () => {
|
|
240
217
|
const dir = mkdtempSync( join( tmpdir(), 'tools-shared-steps-' ) );
|
|
241
218
|
mkdirSync( join( dir, 'shared', 'steps' ), { recursive: true } );
|
|
@@ -43,7 +43,7 @@ const removeRequireDeclarator = path => {
|
|
|
43
43
|
|
|
44
44
|
const collectDestructuredRequires = ( path, absolutePath, req, descriptors ) => {
|
|
45
45
|
const propFilter = p => isObjectProperty( p ) && isIdentifier( p.key );
|
|
46
|
-
for ( const { match, buildMap, cache,
|
|
46
|
+
for ( const { match, buildMap, cache, label, target } of descriptors ) {
|
|
47
47
|
if ( !match( req ) ) {
|
|
48
48
|
continue;
|
|
49
49
|
}
|
|
@@ -54,7 +54,7 @@ const collectDestructuredRequires = ( path, absolutePath, req, descriptors ) =>
|
|
|
54
54
|
if ( localName ) {
|
|
55
55
|
const resolved = nameMap.get( importedName );
|
|
56
56
|
if ( resolved ) {
|
|
57
|
-
target.push( { localName,
|
|
57
|
+
target.push( { localName, activityName: resolved } );
|
|
58
58
|
} else {
|
|
59
59
|
throw unresolvedImportError( importedName, label, absolutePath );
|
|
60
60
|
}
|
|
@@ -67,25 +67,26 @@ const collectDestructuredRequires = ( path, absolutePath, req, descriptors ) =>
|
|
|
67
67
|
|
|
68
68
|
/**
|
|
69
69
|
* Collect and strip target imports and requires from an AST, producing
|
|
70
|
-
*
|
|
70
|
+
* activity import mappings for later rewrites.
|
|
71
71
|
*
|
|
72
72
|
* Mutates the AST by removing matching import declarations and require declarators.
|
|
73
73
|
*
|
|
74
74
|
* @param {import('@babel/types').File} ast - Parsed file AST.
|
|
75
75
|
* @param {string} fileDir - Absolute directory of the file represented by `ast`.
|
|
76
|
-
* @param {{
|
|
76
|
+
* @param {{
|
|
77
|
+
* stepsNameCache: Map<string,Map<string,string>>,
|
|
78
|
+
* evaluatorsNameCache: Map<string,Map<string,string>>,
|
|
79
|
+
* sharedStepsNameCache: Map<string,Map<string,string>>,
|
|
80
|
+
* sharedEvaluatorsNameCache: Map<string,Map<string,string>>
|
|
81
|
+
* }} caches
|
|
77
82
|
* Resolved-name caches to avoid re-reading same modules.
|
|
78
|
-
* @returns {{
|
|
79
|
-
* evaluatorImports: Array<{localName:string,evaluatorName:string}> }} Collected info mappings.
|
|
83
|
+
* @returns {{ activityImports: Array<{localName:string,activityName:string}> }} Collected import mappings.
|
|
80
84
|
*/
|
|
81
85
|
export default function collectTargetImports(
|
|
82
86
|
ast, fileDir,
|
|
83
87
|
{ stepsNameCache, evaluatorsNameCache, sharedStepsNameCache, sharedEvaluatorsNameCache }
|
|
84
88
|
) {
|
|
85
|
-
const
|
|
86
|
-
const sharedStepImports = [];
|
|
87
|
-
const evaluatorImports = [];
|
|
88
|
-
const sharedEvaluatorImports = [];
|
|
89
|
+
const activityImports = [];
|
|
89
90
|
|
|
90
91
|
traverse( ast, {
|
|
91
92
|
ImportDeclaration: path => {
|
|
@@ -98,7 +99,7 @@ export default function collectTargetImports(
|
|
|
98
99
|
}
|
|
99
100
|
|
|
100
101
|
const absolutePath = toAbsolutePath( fileDir, src );
|
|
101
|
-
const collectNamedImports = ( match, buildMapFn, cache,
|
|
102
|
+
const collectNamedImports = ( match, buildMapFn, cache, fileLabel ) => {
|
|
102
103
|
if ( !match ) {
|
|
103
104
|
return;
|
|
104
105
|
}
|
|
@@ -108,22 +109,17 @@ export default function collectTargetImports(
|
|
|
108
109
|
const localName = s.local.name;
|
|
109
110
|
const value = nameMap.get( importedName );
|
|
110
111
|
if ( value ) {
|
|
111
|
-
|
|
112
|
-
entry[valueKey] = value;
|
|
113
|
-
targetArr.push( entry );
|
|
112
|
+
activityImports.push( { localName, activityName: value } );
|
|
114
113
|
} else {
|
|
115
114
|
throw unresolvedImportError( importedName, fileLabel, absolutePath );
|
|
116
115
|
}
|
|
117
116
|
}
|
|
118
117
|
};
|
|
119
118
|
|
|
120
|
-
collectNamedImports( isStepsPath( src ), buildStepsNameMap, stepsNameCache,
|
|
121
|
-
collectNamedImports( isSharedStepsPath( src ), buildSharedStepsNameMap, sharedStepsNameCache,
|
|
122
|
-
collectNamedImports( isEvaluatorsPath( src ), buildEvaluatorsNameMap, evaluatorsNameCache,
|
|
123
|
-
collectNamedImports(
|
|
124
|
-
isSharedEvaluatorsPath( src ), buildSharedEvaluatorsNameMap,
|
|
125
|
-
sharedEvaluatorsNameCache, sharedEvaluatorImports, 'evaluatorName', 'shared evaluators'
|
|
126
|
-
);
|
|
119
|
+
collectNamedImports( isStepsPath( src ), buildStepsNameMap, stepsNameCache, 'steps' );
|
|
120
|
+
collectNamedImports( isSharedStepsPath( src ), buildSharedStepsNameMap, sharedStepsNameCache, 'shared steps' );
|
|
121
|
+
collectNamedImports( isEvaluatorsPath( src ), buildEvaluatorsNameMap, evaluatorsNameCache, 'evaluators' );
|
|
122
|
+
collectNamedImports( isSharedEvaluatorsPath( src ), buildSharedEvaluatorsNameMap, sharedEvaluatorsNameCache, 'shared evaluators' );
|
|
127
123
|
path.remove();
|
|
128
124
|
},
|
|
129
125
|
VariableDeclarator: path => {
|
|
@@ -153,25 +149,21 @@ export default function collectTargetImports(
|
|
|
153
149
|
const cjsDescriptors = [
|
|
154
150
|
{
|
|
155
151
|
match: isStepsPath, buildMap: buildStepsNameMap,
|
|
156
|
-
cache: stepsNameCache, target:
|
|
157
|
-
valueKey: 'stepName', label: 'steps'
|
|
152
|
+
cache: stepsNameCache, target: activityImports, label: 'steps'
|
|
158
153
|
},
|
|
159
154
|
{
|
|
160
155
|
match: isSharedStepsPath, buildMap: buildSharedStepsNameMap,
|
|
161
|
-
cache: sharedStepsNameCache
|
|
162
|
-
target:
|
|
163
|
-
valueKey: 'stepName', label: 'shared steps'
|
|
156
|
+
cache: sharedStepsNameCache,
|
|
157
|
+
target: activityImports, label: 'shared steps'
|
|
164
158
|
},
|
|
165
159
|
{
|
|
166
160
|
match: isEvaluatorsPath, buildMap: buildEvaluatorsNameMap,
|
|
167
|
-
cache: evaluatorsNameCache, target:
|
|
168
|
-
valueKey: 'evaluatorName', label: 'evaluators'
|
|
161
|
+
cache: evaluatorsNameCache, target: activityImports, label: 'evaluators'
|
|
169
162
|
},
|
|
170
163
|
{
|
|
171
164
|
match: isSharedEvaluatorsPath, buildMap: buildSharedEvaluatorsNameMap,
|
|
172
|
-
cache: sharedEvaluatorsNameCache
|
|
173
|
-
target:
|
|
174
|
-
valueKey: 'evaluatorName', label: 'shared evaluators'
|
|
165
|
+
cache: sharedEvaluatorsNameCache,
|
|
166
|
+
target: activityImports, label: 'shared evaluators'
|
|
175
167
|
}
|
|
176
168
|
];
|
|
177
169
|
collectDestructuredRequires(
|
|
@@ -182,5 +174,5 @@ export default function collectTargetImports(
|
|
|
182
174
|
}
|
|
183
175
|
} );
|
|
184
176
|
|
|
185
|
-
return {
|
|
177
|
+
return { activityImports };
|
|
186
178
|
}
|