@output.ai/core 0.1.9-dev.pr156.0 → 0.1.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/README.md +79 -228
- package/package.json +5 -2
- package/src/tracing/processors/local/index.js +52 -14
- package/src/tracing/processors/local/index.spec.js +73 -5
- package/src/tracing/trace_engine.js +9 -1
- package/src/worker/webpack_loaders/tools.js +117 -1
- package/src/worker/webpack_loaders/workflow_rewriter/collect_target_imports.spec.js +20 -26
- package/src/worker/webpack_loaders/workflow_rewriter/index.spec.js +73 -88
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.js +157 -33
- package/src/worker/webpack_loaders/workflow_rewriter/rewrite_fn_bodies.spec.js +91 -15
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import generatorModule from '@babel/generator';
|
|
2
3
|
import { parse } from '../tools.js';
|
|
3
4
|
import rewriteFnBodies from './rewrite_fn_bodies.js';
|
|
4
5
|
|
|
6
|
+
const generate = generatorModule.default ?? generatorModule;
|
|
7
|
+
|
|
5
8
|
describe( 'rewrite_fn_bodies', () => {
|
|
6
9
|
it( 'converts arrow to function and rewrites step/workflow calls', () => {
|
|
7
|
-
const src =
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
].join( '\n' );
|
|
10
|
+
const src = `
|
|
11
|
+
const obj = {
|
|
12
|
+
fn: async x => {
|
|
13
|
+
StepA( 1 );
|
|
14
|
+
FlowB( 2 );
|
|
15
|
+
}
|
|
16
|
+
}`;
|
|
15
17
|
const ast = parse( src, 'file.js' );
|
|
16
18
|
const stepImports = [ { localName: 'StepA', stepName: 'step.a' } ];
|
|
17
19
|
const flowImports = [ { localName: 'FlowB', workflowName: 'flow.b' } ];
|
|
@@ -24,13 +26,12 @@ describe( 'rewrite_fn_bodies', () => {
|
|
|
24
26
|
} );
|
|
25
27
|
|
|
26
28
|
it( 'rewrites evaluator calls to this.invokeEvaluator', () => {
|
|
27
|
-
const src =
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
].join( '\n' );
|
|
29
|
+
const src = `
|
|
30
|
+
const obj = {
|
|
31
|
+
fn: async x => {
|
|
32
|
+
EvalA(3);
|
|
33
|
+
}
|
|
34
|
+
};`;
|
|
34
35
|
const ast = parse( src, 'file.js' );
|
|
35
36
|
const evaluatorImports = [ { localName: 'EvalA', evaluatorName: 'eval.a' } ];
|
|
36
37
|
const rewrote = rewriteFnBodies( { ast, stepImports: [], evaluatorImports, flowImports: [] } );
|
|
@@ -43,5 +44,80 @@ describe( 'rewrite_fn_bodies', () => {
|
|
|
43
44
|
const rewrote = rewriteFnBodies( { ast, stepImports: [], evaluatorImports: [], flowImports: [] } );
|
|
44
45
|
expect( rewrote ).toBe( false );
|
|
45
46
|
} );
|
|
47
|
+
|
|
48
|
+
it( 'rewrites helper calls and helper bodies (steps and evaluators)', () => {
|
|
49
|
+
const src = `
|
|
50
|
+
const foo = async () => {
|
|
51
|
+
StepA( 1 );
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function bar( x ) {
|
|
55
|
+
EvalA( x );
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const obj = {
|
|
59
|
+
fn: async x => {
|
|
60
|
+
foo();
|
|
61
|
+
bar( 2 );
|
|
62
|
+
}
|
|
63
|
+
}`;
|
|
64
|
+
|
|
65
|
+
const ast = parse( src, 'file.js' );
|
|
66
|
+
const stepImports = [ { localName: 'StepA', stepName: 'step.a' } ];
|
|
67
|
+
const evaluatorImports = [ { localName: 'EvalA', evaluatorName: 'eval.a' } ];
|
|
68
|
+
|
|
69
|
+
const rewrote = rewriteFnBodies( { ast, stepImports, sharedStepImports: [], evaluatorImports, flowImports: [] } );
|
|
70
|
+
expect( rewrote ).toBe( true );
|
|
71
|
+
|
|
72
|
+
const { code } = generate( ast, { quotes: 'single' } );
|
|
73
|
+
|
|
74
|
+
// Helper calls in fn are rewritten to call(this, ...)
|
|
75
|
+
expect( code ).toMatch( /foo\.call\(this\)/ );
|
|
76
|
+
expect( code ).toMatch( /bar\.call\(this,\s*2\)/ );
|
|
77
|
+
|
|
78
|
+
// Inside helpers, calls are rewritten
|
|
79
|
+
expect( code ).toMatch( /this\.invokeStep\(([\"'])step\.a\1,\s*1\)/ );
|
|
80
|
+
expect( code ).toMatch( /this\.invokeEvaluator\(([\"'])eval\.a\1,\s*x\)/ );
|
|
81
|
+
|
|
82
|
+
// Arrow helper converted to function expression to allow dynamic this
|
|
83
|
+
expect( code ).toMatch( /const foo = async function/ );
|
|
84
|
+
} );
|
|
85
|
+
|
|
86
|
+
it( 'rewrites nested helper chains until the step invocation', () => {
|
|
87
|
+
const src = `
|
|
88
|
+
const foo = () => {
|
|
89
|
+
bar();
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
function bar() {
|
|
93
|
+
baz( 42 );
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
const baz = n => {
|
|
97
|
+
StepA( n );
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
const obj = {
|
|
101
|
+
fn: async () => {
|
|
102
|
+
foo();
|
|
103
|
+
}
|
|
104
|
+
}`;
|
|
105
|
+
|
|
106
|
+
const ast = parse( src, 'file.js' );
|
|
107
|
+
const stepImports = [ { localName: 'StepA', stepName: 'step.a' } ];
|
|
108
|
+
const rewrote = rewriteFnBodies( { ast, stepImports, evaluatorImports: [], flowImports: [] } );
|
|
109
|
+
expect( rewrote ).toBe( true );
|
|
110
|
+
|
|
111
|
+
const { code } = generate( ast, { quotes: 'single' } );
|
|
112
|
+
// Calls along the chain are bound with this
|
|
113
|
+
expect( code ).toMatch( /foo\.call\(this\)/ );
|
|
114
|
+
expect( code ).toMatch( /bar\.call\(this\)/ );
|
|
115
|
+
expect( code ).toMatch( /baz\.call\(this,\s*42\)/ );
|
|
116
|
+
// Deep step rewrite in the last helper
|
|
117
|
+
expect( code ).toMatch( /this\.invokeStep\(([\"'])step\.a\1,\s*n\)/ );
|
|
118
|
+
// Arrow helpers converted to functions
|
|
119
|
+
expect( code ).toMatch( /const foo = function/ );
|
|
120
|
+
expect( code ).toMatch( /const baz = function/ );
|
|
121
|
+
} );
|
|
46
122
|
} );
|
|
47
123
|
|