@promptbook/documents 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/documents",
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/documents.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
  "colors": "1.4.0",
package/umd/index.umd.js CHANGED
@@ -26,7 +26,7 @@
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
@@ -3794,119 +3794,60 @@
3794
3794
  }
3795
3795
 
3796
3796
  /**
3797
- * Extract all used variable names from ginen JavaScript/TypeScript script
3797
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3798
3798
  *
3799
- * @param script JavaScript/TypeScript script
3800
- * @returns Set of variable names
3799
+ * @param script from which to extract the variables
3800
+ * @returns the list of variable names
3801
3801
  * @throws {ParseError} if the script is invalid
3802
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
3802
+ * @public exported from `@promptbook/execute-javascript`
3803
3803
  */
3804
- function extractVariablesFromScript(script) {
3805
- if (script.trim() === '') {
3806
- return new Set();
3807
- }
3804
+ function extractVariablesFromJavascript(script) {
3808
3805
  const variables = new Set();
3809
- // JS keywords and builtins to exclude
3810
- const exclude = new Set([
3811
- // Keywords
3812
- 'break',
3813
- 'case',
3814
- 'catch',
3815
- 'class',
3816
- 'const',
3817
- 'continue',
3818
- 'debugger',
3819
- 'default',
3820
- 'delete',
3821
- 'do',
3822
- 'else',
3823
- 'export',
3824
- 'extends',
3825
- 'false',
3826
- 'finally',
3827
- 'for',
3828
- 'function',
3829
- 'if',
3830
- 'import',
3831
- 'in',
3832
- 'instanceof',
3833
- 'let',
3834
- 'new',
3835
- 'null',
3836
- 'return',
3837
- 'super',
3838
- 'switch',
3839
- 'this',
3840
- 'throw',
3841
- 'true',
3842
- 'try',
3843
- 'typeof',
3844
- 'var',
3845
- 'void',
3846
- 'while',
3847
- 'with',
3848
- 'yield',
3849
- // Common globals
3850
- 'console',
3851
- 'JSON',
3852
- 'Error',
3853
- // Typescript types
3854
- 'string',
3855
- 'number',
3856
- 'boolean',
3857
- 'object',
3858
- 'symbol',
3859
- // Common methods on built-in objects
3860
- 'test',
3861
- 'match',
3862
- 'exec',
3863
- 'replace',
3864
- 'search',
3865
- 'split',
3866
- ]);
3806
+ const originalScript = script;
3807
+ script = `(()=>{${script}})()`;
3867
3808
  try {
3868
- // Note: Extract variables from template literals like ${variable}
3869
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
3870
- let match;
3871
- while ((match = templateRegex.exec(script)) !== null) {
3872
- const varName = match[1];
3873
- if (!exclude.has(varName)) {
3874
- variables.add(varName);
3809
+ for (let i = 0; i < LOOP_LIMIT; i++)
3810
+ try {
3811
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3875
3812
  }
3876
- }
3877
- // Note: Process the script to handle normal variable usage
3878
- const processedScript = script
3879
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
3880
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
3881
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
3882
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
3883
- // Note: Find identifiers in function arguments
3884
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
3885
- const funcNames = new Set();
3886
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
3887
- funcNames.add(match[1]);
3888
- }
3889
- // Find variable declarations to exclude them
3890
- const declaredVars = new Set();
3891
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3892
- while ((match = declRegex.exec(processedScript)) !== null) {
3893
- declaredVars.add(match[2]);
3894
- }
3895
- // Note: Find identifiers in the script
3896
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
3897
- while ((match = identifierRegex.exec(processedScript)) !== null) {
3898
- const name = match[1];
3899
- // Add if not excluded, not a function name, and not a declared variable
3900
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
3901
- variables.add(name);
3813
+ catch (error) {
3814
+ if (!(error instanceof ReferenceError)) {
3815
+ throw error;
3816
+ }
3817
+ /*
3818
+ Note: Parsing the error
3819
+ 🌟 Most devices:
3820
+ [PipelineUrlError: thing is not defined]
3821
+
3822
+ 🍏 iPhone`s Safari:
3823
+ [PipelineUrlError: Can't find variable: thing]
3824
+ */
3825
+ let variableName = undefined;
3826
+ if (error.message.startsWith(`Can't`)) {
3827
+ // 🍏 Case
3828
+ variableName = error.message.split(' ').pop();
3829
+ }
3830
+ else {
3831
+ // 🌟 Case
3832
+ variableName = error.message.split(' ').shift();
3833
+ }
3834
+ if (variableName === undefined) {
3835
+ throw error;
3836
+ }
3837
+ if (script.includes(variableName + '(')) {
3838
+ script = `const ${variableName} = ()=>'';` + script;
3839
+ }
3840
+ else {
3841
+ variables.add(variableName);
3842
+ script = `const ${variableName} = '';` + script;
3843
+ }
3902
3844
  }
3903
- }
3904
3845
  }
3905
3846
  catch (error) {
3906
3847
  if (!(error instanceof Error)) {
3907
3848
  throw error;
3908
3849
  }
3909
- throw new ParseError(spaceTrim__default["default"]((block) => `
3850
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
3910
3851
  Can not extract variables from the script
3911
3852
  ${block(error.stack || error.message)}
3912
3853
 
@@ -3919,7 +3860,7 @@
3919
3860
  The script:
3920
3861
 
3921
3862
  \`\`\`javascript
3922
- ${block(script)}
3863
+ ${block(originalScript)}
3923
3864
  \`\`\`
3924
3865
  `));
3925
3866
  }
@@ -3940,19 +3881,24 @@
3940
3881
  function extractParameterNamesFromTask(task) {
3941
3882
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3942
3883
  const parameterNames = new Set();
3884
+ let contentParameters;
3885
+ if (taskType !== 'SCRIPT_TASK') {
3886
+ contentParameters = extractParameterNames(content);
3887
+ }
3888
+ else {
3889
+ // TODO: What if script is not javascript?
3890
+ // const { contentLanguage } = task;
3891
+ // if (contentLanguage !== 'javascript') {
3892
+ contentParameters = extractVariablesFromJavascript(content);
3893
+ }
3943
3894
  for (const parameterName of [
3944
3895
  ...extractParameterNames(title),
3945
3896
  ...extractParameterNames(description || ''),
3946
- ...extractParameterNames(content),
3897
+ ...contentParameters,
3947
3898
  ...extractParameterNames(preparedContent || ''),
3948
3899
  ]) {
3949
3900
  parameterNames.add(parameterName);
3950
3901
  }
3951
- if (taskType === 'SCRIPT_TASK') {
3952
- for (const parameterName of extractVariablesFromScript(content)) {
3953
- parameterNames.add(parameterName);
3954
- }
3955
- }
3956
3902
  for (const jokerName of jokerParameterNames || []) {
3957
3903
  parameterNames.add(jokerName);
3958
3904
  }