@promptbook/remote-client 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
@@ -19,7 +19,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
19
19
  * @generated
20
20
  * @see https://github.com/webgptorg/promptbook
21
21
  */
22
- const PROMPTBOOK_ENGINE_VERSION = '0.86.22';
22
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
23
23
  /**
24
24
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
25
25
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -290,6 +290,13 @@ const DEFAULT_TASK_TITLE = `Task`;
290
290
  * @public exported from `@promptbook/core`
291
291
  */
292
292
  const DEFAULT_BOOK_OUTPUT_PARAMETER_NAME = 'result';
293
+ // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
294
+ /**
295
+ * The maximum number of iterations for a loops
296
+ *
297
+ * @private within the repository - too low-level in comparison with other `MAX_...`
298
+ */
299
+ const LOOP_LIMIT = 1000;
293
300
  /**
294
301
  * Timeout for the connections in milliseconds
295
302
  *
@@ -4731,136 +4738,60 @@ function titleToName(value) {
4731
4738
  }
4732
4739
 
4733
4740
  /**
4734
- * Parses the task and returns the list of all parameter names
4741
+ * Parses the given script and returns the list of all used variables that are not defined in the script
4735
4742
  *
4736
- * @param template the string template with parameters in {curly} braces
4737
- * @returns the list of parameter names
4738
- * @public exported from `@promptbook/utils`
4739
- */
4740
- function extractParameterNames(template) {
4741
- const matches = template.matchAll(/{\w+}/g);
4742
- const parameterNames = new Set();
4743
- for (const match of matches) {
4744
- const parameterName = match[0].slice(1, -1);
4745
- parameterNames.add(parameterName);
4746
- }
4747
- return parameterNames;
4748
- }
4749
-
4750
- /**
4751
- * Extract all used variable names from ginen JavaScript/TypeScript script
4752
- *
4753
- * @param script JavaScript/TypeScript script
4754
- * @returns Set of variable names
4743
+ * @param script from which to extract the variables
4744
+ * @returns the list of variable names
4755
4745
  * @throws {ParseError} if the script is invalid
4756
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
4746
+ * @public exported from `@promptbook/execute-javascript`
4757
4747
  */
4758
- function extractVariablesFromScript(script) {
4759
- if (script.trim() === '') {
4760
- return new Set();
4761
- }
4748
+ function extractVariablesFromJavascript(script) {
4762
4749
  const variables = new Set();
4763
- // JS keywords and builtins to exclude
4764
- const exclude = new Set([
4765
- // Keywords
4766
- 'break',
4767
- 'case',
4768
- 'catch',
4769
- 'class',
4770
- 'const',
4771
- 'continue',
4772
- 'debugger',
4773
- 'default',
4774
- 'delete',
4775
- 'do',
4776
- 'else',
4777
- 'export',
4778
- 'extends',
4779
- 'false',
4780
- 'finally',
4781
- 'for',
4782
- 'function',
4783
- 'if',
4784
- 'import',
4785
- 'in',
4786
- 'instanceof',
4787
- 'let',
4788
- 'new',
4789
- 'null',
4790
- 'return',
4791
- 'super',
4792
- 'switch',
4793
- 'this',
4794
- 'throw',
4795
- 'true',
4796
- 'try',
4797
- 'typeof',
4798
- 'var',
4799
- 'void',
4800
- 'while',
4801
- 'with',
4802
- 'yield',
4803
- // Common globals
4804
- 'console',
4805
- 'JSON',
4806
- 'Error',
4807
- // Typescript types
4808
- 'string',
4809
- 'number',
4810
- 'boolean',
4811
- 'object',
4812
- 'symbol',
4813
- // Common methods on built-in objects
4814
- 'test',
4815
- 'match',
4816
- 'exec',
4817
- 'replace',
4818
- 'search',
4819
- 'split',
4820
- ]);
4750
+ const originalScript = script;
4751
+ script = `(()=>{${script}})()`;
4821
4752
  try {
4822
- // Note: Extract variables from template literals like ${variable}
4823
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
4824
- let match;
4825
- while ((match = templateRegex.exec(script)) !== null) {
4826
- const varName = match[1];
4827
- if (!exclude.has(varName)) {
4828
- variables.add(varName);
4753
+ for (let i = 0; i < LOOP_LIMIT; i++)
4754
+ try {
4755
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
4829
4756
  }
4830
- }
4831
- // Note: Process the script to handle normal variable usage
4832
- const processedScript = script
4833
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
4834
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
4835
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
4836
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
4837
- // Note: Find identifiers in function arguments
4838
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
4839
- const funcNames = new Set();
4840
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
4841
- funcNames.add(match[1]);
4842
- }
4843
- // Find variable declarations to exclude them
4844
- const declaredVars = new Set();
4845
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
4846
- while ((match = declRegex.exec(processedScript)) !== null) {
4847
- declaredVars.add(match[2]);
4848
- }
4849
- // Note: Find identifiers in the script
4850
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
4851
- while ((match = identifierRegex.exec(processedScript)) !== null) {
4852
- const name = match[1];
4853
- // Add if not excluded, not a function name, and not a declared variable
4854
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
4855
- variables.add(name);
4757
+ catch (error) {
4758
+ if (!(error instanceof ReferenceError)) {
4759
+ throw error;
4760
+ }
4761
+ /*
4762
+ Note: Parsing the error
4763
+ 🌟 Most devices:
4764
+ [PipelineUrlError: thing is not defined]
4765
+
4766
+ 🍏 iPhone`s Safari:
4767
+ [PipelineUrlError: Can't find variable: thing]
4768
+ */
4769
+ let variableName = undefined;
4770
+ if (error.message.startsWith(`Can't`)) {
4771
+ // 🍏 Case
4772
+ variableName = error.message.split(' ').pop();
4773
+ }
4774
+ else {
4775
+ // 🌟 Case
4776
+ variableName = error.message.split(' ').shift();
4777
+ }
4778
+ if (variableName === undefined) {
4779
+ throw error;
4780
+ }
4781
+ if (script.includes(variableName + '(')) {
4782
+ script = `const ${variableName} = ()=>'';` + script;
4783
+ }
4784
+ else {
4785
+ variables.add(variableName);
4786
+ script = `const ${variableName} = '';` + script;
4787
+ }
4856
4788
  }
4857
- }
4858
4789
  }
4859
4790
  catch (error) {
4860
4791
  if (!(error instanceof Error)) {
4861
4792
  throw error;
4862
4793
  }
4863
- throw new ParseError(spaceTrim$1((block) => `
4794
+ throw new ParseError(spaceTrim((block) => `
4864
4795
  Can not extract variables from the script
4865
4796
  ${block(error.stack || error.message)}
4866
4797
 
@@ -4873,7 +4804,7 @@ function extractVariablesFromScript(script) {
4873
4804
  The script:
4874
4805
 
4875
4806
  \`\`\`javascript
4876
- ${block(script)}
4807
+ ${block(originalScript)}
4877
4808
  \`\`\`
4878
4809
  `));
4879
4810
  }
@@ -4883,6 +4814,23 @@ function extractVariablesFromScript(script) {
4883
4814
  * TODO: [🔣] Support for multiple languages - python, java,...
4884
4815
  */
4885
4816
 
4817
+ /**
4818
+ * Parses the task and returns the list of all parameter names
4819
+ *
4820
+ * @param template the string template with parameters in {curly} braces
4821
+ * @returns the list of parameter names
4822
+ * @public exported from `@promptbook/utils`
4823
+ */
4824
+ function extractParameterNames(template) {
4825
+ const matches = template.matchAll(/{\w+}/g);
4826
+ const parameterNames = new Set();
4827
+ for (const match of matches) {
4828
+ const parameterName = match[0].slice(1, -1);
4829
+ parameterNames.add(parameterName);
4830
+ }
4831
+ return parameterNames;
4832
+ }
4833
+
4886
4834
  /**
4887
4835
  * Parses the task and returns the set of all used parameters
4888
4836
  *
@@ -4894,19 +4842,24 @@ function extractVariablesFromScript(script) {
4894
4842
  function extractParameterNamesFromTask(task) {
4895
4843
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
4896
4844
  const parameterNames = new Set();
4845
+ let contentParameters;
4846
+ if (taskType !== 'SCRIPT_TASK') {
4847
+ contentParameters = extractParameterNames(content);
4848
+ }
4849
+ else {
4850
+ // TODO: What if script is not javascript?
4851
+ // const { contentLanguage } = task;
4852
+ // if (contentLanguage !== 'javascript') {
4853
+ contentParameters = extractVariablesFromJavascript(content);
4854
+ }
4897
4855
  for (const parameterName of [
4898
4856
  ...extractParameterNames(title),
4899
4857
  ...extractParameterNames(description || ''),
4900
- ...extractParameterNames(content),
4858
+ ...contentParameters,
4901
4859
  ...extractParameterNames(preparedContent || ''),
4902
4860
  ]) {
4903
4861
  parameterNames.add(parameterName);
4904
4862
  }
4905
- if (taskType === 'SCRIPT_TASK') {
4906
- for (const parameterName of extractVariablesFromScript(content)) {
4907
- parameterNames.add(parameterName);
4908
- }
4909
- }
4910
4863
  for (const jokerName of jokerParameterNames || []) {
4911
4864
  parameterNames.add(jokerName);
4912
4865
  }