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