@promptbook/node 0.84.0-11 → 0.84.0-12

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-12",
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-12"
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-11';
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
  *
@@ -5331,6 +5340,22 @@
5331
5340
  * TODO: [®] DRY Register logic
5332
5341
  */
5333
5342
 
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
+
5334
5359
  /**
5335
5360
  * @@@
5336
5361
  *
@@ -5393,6 +5418,30 @@
5393
5418
  * Note: [💞] Ignore a discrepancy between file name and entity name
5394
5419
  */
5395
5420
 
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
+
5396
5445
  /**
5397
5446
  * Creates unique name for the source
5398
5447
  *
@@ -5414,6 +5463,15 @@
5414
5463
  * TODO: [🐱‍🐉][🧠] Make some smart crop NOT source-i-m-pavol-a-develop-... BUT source-i-m-pavol-a-developer-...
5415
5464
  */
5416
5465
 
5466
+ /**
5467
+ * @@@
5468
+ *
5469
+ * @private for `FileCacheStorage`
5470
+ */
5471
+ function nameToSubfolderPath(name) {
5472
+ return [name.substr(0, 1).toLowerCase(), name.substr(1, 1).toLowerCase()];
5473
+ }
5474
+
5417
5475
  /**
5418
5476
  * Convert file extension to mime type
5419
5477
  *
@@ -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, mimeType, filename, 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,32 @@
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();
5523
- 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
- }
5547
- });
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
- }
5560
- });
5561
- });
5562
- },
5563
- }];
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);
5584
+ hash = sha256__default["default"](hexEncoder__default["default"].parse(url)).toString( /* hex */);
5585
+ 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));
5587
+ return [4 /*yield*/, tools.fs.mkdir(path.dirname(path.join(rootDirname_1, filepath)), { recursive: true })];
5564
5588
  case 2:
5565
- if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 4];
5589
+ _l.sent();
5590
+ _g = (_f = tools.fs).writeFile;
5591
+ _h = [path.join(rootDirname_1, filepath)];
5592
+ _k = (_j = Buffer).from;
5593
+ return [4 /*yield*/, response.arrayBuffer()];
5594
+ case 3: return [4 /*yield*/, _g.apply(_f, _h.concat([_k.apply(_j, [_l.sent()])]))];
5595
+ case 4:
5596
+ _l.sent();
5597
+ // TODO: !!!!!!!! Check the file security
5598
+ // TODO: !!!!!!!! Check the file size (if it is not too big)
5599
+ // TODO: !!!!!!!! Delete the file
5600
+ return [2 /*return*/, makeKnowledgeSourceHandler({ name: name, knowledgeSourceContent: filepath }, tools, __assign(__assign({}, options), { rootDirname: rootDirname_1 }))];
5601
+ case 5:
5602
+ if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 7];
5566
5603
  if (tools.fs === undefined) {
5567
5604
  throw new EnvironmentMismatchError('Can not import file knowledge without filesystem tools');
5568
5605
  // <- TODO: [🧠] What is the best error type here`
@@ -5575,8 +5612,8 @@
5575
5612
  fileExtension = getFileExtension(filename_1);
5576
5613
  mimeType = extensionToMimeType(fileExtension || '');
5577
5614
  return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
5578
- case 3:
5579
- if (!(_f.sent())) {
5615
+ case 6:
5616
+ if (!(_l.sent())) {
5580
5617
  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
5618
  }
5582
5619
  // TODO: [🧠][😿] Test security file - file is scoped to the project (BUT maybe do this in `filesystemTools`)
@@ -5622,7 +5659,7 @@
5622
5659
  });
5623
5660
  },
5624
5661
  }];
5625
- case 4: return [2 /*return*/, {
5662
+ case 7: return [2 /*return*/, {
5626
5663
  source: name,
5627
5664
  filename: null,
5628
5665
  url: null,
@@ -6733,22 +6770,6 @@
6733
6770
  * TODO: [🌺] Use some intermediate util splitWords
6734
6771
  */
6735
6772
 
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
6773
  /**
6753
6774
  * Removes quotes from a string
6754
6775
  *
@@ -8940,30 +8961,6 @@
8940
8961
  * NOW we are working just with markdown string and its good enough
8941
8962
  */
8942
8963
 
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
8964
  /**
8968
8965
  * Compile pipeline from string (markdown) format to JSON format synchronously
8969
8966
  *
@@ -9696,6 +9693,7 @@
9696
9693
  readFile: promises.readFile,
9697
9694
  writeFile: promises.writeFile,
9698
9695
  readdir: promises.readdir,
9696
+ mkdir: promises.mkdir,
9699
9697
  };
9700
9698
  }
9701
9699
  /**
@@ -11314,15 +11312,6 @@
11314
11312
  * TODO: [🍙] Make some standard order of json properties
11315
11313
  */
11316
11314
 
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
11315
  /**
11327
11316
  * @@@
11328
11317
  *