@promptbook/remote-server 0.85.0-8 → 0.85.0

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/README.md CHANGED
@@ -24,10 +24,6 @@
24
24
 
25
25
 
26
26
 
27
- <blockquote style="color: #ff8811">
28
- <b>⚠ Warning:</b> This is a pre-release version of the library. It is not yet ready for production use. Please look at <a href="https://www.npmjs.com/package/@promptbook/core?activeTab=versions">latest stable release</a>.
29
- </blockquote>
30
-
31
27
  ## 📦 Package `@promptbook/remote-server`
32
28
 
33
29
  - Promptbooks are [divided into several](#-packages) packages, all are published from [single monorepo](https://github.com/webgptorg/promptbook).
package/esm/index.es.js CHANGED
@@ -31,7 +31,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
31
31
  * @generated
32
32
  * @see https://github.com/webgptorg/promptbook
33
33
  */
34
- var PROMPTBOOK_ENGINE_VERSION = '0.85.0-7';
34
+ var PROMPTBOOK_ENGINE_VERSION = '0.85.0-16';
35
35
  /**
36
36
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
37
37
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1491,57 +1491,6 @@ function isValidPromptbookVersion(version) {
1491
1491
  return true;
1492
1492
  }
1493
1493
 
1494
- /**
1495
- * Checks if an URL is reserved for private networks or localhost.
1496
- *
1497
- * Note: There are two simmilar functions:
1498
- * - `isUrlOnPrivateNetwork` which tests full URL
1499
- * - `isHostnameOnPrivateNetwork` *(this one)* which tests just hostname
1500
- *
1501
- * @public exported from `@promptbook/utils`
1502
- */
1503
- function isHostnameOnPrivateNetwork(hostname) {
1504
- if (hostname === 'example.com' ||
1505
- hostname === 'localhost' ||
1506
- hostname.endsWith('.localhost') ||
1507
- hostname.endsWith('.local') ||
1508
- hostname.endsWith('.test') ||
1509
- hostname === '127.0.0.1' ||
1510
- hostname === '::1') {
1511
- return true;
1512
- }
1513
- if (hostname.includes(':')) {
1514
- // IPv6
1515
- var ipParts = hostname.split(':');
1516
- return ipParts[0] === 'fc00' || ipParts[0] === 'fd00' || ipParts[0] === 'fe80';
1517
- }
1518
- else {
1519
- // IPv4
1520
- var ipParts = hostname.split('.').map(function (part) { return Number.parseInt(part, 10); });
1521
- return (ipParts[0] === 10 ||
1522
- (ipParts[0] === 172 && ipParts[1] >= 16 && ipParts[1] <= 31) ||
1523
- (ipParts[0] === 192 && ipParts[1] === 168));
1524
- }
1525
- }
1526
-
1527
- /**
1528
- * Checks if an IP address or hostname is reserved for private networks or localhost.
1529
- *
1530
- * Note: There are two simmilar functions:
1531
- * - `isUrlOnPrivateNetwork` *(this one)* which tests full URL
1532
- * - `isHostnameOnPrivateNetwork` which tests just hostname
1533
- *
1534
- * @param {string} ipAddress - The IP address to check.
1535
- * @returns {boolean} Returns true if the IP address is reserved for private networks or localhost, otherwise false.
1536
- * @public exported from `@promptbook/utils`
1537
- */
1538
- function isUrlOnPrivateNetwork(url) {
1539
- if (typeof url === 'string') {
1540
- url = new URL(url);
1541
- }
1542
- return isHostnameOnPrivateNetwork(url.hostname);
1543
- }
1544
-
1545
1494
  /**
1546
1495
  * Tests if given string is valid URL.
1547
1496
  *
@@ -1584,16 +1533,19 @@ function isValidPipelineUrl(url) {
1584
1533
  if (!isValidUrl(url)) {
1585
1534
  return false;
1586
1535
  }
1587
- if (!url.startsWith('https://')) {
1536
+ if (!url.startsWith('https://') && !url.startsWith('http://') /* <- Note: [👣] */) {
1588
1537
  return false;
1589
1538
  }
1590
1539
  if (url.includes('#')) {
1591
1540
  // TODO: [🐠]
1592
1541
  return false;
1593
1542
  }
1543
+ /*
1544
+ Note: [👣][🧠] Is it secure to allow pipeline URLs on private and unsecured networks?
1594
1545
  if (isUrlOnPrivateNetwork(url)) {
1595
1546
  return false;
1596
1547
  }
1548
+ */
1597
1549
  return true;
1598
1550
  }
1599
1551
  /**
@@ -2009,18 +1961,16 @@ function assertsTaskSuccessful(executionResult) {
2009
1961
  */
2010
1962
  function createTask(options) {
2011
1963
  var taskType = options.taskType, taskProcessCallback = options.taskProcessCallback;
2012
- var taskId = "".concat(taskType.toLowerCase().substring(0, 4), "-").concat($randomToken(8 /* <- TODO: !!! To global config + Use Base58 to avoid simmilar char conflicts */));
1964
+ var taskId = "".concat(taskType.toLowerCase().substring(0, 4), "-").concat($randomToken(8 /* <- TODO: To global config + Use Base58 to avoid simmilar char conflicts */));
2013
1965
  var partialResultSubject = new BehaviorSubject({});
2014
1966
  var finalResultPromise = /* not await */ taskProcessCallback(function (newOngoingResult) {
2015
1967
  partialResultSubject.next(newOngoingResult);
2016
1968
  });
2017
1969
  finalResultPromise
2018
1970
  .catch(function (error) {
2019
- // console.error('!!!!! Task failed:', error);
2020
1971
  partialResultSubject.error(error);
2021
1972
  })
2022
1973
  .then(function (value) {
2023
- // console.error('!!!!! Task finished:', value);
2024
1974
  if (value) {
2025
1975
  try {
2026
1976
  assertsTaskSuccessful(value);
@@ -2042,9 +1992,7 @@ function createTask(options) {
2042
1992
  return [4 /*yield*/, finalResultPromise];
2043
1993
  case 1:
2044
1994
  finalResult = _b.sent();
2045
- console.error('!!!!! finalResult:', finalResult);
2046
1995
  if (isCrashedOnError) {
2047
- console.error('!!!!! isCrashedOnError:', finalResult);
2048
1996
  assertsTaskSuccessful(finalResult);
2049
1997
  }
2050
1998
  return [2 /*return*/, finalResult];
@@ -6897,6 +6845,57 @@ function startRemoteServer(options) {
6897
6845
  .filter(function (part) { return part !== ''; })
6898
6846
  .join('/');
6899
6847
  var startupDate = new Date();
6848
+ function getExecutionToolsFromIdentification(identification) {
6849
+ return __awaiter(this, void 0, void 0, function () {
6850
+ var isAnonymous, llm, llmToolsConfiguration, appId, userId, customOptions, fs, executables, tools;
6851
+ var _a;
6852
+ return __generator(this, function (_b) {
6853
+ switch (_b.label) {
6854
+ case 0:
6855
+ if (identification === null || identification === undefined) {
6856
+ throw new Error("Identification is not provided");
6857
+ }
6858
+ isAnonymous = identification.isAnonymous;
6859
+ if (isAnonymous === true && !isAnonymousModeAllowed) {
6860
+ throw new PipelineExecutionError("Anonymous mode is not allowed"); // <- TODO: [main] !!3 Test
6861
+ }
6862
+ if (isAnonymous === false && !isApplicationModeAllowed) {
6863
+ throw new PipelineExecutionError("Application mode is not allowed"); // <- TODO: [main] !!3 Test
6864
+ }
6865
+ if (!(isAnonymous === true)) return [3 /*break*/, 1];
6866
+ llmToolsConfiguration = identification.llmToolsConfiguration;
6867
+ llm = createLlmToolsFromConfiguration(llmToolsConfiguration, { isVerbose: isVerbose });
6868
+ return [3 /*break*/, 4];
6869
+ case 1:
6870
+ if (!(isAnonymous === false && createLlmExecutionTools !== null)) return [3 /*break*/, 3];
6871
+ appId = identification.appId, userId = identification.userId, customOptions = identification.customOptions;
6872
+ return [4 /*yield*/, createLlmExecutionTools({
6873
+ appId: appId,
6874
+ userId: userId,
6875
+ customOptions: customOptions,
6876
+ })];
6877
+ case 2:
6878
+ llm = _b.sent();
6879
+ return [3 /*break*/, 4];
6880
+ case 3: throw new PipelineExecutionError("You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured");
6881
+ case 4:
6882
+ fs = $provideFilesystemForNode();
6883
+ return [4 /*yield*/, $provideExecutablesForNode()];
6884
+ case 5:
6885
+ executables = _b.sent();
6886
+ _a = {
6887
+ llm: llm,
6888
+ fs: fs
6889
+ };
6890
+ return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
6891
+ case 6:
6892
+ tools = (_a.scrapers = _b.sent(),
6893
+ _a);
6894
+ return [2 /*return*/, tools];
6895
+ }
6896
+ });
6897
+ });
6898
+ }
6900
6899
  var app = express();
6901
6900
  app.use(express.json());
6902
6901
  app.use(function (request, response, next) {
@@ -6970,6 +6969,46 @@ function startRemoteServer(options) {
6970
6969
  }
6971
6970
  });
6972
6971
  }); });
6972
+ // TODO: [🧠] Is it secure / good idea to expose source codes of hosted books
6973
+ app.get("".concat(rootPath, "/books/*"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6974
+ var pipelines, fullUrl, pipelineUrl, pipeline, source, error_1;
6975
+ return __generator(this, function (_a) {
6976
+ switch (_a.label) {
6977
+ case 0:
6978
+ _a.trys.push([0, 3, , 4]);
6979
+ if (collection === null) {
6980
+ response.status(500).send('No collection nor books available');
6981
+ return [2 /*return*/];
6982
+ }
6983
+ return [4 /*yield*/, collection.listPipelines()];
6984
+ case 1:
6985
+ pipelines = _a.sent();
6986
+ fullUrl = request.protocol + '://' + request.get('host') + request.originalUrl;
6987
+ pipelineUrl = pipelines.find(function (pipelineUrl) { return pipelineUrl.endsWith(request.originalUrl); }) || fullUrl;
6988
+ return [4 /*yield*/, collection.getPipelineByUrl(pipelineUrl)];
6989
+ case 2:
6990
+ pipeline = _a.sent();
6991
+ source = pipeline.sources[0];
6992
+ if (source === undefined || source.type !== 'BOOK') {
6993
+ throw new Error('Pipeline source is not a book');
6994
+ }
6995
+ response
6996
+ .type('text/markdown')
6997
+ .send(source.content);
6998
+ return [3 /*break*/, 4];
6999
+ case 3:
7000
+ error_1 = _a.sent();
7001
+ if (!(error_1 instanceof Error)) {
7002
+ throw error_1;
7003
+ }
7004
+ response
7005
+ .status(404)
7006
+ .send({ error: serializeError(error_1) });
7007
+ return [3 /*break*/, 4];
7008
+ case 4: return [2 /*return*/];
7009
+ }
7010
+ });
7011
+ }); });
6973
7012
  app.get("".concat(rootPath, "/executions"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6974
7013
  return __generator(this, function (_a) {
6975
7014
  response.send(runningExecutionTasks);
@@ -6982,7 +7021,9 @@ function startRemoteServer(options) {
6982
7021
  taskId = request.params.taskId;
6983
7022
  execution = runningExecutionTasks.find(function (executionTask) { return executionTask.taskId === taskId; });
6984
7023
  if (execution === undefined) {
6985
- response.status(404).send("Execution \"".concat(taskId, "\" not found"));
7024
+ response
7025
+ .status(404)
7026
+ .send("Execution \"".concat(taskId, "\" not found"));
6986
7027
  return [2 /*return*/];
6987
7028
  }
6988
7029
  response.send(execution.currentValue);
@@ -6990,13 +7031,12 @@ function startRemoteServer(options) {
6990
7031
  });
6991
7032
  }); });
6992
7033
  app.post("".concat(rootPath, "/executions/new"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6993
- var inputParameters, pipelineUrl, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask, error_1;
6994
- var _a;
7034
+ var _a, inputParameters, identification, pipelineUrl, pipeline, tools, pipelineExecutor, executionTask, error_2;
6995
7035
  return __generator(this, function (_b) {
6996
7036
  switch (_b.label) {
6997
7037
  case 0:
6998
- _b.trys.push([0, 6, , 7]);
6999
- inputParameters = request.body.inputParameters;
7038
+ _b.trys.push([0, 4, , 5]);
7039
+ _a = request.body, inputParameters = _a.inputParameters, identification = _a.identification;
7000
7040
  pipelineUrl = request.body.pipelineUrl || request.body.book;
7001
7041
  return [4 /*yield*/, (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl))];
7002
7042
  case 1:
@@ -7005,43 +7045,27 @@ function startRemoteServer(options) {
7005
7045
  response.status(404).send("Pipeline \"".concat(pipelineUrl, "\" not found"));
7006
7046
  return [2 /*return*/];
7007
7047
  }
7008
- return [4 /*yield*/, createLlmExecutionTools({
7009
- appId: '!!!!',
7010
- userId: '!!!!',
7011
- customOptions: {},
7012
- })];
7048
+ return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7013
7049
  case 2:
7014
- llm = _b.sent();
7015
- fs = $provideFilesystemForNode();
7016
- return [4 /*yield*/, $provideExecutablesForNode()];
7017
- case 3:
7018
- executables = _b.sent();
7019
- _a = {
7020
- llm: llm,
7021
- fs: fs
7022
- };
7023
- return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
7024
- case 4:
7025
- tools = (_a.scrapers = _b.sent(),
7026
- _a);
7050
+ tools = _b.sent();
7027
7051
  pipelineExecutor = createPipelineExecutor(__assign({ pipeline: pipeline, tools: tools }, options));
7028
7052
  executionTask = pipelineExecutor(inputParameters);
7029
7053
  runningExecutionTasks.push(executionTask);
7030
7054
  return [4 /*yield*/, forTime(10)];
7031
- case 5:
7055
+ case 3:
7032
7056
  _b.sent();
7033
7057
  // <- Note: Wait for a while to wait for quick responses or sudden but asynchronous errors
7034
7058
  // <- TODO: Put this into configuration
7035
7059
  response.send(executionTask);
7036
- return [3 /*break*/, 7];
7037
- case 6:
7038
- error_1 = _b.sent();
7039
- if (!(error_1 instanceof Error)) {
7040
- throw error_1;
7060
+ return [3 /*break*/, 5];
7061
+ case 4:
7062
+ error_2 = _b.sent();
7063
+ if (!(error_2 instanceof Error)) {
7064
+ throw error_2;
7041
7065
  }
7042
- response.status(400).send({ error: serializeError(error_1) });
7043
- return [3 /*break*/, 7];
7044
- case 7: return [2 /*return*/];
7066
+ response.status(400).send({ error: serializeError(error_2) });
7067
+ return [3 /*break*/, 5];
7068
+ case 5: return [2 /*return*/];
7045
7069
  }
7046
7070
  });
7047
7071
  }); });
@@ -7058,55 +7082,9 @@ function startRemoteServer(options) {
7058
7082
  if (isVerbose) {
7059
7083
  console.info(colors.gray("Client connected"), socket.id);
7060
7084
  }
7061
- var getExecutionToolsFromIdentification = function (identification) { return __awaiter(_this, void 0, void 0, function () {
7062
- var isAnonymous, llm, llmToolsConfiguration, appId, userId, customOptions, fs, executables, tools;
7063
- var _a;
7064
- return __generator(this, function (_b) {
7065
- switch (_b.label) {
7066
- case 0:
7067
- isAnonymous = identification.isAnonymous;
7068
- if (isAnonymous === true && !isAnonymousModeAllowed) {
7069
- throw new PipelineExecutionError("Anonymous mode is not allowed"); // <- TODO: [main] !!3 Test
7070
- }
7071
- if (isAnonymous === false && !isApplicationModeAllowed) {
7072
- throw new PipelineExecutionError("Application mode is not allowed"); // <- TODO: [main] !!3 Test
7073
- }
7074
- if (!(isAnonymous === true)) return [3 /*break*/, 1];
7075
- llmToolsConfiguration = identification.llmToolsConfiguration;
7076
- llm = createLlmToolsFromConfiguration(llmToolsConfiguration, { isVerbose: isVerbose });
7077
- return [3 /*break*/, 4];
7078
- case 1:
7079
- if (!(isAnonymous === false && createLlmExecutionTools !== null)) return [3 /*break*/, 3];
7080
- appId = identification.appId, userId = identification.userId, customOptions = identification.customOptions;
7081
- return [4 /*yield*/, createLlmExecutionTools({
7082
- appId: appId,
7083
- userId: userId,
7084
- customOptions: customOptions,
7085
- })];
7086
- case 2:
7087
- llm = _b.sent();
7088
- return [3 /*break*/, 4];
7089
- case 3: throw new PipelineExecutionError("You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured");
7090
- case 4:
7091
- fs = $provideFilesystemForNode();
7092
- return [4 /*yield*/, $provideExecutablesForNode()];
7093
- case 5:
7094
- executables = _b.sent();
7095
- _a = {
7096
- llm: llm,
7097
- fs: fs
7098
- };
7099
- return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
7100
- case 6:
7101
- tools = (_a.scrapers = _b.sent(),
7102
- _a);
7103
- return [2 /*return*/, tools];
7104
- }
7105
- });
7106
- }); };
7107
7085
  // -----------
7108
7086
  socket.on('prompt-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7109
- var identification, prompt, executionTools, llm, _a, promptResult, _b, error_2;
7087
+ var identification, prompt, tools, llm, _a, promptResult, _b, error_3;
7110
7088
  return __generator(this, function (_c) {
7111
7089
  switch (_c.label) {
7112
7090
  case 0:
@@ -7119,8 +7097,8 @@ function startRemoteServer(options) {
7119
7097
  _c.trys.push([1, 13, 14, 15]);
7120
7098
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7121
7099
  case 2:
7122
- executionTools = _c.sent();
7123
- llm = executionTools.llm;
7100
+ tools = _c.sent();
7101
+ llm = tools.llm;
7124
7102
  _a = identification.isAnonymous === false &&
7125
7103
  collection !== null;
7126
7104
  if (!_a) return [3 /*break*/, 4];
@@ -7175,11 +7153,11 @@ function startRemoteServer(options) {
7175
7153
  socket.emit('prompt-response', { promptResult: promptResult } /* <- Note: [🤛] */);
7176
7154
  return [3 /*break*/, 15];
7177
7155
  case 13:
7178
- error_2 = _c.sent();
7179
- if (!(error_2 instanceof Error)) {
7180
- throw error_2;
7156
+ error_3 = _c.sent();
7157
+ if (!(error_3 instanceof Error)) {
7158
+ throw error_3;
7181
7159
  }
7182
- socket.emit('error', serializeError(error_2) /* <- Note: [🤛] */);
7160
+ socket.emit('error', serializeError(error_3) /* <- Note: [🤛] */);
7183
7161
  return [3 /*break*/, 15];
7184
7162
  case 14:
7185
7163
  socket.disconnect();
@@ -7191,7 +7169,7 @@ function startRemoteServer(options) {
7191
7169
  // -----------
7192
7170
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7193
7171
  socket.on('listModels-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7194
- var identification, executionTools, llm, models, error_3;
7172
+ var identification, tools, llm, models, error_4;
7195
7173
  return __generator(this, function (_a) {
7196
7174
  switch (_a.label) {
7197
7175
  case 0:
@@ -7204,19 +7182,19 @@ function startRemoteServer(options) {
7204
7182
  _a.trys.push([1, 4, 5, 6]);
7205
7183
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7206
7184
  case 2:
7207
- executionTools = _a.sent();
7208
- llm = executionTools.llm;
7185
+ tools = _a.sent();
7186
+ llm = tools.llm;
7209
7187
  return [4 /*yield*/, llm.listModels()];
7210
7188
  case 3:
7211
7189
  models = _a.sent();
7212
7190
  socket.emit('listModels-response', { models: models } /* <- Note: [🤛] */);
7213
7191
  return [3 /*break*/, 6];
7214
7192
  case 4:
7215
- error_3 = _a.sent();
7216
- if (!(error_3 instanceof Error)) {
7217
- throw error_3;
7193
+ error_4 = _a.sent();
7194
+ if (!(error_4 instanceof Error)) {
7195
+ throw error_4;
7218
7196
  }
7219
- socket.emit('error', serializeError(error_3));
7197
+ socket.emit('error', serializeError(error_4));
7220
7198
  return [3 /*break*/, 6];
7221
7199
  case 5:
7222
7200
  socket.disconnect();
@@ -7228,7 +7206,7 @@ function startRemoteServer(options) {
7228
7206
  // -----------
7229
7207
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7230
7208
  socket.on('preparePipeline-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7231
- var identification, pipeline, executionTools, preparedPipeline, error_4;
7209
+ var identification, pipeline, tools, preparedPipeline, error_5;
7232
7210
  return __generator(this, function (_a) {
7233
7211
  switch (_a.label) {
7234
7212
  case 0:
@@ -7241,18 +7219,18 @@ function startRemoteServer(options) {
7241
7219
  _a.trys.push([1, 4, 5, 6]);
7242
7220
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7243
7221
  case 2:
7244
- executionTools = _a.sent();
7245
- return [4 /*yield*/, preparePipeline(pipeline, executionTools, options)];
7222
+ tools = _a.sent();
7223
+ return [4 /*yield*/, preparePipeline(pipeline, tools, options)];
7246
7224
  case 3:
7247
7225
  preparedPipeline = _a.sent();
7248
7226
  socket.emit('preparePipeline-response', { preparedPipeline: preparedPipeline } /* <- Note: [🤛] */);
7249
7227
  return [3 /*break*/, 6];
7250
7228
  case 4:
7251
- error_4 = _a.sent();
7252
- if (!(error_4 instanceof Error)) {
7253
- throw error_4;
7229
+ error_5 = _a.sent();
7230
+ if (!(error_5 instanceof Error)) {
7231
+ throw error_5;
7254
7232
  }
7255
- socket.emit('error', serializeError(error_4));
7233
+ socket.emit('error', serializeError(error_5));
7256
7234
  return [3 /*break*/, 6];
7257
7235
  case 5:
7258
7236
  socket.disconnect();
@@ -7291,8 +7269,7 @@ function startRemoteServer(options) {
7291
7269
  };
7292
7270
  }
7293
7271
  /**
7294
- * TODO: !!!!!!! CORS and security
7295
- * TODO: !!!!!!! Allow to pass tokem here
7272
+ * TODO: !! Add CORS and security - probbably via `helmet`
7296
7273
  * TODO: [👩🏾‍🤝‍🧑🏾] Allow to pass custom fetch function here - PromptbookFetch
7297
7274
  * TODO: Split this file into multiple functions - handler for each request
7298
7275
  * TODO: Maybe use `$exportJson`