@promptbook/remote-server 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/remote-server",
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/remote-server.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
@@ -28,7 +28,7 @@
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
@@ -3964,119 +3964,60 @@
3964
3964
  }
3965
3965
 
3966
3966
  /**
3967
- * Extract all used variable names from ginen JavaScript/TypeScript script
3967
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3968
3968
  *
3969
- * @param script JavaScript/TypeScript script
3970
- * @returns Set of variable names
3969
+ * @param script from which to extract the variables
3970
+ * @returns the list of variable names
3971
3971
  * @throws {ParseError} if the script is invalid
3972
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
3972
+ * @public exported from `@promptbook/execute-javascript`
3973
3973
  */
3974
- function extractVariablesFromScript(script) {
3975
- if (script.trim() === '') {
3976
- return new Set();
3977
- }
3974
+ function extractVariablesFromJavascript(script) {
3978
3975
  const variables = new Set();
3979
- // JS keywords and builtins to exclude
3980
- const exclude = new Set([
3981
- // Keywords
3982
- 'break',
3983
- 'case',
3984
- 'catch',
3985
- 'class',
3986
- 'const',
3987
- 'continue',
3988
- 'debugger',
3989
- 'default',
3990
- 'delete',
3991
- 'do',
3992
- 'else',
3993
- 'export',
3994
- 'extends',
3995
- 'false',
3996
- 'finally',
3997
- 'for',
3998
- 'function',
3999
- 'if',
4000
- 'import',
4001
- 'in',
4002
- 'instanceof',
4003
- 'let',
4004
- 'new',
4005
- 'null',
4006
- 'return',
4007
- 'super',
4008
- 'switch',
4009
- 'this',
4010
- 'throw',
4011
- 'true',
4012
- 'try',
4013
- 'typeof',
4014
- 'var',
4015
- 'void',
4016
- 'while',
4017
- 'with',
4018
- 'yield',
4019
- // Common globals
4020
- 'console',
4021
- 'JSON',
4022
- 'Error',
4023
- // Typescript types
4024
- 'string',
4025
- 'number',
4026
- 'boolean',
4027
- 'object',
4028
- 'symbol',
4029
- // Common methods on built-in objects
4030
- 'test',
4031
- 'match',
4032
- 'exec',
4033
- 'replace',
4034
- 'search',
4035
- 'split',
4036
- ]);
3976
+ const originalScript = script;
3977
+ script = `(()=>{${script}})()`;
4037
3978
  try {
4038
- // Note: Extract variables from template literals like ${variable}
4039
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
4040
- let match;
4041
- while ((match = templateRegex.exec(script)) !== null) {
4042
- const varName = match[1];
4043
- if (!exclude.has(varName)) {
4044
- variables.add(varName);
3979
+ for (let i = 0; i < LOOP_LIMIT; i++)
3980
+ try {
3981
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
4045
3982
  }
4046
- }
4047
- // Note: Process the script to handle normal variable usage
4048
- const processedScript = script
4049
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
4050
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
4051
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
4052
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
4053
- // Note: Find identifiers in function arguments
4054
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
4055
- const funcNames = new Set();
4056
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
4057
- funcNames.add(match[1]);
4058
- }
4059
- // Find variable declarations to exclude them
4060
- const declaredVars = new Set();
4061
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
4062
- while ((match = declRegex.exec(processedScript)) !== null) {
4063
- declaredVars.add(match[2]);
4064
- }
4065
- // Note: Find identifiers in the script
4066
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
4067
- while ((match = identifierRegex.exec(processedScript)) !== null) {
4068
- const name = match[1];
4069
- // Add if not excluded, not a function name, and not a declared variable
4070
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
4071
- variables.add(name);
3983
+ catch (error) {
3984
+ if (!(error instanceof ReferenceError)) {
3985
+ throw error;
3986
+ }
3987
+ /*
3988
+ Note: Parsing the error
3989
+ 🌟 Most devices:
3990
+ [PipelineUrlError: thing is not defined]
3991
+
3992
+ 🍏 iPhone`s Safari:
3993
+ [PipelineUrlError: Can't find variable: thing]
3994
+ */
3995
+ let variableName = undefined;
3996
+ if (error.message.startsWith(`Can't`)) {
3997
+ // 🍏 Case
3998
+ variableName = error.message.split(' ').pop();
3999
+ }
4000
+ else {
4001
+ // 🌟 Case
4002
+ variableName = error.message.split(' ').shift();
4003
+ }
4004
+ if (variableName === undefined) {
4005
+ throw error;
4006
+ }
4007
+ if (script.includes(variableName + '(')) {
4008
+ script = `const ${variableName} = ()=>'';` + script;
4009
+ }
4010
+ else {
4011
+ variables.add(variableName);
4012
+ script = `const ${variableName} = '';` + script;
4013
+ }
4072
4014
  }
4073
- }
4074
4015
  }
4075
4016
  catch (error) {
4076
4017
  if (!(error instanceof Error)) {
4077
4018
  throw error;
4078
4019
  }
4079
- throw new ParseError(spaceTrim__default["default"]((block) => `
4020
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
4080
4021
  Can not extract variables from the script
4081
4022
  ${block(error.stack || error.message)}
4082
4023
 
@@ -4089,7 +4030,7 @@
4089
4030
  The script:
4090
4031
 
4091
4032
  \`\`\`javascript
4092
- ${block(script)}
4033
+ ${block(originalScript)}
4093
4034
  \`\`\`
4094
4035
  `));
4095
4036
  }
@@ -4110,19 +4051,24 @@
4110
4051
  function extractParameterNamesFromTask(task) {
4111
4052
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
4112
4053
  const parameterNames = new Set();
4054
+ let contentParameters;
4055
+ if (taskType !== 'SCRIPT_TASK') {
4056
+ contentParameters = extractParameterNames(content);
4057
+ }
4058
+ else {
4059
+ // TODO: What if script is not javascript?
4060
+ // const { contentLanguage } = task;
4061
+ // if (contentLanguage !== 'javascript') {
4062
+ contentParameters = extractVariablesFromJavascript(content);
4063
+ }
4113
4064
  for (const parameterName of [
4114
4065
  ...extractParameterNames(title),
4115
4066
  ...extractParameterNames(description || ''),
4116
- ...extractParameterNames(content),
4067
+ ...contentParameters,
4117
4068
  ...extractParameterNames(preparedContent || ''),
4118
4069
  ]) {
4119
4070
  parameterNames.add(parameterName);
4120
4071
  }
4121
- if (taskType === 'SCRIPT_TASK') {
4122
- for (const parameterName of extractVariablesFromScript(content)) {
4123
- parameterNames.add(parameterName);
4124
- }
4125
- }
4126
4072
  for (const jokerName of jokerParameterNames || []) {
4127
4073
  parameterNames.add(jokerName);
4128
4074
  }