@promptbook/remote-server 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/remote-server",
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/remote-server.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('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-11';
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
  /**
@@ -5981,6 +5998,22 @@
5981
5998
  * TODO: [®] DRY Register logic
5982
5999
  */
5983
6000
 
6001
+ /**
6002
+ * Removes emojis from a string and fix whitespaces
6003
+ *
6004
+ * @param text with emojis
6005
+ * @returns text without emojis
6006
+ * @public exported from `@promptbook/utils`
6007
+ */
6008
+ function removeEmojis(text) {
6009
+ // Replace emojis (and also ZWJ sequence) with hyphens
6010
+ text = text.replace(/(\p{Extended_Pictographic})\p{Modifier_Symbol}/gu, '$1');
6011
+ text = text.replace(/(\p{Extended_Pictographic})[\u{FE00}-\u{FE0F}]/gu, '$1');
6012
+ text = text.replace(/(\p{Extended_Pictographic})(\u{200D}\p{Extended_Pictographic})*/gu, '$1');
6013
+ text = text.replace(/\p{Extended_Pictographic}/gu, '');
6014
+ return text;
6015
+ }
6016
+
5984
6017
  /**
5985
6018
  * @@@
5986
6019
  *
@@ -6043,6 +6076,30 @@
6043
6076
  * Note: [💞] Ignore a discrepancy between file name and entity name
6044
6077
  */
6045
6078
 
6079
+ /**
6080
+ * @@@
6081
+ *
6082
+ * @param value @@@
6083
+ * @returns @@@
6084
+ * @example @@@
6085
+ * @public exported from `@promptbook/utils`
6086
+ */
6087
+ function titleToName(value) {
6088
+ if (isValidUrl(value)) {
6089
+ value = value.replace(/^https?:\/\//, '');
6090
+ value = value.replace(/\.html$/, '');
6091
+ }
6092
+ else if (isValidFilePath(value)) {
6093
+ value = path.basename(value);
6094
+ // Note: Keeping extension in the name
6095
+ }
6096
+ value = value.split('/').join('-');
6097
+ value = removeEmojis(value);
6098
+ value = normalizeToKebabCase(value);
6099
+ // TODO: [🧠] Maybe warn or add some padding to short name which are not good identifiers
6100
+ return value;
6101
+ }
6102
+
6046
6103
  /**
6047
6104
  * Creates unique name for the source
6048
6105
  *
@@ -6064,6 +6121,15 @@
6064
6121
  * TODO: [🐱‍🐉][🧠] Make some smart crop NOT source-i-m-pavol-a-develop-... BUT source-i-m-pavol-a-developer-...
6065
6122
  */
6066
6123
 
6124
+ /**
6125
+ * @@@
6126
+ *
6127
+ * @private for `FileCacheStorage`
6128
+ */
6129
+ function nameToSubfolderPath(name) {
6130
+ return [name.substr(0, 1).toLowerCase(), name.substr(1, 1).toLowerCase()];
6131
+ }
6132
+
6067
6133
  /**
6068
6134
  * Convert file extension to mime type
6069
6135
  *
@@ -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, mimeType, filename, 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,32 @@
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();
6173
- 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
- }
6197
- });
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
- }
6210
- });
6211
- });
6212
- },
6213
- }];
6239
+ response = _l.sent();
6240
+ mimeType = ((_a = response.headers.get('content-type')) === null || _a === void 0 ? void 0 : _a.split(';')[0]) || 'text/html';
6241
+ filename = url.split('/').pop() || titleToName(url);
6242
+ hash = sha256__default["default"](hexEncoder__default["default"].parse(url)).toString( /* hex */);
6243
+ rootDirname_1 = path.join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
6244
+ 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));
6245
+ return [4 /*yield*/, tools.fs.mkdir(path.dirname(path.join(rootDirname_1, filepath)), { recursive: true })];
6214
6246
  case 2:
6215
- if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 4];
6247
+ _l.sent();
6248
+ _g = (_f = tools.fs).writeFile;
6249
+ _h = [path.join(rootDirname_1, filepath)];
6250
+ _k = (_j = Buffer).from;
6251
+ return [4 /*yield*/, response.arrayBuffer()];
6252
+ case 3: return [4 /*yield*/, _g.apply(_f, _h.concat([_k.apply(_j, [_l.sent()])]))];
6253
+ case 4:
6254
+ _l.sent();
6255
+ // TODO: !!!!!!!! Check the file security
6256
+ // TODO: !!!!!!!! Check the file size (if it is not too big)
6257
+ // TODO: !!!!!!!! Delete the file
6258
+ return [2 /*return*/, makeKnowledgeSourceHandler({ name: name, knowledgeSourceContent: filepath }, tools, __assign(__assign({}, options), { rootDirname: rootDirname_1 }))];
6259
+ case 5:
6260
+ if (!isValidFilePath(knowledgeSourceContent)) return [3 /*break*/, 7];
6216
6261
  if (tools.fs === undefined) {
6217
6262
  throw new EnvironmentMismatchError('Can not import file knowledge without filesystem tools');
6218
6263
  // <- TODO: [🧠] What is the best error type here`
@@ -6225,8 +6270,8 @@
6225
6270
  fileExtension = getFileExtension(filename_1);
6226
6271
  mimeType = extensionToMimeType(fileExtension || '');
6227
6272
  return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
6228
- case 3:
6229
- if (!(_f.sent())) {
6273
+ case 6:
6274
+ if (!(_l.sent())) {
6230
6275
  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
6276
  }
6232
6277
  // TODO: [🧠][😿] Test security file - file is scoped to the project (BUT maybe do this in `filesystemTools`)
@@ -6272,7 +6317,7 @@
6272
6317
  });
6273
6318
  },
6274
6319
  }];
6275
- case 4: return [2 /*return*/, {
6320
+ case 7: return [2 /*return*/, {
6276
6321
  source: name,
6277
6322
  filename: null,
6278
6323
  url: null,