@valerypopoff/rivet2-core 2.0.6 → 2.0.8
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/cjs/bundle.cjs +1440 -769
- package/dist/cjs/bundle.cjs.map +4 -4
- package/dist/esm/api/createProcessor.js +8 -2
- package/dist/esm/model/GraphPreprocessor.js +129 -7
- package/dist/esm/model/GraphProcessor.js +204 -82
- package/dist/esm/model/NodeExclusionPolicy.js +67 -0
- package/dist/esm/model/NodeExecutionPlanner.js +66 -22
- package/dist/esm/model/Nodes.js +3 -0
- package/dist/esm/model/chat-v2/chatV2Outputs.js +183 -0
- package/dist/esm/model/chat-v2/chatV2Pipeline.js +30 -212
- package/dist/esm/model/nodes/CodeNewNode.js +207 -0
- package/dist/esm/model/nodes/CodeNode.js +11 -10
- package/dist/esm/model/nodes/CommentNode.js +1 -1
- package/dist/esm/model/nodes/ExpressionNode.js +23 -39
- package/dist/esm/model/nodes/HttpCallNode.js +20 -18
- package/dist/esm/model/nodes/ObjectNode.js +1 -0
- package/dist/esm/model/nodes/ToolNode.js +1 -0
- package/dist/esm/model/nodes/codeNodeErrorDiagnostics.js +16 -11
- package/dist/esm/model/nodes/jsListCallbackHelpers.js +26 -30
- package/dist/esm/model/nodes/jsValueInterpolation.js +56 -2
- package/dist/esm/utils/serialization/serialization.js +22 -1
- package/dist/types/api/createProcessor.d.ts +8 -2
- package/dist/types/model/EditorDefinition.d.ts +2 -0
- package/dist/types/model/GraphPreprocessor.d.ts +36 -2
- package/dist/types/model/GraphProcessor.d.ts +12 -1
- package/dist/types/model/NodeExclusionPolicy.d.ts +21 -0
- package/dist/types/model/NodeExecutionPlanner.d.ts +3 -8
- package/dist/types/model/Nodes.d.ts +3 -2
- package/dist/types/model/ProcessContext.d.ts +2 -2
- package/dist/types/model/chat-v2/chatV2Outputs.d.ts +29 -0
- package/dist/types/model/nodes/CodeNewNode.d.ts +27 -0
- package/dist/types/model/nodes/codeNodeErrorDiagnostics.d.ts +4 -2
- package/dist/types/model/nodes/jsValueInterpolation.d.ts +20 -0
- package/package.json +1 -1
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid/non-secure';
|
|
2
|
+
import { dedent } from 'ts-dedent';
|
|
3
|
+
import {} from '../EditorDefinition.js';
|
|
4
|
+
import { nodeDefinition } from '../NodeDefinition.js';
|
|
5
|
+
import { NodeImpl } from '../NodeImpl.js';
|
|
6
|
+
import {} from '../NodeBodySpec.js';
|
|
7
|
+
import { appendCodeNodeSourceUrl, buildCodeNodeSourceUrl, enrichCodeNodeErrorWithLocation, } from './codeNodeErrorDiagnostics.js';
|
|
8
|
+
import { buildJsValueInterpolatedSource, buildJsValueInputsInitializer, buildJsValuePreview, getJsValueInterpolationInputDefinitions, getJsValueInterpolationRuntimeContext, interpolateJsValuePreviewSource, sanitizeGeneratedJsValueError, } from './jsValueInterpolation.js';
|
|
9
|
+
const DEFAULT_CODE_NEW = dedent `
|
|
10
|
+
// This is a Code node. Write JavaScript here and return one value.
|
|
11
|
+
// Interpolation tokens create input ports and evaluate as connected values.
|
|
12
|
+
// The returned value becomes the node's single output.
|
|
13
|
+
const value = {{input}};
|
|
14
|
+
return value;
|
|
15
|
+
`;
|
|
16
|
+
const MAX_BODY_PREVIEW_LINES = 15;
|
|
17
|
+
const CODE_NEW_INPUTS_IDENTIFIER = '__codeNewInputs';
|
|
18
|
+
const CODE_NEW_INPUT_CLONE_CACHE_IDENTIFIER = 'codeNewInputCloneCache';
|
|
19
|
+
export const CODE_NEW_OUTPUT_PORT_ID = 'output';
|
|
20
|
+
function buildCodeNewPreview(code) {
|
|
21
|
+
return buildJsValuePreview(code, MAX_BODY_PREVIEW_LINES);
|
|
22
|
+
}
|
|
23
|
+
function buildCodeNewRuntimeSource(code, inputsIdentifier) {
|
|
24
|
+
return buildJsValueInterpolatedSource(code, inputsIdentifier, { trim: false });
|
|
25
|
+
}
|
|
26
|
+
function buildCodeNewInputsInitializer(inputNames, inputsIdentifier) {
|
|
27
|
+
return buildJsValueInputsInitializer({
|
|
28
|
+
cacheIdentifier: CODE_NEW_INPUT_CLONE_CACHE_IDENTIFIER,
|
|
29
|
+
inputNames,
|
|
30
|
+
inputsIdentifier,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
export function interpolateCodeNewSource(code, inputs) {
|
|
34
|
+
return interpolateJsValuePreviewSource(code, inputs, { trim: false });
|
|
35
|
+
}
|
|
36
|
+
function sanitizeCodeNewError(error, inputNames, inputsIdentifier) {
|
|
37
|
+
return sanitizeGeneratedJsValueError(error, inputNames, inputsIdentifier, 'code input');
|
|
38
|
+
}
|
|
39
|
+
function buildCodeNewWrapper(code, interpolationContext) {
|
|
40
|
+
const { inputNames, inputsIdentifier } = interpolationContext;
|
|
41
|
+
const beforeUserCodeLines = [
|
|
42
|
+
...buildCodeNewInputsInitializer(inputNames, inputsIdentifier).split(/\r?\n/),
|
|
43
|
+
'',
|
|
44
|
+
'const __codeNewResult = await (async () => {',
|
|
45
|
+
];
|
|
46
|
+
const afterUserCodeLines = [
|
|
47
|
+
'})();',
|
|
48
|
+
'',
|
|
49
|
+
'return {',
|
|
50
|
+
' output: {',
|
|
51
|
+
" type: 'any',",
|
|
52
|
+
' value: __codeNewResult,',
|
|
53
|
+
' },',
|
|
54
|
+
'};',
|
|
55
|
+
];
|
|
56
|
+
return {
|
|
57
|
+
source: [...beforeUserCodeLines, buildCodeNewRuntimeSource(code, inputsIdentifier), ...afterUserCodeLines].join('\n'),
|
|
58
|
+
userCodeLineOffset: beforeUserCodeLines.length,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function isDataValueLike(value) {
|
|
62
|
+
return (typeof value === 'object' &&
|
|
63
|
+
value !== null &&
|
|
64
|
+
typeof value.type === 'string' &&
|
|
65
|
+
Object.prototype.hasOwnProperty.call(value, 'value'));
|
|
66
|
+
}
|
|
67
|
+
function validateCodeNewRunnerOutputs(outputs) {
|
|
68
|
+
if (outputs == null || typeof outputs !== 'object' || ('then' in outputs && typeof outputs.then === 'function')) {
|
|
69
|
+
throw new Error('Code node runner must return an object containing the Output value.');
|
|
70
|
+
}
|
|
71
|
+
const output = outputs[CODE_NEW_OUTPUT_PORT_ID];
|
|
72
|
+
if (!isDataValueLike(output)) {
|
|
73
|
+
throw new Error('Code node runner must return a DataValue for the Output port.');
|
|
74
|
+
}
|
|
75
|
+
if (output.type !== 'any') {
|
|
76
|
+
throw new Error('Code node runner must return an any DataValue for the Output port.');
|
|
77
|
+
}
|
|
78
|
+
return outputs;
|
|
79
|
+
}
|
|
80
|
+
export class CodeNewNodeImpl extends NodeImpl {
|
|
81
|
+
static create() {
|
|
82
|
+
return {
|
|
83
|
+
type: 'codeNew',
|
|
84
|
+
title: 'Code',
|
|
85
|
+
id: nanoid(),
|
|
86
|
+
visualData: {
|
|
87
|
+
x: 0,
|
|
88
|
+
y: 0,
|
|
89
|
+
width: 260,
|
|
90
|
+
},
|
|
91
|
+
data: {
|
|
92
|
+
code: DEFAULT_CODE_NEW,
|
|
93
|
+
allowFetch: false,
|
|
94
|
+
allowRequire: false,
|
|
95
|
+
allowRivet: false,
|
|
96
|
+
allowProcess: false,
|
|
97
|
+
allowConsole: false,
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
getInputDefinitions() {
|
|
102
|
+
return getJsValueInterpolationInputDefinitions(this.data.code);
|
|
103
|
+
}
|
|
104
|
+
getOutputDefinitions() {
|
|
105
|
+
return [
|
|
106
|
+
{
|
|
107
|
+
id: CODE_NEW_OUTPUT_PORT_ID,
|
|
108
|
+
title: 'Output',
|
|
109
|
+
dataType: 'any',
|
|
110
|
+
},
|
|
111
|
+
];
|
|
112
|
+
}
|
|
113
|
+
getEditors() {
|
|
114
|
+
return [
|
|
115
|
+
{
|
|
116
|
+
type: 'code',
|
|
117
|
+
label: 'Code',
|
|
118
|
+
helperMessage: 'Use {{var}} to create input ports. Interpolated variables evaluate as the connected values.',
|
|
119
|
+
dataKey: 'code',
|
|
120
|
+
language: 'javascript',
|
|
121
|
+
interpolationSyntax: 'js-value',
|
|
122
|
+
enableFolding: true,
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
type: 'group',
|
|
126
|
+
label: 'Runtime permissions',
|
|
127
|
+
defaultOpen: true,
|
|
128
|
+
editors: [
|
|
129
|
+
{
|
|
130
|
+
type: 'toggle',
|
|
131
|
+
label: 'Allow "fetch"',
|
|
132
|
+
dataKey: 'allowFetch',
|
|
133
|
+
},
|
|
134
|
+
{
|
|
135
|
+
type: 'toggle',
|
|
136
|
+
label: 'Allow "Rivet"',
|
|
137
|
+
dataKey: 'allowRivet',
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
type: 'toggle',
|
|
141
|
+
label: 'Allow "console"',
|
|
142
|
+
dataKey: 'allowConsole',
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
type: 'toggle',
|
|
146
|
+
label: 'Allow "require"',
|
|
147
|
+
dataKey: 'allowRequire',
|
|
148
|
+
helperMessage: 'Only available with the Node executor',
|
|
149
|
+
},
|
|
150
|
+
{
|
|
151
|
+
type: 'toggle',
|
|
152
|
+
label: 'Allow "process"',
|
|
153
|
+
dataKey: 'allowProcess',
|
|
154
|
+
helperMessage: 'Only available with the Node executor',
|
|
155
|
+
},
|
|
156
|
+
],
|
|
157
|
+
},
|
|
158
|
+
];
|
|
159
|
+
}
|
|
160
|
+
getBody() {
|
|
161
|
+
return {
|
|
162
|
+
type: 'colorized',
|
|
163
|
+
text: buildCodeNewPreview(this.data.code),
|
|
164
|
+
language: 'javascript',
|
|
165
|
+
fontSize: 12,
|
|
166
|
+
fontFamily: 'monospace',
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
static getUIData() {
|
|
170
|
+
return {
|
|
171
|
+
infoBoxBody: dedent `
|
|
172
|
+
Runs JavaScript code that can use interpolation-created input ports and emits the returned value as the node output.
|
|
173
|
+
`,
|
|
174
|
+
infoBoxTitle: 'Code Node',
|
|
175
|
+
contextMenuTitle: 'Code',
|
|
176
|
+
group: ['Advanced'],
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
async process(inputs, context) {
|
|
180
|
+
const sourceUrl = buildCodeNodeSourceUrl(this.chartNode.id);
|
|
181
|
+
const interpolationContext = getJsValueInterpolationRuntimeContext(this.data.code, CODE_NEW_INPUTS_IDENTIFIER);
|
|
182
|
+
const { inputNames, inputsIdentifier } = interpolationContext;
|
|
183
|
+
const { source, userCodeLineOffset } = buildCodeNewWrapper(this.data.code, interpolationContext);
|
|
184
|
+
try {
|
|
185
|
+
const outputs = await context.codeRunner.runCode(appendCodeNodeSourceUrl(source, sourceUrl), inputs, {
|
|
186
|
+
includeFetch: this.data.allowFetch ?? false,
|
|
187
|
+
includeRequire: this.data.allowRequire ?? false,
|
|
188
|
+
includeRivet: this.data.allowRivet ?? false,
|
|
189
|
+
includeProcess: this.data.allowProcess ?? false,
|
|
190
|
+
includeConsole: this.data.allowConsole ?? false,
|
|
191
|
+
}, context.graphInputNodeValues, context.contextValues);
|
|
192
|
+
return validateCodeNewRunnerOutputs(outputs);
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
const enrichedError = await enrichCodeNodeErrorWithLocation({
|
|
196
|
+
code: this.data.code,
|
|
197
|
+
diagnosticCode: source,
|
|
198
|
+
error,
|
|
199
|
+
locationLabel: 'Code node',
|
|
200
|
+
sourceUrl,
|
|
201
|
+
userCodeLineOffset,
|
|
202
|
+
});
|
|
203
|
+
throw sanitizeCodeNewError(enrichedError, inputNames, inputsIdentifier);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
export const codeNewNode = nodeDefinition(CodeNewNodeImpl, 'Code');
|
|
@@ -11,7 +11,7 @@ export class CodeNodeImpl extends NodeImpl {
|
|
|
11
11
|
static create() {
|
|
12
12
|
const chartNode = {
|
|
13
13
|
type: 'code',
|
|
14
|
-
title: 'Code',
|
|
14
|
+
title: 'Code (legacy)',
|
|
15
15
|
id: nanoid(),
|
|
16
16
|
visualData: {
|
|
17
17
|
x: 0,
|
|
@@ -19,10 +19,10 @@ export class CodeNodeImpl extends NodeImpl {
|
|
|
19
19
|
},
|
|
20
20
|
data: {
|
|
21
21
|
code: dedent `
|
|
22
|
-
// This is a
|
|
22
|
+
// This is a Code (legacy) node. Write JavaScript here and it will be executed.
|
|
23
23
|
// Inputs are accessible via an object \`inputs\` and data is typed (i.e. inputs.foo.type, inputs.foo.value)
|
|
24
24
|
// Return an object with named outputs that match the output names specified in the node's config.
|
|
25
|
-
// Output values must
|
|
25
|
+
// Output values must be typed as well (e.g. { bar: { type: 'string', value: 'bar' } }
|
|
26
26
|
return {
|
|
27
27
|
output1: {
|
|
28
28
|
type: inputs.input1.type,
|
|
@@ -156,15 +156,15 @@ export class CodeNodeImpl extends NodeImpl {
|
|
|
156
156
|
static getUIData() {
|
|
157
157
|
return {
|
|
158
158
|
infoBoxBody: dedent `
|
|
159
|
-
Executes
|
|
159
|
+
Executes legacy JavaScript code with manually configured inputs and outputs.
|
|
160
160
|
`,
|
|
161
|
-
infoBoxTitle: 'Code Node',
|
|
162
|
-
contextMenuTitle: 'Code',
|
|
161
|
+
infoBoxTitle: 'Code (legacy) Node',
|
|
162
|
+
contextMenuTitle: 'Code (legacy)',
|
|
163
163
|
group: ['Advanced'],
|
|
164
164
|
};
|
|
165
165
|
}
|
|
166
166
|
async process(inputs, context) {
|
|
167
|
-
const sourceUrl = buildCodeNodeSourceUrl(this.chartNode.id
|
|
167
|
+
const sourceUrl = buildCodeNodeSourceUrl(this.chartNode.id);
|
|
168
168
|
let outputs;
|
|
169
169
|
try {
|
|
170
170
|
outputs = await context.codeRunner.runCode(appendCodeNodeSourceUrl(this.data.code, sourceUrl), inputs, {
|
|
@@ -179,19 +179,20 @@ export class CodeNodeImpl extends NodeImpl {
|
|
|
179
179
|
throw await enrichCodeNodeErrorWithLocation({
|
|
180
180
|
code: this.data.code,
|
|
181
181
|
error,
|
|
182
|
+
locationLabel: 'Code (legacy) node',
|
|
182
183
|
sourceUrl,
|
|
183
184
|
});
|
|
184
185
|
}
|
|
185
186
|
if (outputs == null || typeof outputs !== 'object' || ('then' in outputs && typeof outputs.then === 'function')) {
|
|
186
|
-
throw new Error('Code node must return an object with output values.');
|
|
187
|
+
throw new Error('Code (legacy) node must return an object with output values.');
|
|
187
188
|
}
|
|
188
189
|
const missingOutputs = this.getOutputDefinitions().filter((output) => !(output.id in outputs));
|
|
189
190
|
if (missingOutputs.length > 0) {
|
|
190
|
-
throw new Error(`Code node must return an object with output values for all outputs. To not run an output, return { "type": "control-flow-excluded", "value": undefined }. To return undefined, return { "type": "any", "value": undefined }. Missing: ${missingOutputs
|
|
191
|
+
throw new Error(`Code (legacy) node must return an object with output values for all outputs. To not run an output, return { "type": "control-flow-excluded", "value": undefined }. To return undefined, return { "type": "any", "value": undefined }. Missing: ${missingOutputs
|
|
191
192
|
.map((output) => output.id)
|
|
192
193
|
.join(', ')}`);
|
|
193
194
|
}
|
|
194
195
|
return outputs;
|
|
195
196
|
}
|
|
196
197
|
}
|
|
197
|
-
export const codeNode = nodeDefinition(CodeNodeImpl, 'Code');
|
|
198
|
+
export const codeNode = nodeDefinition(CodeNodeImpl, 'Code (legacy)');
|
|
@@ -4,49 +4,36 @@ import { NodeImpl } from '../NodeImpl.js';
|
|
|
4
4
|
import {} from '../../index.js';
|
|
5
5
|
import { dedent } from 'ts-dedent';
|
|
6
6
|
import { nodeDefinition } from '../NodeDefinition.js';
|
|
7
|
-
import {
|
|
8
|
-
import { buildClonedInputValueAssignments, buildCloneJsInputValueFunction, buildJsValueInterpolatedSource, getJsValueInterpolationInputNames, interpolateJsValuePreviewSource, sanitizeGeneratedJsValueText, } from './jsValueInterpolation.js';
|
|
9
|
-
import { createInterpolationInputDefinition } from '../interpolationInputDefinition.js';
|
|
7
|
+
import { buildJsValueInterpolatedSource, buildJsValueInputsInitializer, buildJsValuePreview, getJsValueInterpolationInputDefinitions, getJsValueInterpolationRuntimeContext, interpolateJsValuePreviewSource, sanitizeGeneratedJsValueError, } from './jsValueInterpolation.js';
|
|
10
8
|
const DEFAULT_EXPRESSION = '{{a}} == "123" ? {{b}} : {{c}}';
|
|
11
9
|
const MAX_BODY_PREVIEW_LINES = 15;
|
|
12
10
|
const EXPRESSION_INPUTS_IDENTIFIER = '__expressionInputs';
|
|
13
11
|
const EXPRESSION_INPUT_CLONE_CACHE_IDENTIFIER = 'expressionInputCloneCache';
|
|
14
12
|
export const EXPRESSION_OUTPUT_PORT_ID = 'output';
|
|
15
13
|
function buildExpressionPreview(expression) {
|
|
16
|
-
return expression
|
|
14
|
+
return buildJsValuePreview(expression, MAX_BODY_PREVIEW_LINES);
|
|
17
15
|
}
|
|
18
|
-
function
|
|
19
|
-
return
|
|
16
|
+
function buildExpressionRuntimeSource(expression, inputsIdentifier) {
|
|
17
|
+
return buildJsValueInterpolatedSource(expression, inputsIdentifier);
|
|
20
18
|
}
|
|
21
|
-
function
|
|
22
|
-
return
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const ${EXPRESSION_INPUTS_IDENTIFIER} = Object.create(null);
|
|
28
|
-
const ${EXPRESSION_INPUT_CLONE_CACHE_IDENTIFIER} = new WeakMap();
|
|
29
|
-
${buildClonedInputValueAssignments(inputNames, EXPRESSION_INPUTS_IDENTIFIER, EXPRESSION_INPUT_CLONE_CACHE_IDENTIFIER)}
|
|
30
|
-
`;
|
|
19
|
+
function buildExpressionInputsInitializer(inputNames, inputsIdentifier) {
|
|
20
|
+
return buildJsValueInputsInitializer({
|
|
21
|
+
cacheIdentifier: EXPRESSION_INPUT_CLONE_CACHE_IDENTIFIER,
|
|
22
|
+
inputNames,
|
|
23
|
+
inputsIdentifier,
|
|
24
|
+
});
|
|
31
25
|
}
|
|
32
26
|
export function interpolateExpressionSource(expression, inputs) {
|
|
33
27
|
return interpolateJsValuePreviewSource(expression, inputs);
|
|
34
28
|
}
|
|
35
|
-
function
|
|
36
|
-
return
|
|
29
|
+
function sanitizeExpressionError(error, inputNames, inputsIdentifier) {
|
|
30
|
+
return sanitizeGeneratedJsValueError(error, inputNames, inputsIdentifier, 'expression input');
|
|
37
31
|
}
|
|
38
|
-
function
|
|
39
|
-
const
|
|
40
|
-
|
|
41
|
-
sanitizeGeneratedExpressionText(expressionError.message, inputNames) ?? expressionError.message;
|
|
42
|
-
expressionError.stack = sanitizeGeneratedExpressionText(expressionError.stack, inputNames);
|
|
43
|
-
return expressionError;
|
|
44
|
-
}
|
|
45
|
-
function buildExpressionWrapper(expression) {
|
|
46
|
-
const inputNames = getExpressionInputNames(expression);
|
|
47
|
-
const expressionSource = buildExpressionRuntimeSource(expression);
|
|
32
|
+
function buildExpressionWrapper(expression, interpolationContext) {
|
|
33
|
+
const { inputNames, inputsIdentifier } = interpolationContext;
|
|
34
|
+
const expressionSource = buildExpressionRuntimeSource(expression, inputsIdentifier);
|
|
48
35
|
return dedent `
|
|
49
|
-
${buildExpressionInputsInitializer(inputNames)}
|
|
36
|
+
${buildExpressionInputsInitializer(inputNames, inputsIdentifier)}
|
|
50
37
|
|
|
51
38
|
return {
|
|
52
39
|
output: {
|
|
@@ -74,13 +61,7 @@ export class ExpressionNodeImpl extends NodeImpl {
|
|
|
74
61
|
return chartNode;
|
|
75
62
|
}
|
|
76
63
|
getInputDefinitions() {
|
|
77
|
-
return
|
|
78
|
-
return createInterpolationInputDefinition({
|
|
79
|
-
interpolationName: inputName,
|
|
80
|
-
dataType: 'any',
|
|
81
|
-
required: false,
|
|
82
|
-
});
|
|
83
|
-
});
|
|
64
|
+
return getJsValueInterpolationInputDefinitions(this.data.expression);
|
|
84
65
|
}
|
|
85
66
|
getOutputDefinitions() {
|
|
86
67
|
return [
|
|
@@ -99,6 +80,7 @@ export class ExpressionNodeImpl extends NodeImpl {
|
|
|
99
80
|
helperMessage: 'Use {{var}} to create input ports. Interpolated variables evaluate as the connected values.',
|
|
100
81
|
dataKey: 'expression',
|
|
101
82
|
language: 'javascript',
|
|
83
|
+
interpolationSyntax: 'js-value',
|
|
102
84
|
enableFolding: true,
|
|
103
85
|
},
|
|
104
86
|
];
|
|
@@ -125,9 +107,11 @@ export class ExpressionNodeImpl extends NodeImpl {
|
|
|
125
107
|
};
|
|
126
108
|
}
|
|
127
109
|
async process(inputs, context) {
|
|
128
|
-
const
|
|
110
|
+
const interpolationContext = getJsValueInterpolationRuntimeContext(this.data.expression, EXPRESSION_INPUTS_IDENTIFIER);
|
|
111
|
+
const { inputNames, inputsIdentifier } = interpolationContext;
|
|
112
|
+
const source = buildExpressionWrapper(this.data.expression, interpolationContext);
|
|
129
113
|
try {
|
|
130
|
-
return await context.codeRunner.runCode(
|
|
114
|
+
return await context.codeRunner.runCode(source, inputs, {
|
|
131
115
|
includeFetch: false,
|
|
132
116
|
includeRequire: false,
|
|
133
117
|
includeRivet: false,
|
|
@@ -136,7 +120,7 @@ export class ExpressionNodeImpl extends NodeImpl {
|
|
|
136
120
|
}, context.graphInputNodeValues, context.contextValues);
|
|
137
121
|
}
|
|
138
122
|
catch (error) {
|
|
139
|
-
throw sanitizeExpressionError(error, inputNames);
|
|
123
|
+
throw sanitizeExpressionError(error, inputNames, inputsIdentifier);
|
|
140
124
|
}
|
|
141
125
|
}
|
|
142
126
|
}
|
|
@@ -433,7 +433,7 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
433
433
|
};
|
|
434
434
|
}
|
|
435
435
|
async process(inputs, context) {
|
|
436
|
-
const requestAttempts = createHttpCallRequestAttempts();
|
|
436
|
+
const requestAttempts = this.data.retryOnNon200 ? createHttpCallRequestAttempts() : undefined;
|
|
437
437
|
try {
|
|
438
438
|
const method = getInputOrData(this.data, inputs, 'method', 'string');
|
|
439
439
|
const url = getInputOrData(this.data, inputs, 'url', 'string');
|
|
@@ -498,19 +498,21 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
498
498
|
throw err;
|
|
499
499
|
}
|
|
500
500
|
};
|
|
501
|
-
const performTrackedRequest =
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
501
|
+
const performTrackedRequest = requestAttempts
|
|
502
|
+
? async () => {
|
|
503
|
+
try {
|
|
504
|
+
const response = await performRequest();
|
|
505
|
+
recordHttpCallResponseAttempt(requestAttempts, response);
|
|
506
|
+
return response;
|
|
507
|
+
}
|
|
508
|
+
catch (error) {
|
|
509
|
+
recordHttpCallThrownAttempt(requestAttempts, error, context.signal);
|
|
510
|
+
throw error;
|
|
511
|
+
}
|
|
510
512
|
}
|
|
511
|
-
|
|
513
|
+
: performRequest;
|
|
512
514
|
let response = await performTrackedRequest();
|
|
513
|
-
if (
|
|
515
|
+
if (requestAttempts) {
|
|
514
516
|
const repeatTimes = normalizeHttpRetryCount(this.data.retryOnNon200RepeatTimes);
|
|
515
517
|
const cooldownMs = normalizeHttpRetryCooldownMs(this.data.retryOnNon200CooldownMs);
|
|
516
518
|
for (let attempt = 0; response.status !== 200 && attempt < repeatTimes; attempt++) {
|
|
@@ -522,7 +524,7 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
522
524
|
throw buildNon2xxStatusCodeError(response.status);
|
|
523
525
|
}
|
|
524
526
|
const output = {
|
|
525
|
-
[STATUS_CODE_OUTPUT_ID]:
|
|
527
|
+
[STATUS_CODE_OUTPUT_ID]: requestAttempts
|
|
526
528
|
? buildRetryAttemptOutput('number[]', requestAttempts.statusCodeValues)
|
|
527
529
|
: {
|
|
528
530
|
type: 'number',
|
|
@@ -560,14 +562,14 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
560
562
|
};
|
|
561
563
|
}
|
|
562
564
|
}
|
|
563
|
-
if (this.data.catchRequestFailed ||
|
|
564
|
-
output[REQUEST_FAILED_OUTPUT_ID] =
|
|
565
|
+
if (this.data.catchRequestFailed || requestAttempts) {
|
|
566
|
+
output[REQUEST_FAILED_OUTPUT_ID] = requestAttempts
|
|
565
567
|
? buildRetryAttemptOutput('boolean[]', requestAttempts.requestFailedValues)
|
|
566
568
|
: {
|
|
567
569
|
type: 'boolean',
|
|
568
570
|
value: false,
|
|
569
571
|
};
|
|
570
|
-
output[REQUEST_ERROR_OUTPUT_ID] =
|
|
572
|
+
output[REQUEST_ERROR_OUTPUT_ID] = requestAttempts
|
|
571
573
|
? buildRetryAttemptOutput('string[]', requestAttempts.requestErrorMessages)
|
|
572
574
|
: {
|
|
573
575
|
type: 'control-flow-excluded',
|
|
@@ -581,8 +583,8 @@ export class HttpCallNodeImpl extends NodeImpl {
|
|
|
581
583
|
return buildCaughtRequestFailedResult({
|
|
582
584
|
isBinaryOutput: Boolean(this.data.isBinaryOutput),
|
|
583
585
|
error,
|
|
584
|
-
retryOnNon200:
|
|
585
|
-
attempts: requestAttempts,
|
|
586
|
+
retryOnNon200: requestAttempts != null,
|
|
587
|
+
attempts: requestAttempts ?? createHttpCallRequestAttempts(),
|
|
586
588
|
});
|
|
587
589
|
}
|
|
588
590
|
throw error;
|
|
@@ -7,9 +7,8 @@ function escapeRegExp(value) {
|
|
|
7
7
|
function sanitizeSourceUrlPart(value) {
|
|
8
8
|
return String(value).replace(/[^a-zA-Z0-9_-]/g, '_');
|
|
9
9
|
}
|
|
10
|
-
export function buildCodeNodeSourceUrl(nodeId
|
|
11
|
-
|
|
12
|
-
return `rivet-code-node-${sanitizeSourceUrlPart(nodeId)}${processPart}.js`;
|
|
10
|
+
export function buildCodeNodeSourceUrl(nodeId) {
|
|
11
|
+
return `rivet-code-node-${sanitizeSourceUrlPart(nodeId)}.js`;
|
|
13
12
|
}
|
|
14
13
|
export function appendCodeNodeSourceUrl(code, sourceUrl) {
|
|
15
14
|
return `${code}\n//# sourceURL=${sourceUrl}`;
|
|
@@ -26,7 +25,7 @@ function getRuntimeErrorLocationFromStack(params) {
|
|
|
26
25
|
}
|
|
27
26
|
const generatedLine = Number(match[1]);
|
|
28
27
|
const column = Number(match[2]);
|
|
29
|
-
const line = generatedLine - ASYNC_FUNCTION_LINE_OFFSET;
|
|
28
|
+
const line = generatedLine - ASYNC_FUNCTION_LINE_OFFSET - (params.userCodeLineOffset ?? 0);
|
|
30
29
|
const lineCount = Math.max(params.code.split(/\r?\n/).length, 1);
|
|
31
30
|
if (!Number.isFinite(line) || line < 1 || line > lineCount) {
|
|
32
31
|
return undefined;
|
|
@@ -49,11 +48,11 @@ function getParserErrorLocation(error) {
|
|
|
49
48
|
line: loc.line,
|
|
50
49
|
};
|
|
51
50
|
}
|
|
52
|
-
async function getSyntaxErrorLocation(code, error) {
|
|
51
|
+
async function getSyntaxErrorLocation(code, error, options = {}) {
|
|
53
52
|
if (error.name !== 'SyntaxError') {
|
|
54
53
|
return undefined;
|
|
55
54
|
}
|
|
56
|
-
const wrappedCode = `async function __rivetCodeNode__() {\n${code}\n}`;
|
|
55
|
+
const wrappedCode = `async function __rivetCodeNode__() {\n${options.diagnosticCode ?? code}\n}`;
|
|
57
56
|
try {
|
|
58
57
|
const { parse } = await import('acorn');
|
|
59
58
|
parse(wrappedCode, {
|
|
@@ -68,7 +67,7 @@ async function getSyntaxErrorLocation(code, error) {
|
|
|
68
67
|
return undefined;
|
|
69
68
|
}
|
|
70
69
|
const lineCount = Math.max(code.split(/\r?\n/).length, 1);
|
|
71
|
-
const line = Math.min(Math.max(parserLocation.line - SYNTAX_PARSE_WRAPPER_LINE_OFFSET, 1), lineCount);
|
|
70
|
+
const line = Math.min(Math.max(parserLocation.line - SYNTAX_PARSE_WRAPPER_LINE_OFFSET - (options.userCodeLineOffset ?? 0), 1), lineCount);
|
|
72
71
|
return {
|
|
73
72
|
column: parserLocation.column,
|
|
74
73
|
line,
|
|
@@ -76,9 +75,9 @@ async function getSyntaxErrorLocation(code, error) {
|
|
|
76
75
|
}
|
|
77
76
|
return undefined;
|
|
78
77
|
}
|
|
79
|
-
function formatLocationSuffix(location) {
|
|
78
|
+
function formatLocationSuffix(location, locationLabel) {
|
|
80
79
|
const columnText = location.column != null ? `, column ${location.column}` : '';
|
|
81
|
-
return
|
|
80
|
+
return `${locationLabel} line ${location.line}${columnText}`;
|
|
82
81
|
}
|
|
83
82
|
function replaceStackHeader(stack, error) {
|
|
84
83
|
if (!stack) {
|
|
@@ -90,15 +89,21 @@ function replaceStackHeader(stack, error) {
|
|
|
90
89
|
}
|
|
91
90
|
export async function enrichCodeNodeErrorWithLocation(params) {
|
|
92
91
|
const originalError = getError(params.error);
|
|
92
|
+
const userCodeLineOffset = params.userCodeLineOffset ?? 0;
|
|
93
93
|
const location = getRuntimeErrorLocationFromStack({
|
|
94
94
|
code: params.code,
|
|
95
95
|
error: originalError,
|
|
96
96
|
sourceUrl: params.sourceUrl,
|
|
97
|
-
|
|
97
|
+
userCodeLineOffset,
|
|
98
|
+
}) ??
|
|
99
|
+
(await getSyntaxErrorLocation(params.code, originalError, {
|
|
100
|
+
diagnosticCode: params.diagnosticCode,
|
|
101
|
+
userCodeLineOffset,
|
|
102
|
+
}));
|
|
98
103
|
if (!location) {
|
|
99
104
|
return originalError;
|
|
100
105
|
}
|
|
101
|
-
originalError.message = `${originalError.message} (${formatLocationSuffix(location)})`;
|
|
106
|
+
originalError.message = `${originalError.message} (${formatLocationSuffix(location, params.locationLabel ?? 'Code node')})`;
|
|
102
107
|
originalError.stack = replaceStackHeader(originalError.stack, originalError);
|
|
103
108
|
return originalError;
|
|
104
109
|
}
|