@promptbook/website-crawler 0.86.22 → 0.86.31
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/esm/index.es.js +55 -109
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/execute-javascript.index.d.ts +2 -0
- package/esm/typings/src/_packages/utils.index.d.ts +0 -2
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.d.ts +14 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromScript.test.d.ts +1 -0
- package/package.json +2 -2
- package/umd/index.umd.js +55 -109
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/conversion/utils/extractVariablesFromScript.d.ts +0 -14
- /package/esm/typings/src/{conversion/utils/extractVariablesFromScript.test.d.ts → scripting/javascript/utils/extractVariablesFromJavascript.test.d.ts} +0 -0
package/esm/index.es.js
CHANGED
|
@@ -29,7 +29,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
29
29
|
* @generated
|
|
30
30
|
* @see https://github.com/webgptorg/promptbook
|
|
31
31
|
*/
|
|
32
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.86.
|
|
32
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
|
|
33
33
|
/**
|
|
34
34
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
35
35
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -3650,119 +3650,60 @@ function valueToString(value) {
|
|
|
3650
3650
|
}
|
|
3651
3651
|
|
|
3652
3652
|
/**
|
|
3653
|
-
*
|
|
3653
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
3654
3654
|
*
|
|
3655
|
-
* @param script
|
|
3656
|
-
* @returns
|
|
3655
|
+
* @param script from which to extract the variables
|
|
3656
|
+
* @returns the list of variable names
|
|
3657
3657
|
* @throws {ParseError} if the script is invalid
|
|
3658
|
-
* @public exported from `@promptbook/
|
|
3658
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
3659
3659
|
*/
|
|
3660
|
-
function
|
|
3661
|
-
if (script.trim() === '') {
|
|
3662
|
-
return new Set();
|
|
3663
|
-
}
|
|
3660
|
+
function extractVariablesFromJavascript(script) {
|
|
3664
3661
|
const variables = new Set();
|
|
3665
|
-
|
|
3666
|
-
|
|
3667
|
-
// Keywords
|
|
3668
|
-
'break',
|
|
3669
|
-
'case',
|
|
3670
|
-
'catch',
|
|
3671
|
-
'class',
|
|
3672
|
-
'const',
|
|
3673
|
-
'continue',
|
|
3674
|
-
'debugger',
|
|
3675
|
-
'default',
|
|
3676
|
-
'delete',
|
|
3677
|
-
'do',
|
|
3678
|
-
'else',
|
|
3679
|
-
'export',
|
|
3680
|
-
'extends',
|
|
3681
|
-
'false',
|
|
3682
|
-
'finally',
|
|
3683
|
-
'for',
|
|
3684
|
-
'function',
|
|
3685
|
-
'if',
|
|
3686
|
-
'import',
|
|
3687
|
-
'in',
|
|
3688
|
-
'instanceof',
|
|
3689
|
-
'let',
|
|
3690
|
-
'new',
|
|
3691
|
-
'null',
|
|
3692
|
-
'return',
|
|
3693
|
-
'super',
|
|
3694
|
-
'switch',
|
|
3695
|
-
'this',
|
|
3696
|
-
'throw',
|
|
3697
|
-
'true',
|
|
3698
|
-
'try',
|
|
3699
|
-
'typeof',
|
|
3700
|
-
'var',
|
|
3701
|
-
'void',
|
|
3702
|
-
'while',
|
|
3703
|
-
'with',
|
|
3704
|
-
'yield',
|
|
3705
|
-
// Common globals
|
|
3706
|
-
'console',
|
|
3707
|
-
'JSON',
|
|
3708
|
-
'Error',
|
|
3709
|
-
// Typescript types
|
|
3710
|
-
'string',
|
|
3711
|
-
'number',
|
|
3712
|
-
'boolean',
|
|
3713
|
-
'object',
|
|
3714
|
-
'symbol',
|
|
3715
|
-
// Common methods on built-in objects
|
|
3716
|
-
'test',
|
|
3717
|
-
'match',
|
|
3718
|
-
'exec',
|
|
3719
|
-
'replace',
|
|
3720
|
-
'search',
|
|
3721
|
-
'split',
|
|
3722
|
-
]);
|
|
3662
|
+
const originalScript = script;
|
|
3663
|
+
script = `(()=>{${script}})()`;
|
|
3723
3664
|
try {
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
while ((match = templateRegex.exec(script)) !== null) {
|
|
3728
|
-
const varName = match[1];
|
|
3729
|
-
if (!exclude.has(varName)) {
|
|
3730
|
-
variables.add(varName);
|
|
3665
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
3666
|
+
try {
|
|
3667
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
3731
3668
|
}
|
|
3732
|
-
|
|
3733
|
-
|
|
3734
|
-
|
|
3735
|
-
|
|
3736
|
-
|
|
3737
|
-
|
|
3738
|
-
|
|
3739
|
-
|
|
3740
|
-
|
|
3741
|
-
|
|
3742
|
-
|
|
3743
|
-
|
|
3744
|
-
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
|
|
3748
|
-
|
|
3749
|
-
|
|
3750
|
-
|
|
3751
|
-
|
|
3752
|
-
|
|
3753
|
-
|
|
3754
|
-
|
|
3755
|
-
|
|
3756
|
-
|
|
3757
|
-
|
|
3669
|
+
catch (error) {
|
|
3670
|
+
if (!(error instanceof ReferenceError)) {
|
|
3671
|
+
throw error;
|
|
3672
|
+
}
|
|
3673
|
+
/*
|
|
3674
|
+
Note: Parsing the error
|
|
3675
|
+
🌟 Most devices:
|
|
3676
|
+
[PipelineUrlError: thing is not defined]
|
|
3677
|
+
|
|
3678
|
+
🍏 iPhone`s Safari:
|
|
3679
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
3680
|
+
*/
|
|
3681
|
+
let variableName = undefined;
|
|
3682
|
+
if (error.message.startsWith(`Can't`)) {
|
|
3683
|
+
// 🍏 Case
|
|
3684
|
+
variableName = error.message.split(' ').pop();
|
|
3685
|
+
}
|
|
3686
|
+
else {
|
|
3687
|
+
// 🌟 Case
|
|
3688
|
+
variableName = error.message.split(' ').shift();
|
|
3689
|
+
}
|
|
3690
|
+
if (variableName === undefined) {
|
|
3691
|
+
throw error;
|
|
3692
|
+
}
|
|
3693
|
+
if (script.includes(variableName + '(')) {
|
|
3694
|
+
script = `const ${variableName} = ()=>'';` + script;
|
|
3695
|
+
}
|
|
3696
|
+
else {
|
|
3697
|
+
variables.add(variableName);
|
|
3698
|
+
script = `const ${variableName} = '';` + script;
|
|
3699
|
+
}
|
|
3758
3700
|
}
|
|
3759
|
-
}
|
|
3760
3701
|
}
|
|
3761
3702
|
catch (error) {
|
|
3762
3703
|
if (!(error instanceof Error)) {
|
|
3763
3704
|
throw error;
|
|
3764
3705
|
}
|
|
3765
|
-
throw new ParseError(spaceTrim
|
|
3706
|
+
throw new ParseError(spaceTrim((block) => `
|
|
3766
3707
|
Can not extract variables from the script
|
|
3767
3708
|
${block(error.stack || error.message)}
|
|
3768
3709
|
|
|
@@ -3775,7 +3716,7 @@ function extractVariablesFromScript(script) {
|
|
|
3775
3716
|
The script:
|
|
3776
3717
|
|
|
3777
3718
|
\`\`\`javascript
|
|
3778
|
-
${block(
|
|
3719
|
+
${block(originalScript)}
|
|
3779
3720
|
\`\`\`
|
|
3780
3721
|
`));
|
|
3781
3722
|
}
|
|
@@ -3796,19 +3737,24 @@ function extractVariablesFromScript(script) {
|
|
|
3796
3737
|
function extractParameterNamesFromTask(task) {
|
|
3797
3738
|
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
3798
3739
|
const parameterNames = new Set();
|
|
3740
|
+
let contentParameters;
|
|
3741
|
+
if (taskType !== 'SCRIPT_TASK') {
|
|
3742
|
+
contentParameters = extractParameterNames(content);
|
|
3743
|
+
}
|
|
3744
|
+
else {
|
|
3745
|
+
// TODO: What if script is not javascript?
|
|
3746
|
+
// const { contentLanguage } = task;
|
|
3747
|
+
// if (contentLanguage !== 'javascript') {
|
|
3748
|
+
contentParameters = extractVariablesFromJavascript(content);
|
|
3749
|
+
}
|
|
3799
3750
|
for (const parameterName of [
|
|
3800
3751
|
...extractParameterNames(title),
|
|
3801
3752
|
...extractParameterNames(description || ''),
|
|
3802
|
-
...
|
|
3753
|
+
...contentParameters,
|
|
3803
3754
|
...extractParameterNames(preparedContent || ''),
|
|
3804
3755
|
]) {
|
|
3805
3756
|
parameterNames.add(parameterName);
|
|
3806
3757
|
}
|
|
3807
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
3808
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
3809
|
-
parameterNames.add(parameterName);
|
|
3810
|
-
}
|
|
3811
|
-
}
|
|
3812
3758
|
for (const jokerName of jokerParameterNames || []) {
|
|
3813
3759
|
parameterNames.add(jokerName);
|
|
3814
3760
|
}
|