@promptbook/core 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
@@ -27,7 +27,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
27
27
  * @generated
28
28
  * @see https://github.com/webgptorg/promptbook
29
29
  */
30
- const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
30
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
31
31
  /**
32
32
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
33
33
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2325,6 +2325,83 @@ function addUsage(...usageItems) {
2325
2325
  }, deepClone(ZERO_USAGE));
2326
2326
  }
2327
2327
 
2328
+ /**
2329
+ * Parses the given script and returns the list of all used variables that are not defined in the script
2330
+ *
2331
+ * @param script from which to extract the variables
2332
+ * @returns the list of variable names
2333
+ * @throws {ParseError} if the script is invalid
2334
+ * @public exported from `@promptbook/execute-javascript`
2335
+ */
2336
+ function extractVariablesFromJavascript(script) {
2337
+ const variables = new Set();
2338
+ const originalScript = script;
2339
+ script = `(()=>{${script}})()`;
2340
+ try {
2341
+ for (let i = 0; i < LOOP_LIMIT; i++)
2342
+ try {
2343
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
2344
+ }
2345
+ catch (error) {
2346
+ if (!(error instanceof ReferenceError)) {
2347
+ throw error;
2348
+ }
2349
+ /*
2350
+ Note: Parsing the error
2351
+ 🌟 Most devices:
2352
+ [PipelineUrlError: thing is not defined]
2353
+
2354
+ 🍏 iPhone`s Safari:
2355
+ [PipelineUrlError: Can't find variable: thing]
2356
+ */
2357
+ let variableName = undefined;
2358
+ if (error.message.startsWith(`Can't`)) {
2359
+ // 🍏 Case
2360
+ variableName = error.message.split(' ').pop();
2361
+ }
2362
+ else {
2363
+ // 🌟 Case
2364
+ variableName = error.message.split(' ').shift();
2365
+ }
2366
+ if (variableName === undefined) {
2367
+ throw error;
2368
+ }
2369
+ if (script.includes(variableName + '(')) {
2370
+ script = `const ${variableName} = ()=>'';` + script;
2371
+ }
2372
+ else {
2373
+ variables.add(variableName);
2374
+ script = `const ${variableName} = '';` + script;
2375
+ }
2376
+ }
2377
+ }
2378
+ catch (error) {
2379
+ if (!(error instanceof Error)) {
2380
+ throw error;
2381
+ }
2382
+ throw new ParseError(spaceTrim$1((block) => `
2383
+ Can not extract variables from the script
2384
+ ${block(error.stack || error.message)}
2385
+
2386
+ Found variables:
2387
+ ${Array.from(variables)
2388
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
2389
+ .join('\n')}
2390
+
2391
+
2392
+ The script:
2393
+
2394
+ \`\`\`javascript
2395
+ ${block(originalScript)}
2396
+ \`\`\`
2397
+ `));
2398
+ }
2399
+ return variables;
2400
+ }
2401
+ /**
2402
+ * TODO: [🔣] Support for multiple languages - python, java,...
2403
+ */
2404
+
2328
2405
  /**
2329
2406
  * Parses the task and returns the set of all used parameters
2330
2407
  *
@@ -2334,24 +2411,26 @@ function addUsage(...usageItems) {
2334
2411
  * @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
2335
2412
  */
2336
2413
  function extractParameterNamesFromTask(task) {
2337
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
2414
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
2338
2415
  const parameterNames = new Set();
2416
+ let contentParameters;
2417
+ if (taskType !== 'SCRIPT_TASK') {
2418
+ contentParameters = extractParameterNames(content);
2419
+ }
2420
+ else {
2421
+ // TODO: What if script is not javascript?
2422
+ // const { contentLanguage } = task;
2423
+ // if (contentLanguage !== 'javascript') {
2424
+ contentParameters = extractVariablesFromJavascript(content);
2425
+ }
2339
2426
  for (const parameterName of [
2340
2427
  ...extractParameterNames(title),
2341
2428
  ...extractParameterNames(description || ''),
2342
- ...extractParameterNames(content),
2429
+ ...contentParameters,
2343
2430
  ...extractParameterNames(preparedContent || ''),
2344
2431
  ]) {
2345
2432
  parameterNames.add(parameterName);
2346
2433
  }
2347
- /*/
2348
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
2349
- if (taskType === 'SCRIPT_TASK') {
2350
- for (const parameterName of extractVariablesFromScript(content)) {
2351
- parameterNames.add(parameterName);
2352
- }
2353
- }
2354
- /**/
2355
2434
  for (const jokerName of jokerParameterNames || []) {
2356
2435
  parameterNames.add(jokerName);
2357
2436
  }