@promptbook/remote-client 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
@@ -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.30';
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
  *
@@ -4730,6 +4737,83 @@ function titleToName(value) {
4730
4737
  return value;
4731
4738
  }
4732
4739
 
4740
+ /**
4741
+ * Parses the given script and returns the list of all used variables that are not defined in the script
4742
+ *
4743
+ * @param script from which to extract the variables
4744
+ * @returns the list of variable names
4745
+ * @throws {ParseError} if the script is invalid
4746
+ * @public exported from `@promptbook/execute-javascript`
4747
+ */
4748
+ function extractVariablesFromJavascript(script) {
4749
+ const variables = new Set();
4750
+ const originalScript = script;
4751
+ script = `(()=>{${script}})()`;
4752
+ try {
4753
+ for (let i = 0; i < LOOP_LIMIT; i++)
4754
+ try {
4755
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
4756
+ }
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
+ }
4788
+ }
4789
+ }
4790
+ catch (error) {
4791
+ if (!(error instanceof Error)) {
4792
+ throw error;
4793
+ }
4794
+ throw new ParseError(spaceTrim((block) => `
4795
+ Can not extract variables from the script
4796
+ ${block(error.stack || error.message)}
4797
+
4798
+ Found variables:
4799
+ ${Array.from(variables)
4800
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
4801
+ .join('\n')}
4802
+
4803
+
4804
+ The script:
4805
+
4806
+ \`\`\`javascript
4807
+ ${block(originalScript)}
4808
+ \`\`\`
4809
+ `));
4810
+ }
4811
+ return variables;
4812
+ }
4813
+ /**
4814
+ * TODO: [🔣] Support for multiple languages - python, java,...
4815
+ */
4816
+
4733
4817
  /**
4734
4818
  * Parses the task and returns the list of all parameter names
4735
4819
  *
@@ -4756,24 +4840,26 @@ function extractParameterNames(template) {
4756
4840
  * @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
4757
4841
  */
4758
4842
  function extractParameterNamesFromTask(task) {
4759
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
4843
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
4760
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
+ }
4761
4855
  for (const parameterName of [
4762
4856
  ...extractParameterNames(title),
4763
4857
  ...extractParameterNames(description || ''),
4764
- ...extractParameterNames(content),
4858
+ ...contentParameters,
4765
4859
  ...extractParameterNames(preparedContent || ''),
4766
4860
  ]) {
4767
4861
  parameterNames.add(parameterName);
4768
4862
  }
4769
- /*/
4770
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
4771
- if (taskType === 'SCRIPT_TASK') {
4772
- for (const parameterName of extractVariablesFromScript(content)) {
4773
- parameterNames.add(parameterName);
4774
- }
4775
- }
4776
- /**/
4777
4863
  for (const jokerName of jokerParameterNames || []) {
4778
4864
  parameterNames.add(jokerName);
4779
4865
  }