@promptbook/remote-server 0.86.30 → 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 };
@@ -0,0 +1,4 @@
1
+ export {};
2
+ /**
3
+ * TODO: Make shared test between JavascriptEvalExecutionTools and JavascriptExecutionTools to test the same functionality when implemented via vm2
4
+ */
@@ -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.30",
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.30"
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.30';
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
@@ -3963,6 +3963,83 @@
3963
3963
  }
3964
3964
  }
3965
3965
 
3966
+ /**
3967
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3968
+ *
3969
+ * @param script from which to extract the variables
3970
+ * @returns the list of variable names
3971
+ * @throws {ParseError} if the script is invalid
3972
+ * @public exported from `@promptbook/execute-javascript`
3973
+ */
3974
+ function extractVariablesFromJavascript(script) {
3975
+ const variables = new Set();
3976
+ const originalScript = script;
3977
+ script = `(()=>{${script}})()`;
3978
+ try {
3979
+ for (let i = 0; i < LOOP_LIMIT; i++)
3980
+ try {
3981
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3982
+ }
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
+ }
4014
+ }
4015
+ }
4016
+ catch (error) {
4017
+ if (!(error instanceof Error)) {
4018
+ throw error;
4019
+ }
4020
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
4021
+ Can not extract variables from the script
4022
+ ${block(error.stack || error.message)}
4023
+
4024
+ Found variables:
4025
+ ${Array.from(variables)
4026
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
4027
+ .join('\n')}
4028
+
4029
+
4030
+ The script:
4031
+
4032
+ \`\`\`javascript
4033
+ ${block(originalScript)}
4034
+ \`\`\`
4035
+ `));
4036
+ }
4037
+ return variables;
4038
+ }
4039
+ /**
4040
+ * TODO: [🔣] Support for multiple languages - python, java,...
4041
+ */
4042
+
3966
4043
  /**
3967
4044
  * Parses the task and returns the set of all used parameters
3968
4045
  *
@@ -3972,24 +4049,26 @@
3972
4049
  * @public exported from `@promptbook/core` <- Note: [👖] This utility is so tightly interconnected with the Promptbook that it is not exported as util but in core
3973
4050
  */
3974
4051
  function extractParameterNamesFromTask(task) {
3975
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
4052
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3976
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
+ }
3977
4064
  for (const parameterName of [
3978
4065
  ...extractParameterNames(title),
3979
4066
  ...extractParameterNames(description || ''),
3980
- ...extractParameterNames(content),
4067
+ ...contentParameters,
3981
4068
  ...extractParameterNames(preparedContent || ''),
3982
4069
  ]) {
3983
4070
  parameterNames.add(parameterName);
3984
4071
  }
3985
- /*/
3986
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
3987
- if (taskType === 'SCRIPT_TASK') {
3988
- for (const parameterName of extractVariablesFromScript(content)) {
3989
- parameterNames.add(parameterName);
3990
- }
3991
- }
3992
- /**/
3993
4072
  for (const jokerName of jokerParameterNames || []) {
3994
4073
  parameterNames.add(jokerName);
3995
4074
  }