@promptbook/core 0.24.0-0 → 0.24.0-2.0

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
@@ -975,8 +975,11 @@ function validatePromptTemplatePipelineJson(ptp) {
975
975
  * @see https://github.com/webgptorg/promptbook#prompt-template-pipeline
976
976
  */
977
977
  var PromptTemplatePipeline = /** @class */ (function () {
978
- function PromptTemplatePipeline(ptbkUrl, parameters, promptTemplates) {
978
+ function PromptTemplatePipeline(ptbkUrl, title, ptbkVersion, description, parameters, promptTemplates) {
979
979
  this.ptbkUrl = ptbkUrl;
980
+ this.title = title;
981
+ this.ptbkVersion = ptbkVersion;
982
+ this.description = description;
980
983
  this.parameters = parameters;
981
984
  this.promptTemplates = promptTemplates;
982
985
  if (promptTemplates.length === 0) {
@@ -1021,7 +1024,7 @@ var PromptTemplatePipeline = /** @class */ (function () {
1021
1024
  */
1022
1025
  PromptTemplatePipeline.fromJson = function (ptbkjson) {
1023
1026
  validatePromptTemplatePipelineJson(ptbkjson);
1024
- return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, Object.fromEntries(ptbkjson.parameters.map(function (parameter) { return [parameter.name, parameter]; })), ptbkjson.promptTemplates);
1027
+ return new PromptTemplatePipeline(ptbkjson.ptbkUrl ? new URL(ptbkjson.ptbkUrl) : null, ptbkjson.title, ptbkjson.ptbkVersion, ptbkjson.ptbkVersion, Object.fromEntries(ptbkjson.parameters.map(function (parameter) { return [parameter.name, parameter]; })), ptbkjson.promptTemplates);
1025
1028
  };
1026
1029
  Object.defineProperty(PromptTemplatePipeline.prototype, "entryPromptTemplate", {
1027
1030
  /**
@@ -1269,20 +1272,31 @@ function replaceParameters(template, parameters) {
1269
1272
  */
1270
1273
  function createPtpExecutor(options) {
1271
1274
  var _this = this;
1272
- var ptp = options.ptp, tools = options.tools;
1275
+ var ptp = options.ptp, tools = options.tools, settings = options.settings;
1273
1276
  var ptpExecutor = function (inputParameters, onProgress) { return __awaiter(_this, void 0, void 0, function () {
1274
- var parametersToPass, currentTemplate, executionReport, _loop_1;
1275
- return __generator(this, function (_a) {
1276
- switch (_a.label) {
1277
+ var parametersToPass, currentTemplate, executionReport, _loop_1, error_1;
1278
+ var _a;
1279
+ return __generator(this, function (_b) {
1280
+ switch (_b.label) {
1277
1281
  case 0:
1278
1282
  parametersToPass = inputParameters;
1279
1283
  currentTemplate = ptp.entryPromptTemplate;
1280
- executionReport = [];
1284
+ executionReport = {
1285
+ ptbkUrl: ((_a = ptp.ptbkUrl) === null || _a === void 0 ? void 0 : _a.href) || undefined,
1286
+ title: ptp.title || undefined,
1287
+ ptbkUsedVersion: PTBK_VERSION,
1288
+ ptbkRequestedVersion: ptp.ptbkVersion || undefined,
1289
+ description: ptp.description || undefined,
1290
+ promptExecutions: [],
1291
+ };
1292
+ _b.label = 1;
1293
+ case 1:
1294
+ _b.trys.push([1, 5, , 6]);
1281
1295
  _loop_1 = function () {
1282
- var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString, scriptExecutionErrors, isScriptExecutionSuccessful, _b, _c, _d, _e, _f, unit, _g, max, min, amount, _h, _j, scriptTools, error_1, e_1_1;
1283
- var e_2, _k, e_1, _l, _m;
1284
- return __generator(this, function (_o) {
1285
- switch (_o.label) {
1296
+ var resultingParameter, name_1, title, prompt_1, chatThread, completionResult, result, resultString, naturalExecutionError, scriptExecutionErrors, isScriptExecutionSuccessful, _c, attempt, _d, _e, _f, _g, unit, _h, max, min, amount, error_2, _j, _k, scriptTools, error_3, e_1_1;
1297
+ var e_2, _l, e_1, _m, _o;
1298
+ return __generator(this, function (_p) {
1299
+ switch (_p.label) {
1286
1300
  case 0:
1287
1301
  resultingParameter = ptp.getResultingParameter(currentTemplate.name);
1288
1302
  name_1 = "ptp-executor-frame-".concat(currentTemplate.name);
@@ -1298,28 +1312,30 @@ function createPtpExecutor(options) {
1298
1312
  parameterValue: null,
1299
1313
  })];
1300
1314
  case 1:
1301
- _o.sent();
1302
- _o.label = 2;
1315
+ _p.sent();
1316
+ _p.label = 2;
1303
1317
  case 2:
1304
1318
  prompt_1 = void 0;
1305
1319
  chatThread = void 0;
1306
1320
  completionResult = void 0;
1307
- result = void 0;
1321
+ result = null;
1308
1322
  resultString = null;
1323
+ naturalExecutionError = null;
1309
1324
  isScriptExecutionSuccessful = void 0;
1310
- _b = currentTemplate.executionType;
1311
- switch (_b) {
1325
+ _c = currentTemplate.executionType;
1326
+ switch (_c) {
1312
1327
  case 'SIMPLE_TEMPLATE': return [3 /*break*/, 3];
1313
1328
  case 'PROMPT_TEMPLATE': return [3 /*break*/, 4];
1314
- case 'SCRIPT': return [3 /*break*/, 11];
1315
- case 'PROMPT_DIALOG': return [3 /*break*/, 22];
1329
+ case 'SCRIPT': return [3 /*break*/, 18];
1330
+ case 'PROMPT_DIALOG': return [3 /*break*/, 29];
1316
1331
  }
1317
- return [3 /*break*/, 24];
1332
+ return [3 /*break*/, 31];
1318
1333
  case 3:
1319
1334
  resultString = replaceParameters(currentTemplate.content, parametersToPass);
1320
- return [3 /*break*/, 25];
1335
+ return [3 /*break*/, 32];
1321
1336
  case 4:
1322
1337
  prompt_1 = {
1338
+ title: currentTemplate.title,
1323
1339
  ptbkUrl: "".concat(ptp.ptbkUrl
1324
1340
  ? ptp.ptbkUrl.href
1325
1341
  : 'anonymous' /* <- [🧠] !!!! How to deal with anonymous PTPs, do here some auto-url like SHA-256 based ad-hoc identifier? */, "#").concat(currentTemplate.name),
@@ -1327,32 +1343,42 @@ function createPtpExecutor(options) {
1327
1343
  content: replaceParameters(currentTemplate.content, parametersToPass),
1328
1344
  modelRequirements: currentTemplate.modelRequirements,
1329
1345
  };
1330
- _c = currentTemplate.modelRequirements.modelVariant;
1331
- switch (_c) {
1332
- case 'CHAT': return [3 /*break*/, 5];
1333
- case 'COMPLETION': return [3 /*break*/, 7];
1334
- }
1335
- return [3 /*break*/, 9];
1336
- case 5: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1346
+ attempt = 0;
1347
+ _p.label = 5;
1348
+ case 5:
1349
+ if (!(attempt < settings.maxNaturalExecutionAttempts)) return [3 /*break*/, 17];
1350
+ result = null;
1351
+ resultString = null;
1352
+ naturalExecutionError = null;
1353
+ _p.label = 6;
1337
1354
  case 6:
1338
- chatThread = _o.sent();
1355
+ _p.trys.push([6, 13, 14, 15]);
1356
+ _d = currentTemplate.modelRequirements.modelVariant;
1357
+ switch (_d) {
1358
+ case 'CHAT': return [3 /*break*/, 7];
1359
+ case 'COMPLETION': return [3 /*break*/, 9];
1360
+ }
1361
+ return [3 /*break*/, 11];
1362
+ case 7: return [4 /*yield*/, tools.natural.gptChat(prompt_1)];
1363
+ case 8:
1364
+ chatThread = _p.sent();
1339
1365
  // TODO: [🍬] Destroy chatThread
1340
1366
  result = chatThread;
1341
1367
  resultString = chatThread.content;
1342
- return [3 /*break*/, 10];
1343
- case 7: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1344
- case 8:
1345
- completionResult = _o.sent();
1368
+ return [3 /*break*/, 12];
1369
+ case 9: return [4 /*yield*/, tools.natural.gptComplete(prompt_1)];
1370
+ case 10:
1371
+ completionResult = _p.sent();
1346
1372
  result = completionResult;
1347
1373
  resultString = completionResult.content;
1348
- return [3 /*break*/, 10];
1349
- case 9: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1350
- case 10:
1374
+ return [3 /*break*/, 12];
1375
+ case 11: throw new Error("Unknown model variant \"".concat(currentTemplate.modelRequirements.modelVariant, "\""));
1376
+ case 12:
1351
1377
  try {
1352
- for (_d = (e_2 = void 0, __values(Object.entries(currentTemplate.expectations))), _e = _d.next(); !_e.done; _e = _d.next()) {
1353
- _f = __read(_e.value, 2), unit = _f[0], _g = _f[1], max = _g.max, min = _g.min;
1354
- amount = CountUtils[unit](resultString);
1355
- // TODO: !!!!! Do not crash BUT retry some amount of times
1378
+ // TODO: !!!!!! Here should postprocessing happen
1379
+ for (_e = (e_2 = void 0, __values(Object.entries(currentTemplate.expectations))), _f = _e.next(); !_f.done; _f = _e.next()) {
1380
+ _g = __read(_f.value, 2), unit = _g[0], _h = _g[1], max = _h.max, min = _h.min;
1381
+ amount = CountUtils[unit.toUpperCase()](resultString);
1356
1382
  if (min && amount < min) {
1357
1383
  throw new Error("Expected at least ".concat(min, " ").concat(unit, " but got ").concat(amount));
1358
1384
  } /* not else */
@@ -1364,20 +1390,41 @@ function createPtpExecutor(options) {
1364
1390
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
1365
1391
  finally {
1366
1392
  try {
1367
- if (_e && !_e.done && (_k = _d.return)) _k.call(_d);
1393
+ if (_f && !_f.done && (_l = _e.return)) _l.call(_e);
1368
1394
  }
1369
1395
  finally { if (e_2) throw e_2.error; }
1370
1396
  }
1371
- executionReport.push({
1397
+ return [3 /*break*/, 15];
1398
+ case 13:
1399
+ error_2 = _p.sent();
1400
+ if (!(error_2 instanceof Error)) {
1401
+ throw error_2;
1402
+ }
1403
+ naturalExecutionError = error_2;
1404
+ return [3 /*break*/, 15];
1405
+ case 14:
1406
+ executionReport.promptExecutions.push({
1372
1407
  prompt: {
1408
+ title: prompt_1.title,
1373
1409
  content: prompt_1.content,
1374
1410
  modelRequirements: prompt_1.modelRequirements,
1375
1411
  // <- Note: Do want to pass ONLY wanted information to the report
1376
1412
  },
1377
- result: result,
1413
+ result: result /* <- !!!!! Look what is exposed here and probbably also filter out */ ||
1414
+ undefined,
1415
+ error: naturalExecutionError || undefined,
1378
1416
  });
1379
- return [3 /*break*/, 25];
1380
- case 11:
1417
+ return [7 /*endfinally*/];
1418
+ case 15:
1419
+ if (result !== null && naturalExecutionError === null) {
1420
+ return [3 /*break*/, 32];
1421
+ }
1422
+ _p.label = 16;
1423
+ case 16:
1424
+ attempt++;
1425
+ return [3 /*break*/, 5];
1426
+ case 17: throw new Error(spaceTrim(function (block) { return "\n Natural execution failed ".concat(settings.maxNaturalExecutionAttempts, "x\n\n ").concat(block((naturalExecutionError === null || naturalExecutionError === void 0 ? void 0 : naturalExecutionError.message) || ''), "\n "); }));
1427
+ case 18:
1381
1428
  if (tools.script.length === 0) {
1382
1429
  throw new Error('No script execution tools are available');
1383
1430
  }
@@ -1386,50 +1433,50 @@ function createPtpExecutor(options) {
1386
1433
  }
1387
1434
  scriptExecutionErrors = [];
1388
1435
  isScriptExecutionSuccessful = false;
1389
- _o.label = 12;
1390
- case 12:
1391
- _o.trys.push([12, 19, 20, 21]);
1392
- _h = (e_1 = void 0, __values(tools.script)), _j = _h.next();
1393
- _o.label = 13;
1394
- case 13:
1395
- if (!!_j.done) return [3 /*break*/, 18];
1396
- scriptTools = _j.value;
1397
- _o.label = 14;
1398
- case 14:
1399
- _o.trys.push([14, 16, , 17]);
1436
+ _p.label = 19;
1437
+ case 19:
1438
+ _p.trys.push([19, 26, 27, 28]);
1439
+ _j = (e_1 = void 0, __values(tools.script)), _k = _j.next();
1440
+ _p.label = 20;
1441
+ case 20:
1442
+ if (!!_k.done) return [3 /*break*/, 25];
1443
+ scriptTools = _k.value;
1444
+ _p.label = 21;
1445
+ case 21:
1446
+ _p.trys.push([21, 23, , 24]);
1400
1447
  return [4 /*yield*/, scriptTools.execute({
1401
1448
  scriptLanguage: currentTemplate.contentLanguage,
1402
1449
  script: currentTemplate.content,
1403
1450
  parameters: parametersToPass,
1404
1451
  })];
1405
- case 15:
1406
- resultString = _o.sent();
1452
+ case 22:
1453
+ resultString = _p.sent();
1407
1454
  isScriptExecutionSuccessful = true;
1408
- return [3 /*break*/, 18];
1409
- case 16:
1410
- error_1 = _o.sent();
1411
- if (!(error_1 instanceof Error)) {
1412
- throw error_1;
1455
+ return [3 /*break*/, 25];
1456
+ case 23:
1457
+ error_3 = _p.sent();
1458
+ if (!(error_3 instanceof Error)) {
1459
+ throw error_3;
1413
1460
  }
1414
- scriptExecutionErrors.push(error_1);
1415
- return [3 /*break*/, 17];
1416
- case 17:
1417
- _j = _h.next();
1418
- return [3 /*break*/, 13];
1419
- case 18: return [3 /*break*/, 21];
1420
- case 19:
1421
- e_1_1 = _o.sent();
1461
+ scriptExecutionErrors.push(error_3);
1462
+ return [3 /*break*/, 24];
1463
+ case 24:
1464
+ _k = _j.next();
1465
+ return [3 /*break*/, 20];
1466
+ case 25: return [3 /*break*/, 28];
1467
+ case 26:
1468
+ e_1_1 = _p.sent();
1422
1469
  e_1 = { error: e_1_1 };
1423
- return [3 /*break*/, 21];
1424
- case 20:
1470
+ return [3 /*break*/, 28];
1471
+ case 27:
1425
1472
  try {
1426
- if (_j && !_j.done && (_l = _h.return)) _l.call(_h);
1473
+ if (_k && !_k.done && (_m = _j.return)) _m.call(_j);
1427
1474
  }
1428
1475
  finally { if (e_1) throw e_1.error; }
1429
1476
  return [7 /*endfinally*/];
1430
- case 21:
1477
+ case 28:
1431
1478
  if (isScriptExecutionSuccessful) {
1432
- return [3 /*break*/, 25];
1479
+ return [3 /*break*/, 32];
1433
1480
  }
1434
1481
  if (scriptExecutionErrors.length === 1) {
1435
1482
  throw scriptExecutionErrors[0];
@@ -1437,19 +1484,19 @@ function createPtpExecutor(options) {
1437
1484
  else {
1438
1485
  throw new Error(spaceTrim(function (block) { return "\n Script execution failed ".concat(scriptExecutionErrors.length, " times\n\n ").concat(block(scriptExecutionErrors.map(function (error) { return '- ' + error.message; }).join('\n\n')), "\n "); }));
1439
1486
  }
1440
- case 22: return [4 /*yield*/, tools.userInterface.promptDialog({
1487
+ case 29: return [4 /*yield*/, tools.userInterface.promptDialog({
1441
1488
  prompt: replaceParameters(currentTemplate.description || '', parametersToPass),
1442
1489
  defaultValue: replaceParameters(currentTemplate.content, parametersToPass),
1443
1490
  // TODO: [🧠] !! Figure out how to define placeholder in .ptbk.md file
1444
1491
  placeholder: undefined,
1445
1492
  })];
1446
- case 23:
1447
- resultString = _o.sent();
1448
- return [3 /*break*/, 25];
1449
- case 24:
1493
+ case 30:
1494
+ resultString = _p.sent();
1495
+ return [3 /*break*/, 32];
1496
+ case 31:
1450
1497
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1451
1498
  throw new Error("Unknown execution type \"".concat(currentTemplate.executionType, "\""));
1452
- case 25:
1499
+ case 32:
1453
1500
  if (resultString === null) {
1454
1501
  // <- TODO: [🥨] Make some NeverShouldHappenError
1455
1502
  throw new Error('Something went wrong and prompt result is null');
@@ -1465,20 +1512,37 @@ function createPtpExecutor(options) {
1465
1512
  parameterValue: resultString,
1466
1513
  });
1467
1514
  }
1468
- parametersToPass = __assign(__assign({}, parametersToPass), (_m = {}, _m[resultingParameter.name] = resultString /* <- Note: Not need to detect parameter collision here because PromptTemplatePipeline checks logic consistency during construction */, _m));
1515
+ parametersToPass = __assign(__assign({}, parametersToPass), (_o = {}, _o[resultingParameter.name] = resultString /* <- Note: Not need to detect parameter collision here because PromptTemplatePipeline checks logic consistency during construction */, _o));
1469
1516
  currentTemplate = ptp.getFollowingPromptTemplate(currentTemplate.name);
1470
1517
  return [2 /*return*/];
1471
1518
  }
1472
1519
  });
1473
1520
  };
1474
- _a.label = 1;
1475
- case 1:
1476
- if (!(currentTemplate !== null)) return [3 /*break*/, 3];
1477
- return [5 /*yield**/, _loop_1()];
1521
+ _b.label = 2;
1478
1522
  case 2:
1479
- _a.sent();
1480
- return [3 /*break*/, 1];
1481
- case 3: return [2 /*return*/, parametersToPass];
1523
+ if (!(currentTemplate !== null)) return [3 /*break*/, 4];
1524
+ return [5 /*yield**/, _loop_1()];
1525
+ case 3:
1526
+ _b.sent();
1527
+ return [3 /*break*/, 2];
1528
+ case 4: return [3 /*break*/, 6];
1529
+ case 5:
1530
+ error_1 = _b.sent();
1531
+ if (!(error_1 instanceof Error)) {
1532
+ throw error_1;
1533
+ }
1534
+ return [2 /*return*/, {
1535
+ isSuccessful: false,
1536
+ errors: [error_1],
1537
+ executionReport: executionReport,
1538
+ outputParameters: parametersToPass,
1539
+ }];
1540
+ case 6: return [2 /*return*/, {
1541
+ isSuccessful: true,
1542
+ errors: [],
1543
+ executionReport: executionReport,
1544
+ outputParameters: parametersToPass,
1545
+ }];
1482
1546
  }
1483
1547
  });
1484
1548
  }); };
@@ -1499,8 +1563,8 @@ function createPtpExecutor(options) {
1499
1563
  * @see https://github.com/webgptorg/promptbook#prompt-template-pipeline-library
1500
1564
  */
1501
1565
  var PromptTemplatePipelineLibrary = /** @class */ (function () {
1502
- function PromptTemplatePipelineLibrary(promptTemplatePipelines) {
1503
- this.promptTemplatePipelines = promptTemplatePipelines;
1566
+ function PromptTemplatePipelineLibrary(options) {
1567
+ this.options = options;
1504
1568
  }
1505
1569
  /**
1506
1570
  * Constructs PromptTemplatePipeline from any sources
@@ -1511,13 +1575,13 @@ var PromptTemplatePipelineLibrary = /** @class */ (function () {
1511
1575
  * @param ptbkSources contents of .ptbk.md or .ptbk.json files
1512
1576
  * @returns PromptTemplatePipelineLibrary
1513
1577
  */
1514
- PromptTemplatePipelineLibrary.fromSources = function (ptbkSources) {
1578
+ PromptTemplatePipelineLibrary.fromSources = function (ptbkSources, settings) {
1515
1579
  var e_1, _a;
1516
- var promptTemplatePipelines = {};
1580
+ var library = {};
1517
1581
  try {
1518
1582
  for (var _b = __values(Object.entries(ptbkSources)), _c = _b.next(); !_c.done; _c = _b.next()) {
1519
1583
  var _d = __read(_c.value, 2), name_1 = _d[0], source = _d[1];
1520
- promptTemplatePipelines[name_1] = PromptTemplatePipeline.fromSource(source);
1584
+ library[name_1] = PromptTemplatePipeline.fromSource(source);
1521
1585
  }
1522
1586
  }
1523
1587
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
@@ -1527,13 +1591,13 @@ var PromptTemplatePipelineLibrary = /** @class */ (function () {
1527
1591
  }
1528
1592
  finally { if (e_1) throw e_1.error; }
1529
1593
  }
1530
- return new PromptTemplatePipelineLibrary(promptTemplatePipelines);
1594
+ return new PromptTemplatePipelineLibrary({ library: library, settings: settings });
1531
1595
  };
1532
1596
  /**
1533
1597
  * Gets prompt template pipeline by name
1534
1598
  */
1535
1599
  PromptTemplatePipelineLibrary.prototype.getPtp = function (name) {
1536
- var promptTemplatePipeline = this.promptTemplatePipelines[name];
1600
+ var promptTemplatePipeline = this.options.library[name];
1537
1601
  if (!promptTemplatePipeline) {
1538
1602
  throw new Error("Prompt template pipeline with name \"".concat(name, "\" not found"));
1539
1603
  }
@@ -1550,7 +1614,7 @@ var PromptTemplatePipelineLibrary = /** @class */ (function () {
1550
1614
  */
1551
1615
  PromptTemplatePipelineLibrary.prototype.createExecutor = function (name, tools) {
1552
1616
  var ptp = this.getPtp(name);
1553
- return createPtpExecutor({ ptp: ptp, tools: tools });
1617
+ return createPtpExecutor({ ptp: ptp, tools: tools, settings: this.options.settings });
1554
1618
  };
1555
1619
  return PromptTemplatePipelineLibrary;
1556
1620
  }());