@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.
package/esm/index.es.js CHANGED
@@ -31,7 +31,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
31
31
  * @generated
32
32
  * @see https://github.com/webgptorg/promptbook
33
33
  */
34
- const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
34
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
35
35
  /**
36
36
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
37
37
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -3966,6 +3966,83 @@ function valueToString(value) {
3966
3966
  }
3967
3967
  }
3968
3968
 
3969
+ /**
3970
+ * Parses the given script and returns the list of all used variables that are not defined in the script
3971
+ *
3972
+ * @param script from which to extract the variables
3973
+ * @returns the list of variable names
3974
+ * @throws {ParseError} if the script is invalid
3975
+ * @public exported from `@promptbook/execute-javascript`
3976
+ */
3977
+ function extractVariablesFromJavascript(script) {
3978
+ const variables = new Set();
3979
+ const originalScript = script;
3980
+ script = `(()=>{${script}})()`;
3981
+ try {
3982
+ for (let i = 0; i < LOOP_LIMIT; i++)
3983
+ try {
3984
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
3985
+ }
3986
+ catch (error) {
3987
+ if (!(error instanceof ReferenceError)) {
3988
+ throw error;
3989
+ }
3990
+ /*
3991
+ Note: Parsing the error
3992
+ 🌟 Most devices:
3993
+ [PipelineUrlError: thing is not defined]
3994
+
3995
+ 🍏 iPhone`s Safari:
3996
+ [PipelineUrlError: Can't find variable: thing]
3997
+ */
3998
+ let variableName = undefined;
3999
+ if (error.message.startsWith(`Can't`)) {
4000
+ // 🍏 Case
4001
+ variableName = error.message.split(' ').pop();
4002
+ }
4003
+ else {
4004
+ // 🌟 Case
4005
+ variableName = error.message.split(' ').shift();
4006
+ }
4007
+ if (variableName === undefined) {
4008
+ throw error;
4009
+ }
4010
+ if (script.includes(variableName + '(')) {
4011
+ script = `const ${variableName} = ()=>'';` + script;
4012
+ }
4013
+ else {
4014
+ variables.add(variableName);
4015
+ script = `const ${variableName} = '';` + script;
4016
+ }
4017
+ }
4018
+ }
4019
+ catch (error) {
4020
+ if (!(error instanceof Error)) {
4021
+ throw error;
4022
+ }
4023
+ throw new ParseError(spaceTrim((block) => `
4024
+ Can not extract variables from the script
4025
+ ${block(error.stack || error.message)}
4026
+
4027
+ Found variables:
4028
+ ${Array.from(variables)
4029
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
4030
+ .join('\n')}
4031
+
4032
+
4033
+ The script:
4034
+
4035
+ \`\`\`javascript
4036
+ ${block(originalScript)}
4037
+ \`\`\`
4038
+ `));
4039
+ }
4040
+ return variables;
4041
+ }
4042
+ /**
4043
+ * TODO: [🔣] Support for multiple languages - python, java,...
4044
+ */
4045
+
3969
4046
  /**
3970
4047
  * Parses the task and returns the set of all used parameters
3971
4048
  *
@@ -3975,24 +4052,26 @@ function valueToString(value) {
3975
4052
  * @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
3976
4053
  */
3977
4054
  function extractParameterNamesFromTask(task) {
3978
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
4055
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
3979
4056
  const parameterNames = new Set();
4057
+ let contentParameters;
4058
+ if (taskType !== 'SCRIPT_TASK') {
4059
+ contentParameters = extractParameterNames(content);
4060
+ }
4061
+ else {
4062
+ // TODO: What if script is not javascript?
4063
+ // const { contentLanguage } = task;
4064
+ // if (contentLanguage !== 'javascript') {
4065
+ contentParameters = extractVariablesFromJavascript(content);
4066
+ }
3980
4067
  for (const parameterName of [
3981
4068
  ...extractParameterNames(title),
3982
4069
  ...extractParameterNames(description || ''),
3983
- ...extractParameterNames(content),
4070
+ ...contentParameters,
3984
4071
  ...extractParameterNames(preparedContent || ''),
3985
4072
  ]) {
3986
4073
  parameterNames.add(parameterName);
3987
4074
  }
3988
- /*/
3989
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
3990
- if (taskType === 'SCRIPT_TASK') {
3991
- for (const parameterName of extractVariablesFromScript(content)) {
3992
- parameterNames.add(parameterName);
3993
- }
3994
- }
3995
- /**/
3996
4075
  for (const jokerName of jokerParameterNames || []) {
3997
4076
  parameterNames.add(jokerName);
3998
4077
  }