@output.ai/core 0.2.1 → 0.3.0-dev.pr263-a59dd0e

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.
@@ -24,7 +24,15 @@ import {
24
24
  isExportDefaultDeclaration,
25
25
  isFunctionDeclaration
26
26
  } from '@babel/types';
27
- import { ComponentFile, EXTRANEOUS_FILE, ExtraneousFileList, NodeType } from './consts.js';
27
+ import { ComponentFile, NodeType } from './consts.js';
28
+
29
+ // Path pattern regexes - shared across multiple helper functions
30
+ const STEPS_FILE_REGEX = /(^|\/)steps\.js$/;
31
+ const STEPS_FOLDER_REGEX = /\/steps\/[^/]+\.js$/;
32
+ const EVALUATORS_FILE_REGEX = /(^|\/)evaluators\.js$/;
33
+ const EVALUATORS_FOLDER_REGEX = /\/evaluators\/[^/]+\.js$/;
34
+ const PATH_TRAVERSAL_REGEX = /\.\.\//;
35
+ const SHARED_PATH_REGEX = /\/shared\//;
28
36
 
29
37
  /**
30
38
  * Resolve a relative module specifier against a base directory.
@@ -104,25 +112,67 @@ export const toFunctionExpression = arrow => {
104
112
  };
105
113
 
106
114
  /**
107
- * Check if a module specifier or request string points to steps.js.
115
+ * Check if a module specifier or request string points to steps.js or is in a steps folder.
116
+ * Matches: steps.js, /steps.js, /steps/*.js
117
+ * This matches LOCAL steps only (no path traversal).
118
+ * @param {string} value - Module path or request string.
119
+ * @returns {boolean} True if it matches a local steps path.
120
+ */
121
+ export const isStepsPath = value => {
122
+ // Exclude shared steps (paths with ../ or containing /shared/)
123
+ if ( PATH_TRAVERSAL_REGEX.test( value ) || SHARED_PATH_REGEX.test( value ) ) {
124
+ return false;
125
+ }
126
+ return STEPS_FILE_REGEX.test( value ) || STEPS_FOLDER_REGEX.test( value );
127
+ };
128
+
129
+ /**
130
+ * Check if a module specifier or request string points to shared steps.
131
+ * Shared steps are steps imported from outside the current workflow directory.
132
+ * Matches paths with ../ traversal or /shared/ and containing steps pattern.
133
+ * @param {string} value - Module path or request string.
134
+ * @returns {boolean} True if it matches a shared steps path.
135
+ */
136
+ export const isSharedStepsPath = value => {
137
+ const hasStepsPattern = STEPS_FILE_REGEX.test( value ) || STEPS_FOLDER_REGEX.test( value );
138
+ if ( !hasStepsPattern ) {
139
+ return false;
140
+ }
141
+ return PATH_TRAVERSAL_REGEX.test( value ) || SHARED_PATH_REGEX.test( value );
142
+ };
143
+
144
+ /**
145
+ * Check if a path matches any steps pattern (local or shared).
146
+ * Used for validation purposes.
108
147
  * @param {string} value - Module path or request string.
109
- * @returns {boolean} True if it matches steps.js.
148
+ * @returns {boolean} True if it matches any steps path pattern.
110
149
  */
111
- export const isStepsPath = value => /(^|\/)steps\.js$/.test( value );
150
+ export const isAnyStepsPath = value =>
151
+ STEPS_FILE_REGEX.test( value ) || STEPS_FOLDER_REGEX.test( value );
112
152
 
113
153
  /**
114
- * Check if a module specifier or request string points to shared_steps.js.
154
+ * Check if a module specifier or request string points to evaluators.js or is in an evaluators folder.
155
+ * Matches: evaluators.js, /evaluators.js, /evaluators/*.js
115
156
  * @param {string} value - Module path or request string.
116
- * @returns {boolean} True if it matches shared_steps.js.
157
+ * @returns {boolean} True if it matches an evaluators path.
117
158
  */
118
- export const isSharedStepsPath = value => /(^|\/)shared_steps\.js$/.test( value );
159
+ export const isEvaluatorsPath = value =>
160
+ EVALUATORS_FILE_REGEX.test( value ) || EVALUATORS_FOLDER_REGEX.test( value );
119
161
 
120
162
  /**
121
- * Check if a module specifier or request string points to evaluators.js.
163
+ * Check if a module specifier or request string points to shared evaluators.
164
+ * Shared evaluators are evaluators imported from outside the current workflow directory.
165
+ * Matches paths with ../ traversal or /shared/ and containing evaluators pattern.
122
166
  * @param {string} value - Module path or request string.
123
- * @returns {boolean} True if it matches evaluators.js.
167
+ * @returns {boolean} True if it matches a shared evaluators path.
124
168
  */
125
- export const isEvaluatorsPath = value => /(^|\/)evaluators\.js$/.test( value );
169
+ export const isSharedEvaluatorsPath = value => {
170
+ const hasEvaluatorsPattern = EVALUATORS_FILE_REGEX.test( value ) || EVALUATORS_FOLDER_REGEX.test( value );
171
+ if ( !hasEvaluatorsPattern ) {
172
+ return false;
173
+ }
174
+ return PATH_TRAVERSAL_REGEX.test( value ) || SHARED_PATH_REGEX.test( value );
175
+ };
126
176
 
127
177
  /**
128
178
  * Check if a module specifier or request string points to workflow.js.
@@ -132,35 +182,29 @@ export const isEvaluatorsPath = value => /(^|\/)evaluators\.js$/.test( value );
132
182
  export const isWorkflowPath = value => /(^|\/)workflow\.js$/.test( value );
133
183
 
134
184
  /**
135
- * Check if a module specifier or request string points to types.js.
185
+ * Check if a path is a component file (steps, evaluators, or workflow).
136
186
  * @param {string} value - Module path or request string.
137
- * @returns {boolean} True if it matches types.js.
187
+ * @returns {boolean} True if it matches any component file path.
138
188
  */
139
- export const isTypesPath = value => /(^|\/)types\.js$/.test( value );
140
-
141
- export const isExtraneousFile = value => ExtraneousFileList.map( t => new RegExp( `(^|\/)${t}\\.js$` ) ).find( rx => rx.test( value ) );
189
+ export const isComponentFile = value =>
190
+ isAnyStepsPath( value ) || isEvaluatorsPath( value ) || isWorkflowPath( value );
142
191
 
143
192
  /**
144
193
  * Determine file kind based on its path.
145
- * @param {string} filename
146
- * @returns {'workflow'|'steps'|'shared_steps'|'evaluators'|null}
194
+ * Returns the component type if it's a component file, null otherwise.
195
+ * @param {string} path
196
+ * @returns {'workflow'|'steps'|'evaluators'|null}
147
197
  */
148
198
  export const getFileKind = path => {
149
- if ( isStepsPath( path ) ) {
199
+ if ( isAnyStepsPath( path ) ) {
150
200
  return ComponentFile.STEPS;
151
201
  }
152
- if ( isSharedStepsPath( path ) ) {
153
- return ComponentFile.SHARED_STEPS;
154
- }
155
202
  if ( isEvaluatorsPath( path ) ) {
156
203
  return ComponentFile.EVALUATORS;
157
204
  }
158
205
  if ( isWorkflowPath( path ) ) {
159
206
  return ComponentFile.WORKFLOW;
160
207
  }
161
- if ( isExtraneousFile( path ) ) {
162
- return EXTRANEOUS_FILE;
163
- }
164
208
  return null;
165
209
  };
166
210
 
@@ -283,12 +327,12 @@ export const buildStepsNameMap = ( path, cache ) => buildComponentNameMap( {
283
327
 
284
328
  /**
285
329
  * Build a map from exported shared step identifier to declared step name.
286
- * Parses `shared_steps.js` for `export const X = step({ name: '...' })`.
287
- * Uses the same factory as regular steps.
330
+ * Same as buildStepsNameMap but for shared steps.
288
331
  *
289
332
  * @param {string} path - Absolute path to the shared steps module file.
290
- * @param {Map<string, Map<string,string>>} cache - Cache of computed name maps.
333
+ * @param {Map<string, Map<string,string>>} cache - Cache of computed step name maps.
291
334
  * @returns {Map<string,string>} Exported identifier -> step name.
335
+ * @throws {Error} When a step name is invalid (non-static or missing).
292
336
  */
293
337
  export const buildSharedStepsNameMap = ( path, cache ) => buildComponentNameMap( {
294
338
  path,
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
2
+ import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from 'node:fs';
3
3
  import { tmpdir } from 'node:os';
4
4
  import { join, resolve as resolvePath } from 'node:path';
5
5
  import * as t from '@babel/types';
@@ -12,7 +12,9 @@ import {
12
12
  toFunctionExpression,
13
13
  isStepsPath,
14
14
  isSharedStepsPath,
15
+ isAnyStepsPath,
15
16
  isEvaluatorsPath,
17
+ isSharedEvaluatorsPath,
16
18
  isWorkflowPath,
17
19
  createThisMethodCall,
18
20
  resolveNameFromOptions,
@@ -20,7 +22,6 @@ import {
20
22
  buildSharedStepsNameMap,
21
23
  buildWorkflowNameMap,
22
24
  buildEvaluatorsNameMap,
23
- isExtraneousFile,
24
25
  getFileKind
25
26
  } from './tools.js';
26
27
 
@@ -138,14 +139,49 @@ describe( 'workflow_rewriter tools', () => {
138
139
  expect( t.isFunctionExpression( fn2 ) ).toBe( true );
139
140
  } );
140
141
 
141
- it( 'isStepsPath: matches steps.js at root or subpath', () => {
142
+ it( 'isStepsPath: matches LOCAL steps.js (no path traversal)', () => {
143
+ // Local steps (without ../ or /shared/)
142
144
  expect( isStepsPath( 'steps.js' ) ).toBe( true );
143
145
  expect( isStepsPath( './steps.js' ) ).toBe( true );
144
146
  expect( isStepsPath( '/a/b/steps.js' ) ).toBe( true );
147
+ expect( isStepsPath( './steps/fetch.js' ) ).toBe( true );
148
+ // Shared steps (with ../ or /shared/) should NOT match isStepsPath
149
+ expect( isStepsPath( '../steps.js' ) ).toBe( false );
150
+ expect( isStepsPath( '../../shared/steps/common.js' ) ).toBe( false );
151
+ // Non-steps
145
152
  expect( isStepsPath( 'steps.ts' ) ).toBe( false );
146
153
  expect( isStepsPath( 'workflow.js' ) ).toBe( false );
147
154
  } );
148
155
 
156
+ it( 'isSharedStepsPath: matches steps imported from outside workflow directory', () => {
157
+ // Shared steps: must have steps pattern AND have path traversal or /shared/
158
+ expect( isSharedStepsPath( '../steps.js' ) ).toBe( true );
159
+ expect( isSharedStepsPath( '../../steps.js' ) ).toBe( true );
160
+ expect( isSharedStepsPath( '../../shared/steps/common.js' ) ).toBe( true );
161
+ expect( isSharedStepsPath( '../other_workflow/steps.js' ) ).toBe( true );
162
+ expect( isSharedStepsPath( '/src/shared/steps/common.js' ) ).toBe( true );
163
+ // Local steps (no traversal, no /shared/) should NOT match
164
+ expect( isSharedStepsPath( './steps.js' ) ).toBe( false );
165
+ expect( isSharedStepsPath( 'steps.js' ) ).toBe( false );
166
+ expect( isSharedStepsPath( './steps/fetch.js' ) ).toBe( false );
167
+ // Non-steps should NOT match
168
+ expect( isSharedStepsPath( '../utils.js' ) ).toBe( false );
169
+ expect( isSharedStepsPath( 'evaluators.js' ) ).toBe( false );
170
+ } );
171
+
172
+ it( 'isAnyStepsPath: matches any steps pattern (local or shared)', () => {
173
+ // Local steps
174
+ expect( isAnyStepsPath( 'steps.js' ) ).toBe( true );
175
+ expect( isAnyStepsPath( './steps.js' ) ).toBe( true );
176
+ expect( isAnyStepsPath( './steps/fetch.js' ) ).toBe( true );
177
+ // Shared steps
178
+ expect( isAnyStepsPath( '../steps.js' ) ).toBe( true );
179
+ expect( isAnyStepsPath( '../../shared/steps/common.js' ) ).toBe( true );
180
+ // Non-steps
181
+ expect( isAnyStepsPath( 'workflow.js' ) ).toBe( false );
182
+ expect( isAnyStepsPath( 'utils.js' ) ).toBe( false );
183
+ } );
184
+
149
185
  it( 'isWorkflowPath: matches workflow.js at root or subpath', () => {
150
186
  expect( isWorkflowPath( 'workflow.js' ) ).toBe( true );
151
187
  expect( isWorkflowPath( './workflow.js' ) ).toBe( true );
@@ -154,63 +190,29 @@ describe( 'workflow_rewriter tools', () => {
154
190
  expect( isWorkflowPath( 'steps.js' ) ).toBe( false );
155
191
  } );
156
192
 
157
- it( 'isSharedStepsPath: matches shared_steps.js at root or subpath', () => {
158
- expect( isSharedStepsPath( 'shared_steps.js' ) ).toBe( true );
159
- expect( isSharedStepsPath( './shared_steps.js' ) ).toBe( true );
160
- expect( isSharedStepsPath( '/a/b/shared_steps.js' ) ).toBe( true );
161
- expect( isSharedStepsPath( 'shared_steps.ts' ) ).toBe( false );
162
- expect( isSharedStepsPath( 'evaluators.js' ) ).toBe( false );
163
- } );
164
-
165
193
  it( 'isEvaluatorsPath: matches evaluators.js at root or subpath', () => {
166
194
  expect( isEvaluatorsPath( 'evaluators.js' ) ).toBe( true );
167
195
  expect( isEvaluatorsPath( './evaluators.js' ) ).toBe( true );
168
196
  expect( isEvaluatorsPath( '/a/b/evaluators.js' ) ).toBe( true );
197
+ expect( isEvaluatorsPath( './evaluators/quality.js' ) ).toBe( true );
169
198
  expect( isEvaluatorsPath( 'evaluators.ts' ) ).toBe( false );
170
199
  expect( isEvaluatorsPath( 'steps.js' ) ).toBe( false );
171
200
  } );
172
201
 
173
- it( 'isExtraneousFile: returns truthy for extraneous files (types/consts/constants/vars/variables)', () => {
174
- const ok = [
175
- 'types.js',
176
- './types.js',
177
- '/a/b/types.js',
178
- 'consts.js',
179
- './consts.js',
180
- '/a/b/consts.js',
181
- 'constants.js',
182
- './constants.js',
183
- '/a/b/constants.js',
184
- 'vars.js',
185
- './vars.js',
186
- '/a/b/vars.js',
187
- 'variables.js',
188
- './variables.js',
189
- '/a/b/variables.js'
190
- ];
191
- for ( const p of ok ) {
192
- expect( Boolean( isExtraneousFile( p ) ) ).toBe( true );
193
- }
194
- } );
195
-
196
- it( 'isExtraneousFile: returns falsy for non-extraneous or non-.js files', () => {
197
- const bad = [
198
- 'types.ts',
199
- '/a/b/types.ts',
200
- 'types.mjs',
201
- 'variables.jsx',
202
- 'variables.mjs',
203
- 'myconstants.js',
204
- 'steps.js',
205
- 'evaluators.js',
206
- 'shared_steps.js',
207
- 'workflow.js',
208
- 'typess.js',
209
- '/a/b/c/variables.json'
210
- ];
211
- for ( const p of bad ) {
212
- expect( isExtraneousFile( p ) ).toBeFalsy();
213
- }
202
+ it( 'isSharedEvaluatorsPath: matches evaluators imported from outside workflow directory', () => {
203
+ // Shared evaluators: must have evaluators pattern AND have path traversal or /shared/
204
+ expect( isSharedEvaluatorsPath( '../evaluators.js' ) ).toBe( true );
205
+ expect( isSharedEvaluatorsPath( '../../evaluators.js' ) ).toBe( true );
206
+ expect( isSharedEvaluatorsPath( '../../shared/evaluators/quality.js' ) ).toBe( true );
207
+ expect( isSharedEvaluatorsPath( '../other_workflow/evaluators.js' ) ).toBe( true );
208
+ expect( isSharedEvaluatorsPath( '/src/shared/evaluators/quality.js' ) ).toBe( true );
209
+ // Local evaluators (no traversal, no /shared/) should NOT match
210
+ expect( isSharedEvaluatorsPath( './evaluators.js' ) ).toBe( false );
211
+ expect( isSharedEvaluatorsPath( 'evaluators.js' ) ).toBe( false );
212
+ expect( isSharedEvaluatorsPath( './evaluators/quality.js' ) ).toBe( false );
213
+ // Non-evaluators should NOT match
214
+ expect( isSharedEvaluatorsPath( '../utils.js' ) ).toBe( false );
215
+ expect( isSharedEvaluatorsPath( 'steps.js' ) ).toBe( false );
214
216
  } );
215
217
 
216
218
  it( 'createThisMethodCall: builds this.method(\'name\', ...args) call', () => {
@@ -223,9 +225,10 @@ describe( 'workflow_rewriter tools', () => {
223
225
  expect( call.arguments.length ).toBe( 3 );
224
226
  } );
225
227
 
226
- it( 'buildSharedStepsNameMap: reads names from shared_steps module and caches result', () => {
228
+ it( 'buildSharedStepsNameMap: reads names from shared steps module and caches result', () => {
227
229
  const dir = mkdtempSync( join( tmpdir(), 'tools-shared-steps-' ) );
228
- const stepsPath = join( dir, 'shared_steps.js' );
230
+ mkdirSync( join( dir, 'shared', 'steps' ), { recursive: true } );
231
+ const stepsPath = join( dir, 'shared', 'steps', 'common.js' );
229
232
  writeFileSync( stepsPath, [
230
233
  'export const StepA = step({ name: "shared.step.a" })',
231
234
  'export const StepB = step({ name: "shared.step.b" })'
@@ -243,9 +246,13 @@ describe( 'workflow_rewriter tools', () => {
243
246
  it( 'getFileKind: classifies file by its path', () => {
244
247
  expect( getFileKind( '/p/workflow.js' ) ).toBe( 'workflow' );
245
248
  expect( getFileKind( '/p/steps.js' ) ).toBe( 'steps' );
246
- expect( getFileKind( '/p/shared_steps.js' ) ).toBe( 'shared_steps' );
249
+ // Files in steps folder are steps
250
+ expect( getFileKind( '/p/steps/fetch.js' ) ).toBe( 'steps' );
251
+ expect( getFileKind( '/p/shared/steps/common.js' ) ).toBe( 'steps' );
247
252
  expect( getFileKind( '/p/evaluators.js' ) ).toBe( 'evaluators' );
253
+ expect( getFileKind( '/p/evaluators/quality.js' ) ).toBe( 'evaluators' );
248
254
  expect( getFileKind( '/p/other.js' ) ).toBe( null );
255
+ expect( getFileKind( '/p/utils.js' ) ).toBe( null );
256
+ expect( getFileKind( '/p/clients/api.js' ) ).toBe( null );
249
257
  } );
250
258
  } );
251
-
@@ -1,5 +1,5 @@
1
1
  import { describe, it, expect } from 'vitest';
2
- import { mkdtempSync, writeFileSync, rmSync } from 'node:fs';
2
+ import { mkdtempSync, writeFileSync, rmSync, mkdirSync } from 'node:fs';
3
3
  import { tmpdir } from 'node:os';
4
4
  import { join } from 'node:path';
5
5
  import loader from './index.mjs';
@@ -48,12 +48,15 @@ const obj = {
48
48
  rmSync( dir, { recursive: true, force: true } );
49
49
  } );
50
50
 
51
- it( 'rewrites ESM shared_steps imports to invokeSharedStep', async () => {
51
+ it( 'rewrites ESM shared steps imports to invokeSharedStep', async () => {
52
+ // Create directory structure: shared/steps/common.js
52
53
  const dir = mkdtempSync( join( tmpdir(), 'ast-loader-esm-shared-' ) );
53
- writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedA = step({ name: \'shared.a\' });' );
54
+ mkdirSync( join( dir, 'shared', 'steps' ), { recursive: true } );
55
+ mkdirSync( join( dir, 'workflows', 'my_workflow' ), { recursive: true } );
56
+ writeFileSync( join( dir, 'shared', 'steps', 'common.js' ), 'export const SharedA = step({ name: \'shared.a\' });' );
54
57
 
55
58
  const source = `
56
- import { SharedA } from './shared_steps.js';
59
+ import { SharedA } from '../../shared/steps/common.js';
57
60
 
58
61
  const obj = {
59
62
  fn: async (x) => {
@@ -61,21 +64,24 @@ const obj = {
61
64
  }
62
65
  }`;
63
66
 
64
- const { code } = await runLoader( source, join( dir, 'file.js' ) );
67
+ const { code } = await runLoader( source, join( dir, 'workflows', 'my_workflow', 'workflow.js' ) );
65
68
 
66
- expect( code ).not.toMatch( /from '\.\/shared_steps\.js'/ );
69
+ expect( code ).not.toMatch( /from '\.\.\/\.\.\/shared\/steps\/common\.js'/ );
67
70
  expect( code ).toMatch( /fn:\s*async function \(x\)/ );
68
71
  expect( code ).toMatch( /this\.invokeSharedStep\('shared\.a',\s*1\)/ );
69
72
 
70
73
  rmSync( dir, { recursive: true, force: true } );
71
74
  } );
72
75
 
73
- it( 'rewrites CJS shared_steps requires to invokeSharedStep', async () => {
76
+ it( 'rewrites CJS shared steps requires to invokeSharedStep', async () => {
77
+ // Create directory structure: shared/steps/common.js
74
78
  const dir = mkdtempSync( join( tmpdir(), 'ast-loader-cjs-shared-' ) );
75
- writeFileSync( join( dir, 'shared_steps.js' ), 'export const SharedB = step({ name: \'shared.b\' });' );
79
+ mkdirSync( join( dir, 'shared', 'steps' ), { recursive: true } );
80
+ mkdirSync( join( dir, 'workflows', 'my_workflow' ), { recursive: true } );
81
+ writeFileSync( join( dir, 'shared', 'steps', 'common.js' ), 'export const SharedB = step({ name: \'shared.b\' });' );
76
82
 
77
83
  const source = `
78
- const { SharedB } = require( './shared_steps.js' );
84
+ const { SharedB } = require( '../../shared/steps/common.js' );
79
85
 
80
86
  const obj = {
81
87
  fn: async (y) => {
@@ -83,9 +89,9 @@ const obj = {
83
89
  }
84
90
  }`;
85
91
 
86
- const { code } = await runLoader( source, join( dir, 'file.js' ) );
92
+ const { code } = await runLoader( source, join( dir, 'workflows', 'my_workflow', 'workflow.js' ) );
87
93
 
88
- expect( code ).not.toMatch( /require\('\.\/shared_steps\.js'\)/ );
94
+ expect( code ).not.toMatch( /require\('\.\.\/\.\.\/shared\/steps\/common\.js'\)/ );
89
95
  expect( code ).toMatch( /fn:\s*async function \(y\)/ );
90
96
  expect( code ).toMatch( /this\.invokeSharedStep\('shared\.b'\)/ );
91
97
 
@@ -1,7 +1,7 @@
1
1
  import traverseModule from '@babel/traverse';
2
2
  import { dirname } from 'node:path';
3
- import { parse, toAbsolutePath, getFileKind } from '../tools.js';
4
- import { ComponentFile, CoreModule, EXTRANEOUS_FILE, ExtraneousFileList } from '../consts.js';
3
+ import { parse, toAbsolutePath, getFileKind, isAnyStepsPath, isEvaluatorsPath, isWorkflowPath } from '../tools.js';
4
+ import { ComponentFile, CoreModule } from '../consts.js';
5
5
  import {
6
6
  isCallExpression,
7
7
  isFunctionExpression,
@@ -18,36 +18,92 @@ import {
18
18
  const traverse = traverseModule.default ?? traverseModule;
19
19
 
20
20
  /**
21
- * Check if workflow dependencies
21
+ * Determine the file kind label for error messages.
22
+ * Handles both flat files (steps.js) and folder-based files (steps/fetch_data.js).
23
+ * @param {string} filename - The file path
24
+ * @returns {string} Human-readable file kind for error messages
25
+ */
26
+ const getFileKindLabel = filename => {
27
+ if ( isAnyStepsPath( filename ) ) {
28
+ return 'steps.js';
29
+ }
30
+ if ( isEvaluatorsPath( filename ) ) {
31
+ return 'evaluators.js';
32
+ }
33
+ if ( /workflow\.js$/.test( filename ) ) {
34
+ return 'workflow.js';
35
+ }
36
+ return filename;
37
+ };
38
+
39
+ /**
40
+ * Check if workflow dependencies are valid.
41
+ * Workflows can import:
42
+ * - Components (steps, evaluators, workflow)
43
+ * - Core modules (@output.ai/core, local_core)
44
+ * - ANY file that is NOT a component file (flexible utility imports)
22
45
  */
23
46
  const validateWorkflowImports = ( { specifier, filename } ) => {
24
47
  const isCore = Object.values( CoreModule ).includes( specifier );
25
- const isComponent = Object.values( ComponentFile ).includes( getFileKind( specifier ) );
26
- const isAllowedExtraneous = getFileKind( specifier ) === EXTRANEOUS_FILE;
27
- if ( !isCore && !isComponent && !isAllowedExtraneous ) {
48
+ const fileKind = getFileKind( specifier );
49
+ const isComponent = Object.values( ComponentFile ).includes( fileKind );
50
+ const isNonComponentFile = fileKind === null;
51
+
52
+ if ( !isCore && !isComponent && !isNonComponentFile ) {
28
53
  throw new Error( `Invalid dependency in workflow.js: '${specifier}'. \
29
- Only components (${Object.values( ComponentFile ) } ), @output.ai/core, or whitelisted (${ExtraneousFileList}) imports are allowed in ${filename}` );
54
+ Only components (${Object.values( ComponentFile ) } ), @output.ai/core, or non-component files are allowed in ${filename}` );
30
55
  }
31
56
  };
32
57
 
33
58
  /**
34
- * Check if evaluators, steps or shared_steps import invalid dependencies
59
+ * Check if evaluators or steps import invalid dependencies.
60
+ * Steps and evaluators CANNOT import:
61
+ * - Other steps (local or shared) - activity isolation
62
+ * - Other evaluators (local or shared) - activity isolation
63
+ * - Workflows
64
+ *
65
+ * Steps and evaluators CAN import:
66
+ * - ANY file that is NOT a component file (flexible utility imports)
35
67
  */
36
- const validateStepEvaluatorImports = ( { fileKind, specifier, filename } ) => {
37
- if ( Object.values( ComponentFile ).includes( getFileKind( specifier ) ) ) {
38
- throw new Error( `Invalid dependency in ${fileKind}.js: '${specifier}'. \
39
- Steps, shared_steps, evaluators or workflows are not allowed dependencies in ${filename}` );
68
+ const validateStepEvaluatorImports = ( { specifier, filename } ) => {
69
+ const importedFileKind = getFileKind( specifier );
70
+
71
+ // Activity isolation: steps/evaluators cannot import other steps, evaluators, or workflows
72
+ if ( Object.values( ComponentFile ).includes( importedFileKind ) ) {
73
+ const fileLabel = getFileKindLabel( filename );
74
+ throw new Error( `Invalid dependency in ${fileLabel}: '${specifier}'. \
75
+ Steps, evaluators or workflows are not allowed dependencies in ${filename}` );
40
76
  }
41
77
  };
42
78
 
43
79
  /**
44
- * Validate import for evaluators, steps, shared_steps, workflow
80
+ * Validate import for evaluators, steps, workflow
45
81
  */
46
82
  const executeImportValidations = ( { fileKind, specifier, filename } ) => {
47
83
  if ( fileKind === ComponentFile.WORKFLOW ) {
48
- validateWorkflowImports( { fileKind, specifier, filename } );
84
+ validateWorkflowImports( { specifier, filename } );
49
85
  } else if ( Object.values( ComponentFile ).includes( fileKind ) ) {
50
- validateStepEvaluatorImports( { fileKind, specifier, filename } );
86
+ validateStepEvaluatorImports( { specifier, filename } );
87
+ }
88
+ };
89
+
90
+ /**
91
+ * Validate that component instantiation calls occur in the correct file locations.
92
+ * - step() must be called in a file whose path contains 'steps'
93
+ * - evaluator() must be called in a file whose path contains 'evaluators'
94
+ * - workflow() must be called in a file whose path contains 'workflow'
95
+ * @param {string} calleeName - The factory function name (step, evaluator, workflow)
96
+ * @param {string} filename - The file path where the call occurs
97
+ */
98
+ const validateInstantiationLocation = ( calleeName, filename ) => {
99
+ if ( calleeName === 'step' && !isAnyStepsPath( filename ) ) {
100
+ throw new Error( `Invalid instantiation location: step() can only be called in files with 'steps' in the path. Found in: ${filename}` );
101
+ }
102
+ if ( calleeName === 'evaluator' && !isEvaluatorsPath( filename ) ) {
103
+ throw new Error( `Invalid instantiation location: evaluator() can only be called in files with 'evaluators' in the path. Found in: ${filename}` );
104
+ }
105
+ if ( calleeName === 'workflow' && !isWorkflowPath( filename ) ) {
106
+ throw new Error( `Invalid instantiation location: workflow() can only be called in files with 'workflow' in the path. Found in: ${filename}` );
51
107
  }
52
108
  };
53
109
 
@@ -56,13 +112,13 @@ const executeImportValidations = ( { fileKind, specifier, filename } ) => {
56
112
  * Returns the source unchanged unless a validation error is found.
57
113
  *
58
114
  * Rules enforced:
59
- * - evaluators.js `fn`: at each evaluator().fn body: calling any evaluator, step, shared_step or workflow is forbidden
60
- * - evaluators.js: may not import evaluators.js, steps.js/shared_steps.js, workflow.js
61
- * - shared_steps.js `fn`: at each step().fn body: calling any evaluator, step, shared_step or workflow is forbidden
62
- * - shared_steps.js: may not import evaluators.js, steps.js, shared_steps.js, workflow.js
63
- * - steps.js: at each step().fn body: calling any evaluator, step, shared_step or workflow is forbidden
64
- * - steps.js: may not import evaluators.js, steps.js, shared_steps.js, workflow.js
65
- * - workflow.js: may only import components: evaluators.js, steps.js, shared_steps.js, workflow.js; and files: types.js or `@output.ai/core`
115
+ * - Instantiation location: step() must be in steps path, evaluator() in evaluators path, workflow() in workflow path
116
+ * - evaluators.js `fn`: at each evaluator().fn body: calling any evaluator, step, or workflow is forbidden
117
+ * - evaluators.js: may not import evaluators.js, steps.js, workflow.js, or any shared steps/evaluators
118
+ * - steps.js: at each step().fn body: calling any evaluator, step, or workflow is forbidden
119
+ * - steps.js: may not import evaluators.js, steps.js, workflow.js, or any shared steps/evaluators
120
+ * - workflow.js: may import components (evaluators.js, steps.js, workflow.js including shared);
121
+ * and any non-component file, or `@output.ai/core`
66
122
  *
67
123
  * @param {string|Buffer} source
68
124
  * @param {any} inputMap
@@ -82,7 +138,7 @@ export default function workflowValidatorLoader( source, inputMap ) {
82
138
  // Collect local declarations and imported identifiers by type
83
139
  const localStepIds = new Set();
84
140
  const localEvaluatorIds = new Set();
85
- const importedStepIds = new Set(); // includes shared_steps
141
+ const importedStepIds = new Set();
86
142
  const importedEvaluatorIds = new Set();
87
143
  const importedWorkflowIds = new Set();
88
144
 
@@ -94,12 +150,12 @@ export default function workflowValidatorLoader( source, inputMap ) {
94
150
  executeImportValidations( { fileKind, specifier, filename } );
95
151
 
96
152
  // Collect imported identifiers for later call checks
153
+ const importedKind = getFileKind( specifier );
97
154
  const accumulator = ( {
98
155
  [ComponentFile.STEPS]: importedStepIds,
99
- [ComponentFile.SHARED_STEPS]: importedStepIds,
100
156
  [ComponentFile.EVALUATORS]: importedEvaluatorIds,
101
157
  [ComponentFile.WORKFLOW]: importedWorkflowIds
102
- } )[fileKind];
158
+ } )[importedKind];
103
159
  if ( accumulator ) {
104
160
  for ( const s of path.node.specifiers ) {
105
161
  if ( isImportSpecifier( s ) || isImportDefaultSpecifier( s ) ) {
@@ -114,12 +170,21 @@ export default function workflowValidatorLoader( source, inputMap ) {
114
170
  return;
115
171
  }
116
172
 
117
- // Collect local step/evaluator declarations: const X = step({...}) / evaluator({...})
118
- if ( isIdentifier( init.callee, { name: 'step' } ) && isIdentifier( path.node.id ) ) {
119
- localStepIds.add( path.node.id.name );
173
+ // Validate instantiation location for step/evaluator/workflow calls
174
+ if ( isIdentifier( init.callee, { name: 'step' } ) ) {
175
+ validateInstantiationLocation( 'step', filename );
176
+ if ( isIdentifier( path.node.id ) ) {
177
+ localStepIds.add( path.node.id.name );
178
+ }
179
+ }
180
+ if ( isIdentifier( init.callee, { name: 'evaluator' } ) ) {
181
+ validateInstantiationLocation( 'evaluator', filename );
182
+ if ( isIdentifier( path.node.id ) ) {
183
+ localEvaluatorIds.add( path.node.id.name );
184
+ }
120
185
  }
121
- if ( isIdentifier( init.callee, { name: 'evaluator' } ) && isIdentifier( path.node.id ) ) {
122
- localEvaluatorIds.add( path.node.id.name );
186
+ if ( isIdentifier( init.callee, { name: 'workflow' } ) ) {
187
+ validateInstantiationLocation( 'workflow', filename );
123
188
  }
124
189
 
125
190
  // CommonJS requires: validate source and collect identifiers
@@ -134,7 +199,7 @@ export default function workflowValidatorLoader( source, inputMap ) {
134
199
  // Collect imported identifiers from require patterns
135
200
  if ( isStringLiteral( firstArg ) ) {
136
201
  const reqType = getFileKind( toAbsolutePath( fileDir, req ) );
137
- if ( [ ComponentFile.STEPS, ComponentFile.SHARED_STEPS ].includes( reqType ) && isObjectPattern( path.node.id ) ) {
202
+ if ( reqType === ComponentFile.STEPS && isObjectPattern( path.node.id ) ) {
138
203
  for ( const prop of path.node.id.properties ) {
139
204
  if ( isObjectProperty( prop ) && isIdentifier( prop.value ) ) {
140
205
  importedStepIds.add( prop.value.name );
@@ -157,7 +222,7 @@ export default function workflowValidatorLoader( source, inputMap ) {
157
222
  } );
158
223
 
159
224
  // Function-body call validations for steps/evaluators files
160
- if ( [ ComponentFile.STEPS, ComponentFile.SHARED_STEPS, ComponentFile.EVALUATORS ].includes( fileKind ) ) {
225
+ if ( [ ComponentFile.STEPS, ComponentFile.EVALUATORS ].includes( fileKind ) ) {
161
226
  traverse( ast, {
162
227
  ObjectProperty: path => {
163
228
  if ( !isIdentifier( path.node.key, { name: 'fn' } ) ) {
@@ -173,6 +238,7 @@ export default function workflowValidatorLoader( source, inputMap ) {
173
238
  const callee = cPath.node.callee;
174
239
  if ( isIdentifier( callee ) ) {
175
240
  const { name } = callee;
241
+ const fileLabel = getFileKindLabel( filename );
176
242
  const violation = [
177
243
  [ 'step', localStepIds.has( name ) || importedStepIds.has( name ) ],
178
244
  [ 'evaluator', localEvaluatorIds.has( name ) || importedEvaluatorIds.has( name ) ],
@@ -180,7 +246,7 @@ export default function workflowValidatorLoader( source, inputMap ) {
180
246
  ].find( v => v[1] )?.[0];
181
247
 
182
248
  if ( violation ) {
183
- throw new Error( `Invalid call in ${fileKind}.js fn: calling a ${violation} ('${name}') is not allowed in ${filename}` );
249
+ throw new Error( `Invalid call in ${fileLabel} fn: calling a ${violation} ('${name}') is not allowed in ${filename}` );
184
250
  }
185
251
  }
186
252
  }