@promptbook/website-crawler 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 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.22';
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
@@ -3650,119 +3650,60 @@ function valueToString(value) {
3650
3650
  }
3651
3651
 
3652
3652
  /**
3653
- * Extract all used variable names from ginen JavaScript/TypeScript script
3653
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3654
3654
  *
3655
- * @param script JavaScript/TypeScript script
3656
- * @returns Set of variable names
3655
+ * @param script from which to extract the variables
3656
+ * @returns the list of variable names
3657
3657
  * @throws {ParseError} if the script is invalid
3658
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
3658
+ * @public exported from `@promptbook/execute-javascript`
3659
3659
  */
3660
- function extractVariablesFromScript(script) {
3661
- if (script.trim() === '') {
3662
- return new Set();
3663
- }
3660
+ function extractVariablesFromJavascript(script) {
3664
3661
  const variables = new Set();
3665
- // JS keywords and builtins to exclude
3666
- const exclude = new Set([
3667
- // Keywords
3668
- 'break',
3669
- 'case',
3670
- 'catch',
3671
- 'class',
3672
- 'const',
3673
- 'continue',
3674
- 'debugger',
3675
- 'default',
3676
- 'delete',
3677
- 'do',
3678
- 'else',
3679
- 'export',
3680
- 'extends',
3681
- 'false',
3682
- 'finally',
3683
- 'for',
3684
- 'function',
3685
- 'if',
3686
- 'import',
3687
- 'in',
3688
- 'instanceof',
3689
- 'let',
3690
- 'new',
3691
- 'null',
3692
- 'return',
3693
- 'super',
3694
- 'switch',
3695
- 'this',
3696
- 'throw',
3697
- 'true',
3698
- 'try',
3699
- 'typeof',
3700
- 'var',
3701
- 'void',
3702
- 'while',
3703
- 'with',
3704
- 'yield',
3705
- // Common globals
3706
- 'console',
3707
- 'JSON',
3708
- 'Error',
3709
- // Typescript types
3710
- 'string',
3711
- 'number',
3712
- 'boolean',
3713
- 'object',
3714
- 'symbol',
3715
- // Common methods on built-in objects
3716
- 'test',
3717
- 'match',
3718
- 'exec',
3719
- 'replace',
3720
- 'search',
3721
- 'split',
3722
- ]);
3662
+ const originalScript = script;
3663
+ script = `(()=>{${script}})()`;
3723
3664
  try {
3724
- // Note: Extract variables from template literals like ${variable}
3725
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
3726
- let match;
3727
- while ((match = templateRegex.exec(script)) !== null) {
3728
- const varName = match[1];
3729
- if (!exclude.has(varName)) {
3730
- variables.add(varName);
3665
+ for (let i = 0; i < LOOP_LIMIT; i++)
3666
+ try {
3667
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3731
3668
  }
3732
- }
3733
- // Note: Process the script to handle normal variable usage
3734
- const processedScript = script
3735
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
3736
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
3737
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
3738
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
3739
- // Note: Find identifiers in function arguments
3740
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
3741
- const funcNames = new Set();
3742
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
3743
- funcNames.add(match[1]);
3744
- }
3745
- // Find variable declarations to exclude them
3746
- const declaredVars = new Set();
3747
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3748
- while ((match = declRegex.exec(processedScript)) !== null) {
3749
- declaredVars.add(match[2]);
3750
- }
3751
- // Note: Find identifiers in the script
3752
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3753
- while ((match = identifierRegex.exec(processedScript)) !== null) {
3754
- const name = match[1];
3755
- // Add if not excluded, not a function name, and not a declared variable
3756
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
3757
- variables.add(name);
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
+ }
3758
3700
  }
3759
- }
3760
3701
  }
3761
3702
  catch (error) {
3762
3703
  if (!(error instanceof Error)) {
3763
3704
  throw error;
3764
3705
  }
3765
- throw new ParseError(spaceTrim$1((block) => `
3706
+ throw new ParseError(spaceTrim((block) => `
3766
3707
  Can not extract variables from the script
3767
3708
  ${block(error.stack || error.message)}
3768
3709
 
@@ -3775,7 +3716,7 @@ function extractVariablesFromScript(script) {
3775
3716
  The script:
3776
3717
 
3777
3718
  \`\`\`javascript
3778
- ${block(script)}
3719
+ ${block(originalScript)}
3779
3720
  \`\`\`
3780
3721
  `));
3781
3722
  }
@@ -3796,19 +3737,24 @@ function extractVariablesFromScript(script) {
3796
3737
  function extractParameterNamesFromTask(task) {
3797
3738
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3798
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
+ }
3799
3750
  for (const parameterName of [
3800
3751
  ...extractParameterNames(title),
3801
3752
  ...extractParameterNames(description || ''),
3802
- ...extractParameterNames(content),
3753
+ ...contentParameters,
3803
3754
  ...extractParameterNames(preparedContent || ''),
3804
3755
  ]) {
3805
3756
  parameterNames.add(parameterName);
3806
3757
  }
3807
- if (taskType === 'SCRIPT_TASK') {
3808
- for (const parameterName of extractVariablesFromScript(content)) {
3809
- parameterNames.add(parameterName);
3810
- }
3811
- }
3812
3758
  for (const jokerName of jokerParameterNames || []) {
3813
3759
  parameterNames.add(jokerName);
3814
3760
  }