@promptbook/core 0.86.22 → 0.86.30
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 +5 -138
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/utils.index.d.ts +0 -2
- package/package.json +1 -1
- package/umd/index.umd.js +5 -138
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/conversion/utils/extractVariablesFromScript.d.ts +0 -14
- package/esm/typings/src/conversion/utils/extractVariablesFromScript.test.d.ts +0 -1
- package/esm/typings/src/scripting/javascript/JavascriptEvalExecutionTools.test.d.ts +0 -4
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
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
|
|
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,142 +2325,6 @@ function addUsage(...usageItems) {
|
|
|
2325
2325
|
}, deepClone(ZERO_USAGE));
|
|
2326
2326
|
}
|
|
2327
2327
|
|
|
2328
|
-
/**
|
|
2329
|
-
* Extract all used variable names from ginen JavaScript/TypeScript script
|
|
2330
|
-
*
|
|
2331
|
-
* @param script JavaScript/TypeScript script
|
|
2332
|
-
* @returns Set of variable names
|
|
2333
|
-
* @throws {ParseError} if the script is invalid
|
|
2334
|
-
* @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
|
|
2335
|
-
*/
|
|
2336
|
-
function extractVariablesFromScript(script) {
|
|
2337
|
-
if (script.trim() === '') {
|
|
2338
|
-
return new Set();
|
|
2339
|
-
}
|
|
2340
|
-
const variables = new Set();
|
|
2341
|
-
// JS keywords and builtins to exclude
|
|
2342
|
-
const exclude = new Set([
|
|
2343
|
-
// Keywords
|
|
2344
|
-
'break',
|
|
2345
|
-
'case',
|
|
2346
|
-
'catch',
|
|
2347
|
-
'class',
|
|
2348
|
-
'const',
|
|
2349
|
-
'continue',
|
|
2350
|
-
'debugger',
|
|
2351
|
-
'default',
|
|
2352
|
-
'delete',
|
|
2353
|
-
'do',
|
|
2354
|
-
'else',
|
|
2355
|
-
'export',
|
|
2356
|
-
'extends',
|
|
2357
|
-
'false',
|
|
2358
|
-
'finally',
|
|
2359
|
-
'for',
|
|
2360
|
-
'function',
|
|
2361
|
-
'if',
|
|
2362
|
-
'import',
|
|
2363
|
-
'in',
|
|
2364
|
-
'instanceof',
|
|
2365
|
-
'let',
|
|
2366
|
-
'new',
|
|
2367
|
-
'null',
|
|
2368
|
-
'return',
|
|
2369
|
-
'super',
|
|
2370
|
-
'switch',
|
|
2371
|
-
'this',
|
|
2372
|
-
'throw',
|
|
2373
|
-
'true',
|
|
2374
|
-
'try',
|
|
2375
|
-
'typeof',
|
|
2376
|
-
'var',
|
|
2377
|
-
'void',
|
|
2378
|
-
'while',
|
|
2379
|
-
'with',
|
|
2380
|
-
'yield',
|
|
2381
|
-
// Common globals
|
|
2382
|
-
'console',
|
|
2383
|
-
'JSON',
|
|
2384
|
-
'Error',
|
|
2385
|
-
// Typescript types
|
|
2386
|
-
'string',
|
|
2387
|
-
'number',
|
|
2388
|
-
'boolean',
|
|
2389
|
-
'object',
|
|
2390
|
-
'symbol',
|
|
2391
|
-
// Common methods on built-in objects
|
|
2392
|
-
'test',
|
|
2393
|
-
'match',
|
|
2394
|
-
'exec',
|
|
2395
|
-
'replace',
|
|
2396
|
-
'search',
|
|
2397
|
-
'split',
|
|
2398
|
-
]);
|
|
2399
|
-
try {
|
|
2400
|
-
// Note: Extract variables from template literals like ${variable}
|
|
2401
|
-
const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
|
|
2402
|
-
let match;
|
|
2403
|
-
while ((match = templateRegex.exec(script)) !== null) {
|
|
2404
|
-
const varName = match[1];
|
|
2405
|
-
if (!exclude.has(varName)) {
|
|
2406
|
-
variables.add(varName);
|
|
2407
|
-
}
|
|
2408
|
-
}
|
|
2409
|
-
// Note: Process the script to handle normal variable usage
|
|
2410
|
-
const processedScript = script
|
|
2411
|
-
.replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
|
|
2412
|
-
.replace(/"(?:\\.|[^"\\])*"/g, '""')
|
|
2413
|
-
.replace(/`(?:\\.|[^`\\])*`/g, '``')
|
|
2414
|
-
.replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
|
|
2415
|
-
// Note: Find identifiers in function arguments
|
|
2416
|
-
const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
|
|
2417
|
-
const funcNames = new Set();
|
|
2418
|
-
while ((match = funcArgRegex.exec(processedScript)) !== null) {
|
|
2419
|
-
funcNames.add(match[1]);
|
|
2420
|
-
}
|
|
2421
|
-
// Find variable declarations to exclude them
|
|
2422
|
-
const declaredVars = new Set();
|
|
2423
|
-
const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
|
|
2424
|
-
while ((match = declRegex.exec(processedScript)) !== null) {
|
|
2425
|
-
declaredVars.add(match[2]);
|
|
2426
|
-
}
|
|
2427
|
-
// Note: Find identifiers in the script
|
|
2428
|
-
const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
|
|
2429
|
-
while ((match = identifierRegex.exec(processedScript)) !== null) {
|
|
2430
|
-
const name = match[1];
|
|
2431
|
-
// Add if not excluded, not a function name, and not a declared variable
|
|
2432
|
-
if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
|
|
2433
|
-
variables.add(name);
|
|
2434
|
-
}
|
|
2435
|
-
}
|
|
2436
|
-
}
|
|
2437
|
-
catch (error) {
|
|
2438
|
-
if (!(error instanceof Error)) {
|
|
2439
|
-
throw error;
|
|
2440
|
-
}
|
|
2441
|
-
throw new ParseError(spaceTrim((block) => `
|
|
2442
|
-
Can not extract variables from the script
|
|
2443
|
-
${block(error.stack || error.message)}
|
|
2444
|
-
|
|
2445
|
-
Found variables:
|
|
2446
|
-
${Array.from(variables)
|
|
2447
|
-
.map((variableName, i) => `${i + 1}) ${variableName}`)
|
|
2448
|
-
.join('\n')}
|
|
2449
|
-
|
|
2450
|
-
|
|
2451
|
-
The script:
|
|
2452
|
-
|
|
2453
|
-
\`\`\`javascript
|
|
2454
|
-
${block(script)}
|
|
2455
|
-
\`\`\`
|
|
2456
|
-
`));
|
|
2457
|
-
}
|
|
2458
|
-
return variables;
|
|
2459
|
-
}
|
|
2460
|
-
/**
|
|
2461
|
-
* TODO: [🔣] Support for multiple languages - python, java,...
|
|
2462
|
-
*/
|
|
2463
|
-
|
|
2464
2328
|
/**
|
|
2465
2329
|
* Parses the task and returns the set of all used parameters
|
|
2466
2330
|
*
|
|
@@ -2470,7 +2334,7 @@ function extractVariablesFromScript(script) {
|
|
|
2470
2334
|
* @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
|
|
2471
2335
|
*/
|
|
2472
2336
|
function extractParameterNamesFromTask(task) {
|
|
2473
|
-
const { title, description, taskType
|
|
2337
|
+
const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
|
|
2474
2338
|
const parameterNames = new Set();
|
|
2475
2339
|
for (const parameterName of [
|
|
2476
2340
|
...extractParameterNames(title),
|
|
@@ -2480,11 +2344,14 @@ function extractParameterNamesFromTask(task) {
|
|
|
2480
2344
|
]) {
|
|
2481
2345
|
parameterNames.add(parameterName);
|
|
2482
2346
|
}
|
|
2347
|
+
/*/
|
|
2348
|
+
// TODO: [🙊] Fix `extractVariablesFromScript` or delete
|
|
2483
2349
|
if (taskType === 'SCRIPT_TASK') {
|
|
2484
2350
|
for (const parameterName of extractVariablesFromScript(content)) {
|
|
2485
2351
|
parameterNames.add(parameterName);
|
|
2486
2352
|
}
|
|
2487
2353
|
}
|
|
2354
|
+
/**/
|
|
2488
2355
|
for (const jokerName of jokerParameterNames || []) {
|
|
2489
2356
|
parameterNames.add(jokerName);
|
|
2490
2357
|
}
|