@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.
- package/esm/index.es.js +90 -12
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/execute-javascript.index.d.ts +2 -0
- package/esm/typings/src/scripting/javascript/JavascriptEvalExecutionTools.test.d.ts +4 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.d.ts +14 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.test.d.ts +1 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromScript.test.d.ts +1 -0
- package/package.json +1 -1
- package/umd/index.umd.js +90 -12
- package/umd/index.umd.js.map +1 -1
package/esm/index.es.js
CHANGED
|
@@ -44,7 +44,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
|
|
|
44
44
|
* @generated
|
|
45
45
|
* @see https://github.com/webgptorg/promptbook
|
|
46
46
|
*/
|
|
47
|
-
const PROMPTBOOK_ENGINE_VERSION = '0.86.
|
|
47
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
|
|
48
48
|
/**
|
|
49
49
|
* TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
|
|
50
50
|
* Note: [💞] Ignore a discrepancy between file name and entity name
|
|
@@ -4550,7 +4550,6 @@ class JavascriptEvalExecutionTools {
|
|
|
4550
4550
|
`const ${functionName} = customFunctions.${functionName};`)
|
|
4551
4551
|
.join('\n');
|
|
4552
4552
|
script = templateParameters(script, parameters);
|
|
4553
|
-
// <- Note: [🙊]
|
|
4554
4553
|
const statementToEvaluate = spaceTrim((block) => `
|
|
4555
4554
|
|
|
4556
4555
|
// Build-in functions:
|
|
@@ -5450,6 +5449,83 @@ function createTask(options) {
|
|
|
5450
5449
|
* TODO: [🐚] Split into more files and make `PrepareTask` & `RemoteTask` + split the function
|
|
5451
5450
|
*/
|
|
5452
5451
|
|
|
5452
|
+
/**
|
|
5453
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
5454
|
+
*
|
|
5455
|
+
* @param script from which to extract the variables
|
|
5456
|
+
* @returns the list of variable names
|
|
5457
|
+
* @throws {ParseError} if the script is invalid
|
|
5458
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
5459
|
+
*/
|
|
5460
|
+
function extractVariablesFromJavascript(script) {
|
|
5461
|
+
const variables = new Set();
|
|
5462
|
+
const originalScript = script;
|
|
5463
|
+
script = `(()=>{${script}})()`;
|
|
5464
|
+
try {
|
|
5465
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
5466
|
+
try {
|
|
5467
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
5468
|
+
}
|
|
5469
|
+
catch (error) {
|
|
5470
|
+
if (!(error instanceof ReferenceError)) {
|
|
5471
|
+
throw error;
|
|
5472
|
+
}
|
|
5473
|
+
/*
|
|
5474
|
+
Note: Parsing the error
|
|
5475
|
+
🌟 Most devices:
|
|
5476
|
+
[PipelineUrlError: thing is not defined]
|
|
5477
|
+
|
|
5478
|
+
🍏 iPhone`s Safari:
|
|
5479
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
5480
|
+
*/
|
|
5481
|
+
let variableName = undefined;
|
|
5482
|
+
if (error.message.startsWith(`Can't`)) {
|
|
5483
|
+
// 🍏 Case
|
|
5484
|
+
variableName = error.message.split(' ').pop();
|
|
5485
|
+
}
|
|
5486
|
+
else {
|
|
5487
|
+
// 🌟 Case
|
|
5488
|
+
variableName = error.message.split(' ').shift();
|
|
5489
|
+
}
|
|
5490
|
+
if (variableName === undefined) {
|
|
5491
|
+
throw error;
|
|
5492
|
+
}
|
|
5493
|
+
if (script.includes(variableName + '(')) {
|
|
5494
|
+
script = `const ${variableName} = ()=>'';` + script;
|
|
5495
|
+
}
|
|
5496
|
+
else {
|
|
5497
|
+
variables.add(variableName);
|
|
5498
|
+
script = `const ${variableName} = '';` + script;
|
|
5499
|
+
}
|
|
5500
|
+
}
|
|
5501
|
+
}
|
|
5502
|
+
catch (error) {
|
|
5503
|
+
if (!(error instanceof Error)) {
|
|
5504
|
+
throw error;
|
|
5505
|
+
}
|
|
5506
|
+
throw new ParseError(spaceTrim$1((block) => `
|
|
5507
|
+
Can not extract variables from the script
|
|
5508
|
+
${block(error.stack || error.message)}
|
|
5509
|
+
|
|
5510
|
+
Found variables:
|
|
5511
|
+
${Array.from(variables)
|
|
5512
|
+
.map((variableName, i) => `${i + 1}) ${variableName}`)
|
|
5513
|
+
.join('\n')}
|
|
5514
|
+
|
|
5515
|
+
|
|
5516
|
+
The script:
|
|
5517
|
+
|
|
5518
|
+
\`\`\`javascript
|
|
5519
|
+
${block(originalScript)}
|
|
5520
|
+
\`\`\`
|
|
5521
|
+
`));
|
|
5522
|
+
}
|
|
5523
|
+
return variables;
|
|
5524
|
+
}
|
|
5525
|
+
/**
|
|
5526
|
+
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
5527
|
+
*/
|
|
5528
|
+
|
|
5453
5529
|
/**
|
|
5454
5530
|
* Parses the task and returns the set of all used parameters
|
|
5455
5531
|
*
|
|
@@ -5459,24 +5535,26 @@ function createTask(options) {
|
|
|
5459
5535
|
* @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
|
|
5460
5536
|
*/
|
|
5461
5537
|
function extractParameterNamesFromTask(task) {
|
|
5462
|
-
const { title, description,
|
|
5538
|
+
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
5463
5539
|
const parameterNames = new Set();
|
|
5540
|
+
let contentParameters;
|
|
5541
|
+
if (taskType !== 'SCRIPT_TASK') {
|
|
5542
|
+
contentParameters = extractParameterNames(content);
|
|
5543
|
+
}
|
|
5544
|
+
else {
|
|
5545
|
+
// TODO: What if script is not javascript?
|
|
5546
|
+
// const { contentLanguage } = task;
|
|
5547
|
+
// if (contentLanguage !== 'javascript') {
|
|
5548
|
+
contentParameters = extractVariablesFromJavascript(content);
|
|
5549
|
+
}
|
|
5464
5550
|
for (const parameterName of [
|
|
5465
5551
|
...extractParameterNames(title),
|
|
5466
5552
|
...extractParameterNames(description || ''),
|
|
5467
|
-
...
|
|
5553
|
+
...contentParameters,
|
|
5468
5554
|
...extractParameterNames(preparedContent || ''),
|
|
5469
5555
|
]) {
|
|
5470
5556
|
parameterNames.add(parameterName);
|
|
5471
5557
|
}
|
|
5472
|
-
/*/
|
|
5473
|
-
// TODO: [🙊] Fix `extractVariablesFromScript` or delete
|
|
5474
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
5475
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
5476
|
-
parameterNames.add(parameterName);
|
|
5477
|
-
}
|
|
5478
|
-
}
|
|
5479
|
-
/**/
|
|
5480
5558
|
for (const jokerName of jokerParameterNames || []) {
|
|
5481
5559
|
parameterNames.add(jokerName);
|
|
5482
5560
|
}
|