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