@promptbook/remote-server 0.85.0-3 → 0.85.0-5

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.
@@ -1,7 +1,7 @@
1
1
  import type { Observable } from 'rxjs';
2
2
  import { PartialDeep } from 'type-fest';
3
- import type { string_SCREAMING_CASE } from '../utils/normalization/normalizeTo_SCREAMING_CASE';
4
3
  import type { task_id } from '../types/typeAliases';
4
+ import type { string_SCREAMING_CASE } from '../utils/normalization/normalizeTo_SCREAMING_CASE';
5
5
  import type { AbstractTaskResult } from './AbstractTaskResult';
6
6
  import type { PipelineExecutorResult } from './PipelineExecutorResult';
7
7
  /**
@@ -30,7 +30,7 @@ export declare function createTask<TTaskResult extends AbstractTaskResult>(optio
30
30
  */
31
31
  export type ExecutionTask = AbstractTask<PipelineExecutorResult> & {
32
32
  readonly taskType: 'EXECUTION';
33
- readonly taskId: `execution-${task_id}`;
33
+ readonly taskId: `exec-${task_id}`;
34
34
  };
35
35
  /**
36
36
  * Represents a task that prepares a pipeline
@@ -38,7 +38,7 @@ export type ExecutionTask = AbstractTask<PipelineExecutorResult> & {
38
38
  */
39
39
  export type PreparationTask = AbstractTask<PipelineExecutorResult> & {
40
40
  readonly taskType: 'PREPARATION';
41
- readonly taskId: `preparation-${task_id}`;
41
+ readonly taskId: `prep-${task_id}`;
42
42
  };
43
43
  /**
44
44
  * Base interface for all task types
@@ -62,6 +62,10 @@ export type AbstractTask<TTaskResult extends AbstractTaskResult> = {
62
62
  * Gets an observable stream of partial task results
63
63
  */
64
64
  asObservable(): Observable<PartialDeep<TTaskResult>>;
65
+ /**
66
+ * Gets just the current value which is mutated during the task processing
67
+ */
68
+ currentValue: PartialDeep<TTaskResult>;
65
69
  };
66
70
  export type Task = ExecutionTask | PreparationTask;
67
71
  export {};
@@ -1,5 +1,5 @@
1
1
  /// <reference types="node" />
2
- import type fs from 'node:fs/promises';
2
+ import type fs from 'fs/promises';
3
3
  /**
4
4
  * Container for all the tools needed to manipulate with filesystem
5
5
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/remote-server",
3
- "version": "0.85.0-3",
3
+ "version": "0.85.0-5",
4
4
  "description": "It's time for a paradigm shift. The future of software in plain English, French or Latin",
5
5
  "private": false,
6
6
  "sideEffects": false,
@@ -47,7 +47,7 @@
47
47
  "module": "./esm/index.es.js",
48
48
  "typings": "./esm/typings/src/_packages/remote-server.index.d.ts",
49
49
  "peerDependencies": {
50
- "@promptbook/core": "0.85.0-3"
50
+ "@promptbook/core": "0.85.0-5"
51
51
  },
52
52
  "dependencies": {
53
53
  "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('express'), require('node:http'), require('socket.io'), require('spacetrim'), require('node:child_process'), require('waitasecond'), require('node:fs/promises'), require('node:path'), require('rxjs'), require('crypto'), require('prettier'), require('prettier/parser-html'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('crypto-js'), require('mime-types'), require('papaparse')) :
3
- typeof define === 'function' && define.amd ? define(['exports', 'colors', 'express', 'node:http', 'socket.io', 'spacetrim', 'node:child_process', 'waitasecond', 'node:fs/promises', 'node:path', 'rxjs', 'crypto', 'prettier', 'prettier/parser-html', 'crypto-js/enc-hex', 'crypto-js/sha256', 'crypto-js', 'mime-types', 'papaparse'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-remote-server"] = {}, global.colors, global.express, global.http, global.socket_io, global.spaceTrim, global.node_child_process, global.waitasecond, global.promises, global.node_path, global.rxjs, global.crypto, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.cryptoJs, global.mimeTypes, global.papaparse));
5
- })(this, (function (exports, colors, express, http, socket_io, spaceTrim, node_child_process, waitasecond, promises, node_path, rxjs, crypto, prettier, parserHtml, hexEncoder, sha256, cryptoJs, mimeTypes, papaparse) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('colors'), require('express'), require('http'), require('socket.io'), require('spacetrim'), require('waitasecond'), require('child_process'), require('fs/promises'), require('path'), require('rxjs'), require('crypto'), require('prettier'), require('prettier/parser-html'), require('crypto-js/enc-hex'), require('crypto-js/sha256'), require('crypto-js'), require('mime-types'), require('papaparse')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'colors', 'express', 'http', 'socket.io', 'spacetrim', 'waitasecond', 'child_process', 'fs/promises', 'path', 'rxjs', 'crypto', 'prettier', 'prettier/parser-html', 'crypto-js/enc-hex', 'crypto-js/sha256', 'crypto-js', 'mime-types', 'papaparse'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global["promptbook-remote-server"] = {}, global.colors, global.express, global.http, global.socket_io, global.spaceTrim, global.waitasecond, global.child_process, global.promises, global.path, global.rxjs, global.crypto, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.cryptoJs, global.mimeTypes, global.papaparse));
5
+ })(this, (function (exports, colors, express, http, socket_io, spaceTrim, waitasecond, child_process, promises, path, rxjs, crypto, prettier, parserHtml, hexEncoder, sha256, cryptoJs, mimeTypes, papaparse) { 'use strict';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -28,7 +28,7 @@
28
28
  * @generated
29
29
  * @see https://github.com/webgptorg/promptbook
30
30
  */
31
- var PROMPTBOOK_ENGINE_VERSION = '0.85.0-2';
31
+ var PROMPTBOOK_ENGINE_VERSION = '0.85.0-4';
32
32
  /**
33
33
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
34
34
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -777,7 +777,7 @@
777
777
  console.info(colors__default["default"].yellow(cwd) + ' ' + colors__default["default"].green(command) + ' ' + colors__default["default"].blue(args.join(' ')));
778
778
  }
779
779
  try {
780
- var commandProcess = node_child_process.spawn(command, args, { cwd: cwd, shell: true });
780
+ var commandProcess = child_process.spawn(command, args, { cwd: cwd, shell: true });
781
781
  if (isVerbose) {
782
782
  commandProcess.on('message', function (message) {
783
783
  console.info({ message: message });
@@ -987,7 +987,7 @@
987
987
  function locateAppOnWindows(_a) {
988
988
  var appName = _a.appName, windowsSuffix = _a.windowsSuffix;
989
989
  return __awaiter(this, void 0, void 0, function () {
990
- var prefixes, prefixes_1, prefixes_1_1, prefix, path, e_1_1, error_1;
990
+ var prefixes, prefixes_1, prefixes_1_1, prefix, path$1, e_1_1, error_1;
991
991
  var e_1, _b;
992
992
  return __generator(this, function (_c) {
993
993
  switch (_c.label) {
@@ -995,7 +995,7 @@
995
995
  _c.trys.push([0, 9, , 10]);
996
996
  prefixes = [
997
997
  process.env.LOCALAPPDATA,
998
- node_path.join(process.env.LOCALAPPDATA || '', 'Programs'),
998
+ path.join(process.env.LOCALAPPDATA || '', 'Programs'),
999
999
  process.env.PROGRAMFILES,
1000
1000
  process.env['PROGRAMFILES(X86)'],
1001
1001
  ];
@@ -1007,11 +1007,11 @@
1007
1007
  case 2:
1008
1008
  if (!!prefixes_1_1.done) return [3 /*break*/, 5];
1009
1009
  prefix = prefixes_1_1.value;
1010
- path = prefix + windowsSuffix;
1011
- return [4 /*yield*/, isExecutable(path, $provideFilesystemForNode())];
1010
+ path$1 = prefix + windowsSuffix;
1011
+ return [4 /*yield*/, isExecutable(path$1, $provideFilesystemForNode())];
1012
1012
  case 3:
1013
1013
  if (_c.sent()) {
1014
- return [2 /*return*/, path];
1014
+ return [2 /*return*/, path$1];
1015
1015
  }
1016
1016
  _c.label = 4;
1017
1017
  case 4:
@@ -2006,11 +2006,29 @@
2006
2006
  */
2007
2007
  function createTask(options) {
2008
2008
  var taskType = options.taskType, taskProcessCallback = options.taskProcessCallback;
2009
- var taskId = "".concat(taskType.toLowerCase(), "-").concat($randomToken(256 /* <- TODO: !!! To global config */));
2009
+ var taskId = "".concat(taskType.toLowerCase().substring(0, 4), "-").concat($randomToken(8 /* <- TODO: !!! To global config + Use Base58 to avoid simmilar char conflicts */));
2010
2010
  var partialResultSubject = new rxjs.BehaviorSubject({});
2011
2011
  var finalResultPromise = /* not await */ taskProcessCallback(function (newOngoingResult) {
2012
2012
  partialResultSubject.next(newOngoingResult);
2013
2013
  });
2014
+ finalResultPromise
2015
+ .catch(function (error) {
2016
+ // console.error('!!!!! Task failed:', error);
2017
+ partialResultSubject.error(error);
2018
+ })
2019
+ .then(function (value) {
2020
+ // console.error('!!!!! Task finished:', value);
2021
+ if (value) {
2022
+ try {
2023
+ assertsTaskSuccessful(value);
2024
+ partialResultSubject.next(value);
2025
+ }
2026
+ catch (error) {
2027
+ partialResultSubject.error(error);
2028
+ }
2029
+ }
2030
+ partialResultSubject.complete();
2031
+ });
2014
2032
  function asPromise(options) {
2015
2033
  return __awaiter(this, void 0, void 0, function () {
2016
2034
  var _a, isCrashedOnError, finalResult;
@@ -2021,7 +2039,9 @@
2021
2039
  return [4 /*yield*/, finalResultPromise];
2022
2040
  case 1:
2023
2041
  finalResult = _b.sent();
2042
+ console.error('!!!!! finalResult:', finalResult);
2024
2043
  if (isCrashedOnError) {
2044
+ console.error('!!!!! isCrashedOnError:', finalResult);
2025
2045
  assertsTaskSuccessful(finalResult);
2026
2046
  }
2027
2047
  return [2 /*return*/, finalResult];
@@ -2034,9 +2054,10 @@
2034
2054
  taskId: taskId,
2035
2055
  asPromise: asPromise,
2036
2056
  asObservable: function () {
2037
- return rxjs.concat(partialResultSubject.asObservable(), rxjs.from(asPromise({
2038
- isCrashedOnError: true,
2039
- })));
2057
+ return partialResultSubject.asObservable();
2058
+ },
2059
+ get currentValue() {
2060
+ return partialResultSubject.value;
2040
2061
  },
2041
2062
  };
2042
2063
  }
@@ -3979,7 +4000,7 @@
3979
4000
  value = value.replace(/\.html$/, '');
3980
4001
  }
3981
4002
  else if (isValidFilePath(value)) {
3982
- value = node_path.basename(value);
4003
+ value = path.basename(value);
3983
4004
  // Note: Keeping extension in the name
3984
4005
  }
3985
4006
  value = value.split('/').join('-');
@@ -4085,9 +4106,9 @@
4085
4106
  }
4086
4107
  basename = url.split('/').pop() || titleToName(url);
4087
4108
  hash = sha256__default["default"](hexEncoder__default["default"].parse(url)).toString( /* hex */);
4088
- rootDirname_1 = node_path.join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
4089
- filepath = node_path.join.apply(void 0, __spreadArray(__spreadArray([], __read(nameToSubfolderPath(hash /* <- TODO: [🎎] Maybe add some SHA256 prefix */)), false), ["".concat(basename.substring(0, MAX_FILENAME_LENGTH), ".").concat(mimeTypeToExtension(mimeType))], false));
4090
- return [4 /*yield*/, tools.fs.mkdir(node_path.dirname(node_path.join(rootDirname_1, filepath)), { recursive: true })];
4109
+ rootDirname_1 = path.join(process.cwd(), DEFAULT_DOWNLOAD_CACHE_DIRNAME);
4110
+ 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), ".").concat(mimeTypeToExtension(mimeType))], false));
4111
+ return [4 /*yield*/, tools.fs.mkdir(path.dirname(path.join(rootDirname_1, filepath)), { recursive: true })];
4091
4112
  case 2:
4092
4113
  _h.sent();
4093
4114
  _g = (_f = Buffer).from;
@@ -4097,7 +4118,7 @@
4097
4118
  if (fileContent.length > DEFAULT_MAX_FILE_SIZE /* <- TODO: Allow to pass different value to remote server */) {
4098
4119
  throw new LimitReachedError("File is too large (".concat(Math.round(fileContent.length / 1024 / 1024), "MB). Maximum allowed size is ").concat(Math.round(DEFAULT_MAX_FILE_SIZE / 1024 / 1024), "MB."));
4099
4120
  }
4100
- return [4 /*yield*/, tools.fs.writeFile(node_path.join(rootDirname_1, filepath), fileContent)];
4121
+ return [4 /*yield*/, tools.fs.writeFile(path.join(rootDirname_1, filepath), fileContent)];
4101
4122
  case 4:
4102
4123
  _h.sent();
4103
4124
  // TODO: [💵] Check the file security
@@ -4113,7 +4134,7 @@
4113
4134
  throw new EnvironmentMismatchError('Can not import file knowledge in non-file pipeline');
4114
4135
  // <- TODO: [🧠] What is the best error type here`
4115
4136
  }
4116
- filename_1 = node_path.join(rootDirname, knowledgeSourceContent).split('\\').join('/');
4137
+ filename_1 = path.join(rootDirname, knowledgeSourceContent).split('\\').join('/');
4117
4138
  fileExtension = getFileExtension(filename_1);
4118
4139
  mimeType = extensionToMimeType(fileExtension || '');
4119
4140
  return [4 /*yield*/, isFileExisting(filename_1, tools.fs)];
@@ -6880,7 +6901,7 @@
6880
6901
  next();
6881
6902
  });
6882
6903
  var runningExecutionTasks = [];
6883
- // TODO: !!!!!! Do here some garbage collection of finished tasks
6904
+ // TODO: [🧠] Do here some garbage collection of finished tasks
6884
6905
  app.get(['/', rootPath], function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6885
6906
  var _a, _b;
6886
6907
  var _this = this;
@@ -6929,20 +6950,19 @@
6929
6950
  });
6930
6951
  }); });
6931
6952
  app.get("".concat(rootPath, "/books"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6932
- var _a, _b, _c;
6933
- return __generator(this, function (_d) {
6934
- switch (_d.label) {
6953
+ var pipelines;
6954
+ return __generator(this, function (_a) {
6955
+ switch (_a.label) {
6935
6956
  case 0:
6936
- _b = (_a = response).send;
6937
- if (!(collection === null)) return [3 /*break*/, 1];
6938
- _c = [];
6939
- return [3 /*break*/, 3];
6940
- case 1: return [4 /*yield*/, collection.listPipelines()];
6941
- case 2:
6942
- _c = _d.sent();
6943
- _d.label = 3;
6944
- case 3:
6945
- _b.apply(_a, [_c]);
6957
+ if (collection === null) {
6958
+ response.status(500).send('No collection available');
6959
+ return [2 /*return*/];
6960
+ }
6961
+ return [4 /*yield*/, collection.listPipelines()];
6962
+ case 1:
6963
+ pipelines = _a.sent();
6964
+ // <- TODO: [🧠][👩🏾‍🤝‍🧑🏿] List `inputParameters` required for the execution
6965
+ response.send(pipelines);
6946
6966
  return [2 /*return*/];
6947
6967
  }
6948
6968
  });
@@ -6956,22 +6976,23 @@
6956
6976
  app.get("".concat(rootPath, "/executions/:taskId"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6957
6977
  var taskId, execution;
6958
6978
  return __generator(this, function (_a) {
6959
- taskId = request.query.taskId;
6979
+ taskId = request.params.taskId;
6960
6980
  execution = runningExecutionTasks.find(function (executionTask) { return executionTask.taskId === taskId; });
6961
6981
  if (execution === undefined) {
6962
6982
  response.status(404).send("Execution \"".concat(taskId, "\" not found"));
6963
6983
  return [2 /*return*/];
6964
6984
  }
6965
- response.send(execution);
6985
+ response.send(execution.currentValue);
6966
6986
  return [2 /*return*/];
6967
6987
  });
6968
6988
  }); });
6969
6989
  app.post("".concat(rootPath, "/executions/new"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
6970
- var inputParameters, pipelineUrl, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask;
6990
+ var inputParameters, pipelineUrl, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask, error_1;
6971
6991
  var _a;
6972
6992
  return __generator(this, function (_b) {
6973
6993
  switch (_b.label) {
6974
6994
  case 0:
6995
+ _b.trys.push([0, 6, , 7]);
6975
6996
  inputParameters = request.body.inputParameters;
6976
6997
  pipelineUrl = request.body.pipelineUrl || request.body.book;
6977
6998
  return [4 /*yield*/, (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl))];
@@ -7003,8 +7024,21 @@
7003
7024
  pipelineExecutor = createPipelineExecutor(__assign({ pipeline: pipeline, tools: tools }, options));
7004
7025
  executionTask = pipelineExecutor(inputParameters);
7005
7026
  runningExecutionTasks.push(executionTask);
7027
+ return [4 /*yield*/, waitasecond.forTime(10)];
7028
+ case 5:
7029
+ _b.sent();
7030
+ // <- Note: Wait for a while to wait for quick responses or sudden but asynchronous errors
7031
+ // <- TODO: Put this into configuration
7006
7032
  response.send(executionTask);
7007
- return [2 /*return*/];
7033
+ return [3 /*break*/, 7];
7034
+ case 6:
7035
+ error_1 = _b.sent();
7036
+ if (!(error_1 instanceof Error)) {
7037
+ throw error_1;
7038
+ }
7039
+ response.status(400).send({ error: serializeError(error_1) });
7040
+ return [3 /*break*/, 7];
7041
+ case 7: return [2 /*return*/];
7008
7042
  }
7009
7043
  });
7010
7044
  }); });
@@ -7069,7 +7103,7 @@
7069
7103
  }); };
7070
7104
  // -----------
7071
7105
  socket.on('prompt-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7072
- var identification, prompt, executionTools, llm, _a, promptResult, _b, error_1;
7106
+ var identification, prompt, executionTools, llm, _a, promptResult, _b, error_2;
7073
7107
  return __generator(this, function (_c) {
7074
7108
  switch (_c.label) {
7075
7109
  case 0:
@@ -7138,11 +7172,11 @@
7138
7172
  socket.emit('prompt-response', { promptResult: promptResult } /* <- Note: [🤛] */);
7139
7173
  return [3 /*break*/, 15];
7140
7174
  case 13:
7141
- error_1 = _c.sent();
7142
- if (!(error_1 instanceof Error)) {
7143
- throw error_1;
7175
+ error_2 = _c.sent();
7176
+ if (!(error_2 instanceof Error)) {
7177
+ throw error_2;
7144
7178
  }
7145
- socket.emit('error', serializeError(error_1) /* <- Note: [🤛] */);
7179
+ socket.emit('error', serializeError(error_2) /* <- Note: [🤛] */);
7146
7180
  return [3 /*break*/, 15];
7147
7181
  case 14:
7148
7182
  socket.disconnect();
@@ -7154,7 +7188,7 @@
7154
7188
  // -----------
7155
7189
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7156
7190
  socket.on('listModels-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7157
- var identification, executionTools, llm, models, error_2;
7191
+ var identification, executionTools, llm, models, error_3;
7158
7192
  return __generator(this, function (_a) {
7159
7193
  switch (_a.label) {
7160
7194
  case 0:
@@ -7175,11 +7209,11 @@
7175
7209
  socket.emit('listModels-response', { models: models } /* <- Note: [🤛] */);
7176
7210
  return [3 /*break*/, 6];
7177
7211
  case 4:
7178
- error_2 = _a.sent();
7179
- if (!(error_2 instanceof Error)) {
7180
- throw error_2;
7212
+ error_3 = _a.sent();
7213
+ if (!(error_3 instanceof Error)) {
7214
+ throw error_3;
7181
7215
  }
7182
- socket.emit('error', serializeError(error_2));
7216
+ socket.emit('error', serializeError(error_3));
7183
7217
  return [3 /*break*/, 6];
7184
7218
  case 5:
7185
7219
  socket.disconnect();
@@ -7191,7 +7225,7 @@
7191
7225
  // -----------
7192
7226
  // TODO: [👒] Listing models (and checking configuration) probbably should go through REST API not Socket.io
7193
7227
  socket.on('preparePipeline-request', function (request) { return __awaiter(_this, void 0, void 0, function () {
7194
- var identification, pipeline, executionTools, preparedPipeline, error_3;
7228
+ var identification, pipeline, executionTools, preparedPipeline, error_4;
7195
7229
  return __generator(this, function (_a) {
7196
7230
  switch (_a.label) {
7197
7231
  case 0:
@@ -7211,11 +7245,11 @@
7211
7245
  socket.emit('preparePipeline-response', { preparedPipeline: preparedPipeline } /* <- Note: [🤛] */);
7212
7246
  return [3 /*break*/, 6];
7213
7247
  case 4:
7214
- error_3 = _a.sent();
7215
- if (!(error_3 instanceof Error)) {
7216
- throw error_3;
7248
+ error_4 = _a.sent();
7249
+ if (!(error_4 instanceof Error)) {
7250
+ throw error_4;
7217
7251
  }
7218
- socket.emit('error', serializeError(error_3));
7252
+ socket.emit('error', serializeError(error_4));
7219
7253
  return [3 /*break*/, 6];
7220
7254
  case 5:
7221
7255
  socket.disconnect();