@promptbook/cli 0.74.0-6 → 0.74.0-7

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
@@ -1,6 +1,6 @@
1
+ import colors from 'colors';
1
2
  import commander from 'commander';
2
3
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
3
- import colors from 'colors';
4
4
  import { forTime } from 'waitasecond';
5
5
  import { stat, access, constants, readFile, writeFile, readdir, mkdir, unlink, rm, rmdir, rename } from 'fs/promises';
6
6
  import { join, basename, dirname } from 'path';
@@ -37,7 +37,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
37
37
  *
38
38
  * @see https://github.com/webgptorg/promptbook
39
39
  */
40
- var PROMPTBOOK_ENGINE_VERSION = '0.74.0-5';
40
+ var PROMPTBOOK_ENGINE_VERSION = '0.74.0-6';
41
41
  /**
42
42
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
43
43
  */
@@ -7953,11 +7953,18 @@ function pipelineStringToJsonSync(pipelineString) {
7953
7953
  }
7954
7954
  // =============================================================
7955
7955
  // Note: 1️⃣ Parsing of the markdown into object
7956
+ if (pipelineString.startsWith('#!')) {
7957
+ var _c = __read(pipelineString.split('\n')), shebangLine_1 = _c[0], restLines = _c.slice(1);
7958
+ if (!(shebangLine_1 || '').includes('ptbk')) {
7959
+ 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 "); }));
7960
+ }
7961
+ pipelineString = restLines.join('\n');
7962
+ }
7956
7963
  pipelineString = removeContentComments(pipelineString);
7957
7964
  pipelineString = flattenMarkdown(pipelineString) /* <- Note: [🥞] */;
7958
7965
  pipelineString = pipelineString.replaceAll(/`\{(?<parameterName>[a-z0-9_]+)\}`/gi, '{$<parameterName>}');
7959
7966
  pipelineString = pipelineString.replaceAll(/`->\s+\{(?<parameterName>[a-z0-9_]+)\}`/gi, '-> {$<parameterName>}');
7960
- var _c = __read(splitMarkdownIntoSections(pipelineString).map(parseMarkdownSection)), pipelineHead = _c[0], pipelineSections = _c.slice(1); /* <- Note: [🥞] */
7967
+ var _d = __read(splitMarkdownIntoSections(pipelineString).map(parseMarkdownSection)), pipelineHead = _d[0], pipelineSections = _d.slice(1); /* <- Note: [🥞] */
7961
7968
  if (pipelineHead === undefined) {
7962
7969
  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 "); }));
7963
7970
  }
@@ -8047,10 +8054,10 @@ function pipelineStringToJsonSync(pipelineString) {
8047
8054
  finally { if (e_1) throw e_1.error; }
8048
8055
  }
8049
8056
  var _loop_2 = function (section) {
8050
- var e_3, _d;
8057
+ var e_3, _e;
8051
8058
  // TODO: Parse template description (the content out of the codeblock and lists)
8052
8059
  var listItems_2 = extractAllListItemsFromMarkdown(section.content);
8053
- var _e = extractOneBlockFromMarkdown(section.content), language = _e.language, content = _e.content;
8060
+ var _f = extractOneBlockFromMarkdown(section.content), language = _f.language, content = _f.content;
8054
8061
  // TODO: [🎾][1] DRY description
8055
8062
  var description_1 = section.content;
8056
8063
  // Note: Remove codeblocks - TODO: [🎾]
@@ -8115,14 +8122,14 @@ function pipelineStringToJsonSync(pipelineString) {
8115
8122
  try {
8116
8123
  // TODO [♓️] List commands and before apply order them to achieve order-agnostic commands
8117
8124
  for (var commands_1 = (e_3 = void 0, __values(commands)), commands_1_1 = commands_1.next(); !commands_1_1.done; commands_1_1 = commands_1.next()) {
8118
- var _f = commands_1_1.value, listItem = _f.listItem, command = _f.command;
8125
+ var _g = commands_1_1.value, listItem = _g.listItem, command = _g.command;
8119
8126
  _loop_3(listItem, command);
8120
8127
  }
8121
8128
  }
8122
8129
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
8123
8130
  finally {
8124
8131
  try {
8125
- if (commands_1_1 && !commands_1_1.done && (_d = commands_1.return)) _d.call(commands_1);
8132
+ if (commands_1_1 && !commands_1_1.done && (_e = commands_1.return)) _e.call(commands_1);
8126
8133
  }
8127
8134
  finally { if (e_3) throw e_3.error; }
8128
8135
  }
@@ -10974,10 +10981,10 @@ function initializeRunCommand(program) {
10974
10981
  runCommand.option('-v, --verbose', "Is output verbose", false);
10975
10982
  runCommand.option('--no-interactive', "Input is not interactive", false);
10976
10983
  runCommand.option('-s, --save-report <path>', "Save report to file");
10977
- // TODO: !!!!!! Interactive mode
10984
+ // TODO: !!!!!! Implement non-interactive mode - allow to pass input parameters as JSON
10978
10985
  // TODO: !!!!!! JSON output
10979
10986
  runCommand.action(function (filePathRaw, options) { return __awaiter(_this, void 0, void 0, function () {
10980
- var isCacheReloaded, isVerbose, saveReport, prepareAndScrapeOptions, fs, filePath, filePathCandidates, filePathCandidates_1, filePathCandidates_1_1, filePathCandidate, e_1_1, llm, executables, tools, pipelineString, pipeline, pipelineExecutor, questions, response, inputParameters, result, isSuccessful, errors, warnings, outputParameters, executionReport, executionReportString, _a, _b, error, _c, _d, warning, _e, _f, key, value, separator;
10987
+ var isCacheReloaded, isVerbose, saveReport, prepareAndScrapeOptions, fs, filePath, filePathCandidates, filePathCandidates_1, filePathCandidates_1_1, filePathCandidate, e_1_1, llm, executables, tools, pipelineString, pipeline, error_1, pipelineExecutor, questions, response, inputParameters, result, isSuccessful, errors, warnings, outputParameters, executionReport, executionReportString, _a, _b, error, _c, _d, warning, _e, _f, key, value, separator;
10981
10988
  var e_1, _g, _h, e_2, _j, e_3, _k, e_4, _l;
10982
10989
  return __generator(this, function (_m) {
10983
10990
  switch (_m.label) {
@@ -11076,12 +11083,25 @@ function initializeRunCommand(program) {
11076
11083
  if (isVerbose) {
11077
11084
  console.info(colors.gray('--- Preparing pipeline ---'));
11078
11085
  }
11079
- return [4 /*yield*/, pipelineStringToJson(pipelineString, tools)];
11086
+ _m.label = 12;
11080
11087
  case 12:
11088
+ _m.trys.push([12, 14, , 15]);
11089
+ return [4 /*yield*/, pipelineStringToJson(pipelineString, tools)];
11090
+ case 13:
11081
11091
  pipeline = _m.sent();
11092
+ return [3 /*break*/, 15];
11093
+ case 14:
11094
+ error_1 = _m.sent();
11095
+ if (!(error_1 instanceof ParseError)) {
11096
+ throw error_1;
11097
+ }
11098
+ console.error(colors.red(spaceTrim$1(function (block) { return "\n ".concat(block(error_1.message), "\n\n in ").concat(filePath, "\n "); })));
11099
+ return [2 /*return*/, process.exit(1)];
11100
+ case 15:
11082
11101
  if (isVerbose) {
11083
11102
  console.info(colors.gray('--- Validating pipeline ---'));
11084
11103
  }
11104
+ // TODO: !!!!!! Same try-catch for LogicError
11085
11105
  validatePipeline(pipeline);
11086
11106
  if (isVerbose) {
11087
11107
  console.info(colors.gray('--- Creating executor ---'));
@@ -11103,16 +11123,26 @@ function initializeRunCommand(program) {
11103
11123
  return isInput;
11104
11124
  })
11105
11125
  .map(function (_a) {
11106
- var name = _a.name;
11107
- return ({
11126
+ var name = _a.name, exampleValues = _a.exampleValues;
11127
+ var message = name;
11128
+ var initial = '';
11129
+ if (exampleValues && exampleValues.length > 0) {
11130
+ var exampleValuesFiltered = exampleValues.filter(function (exampleValue) { return countLines(exampleValue) <= 1 && countCharacters(exampleValue) <= 30; });
11131
+ if (exampleValuesFiltered.length !== 0) {
11132
+ message += " (e.g. ".concat(exampleValuesFiltered.join(', '), ")");
11133
+ }
11134
+ initial = exampleValues[0] || '';
11135
+ }
11136
+ return {
11108
11137
  type: 'text',
11109
11138
  name: name,
11110
- message: name,
11139
+ message: message,
11140
+ initial: initial,
11111
11141
  // TODO: Maybe use> validate: value => value < 18 ? `Forbidden` : true
11112
- });
11142
+ };
11113
11143
  });
11114
11144
  return [4 /*yield*/, prompts(questions)];
11115
- case 13:
11145
+ case 16:
11116
11146
  response = _m.sent();
11117
11147
  inputParameters = response;
11118
11148
  if (isVerbose) {
@@ -11124,26 +11154,26 @@ function initializeRunCommand(program) {
11124
11154
  console.info(taskProgress);
11125
11155
  }
11126
11156
  })];
11127
- case 14:
11157
+ case 17:
11128
11158
  result = _m.sent();
11129
11159
  isSuccessful = result.isSuccessful, errors = result.errors, warnings = result.warnings, outputParameters = result.outputParameters, executionReport = result.executionReport;
11130
11160
  if (isVerbose) {
11131
11161
  console.info(colors.gray('--- Detailed Result ---'));
11132
11162
  console.info({ isSuccessful: isSuccessful, errors: errors, warnings: warnings, outputParameters: outputParameters, executionReport: executionReport });
11133
11163
  }
11134
- if (!(saveReport && saveReport.endsWith('.json'))) return [3 /*break*/, 16];
11164
+ if (!(saveReport && saveReport.endsWith('.json'))) return [3 /*break*/, 19];
11135
11165
  return [4 /*yield*/, writeFile(saveReport, JSON.stringify(executionReport, null, 4) + '\n', 'utf-8')];
11136
- case 15:
11166
+ case 18:
11137
11167
  _m.sent();
11138
- return [3 /*break*/, 18];
11139
- case 16:
11140
- if (!(saveReport && saveReport.endsWith('.md'))) return [3 /*break*/, 18];
11168
+ return [3 /*break*/, 21];
11169
+ case 19:
11170
+ if (!(saveReport && saveReport.endsWith('.md'))) return [3 /*break*/, 21];
11141
11171
  executionReportString = executionReportJsonToString(executionReport);
11142
11172
  return [4 /*yield*/, writeFile(saveReport, executionReportString, 'utf-8')];
11143
- case 17:
11173
+ case 20:
11144
11174
  _m.sent();
11145
- _m.label = 18;
11146
- case 18:
11175
+ _m.label = 21;
11176
+ case 21:
11147
11177
  if (saveReport && isVerbose) {
11148
11178
  console.info(colors.green("Report saved to ".concat(saveReport)));
11149
11179
  }
@@ -11337,11 +11367,16 @@ function initializeTestCommand(program) {
11337
11367
  */
11338
11368
  function promptbookCli() {
11339
11369
  return __awaiter(this, void 0, void 0, function () {
11340
- var program;
11370
+ var isVerbose, program;
11341
11371
  return __generator(this, function (_a) {
11342
11372
  if (!$isRunningInNode()) {
11343
11373
  throw new EnvironmentMismatchError(spaceTrim("\n Function promptbookCli is initiator of CLI script and should be run in Node.js environment.\n\n - In browser use function exported from `@promptbook/utils` or `@promptbook/core` directly, for example `prettifyPipelineString`.\n\n "));
11344
11374
  }
11375
+ isVerbose = process.argv.some(function (arg) { return arg === '--verbose' || arg === '-v'; });
11376
+ // <- TODO: Can be this be done with commander before the commander commands are initialized?
11377
+ if (isVerbose) {
11378
+ console.info(colors.gray("Promptbook CLI version ".concat(PROMPTBOOK_ENGINE_VERSION, " in ").concat(__filename.split('\\').join('/'))));
11379
+ }
11345
11380
  program = new commander.Command();
11346
11381
  program.name('promptbook');
11347
11382
  program.alias('ptbk');
@@ -13675,12 +13710,9 @@ function $execCommandNormalizeOptions(options) {
13675
13710
  })
13676
13711
  .filter(function (arg) { return arg !== ''; });
13677
13712
  if (_.length > 1) {
13678
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13679
13713
  _a = __read(_), command = _a[0], args = _a.slice(1);
13680
13714
  }
13681
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13682
13715
  if (options.args) {
13683
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
13684
13716
  args = __spreadArray(__spreadArray([], __read(args), false), __read(options.args), false);
13685
13717
  }
13686
13718
  var humanReadableCommand = !['npx', 'npm'].includes(command) ? command : args[0];