@promptbook/markdown-utils 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 +1 -1
- package/umd/index.umd.js +90 -11
- package/umd/index.umd.js.map +1 -1
|
@@ -2,7 +2,9 @@ import { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION } from '../version';
|
|
|
2
2
|
import { JavascriptEvalExecutionTools } from '../scripting/javascript/JavascriptEvalExecutionTools';
|
|
3
3
|
import { JavascriptExecutionTools } from '../scripting/javascript/JavascriptExecutionTools';
|
|
4
4
|
import { POSTPROCESSING_FUNCTIONS } from '../scripting/javascript/postprocessing-functions';
|
|
5
|
+
import { extractVariablesFromJavascript } from '../scripting/javascript/utils/extractVariablesFromJavascript';
|
|
5
6
|
export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
|
|
6
7
|
export { JavascriptEvalExecutionTools };
|
|
7
8
|
export { JavascriptExecutionTools };
|
|
8
9
|
export { POSTPROCESSING_FUNCTIONS };
|
|
10
|
+
export { extractVariablesFromJavascript };
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { string_javascript } from '../../../types/typeAliases';
|
|
2
|
+
import type { string_javascript_name } from '../../../types/typeAliases';
|
|
3
|
+
/**
|
|
4
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
5
|
+
*
|
|
6
|
+
* @param script from which to extract the variables
|
|
7
|
+
* @returns the list of variable names
|
|
8
|
+
* @throws {ParseError} if the script is invalid
|
|
9
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractVariablesFromJavascript(script: string_javascript): Set<string_javascript_name>;
|
|
12
|
+
/**
|
|
13
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
14
|
+
*/
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
package/umd/index.umd.js
CHANGED
|
@@ -25,7 +25,7 @@
|
|
|
25
25
|
* @generated
|
|
26
26
|
* @see https://github.com/webgptorg/promptbook
|
|
27
27
|
*/
|
|
28
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.86.
|
|
28
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
|
|
29
29
|
/**
|
|
30
30
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
31
31
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -3720,6 +3720,83 @@
|
|
|
3720
3720
|
}
|
|
3721
3721
|
}
|
|
3722
3722
|
|
|
3723
|
+
/**
|
|
3724
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
3725
|
+
*
|
|
3726
|
+
* @param script from which to extract the variables
|
|
3727
|
+
* @returns the list of variable names
|
|
3728
|
+
* @throws {ParseError} if the script is invalid
|
|
3729
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
3730
|
+
*/
|
|
3731
|
+
function extractVariablesFromJavascript(script) {
|
|
3732
|
+
const variables = new Set();
|
|
3733
|
+
const originalScript = script;
|
|
3734
|
+
script = `(()=>{${script}})()`;
|
|
3735
|
+
try {
|
|
3736
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
3737
|
+
try {
|
|
3738
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
3739
|
+
}
|
|
3740
|
+
catch (error) {
|
|
3741
|
+
if (!(error instanceof ReferenceError)) {
|
|
3742
|
+
throw error;
|
|
3743
|
+
}
|
|
3744
|
+
/*
|
|
3745
|
+
Note: Parsing the error
|
|
3746
|
+
🌟 Most devices:
|
|
3747
|
+
[PipelineUrlError: thing is not defined]
|
|
3748
|
+
|
|
3749
|
+
🍏 iPhone`s Safari:
|
|
3750
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
3751
|
+
*/
|
|
3752
|
+
let variableName = undefined;
|
|
3753
|
+
if (error.message.startsWith(`Can't`)) {
|
|
3754
|
+
// 🍏 Case
|
|
3755
|
+
variableName = error.message.split(' ').pop();
|
|
3756
|
+
}
|
|
3757
|
+
else {
|
|
3758
|
+
// 🌟 Case
|
|
3759
|
+
variableName = error.message.split(' ').shift();
|
|
3760
|
+
}
|
|
3761
|
+
if (variableName === undefined) {
|
|
3762
|
+
throw error;
|
|
3763
|
+
}
|
|
3764
|
+
if (script.includes(variableName + '(')) {
|
|
3765
|
+
script = `const ${variableName} = ()=>'';` + script;
|
|
3766
|
+
}
|
|
3767
|
+
else {
|
|
3768
|
+
variables.add(variableName);
|
|
3769
|
+
script = `const ${variableName} = '';` + script;
|
|
3770
|
+
}
|
|
3771
|
+
}
|
|
3772
|
+
}
|
|
3773
|
+
catch (error) {
|
|
3774
|
+
if (!(error instanceof Error)) {
|
|
3775
|
+
throw error;
|
|
3776
|
+
}
|
|
3777
|
+
throw new ParseError(spaceTrim.spaceTrim((block) => `
|
|
3778
|
+
Can not extract variables from the script
|
|
3779
|
+
${block(error.stack || error.message)}
|
|
3780
|
+
|
|
3781
|
+
Found variables:
|
|
3782
|
+
${Array.from(variables)
|
|
3783
|
+
.map((variableName, i) => `${i + 1}) ${variableName}`)
|
|
3784
|
+
.join('\n')}
|
|
3785
|
+
|
|
3786
|
+
|
|
3787
|
+
The script:
|
|
3788
|
+
|
|
3789
|
+
\`\`\`javascript
|
|
3790
|
+
${block(originalScript)}
|
|
3791
|
+
\`\`\`
|
|
3792
|
+
`));
|
|
3793
|
+
}
|
|
3794
|
+
return variables;
|
|
3795
|
+
}
|
|
3796
|
+
/**
|
|
3797
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
3798
|
+
*/
|
|
3799
|
+
|
|
3723
3800
|
/**
|
|
3724
3801
|
* Parses the task and returns the set of all used parameters
|
|
3725
3802
|
*
|
|
@@ -3729,24 +3806,26 @@
|
|
|
3729
3806
|
* @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
|
|
3730
3807
|
*/
|
|
3731
3808
|
function extractParameterNamesFromTask(task) {
|
|
3732
|
-
const { title, description,
|
|
3809
|
+
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
3733
3810
|
const parameterNames = new Set();
|
|
3811
|
+
let contentParameters;
|
|
3812
|
+
if (taskType !== 'SCRIPT_TASK') {
|
|
3813
|
+
contentParameters = extractParameterNames(content);
|
|
3814
|
+
}
|
|
3815
|
+
else {
|
|
3816
|
+
// TODO: What if script is not javascript?
|
|
3817
|
+
// const { contentLanguage } = task;
|
|
3818
|
+
// if (contentLanguage !== 'javascript') {
|
|
3819
|
+
contentParameters = extractVariablesFromJavascript(content);
|
|
3820
|
+
}
|
|
3734
3821
|
for (const parameterName of [
|
|
3735
3822
|
...extractParameterNames(title),
|
|
3736
3823
|
...extractParameterNames(description || ''),
|
|
3737
|
-
...
|
|
3824
|
+
...contentParameters,
|
|
3738
3825
|
...extractParameterNames(preparedContent || ''),
|
|
3739
3826
|
]) {
|
|
3740
3827
|
parameterNames.add(parameterName);
|
|
3741
3828
|
}
|
|
3742
|
-
/*/
|
|
3743
|
-
// TODO: [🙊] Fix `extractVariablesFromScript` or delete
|
|
3744
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
3745
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
3746
|
-
parameterNames.add(parameterName);
|
|
3747
|
-
}
|
|
3748
|
-
}
|
|
3749
|
-
/**/
|
|
3750
3829
|
for (const jokerName of jokerParameterNames || []) {
|
|
3751
3830
|
parameterNames.add(jokerName);
|
|
3752
3831
|
}
|