@promptbook/remote-server 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.
package/esm/index.es.js CHANGED
@@ -4,13 +4,14 @@ import { Server } from 'socket.io';
4
4
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
5
5
  import { spawn } from 'child_process';
6
6
  import { forTime } from 'waitasecond';
7
- import { stat, access, constants, readFile, writeFile, readdir } from 'fs/promises';
8
- import { join } from 'path';
7
+ import { stat, access, constants, readFile, writeFile, readdir, mkdir } from 'fs/promises';
8
+ import { join, basename, dirname } from 'path';
9
9
  import { format } from 'prettier';
10
10
  import parserHtml from 'prettier/parser-html';
11
11
  import { unparse, parse } from 'papaparse';
12
- import { SHA256 } from 'crypto-js';
13
12
  import hexEncoder from 'crypto-js/enc-hex';
13
+ import sha256 from 'crypto-js/sha256';
14
+ import { SHA256 } from 'crypto-js';
14
15
  import { lookup } from 'mime-types';
15
16
 
16
17
  // ⚠️ WARNING: This code has been generated so that any manual changes will be overwritten
@@ -27,7 +28,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
27
28
  * @generated
28
29
  * @see https://github.com/webgptorg/promptbook
29
30
  */
30
- var PROMPTBOOK_ENGINE_VERSION = '0.84.0-10';
31
+ var PROMPTBOOK_ENGINE_VERSION = '0.84.0-12';
31
32
  /**
32
33
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
33
34
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -231,6 +232,12 @@ var SMALL_NUMBER = 0.001;
231
232
  * @private within the repository - too low-level in comparison with other `MAX_...`
232
233
  */
233
234
  var IMMEDIATE_TIME = 10;
235
+ /**
236
+ * The maximum length of the (generated) filename
237
+ *
238
+ * @public exported from `@promptbook/core`
239
+ */
240
+ var MAX_FILENAME_LENGTH = 30;
234
241
  /**
235
242
  * Strategy for caching the intermediate results for knowledge sources
236
243
  *
@@ -250,6 +257,15 @@ var DEFAULT_MAX_PARALLEL_COUNT = 5; // <- TODO: [🤹‍♂️]
250
257
  * @public exported from `@promptbook/core`
251
258
  */
252
259
  var DEFAULT_MAX_EXECUTION_ATTEMPTS = 3; // <- TODO: [🤹‍♂️]
260
+ // <- TODO: [🕝] Make also `BOOKS_DIRNAME_ALTERNATIVES`
261
+ /**
262
+ * Where to store the temporary downloads
263
+ *
264
+ * Note: When the folder does not exist, it is created recursively
265
+ *
266
+ * @public exported from `@promptbook/core`
267
+ */
268
+ var DEFAULT_DOWNLOAD_CACHE_DIRNAME = './.promptbook/download-cache';
253
269
  /**
254
270
  * Where to store the scrape cache
255
271
  *
@@ -858,6 +874,7 @@ function $provideFilesystemForNode(options) {
858
874
  readFile: readFile,
859
875
  writeFile: writeFile,
860
876
  readdir: readdir,
877
+ mkdir: mkdir,
861
878
  };
862
879
  }
863
880
  /**
@@ -6065,6 +6082,15 @@ function knowledgeSourceContentToName(knowledgeSourceContent) {
6065
6082
  * TODO: [🐱‍🐉][🧠] Make some smart crop NOT source-i-m-pavol-a-develop-... BUT source-i-m-pavol-a-developer-...
6066
6083
  */
6067
6084
 
6085
+ /**
6086
+ * @@@
6087
+ *
6088
+ * @private for `FileCacheStorage`
6089
+ */
6090
+ function nameToSubfolderPath(name) {
6091
+ return [name.substr(0, 1).toLowerCase(), name.substr(1, 1).toLowerCase()];
6092
+ }
6093
+
6068
6094
  /**
6069
6095
  * Convert file extension to mime type
6070
6096
  *
@@ -6120,6 +6146,46 @@ function isFileExisting(filename, fs) {
6120
6146
  * TODO: [🖇] What about symlinks?
6121
6147
  */
6122
6148
 
6149
+ /**
6150
+ * Removes emojis from a string and fix whitespaces
6151
+ *
6152
+ * @param text with emojis
6153
+ * @returns text without emojis
6154
+ * @public exported from `@promptbook/utils`
6155
+ */
6156
+ function removeEmojis(text) {
6157
+ // Replace emojis (and also ZWJ sequence) with hyphens
6158
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
6159
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
6160
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
6161
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
6162
+ return text;
6163
+ }
6164
+
6165
+ /**
6166
+ * @@@
6167
+ *
6168
+ * @param value @@@
6169
+ * @returns @@@
6170
+ * @example @@@
6171
+ * @public exported from `@promptbook/utils`
6172
+ */
6173
+ function titleToName(value) {
6174
+ if (isValidUrl(value)) {
6175
+ value = value.replace(/^https?:\/\//, '');
6176
+ value = value.replace(/\.html$/, '');
6177
+ }
6178
+ else if (isValidFilePath(value)) {
6179
+ value = basename(value);
6180
+ // Note: Keeping extension in the name
6181
+ }
6182
+ value = value.split('/').join('-');
6183
+ value = removeEmojis(value);
6184
+ value = normalizeToKebabCase(value);
6185
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
6186
+ return value;
6187
+ }
6188
+
6123
6189
  /**
6124
6190
  * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
6125
6191
  *
@@ -6155,10 +6221,11 @@ var scraperFetch = function (url, init) { return __awaiter(void 0, void 0, void
6155
6221
  function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
6156
6222
  var _a;
6157
6223
  return __awaiter(this, void 0, void 0, function () {
6158
- var _b, fetch, knowledgeSourceContent, name, _c, _d, rootDirname, url, response_1, mimeType, filename_1, fileExtension, mimeType;
6159
- return __generator(this, function (_f) {
6160
- switch (_f.label) {
6224
+ 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;
6225
+ return __generator(this, function (_l) {
6226
+ switch (_l.label) {
6161
6227
  case 0:
6228
+ console.log('!!! makeKnowledgeSourceHandler', knowledgeSource);
6162
6229
  _b = tools.fetch, fetch = _b === void 0 ? scraperFetch : _b;
6163
6230
  knowledgeSourceContent = knowledgeSource.knowledgeSourceContent;
6164
6231
  name = knowledgeSource.name;
@@ -6166,54 +6233,74 @@ function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
6166
6233
  if (!name) {
6167
6234
  name = knowledgeSourceContentToName(knowledgeSourceContent);
6168
6235
  }
6169
- if (!isValidUrl(knowledgeSourceContent)) return [3 /*break*/, 2];
6236
+ if (!isValidUrl(knowledgeSourceContent)) return [3 /*break*/, 5];
6170
6237
  url = knowledgeSourceContent;
6171
6238
  return [4 /*yield*/, fetch(url)];
6172
6239
  case 1:
6173
- response_1 = _f.sent();
6240
+ response_1 = _l.sent();
6174
6241
  mimeType = ((_a = response_1.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]) || 'text/html';
6175
- return [2 /*return*/, {
6176
- source: name,
6177
- filename: null,
6178
- url: url,
6179
- mimeType: mimeType,
6180
- /*
6181
- TODO: [🥽]
6182
- > async asBlob() {
6183
- > // TODO: [👨🏻‍🤝‍👨🏻] This can be called multiple times BUT when called second time, response in already consumed
6184
- > const content = await response.blob();
6185
- > return content;
6186
- > },
6187
- */
6188
- asJson: function () {
6189
- return __awaiter(this, void 0, void 0, function () {
6190
- var content;
6191
- return __generator(this, function (_a) {
6192
- switch (_a.label) {
6193
- case 0: return [4 /*yield*/, response_1.json()];
6194
- case 1:
6195
- content = _a.sent();
6196
- return [2 /*return*/, content];
6197
- }
6242
+ if (tools.fs === undefined || !url.endsWith('.pdf')) {
6243
+ return [2 /*return*/, {
6244
+ source: name,
6245
+ filename: null,
6246
+ url: url,
6247
+ mimeType: mimeType,
6248
+ /*
6249
+ TODO: [🥽]
6250
+ > async asBlob() {
6251
+ > // TODO: [👨🏻‍🤝‍👨🏻] This can be called multiple times BUT when called second time, response in already consumed
6252
+ > const content = await response.blob();
6253
+ > return content;
6254
+ > },
6255
+ */
6256
+ asJson: function () {
6257
+ return __awaiter(this, void 0, void 0, function () {
6258
+ var content;
6259
+ return __generator(this, function (_a) {
6260
+ switch (_a.label) {
6261
+ case 0: return [4 /*yield*/, response_1.json()];
6262
+ case 1:
6263
+ content = _a.sent();
6264
+ return [2 /*return*/, content];
6265
+ }
6266
+ });
6198
6267
  });
6199
- });
6200
- },
6201
- asText: function () {
6202
- return __awaiter(this, void 0, void 0, function () {
6203
- var content;
6204
- return __generator(this, function (_a) {
6205
- switch (_a.label) {
6206
- case 0: return [4 /*yield*/, response_1.text()];
6207
- case 1:
6208
- content = _a.sent();
6209
- return [2 /*return*/, content];
6210
- }
6268
+ },
6269
+ asText: function () {
6270
+ return __awaiter(this, void 0, void 0, function () {
6271
+ var content;
6272
+ return __generator(this, function (_a) {
6273
+ switch (_a.label) {
6274
+ case 0: return [4 /*yield*/, response_1.text()];
6275
+ case 1:
6276
+ content = _a.sent();
6277
+ return [2 /*return*/, content];
6278
+ }
6279
+ });
6211
6280
  });
6212
- });
6213
- },
6214
- }];
6281
+ },
6282
+ }];
6283
+ }
6284
+ basename = url.split('/').pop() || titleToName(url);
6285
+ hash = sha256(hexEncoder.parse(url)).toString( /* hex */);
6286
+ rootDirname_1 = join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
6287
+ filepath = 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));
6288
+ return [4 /*yield*/, tools.fs.mkdir(dirname(join(rootDirname_1, filepath)), { recursive: true })];
6215
6289
  case 2:
6216
- if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 4];
6290
+ _l.sent();
6291
+ _g = (_f = tools.fs).writeFile;
6292
+ _h = [join(rootDirname_1, filepath)];
6293
+ _k = (_j = Buffer).from;
6294
+ return [4 /*yield*/, response_1.arrayBuffer()];
6295
+ case 3: return [4 /*yield*/, _g.apply(_f, _h.concat([_k.apply(_j, [_l.sent()])]))];
6296
+ case 4:
6297
+ _l.sent();
6298
+ // TODO: !!!!!!!! Check the file security
6299
+ // TODO: !!!!!!!! Check the file size (if it is not too big)
6300
+ // TODO: !!!!!!!! Delete the file
6301
+ return [2 /*return*/, makeKnowledgeSourceHandler({ name: name, knowledgeSourceContent: filepath }, tools, __assign(__assign({}, options), { rootDirname: rootDirname_1 }))];
6302
+ case 5:
6303
+ if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 7];
6217
6304
  if (tools.fs === undefined) {
6218
6305
  throw new EnvironmentMismatchError('Can not import file knowledge without filesystem tools');
6219
6306
  // <- TODO: [🧠] What is the best error type here`
@@ -6226,8 +6313,8 @@ function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
6226
6313
  fileExtension = getFileExtension(filename_1);
6227
6314
  mimeType = extensionToMimeType(fileExtension || '');
6228
6315
  return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
6229
- case 3:
6230
- if (!(_f.sent())) {
6316
+ case 6:
6317
+ if (!(_l.sent())) {
6231
6318
  throw new NotFoundError(spaceTrim$1(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 "); }));
6232
6319
  }
6233
6320
  // TODO: [🧠][😿] Test security file - file is scoped to the project (BUT maybe do this in `filesystemTools`)
@@ -6273,7 +6360,7 @@ function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
6273
6360
  });
6274
6361
  },
6275
6362
  }];
6276
- case 4: return [2 /*return*/, {
6363
+ case 7: return [2 /*return*/, {
6277
6364
  source: name,
6278
6365
  filename: null,
6279
6366
  url: null,