@promptbook/website-crawler 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 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.30';
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
@@ -3649,6 +3649,83 @@ function valueToString(value) {
3649
3649
  }
3650
3650
  }
3651
3651
 
3652
+ /**
3653
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3654
+ *
3655
+ * @param script from which to extract the variables
3656
+ * @returns the list of variable names
3657
+ * @throws {ParseError} if the script is invalid
3658
+ * @public exported from `@promptbook/execute-javascript`
3659
+ */
3660
+ function extractVariablesFromJavascript(script) {
3661
+ const variables = new Set();
3662
+ const originalScript = script;
3663
+ script = `(()=>{${script}})()`;
3664
+ try {
3665
+ for (let i = 0; i < LOOP_LIMIT; i++)
3666
+ try {
3667
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3668
+ }
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
+ }
3700
+ }
3701
+ }
3702
+ catch (error) {
3703
+ if (!(error instanceof Error)) {
3704
+ throw error;
3705
+ }
3706
+ throw new ParseError(spaceTrim((block) => `
3707
+ Can not extract variables from the script
3708
+ ${block(error.stack || error.message)}
3709
+
3710
+ Found variables:
3711
+ ${Array.from(variables)
3712
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
3713
+ .join('\n')}
3714
+
3715
+
3716
+ The script:
3717
+
3718
+ \`\`\`javascript
3719
+ ${block(originalScript)}
3720
+ \`\`\`
3721
+ `));
3722
+ }
3723
+ return variables;
3724
+ }
3725
+ /**
3726
+ * TODO: [🔣] Support for multiple languages - python, java,...
3727
+ */
3728
+
3652
3729
  /**
3653
3730
  * Parses the task and returns the set of all used parameters
3654
3731
  *
@@ -3658,24 +3735,26 @@ function valueToString(value) {
3658
3735
  * @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
3659
3736
  */
3660
3737
  function extractParameterNamesFromTask(task) {
3661
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
3738
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3662
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
+ }
3663
3750
  for (const parameterName of [
3664
3751
  ...extractParameterNames(title),
3665
3752
  ...extractParameterNames(description || ''),
3666
- ...extractParameterNames(content),
3753
+ ...contentParameters,
3667
3754
  ...extractParameterNames(preparedContent || ''),
3668
3755
  ]) {
3669
3756
  parameterNames.add(parameterName);
3670
3757
  }
3671
- /*/
3672
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
3673
- if (taskType === 'SCRIPT_TASK') {
3674
- for (const parameterName of extractVariablesFromScript(content)) {
3675
- parameterNames.add(parameterName);
3676
- }
3677
- }
3678
- /**/
3679
3758
  for (const jokerName of jokerParameterNames || []) {
3680
3759
  parameterNames.add(jokerName);
3681
3760
  }