@promptbook/node 0.77.0-6 → 0.77.1

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/README.md CHANGED
@@ -23,10 +23,6 @@
23
23
 
24
24
 
25
25
 
26
- <blockquote style="color: #ff8811">
27
- <b>⚠ Warning:</b> This is a pre-release version of the library. It is not yet ready for production use. Please look at <a href="https://www.npmjs.com/package/@promptbook/core?activeTab=versions">latest stable release</a>.
28
- </blockquote>
29
-
30
26
  ## 📦 Package `@promptbook/node`
31
27
 
32
28
  - Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
package/esm/index.es.js CHANGED
@@ -26,7 +26,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
26
26
  *
27
27
  * @see https://github.com/webgptorg/promptbook
28
28
  */
29
- var PROMPTBOOK_ENGINE_VERSION = '0.77.0-5';
29
+ var PROMPTBOOK_ENGINE_VERSION = '0.77.0';
30
30
  /**
31
31
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
32
32
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2169,7 +2169,7 @@ var NotYetImplementedError = /** @class */ (function (_super) {
2169
2169
  *
2170
2170
  * @public exported from `@promptbook/core`
2171
2171
  */
2172
- var ERRORS = {
2172
+ var PROMPTBOOK_ERRORS = {
2173
2173
  AbstractFormatError: AbstractFormatError,
2174
2174
  CsvFormatError: CsvFormatError,
2175
2175
  CollectionError: CollectionError,
@@ -2187,6 +2187,35 @@ var ERRORS = {
2187
2187
  UnexpectedError: UnexpectedError,
2188
2188
  // TODO: [🪑]> VersionMismatchError,
2189
2189
  };
2190
+ /**
2191
+ * Index of all javascript errors
2192
+ *
2193
+ * @private for internal usage
2194
+ */
2195
+ var COMMON_JAVASCRIPT_ERRORS = {
2196
+ Error: Error,
2197
+ EvalError: EvalError,
2198
+ RangeError: RangeError,
2199
+ ReferenceError: ReferenceError,
2200
+ SyntaxError: SyntaxError,
2201
+ TypeError: TypeError,
2202
+ URIError: URIError,
2203
+ AggregateError: AggregateError,
2204
+ /*
2205
+ Note: Not widely supported
2206
+ > InternalError,
2207
+ > ModuleError,
2208
+ > HeapError,
2209
+ > WebAssemblyCompileError,
2210
+ > WebAssemblyRuntimeError,
2211
+ */
2212
+ };
2213
+ /**
2214
+ * Index of all errors
2215
+ *
2216
+ * @private for internal usage
2217
+ */
2218
+ var ALL_ERRORS = __assign(__assign({}, PROMPTBOOK_ERRORS), COMMON_JAVASCRIPT_ERRORS);
2190
2219
  /**
2191
2220
  * Note: [💞] Ignore a discrepancy between file name and entity name
2192
2221
  */
@@ -2197,11 +2226,11 @@ var ERRORS = {
2197
2226
  * @public exported from `@promptbook/utils`
2198
2227
  */
2199
2228
  function deserializeError(error) {
2200
- if (error.name === 'Error') {
2201
- return new Error(error.message);
2229
+ var ErrorClass = ALL_ERRORS[error.name];
2230
+ if (ErrorClass === undefined) {
2231
+ return new Error("".concat(error.name, ": ").concat(error.message));
2202
2232
  }
2203
- var CustomError = ERRORS[error.name];
2204
- return new CustomError(error.message);
2233
+ return new ErrorClass(error.message);
2205
2234
  }
2206
2235
 
2207
2236
  /**
@@ -2275,8 +2304,8 @@ function isPipelinePrepared(pipeline) {
2275
2304
  */
2276
2305
  function serializeError(error) {
2277
2306
  var name = error.name, message = error.message, stack = error.stack;
2278
- if (!__spreadArray(['Error'], __read(Object.keys(ERRORS)), false).includes(name)) {
2279
- throw new UnexpectedError(spaceTrim$1(function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
2307
+ if (!Object.keys(ALL_ERRORS).includes(name)) {
2308
+ console.error(spaceTrim$1(function (block) { return "\n \n Cannot serialize error with name \"".concat(name, "\"\n\n ").concat(block(stack || message), "\n \n "); }));
2280
2309
  }
2281
2310
  return {
2282
2311
  name: name,
@@ -8153,7 +8182,7 @@ function titleToName(value) {
8153
8182
  * @public exported from `@promptbook/core`
8154
8183
  */
8155
8184
  function pipelineStringToJsonSync(pipelineString) {
8156
- var e_1, _a, e_2, _b, e_3, _c, e_4, _d;
8185
+ var e_1, _a, e_2, _b, e_3, _c, e_4, _d, e_5, _e;
8157
8186
  var $pipelineJson = {
8158
8187
  title: undefined /* <- Note: [🍙] Putting here placeholder to keep `title` on top at final JSON */,
8159
8188
  pipelineUrl: undefined /* <- Note: Putting here placeholder to keep `pipelineUrl` on top at final JSON */,
@@ -8182,7 +8211,7 @@ function pipelineStringToJsonSync(pipelineString) {
8182
8211
  // =============================================================
8183
8212
  // Note: 1️⃣ Parsing of the markdown into object
8184
8213
  if (pipelineString.startsWith('#!')) {
8185
- var _e = __read(pipelineString.split('\n')), shebangLine_1 = _e[0], restLines = _e.slice(1);
8214
+ var _f = __read(pipelineString.split('\n')), shebangLine_1 = _f[0], restLines = _f.slice(1);
8186
8215
  if (!(shebangLine_1 || '').includes('ptbk')) {
8187
8216
  throw new ParseError(spaceTrim(function (block) { return "\n It seems that you try to parse a book file which has non-standard shebang line for book files:\n Shebang line must contain 'ptbk'\n\n You have:\n ".concat(block(shebangLine_1 || '(empty line)'), "\n\n It should look like this:\n #!/usr/bin/env ptbk\n\n ").concat(block(getPipelineIdentification()), "\n "); }));
8188
8217
  }
@@ -8192,7 +8221,7 @@ function pipelineStringToJsonSync(pipelineString) {
8192
8221
  pipelineString = flattenMarkdown(pipelineString) /* <- Note: [🥞] */;
8193
8222
  pipelineString = pipelineString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
8194
8223
  pipelineString = pipelineString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
8195
- var _f = __read(splitMarkdownIntoSections(pipelineString).map(parseMarkdownSection)), pipelineHead = _f[0], pipelineSections = _f.slice(1); /* <- Note: [🥞] */
8224
+ var _g = __read(splitMarkdownIntoSections(pipelineString).map(parseMarkdownSection)), pipelineHead = _g[0], pipelineSections = _g.slice(1); /* <- Note: [🥞] */
8196
8225
  if (pipelineHead === undefined) {
8197
8226
  throw new UnexpectedError(spaceTrim(function (block) { return "\n Pipeline head is not defined\n\n ".concat(block(getPipelineIdentification()), "\n\n This should never happen, because the pipeline already flattened\n "); }));
8198
8227
  }
@@ -8283,11 +8312,41 @@ function pipelineStringToJsonSync(pipelineString) {
8283
8312
  }
8284
8313
  finally { if (e_1) throw e_1.error; }
8285
8314
  }
8315
+ // =============================================================
8316
+ // Note: 4️⃣ Prepare unique section names with indexes when needed
8317
+ var sectionCounts = {};
8318
+ try {
8319
+ for (var pipelineSections_1 = __values(pipelineSections), pipelineSections_1_1 = pipelineSections_1.next(); !pipelineSections_1_1.done; pipelineSections_1_1 = pipelineSections_1.next()) {
8320
+ var section = pipelineSections_1_1.value;
8321
+ var name_1 = titleToName(section.title);
8322
+ if (sectionCounts[name_1] === undefined) {
8323
+ sectionCounts[name_1] = { count: 0, currentIndex: 0 };
8324
+ }
8325
+ sectionCounts[name_1].count++;
8326
+ }
8327
+ }
8328
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
8329
+ finally {
8330
+ try {
8331
+ if (pipelineSections_1_1 && !pipelineSections_1_1.done && (_b = pipelineSections_1.return)) _b.call(pipelineSections_1);
8332
+ }
8333
+ finally { if (e_2) throw e_2.error; }
8334
+ }
8335
+ var getUniqueSectionName = function (title) {
8336
+ var name = titleToName(title);
8337
+ var count = sectionCounts[name];
8338
+ if (count.count === 1) {
8339
+ return name;
8340
+ }
8341
+ var nameWithSuffix = "".concat(name, "-").concat(count.currentIndex);
8342
+ count.currentIndex++;
8343
+ return nameWithSuffix;
8344
+ };
8286
8345
  var _loop_2 = function (section) {
8287
- var e_5, _l, e_6, _m;
8346
+ var e_6, _m, e_7, _o;
8288
8347
  // TODO: Parse section's description (the content out of the codeblock and lists)
8289
8348
  var listItems_2 = extractAllListItemsFromMarkdown(section.content);
8290
- var _o = extractOneBlockFromMarkdown(section.content), language = _o.language, content = _o.content;
8349
+ var _p = extractOneBlockFromMarkdown(section.content), language = _p.language, content = _p.content;
8291
8350
  // TODO: [🎾][1] DRY description
8292
8351
  var description_1 = section.content;
8293
8352
  // Note: Remove codeblocks - TODO: [🎾]
@@ -8303,7 +8362,7 @@ function pipelineStringToJsonSync(pipelineString) {
8303
8362
  isSectionTypeSet: false,
8304
8363
  isTask: true,
8305
8364
  taskType: undefined /* <- Note: [🍙] Putting here placeholder to keep `taskType` on top at final JSON */,
8306
- name: titleToName(section.title),
8365
+ name: getUniqueSectionName(section.title),
8307
8366
  title: section.title,
8308
8367
  description: description_1,
8309
8368
  content: content,
@@ -8351,17 +8410,17 @@ function pipelineStringToJsonSync(pipelineString) {
8351
8410
  };
8352
8411
  try {
8353
8412
  // TODO [♓️] List commands and before apply order them to achieve order-agnostic commands
8354
- for (var commands_1 = (e_5 = void 0, __values(commands)), commands_1_1 = commands_1.next(); !commands_1_1.done; commands_1_1 = commands_1.next()) {
8355
- var _p = commands_1_1.value, listItem = _p.listItem, command = _p.command;
8413
+ for (var commands_1 = (e_6 = void 0, __values(commands)), commands_1_1 = commands_1.next(); !commands_1_1.done; commands_1_1 = commands_1.next()) {
8414
+ var _q = commands_1_1.value, listItem = _q.listItem, command = _q.command;
8356
8415
  _loop_4(listItem, command);
8357
8416
  }
8358
8417
  }
8359
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
8418
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
8360
8419
  finally {
8361
8420
  try {
8362
- if (commands_1_1 && !commands_1_1.done && (_l = commands_1.return)) _l.call(commands_1);
8421
+ if (commands_1_1 && !commands_1_1.done && (_m = commands_1.return)) _m.call(commands_1);
8363
8422
  }
8364
- finally { if (e_5) throw e_5.error; }
8423
+ finally { if (e_6) throw e_6.error; }
8365
8424
  }
8366
8425
  // TODO: [🍧] Should be done in SECTION command
8367
8426
  if ($taskJson.taskType === 'SCRIPT_TASK') {
@@ -8375,8 +8434,8 @@ function pipelineStringToJsonSync(pipelineString) {
8375
8434
  }
8376
8435
  $taskJson.dependentParameterNames = Array.from(extractParameterNamesFromTask($taskJson));
8377
8436
  try {
8378
- for (var _q = (e_6 = void 0, __values($taskJson.dependentParameterNames)), _r = _q.next(); !_r.done; _r = _q.next()) {
8379
- var parameterName = _r.value;
8437
+ for (var _r = (e_7 = void 0, __values($taskJson.dependentParameterNames)), _s = _r.next(); !_s.done; _s = _r.next()) {
8438
+ var parameterName = _s.value;
8380
8439
  // TODO: [🧠] This definition should be made first in the task
8381
8440
  defineParam({
8382
8441
  parameterName: parameterName,
@@ -8387,12 +8446,12 @@ function pipelineStringToJsonSync(pipelineString) {
8387
8446
  });
8388
8447
  }
8389
8448
  }
8390
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
8449
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
8391
8450
  finally {
8392
8451
  try {
8393
- if (_r && !_r.done && (_m = _q.return)) _m.call(_q);
8452
+ if (_s && !_s.done && (_o = _r.return)) _o.call(_r);
8394
8453
  }
8395
- finally { if (e_6) throw e_6.error; }
8454
+ finally { if (e_7) throw e_7.error; }
8396
8455
  }
8397
8456
  /*
8398
8457
  // TODO: [🍧] This should be checked in `MODEL` command + better error message
@@ -8421,21 +8480,21 @@ function pipelineStringToJsonSync(pipelineString) {
8421
8480
  };
8422
8481
  try {
8423
8482
  // =============================================================
8424
- // Note: 4️⃣ Process each section of the pipeline
8425
- for (var pipelineSections_1 = __values(pipelineSections), pipelineSections_1_1 = pipelineSections_1.next(); !pipelineSections_1_1.done; pipelineSections_1_1 = pipelineSections_1.next()) {
8426
- var section = pipelineSections_1_1.value;
8483
+ // Note: 5️⃣ Process each section of the pipeline
8484
+ for (var pipelineSections_2 = __values(pipelineSections), pipelineSections_2_1 = pipelineSections_2.next(); !pipelineSections_2_1.done; pipelineSections_2_1 = pipelineSections_2.next()) {
8485
+ var section = pipelineSections_2_1.value;
8427
8486
  _loop_2(section);
8428
8487
  }
8429
8488
  }
8430
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
8489
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
8431
8490
  finally {
8432
8491
  try {
8433
- if (pipelineSections_1_1 && !pipelineSections_1_1.done && (_b = pipelineSections_1.return)) _b.call(pipelineSections_1);
8492
+ if (pipelineSections_2_1 && !pipelineSections_2_1.done && (_c = pipelineSections_2.return)) _c.call(pipelineSections_2);
8434
8493
  }
8435
- finally { if (e_2) throw e_2.error; }
8494
+ finally { if (e_3) throw e_3.error; }
8436
8495
  }
8437
8496
  // =============================================================
8438
- // Note: 5️⃣ Mark parameters as INPUT if not explicitly set
8497
+ // Note: 6️⃣ Mark parameters as INPUT if not explicitly set
8439
8498
  if ($pipelineJson.parameters.every(function (parameter) { return !parameter.isInput; })) {
8440
8499
  var _loop_3 = function (parameter) {
8441
8500
  var isThisParameterResulting = $pipelineJson.tasks.some(function (task) { return task.resultingParameterName === parameter.name; });
@@ -8444,42 +8503,42 @@ function pipelineStringToJsonSync(pipelineString) {
8444
8503
  }
8445
8504
  };
8446
8505
  try {
8447
- for (var _g = __values($pipelineJson.parameters), _h = _g.next(); !_h.done; _h = _g.next()) {
8448
- var parameter = _h.value;
8506
+ for (var _h = __values($pipelineJson.parameters), _j = _h.next(); !_j.done; _j = _h.next()) {
8507
+ var parameter = _j.value;
8449
8508
  _loop_3(parameter);
8450
8509
  }
8451
8510
  }
8452
- catch (e_3_1) { e_3 = { error: e_3_1 }; }
8511
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
8453
8512
  finally {
8454
8513
  try {
8455
- if (_h && !_h.done && (_c = _g.return)) _c.call(_g);
8514
+ if (_j && !_j.done && (_d = _h.return)) _d.call(_h);
8456
8515
  }
8457
- finally { if (e_3) throw e_3.error; }
8516
+ finally { if (e_4) throw e_4.error; }
8458
8517
  }
8459
8518
  }
8460
8519
  // =============================================================
8461
- // Note: 6️⃣ Mark all non-INPUT parameters as OUTPUT if any OUTPUT is not set
8520
+ // Note: 7️⃣ Mark all non-INPUT parameters as OUTPUT if any OUTPUT is not set
8462
8521
  if ($pipelineJson.parameters.every(function (parameter) { return !parameter.isOutput; })) {
8463
8522
  try {
8464
- for (var _j = __values($pipelineJson.parameters), _k = _j.next(); !_k.done; _k = _j.next()) {
8465
- var parameter = _k.value;
8523
+ for (var _k = __values($pipelineJson.parameters), _l = _k.next(); !_l.done; _l = _k.next()) {
8524
+ var parameter = _l.value;
8466
8525
  if (!parameter.isInput) {
8467
8526
  parameter.isOutput = true;
8468
8527
  }
8469
8528
  }
8470
8529
  }
8471
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
8530
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
8472
8531
  finally {
8473
8532
  try {
8474
- if (_k && !_k.done && (_d = _j.return)) _d.call(_j);
8533
+ if (_l && !_l.done && (_e = _k.return)) _e.call(_k);
8475
8534
  }
8476
- finally { if (e_4) throw e_4.error; }
8535
+ finally { if (e_5) throw e_5.error; }
8477
8536
  }
8478
8537
  }
8479
8538
  // =============================================================
8480
- // Note: 7️⃣ Cleanup of undefined values
8539
+ // Note: 8️⃣ Cleanup of undefined values
8481
8540
  $pipelineJson.tasks.forEach(function (tasks) {
8482
- var e_7, _a;
8541
+ var e_8, _a;
8483
8542
  try {
8484
8543
  for (var _b = __values(Object.entries(tasks)), _c = _b.next(); !_c.done; _c = _b.next()) {
8485
8544
  var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
@@ -8488,16 +8547,16 @@ function pipelineStringToJsonSync(pipelineString) {
8488
8547
  }
8489
8548
  }
8490
8549
  }
8491
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
8550
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
8492
8551
  finally {
8493
8552
  try {
8494
8553
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
8495
8554
  }
8496
- finally { if (e_7) throw e_7.error; }
8555
+ finally { if (e_8) throw e_8.error; }
8497
8556
  }
8498
8557
  });
8499
8558
  $pipelineJson.parameters.forEach(function (parameter) {
8500
- var e_8, _a;
8559
+ var e_9, _a;
8501
8560
  try {
8502
8561
  for (var _b = __values(Object.entries(parameter)), _c = _b.next(); !_c.done; _c = _b.next()) {
8503
8562
  var _d = __read(_c.value, 2), key = _d[0], value = _d[1];
@@ -8506,12 +8565,12 @@ function pipelineStringToJsonSync(pipelineString) {
8506
8565
  }
8507
8566
  }
8508
8567
  }
8509
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
8568
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
8510
8569
  finally {
8511
8570
  try {
8512
8571
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
8513
8572
  }
8514
- finally { if (e_8) throw e_8.error; }
8573
+ finally { if (e_9) throw e_9.error; }
8515
8574
  }
8516
8575
  });
8517
8576
  // =============================================================
@@ -8971,7 +9030,14 @@ var $llmToolsRegister = new $Register('llm_execution_tools_constructors');
8971
9030
  */
8972
9031
  function $registeredLlmToolsMessage() {
8973
9032
  var e_1, _a, e_2, _b;
8974
- var env = process.env;
9033
+ var env;
9034
+ if ($isRunningInNode()) {
9035
+ env = process.env;
9036
+ // <- TODO: [⚛] Some DRY way how to get to `process.env` and pass it into functions - ACRY search for `env`
9037
+ }
9038
+ else {
9039
+ env = {};
9040
+ }
8975
9041
  /**
8976
9042
  * Mixes registered LLM tools from $llmToolsMetadataRegister and $llmToolsRegister
8977
9043
  */