@promptbook/cli 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.
@@ -2,7 +2,9 @@ import { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION } from '../version';
2
2
  import { JavascriptEvalExecutionTools } from '../scripting/javascript/JavascriptEvalExecutionTools';
3
3
  import { JavascriptExecutionTools } from '../scripting/javascript/JavascriptExecutionTools';
4
4
  import { POSTPROCESSING_FUNCTIONS } from '../scripting/javascript/postprocessing-functions';
5
+ import { extractVariablesFromJavascript } from '../scripting/javascript/utils/extractVariablesFromJavascript';
5
6
  export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
6
7
  export { JavascriptEvalExecutionTools };
7
8
  export { JavascriptExecutionTools };
8
9
  export { POSTPROCESSING_FUNCTIONS };
10
+ export { extractVariablesFromJavascript };
@@ -0,0 +1,4 @@
1
+ export {};
2
+ /**
3
+ * TODO: Make shared test between JavascriptEvalExecutionTools and JavascriptExecutionTools to test the same functionality when implemented via vm2
4
+ */
@@ -0,0 +1,14 @@
1
+ import type { string_javascript } from '../../../types/typeAliases';
2
+ import type { string_javascript_name } from '../../../types/typeAliases';
3
+ /**
4
+ * Parses the given script and returns the list of all used variables that are not defined in the script
5
+ *
6
+ * @param script from which to extract the variables
7
+ * @returns the list of variable names
8
+ * @throws {ParseError} if the script is invalid
9
+ * @public exported from `@promptbook/execute-javascript`
10
+ */
11
+ export declare function extractVariablesFromJavascript(script: string_javascript): Set<string_javascript_name>;
12
+ /**
13
+ * TODO: [🔣] Support for multiple languages - python, java,...
14
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/cli",
3
- "version": "0.86.30",
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,
package/umd/index.umd.js CHANGED
@@ -54,7 +54,7 @@
54
54
  * @generated
55
55
  * @see https://github.com/webgptorg/promptbook
56
56
  */
57
- const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
57
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
58
58
  /**
59
59
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
60
60
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -4560,7 +4560,6 @@
4560
4560
  `const ${functionName} = customFunctions.${functionName};`)
4561
4561
  .join('\n');
4562
4562
  script = templateParameters(script, parameters);
4563
- // <- Note: [🙊]
4564
4563
  const statementToEvaluate = spaceTrim__default["default"]((block) => `
4565
4564
 
4566
4565
  // Build-in functions:
@@ -5460,6 +5459,83 @@
5460
5459
  * TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function
5461
5460
  */
5462
5461
 
5462
+ /**
5463
+ * Parses the given script and returns the list of all used variables that are not defined in the script
5464
+ *
5465
+ * @param script from which to extract the variables
5466
+ * @returns the list of variable names
5467
+ * @throws {ParseError} if the script is invalid
5468
+ * @public exported from `@promptbook/execute-javascript`
5469
+ */
5470
+ function extractVariablesFromJavascript(script) {
5471
+ const variables = new Set();
5472
+ const originalScript = script;
5473
+ script = `(()=>{${script}})()`;
5474
+ try {
5475
+ for (let i = 0; i < LOOP_LIMIT; i++)
5476
+ try {
5477
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
5478
+ }
5479
+ catch (error) {
5480
+ if (!(error instanceof ReferenceError)) {
5481
+ throw error;
5482
+ }
5483
+ /*
5484
+ Note: Parsing the error
5485
+ 🌟 Most devices:
5486
+ [PipelineUrlError: thing is not defined]
5487
+
5488
+ 🍏 iPhone`s Safari:
5489
+ [PipelineUrlError: Can't find variable: thing]
5490
+ */
5491
+ let variableName = undefined;
5492
+ if (error.message.startsWith(`Can't`)) {
5493
+ // 🍏 Case
5494
+ variableName = error.message.split(' ').pop();
5495
+ }
5496
+ else {
5497
+ // 🌟 Case
5498
+ variableName = error.message.split(' ').shift();
5499
+ }
5500
+ if (variableName === undefined) {
5501
+ throw error;
5502
+ }
5503
+ if (script.includes(variableName + '(')) {
5504
+ script = `const ${variableName} = ()=>'';` + script;
5505
+ }
5506
+ else {
5507
+ variables.add(variableName);
5508
+ script = `const ${variableName} = '';` + script;
5509
+ }
5510
+ }
5511
+ }
5512
+ catch (error) {
5513
+ if (!(error instanceof Error)) {
5514
+ throw error;
5515
+ }
5516
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
5517
+ Can not extract variables from the script
5518
+ ${block(error.stack || error.message)}
5519
+
5520
+ Found variables:
5521
+ ${Array.from(variables)
5522
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
5523
+ .join('\n')}
5524
+
5525
+
5526
+ The script:
5527
+
5528
+ \`\`\`javascript
5529
+ ${block(originalScript)}
5530
+ \`\`\`
5531
+ `));
5532
+ }
5533
+ return variables;
5534
+ }
5535
+ /**
5536
+ * TODO: [🔣] Support for multiple languages - python, java,...
5537
+ */
5538
+
5463
5539
  /**
5464
5540
  * Parses the task and returns the set of all used parameters
5465
5541
  *
@@ -5469,24 +5545,26 @@
5469
5545
  * @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
5470
5546
  */
5471
5547
  function extractParameterNamesFromTask(task) {
5472
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
5548
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
5473
5549
  const parameterNames = new Set();
5550
+ let contentParameters;
5551
+ if (taskType !== 'SCRIPT_TASK') {
5552
+ contentParameters = extractParameterNames(content);
5553
+ }
5554
+ else {
5555
+ // TODO: What if script is not javascript?
5556
+ // const { contentLanguage } = task;
5557
+ // if (contentLanguage !== 'javascript') {
5558
+ contentParameters = extractVariablesFromJavascript(content);
5559
+ }
5474
5560
  for (const parameterName of [
5475
5561
  ...extractParameterNames(title),
5476
5562
  ...extractParameterNames(description || ''),
5477
- ...extractParameterNames(content),
5563
+ ...contentParameters,
5478
5564
  ...extractParameterNames(preparedContent || ''),
5479
5565
  ]) {
5480
5566
  parameterNames.add(parameterName);
5481
5567
  }
5482
- /*/
5483
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
5484
- if (taskType === 'SCRIPT_TASK') {
5485
- for (const parameterName of extractVariablesFromScript(content)) {
5486
- parameterNames.add(parameterName);
5487
- }
5488
- }
5489
- /**/
5490
5568
  for (const jokerName of jokerParameterNames || []) {
5491
5569
  parameterNames.add(jokerName);
5492
5570
  }