@promptbook/documents 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
|
@@ -28,7 +28,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
28
28
|
* @generated
|
|
29
29
|
* @see https://github.com/webgptorg/promptbook
|
|
30
30
|
*/
|
|
31
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.86.
|
|
31
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
|
|
32
32
|
/**
|
|
33
33
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
34
34
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -3796,119 +3796,60 @@ function valueToString(value) {
|
|
|
3796
3796
|
}
|
|
3797
3797
|
|
|
3798
3798
|
/**
|
|
3799
|
-
*
|
|
3799
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
3800
3800
|
*
|
|
3801
|
-
* @param script
|
|
3802
|
-
* @returns
|
|
3801
|
+
* @param script from which to extract the variables
|
|
3802
|
+
* @returns the list of variable names
|
|
3803
3803
|
* @throws {ParseError} if the script is invalid
|
|
3804
|
-
* @public exported from `@promptbook/
|
|
3804
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
3805
3805
|
*/
|
|
3806
|
-
function
|
|
3807
|
-
if (script.trim() === '') {
|
|
3808
|
-
return new Set();
|
|
3809
|
-
}
|
|
3806
|
+
function extractVariablesFromJavascript(script) {
|
|
3810
3807
|
const variables = new Set();
|
|
3811
|
-
|
|
3812
|
-
|
|
3813
|
-
// Keywords
|
|
3814
|
-
'break',
|
|
3815
|
-
'case',
|
|
3816
|
-
'catch',
|
|
3817
|
-
'class',
|
|
3818
|
-
'const',
|
|
3819
|
-
'continue',
|
|
3820
|
-
'debugger',
|
|
3821
|
-
'default',
|
|
3822
|
-
'delete',
|
|
3823
|
-
'do',
|
|
3824
|
-
'else',
|
|
3825
|
-
'export',
|
|
3826
|
-
'extends',
|
|
3827
|
-
'false',
|
|
3828
|
-
'finally',
|
|
3829
|
-
'for',
|
|
3830
|
-
'function',
|
|
3831
|
-
'if',
|
|
3832
|
-
'import',
|
|
3833
|
-
'in',
|
|
3834
|
-
'instanceof',
|
|
3835
|
-
'let',
|
|
3836
|
-
'new',
|
|
3837
|
-
'null',
|
|
3838
|
-
'return',
|
|
3839
|
-
'super',
|
|
3840
|
-
'switch',
|
|
3841
|
-
'this',
|
|
3842
|
-
'throw',
|
|
3843
|
-
'true',
|
|
3844
|
-
'try',
|
|
3845
|
-
'typeof',
|
|
3846
|
-
'var',
|
|
3847
|
-
'void',
|
|
3848
|
-
'while',
|
|
3849
|
-
'with',
|
|
3850
|
-
'yield',
|
|
3851
|
-
// Common globals
|
|
3852
|
-
'console',
|
|
3853
|
-
'JSON',
|
|
3854
|
-
'Error',
|
|
3855
|
-
// Typescript types
|
|
3856
|
-
'string',
|
|
3857
|
-
'number',
|
|
3858
|
-
'boolean',
|
|
3859
|
-
'object',
|
|
3860
|
-
'symbol',
|
|
3861
|
-
// Common methods on built-in objects
|
|
3862
|
-
'test',
|
|
3863
|
-
'match',
|
|
3864
|
-
'exec',
|
|
3865
|
-
'replace',
|
|
3866
|
-
'search',
|
|
3867
|
-
'split',
|
|
3868
|
-
]);
|
|
3808
|
+
const originalScript = script;
|
|
3809
|
+
script = `(()=>{${script}})()`;
|
|
3869
3810
|
try {
|
|
3870
|
-
|
|
3871
|
-
|
|
3872
|
-
|
|
3873
|
-
while ((match = templateRegex.exec(script)) !== null) {
|
|
3874
|
-
const varName = match[1];
|
|
3875
|
-
if (!exclude.has(varName)) {
|
|
3876
|
-
variables.add(varName);
|
|
3811
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
3812
|
+
try {
|
|
3813
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
3877
3814
|
}
|
|
3878
|
-
|
|
3879
|
-
|
|
3880
|
-
|
|
3881
|
-
|
|
3882
|
-
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
|
|
3886
|
-
|
|
3887
|
-
|
|
3888
|
-
|
|
3889
|
-
|
|
3890
|
-
|
|
3891
|
-
|
|
3892
|
-
|
|
3893
|
-
|
|
3894
|
-
|
|
3895
|
-
|
|
3896
|
-
|
|
3897
|
-
|
|
3898
|
-
|
|
3899
|
-
|
|
3900
|
-
|
|
3901
|
-
|
|
3902
|
-
|
|
3903
|
-
|
|
3815
|
+
catch (error) {
|
|
3816
|
+
if (!(error instanceof ReferenceError)) {
|
|
3817
|
+
throw error;
|
|
3818
|
+
}
|
|
3819
|
+
/*
|
|
3820
|
+
Note: Parsing the error
|
|
3821
|
+
🌟 Most devices:
|
|
3822
|
+
[PipelineUrlError: thing is not defined]
|
|
3823
|
+
|
|
3824
|
+
🍏 iPhone`s Safari:
|
|
3825
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
3826
|
+
*/
|
|
3827
|
+
let variableName = undefined;
|
|
3828
|
+
if (error.message.startsWith(`Can't`)) {
|
|
3829
|
+
// 🍏 Case
|
|
3830
|
+
variableName = error.message.split(' ').pop();
|
|
3831
|
+
}
|
|
3832
|
+
else {
|
|
3833
|
+
// 🌟 Case
|
|
3834
|
+
variableName = error.message.split(' ').shift();
|
|
3835
|
+
}
|
|
3836
|
+
if (variableName === undefined) {
|
|
3837
|
+
throw error;
|
|
3838
|
+
}
|
|
3839
|
+
if (script.includes(variableName + '(')) {
|
|
3840
|
+
script = `const ${variableName} = ()=>'';` + script;
|
|
3841
|
+
}
|
|
3842
|
+
else {
|
|
3843
|
+
variables.add(variableName);
|
|
3844
|
+
script = `const ${variableName} = '';` + script;
|
|
3845
|
+
}
|
|
3904
3846
|
}
|
|
3905
|
-
}
|
|
3906
3847
|
}
|
|
3907
3848
|
catch (error) {
|
|
3908
3849
|
if (!(error instanceof Error)) {
|
|
3909
3850
|
throw error;
|
|
3910
3851
|
}
|
|
3911
|
-
throw new ParseError(spaceTrim
|
|
3852
|
+
throw new ParseError(spaceTrim((block) => `
|
|
3912
3853
|
Can not extract variables from the script
|
|
3913
3854
|
${block(error.stack || error.message)}
|
|
3914
3855
|
|
|
@@ -3921,7 +3862,7 @@ function extractVariablesFromScript(script) {
|
|
|
3921
3862
|
The script:
|
|
3922
3863
|
|
|
3923
3864
|
\`\`\`javascript
|
|
3924
|
-
${block(
|
|
3865
|
+
${block(originalScript)}
|
|
3925
3866
|
\`\`\`
|
|
3926
3867
|
`));
|
|
3927
3868
|
}
|
|
@@ -3942,19 +3883,24 @@ function extractVariablesFromScript(script) {
|
|
|
3942
3883
|
function extractParameterNamesFromTask(task) {
|
|
3943
3884
|
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
3944
3885
|
const parameterNames = new Set();
|
|
3886
|
+
let contentParameters;
|
|
3887
|
+
if (taskType !== 'SCRIPT_TASK') {
|
|
3888
|
+
contentParameters = extractParameterNames(content);
|
|
3889
|
+
}
|
|
3890
|
+
else {
|
|
3891
|
+
// TODO: What if script is not javascript?
|
|
3892
|
+
// const { contentLanguage } = task;
|
|
3893
|
+
// if (contentLanguage !== 'javascript') {
|
|
3894
|
+
contentParameters = extractVariablesFromJavascript(content);
|
|
3895
|
+
}
|
|
3945
3896
|
for (const parameterName of [
|
|
3946
3897
|
...extractParameterNames(title),
|
|
3947
3898
|
...extractParameterNames(description || ''),
|
|
3948
|
-
...
|
|
3899
|
+
...contentParameters,
|
|
3949
3900
|
...extractParameterNames(preparedContent || ''),
|
|
3950
3901
|
]) {
|
|
3951
3902
|
parameterNames.add(parameterName);
|
|
3952
3903
|
}
|
|
3953
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
3954
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
3955
|
-
parameterNames.add(parameterName);
|
|
3956
|
-
}
|
|
3957
|
-
}
|
|
3958
3904
|
for (const jokerName of jokerParameterNames || []) {
|
|
3959
3905
|
parameterNames.add(jokerName);
|
|
3960
3906
|
}
|