@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.
@@ -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 };
@@ -2,7 +2,6 @@ import { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION } from '../version';
2
2
  import { VALUE_STRINGS } from '../config';
3
3
  import { SMALL_NUMBER } from '../config';
4
4
  import { renderPromptbookMermaid } from '../conversion/prettify/renderPipelineMermaidOptions';
5
- import { extractVariablesFromScript } from '../conversion/utils/extractVariablesFromScript';
6
5
  import { deserializeError } from '../errors/utils/deserializeError';
7
6
  import { serializeError } from '../errors/utils/serializeError';
8
7
  import { forEachAsync } from '../execution/utils/forEachAsync';
@@ -84,7 +83,6 @@ export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
84
83
  export { VALUE_STRINGS };
85
84
  export { SMALL_NUMBER };
86
85
  export { renderPromptbookMermaid };
87
- export { extractVariablesFromScript };
88
86
  export { deserializeError };
89
87
  export { serializeError };
90
88
  export { forEachAsync };
@@ -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
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/pdf",
3
- "version": "0.86.22",
3
+ "version": "0.86.31",
4
4
  "description": "It's time for a paradigm shift. The future of software in plain English, French or Latin",
5
5
  "private": false,
6
6
  "sideEffects": false,
@@ -47,7 +47,7 @@
47
47
  "module": "./esm/index.es.js",
48
48
  "typings": "./esm/typings/src/_packages/pdf.index.d.ts",
49
49
  "peerDependencies": {
50
- "@promptbook/core": "0.86.22"
50
+ "@promptbook/core": "0.86.31"
51
51
  },
52
52
  "dependencies": {
53
53
  "crypto": "^1.0.1",
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.22';
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
@@ -3648,119 +3648,60 @@
3648
3648
  }
3649
3649
 
3650
3650
  /**
3651
- * Extract all used variable names from ginen JavaScript/TypeScript script
3651
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3652
3652
  *
3653
- * @param script JavaScript/TypeScript script
3654
- * @returns Set of variable names
3653
+ * @param script from which to extract the variables
3654
+ * @returns the list of variable names
3655
3655
  * @throws {ParseError} if the script is invalid
3656
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
3656
+ * @public exported from `@promptbook/execute-javascript`
3657
3657
  */
3658
- function extractVariablesFromScript(script) {
3659
- if (script.trim() === '') {
3660
- return new Set();
3661
- }
3658
+ function extractVariablesFromJavascript(script) {
3662
3659
  const variables = new Set();
3663
- // JS keywords and builtins to exclude
3664
- const exclude = new Set([
3665
- // Keywords
3666
- 'break',
3667
- 'case',
3668
- 'catch',
3669
- 'class',
3670
- 'const',
3671
- 'continue',
3672
- 'debugger',
3673
- 'default',
3674
- 'delete',
3675
- 'do',
3676
- 'else',
3677
- 'export',
3678
- 'extends',
3679
- 'false',
3680
- 'finally',
3681
- 'for',
3682
- 'function',
3683
- 'if',
3684
- 'import',
3685
- 'in',
3686
- 'instanceof',
3687
- 'let',
3688
- 'new',
3689
- 'null',
3690
- 'return',
3691
- 'super',
3692
- 'switch',
3693
- 'this',
3694
- 'throw',
3695
- 'true',
3696
- 'try',
3697
- 'typeof',
3698
- 'var',
3699
- 'void',
3700
- 'while',
3701
- 'with',
3702
- 'yield',
3703
- // Common globals
3704
- 'console',
3705
- 'JSON',
3706
- 'Error',
3707
- // Typescript types
3708
- 'string',
3709
- 'number',
3710
- 'boolean',
3711
- 'object',
3712
- 'symbol',
3713
- // Common methods on built-in objects
3714
- 'test',
3715
- 'match',
3716
- 'exec',
3717
- 'replace',
3718
- 'search',
3719
- 'split',
3720
- ]);
3660
+ const originalScript = script;
3661
+ script = `(()=>{${script}})()`;
3721
3662
  try {
3722
- // Note: Extract variables from template literals like ${variable}
3723
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
3724
- let match;
3725
- while ((match = templateRegex.exec(script)) !== null) {
3726
- const varName = match[1];
3727
- if (!exclude.has(varName)) {
3728
- variables.add(varName);
3663
+ for (let i = 0; i < LOOP_LIMIT; i++)
3664
+ try {
3665
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3729
3666
  }
3730
- }
3731
- // Note: Process the script to handle normal variable usage
3732
- const processedScript = script
3733
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
3734
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
3735
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
3736
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
3737
- // Note: Find identifiers in function arguments
3738
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
3739
- const funcNames = new Set();
3740
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
3741
- funcNames.add(match[1]);
3742
- }
3743
- // Find variable declarations to exclude them
3744
- const declaredVars = new Set();
3745
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3746
- while ((match = declRegex.exec(processedScript)) !== null) {
3747
- declaredVars.add(match[2]);
3748
- }
3749
- // Note: Find identifiers in the script
3750
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3751
- while ((match = identifierRegex.exec(processedScript)) !== null) {
3752
- const name = match[1];
3753
- // Add if not excluded, not a function name, and not a declared variable
3754
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
3755
- variables.add(name);
3667
+ catch (error) {
3668
+ if (!(error instanceof ReferenceError)) {
3669
+ throw error;
3670
+ }
3671
+ /*
3672
+ Note: Parsing the error
3673
+ 🌟 Most devices:
3674
+ [PipelineUrlError: thing is not defined]
3675
+
3676
+ 🍏 iPhone`s Safari:
3677
+ [PipelineUrlError: Can't find variable: thing]
3678
+ */
3679
+ let variableName = undefined;
3680
+ if (error.message.startsWith(`Can't`)) {
3681
+ // 🍏 Case
3682
+ variableName = error.message.split(' ').pop();
3683
+ }
3684
+ else {
3685
+ // 🌟 Case
3686
+ variableName = error.message.split(' ').shift();
3687
+ }
3688
+ if (variableName === undefined) {
3689
+ throw error;
3690
+ }
3691
+ if (script.includes(variableName + '(')) {
3692
+ script = `const ${variableName} = ()=>'';` + script;
3693
+ }
3694
+ else {
3695
+ variables.add(variableName);
3696
+ script = `const ${variableName} = '';` + script;
3697
+ }
3756
3698
  }
3757
- }
3758
3699
  }
3759
3700
  catch (error) {
3760
3701
  if (!(error instanceof Error)) {
3761
3702
  throw error;
3762
3703
  }
3763
- throw new ParseError(spaceTrim__default["default"]((block) => `
3704
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
3764
3705
  Can not extract variables from the script
3765
3706
  ${block(error.stack || error.message)}
3766
3707
 
@@ -3773,7 +3714,7 @@
3773
3714
  The script:
3774
3715
 
3775
3716
  \`\`\`javascript
3776
- ${block(script)}
3717
+ ${block(originalScript)}
3777
3718
  \`\`\`
3778
3719
  `));
3779
3720
  }
@@ -3794,19 +3735,24 @@
3794
3735
  function extractParameterNamesFromTask(task) {
3795
3736
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3796
3737
  const parameterNames = new Set();
3738
+ let contentParameters;
3739
+ if (taskType !== 'SCRIPT_TASK') {
3740
+ contentParameters = extractParameterNames(content);
3741
+ }
3742
+ else {
3743
+ // TODO: What if script is not javascript?
3744
+ // const { contentLanguage } = task;
3745
+ // if (contentLanguage !== 'javascript') {
3746
+ contentParameters = extractVariablesFromJavascript(content);
3747
+ }
3797
3748
  for (const parameterName of [
3798
3749
  ...extractParameterNames(title),
3799
3750
  ...extractParameterNames(description || ''),
3800
- ...extractParameterNames(content),
3751
+ ...contentParameters,
3801
3752
  ...extractParameterNames(preparedContent || ''),
3802
3753
  ]) {
3803
3754
  parameterNames.add(parameterName);
3804
3755
  }
3805
- if (taskType === 'SCRIPT_TASK') {
3806
- for (const parameterName of extractVariablesFromScript(content)) {
3807
- parameterNames.add(parameterName);
3808
- }
3809
- }
3810
3756
  for (const jokerName of jokerParameterNames || []) {
3811
3757
  parameterNames.add(jokerName);
3812
3758
  }