@promptbook/remote-server 0.85.0-1 → 0.85.0-10

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
@@ -1,13 +1,13 @@
1
1
  import colors from 'colors';
2
2
  import express from 'express';
3
- import http from 'node:http';
3
+ import http from 'http';
4
4
  import { Server } from 'socket.io';
5
5
  import spaceTrim$1, { spaceTrim } from 'spacetrim';
6
- import { spawn } from 'node:child_process';
7
6
  import { forTime } from 'waitasecond';
8
- import { stat, access, constants, readFile, writeFile, readdir, mkdir } from 'node:fs/promises';
9
- import { join, basename, dirname } from 'node:path';
10
- import { BehaviorSubject, concat, from } from 'rxjs';
7
+ import { spawn } from 'child_process';
8
+ import { stat, access, constants, readFile, writeFile, readdir, mkdir } from 'fs/promises';
9
+ import { join, basename, dirname } from 'path';
10
+ import { BehaviorSubject } from 'rxjs';
11
11
  import { randomBytes } from 'crypto';
12
12
  import { format } from 'prettier';
13
13
  import parserHtml from 'prettier/parser-html';
@@ -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-0';
34
+ var PROMPTBOOK_ENGINE_VERSION = '0.85.0-9';
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
@@ -2009,11 +2009,27 @@ function assertsTaskSuccessful(executionResult) {
2009
2009
  */
2010
2010
  function createTask(options) {
2011
2011
  var taskType = options.taskType, taskProcessCallback = options.taskProcessCallback;
2012
- var taskId = "".concat(taskType.toLowerCase(), "-").concat($randomToken(256 /* <- TODO: !!! To global config */));
2012
+ var taskId = "".concat(taskType.toLowerCase().substring(0, 4), "-").concat($randomToken(8 /* <- TODO: To global config + Use Base58 to avoid simmilar char conflicts */));
2013
2013
  var partialResultSubject = new BehaviorSubject({});
2014
2014
  var finalResultPromise = /* not await */ taskProcessCallback(function (newOngoingResult) {
2015
2015
  partialResultSubject.next(newOngoingResult);
2016
2016
  });
2017
+ finalResultPromise
2018
+ .catch(function (error) {
2019
+ partialResultSubject.error(error);
2020
+ })
2021
+ .then(function (value) {
2022
+ if (value) {
2023
+ try {
2024
+ assertsTaskSuccessful(value);
2025
+ partialResultSubject.next(value);
2026
+ }
2027
+ catch (error) {
2028
+ partialResultSubject.error(error);
2029
+ }
2030
+ }
2031
+ partialResultSubject.complete();
2032
+ });
2017
2033
  function asPromise(options) {
2018
2034
  return __awaiter(this, void 0, void 0, function () {
2019
2035
  var _a, isCrashedOnError, finalResult;
@@ -2037,9 +2053,10 @@ function createTask(options) {
2037
2053
  taskId: taskId,
2038
2054
  asPromise: asPromise,
2039
2055
  asObservable: function () {
2040
- return concat(partialResultSubject.asObservable(), from(asPromise({
2041
- isCrashedOnError: true,
2042
- })));
2056
+ return partialResultSubject.asObservable();
2057
+ },
2058
+ get currentValue() {
2059
+ return partialResultSubject.value;
2043
2060
  },
2044
2061
  };
2045
2062
  }
@@ -6876,6 +6893,57 @@ function startRemoteServer(options) {
6876
6893
  .filter(function (part) { return part !== ''; })
6877
6894
  .join('/');
6878
6895
  var startupDate = new Date();
6896
+ function getExecutionToolsFromIdentification(identification) {
6897
+ return __awaiter(this, void 0, void 0, function () {
6898
+ var isAnonymous, llm, llmToolsConfiguration, appId, userId, customOptions, fs, executables, tools;
6899
+ var _a;
6900
+ return __generator(this, function (_b) {
6901
+ switch (_b.label) {
6902
+ case 0:
6903
+ if (identification === null || identification === undefined) {
6904
+ throw new Error("Identification is not provided");
6905
+ }
6906
+ isAnonymous = identification.isAnonymous;
6907
+ if (isAnonymous === true && !isAnonymousModeAllowed) {
6908
+ throw new PipelineExecutionError("Anonymous mode is not allowed"); // <- TODO: [main] !!3 Test
6909
+ }
6910
+ if (isAnonymous === false && !isApplicationModeAllowed) {
6911
+ throw new PipelineExecutionError("Application mode is not allowed"); // <- TODO: [main] !!3 Test
6912
+ }
6913
+ if (!(isAnonymous === true)) return [3 /*break*/, 1];
6914
+ llmToolsConfiguration = identification.llmToolsConfiguration;
6915
+ llm = createLlmToolsFromConfiguration(llmToolsConfiguration, { isVerbose: isVerbose });
6916
+ return [3 /*break*/, 4];
6917
+ case 1:
6918
+ if (!(isAnonymous === false && createLlmExecutionTools !== null)) return [3 /*break*/, 3];
6919
+ appId = identification.appId, userId = identification.userId, customOptions = identification.customOptions;
6920
+ return [4 /*yield*/, createLlmExecutionTools({
6921
+ appId: appId,
6922
+ userId: userId,
6923
+ customOptions: customOptions,
6924
+ })];
6925
+ case 2:
6926
+ llm = _b.sent();
6927
+ return [3 /*break*/, 4];
6928
+ case 3: throw new PipelineExecutionError("You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured");
6929
+ case 4:
6930
+ fs = $provideFilesystemForNode();
6931
+ return [4 /*yield*/, $provideExecutablesForNode()];
6932
+ case 5:
6933
+ executables = _b.sent();
6934
+ _a = {
6935
+ llm: llm,
6936
+ fs: fs
6937
+ };
6938
+ return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
6939
+ case 6:
6940
+ tools = (_a.scrapers = _b.sent(),
6941
+ _a);
6942
+ return [2 /*return*/, tools];
6943
+ }
6944
+ });
6945
+ });
6946
+ }
6879
6947
  var app = express();
6880
6948
  app.use(express.json());
6881
6949
  app.use(function (request, response, next) {
@@ -6883,7 +6951,7 @@ function startRemoteServer(options) {
6883
6951
  next();
6884
6952
  });
6885
6953
  var runningExecutionTasks = [];
6886
- // TODO: !!!!!! Do here some garbage collection of finished tasks
6954
+ // TODO: [🧠] Do here some garbage collection of finished tasks
6887
6955
  app.get(['/', rootPath], function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6888
6956
  var _a, _b;
6889
6957
  var _this = this;
@@ -6932,20 +7000,19 @@ function startRemoteServer(options) {
6932
7000
  });
6933
7001
  }); });
6934
7002
  app.get("".concat(rootPath, "/books"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6935
- var _a, _b, _c;
6936
- return __generator(this, function (_d) {
6937
- switch (_d.label) {
7003
+ var pipelines;
7004
+ return __generator(this, function (_a) {
7005
+ switch (_a.label) {
6938
7006
  case 0:
6939
- _b = (_a = response).send;
6940
- if (!(collection === null)) return [3 /*break*/, 1];
6941
- _c = [];
6942
- return [3 /*break*/, 3];
6943
- case 1: return [4 /*yield*/, collection.listPipelines()];
6944
- case 2:
6945
- _c = _d.sent();
6946
- _d.label = 3;
6947
- case 3:
6948
- _b.apply(_a, [_c]);
7007
+ if (collection === null) {
7008
+ response.status(500).send('No collection available');
7009
+ return [2 /*return*/];
7010
+ }
7011
+ return [4 /*yield*/, collection.listPipelines()];
7012
+ case 1:
7013
+ pipelines = _a.sent();
7014
+ // <- TODO: [🧠][👩🏾‍🤝‍🧑🏿] List `inputParameters` required for the execution
7015
+ response.send(pipelines);
6949
7016
  return [2 /*return*/];
6950
7017
  }
6951
7018
  });
@@ -6959,23 +7026,23 @@ function startRemoteServer(options) {
6959
7026
  app.get("".concat(rootPath, "/executions/:taskId"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6960
7027
  var taskId, execution;
6961
7028
  return __generator(this, function (_a) {
6962
- taskId = request.query.taskId;
7029
+ taskId = request.params.taskId;
6963
7030
  execution = runningExecutionTasks.find(function (executionTask) { return executionTask.taskId === taskId; });
6964
7031
  if (execution === undefined) {
6965
7032
  response.status(404).send("Execution \"".concat(taskId, "\" not found"));
6966
7033
  return [2 /*return*/];
6967
7034
  }
6968
- response.send(execution);
7035
+ response.send(execution.currentValue);
6969
7036
  return [2 /*return*/];
6970
7037
  });
6971
7038
  }); });
6972
7039
  app.post("".concat(rootPath, "/executions/new"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6973
- var inputParameters, pipelineUrl, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask;
6974
- var _a;
7040
+ var _a, inputParameters, identification, pipelineUrl, pipeline, tools, pipelineExecutor, executionTask, error_1;
6975
7041
  return __generator(this, function (_b) {
6976
7042
  switch (_b.label) {
6977
7043
  case 0:
6978
- inputParameters = request.body.inputParameters;
7044
+ _b.trys.push([0, 4, , 5]);
7045
+ _a = request.body, inputParameters = _a.inputParameters, identification = _a.identification;
6979
7046
  pipelineUrl = request.body.pipelineUrl || request.body.book;
6980
7047
  return [4 /*yield*/, (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl))];
6981
7048
  case 1:
@@ -6984,30 +7051,27 @@ function startRemoteServer(options) {
6984
7051
  response.status(404).send("Pipeline \"".concat(pipelineUrl, "\" not found"));
6985
7052
  return [2 /*return*/];
6986
7053
  }
6987
- return [4 /*yield*/, createLlmExecutionTools({
6988
- appId: '!!!!',
6989
- userId: '!!!!',
6990
- customOptions: {},
6991
- })];
7054
+ return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
6992
7055
  case 2:
6993
- llm = _b.sent();
6994
- fs = $provideFilesystemForNode();
6995
- return [4 /*yield*/, $provideExecutablesForNode()];
6996
- case 3:
6997
- executables = _b.sent();
6998
- _a = {
6999
- llm: llm,
7000
- fs: fs
7001
- };
7002
- return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
7003
- case 4:
7004
- tools = (_a.scrapers = _b.sent(),
7005
- _a);
7056
+ tools = _b.sent();
7006
7057
  pipelineExecutor = createPipelineExecutor(__assign({ pipeline: pipeline, tools: tools }, options));
7007
7058
  executionTask = pipelineExecutor(inputParameters);
7008
7059
  runningExecutionTasks.push(executionTask);
7060
+ return [4 /*yield*/, forTime(10)];
7061
+ case 3:
7062
+ _b.sent();
7063
+ // <- Note: Wait for a while to wait for quick responses or sudden but asynchronous errors
7064
+ // <- TODO: Put this into configuration
7009
7065
  response.send(executionTask);
7010
- return [2 /*return*/];
7066
+ return [3 /*break*/, 5];
7067
+ case 4:
7068
+ error_1 = _b.sent();
7069
+ if (!(error_1 instanceof Error)) {
7070
+ throw error_1;
7071
+ }
7072
+ response.status(400).send({ error: serializeError(error_1) });
7073
+ return [3 /*break*/, 5];
7074
+ case 5: return [2 /*return*/];
7011
7075
  }
7012
7076
  });
7013
7077
  }); });
@@ -7024,55 +7088,9 @@ function startRemoteServer(options) {
7024
7088
  if (isVerbose) {
7025
7089
  console.info(colors.gray("Client connected"), socket.id);
7026
7090
  }
7027
- var getExecutionToolsFromIdentification = function (identification) { return __awaiter(_this, void 0, void 0, function () {
7028
- var isAnonymous, llm, llmToolsConfiguration, appId, userId, customOptions, fs, executables, tools;
7029
- var _a;
7030
- return __generator(this, function (_b) {
7031
- switch (_b.label) {
7032
- case 0:
7033
- isAnonymous = identification.isAnonymous;
7034
- if (isAnonymous === true && !isAnonymousModeAllowed) {
7035
- throw new PipelineExecutionError("Anonymous mode is not allowed"); // <- TODO: [main] !!3 Test
7036
- }
7037
- if (isAnonymous === false && !isApplicationModeAllowed) {
7038
- throw new PipelineExecutionError("Application mode is not allowed"); // <- TODO: [main] !!3 Test
7039
- }
7040
- if (!(isAnonymous === true)) return [3 /*break*/, 1];
7041
- llmToolsConfiguration = identification.llmToolsConfiguration;
7042
- llm = createLlmToolsFromConfiguration(llmToolsConfiguration, { isVerbose: isVerbose });
7043
- return [3 /*break*/, 4];
7044
- case 1:
7045
- if (!(isAnonymous === false && createLlmExecutionTools !== null)) return [3 /*break*/, 3];
7046
- appId = identification.appId, userId = identification.userId, customOptions = identification.customOptions;
7047
- return [4 /*yield*/, createLlmExecutionTools({
7048
- appId: appId,
7049
- userId: userId,
7050
- customOptions: customOptions,
7051
- })];
7052
- case 2:
7053
- llm = _b.sent();
7054
- return [3 /*break*/, 4];
7055
- case 3: throw new PipelineExecutionError("You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured");
7056
- case 4:
7057
- fs = $provideFilesystemForNode();
7058
- return [4 /*yield*/, $provideExecutablesForNode()];
7059
- case 5:
7060
- executables = _b.sent();
7061
- _a = {
7062
- llm: llm,
7063
- fs: fs
7064
- };
7065
- return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
7066
- case 6:
7067
- tools = (_a.scrapers = _b.sent(),
7068
- _a);
7069
- return [2 /*return*/, tools];
7070
- }
7071
- });
7072
- }); };
7073
7091
  // -----------
7074
7092
  socket.on('prompt-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7075
- var identification, prompt, executionTools, llm, _a, promptResult, _b, error_1;
7093
+ var identification, prompt, tools, llm, _a, promptResult, _b, error_2;
7076
7094
  return __generator(this, function (_c) {
7077
7095
  switch (_c.label) {
7078
7096
  case 0:
@@ -7085,8 +7103,8 @@ function startRemoteServer(options) {
7085
7103
  _c.trys.push([1, 13, 14, 15]);
7086
7104
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7087
7105
  case 2:
7088
- executionTools = _c.sent();
7089
- llm = executionTools.llm;
7106
+ tools = _c.sent();
7107
+ llm = tools.llm;
7090
7108
  _a = identification.isAnonymous === false &&
7091
7109
  collection !== null;
7092
7110
  if (!_a) return [3 /*break*/, 4];
@@ -7141,11 +7159,11 @@ function startRemoteServer(options) {
7141
7159
  socket.emit('prompt-response', { promptResult: promptResult } /* <- Note: [🤛] */);
7142
7160
  return [3 /*break*/, 15];
7143
7161
  case 13:
7144
- error_1 = _c.sent();
7145
- if (!(error_1 instanceof Error)) {
7146
- throw error_1;
7162
+ error_2 = _c.sent();
7163
+ if (!(error_2 instanceof Error)) {
7164
+ throw error_2;
7147
7165
  }
7148
- socket.emit('error', serializeError(error_1) /* <- Note: [🤛] */);
7166
+ socket.emit('error', serializeError(error_2) /* <- Note: [🤛] */);
7149
7167
  return [3 /*break*/, 15];
7150
7168
  case 14:
7151
7169
  socket.disconnect();
@@ -7157,7 +7175,7 @@ function startRemoteServer(options) {
7157
7175
  // -----------
7158
7176
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7159
7177
  socket.on('listModels-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7160
- var identification, executionTools, llm, models, error_2;
7178
+ var identification, tools, llm, models, error_3;
7161
7179
  return __generator(this, function (_a) {
7162
7180
  switch (_a.label) {
7163
7181
  case 0:
@@ -7170,19 +7188,19 @@ function startRemoteServer(options) {
7170
7188
  _a.trys.push([1, 4, 5, 6]);
7171
7189
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7172
7190
  case 2:
7173
- executionTools = _a.sent();
7174
- llm = executionTools.llm;
7191
+ tools = _a.sent();
7192
+ llm = tools.llm;
7175
7193
  return [4 /*yield*/, llm.listModels()];
7176
7194
  case 3:
7177
7195
  models = _a.sent();
7178
7196
  socket.emit('listModels-response', { models: models } /* <- Note: [🤛] */);
7179
7197
  return [3 /*break*/, 6];
7180
7198
  case 4:
7181
- error_2 = _a.sent();
7182
- if (!(error_2 instanceof Error)) {
7183
- throw error_2;
7199
+ error_3 = _a.sent();
7200
+ if (!(error_3 instanceof Error)) {
7201
+ throw error_3;
7184
7202
  }
7185
- socket.emit('error', serializeError(error_2));
7203
+ socket.emit('error', serializeError(error_3));
7186
7204
  return [3 /*break*/, 6];
7187
7205
  case 5:
7188
7206
  socket.disconnect();
@@ -7194,7 +7212,7 @@ function startRemoteServer(options) {
7194
7212
  // -----------
7195
7213
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7196
7214
  socket.on('preparePipeline-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7197
- var identification, pipeline, executionTools, preparedPipeline, error_3;
7215
+ var identification, pipeline, tools, preparedPipeline, error_4;
7198
7216
  return __generator(this, function (_a) {
7199
7217
  switch (_a.label) {
7200
7218
  case 0:
@@ -7207,18 +7225,18 @@ function startRemoteServer(options) {
7207
7225
  _a.trys.push([1, 4, 5, 6]);
7208
7226
  return [4 /*yield*/, getExecutionToolsFromIdentification(identification)];
7209
7227
  case 2:
7210
- executionTools = _a.sent();
7211
- return [4 /*yield*/, preparePipeline(pipeline, executionTools, options)];
7228
+ tools = _a.sent();
7229
+ return [4 /*yield*/, preparePipeline(pipeline, tools, options)];
7212
7230
  case 3:
7213
7231
  preparedPipeline = _a.sent();
7214
7232
  socket.emit('preparePipeline-response', { preparedPipeline: preparedPipeline } /* <- Note: [🤛] */);
7215
7233
  return [3 /*break*/, 6];
7216
7234
  case 4:
7217
- error_3 = _a.sent();
7218
- if (!(error_3 instanceof Error)) {
7219
- throw error_3;
7235
+ error_4 = _a.sent();
7236
+ if (!(error_4 instanceof Error)) {
7237
+ throw error_4;
7220
7238
  }
7221
- socket.emit('error', serializeError(error_3));
7239
+ socket.emit('error', serializeError(error_4));
7222
7240
  return [3 /*break*/, 6];
7223
7241
  case 5:
7224
7242
  socket.disconnect();
@@ -7257,8 +7275,7 @@ function startRemoteServer(options) {
7257
7275
  };
7258
7276
  }
7259
7277
  /**
7260
- * TODO: !!!!!!! CORS and security
7261
- * TODO: !!!!!!! Allow to pass tokem here
7278
+ * TODO: !! Add CORS and security - probbably via `helmet`
7262
7279
  * TODO: [👩🏾‍🤝‍🧑🏾] Allow to pass custom fetch function here - PromptbookFetch
7263
7280
  * TODO: Split this file into multiple functions - handler for each request
7264
7281
  * TODO: Maybe use `$exportJson`