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