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