@promptbook/node 0.84.0-12 → 0.84.0-14

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.
@@ -12,6 +12,8 @@ import { LOGO_LIGHT_SRC } from '../config';
12
12
  import { LOGO_DARK_SRC } from '../config';
13
13
  import { DEFAULT_BOOK_TITLE } from '../config';
14
14
  import { DEFAULT_TASK_TITLE } from '../config';
15
+ import { DEFAULT_PROMPT_TASK_TITLE } from '../config';
16
+ import { DEFAULT_BOOK_OUTPUT_PARAMETER_NAME } from '../config';
15
17
  import { MAX_FILENAME_LENGTH } from '../config';
16
18
  import { DEFAULT_INTERMEDIATE_FILES_STRATEGY } from '../config';
17
19
  import { DEFAULT_MAX_PARALLEL_COUNT } from '../config';
@@ -139,6 +141,8 @@ export { LOGO_LIGHT_SRC };
139
141
  export { LOGO_DARK_SRC };
140
142
  export { DEFAULT_BOOK_TITLE };
141
143
  export { DEFAULT_TASK_TITLE };
144
+ export { DEFAULT_PROMPT_TASK_TITLE };
145
+ export { DEFAULT_BOOK_OUTPUT_PARAMETER_NAME };
142
146
  export { MAX_FILENAME_LENGTH };
143
147
  export { DEFAULT_INTERMEDIATE_FILES_STRATEGY };
144
148
  export { DEFAULT_MAX_PARALLEL_COUNT };
@@ -65,6 +65,18 @@ export declare const DEFAULT_BOOK_TITLE = "\u2728 Untitled Book";
65
65
  * @public exported from `@promptbook/core`
66
66
  */
67
67
  export declare const DEFAULT_TASK_TITLE = "Task";
68
+ /**
69
+ * When the title of the prompt task is not provided, the default title is used
70
+ *
71
+ * @public exported from `@promptbook/core`
72
+ */
73
+ export declare const DEFAULT_PROMPT_TASK_TITLE = "Prompt";
74
+ /**
75
+ * When the pipeline is flat and no name of return parameter is provided, this name is used
76
+ *
77
+ * @public exported from `@promptbook/core`
78
+ */
79
+ export declare const DEFAULT_BOOK_OUTPUT_PARAMETER_NAME = "result";
68
80
  /**
69
81
  * Warning message for the generated sections and files files
70
82
  *
@@ -0,0 +1,10 @@
1
+ import type { string_file_extension } from '../../types/typeAliases';
2
+ import type { string_mime_type } from '../../types/typeAliases';
3
+ /**
4
+ * Convert mime type to file extension
5
+ *
6
+ * Note: If the mime type is invalid, `null` is returned
7
+ *
8
+ * @private within the repository
9
+ */
10
+ export declare function mimeTypeToExtension(value: string_mime_type): string_file_extension | null;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/node",
3
- "version": "0.84.0-12",
3
+ "version": "0.84.0-14",
4
4
  "description": "It's time for a paradigm shift. The future of software in plain English, French or Latin",
5
5
  "--note-0": " <- [🐊]",
6
6
  "private": false,
@@ -54,7 +54,7 @@
54
54
  "module": "./esm/index.es.js",
55
55
  "typings": "./esm/typings/src/_packages/node.index.d.ts",
56
56
  "peerDependencies": {
57
- "@promptbook/core": "0.84.0-12"
57
+ "@promptbook/core": "0.84.0-14"
58
58
  },
59
59
  "dependencies": {
60
60
  "colors": "1.4.0",
package/umd/index.umd.js CHANGED
@@ -45,7 +45,7 @@
45
45
  * @generated
46
46
  * @see https://github.com/webgptorg/promptbook
47
47
  */
48
- var PROMPTBOOK_ENGINE_VERSION = '0.84.0-11';
48
+ var PROMPTBOOK_ENGINE_VERSION = '0.84.0-13';
49
49
  /**
50
50
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
51
51
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -222,6 +222,12 @@
222
222
  * @public exported from `@promptbook/core`
223
223
  */
224
224
  var DEFAULT_TASK_TITLE = "Task";
225
+ /**
226
+ * When the pipeline is flat and no name of return parameter is provided, this name is used
227
+ *
228
+ * @public exported from `@promptbook/core`
229
+ */
230
+ var DEFAULT_BOOK_OUTPUT_PARAMETER_NAME = 'result';
225
231
  // <- TODO: [🧠] Better system for generator warnings - not always "code" and "by `@promptbook/cli`"
226
232
  /**
227
233
  * The maximum number of iterations for a loops
@@ -5340,22 +5346,6 @@
5340
5346
  * TODO: [®] DRY Register logic
5341
5347
  */
5342
5348
 
5343
- /**
5344
- * Removes emojis from a string and fix whitespaces
5345
- *
5346
- * @param text with emojis
5347
- * @returns text without emojis
5348
- * @public exported from `@promptbook/utils`
5349
- */
5350
- function removeEmojis(text) {
5351
- // Replace emojis (and also ZWJ sequence) with hyphens
5352
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
5353
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
5354
- text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
5355
- text = text.replace(/\p{Extended_Pictographic}/gu, '');
5356
- return text;
5357
- }
5358
-
5359
5349
  /**
5360
5350
  * @@@
5361
5351
  *
@@ -5418,30 +5408,6 @@
5418
5408
  * Note: [💞] Ignore a discrepancy between file name and entity name
5419
5409
  */
5420
5410
 
5421
- /**
5422
- * @@@
5423
- *
5424
- * @param value @@@
5425
- * @returns @@@
5426
- * @example @@@
5427
- * @public exported from `@promptbook/utils`
5428
- */
5429
- function titleToName(value) {
5430
- if (isValidUrl(value)) {
5431
- value = value.replace(/^https?:\/\//, '');
5432
- value = value.replace(/\.html$/, '');
5433
- }
5434
- else if (isValidFilePath(value)) {
5435
- value = path.basename(value);
5436
- // Note: Keeping extension in the name
5437
- }
5438
- value = value.split('/').join('-');
5439
- value = removeEmojis(value);
5440
- value = normalizeToKebabCase(value);
5441
- // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
5442
- return value;
5443
- }
5444
-
5445
5411
  /**
5446
5412
  * Creates unique name for the source
5447
5413
  *
@@ -5527,6 +5493,57 @@
5527
5493
  * TODO: [🖇] What about symlinks?
5528
5494
  */
5529
5495
 
5496
+ /**
5497
+ * Convert mime type to file extension
5498
+ *
5499
+ * Note: If the mime type is invalid, `null` is returned
5500
+ *
5501
+ * @private within the repository
5502
+ */
5503
+ function mimeTypeToExtension(value) {
5504
+ return mimeTypes.extension(value) || null;
5505
+ }
5506
+
5507
+ /**
5508
+ * Removes emojis from a string and fix whitespaces
5509
+ *
5510
+ * @param text with emojis
5511
+ * @returns text without emojis
5512
+ * @public exported from `@promptbook/utils`
5513
+ */
5514
+ function removeEmojis(text) {
5515
+ // Replace emojis (and also ZWJ sequence) with hyphens
5516
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
5517
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
5518
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
5519
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
5520
+ return text;
5521
+ }
5522
+
5523
+ /**
5524
+ * @@@
5525
+ *
5526
+ * @param value @@@
5527
+ * @returns @@@
5528
+ * @example @@@
5529
+ * @public exported from `@promptbook/utils`
5530
+ */
5531
+ function titleToName(value) {
5532
+ if (isValidUrl(value)) {
5533
+ value = value.replace(/^https?:\/\//, '');
5534
+ value = value.replace(/\.html$/, '');
5535
+ }
5536
+ else if (isValidFilePath(value)) {
5537
+ value = path.basename(value);
5538
+ // Note: Keeping extension in the name
5539
+ }
5540
+ value = value.split('/').join('-');
5541
+ value = removeEmojis(value);
5542
+ value = normalizeToKebabCase(value);
5543
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
5544
+ return value;
5545
+ }
5546
+
5530
5547
  /**
5531
5548
  * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
5532
5549
  *
@@ -5562,7 +5579,7 @@
5562
5579
  function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
5563
5580
  var _a;
5564
5581
  return __awaiter(this, void 0, void 0, function () {
5565
- var _b, fetch, knowledgeSourceContent, name, _c, _d, rootDirname, url, response, mimeType, filename, hash, rootDirname_1, filepath, _f, _g, _h, _j, _k, filename_1, fileExtension, mimeType;
5582
+ var _b, fetch, knowledgeSourceContent, name, _c, _d, rootDirname, url, response_1, mimeType, basename, hash, rootDirname_1, filepath, _f, _g, _h, _j, _k, filename_1, fileExtension, mimeType;
5566
5583
  return __generator(this, function (_l) {
5567
5584
  switch (_l.label) {
5568
5585
  case 0:
@@ -5578,25 +5595,67 @@
5578
5595
  url = knowledgeSourceContent;
5579
5596
  return [4 /*yield*/, fetch(url)];
5580
5597
  case 1:
5581
- response = _l.sent();
5582
- mimeType = ((_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]) || 'text/html';
5583
- filename = url.split('/').pop() || titleToName(url);
5598
+ response_1 = _l.sent();
5599
+ mimeType = ((_a = response_1.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]) || 'text/html';
5600
+ if (tools.fs === undefined || !url.endsWith('.pdf' /* <- TODO: [💵] */)) {
5601
+ return [2 /*return*/, {
5602
+ source: name,
5603
+ filename: null,
5604
+ url: url,
5605
+ mimeType: mimeType,
5606
+ /*
5607
+ TODO: [🥽]
5608
+ > async asBlob() {
5609
+ > // TODO: [👨🏻‍🤝‍👨🏻] This can be called multiple times BUT when called second time, response in already consumed
5610
+ > const content = await response.blob();
5611
+ > return content;
5612
+ > },
5613
+ */
5614
+ asJson: function () {
5615
+ return __awaiter(this, void 0, void 0, function () {
5616
+ var content;
5617
+ return __generator(this, function (_a) {
5618
+ switch (_a.label) {
5619
+ case 0: return [4 /*yield*/, response_1.json()];
5620
+ case 1:
5621
+ content = _a.sent();
5622
+ return [2 /*return*/, content];
5623
+ }
5624
+ });
5625
+ });
5626
+ },
5627
+ asText: function () {
5628
+ return __awaiter(this, void 0, void 0, function () {
5629
+ var content;
5630
+ return __generator(this, function (_a) {
5631
+ switch (_a.label) {
5632
+ case 0: return [4 /*yield*/, response_1.text()];
5633
+ case 1:
5634
+ content = _a.sent();
5635
+ return [2 /*return*/, content];
5636
+ }
5637
+ });
5638
+ });
5639
+ },
5640
+ }];
5641
+ }
5642
+ basename = url.split('/').pop() || titleToName(url);
5584
5643
  hash = sha256__default["default"](hexEncoder__default["default"].parse(url)).toString( /* hex */);
5585
5644
  rootDirname_1 = path.join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
5586
- filepath = path.join.apply(void 0, __spreadArray(__spreadArray([], __read(nameToSubfolderPath(hash /* <- TODO: [🎎] Maybe add some SHA256 prefix */)), false), ["".concat(filename.substring(0, MAX_FILENAME_LENGTH), ".pdf")], false));
5645
+ filepath = path.join.apply(void 0, __spreadArray(__spreadArray([], __read(nameToSubfolderPath(hash /* <- TODO: [🎎] Maybe add some SHA256 prefix */)), false), ["".concat(basename.substring(0, MAX_FILENAME_LENGTH), ".").concat(mimeTypeToExtension(mimeType))], false));
5587
5646
  return [4 /*yield*/, tools.fs.mkdir(path.dirname(path.join(rootDirname_1, filepath)), { recursive: true })];
5588
5647
  case 2:
5589
5648
  _l.sent();
5590
5649
  _g = (_f = tools.fs).writeFile;
5591
5650
  _h = [path.join(rootDirname_1, filepath)];
5592
5651
  _k = (_j = Buffer).from;
5593
- return [4 /*yield*/, response.arrayBuffer()];
5652
+ return [4 /*yield*/, response_1.arrayBuffer()];
5594
5653
  case 3: return [4 /*yield*/, _g.apply(_f, _h.concat([_k.apply(_j, [_l.sent()])]))];
5595
5654
  case 4:
5596
5655
  _l.sent();
5597
- // TODO: !!!!!!!! Check the file security
5656
+ // TODO: [💵] Check the file security
5598
5657
  // TODO: !!!!!!!! Check the file size (if it is not too big)
5599
- // TODO: !!!!!!!! Delete the file
5658
+ // TODO: !!!!!!!! Delete the file after the scraping is done
5600
5659
  return [2 /*return*/, makeKnowledgeSourceHandler({ name: name, knowledgeSourceContent: filepath }, tools, __assign(__assign({}, options), { rootDirname: rootDirname_1 }))];
5601
5660
  case 5:
5602
5661
  if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 7];
@@ -8717,12 +8776,15 @@
8717
8776
  pipelineString = removeMarkdownComments(pipelineString);
8718
8777
  pipelineString = spaceTrim__default["default"](pipelineString);
8719
8778
  var isMarkdownBeginningWithHeadline = pipelineString.startsWith('# ');
8720
- var isLastLineReturnStatement = pipelineString.split('\n').pop().split('`').join('').startsWith('->');
8721
- // TODO: Also (double)check
8779
+ //const isLastLineReturnStatement = pipelineString.split('\n').pop()!.split('`').join('').startsWith('->');
8780
+ var isBacktickBlockUsed = pipelineString.includes('```');
8781
+ var isQuoteBlocksUsed = /^>\s+/m.test(pipelineString);
8782
+ var isBlocksUsed = isBacktickBlockUsed || isQuoteBlocksUsed;
8783
+ // TODO: [🧉] Also (double)check
8722
8784
  // > const usedCommands
8723
8785
  // > const isBlocksUsed
8724
8786
  // > const returnStatementCount
8725
- var isFlat = !isMarkdownBeginningWithHeadline && isLastLineReturnStatement;
8787
+ var isFlat = !isMarkdownBeginningWithHeadline && !isBlocksUsed; /* && isLastLineReturnStatement */
8726
8788
  return isFlat;
8727
8789
  }
8728
8790
 
@@ -8736,9 +8798,26 @@
8736
8798
  return pipelineString;
8737
8799
  }
8738
8800
  var pipelineStringLines = pipelineString.split('\n');
8739
- var returnStatement = pipelineStringLines.pop();
8801
+ var potentialReturnStatement = pipelineStringLines.pop();
8802
+ var returnStatement;
8803
+ if (/(-|=)>\s*\{.*\}/.test(potentialReturnStatement)) {
8804
+ // Note: Last line is return statement
8805
+ returnStatement = potentialReturnStatement;
8806
+ }
8807
+ else {
8808
+ // Note: Last line is not a return statement
8809
+ returnStatement = "-> {".concat(DEFAULT_BOOK_OUTPUT_PARAMETER_NAME, "}");
8810
+ pipelineStringLines.push(potentialReturnStatement);
8811
+ }
8740
8812
  var prompt = spaceTrim__default["default"](pipelineStringLines.join('\n'));
8741
- pipelineString = validatePipelineString(spaceTrim__default["default"](function (block) { return "\n # ".concat(DEFAULT_BOOK_TITLE, "\n\n ## Prompt\n\n ```\n ").concat(block(prompt), "\n ```\n\n ").concat(returnStatement, "\n "); }));
8813
+ var quotedPrompt;
8814
+ if (prompt.split('\n').length <= 1) {
8815
+ quotedPrompt = "> ".concat(prompt);
8816
+ }
8817
+ else {
8818
+ quotedPrompt = spaceTrim__default["default"](function (block) { return "\n ```\n ".concat(block(prompt.split('`').join('\\`')), "\n ```\n "); });
8819
+ }
8820
+ pipelineString = validatePipelineString(spaceTrim__default["default"](function (block) { return "\n # ".concat(DEFAULT_BOOK_TITLE, "\n\n ## Prompt\n\n ").concat(block(quotedPrompt), "\n\n ").concat(returnStatement, "\n "); }));
8742
8821
  // <- TODO: Maybe use book` notation
8743
8822
  return pipelineString;
8744
8823
  }