@promptbook/node 0.84.0-11 → 0.84.0-13

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.
@@ -19,7 +19,8 @@ import { DEFAULT_MAX_EXECUTION_ATTEMPTS } from '../config';
19
19
  import { DEFAULT_MAX_KNOWLEDGE_SOURCES_SCRAPING_DEPTH } from '../config';
20
20
  import { DEFAULT_MAX_KNOWLEDGE_SOURCES_SCRAPING_TOTAL } from '../config';
21
21
  import { DEFAULT_BOOKS_DIRNAME } from '../config';
22
- import { DEFAULT_EXECUTIONS_CACHE_DIRNAME } from '../config';
22
+ import { DEFAULT_DOWNLOAD_CACHE_DIRNAME } from '../config';
23
+ import { DEFAULT_EXECUTION_CACHE_DIRNAME } from '../config';
23
24
  import { DEFAULT_SCRAPE_CACHE_DIRNAME } from '../config';
24
25
  import { DEFAULT_PIPELINE_COLLECTION_BASE_FILENAME } from '../config';
25
26
  import { DEFAULT_REMOTE_URL } from '../config';
@@ -145,7 +146,8 @@ export { DEFAULT_MAX_EXECUTION_ATTEMPTS };
145
146
  export { DEFAULT_MAX_KNOWLEDGE_SOURCES_SCRAPING_DEPTH };
146
147
  export { DEFAULT_MAX_KNOWLEDGE_SOURCES_SCRAPING_TOTAL };
147
148
  export { DEFAULT_BOOKS_DIRNAME };
148
- export { DEFAULT_EXECUTIONS_CACHE_DIRNAME };
149
+ export { DEFAULT_DOWNLOAD_CACHE_DIRNAME };
150
+ export { DEFAULT_EXECUTION_CACHE_DIRNAME };
149
151
  export { DEFAULT_SCRAPE_CACHE_DIRNAME };
150
152
  export { DEFAULT_PIPELINE_COLLECTION_BASE_FILENAME };
151
153
  export { DEFAULT_REMOTE_URL };
@@ -166,6 +166,14 @@ export declare const DEFAULT_MAX_KNOWLEDGE_SOURCES_SCRAPING_TOTAL = 200;
166
166
  * @public exported from `@promptbook/core`
167
167
  */
168
168
  export declare const DEFAULT_BOOKS_DIRNAME = "./books";
169
+ /**
170
+ * Where to store the temporary downloads
171
+ *
172
+ * Note: When the folder does not exist, it is created recursively
173
+ *
174
+ * @public exported from `@promptbook/core`
175
+ */
176
+ export declare const DEFAULT_DOWNLOAD_CACHE_DIRNAME = "./.promptbook/download-cache";
169
177
  /**
170
178
  * Where to store the cache of executions for promptbook CLI
171
179
  *
@@ -173,7 +181,7 @@ export declare const DEFAULT_BOOKS_DIRNAME = "./books";
173
181
  *
174
182
  * @public exported from `@promptbook/core`
175
183
  */
176
- export declare const DEFAULT_EXECUTIONS_CACHE_DIRNAME = "./.promptbook/executions-cache";
184
+ export declare const DEFAULT_EXECUTION_CACHE_DIRNAME = "./.promptbook/execution-cache";
177
185
  /**
178
186
  * Where to store the scrape cache
179
187
  *
@@ -3,7 +3,7 @@ import type fs from 'fs/promises';
3
3
  /**
4
4
  * Container for all the tools needed to manipulate with filesystem
5
5
  */
6
- export type FilesystemTools = Pick<typeof fs, 'access' | 'constants' | 'readFile' | 'writeFile' | 'stat' | 'readdir'>;
6
+ export type FilesystemTools = Pick<typeof fs, 'access' | 'constants' | 'readFile' | 'writeFile' | 'stat' | 'readdir' | 'mkdir'>;
7
7
  /**
8
8
  * TODO: Implement destroyable pattern to free resources
9
9
  */
@@ -6,6 +6,7 @@ import type { PipelineString } from '../pipeline/PipelineString';
6
6
  import type { TaskProgress } from '../types/TaskProgress';
7
7
  import type { InputParameters } from '../types/typeAliases';
8
8
  import type { string_filename } from '../types/typeAliases';
9
+ import type { string_parameter_value } from '../types/typeAliases';
9
10
  import type { string_pipeline_url } from '../types/typeAliases';
10
11
  /**
11
12
  * Wizzard for simple usage of the Promptbook
@@ -27,7 +28,12 @@ declare class Wizzard {
27
28
  *
28
29
  * Note: This works simmilar to the `ptbk run` command
29
30
  */
30
- execute(book: string_pipeline_url | string_filename | PipelineString, inputParameters: InputParameters, onProgress?: (taskProgress: TaskProgress) => Promisable<void>): Promise<PipelineExecutorResult>;
31
+ execute(book: string_pipeline_url | string_filename | PipelineString, inputParameters: InputParameters, onProgress?: (taskProgress: TaskProgress) => Promisable<void>): Promise<{
32
+ /**
33
+ * Simple result of the execution
34
+ */
35
+ result: string_parameter_value;
36
+ } & PipelineExecutorResult>;
31
37
  private executionTools;
32
38
  /**
33
39
  * Provides the tools automatically for the Node.js environment
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/node",
3
- "version": "0.84.0-11",
3
+ "version": "0.84.0-13",
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-11"
57
+ "@promptbook/core": "0.84.0-13"
58
58
  },
59
59
  "dependencies": {
60
60
  "colors": "1.4.0",
package/umd/index.umd.js CHANGED
@@ -1,8 +1,8 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('colors'), require('fs/promises'), require('path'), require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('waitasecond'), require('papaparse'), require('crypto-js'), require('crypto-js/enc-hex'), require('mime-types'), require('child_process'), require('dotenv'), require('crypto-js/sha256')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'colors', 'fs/promises', 'path', 'spacetrim', 'prettier', 'prettier/parser-html', 'waitasecond', 'papaparse', 'crypto-js', 'crypto-js/enc-hex', 'mime-types', 'child_process', 'dotenv', 'crypto-js/sha256'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-node"] = {}, global.colors, global.promises, global.path, global.spaceTrim, global.prettier, global.parserHtml, global.waitasecond, global.papaparse, global.cryptoJs, global.hexEncoder, global.mimeTypes, global.child_process, global.dotenv, global.sha256));
5
- })(this, (function (exports, colors, promises, path, spaceTrim, prettier, parserHtml, waitasecond, papaparse, cryptoJs, hexEncoder, mimeTypes, child_process, dotenv, sha256) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('colors'), require('fs/promises'), require('path'), require('spacetrim'), require('prettier'), require('prettier/parser-html'), require('waitasecond'), require('papaparse'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('crypto-js'), require('mime-types'), require('child_process'), require('dotenv')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'colors', 'fs/promises', 'path', 'spacetrim', 'prettier', 'prettier/parser-html', 'waitasecond', 'papaparse', 'crypto-js/enc-hex', 'crypto-js/sha256', 'crypto-js', 'mime-types', 'child_process', 'dotenv'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-node"] = {}, global.colors, global.promises, global.path, global.spaceTrim, global.prettier, global.parserHtml, global.waitasecond, global.papaparse, global.hexEncoder, global.sha256, global.cryptoJs, global.mimeTypes, global.child_process, global.dotenv));
5
+ })(this, (function (exports, colors, promises, path, spaceTrim, prettier, parserHtml, waitasecond, papaparse, hexEncoder, sha256, cryptoJs, mimeTypes, child_process, dotenv) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -28,8 +28,8 @@
28
28
  var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
29
29
  var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
30
30
  var hexEncoder__default = /*#__PURE__*/_interopDefaultLegacy(hexEncoder);
31
- var dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
32
31
  var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256);
32
+ var dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
33
33
 
34
34
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
35
35
  /**
@@ -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-10';
48
+ var PROMPTBOOK_ENGINE_VERSION = '0.84.0-12';
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
@@ -280,6 +280,15 @@
280
280
  * @public exported from `@promptbook/core`
281
281
  */
282
282
  var DEFAULT_MAX_EXECUTION_ATTEMPTS = 3; // <- TODO: [🤹‍♂️]
283
+ // <- TODO: [🕝] Make also `BOOKS_DIRNAME_ALTERNATIVES`
284
+ /**
285
+ * Where to store the temporary downloads
286
+ *
287
+ * Note: When the folder does not exist, it is created recursively
288
+ *
289
+ * @public exported from `@promptbook/core`
290
+ */
291
+ var DEFAULT_DOWNLOAD_CACHE_DIRNAME = './.promptbook/download-cache';
283
292
  /**
284
293
  * Where to store the scrape cache
285
294
  *
@@ -5414,6 +5423,15 @@
5414
5423
  * TODO: [🐱‍🐉][🧠] Make some smart crop NOT source-i-m-pavol-a-develop-... BUT source-i-m-pavol-a-developer-...
5415
5424
  */
5416
5425
 
5426
+ /**
5427
+ * @@@
5428
+ *
5429
+ * @private for `FileCacheStorage`
5430
+ */
5431
+ function nameToSubfolderPath(name) {
5432
+ return [name.substr(0, 1).toLowerCase(), name.substr(1, 1).toLowerCase()];
5433
+ }
5434
+
5417
5435
  /**
5418
5436
  * Convert file extension to mime type
5419
5437
  *
@@ -5469,6 +5487,46 @@
5469
5487
  * TODO: [🖇] What about symlinks?
5470
5488
  */
5471
5489
 
5490
+ /**
5491
+ * Removes emojis from a string and fix whitespaces
5492
+ *
5493
+ * @param text with emojis
5494
+ * @returns text without emojis
5495
+ * @public exported from `@promptbook/utils`
5496
+ */
5497
+ function removeEmojis(text) {
5498
+ // Replace emojis (and also ZWJ sequence) with hyphens
5499
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
5500
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
5501
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
5502
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
5503
+ return text;
5504
+ }
5505
+
5506
+ /**
5507
+ * @@@
5508
+ *
5509
+ * @param value @@@
5510
+ * @returns @@@
5511
+ * @example @@@
5512
+ * @public exported from `@promptbook/utils`
5513
+ */
5514
+ function titleToName(value) {
5515
+ if (isValidUrl(value)) {
5516
+ value = value.replace(/^https?:\/\//, '');
5517
+ value = value.replace(/\.html$/, '');
5518
+ }
5519
+ else if (isValidFilePath(value)) {
5520
+ value = path.basename(value);
5521
+ // Note: Keeping extension in the name
5522
+ }
5523
+ value = value.split('/').join('-');
5524
+ value = removeEmojis(value);
5525
+ value = normalizeToKebabCase(value);
5526
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
5527
+ return value;
5528
+ }
5529
+
5472
5530
  /**
5473
5531
  * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
5474
5532
  *
@@ -5504,10 +5562,11 @@
5504
5562
  function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
5505
5563
  var _a;
5506
5564
  return __awaiter(this, void 0, void 0, function () {
5507
- var _b, fetch, knowledgeSourceContent, name, _c, _d, rootDirname, url, response_1, mimeType, filename_1, fileExtension, mimeType;
5508
- return __generator(this, function (_f) {
5509
- switch (_f.label) {
5565
+ 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
+ return __generator(this, function (_l) {
5567
+ switch (_l.label) {
5510
5568
  case 0:
5569
+ console.log('!!! makeKnowledgeSourceHandler', knowledgeSource);
5511
5570
  _b = tools.fetch, fetch = _b === void 0 ? scraperFetch : _b;
5512
5571
  knowledgeSourceContent = knowledgeSource.knowledgeSourceContent;
5513
5572
  name = knowledgeSource.name;
@@ -5515,54 +5574,74 @@
5515
5574
  if (!name) {
5516
5575
  name = knowledgeSourceContentToName(knowledgeSourceContent);
5517
5576
  }
5518
- if (!isValidUrl(knowledgeSourceContent)) return [3 /*break*/, 2];
5577
+ if (!isValidUrl(knowledgeSourceContent)) return [3 /*break*/, 5];
5519
5578
  url = knowledgeSourceContent;
5520
5579
  return [4 /*yield*/, fetch(url)];
5521
5580
  case 1:
5522
- response_1 = _f.sent();
5581
+ response_1 = _l.sent();
5523
5582
  mimeType = ((_a = response_1.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]) || 'text/html';
5524
- return [2 /*return*/, {
5525
- source: name,
5526
- filename: null,
5527
- url: url,
5528
- mimeType: mimeType,
5529
- /*
5530
- TODO: [🥽]
5531
- > async asBlob() {
5532
- > // TODO: [👨🏻‍🤝‍👨🏻] This can be called multiple times BUT when called second time, response in already consumed
5533
- > const content = await response.blob();
5534
- > return content;
5535
- > },
5536
- */
5537
- asJson: function () {
5538
- return __awaiter(this, void 0, void 0, function () {
5539
- var content;
5540
- return __generator(this, function (_a) {
5541
- switch (_a.label) {
5542
- case 0: return [4 /*yield*/, response_1.json()];
5543
- case 1:
5544
- content = _a.sent();
5545
- return [2 /*return*/, content];
5546
- }
5583
+ if (tools.fs === undefined || !url.endsWith('.pdf')) {
5584
+ return [2 /*return*/, {
5585
+ source: name,
5586
+ filename: null,
5587
+ url: url,
5588
+ mimeType: mimeType,
5589
+ /*
5590
+ TODO: [🥽]
5591
+ > async asBlob() {
5592
+ > // TODO: [👨🏻‍🤝‍👨🏻] This can be called multiple times BUT when called second time, response in already consumed
5593
+ > const content = await response.blob();
5594
+ > return content;
5595
+ > },
5596
+ */
5597
+ asJson: function () {
5598
+ return __awaiter(this, void 0, void 0, function () {
5599
+ var content;
5600
+ return __generator(this, function (_a) {
5601
+ switch (_a.label) {
5602
+ case 0: return [4 /*yield*/, response_1.json()];
5603
+ case 1:
5604
+ content = _a.sent();
5605
+ return [2 /*return*/, content];
5606
+ }
5607
+ });
5547
5608
  });
5548
- });
5549
- },
5550
- asText: function () {
5551
- return __awaiter(this, void 0, void 0, function () {
5552
- var content;
5553
- return __generator(this, function (_a) {
5554
- switch (_a.label) {
5555
- case 0: return [4 /*yield*/, response_1.text()];
5556
- case 1:
5557
- content = _a.sent();
5558
- return [2 /*return*/, content];
5559
- }
5609
+ },
5610
+ asText: function () {
5611
+ return __awaiter(this, void 0, void 0, function () {
5612
+ var content;
5613
+ return __generator(this, function (_a) {
5614
+ switch (_a.label) {
5615
+ case 0: return [4 /*yield*/, response_1.text()];
5616
+ case 1:
5617
+ content = _a.sent();
5618
+ return [2 /*return*/, content];
5619
+ }
5620
+ });
5560
5621
  });
5561
- });
5562
- },
5563
- }];
5622
+ },
5623
+ }];
5624
+ }
5625
+ basename = url.split('/').pop() || titleToName(url);
5626
+ hash = sha256__default["default"](hexEncoder__default["default"].parse(url)).toString( /* hex */);
5627
+ rootDirname_1 = path.join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
5628
+ 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), ".pdf")], false));
5629
+ return [4 /*yield*/, tools.fs.mkdir(path.dirname(path.join(rootDirname_1, filepath)), { recursive: true })];
5564
5630
  case 2:
5565
- if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 4];
5631
+ _l.sent();
5632
+ _g = (_f = tools.fs).writeFile;
5633
+ _h = [path.join(rootDirname_1, filepath)];
5634
+ _k = (_j = Buffer).from;
5635
+ return [4 /*yield*/, response_1.arrayBuffer()];
5636
+ case 3: return [4 /*yield*/, _g.apply(_f, _h.concat([_k.apply(_j, [_l.sent()])]))];
5637
+ case 4:
5638
+ _l.sent();
5639
+ // TODO: !!!!!!!! Check the file security
5640
+ // TODO: !!!!!!!! Check the file size (if it is not too big)
5641
+ // TODO: !!!!!!!! Delete the file
5642
+ return [2 /*return*/, makeKnowledgeSourceHandler({ name: name, knowledgeSourceContent: filepath }, tools, __assign(__assign({}, options), { rootDirname: rootDirname_1 }))];
5643
+ case 5:
5644
+ if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 7];
5566
5645
  if (tools.fs === undefined) {
5567
5646
  throw new EnvironmentMismatchError('Can not import file knowledge without filesystem tools');
5568
5647
  // <- TODO: [🧠] What is the best error type here`
@@ -5575,8 +5654,8 @@
5575
5654
  fileExtension = getFileExtension(filename_1);
5576
5655
  mimeType = extensionToMimeType(fileExtension || '');
5577
5656
  return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
5578
- case 3:
5579
- if (!(_f.sent())) {
5657
+ case 6:
5658
+ if (!(_l.sent())) {
5580
5659
  throw new NotFoundError(spaceTrim__default["default"](function (block) { return "\n Can not make source handler for file which does not exist:\n\n File:\n ".concat(block(knowledgeSourceContent), "\n\n Full file path:\n ").concat(block(filename_1), "\n "); }));
5581
5660
  }
5582
5661
  // TODO: [🧠][😿] Test security file - file is scoped to the project (BUT maybe do this in `filesystemTools`)
@@ -5622,7 +5701,7 @@
5622
5701
  });
5623
5702
  },
5624
5703
  }];
5625
- case 4: return [2 /*return*/, {
5704
+ case 7: return [2 /*return*/, {
5626
5705
  source: name,
5627
5706
  filename: null,
5628
5707
  url: null,
@@ -6733,22 +6812,6 @@
6733
6812
  * TODO: [🌺] Use some intermediate util splitWords
6734
6813
  */
6735
6814
 
6736
- /**
6737
- * Removes emojis from a string and fix whitespaces
6738
- *
6739
- * @param text with emojis
6740
- * @returns text without emojis
6741
- * @public exported from `@promptbook/utils`
6742
- */
6743
- function removeEmojis(text) {
6744
- // Replace emojis (and also ZWJ sequence) with hyphens
6745
- text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
6746
- text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
6747
- text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
6748
- text = text.replace(/\p{Extended_Pictographic}/gu, '');
6749
- return text;
6750
- }
6751
-
6752
6815
  /**
6753
6816
  * Removes quotes from a string
6754
6817
  *
@@ -8940,30 +9003,6 @@
8940
9003
  * NOW we are working just with markdown string and its good enough
8941
9004
  */
8942
9005
 
8943
- /**
8944
- * @@@
8945
- *
8946
- * @param value @@@
8947
- * @returns @@@
8948
- * @example @@@
8949
- * @public exported from `@promptbook/utils`
8950
- */
8951
- function titleToName(value) {
8952
- if (isValidUrl(value)) {
8953
- value = value.replace(/^https?:\/\//, '');
8954
- value = value.replace(/\.html$/, '');
8955
- }
8956
- else if (isValidFilePath(value)) {
8957
- value = path.basename(value);
8958
- // Note: Keeping extension in the name
8959
- }
8960
- value = value.split('/').join('-');
8961
- value = removeEmojis(value);
8962
- value = normalizeToKebabCase(value);
8963
- // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
8964
- return value;
8965
- }
8966
-
8967
9006
  /**
8968
9007
  * Compile pipeline from string (markdown) format to JSON format synchronously
8969
9008
  *
@@ -9696,6 +9735,7 @@
9696
9735
  readFile: promises.readFile,
9697
9736
  writeFile: promises.writeFile,
9698
9737
  readdir: promises.readdir,
9738
+ mkdir: promises.mkdir,
9699
9739
  };
9700
9740
  }
9701
9741
  /**
@@ -11314,15 +11354,6 @@
11314
11354
  * TODO: [🍙] Make some standard order of json properties
11315
11355
  */
11316
11356
 
11317
- /**
11318
- * @@@
11319
- *
11320
- * @private for `FileCacheStorage`
11321
- */
11322
- function nameToSubfolderPath(name) {
11323
- return [name.substr(0, 1).toLowerCase(), name.substr(1, 1).toLowerCase()];
11324
- }
11325
-
11326
11357
  /**
11327
11358
  * @@@
11328
11359
  *