@promptbook/node 0.71.0-13 → 0.71.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.
Files changed (29) hide show
  1. package/esm/index.es.js +89 -79
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/node.index.d.ts +2 -0
  4. package/esm/typings/src/_packages/types.index.d.ts +2 -0
  5. package/esm/typings/src/collection/constructors/createCollectionFromDirectory.d.ts +2 -2
  6. package/esm/typings/src/conversion/pipelineStringToJson.d.ts +1 -1
  7. package/esm/typings/src/execution/ExecutionTools.d.ts +12 -2
  8. package/esm/typings/src/execution/FilesystemTools.d.ts +9 -0
  9. package/esm/typings/src/execution/translation/automatic-translate/translateMessages.d.ts +1 -0
  10. package/esm/typings/src/prepare/preparePipeline.d.ts +1 -1
  11. package/esm/typings/src/prepare/prepareTemplates.d.ts +1 -1
  12. package/esm/typings/src/scrapers/_common/prepareKnowledgePieces.d.ts +1 -1
  13. package/esm/typings/src/scrapers/_common/register/$provideFilesystemForNode.d.ts +11 -0
  14. package/esm/typings/src/scrapers/_common/utils/getScraperIntermediateSource.d.ts +1 -0
  15. package/esm/typings/src/scrapers/_common/utils/makeKnowledgeSourceHandler.d.ts +2 -4
  16. package/esm/typings/src/scrapers/document/DocumentScraper.d.ts +1 -1
  17. package/esm/typings/src/scrapers/document-legacy/LegacyDocumentScraper.d.ts +1 -1
  18. package/esm/typings/src/scrapers/website/WebsiteScraper.d.ts +1 -1
  19. package/esm/typings/src/storage/file-cache-storage/FileCacheStorage.d.ts +3 -1
  20. package/esm/typings/src/utils/files/{$isDirectoryExisting.d.ts → isDirectoryExisting.d.ts} +3 -4
  21. package/esm/typings/src/utils/files/isFileExisting.d.ts +13 -0
  22. package/esm/typings/src/utils/files/{$listAllFiles.d.ts → listAllFiles.d.ts} +3 -4
  23. package/package.json +2 -2
  24. package/umd/index.umd.js +88 -77
  25. package/umd/index.umd.js.map +1 -1
  26. package/esm/typings/src/utils/files/$isFileExisting.d.ts +0 -14
  27. /package/esm/typings/src/utils/files/{$isDirectoryExisting.test.d.ts → isDirectoryExisting.test.d.ts} +0 -0
  28. /package/esm/typings/src/utils/files/{$isFileExisting.test.d.ts → isFileExisting.test.d.ts} +0 -0
  29. /package/esm/typings/src/utils/files/{$listAllFiles.test.d.ts → listAllFiles.test.d.ts} +0 -0
package/umd/index.umd.js CHANGED
@@ -35,7 +35,7 @@
35
35
  /**
36
36
  * The version of the Promptbook library
37
37
  */
38
- var PROMPTBOOK_VERSION = '0.71.0-12';
38
+ var PROMPTBOOK_VERSION = '0.71.0-13';
39
39
  // TODO: [main] !!!! List here all the versions and annotate + put into script
40
40
 
41
41
  /*! *****************************************************************************
@@ -4948,39 +4948,45 @@
4948
4948
  */
4949
4949
 
4950
4950
  /**
4951
- * Detects if the code is running in a Node.js environment
4951
+ * Convert file extension to mime type
4952
4952
  *
4953
- * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
4953
+ * @private within the repository
4954
+ */
4955
+ function extensionToMimeType(value) {
4956
+ return mimeTypes.lookup(value) || 'application/octet-stream';
4957
+ }
4958
+
4959
+ /**
4960
+ * Get the file extension from a file name
4954
4961
  *
4955
- * @public exported from `@promptbook/utils`
4962
+ * @private within the repository
4956
4963
  */
4957
- var $isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
4964
+ function getFileExtension(value) {
4965
+ var match = value.match(/\.([0-9a-z]+)(?:[?#]|$)/i);
4966
+ return match ? match[1].toLowerCase() : null;
4967
+ }
4958
4968
 
4959
4969
  /**
4960
4970
  * Checks if the file exists
4961
4971
  *
4962
- * Note: `$` is used to indicate that this function is not a pure function - it looks at the filesystem
4963
- *
4964
4972
  * @private within the repository
4965
4973
  */
4966
- function $isFileExisting(filename) {
4974
+ function isFileExisting(filename, fs) {
4967
4975
  return __awaiter(this, void 0, void 0, function () {
4968
4976
  var isReadAccessAllowed, isFile;
4969
4977
  return __generator(this, function (_a) {
4970
4978
  switch (_a.label) {
4971
- case 0:
4972
- if (!$isRunningInNode()) {
4973
- throw new EnvironmentMismatchError('Function `$isFileExisting` works only in Node environment.js');
4974
- }
4975
- return [4 /*yield*/, promises.access(filename, promises.constants.R_OK)
4976
- .then(function () { return true; })
4977
- .catch(function () { return false; })];
4979
+ case 0: return [4 /*yield*/, fs
4980
+ .access(filename, fs.constants.R_OK)
4981
+ .then(function () { return true; })
4982
+ .catch(function () { return false; })];
4978
4983
  case 1:
4979
4984
  isReadAccessAllowed = _a.sent();
4980
4985
  if (!isReadAccessAllowed) {
4981
4986
  return [2 /*return*/, false];
4982
4987
  }
4983
- return [4 /*yield*/, promises.stat(filename)
4988
+ return [4 /*yield*/, fs
4989
+ .stat(filename)
4984
4990
  .then(function (fileStat) { return fileStat.isFile(); })
4985
4991
  .catch(function () { return false; })];
4986
4992
  case 2:
@@ -4991,30 +4997,11 @@
4991
4997
  });
4992
4998
  }
4993
4999
  /**
4994
- * Note: [🟢 !!!!!! After fix makeKnowledgeSourceHandler] Code in this file should never be published outside of `@promptbook/node` and `@promptbook/cli`
5000
+ * Note: Not [~🟢~] because it is not directly dependent on `fs
4995
5001
  * TODO: [🐠] This can be a validator - with variants that return true/false and variants that throw errors with meaningless messages
4996
5002
  * TODO: [🖇] What about symlinks?
4997
5003
  */
4998
5004
 
4999
- /**
5000
- * Convert file extension to mime type
5001
- *
5002
- * @private within the repository
5003
- */
5004
- function extensionToMimeType(value) {
5005
- return mimeTypes.lookup(value) || 'application/octet-stream';
5006
- }
5007
-
5008
- /**
5009
- * Get the file extension from a file name
5010
- *
5011
- * @private within the repository
5012
- */
5013
- function getFileExtension(value) {
5014
- var match = value.match(/\.([0-9a-z]+)(?:[?#]|$)/i);
5015
- return match ? match[1].toLowerCase() : null;
5016
- }
5017
-
5018
5005
  /**
5019
5006
  * Tests if given string is valid URL.
5020
5007
  *
@@ -5046,7 +5033,7 @@
5046
5033
  *
5047
5034
  * @private for scraper utilities
5048
5035
  */
5049
- function makeKnowledgeSourceHandler(knowledgeSource, options) {
5036
+ function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
5050
5037
  var _a;
5051
5038
  return __awaiter(this, void 0, void 0, function () {
5052
5039
  var sourceContent, name, _b, _c, rootDirname, _d,
@@ -5115,8 +5102,9 @@
5115
5102
  }];
5116
5103
  case 2:
5117
5104
  if (!(isValidFilePath(sourceContent) || /\.[a-z]{1,10}$/i.exec(sourceContent))) return [3 /*break*/, 4];
5118
- if (!$isRunningInNode()) {
5119
- throw new EnvironmentMismatchError('Importing knowledge source file works only in Node.js environment');
5105
+ if (tools.fs === undefined) {
5106
+ throw new EnvironmentMismatchError('Can not import file knowledge without filesystem tools');
5107
+ // <- TODO: [🧠] What is the best error type here`
5120
5108
  }
5121
5109
  if (rootDirname === null) {
5122
5110
  throw new EnvironmentMismatchError('Can not import file knowledge in non-file pipeline');
@@ -5125,7 +5113,7 @@
5125
5113
  filename_1 = path.join(rootDirname, sourceContent).split('\\').join('/');
5126
5114
  fileExtension = getFileExtension(filename_1);
5127
5115
  mimeType_1 = extensionToMimeType(fileExtension || '');
5128
- return [4 /*yield*/, $isFileExisting(filename_1)];
5116
+ return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
5129
5117
  case 3:
5130
5118
  if (!(_e.sent())) {
5131
5119
  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(filename_1), "\n "); }));
@@ -5141,7 +5129,7 @@
5141
5129
  var content;
5142
5130
  return __generator(this, function (_a) {
5143
5131
  switch (_a.label) {
5144
- case 0: return [4 /*yield*/, promises.readFile(filename_1)];
5132
+ case 0: return [4 /*yield*/, tools.fs.readFile(filename_1)];
5145
5133
  case 1:
5146
5134
  content = _a.sent();
5147
5135
  return [2 /*return*/, new Blob([
@@ -5195,9 +5183,6 @@
5195
5183
  });
5196
5184
  });
5197
5185
  }
5198
- /**
5199
- * TODO: !!!!!!! Maybe constrain to @promptbook/node bundle
5200
- */
5201
5186
 
5202
5187
  /**
5203
5188
  * Prepares the knowle
@@ -5221,7 +5206,7 @@
5221
5206
  switch (_d.label) {
5222
5207
  case 0:
5223
5208
  partialPieces = null;
5224
- return [4 /*yield*/, makeKnowledgeSourceHandler(knowledgeSource, { rootDirname: rootDirname, isVerbose: isVerbose })];
5209
+ return [4 /*yield*/, makeKnowledgeSourceHandler(knowledgeSource, tools, { rootDirname: rootDirname, isVerbose: isVerbose })];
5225
5210
  case 1:
5226
5211
  sourceHandler = _d.sent();
5227
5212
  _d.label = 2;
@@ -8222,6 +8207,15 @@
8222
8207
  * TODO: [🧠] Should be in generated JSON file GENERATOR_WARNING
8223
8208
  */
8224
8209
 
8210
+ /**
8211
+ * Detects if the code is running in a Node.js environment
8212
+ *
8213
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
8214
+ *
8215
+ * @public exported from `@promptbook/utils`
8216
+ */
8217
+ var $isRunningInNode = new Function("\n try {\n return this === global;\n } catch (e) {\n return false;\n }\n");
8218
+
8225
8219
  /**
8226
8220
  * @@@
8227
8221
  *
@@ -8453,6 +8447,29 @@
8453
8447
  * TODO: [🍂] Maybe make llm = $provideLlmToolsFromEnv() without problem with bundle contaminated by only `@promptbook/node` and `@promptbook/cli` stuff
8454
8448
  */
8455
8449
 
8450
+ /**
8451
+ * @@@
8452
+ *
8453
+ * @public exported from `@promptbook/node`
8454
+ */
8455
+ function $provideFilesystemForNode(options) {
8456
+ if (!$isRunningInNode()) {
8457
+ throw new EnvironmentMismatchError('Function `$provideFilesystemForNode` works only in Node.js environment');
8458
+ }
8459
+ var _a = (options || {}).isVerbose, isVerbose = _a === void 0 ? IS_VERBOSE : _a;
8460
+ TODO_USE(isVerbose);
8461
+ return {
8462
+ stat: promises.stat,
8463
+ access: promises.access,
8464
+ constants: promises.constants,
8465
+ readFile: promises.readFile,
8466
+ readdir: promises.readdir,
8467
+ };
8468
+ }
8469
+ /**
8470
+ * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
8471
+ */
8472
+
8456
8473
  /**
8457
8474
  * !!!!!!
8458
8475
  *
@@ -8975,7 +8992,8 @@
8975
8992
  }
8976
8993
  llm = $provideLlmToolsFromEnv(options);
8977
8994
  _a = {
8978
- llm: llm
8995
+ llm: llm,
8996
+ fs: $provideFilesystemForNode()
8979
8997
  };
8980
8998
  return [4 /*yield*/, $provideScrapersForNode({ llm: llm }, options)];
8981
8999
  case 1:
@@ -8994,28 +9012,24 @@
8994
9012
  /**
8995
9013
  * Checks if the directory exists
8996
9014
  *
8997
- * Note: `$` is used to indicate that this function is not a pure function - it looks at the filesystem
8998
- *
8999
9015
  * @private within the repository
9000
9016
  */
9001
- function $isDirectoryExisting(directoryPath) {
9017
+ function isDirectoryExisting(directoryPath, fs) {
9002
9018
  return __awaiter(this, void 0, void 0, function () {
9003
9019
  var isReadAccessAllowed, isDirectory;
9004
9020
  return __generator(this, function (_a) {
9005
9021
  switch (_a.label) {
9006
- case 0:
9007
- if (!$isRunningInNode()) {
9008
- throw new EnvironmentMismatchError('Function `$isDirectoryExisting` works only in Node environment.js');
9009
- }
9010
- return [4 /*yield*/, promises.access(directoryPath, promises.constants.R_OK)
9011
- .then(function () { return true; })
9012
- .catch(function () { return false; })];
9022
+ case 0: return [4 /*yield*/, fs
9023
+ .access(directoryPath, fs.constants.R_OK)
9024
+ .then(function () { return true; })
9025
+ .catch(function () { return false; })];
9013
9026
  case 1:
9014
9027
  isReadAccessAllowed = _a.sent();
9015
9028
  if (!isReadAccessAllowed) {
9016
9029
  return [2 /*return*/, false];
9017
9030
  }
9018
- return [4 /*yield*/, promises.stat(directoryPath)
9031
+ return [4 /*yield*/, fs
9032
+ .stat(directoryPath)
9019
9033
  .then(function (fileStat) { return fileStat.isDirectory(); })
9020
9034
  .catch(function () { return false; })];
9021
9035
  case 2:
@@ -9026,7 +9040,7 @@
9026
9040
  });
9027
9041
  }
9028
9042
  /**
9029
- * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
9043
+ * Note: Not [~🟢~] because it is not directly dependent on `fs
9030
9044
  * TODO: [🐠] This can be a validator - with variants that return true/false and variants that throw errors with meaningless messages
9031
9045
  * TODO: [🧠][📂] "directory" vs "folder"
9032
9046
  * TODO: [🖇] What about symlinks?
@@ -9035,30 +9049,24 @@
9035
9049
  /**
9036
9050
  * Reads all files in the directory
9037
9051
  *
9038
- * Note: `$` is used to indicate that this function is not a pure function - it looks at the filesystem
9039
- *
9040
9052
  * @param path
9041
9053
  * @param isRecursive
9042
9054
  * @returns List of all files in the directory
9043
9055
  * @private internal function of `createCollectionFromDirectory`
9044
9056
  */
9045
- function $listAllFiles(path$1, isRecursive) {
9057
+ function listAllFiles(path$1, isRecursive, fs) {
9046
9058
  return __awaiter(this, void 0, void 0, function () {
9047
9059
  var dirents, fileNames, _a, _b, dirent, subPath, _c, _d, _e, _f, e_1_1;
9048
9060
  var e_1, _g;
9049
9061
  return __generator(this, function (_h) {
9050
9062
  switch (_h.label) {
9051
- case 0:
9052
- if (!$isRunningInNode()) {
9053
- throw new EnvironmentMismatchError('Function `$listAllFiles` works only in Node environment.js');
9054
- }
9055
- return [4 /*yield*/, $isDirectoryExisting(path$1)];
9063
+ case 0: return [4 /*yield*/, isDirectoryExisting(path$1, fs)];
9056
9064
  case 1:
9057
9065
  if (!(_h.sent())) {
9058
9066
  throw new Error("Directory \"".concat(path$1, "\" does not exist or is not readable"));
9059
9067
  // <- TODO: Use some custom error class
9060
9068
  }
9061
- return [4 /*yield*/, promises.readdir(path$1, {
9069
+ return [4 /*yield*/, fs.readdir(path$1, {
9062
9070
  withFileTypes: true /* Note: This is not working: recursive: isRecursive */,
9063
9071
  })];
9064
9072
  case 2:
@@ -9082,7 +9090,7 @@
9082
9090
  _d = (_c = fileNames.push).apply;
9083
9091
  _e = [fileNames];
9084
9092
  _f = [[]];
9085
- return [4 /*yield*/, $listAllFiles(subPath, isRecursive)];
9093
+ return [4 /*yield*/, listAllFiles(subPath, isRecursive, fs)];
9086
9094
  case 5:
9087
9095
  _d.apply(_c, _e.concat([__spreadArray.apply(void 0, _f.concat([__read.apply(void 0, [(_h.sent()).map(function (filename) { return filename; })]), false]))]));
9088
9096
  _h.label = 6;
@@ -9107,7 +9115,7 @@
9107
9115
  }
9108
9116
  /**
9109
9117
  * TODO: [😶] Unite floder listing
9110
- * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
9118
+ * Note: Not [~🟢~] because it is not directly dependent on `fs
9111
9119
  * TODO: [🖇] What about symlinks?
9112
9120
  */
9113
9121
 
@@ -9216,17 +9224,18 @@
9216
9224
  return __generator(this, function (_f) {
9217
9225
  switch (_f.label) {
9218
9226
  case 0:
9219
- if (!$isRunningInNode()) {
9220
- throw new Error('Function `createCollectionFromDirectory` can only be run in Node.js environment because it reads the file system.');
9221
- }
9222
9227
  if (!(tools === undefined)) return [3 /*break*/, 2];
9223
9228
  return [4 /*yield*/, $provideExecutionToolsForNode()];
9224
9229
  case 1:
9225
9230
  tools = _f.sent();
9226
9231
  _f.label = 2;
9227
9232
  case 2:
9233
+ if (tools === undefined || tools.fs === undefined) {
9234
+ throw new EnvironmentMismatchError('Can not create collection without filesystem tools');
9235
+ // <- TODO: [🧠] What is the best error type here`
9236
+ }
9228
9237
  makedLibraryFilePath = path.join(path$1, "".concat(PIPELINE_COLLECTION_BASE_FILENAME, ".json"));
9229
- return [4 /*yield*/, $isFileExisting(makedLibraryFilePath)];
9238
+ return [4 /*yield*/, isFileExisting(makedLibraryFilePath, tools.fs)];
9230
9239
  case 3:
9231
9240
  if (!(_f.sent())) {
9232
9241
  console.info(colors__default["default"].yellow("Tip: Prebuild your pipeline collection (file with supposed prebuild ".concat(makedLibraryFilePath, " not found) with CLI util \"ptbk make\" to speed up the collection creation.")));
@@ -9246,7 +9255,7 @@
9246
9255
  if (isVerbose) {
9247
9256
  console.info(colors__default["default"].cyan("Creating pipeline collection from path ".concat(path$1.split('\\').join('/'))));
9248
9257
  }
9249
- return [4 /*yield*/, $listAllFiles(path$1, isRecursive)];
9258
+ return [4 /*yield*/, listAllFiles(path$1, isRecursive, tools.fs)];
9250
9259
  case 1:
9251
9260
  fileNames = _b.sent();
9252
9261
  // Note: First load all .ptbk.json and then .ptbk.md files
@@ -9394,8 +9403,8 @@
9394
9403
  });
9395
9404
  }
9396
9405
  /**
9397
- * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
9398
9406
  * TODO: [🖇] What about symlinks? Maybe option isSymlinksFollowed
9407
+ * TODO: Maybe move from `@promptbook/node` to `@promptbook/core` as we removes direct dependency on `fs`
9399
9408
  */
9400
9409
 
9401
9410
  /**
@@ -9473,7 +9482,8 @@
9473
9482
  * @public exported from `@promptbook/node`
9474
9483
  */
9475
9484
  var FileCacheStorage = /** @class */ (function () {
9476
- function FileCacheStorage(options) {
9485
+ function FileCacheStorage(tools, options) {
9486
+ this.tools = tools;
9477
9487
  this.options = options;
9478
9488
  if (!$isRunningInNode()) {
9479
9489
  throw new EnvironmentMismatchError("FileCacheStorage works only in Node.js environment");
@@ -9499,7 +9509,7 @@
9499
9509
  switch (_a.label) {
9500
9510
  case 0:
9501
9511
  filename = this.getFilenameForKey(key);
9502
- return [4 /*yield*/, $isFileExisting(filename)];
9512
+ return [4 /*yield*/, isFileExisting(filename, this.tools.fs)];
9503
9513
  case 1:
9504
9514
  if (!(_a.sent())) {
9505
9515
  return [2 /*return*/, null];
@@ -9768,6 +9778,7 @@
9768
9778
  exports.$execCommand = $execCommand;
9769
9779
  exports.$execCommands = $execCommands;
9770
9780
  exports.$provideExecutionToolsForNode = $provideExecutionToolsForNode;
9781
+ exports.$provideFilesystemForNode = $provideFilesystemForNode;
9771
9782
  exports.$provideLlmToolsConfigurationFromEnv = $provideLlmToolsConfigurationFromEnv;
9772
9783
  exports.$provideLlmToolsFromEnv = $provideLlmToolsFromEnv;
9773
9784
  exports.$provideScrapersForNode = $provideScrapersForNode;