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