@promptbook/documents 0.86.30 → 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 +90 -11
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/execute-javascript.index.d.ts +2 -0
- package/esm/typings/src/scripting/javascript/JavascriptEvalExecutionTools.test.d.ts +4 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.d.ts +14 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.test.d.ts +1 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromScript.test.d.ts +1 -0
- package/package.json +2 -2
- package/umd/index.umd.js +90 -11
- package/umd/index.umd.js.map +1 -1
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
|
|
@@ -3795,6 +3795,83 @@ function valueToString(value) {
|
|
|
3795
3795
|
}
|
|
3796
3796
|
}
|
|
3797
3797
|
|
|
3798
|
+
/**
|
|
3799
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
3800
|
+
*
|
|
3801
|
+
* @param script from which to extract the variables
|
|
3802
|
+
* @returns the list of variable names
|
|
3803
|
+
* @throws {ParseError} if the script is invalid
|
|
3804
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
3805
|
+
*/
|
|
3806
|
+
function extractVariablesFromJavascript(script) {
|
|
3807
|
+
const variables = new Set();
|
|
3808
|
+
const originalScript = script;
|
|
3809
|
+
script = `(()=>{${script}})()`;
|
|
3810
|
+
try {
|
|
3811
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
3812
|
+
try {
|
|
3813
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
3814
|
+
}
|
|
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
|
+
}
|
|
3846
|
+
}
|
|
3847
|
+
}
|
|
3848
|
+
catch (error) {
|
|
3849
|
+
if (!(error instanceof Error)) {
|
|
3850
|
+
throw error;
|
|
3851
|
+
}
|
|
3852
|
+
throw new ParseError(spaceTrim((block) => `
|
|
3853
|
+
Can not extract variables from the script
|
|
3854
|
+
${block(error.stack || error.message)}
|
|
3855
|
+
|
|
3856
|
+
Found variables:
|
|
3857
|
+
${Array.from(variables)
|
|
3858
|
+
.map((variableName, i) => `${i + 1}) ${variableName}`)
|
|
3859
|
+
.join('\n')}
|
|
3860
|
+
|
|
3861
|
+
|
|
3862
|
+
The script:
|
|
3863
|
+
|
|
3864
|
+
\`\`\`javascript
|
|
3865
|
+
${block(originalScript)}
|
|
3866
|
+
\`\`\`
|
|
3867
|
+
`));
|
|
3868
|
+
}
|
|
3869
|
+
return variables;
|
|
3870
|
+
}
|
|
3871
|
+
/**
|
|
3872
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
3873
|
+
*/
|
|
3874
|
+
|
|
3798
3875
|
/**
|
|
3799
3876
|
* Parses the task and returns the set of all used parameters
|
|
3800
3877
|
*
|
|
@@ -3804,24 +3881,26 @@ function valueToString(value) {
|
|
|
3804
3881
|
* @public exported from `@promptbook/core` <- Note: [👖] This utility is so tightly interconnected with the Promptbook that it is not exported as util but in core
|
|
3805
3882
|
*/
|
|
3806
3883
|
function extractParameterNamesFromTask(task) {
|
|
3807
|
-
const { title, description,
|
|
3884
|
+
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
3808
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
|
+
}
|
|
3809
3896
|
for (const parameterName of [
|
|
3810
3897
|
...extractParameterNames(title),
|
|
3811
3898
|
...extractParameterNames(description || ''),
|
|
3812
|
-
...
|
|
3899
|
+
...contentParameters,
|
|
3813
3900
|
...extractParameterNames(preparedContent || ''),
|
|
3814
3901
|
]) {
|
|
3815
3902
|
parameterNames.add(parameterName);
|
|
3816
3903
|
}
|
|
3817
|
-
/*/
|
|
3818
|
-
// TODO: [🙊] Fix `extractVariablesFromScript` or delete
|
|
3819
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
3820
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
3821
|
-
parameterNames.add(parameterName);
|
|
3822
|
-
}
|
|
3823
|
-
}
|
|
3824
|
-
/**/
|
|
3825
3904
|
for (const jokerName of jokerParameterNames || []) {
|
|
3826
3905
|
parameterNames.add(jokerName);
|
|
3827
3906
|
}
|