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