@promptbook/remote-server 0.89.0-8 → 0.89.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.
Files changed (50) hide show
  1. package/README.md +9 -11
  2. package/esm/index.es.js +570 -420
  3. package/esm/index.es.js.map +1 -1
  4. package/esm/typings/servers.d.ts +40 -0
  5. package/esm/typings/src/_packages/core.index.d.ts +12 -4
  6. package/esm/typings/src/_packages/remote-client.index.d.ts +6 -6
  7. package/esm/typings/src/_packages/remote-server.index.d.ts +6 -6
  8. package/esm/typings/src/_packages/types.index.d.ts +24 -14
  9. package/esm/typings/src/_packages/utils.index.d.ts +4 -0
  10. package/esm/typings/src/cli/cli-commands/login.d.ts +0 -1
  11. package/esm/typings/src/cli/common/$provideLlmToolsForCli.d.ts +16 -3
  12. package/esm/typings/src/cli/test/ptbk.d.ts +1 -1
  13. package/esm/typings/src/commands/EXPECT/expectCommandParser.d.ts +2 -0
  14. package/esm/typings/src/config.d.ts +10 -19
  15. package/esm/typings/src/errors/0-index.d.ts +8 -2
  16. package/esm/typings/src/errors/PipelineExecutionError.d.ts +1 -1
  17. package/esm/typings/src/errors/PromptbookFetchError.d.ts +9 -0
  18. package/esm/typings/src/errors/WrappedError.d.ts +10 -0
  19. package/esm/typings/src/errors/assertsError.d.ts +11 -0
  20. package/esm/typings/src/execution/PromptbookFetch.d.ts +1 -1
  21. package/esm/typings/src/formats/csv/utils/isValidCsvString.d.ts +9 -0
  22. package/esm/typings/src/formats/csv/utils/isValidCsvString.test.d.ts +1 -0
  23. package/esm/typings/src/formats/json/utils/isValidJsonString.d.ts +3 -0
  24. package/esm/typings/src/formats/xml/utils/isValidXmlString.d.ts +9 -0
  25. package/esm/typings/src/formats/xml/utils/isValidXmlString.test.d.ts +1 -0
  26. package/esm/typings/src/llm-providers/_common/register/$provideEnvFilename.d.ts +12 -0
  27. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsConfigurationFromEnv.d.ts +2 -8
  28. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForTestingAndScriptsAndPlayground.d.ts +2 -0
  29. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForWizzardOrCli.d.ts +15 -4
  30. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsFromEnv.d.ts +1 -0
  31. package/esm/typings/src/remote-server/openapi-types.d.ts +284 -0
  32. package/esm/typings/src/remote-server/openapi.d.ts +187 -0
  33. package/esm/typings/src/remote-server/socket-types/_subtypes/{PromptbookServer_Identification.d.ts → Identification.d.ts} +9 -3
  34. package/esm/typings/src/remote-server/socket-types/_subtypes/identificationToPromptbookToken.d.ts +11 -0
  35. package/esm/typings/src/remote-server/socket-types/_subtypes/promptbookTokenToIdentification.d.ts +10 -0
  36. package/esm/typings/src/remote-server/socket-types/listModels/PromptbookServer_ListModels_Request.d.ts +2 -2
  37. package/esm/typings/src/remote-server/socket-types/prepare/PromptbookServer_PreparePipeline_Request.d.ts +2 -2
  38. package/esm/typings/src/remote-server/socket-types/prompt/PromptbookServer_Prompt_Request.d.ts +2 -2
  39. package/esm/typings/src/remote-server/startRemoteServer.d.ts +1 -2
  40. package/esm/typings/src/remote-server/types/RemoteClientOptions.d.ts +2 -2
  41. package/esm/typings/src/remote-server/types/RemoteServerOptions.d.ts +57 -38
  42. package/esm/typings/src/scrapers/_common/utils/{scraperFetch.d.ts → promptbookFetch.d.ts} +2 -2
  43. package/esm/typings/src/storage/env-storage/$EnvStorage.d.ts +40 -0
  44. package/esm/typings/src/types/typeAliases.d.ts +26 -0
  45. package/package.json +11 -7
  46. package/umd/index.umd.js +492 -324
  47. package/umd/index.umd.js.map +1 -1
  48. package/esm/typings/src/cli/test/ptbk2.d.ts +0 -5
  49. package/esm/typings/src/playground/BrjappConnector.d.ts +0 -67
  50. package/esm/typings/src/playground/brjapp-api-schema.d.ts +0 -12879
package/umd/index.umd.js CHANGED
@@ -1,16 +1,34 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('colors'), require('express'), require('http'), require('socket.io'), require('spacetrim'), require('swagger-jsdoc'), require('swagger-ui-express'), require('waitasecond'), require('crypto'), require('child_process'), require('fs/promises'), require('path'), require('rxjs'), 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', 'swagger-jsdoc', 'swagger-ui-express', 'waitasecond', 'crypto', 'child_process', 'fs/promises', 'path', 'rxjs', '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.swaggerJsdoc, global.swaggerUi, global.waitasecond, global.crypto, global.child_process, global.promises, global.path, global.rxjs, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.cryptoJs, global.mimeTypes, global.papaparse));
5
- })(this, (function (exports, colors, express, http, socket_io, spaceTrim, swaggerJsdoc, swaggerUi, waitasecond, crypto, child_process, promises, path, rxjs, 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('express-openapi-validator'), require('swagger-ui-express'), require('waitasecond'), require('crypto'), require('child_process'), require('fs/promises'), require('path'), require('rxjs'), 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', 'express-openapi-validator', 'swagger-ui-express', 'waitasecond', 'crypto', 'child_process', 'fs/promises', 'path', 'rxjs', '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.OpenApiValidator, global.swaggerUi, global.waitasecond, global.crypto, global.child_process, global.promises, global.path, global.rxjs, global.prettier, global.parserHtml, global.hexEncoder, global.sha256, global.cryptoJs, global.mimeTypes, global.papaparse));
5
+ })(this, (function (exports, colors, express, http, socket_io, spaceTrim, OpenApiValidator, swaggerUi, waitasecond, crypto, child_process, promises, path, rxjs, 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
 
9
+ function _interopNamespace(e) {
10
+ if (e && e.__esModule) return e;
11
+ var n = Object.create(null);
12
+ if (e) {
13
+ Object.keys(e).forEach(function (k) {
14
+ if (k !== 'default') {
15
+ var d = Object.getOwnPropertyDescriptor(e, k);
16
+ Object.defineProperty(n, k, d.get ? d : {
17
+ enumerable: true,
18
+ get: function () { return e[k]; }
19
+ });
20
+ }
21
+ });
22
+ }
23
+ n["default"] = e;
24
+ return Object.freeze(n);
25
+ }
26
+
9
27
  var colors__default = /*#__PURE__*/_interopDefaultLegacy(colors);
10
28
  var express__default = /*#__PURE__*/_interopDefaultLegacy(express);
11
29
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
12
30
  var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
13
- var swaggerJsdoc__default = /*#__PURE__*/_interopDefaultLegacy(swaggerJsdoc);
31
+ var OpenApiValidator__namespace = /*#__PURE__*/_interopNamespace(OpenApiValidator);
14
32
  var swaggerUi__default = /*#__PURE__*/_interopDefaultLegacy(swaggerUi);
15
33
  var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
16
34
  var hexEncoder__default = /*#__PURE__*/_interopDefaultLegacy(hexEncoder);
@@ -30,7 +48,7 @@
30
48
  * @generated
31
49
  * @see https://github.com/webgptorg/promptbook
32
50
  */
33
- const PROMPTBOOK_ENGINE_VERSION = '0.89.0-8';
51
+ const PROMPTBOOK_ENGINE_VERSION = '0.89.0';
34
52
  /**
35
53
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
36
54
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -84,6 +102,7 @@
84
102
  * @public exported from `@promptbook/core`
85
103
  */
86
104
  const CLAIM = `It's time for a paradigm shift. The future of software in plain English, French or Latin`;
105
+ // <- TODO: [🐊] Pick the best claim
87
106
  /**
88
107
  * When the title is not provided, the default title is used
89
108
  *
@@ -116,6 +135,7 @@
116
135
  infinity: '(infinity; ∞)',
117
136
  negativeInfinity: '(negative infinity; -∞)',
118
137
  unserializable: '(unserializable value)',
138
+ circular: '(circular JSON)',
119
139
  };
120
140
  /**
121
141
  * Small number limit
@@ -155,7 +175,7 @@
155
175
  */
156
176
  const DEFAULT_MAX_EXECUTION_ATTEMPTS = 10; // <- TODO: [🤹‍♂️]
157
177
  // <- TODO: [🕝] Make also `BOOKS_DIRNAME_ALTERNATIVES`
158
- // TODO: !!!!!! Just .promptbook dir, hardocode others
178
+ // TODO: Just `.promptbook` in config, hardcode subfolders like `download-cache` or `execution-cache`
159
179
  /**
160
180
  * Where to store the temporary downloads
161
181
  *
@@ -210,6 +230,122 @@
210
230
  * TODO: [🧠][🧜‍♂️] Maybe join remoteServerUrl and path into single value
211
231
  */
212
232
 
233
+ /**
234
+ * Make error report URL for the given error
235
+ *
236
+ * @private private within the repository
237
+ */
238
+ function getErrorReportUrl(error) {
239
+ const report = {
240
+ title: `🐜 Error report from ${NAME}`,
241
+ body: spaceTrim__default["default"]((block) => `
242
+
243
+
244
+ \`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
245
+
246
+ \`\`\`
247
+ ${block(error.message || '(no error message)')}
248
+ \`\`\`
249
+
250
+
251
+ ## More info:
252
+
253
+ - **Promptbook engine version:** ${PROMPTBOOK_ENGINE_VERSION}
254
+ - **Book language version:** ${BOOK_LANGUAGE_VERSION}
255
+ - **Time:** ${new Date().toISOString()}
256
+
257
+ <details>
258
+ <summary>Stack trace:</summary>
259
+
260
+ ## Stack trace:
261
+
262
+ \`\`\`stacktrace
263
+ ${block(error.stack || '(empty)')}
264
+ \`\`\`
265
+ </details>
266
+
267
+ `),
268
+ };
269
+ const reportUrl = new URL(`https://github.com/webgptorg/promptbook/issues/new`);
270
+ reportUrl.searchParams.set('labels', 'bug');
271
+ reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
272
+ reportUrl.searchParams.set('title', report.title);
273
+ reportUrl.searchParams.set('body', report.body);
274
+ return reportUrl;
275
+ }
276
+
277
+ /**
278
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
279
+ *
280
+ * @public exported from `@promptbook/core`
281
+ */
282
+ class UnexpectedError extends Error {
283
+ constructor(message) {
284
+ super(spaceTrim.spaceTrim((block) => `
285
+ ${block(message)}
286
+
287
+ Note: This error should not happen.
288
+ It's probbably a bug in the pipeline collection
289
+
290
+ Please report issue:
291
+ ${block(getErrorReportUrl(new Error(message)).href)}
292
+
293
+ Or contact us on ${ADMIN_EMAIL}
294
+
295
+ `));
296
+ this.name = 'UnexpectedError';
297
+ Object.setPrototypeOf(this, UnexpectedError.prototype);
298
+ }
299
+ }
300
+
301
+ /**
302
+ * This error type indicates that somewhere in the code non-Error object was thrown and it was wrapped into the `WrappedError`
303
+ *
304
+ * @public exported from `@promptbook/core`
305
+ */
306
+ class WrappedError extends Error {
307
+ constructor(whatWasThrown) {
308
+ const tag = `[🤮]`;
309
+ console.error(tag, whatWasThrown);
310
+ super(spaceTrim.spaceTrim(`
311
+ Non-Error object was thrown
312
+
313
+ Note: Look for ${tag} in the console for more details
314
+ Please report issue on ${ADMIN_EMAIL}
315
+ `));
316
+ this.name = 'WrappedError';
317
+ Object.setPrototypeOf(this, WrappedError.prototype);
318
+ }
319
+ }
320
+
321
+ /**
322
+ * Helper used in catch blocks to assert that the error is an instance of `Error`
323
+ *
324
+ * @param whatWasThrown Any object that was thrown
325
+ * @returns Nothing if the error is an instance of `Error`
326
+ * @throws `WrappedError` or `UnexpectedError` if the error is not standard
327
+ *
328
+ * @private within the repository
329
+ */
330
+ function assertsError(whatWasThrown) {
331
+ // Case 1: Handle error which was rethrown as `WrappedError`
332
+ if (whatWasThrown instanceof WrappedError) {
333
+ const wrappedError = whatWasThrown;
334
+ throw wrappedError;
335
+ }
336
+ // Case 2: Handle unexpected errors
337
+ if (whatWasThrown instanceof UnexpectedError) {
338
+ const unexpectedError = whatWasThrown;
339
+ throw unexpectedError;
340
+ }
341
+ // Case 3: Handle standard errors - keep them up to consumer
342
+ if (whatWasThrown instanceof Error) {
343
+ return;
344
+ }
345
+ // Case 4: Handle non-standard errors - wrap them into `WrappedError` and throw
346
+ throw new WrappedError(whatWasThrown);
347
+ }
348
+
213
349
  /**
214
350
  * AuthenticationError is thrown from login function which is dependency of remote server
215
351
  *
@@ -254,7 +390,7 @@
254
390
  }
255
391
  }
256
392
  /**
257
- * TODO: !!!!!! Add id to all errors
393
+ * TODO: [🧠][🌂] Add id to all errors
258
394
  */
259
395
 
260
396
  /**
@@ -452,70 +588,15 @@
452
588
  }
453
589
 
454
590
  /**
455
- * Make error report URL for the given error
456
- *
457
- * @private private within the repository
458
- */
459
- function getErrorReportUrl(error) {
460
- const report = {
461
- title: `🐜 Error report from ${NAME}`,
462
- body: spaceTrim__default["default"]((block) => `
463
-
464
-
465
- \`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
466
-
467
- \`\`\`
468
- ${block(error.message || '(no error message)')}
469
- \`\`\`
470
-
471
-
472
- ## More info:
473
-
474
- - **Promptbook engine version:** ${PROMPTBOOK_ENGINE_VERSION}
475
- - **Book language version:** ${BOOK_LANGUAGE_VERSION}
476
- - **Time:** ${new Date().toISOString()}
477
-
478
- <details>
479
- <summary>Stack trace:</summary>
480
-
481
- ## Stack trace:
482
-
483
- \`\`\`stacktrace
484
- ${block(error.stack || '(empty)')}
485
- \`\`\`
486
- </details>
487
-
488
- `),
489
- };
490
- const reportUrl = new URL(`https://github.com/webgptorg/promptbook/issues/new`);
491
- reportUrl.searchParams.set('labels', 'bug');
492
- reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
493
- reportUrl.searchParams.set('title', report.title);
494
- reportUrl.searchParams.set('body', report.body);
495
- return reportUrl;
496
- }
497
-
498
- /**
499
- * This error type indicates that the error should not happen and its last check before crashing with some other error
591
+ * Error thrown when a fetch request fails
500
592
  *
501
593
  * @public exported from `@promptbook/core`
502
594
  */
503
- class UnexpectedError extends Error {
595
+ class PromptbookFetchError extends Error {
504
596
  constructor(message) {
505
- super(spaceTrim.spaceTrim((block) => `
506
- ${block(message)}
507
-
508
- Note: This error should not happen.
509
- It's probbably a bug in the pipeline collection
510
-
511
- Please report issue:
512
- ${block(getErrorReportUrl(new Error(message)).href)}
513
-
514
- Or contact us on ${ADMIN_EMAIL}
515
-
516
- `));
517
- this.name = 'UnexpectedError';
518
- Object.setPrototypeOf(this, UnexpectedError.prototype);
597
+ super(message);
598
+ this.name = 'PromptbookFetchError';
599
+ Object.setPrototypeOf(this, PromptbookFetchError.prototype);
519
600
  }
520
601
  }
521
602
 
@@ -539,7 +620,10 @@
539
620
  PipelineExecutionError,
540
621
  PipelineLogicError,
541
622
  PipelineUrlError,
623
+ AuthenticationError,
624
+ PromptbookFetchError,
542
625
  UnexpectedError,
626
+ WrappedError,
543
627
  // TODO: [🪑]> VersionMismatchError,
544
628
  };
545
629
  /**
@@ -556,7 +640,6 @@
556
640
  TypeError,
557
641
  URIError,
558
642
  AggregateError,
559
- AuthenticationError,
560
643
  /*
561
644
  Note: Not widely supported
562
645
  > InternalError,
@@ -799,9 +882,7 @@
799
882
  return result.trim();
800
883
  }
801
884
  catch (error) {
802
- if (!(error instanceof Error)) {
803
- throw error;
804
- }
885
+ assertsError(error);
805
886
  return null;
806
887
  }
807
888
  }
@@ -879,9 +960,7 @@
879
960
  return result.trim() + toExec;
880
961
  }
881
962
  catch (error) {
882
- if (!(error instanceof Error)) {
883
- throw error;
884
- }
963
+ assertsError(error);
885
964
  return null;
886
965
  }
887
966
  }
@@ -912,9 +991,7 @@
912
991
  throw new Error(`Can not locate app ${appName} on Windows.`);
913
992
  }
914
993
  catch (error) {
915
- if (!(error instanceof Error)) {
916
- throw error;
917
- }
994
+ assertsError(error);
918
995
  return null;
919
996
  }
920
997
  }
@@ -1152,9 +1229,7 @@
1152
1229
  JSON.stringify(value); // <- TODO: [0]
1153
1230
  }
1154
1231
  catch (error) {
1155
- if (!(error instanceof Error)) {
1156
- throw error;
1157
- }
1232
+ assertsError(error);
1158
1233
  throw new UnexpectedError(spaceTrim__default["default"]((block) => `
1159
1234
  \`${name}\` is not serializable
1160
1235
 
@@ -1754,6 +1829,9 @@
1754
1829
  /**
1755
1830
  * Function isValidJsonString will tell you if the string is valid JSON or not
1756
1831
  *
1832
+ * @param value The string to check
1833
+ * @returns True if the string is a valid JSON string, false otherwise
1834
+ *
1757
1835
  * @public exported from `@promptbook/utils`
1758
1836
  */
1759
1837
  function isValidJsonString(value /* <- [👨‍⚖️] */) {
@@ -1762,9 +1840,7 @@
1762
1840
  return true;
1763
1841
  }
1764
1842
  catch (error) {
1765
- if (!(error instanceof Error)) {
1766
- throw error;
1767
- }
1843
+ assertsError(error);
1768
1844
  if (error.message.includes('Unexpected token')) {
1769
1845
  return false;
1770
1846
  }
@@ -1902,8 +1978,8 @@
1902
1978
  updatedAt = new Date();
1903
1979
  errors.push(...executionResult.errors);
1904
1980
  warnings.push(...executionResult.warnings);
1905
- // <- TODO: !!! Only unique errors and warnings should be added (or filtered)
1906
- // TODO: [🧠] !!! errors, warning, isSuccessful are redundant both in `ExecutionTask` and `ExecutionTask.currentValue`
1981
+ // <- TODO: [🌂] Only unique errors and warnings should be added (or filtered)
1982
+ // TODO: [🧠] !! errors, warning, isSuccessful are redundant both in `ExecutionTask` and `ExecutionTask.currentValue`
1907
1983
  // Also maybe move `ExecutionTask.currentValue.usage` -> `ExecutionTask.usage`
1908
1984
  // And delete `ExecutionTask.currentValue.preparedPipeline`
1909
1985
  assertsTaskSuccessful(executionResult);
@@ -1913,6 +1989,7 @@
1913
1989
  partialResultSubject.next(executionResult);
1914
1990
  }
1915
1991
  catch (error) {
1992
+ assertsError(error);
1916
1993
  status = 'ERROR';
1917
1994
  errors.push(error);
1918
1995
  partialResultSubject.error(error);
@@ -2723,14 +2800,15 @@
2723
2800
  }
2724
2801
  }
2725
2802
  catch (error) {
2726
- if (!(error instanceof Error) || error instanceof UnexpectedError) {
2803
+ assertsError(error);
2804
+ if (error instanceof UnexpectedError) {
2727
2805
  throw error;
2728
2806
  }
2729
2807
  errors.push({ llmExecutionTools, error });
2730
2808
  }
2731
2809
  }
2732
2810
  if (errors.length === 1) {
2733
- throw errors[0];
2811
+ throw errors[0].error;
2734
2812
  }
2735
2813
  else if (errors.length > 1) {
2736
2814
  throw new PipelineExecutionError(
@@ -3564,17 +3642,22 @@
3564
3642
  /**
3565
3643
  * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
3566
3644
  *
3567
- * @private as default `fetch` function used in Promptbook scrapers
3645
+ * @public exported from `@promptbook/core`
3568
3646
  */
3569
- const scraperFetch = async (url, init) => {
3647
+ const promptbookFetch = async (urlOrRequest, init) => {
3570
3648
  try {
3571
- return await fetch(url, init);
3649
+ return await fetch(urlOrRequest, init);
3572
3650
  }
3573
3651
  catch (error) {
3574
- if (!(error instanceof Error)) {
3575
- throw error;
3652
+ assertsError(error);
3653
+ let url;
3654
+ if (typeof urlOrRequest === 'string') {
3655
+ url = urlOrRequest;
3576
3656
  }
3577
- throw new KnowledgeScrapeError(spaceTrim__default["default"]((block) => `
3657
+ else if (urlOrRequest instanceof Request) {
3658
+ url = urlOrRequest.url;
3659
+ }
3660
+ throw new PromptbookFetchError(spaceTrim__default["default"]((block) => `
3578
3661
  Can not fetch "${url}"
3579
3662
 
3580
3663
  Fetch error:
@@ -3595,7 +3678,7 @@
3595
3678
  async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3596
3679
  // console.log('!! makeKnowledgeSourceHandler', knowledgeSource);
3597
3680
  var _a;
3598
- const { fetch = scraperFetch } = tools;
3681
+ const { fetch = promptbookFetch } = tools;
3599
3682
  const { knowledgeSourceContent } = knowledgeSource;
3600
3683
  let { name } = knowledgeSource;
3601
3684
  const { rootDirname = null,
@@ -3797,9 +3880,7 @@
3797
3880
  knowledgePreparedUnflatten[index] = pieces;
3798
3881
  }
3799
3882
  catch (error) {
3800
- if (!(error instanceof Error)) {
3801
- throw error;
3802
- }
3883
+ assertsError(error);
3803
3884
  console.warn(error);
3804
3885
  // <- TODO: [🏮] Some standard way how to transform errors into warnings and how to handle non-critical fails during the tasks
3805
3886
  }
@@ -4091,13 +4172,19 @@
4091
4172
  return value.toISOString();
4092
4173
  }
4093
4174
  else {
4094
- return JSON.stringify(value);
4175
+ try {
4176
+ return JSON.stringify(value);
4177
+ }
4178
+ catch (error) {
4179
+ if (error instanceof TypeError && error.message.includes('circular structure')) {
4180
+ return VALUE_STRINGS.circular;
4181
+ }
4182
+ throw error;
4183
+ }
4095
4184
  }
4096
4185
  }
4097
4186
  catch (error) {
4098
- if (!(error instanceof Error)) {
4099
- throw error;
4100
- }
4187
+ assertsError(error);
4101
4188
  console.error(error);
4102
4189
  return VALUE_STRINGS.unserializable;
4103
4190
  }
@@ -4154,9 +4241,7 @@
4154
4241
  }
4155
4242
  }
4156
4243
  catch (error) {
4157
- if (!(error instanceof Error)) {
4158
- throw error;
4159
- }
4244
+ assertsError(error);
4160
4245
  throw new ParseError(spaceTrim.spaceTrim((block) => `
4161
4246
  Can not extract variables from the script
4162
4247
  ${block(error.stack || error.message)}
@@ -4275,6 +4360,28 @@
4275
4360
  // encoding: 'utf-8',
4276
4361
  });
4277
4362
 
4363
+ /**
4364
+ * Function to check if a string is valid CSV
4365
+ *
4366
+ * @param value The string to check
4367
+ * @returns True if the string is a valid CSV string, false otherwise
4368
+ *
4369
+ * @public exported from `@promptbook/utils`
4370
+ */
4371
+ function isValidCsvString(value) {
4372
+ try {
4373
+ // A simple check for CSV format: at least one comma and no invalid characters
4374
+ if (value.includes(',') && /^[\w\s,"']+$/.test(value)) {
4375
+ return true;
4376
+ }
4377
+ return false;
4378
+ }
4379
+ catch (error) {
4380
+ assertsError(error);
4381
+ return false;
4382
+ }
4383
+ }
4384
+
4278
4385
  /**
4279
4386
  * Definition for CSV spreadsheet
4280
4387
  *
@@ -4285,7 +4392,7 @@
4285
4392
  formatName: 'CSV',
4286
4393
  aliases: ['SPREADSHEET', 'TABLE'],
4287
4394
  isValid(value, settings, schema) {
4288
- return true;
4395
+ return isValidCsvString(value);
4289
4396
  },
4290
4397
  canBeValid(partialValue, settings, schema) {
4291
4398
  return true;
@@ -4439,6 +4546,30 @@
4439
4546
  * TODO: [🏢] Allow to expect something inside each item of list and other formats
4440
4547
  */
4441
4548
 
4549
+ /**
4550
+ * Function to check if a string is valid XML
4551
+ *
4552
+ * @param value
4553
+ * @returns True if the string is a valid XML string, false otherwise
4554
+ *
4555
+ * @public exported from `@promptbook/utils`
4556
+ */
4557
+ function isValidXmlString(value) {
4558
+ try {
4559
+ const parser = new DOMParser();
4560
+ const parsedDocument = parser.parseFromString(value, 'application/xml');
4561
+ const parserError = parsedDocument.getElementsByTagName('parsererror');
4562
+ if (parserError.length > 0) {
4563
+ return false;
4564
+ }
4565
+ return true;
4566
+ }
4567
+ catch (error) {
4568
+ assertsError(error);
4569
+ return false;
4570
+ }
4571
+ }
4572
+
4442
4573
  /**
4443
4574
  * Definition for XML format
4444
4575
  *
@@ -4448,7 +4579,7 @@
4448
4579
  formatName: 'XML',
4449
4580
  mimeType: 'application/xml',
4450
4581
  isValid(value, settings, schema) {
4451
- return true;
4582
+ return isValidXmlString(value);
4452
4583
  },
4453
4584
  canBeValid(partialValue, settings, schema) {
4454
4585
  return true;
@@ -5038,9 +5169,7 @@
5038
5169
  break scripts;
5039
5170
  }
5040
5171
  catch (error) {
5041
- if (!(error instanceof Error)) {
5042
- throw error;
5043
- }
5172
+ assertsError(error);
5044
5173
  if (error instanceof UnexpectedError) {
5045
5174
  throw error;
5046
5175
  }
@@ -5110,9 +5239,7 @@
5110
5239
  break scripts;
5111
5240
  }
5112
5241
  catch (error) {
5113
- if (!(error instanceof Error)) {
5114
- throw error;
5115
- }
5242
+ assertsError(error);
5116
5243
  if (error instanceof UnexpectedError) {
5117
5244
  throw error;
5118
5245
  }
@@ -5733,9 +5860,7 @@
5733
5860
  await Promise.all(resolving);
5734
5861
  }
5735
5862
  catch (error /* <- Note: [3] */) {
5736
- if (!(error instanceof Error)) {
5737
- throw error;
5738
- }
5863
+ assertsError(error);
5739
5864
  // Note: No need to rethrow UnexpectedError
5740
5865
  // if (error instanceof UnexpectedError) {
5741
5866
  // Note: Count usage, [🧠] Maybe put to separate function executionReportJsonToUsage + DRY [🤹‍♂️]
@@ -6637,9 +6762,7 @@
6637
6762
  }
6638
6763
  }
6639
6764
  catch (error) {
6640
- if (!(error instanceof Error)) {
6641
- throw error;
6642
- }
6765
+ assertsError(error);
6643
6766
  if (error instanceof ReferenceError) {
6644
6767
  const undefinedName = error.message.split(' ')[0];
6645
6768
  /*
@@ -6719,6 +6842,198 @@
6719
6842
  * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
6720
6843
  */
6721
6844
 
6845
+ // TODO: !!!! List running services from REMOTE_SERVER_URLS
6846
+ // TODO: !!!! Import directly from YML
6847
+ /**
6848
+ * @private !!!! Decide how to expose this
6849
+ */
6850
+ const openapiJson = {
6851
+ openapi: '3.0.0',
6852
+ info: {
6853
+ title: 'Promptbook Remote Server API (!!!! From TS)',
6854
+ version: '1.0.0',
6855
+ description: 'API documentation for the Promptbook Remote Server',
6856
+ },
6857
+ paths: {
6858
+ '/': {
6859
+ get: {
6860
+ summary: 'Get server details',
6861
+ description: 'Returns details about the Promptbook server.',
6862
+ responses: {
6863
+ '200': {
6864
+ description: 'Server details in markdown format.',
6865
+ },
6866
+ },
6867
+ },
6868
+ },
6869
+ '/login': {
6870
+ post: {
6871
+ summary: 'Login to the server',
6872
+ description: 'Login to the server and get identification.',
6873
+ requestBody: {
6874
+ required: true,
6875
+ content: {
6876
+ 'application/json': {
6877
+ schema: {
6878
+ type: 'object',
6879
+ properties: {
6880
+ username: {
6881
+ type: 'string',
6882
+ },
6883
+ password: {
6884
+ type: 'string',
6885
+ },
6886
+ appId: {
6887
+ type: 'string',
6888
+ },
6889
+ },
6890
+ },
6891
+ },
6892
+ },
6893
+ },
6894
+ responses: {
6895
+ '200': {
6896
+ description: 'Successful login',
6897
+ content: {
6898
+ 'application/json': {
6899
+ schema: {
6900
+ type: 'object',
6901
+ properties: {
6902
+ identification: {
6903
+ type: 'object',
6904
+ },
6905
+ },
6906
+ },
6907
+ },
6908
+ },
6909
+ },
6910
+ },
6911
+ },
6912
+ },
6913
+ '/books': {
6914
+ get: {
6915
+ summary: 'List all books',
6916
+ description: 'Returns a list of all available books in the collection.',
6917
+ responses: {
6918
+ '200': {
6919
+ description: 'A list of books.',
6920
+ content: {
6921
+ 'application/json': {
6922
+ schema: {
6923
+ type: 'array',
6924
+ items: {
6925
+ type: 'string',
6926
+ },
6927
+ },
6928
+ },
6929
+ },
6930
+ },
6931
+ },
6932
+ },
6933
+ },
6934
+ '/books/{bookId}': {
6935
+ get: {
6936
+ summary: 'Get book content',
6937
+ description: 'Returns the content of a specific book.',
6938
+ parameters: [
6939
+ {
6940
+ in: 'path',
6941
+ name: 'bookId',
6942
+ required: true,
6943
+ schema: {
6944
+ type: 'string',
6945
+ },
6946
+ description: 'The ID of the book to retrieve.',
6947
+ },
6948
+ ],
6949
+ responses: {
6950
+ '200': {
6951
+ description: 'The content of the book.',
6952
+ content: {
6953
+ 'text/markdown': {
6954
+ schema: {
6955
+ type: 'string',
6956
+ },
6957
+ },
6958
+ },
6959
+ },
6960
+ '404': {
6961
+ description: 'Book not found.',
6962
+ },
6963
+ },
6964
+ },
6965
+ },
6966
+ '/executions': {
6967
+ get: {
6968
+ summary: 'List all executions',
6969
+ description: 'Returns a list of all running execution tasks.',
6970
+ responses: {
6971
+ '200': {
6972
+ description: 'A list of execution tasks.',
6973
+ content: {
6974
+ 'application/json': {
6975
+ schema: {
6976
+ type: 'array',
6977
+ items: {
6978
+ type: 'object',
6979
+ },
6980
+ },
6981
+ },
6982
+ },
6983
+ },
6984
+ },
6985
+ },
6986
+ },
6987
+ '/executions/new': {
6988
+ post: {
6989
+ summary: 'Start a new execution',
6990
+ description: 'Starts a new execution task for a given pipeline.',
6991
+ requestBody: {
6992
+ required: true,
6993
+ content: {
6994
+ 'application/json': {
6995
+ schema: {
6996
+ type: 'object',
6997
+ properties: {
6998
+ pipelineUrl: {
6999
+ type: 'string',
7000
+ },
7001
+ inputParameters: {
7002
+ type: 'object',
7003
+ },
7004
+ identification: {
7005
+ type: 'object',
7006
+ },
7007
+ },
7008
+ },
7009
+ },
7010
+ },
7011
+ },
7012
+ responses: {
7013
+ '200': {
7014
+ description: 'The newly created execution task.',
7015
+ content: {
7016
+ 'application/json': {
7017
+ schema: {
7018
+ type: 'object',
7019
+ },
7020
+ },
7021
+ },
7022
+ },
7023
+ '400': {
7024
+ description: 'Invalid input.',
7025
+ },
7026
+ },
7027
+ },
7028
+ },
7029
+ },
7030
+ components: {},
7031
+ tags: [],
7032
+ };
7033
+ /**
7034
+ * Note: [💞] Ignore a discrepancy between file name and entity name
7035
+ */
7036
+
6722
7037
  /**
6723
7038
  * Remote server is a proxy server that uses its execution tools internally and exposes the executor interface externally.
6724
7039
  *
@@ -6729,7 +7044,7 @@
6729
7044
  * @public exported from `@promptbook/remote-server`
6730
7045
  */
6731
7046
  function startRemoteServer(options) {
6732
- const { port, collection, createLlmExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, login, } = {
7047
+ const { port, collection, createLlmExecutionTools, createExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, login, } = {
6733
7048
  isAnonymousModeAllowed: false,
6734
7049
  isApplicationModeAllowed: false,
6735
7050
  collection: null,
@@ -6737,22 +7052,6 @@
6737
7052
  login: null,
6738
7053
  ...options,
6739
7054
  };
6740
- // <- TODO: [🦪] Some helper type to be able to use discriminant union types with destructuring
6741
- let { rootPath = '/' } = options;
6742
- if (!rootPath.startsWith('/')) {
6743
- rootPath = `/${rootPath}`;
6744
- } /* not else */
6745
- if (rootPath.endsWith('/')) {
6746
- rootPath = rootPath.slice(0, -1);
6747
- } /* not else */
6748
- if (rootPath === '/') {
6749
- rootPath = '';
6750
- }
6751
- const socketioPath = '/' +
6752
- `${rootPath}/socket.io`
6753
- .split('/')
6754
- .filter((part) => part !== '')
6755
- .join('/');
6756
7055
  const startupDate = new Date();
6757
7056
  async function getExecutionToolsFromIdentification(identification) {
6758
7057
  if (identification === null || identification === undefined) {
@@ -6775,23 +7074,25 @@
6775
7074
  }
6776
7075
  else if (isAnonymous === false && createLlmExecutionTools !== null) {
6777
7076
  // Note: Application mode
6778
- const { appId, userId, customOptions } = identification;
6779
- llm = await createLlmExecutionTools({
6780
- appId,
6781
- userId,
6782
- customOptions,
6783
- });
7077
+ llm = await createLlmExecutionTools(identification);
6784
7078
  }
6785
7079
  else {
6786
7080
  throw new PipelineExecutionError(`You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured`);
6787
7081
  }
6788
- const fs = $provideFilesystemForNode();
6789
- const executables = await $provideExecutablesForNode();
7082
+ const customExecutionTools = createExecutionTools ? await createExecutionTools(identification) : {};
7083
+ const fs = customExecutionTools.fs || $provideFilesystemForNode();
7084
+ const executables = customExecutionTools.executables || (await $provideExecutablesForNode());
7085
+ const scrapers = customExecutionTools.scrapers || (await $provideScrapersForNode({ fs, llm, executables }));
7086
+ const script = customExecutionTools.script || (await $provideScriptingForNode({}));
7087
+ const fetch = customExecutionTools.fetch || promptbookFetch;
7088
+ const userInterface = customExecutionTools.userInterface || undefined;
6790
7089
  const tools = {
6791
7090
  llm,
6792
7091
  fs,
6793
- scrapers: await $provideScrapersForNode({ fs, llm, executables }),
6794
- script: await $provideScriptingForNode({}),
7092
+ scrapers,
7093
+ script,
7094
+ fetch,
7095
+ userInterface,
6795
7096
  };
6796
7097
  return tools;
6797
7098
  }
@@ -6801,39 +7102,27 @@
6801
7102
  response.setHeader('X-Powered-By', 'Promptbook engine');
6802
7103
  next();
6803
7104
  });
6804
- const swaggerOptions = {
6805
- definition: {
6806
- openapi: '3.0.0',
6807
- info: {
6808
- title: 'Promptbook Remote Server API',
6809
- version: '1.0.0',
6810
- description: 'API documentation for the Promptbook Remote Server',
6811
- },
6812
- servers: [
6813
- {
6814
- url: `http://localhost:${port}${rootPath}`,
6815
- // <- TODO: !!!!! Probbably: Pass `remoteServerUrl` instead of `port` and `rootPath`
6816
- },
6817
- ],
7105
+ // TODO: !!!! Expose openapiJson to consumer and also allow to add new routes
7106
+ app.use(OpenApiValidator__namespace.middleware({
7107
+ apiSpec: openapiJson,
7108
+ ignorePaths(path) {
7109
+ return path.startsWith('/api-docs') || path.startsWith('/swagger') || path.startsWith('/openapi');
6818
7110
  },
6819
- apis: ['./src/remote-server/**/*.ts'], // Adjust path as needed
6820
- };
6821
- const swaggerSpec = swaggerJsdoc__default["default"](swaggerOptions);
6822
- app.use([`/api-docs`, `${rootPath}/api-docs`], swaggerUi__default["default"].serve, swaggerUi__default["default"].setup(swaggerSpec));
7111
+ validateRequests: true,
7112
+ validateResponses: true,
7113
+ }));
7114
+ app.use([`/api-docs`, `/swagger`], swaggerUi__default["default"].serve, swaggerUi__default["default"].setup(openapiJson, {
7115
+ // customCss: '.swagger-ui .topbar { display: none }',
7116
+ // customSiteTitle: 'BRJ API',
7117
+ // customfavIcon: 'https://brj.app/favicon.ico',
7118
+ }));
7119
+ app.get(`/openapi`, (request, response) => {
7120
+ response.json(openapiJson);
7121
+ });
6823
7122
  const runningExecutionTasks = [];
6824
7123
  // <- TODO: [🤬] Identify the users
6825
7124
  // TODO: [🧠] Do here some garbage collection of finished tasks
6826
- /**
6827
- * @swagger
6828
- * /:
6829
- * get:
6830
- * summary: Get server details
6831
- * description: Returns details about the Promptbook server.
6832
- * responses:
6833
- * 200:
6834
- * description: Server details in markdown format.
6835
- */
6836
- app.get(['/', rootPath], async (request, response) => {
7125
+ app.get('/', async (request, response) => {
6837
7126
  var _a;
6838
7127
  if ((_a = request.url) === null || _a === void 0 ? void 0 : _a.includes('socket.io')) {
6839
7128
  return;
@@ -6852,8 +7141,6 @@
6852
7141
  ## Details
6853
7142
 
6854
7143
  **Server port:** ${port}
6855
- **Server root path:** ${rootPath}
6856
- **Socket.io path:** ${socketioPath}
6857
7144
  **Startup date:** ${startupDate.toISOString()}
6858
7145
  **Anonymouse mode:** ${isAnonymousModeAllowed ? 'enabled' : 'disabled'}
6859
7146
  **Application mode:** ${isApplicationModeAllowed ? 'enabled' : 'disabled'}
@@ -6892,38 +7179,7 @@
6892
7179
  https://github.com/webgptorg/promptbook
6893
7180
  `));
6894
7181
  });
6895
- /**
6896
- * @swagger
6897
- *
6898
- * /login:
6899
- * post:
6900
- * summary: Login to the server
6901
- * description: Login to the server and get identification.
6902
- * requestBody:
6903
- * required: true
6904
- * content:
6905
- * application/json:
6906
- * schema:
6907
- * type: object
6908
- * properties:
6909
- * username:
6910
- * type: string
6911
- * password:
6912
- * type: string
6913
- * appId:
6914
- * type: string
6915
- * responses:
6916
- * 200:
6917
- * description: Successful login
6918
- * content:
6919
- * application/json:
6920
- * schema:
6921
- * type: object
6922
- * properties:
6923
- * identification:
6924
- * type: object
6925
- */
6926
- app.post([`/login`, `${rootPath}/login`], async (request, response) => {
7182
+ app.post(`/login`, async (request, response) => {
6927
7183
  if (!isApplicationModeAllowed || login === null) {
6928
7184
  response.status(400).send('Application mode is not allowed');
6929
7185
  return;
@@ -6948,9 +7204,7 @@
6948
7204
  return;
6949
7205
  }
6950
7206
  catch (error) {
6951
- if (!(error instanceof Error)) {
6952
- throw error;
6953
- }
7207
+ assertsError(error);
6954
7208
  if (error instanceof AuthenticationError) {
6955
7209
  response.status(401).send({
6956
7210
  isSuccess: false,
@@ -6965,23 +7219,7 @@
6965
7219
  response.status(400).send({ error: serializeError(error) });
6966
7220
  }
6967
7221
  });
6968
- /**
6969
- * @swagger
6970
- * /books:
6971
- * get:
6972
- * summary: List all books
6973
- * description: Returns a list of all available books in the collection.
6974
- * responses:
6975
- * 200:
6976
- * description: A list of books.
6977
- * content:
6978
- * application/json:
6979
- * schema:
6980
- * type: array
6981
- * items:
6982
- * type: string
6983
- */
6984
- app.get([`/books`, `${rootPath}/books`], async (request, response) => {
7222
+ app.get(`/books`, async (request, response) => {
6985
7223
  if (collection === null) {
6986
7224
  response.status(500).send('No collection available');
6987
7225
  return;
@@ -6991,30 +7229,7 @@
6991
7229
  response.send(pipelines);
6992
7230
  });
6993
7231
  // TODO: [🧠] Is it secure / good idea to expose source codes of hosted books
6994
- /**
6995
- * @swagger
6996
- * /books/{bookId}:
6997
- * get:
6998
- * summary: Get book content
6999
- * description: Returns the content of a specific book.
7000
- * parameters:
7001
- * - in: path
7002
- * name: bookId
7003
- * required: true
7004
- * schema:
7005
- * type: string
7006
- * description: The ID of the book to retrieve.
7007
- * responses:
7008
- * 200:
7009
- * description: The content of the book.
7010
- * content:
7011
- * text/markdown:
7012
- * schema:
7013
- * type: string
7014
- * 404:
7015
- * description: Book not found.
7016
- */
7017
- app.get([`/books/*`, `${rootPath}/books/*`], async (request, response) => {
7232
+ app.get(`/books/*`, async (request, response) => {
7018
7233
  try {
7019
7234
  if (collection === null) {
7020
7235
  response.status(500).send('No collection nor books available');
@@ -7033,9 +7248,7 @@
7033
7248
  .send(source.content);
7034
7249
  }
7035
7250
  catch (error) {
7036
- if (!(error instanceof Error)) {
7037
- throw error;
7038
- }
7251
+ assertsError(error);
7039
7252
  response
7040
7253
  .status(404)
7041
7254
  .send({ error: serializeError(error) });
@@ -7068,26 +7281,10 @@
7068
7281
  };
7069
7282
  }
7070
7283
  }
7071
- /**
7072
- * @swagger
7073
- * /executions:
7074
- * get:
7075
- * summary: List all executions
7076
- * description: Returns a list of all running execution tasks.
7077
- * responses:
7078
- * 200:
7079
- * description: A list of execution tasks.
7080
- * content:
7081
- * application/json:
7082
- * schema:
7083
- * type: array
7084
- * items:
7085
- * type: object
7086
- */
7087
- app.get([`/executions`, `${rootPath}/executions`], async (request, response) => {
7088
- response.send(runningExecutionTasks.map((runningExecutionTask) => exportExecutionTask(runningExecutionTask, false)));
7284
+ app.get(`/executions`, async (request, response) => {
7285
+ response.send(runningExecutionTasks.map((runningExecutionTask) => exportExecutionTask(runningExecutionTask, false)) /* <- TODO: satisfies paths['/executions']['get']['responses']['200']['content']['application/json'] */);
7089
7286
  });
7090
- app.get([`/executions/last`, `${rootPath}/executions/last`], async (request, response) => {
7287
+ app.get(`/executions/last`, async (request, response) => {
7091
7288
  // TODO: [🤬] Filter only for user
7092
7289
  if (runningExecutionTasks.length === 0) {
7093
7290
  response.status(404).send('No execution tasks found');
@@ -7096,7 +7293,7 @@
7096
7293
  const lastExecutionTask = runningExecutionTasks[runningExecutionTasks.length - 1];
7097
7294
  response.send(exportExecutionTask(lastExecutionTask, true));
7098
7295
  });
7099
- app.get([`/executions/:taskId`, `${rootPath}/executions/:taskId`], async (request, response) => {
7296
+ app.get(`/executions/:taskId`, async (request, response) => {
7100
7297
  const { taskId } = request.params;
7101
7298
  // TODO: [🤬] Filter only for user
7102
7299
  const executionTask = runningExecutionTasks.find((executionTask) => executionTask.taskId === taskId);
@@ -7108,39 +7305,12 @@
7108
7305
  }
7109
7306
  response.send(exportExecutionTask(executionTask, true));
7110
7307
  });
7111
- /**
7112
- * @swagger
7113
- * /executions/new:
7114
- * post:
7115
- * summary: Start a new execution
7116
- * description: Starts a new execution task for a given pipeline.
7117
- * requestBody:
7118
- * required: true
7119
- * content:
7120
- * application/json:
7121
- * schema:
7122
- * type: object
7123
- * properties:
7124
- * pipelineUrl:
7125
- * type: string
7126
- * inputParameters:
7127
- * type: object
7128
- * identification:
7129
- * type: object
7130
- * responses:
7131
- * 200:
7132
- * description: The newly created execution task.
7133
- * content:
7134
- * application/json:
7135
- * schema:
7136
- * type: object
7137
- * 400:
7138
- * description: Invalid input.
7139
- */
7140
- app.post([`/executions/new`, `${rootPath}/executions/new`], async (request, response) => {
7308
+ app.post(`/executions/new`, async (request, response) => {
7141
7309
  try {
7142
7310
  const { inputParameters, identification /* <- [🤬] */ } = request.body;
7143
- const pipelineUrl = request.body.pipelineUrl || request.body.book;
7311
+ const pipelineUrl = request.body
7312
+ .pipelineUrl /* <- TODO: as paths['/executions/new']['post']['requestBody']['content']['application/json'] */ ||
7313
+ request.body.book;
7144
7314
  // TODO: [🧠] Check `pipelineUrl` and `inputParameters` here or it should be responsibility of `collection.getPipelineByUrl` and `pipelineExecutor`
7145
7315
  const pipeline = await (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl));
7146
7316
  if (pipeline === undefined) {
@@ -7154,7 +7324,7 @@
7154
7324
  await waitasecond.forTime(10);
7155
7325
  // <- Note: Wait for a while to wait for quick responses or sudden but asynchronous errors
7156
7326
  // <- TODO: Put this into configuration
7157
- response.send(executionTask);
7327
+ response.send(executionTask /* <- TODO: satisfies paths['/executions/new']['post']['responses']['200']['content']['application/json'] */);
7158
7328
  /*/
7159
7329
  executionTask.asObservable().subscribe({
7160
7330
  next(partialResult) {
@@ -7174,19 +7344,24 @@
7174
7344
  */
7175
7345
  }
7176
7346
  catch (error) {
7177
- if (!(error instanceof Error)) {
7178
- throw error;
7179
- }
7347
+ assertsError(error);
7180
7348
  response.status(400).send({ error: serializeError(error) });
7181
7349
  }
7182
7350
  });
7351
+ /**
7352
+ * Catch-all handler for unmatched routes
7353
+ */
7354
+ app.use((request, response) => {
7355
+ response.status(404).send(`URL "${request.originalUrl}" was not found on Promptbook server.`);
7356
+ });
7183
7357
  const httpServer = http__default["default"].createServer(app);
7184
7358
  const server = new socket_io.Server(httpServer, {
7185
- path: socketioPath,
7186
- transports: [/*'websocket', <- TODO: [🌬] Make websocket transport work */ 'polling'],
7359
+ path: '/socket.io',
7360
+ transports: ['polling', 'websocket' /*, <- TODO: [🌬] Allow to pass `transports`, add 'webtransport' */],
7187
7361
  cors: {
7188
7362
  origin: '*',
7189
7363
  methods: ['GET', 'POST'],
7364
+ // <- TODO: [🌡] Allow to pass
7190
7365
  },
7191
7366
  });
7192
7367
  server.on('connection', (socket) => {
@@ -7240,9 +7415,7 @@
7240
7415
  socket.emit('prompt-response', { promptResult } /* <- Note: [🤛] */);
7241
7416
  }
7242
7417
  catch (error) {
7243
- if (!(error instanceof Error)) {
7244
- throw error;
7245
- }
7418
+ assertsError(error);
7246
7419
  socket.emit('error', serializeError(error) /* <- Note: [🤛] */);
7247
7420
  }
7248
7421
  finally {
@@ -7264,9 +7437,7 @@
7264
7437
  socket.emit('listModels-response', { models } /* <- Note: [🤛] */);
7265
7438
  }
7266
7439
  catch (error) {
7267
- if (!(error instanceof Error)) {
7268
- throw error;
7269
- }
7440
+ assertsError(error);
7270
7441
  socket.emit('error', serializeError(error));
7271
7442
  }
7272
7443
  finally {
@@ -7287,9 +7458,7 @@
7287
7458
  socket.emit('preparePipeline-response', { preparedPipeline } /* <- Note: [🤛] */);
7288
7459
  }
7289
7460
  catch (error) {
7290
- if (!(error instanceof Error)) {
7291
- throw error;
7292
- }
7461
+ assertsError(error);
7293
7462
  socket.emit('error', serializeError(error));
7294
7463
  // <- TODO: [🚋] There is a problem with the remote server handling errors and sending them back to the client
7295
7464
  }
@@ -7337,8 +7506,7 @@
7337
7506
  };
7338
7507
  }
7339
7508
  /**
7340
- * TODO: !! Add CORS and security - probbably via `helmet`
7341
- * TODO: [👩🏾‍🤝‍🧑🏾] Allow to pass custom fetch function here - PromptbookFetch
7509
+ * TODO: [🌡] Add CORS and security - probbably via `helmet`
7342
7510
  * TODO: Split this file into multiple functions - handler for each request
7343
7511
  * TODO: Maybe use `$exportJson`
7344
7512
  * TODO: [🧠][🛍] Maybe not `isAnonymous: boolean` BUT `mode: 'ANONYMOUS'|'COLLECTION'`