@promptbook/remote-server 0.89.0-2 → 0.89.0-21

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 (48) hide show
  1. package/README.md +10 -6
  2. package/esm/index.es.js +526 -262
  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 +20 -8
  6. package/esm/typings/src/_packages/remote-client.index.d.ts +6 -8
  7. package/esm/typings/src/_packages/remote-server.index.d.ts +6 -6
  8. package/esm/typings/src/_packages/types.index.d.ts +18 -16
  9. package/esm/typings/src/cli/cli-commands/login.d.ts +0 -1
  10. package/esm/typings/src/cli/common/$addGlobalOptionsToCommand.d.ts +7 -0
  11. package/esm/typings/src/cli/common/$provideLlmToolsForCli.d.ts +28 -0
  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 +24 -26
  15. package/esm/typings/src/errors/0-index.d.ts +9 -0
  16. package/esm/typings/src/errors/AuthenticationError.d.ts +9 -0
  17. package/esm/typings/src/errors/PipelineExecutionError.d.ts +1 -1
  18. package/esm/typings/src/errors/PromptbookFetchError.d.ts +9 -0
  19. package/esm/typings/src/errors/WrappedError.d.ts +10 -0
  20. package/esm/typings/src/errors/assertsError.d.ts +11 -0
  21. package/esm/typings/src/execution/PromptbookFetch.d.ts +1 -1
  22. package/esm/typings/src/llm-providers/_common/register/$provideEnvFilename.d.ts +12 -0
  23. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsConfigurationFromEnv.d.ts +2 -8
  24. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForTestingAndScriptsAndPlayground.d.ts +2 -0
  25. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForWizzardOrCli.d.ts +45 -1
  26. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsFromEnv.d.ts +1 -0
  27. package/esm/typings/src/llm-providers/anthropic-claude/AnthropicClaudeExecutionToolsOptions.d.ts +1 -1
  28. package/esm/typings/src/llm-providers/anthropic-claude/register-configuration.d.ts +1 -1
  29. package/esm/typings/src/remote-server/RemoteServer.d.ts +23 -0
  30. package/esm/typings/src/remote-server/socket-types/_subtypes/{PromptbookServer_Identification.d.ts → Identification.d.ts} +3 -3
  31. package/esm/typings/src/remote-server/socket-types/_subtypes/identificationToPromptbookToken.d.ts +11 -0
  32. package/esm/typings/src/remote-server/socket-types/_subtypes/promptbookTokenToIdentification.d.ts +10 -0
  33. package/esm/typings/src/remote-server/socket-types/listModels/PromptbookServer_ListModels_Request.d.ts +2 -2
  34. package/esm/typings/src/remote-server/socket-types/prepare/PromptbookServer_PreparePipeline_Request.d.ts +2 -2
  35. package/esm/typings/src/remote-server/socket-types/prompt/PromptbookServer_Prompt_Request.d.ts +2 -2
  36. package/esm/typings/src/remote-server/startRemoteServer.d.ts +3 -4
  37. package/esm/typings/src/remote-server/types/RemoteClientOptions.d.ts +4 -12
  38. package/esm/typings/src/remote-server/types/RemoteServerOptions.d.ts +84 -9
  39. package/esm/typings/src/scrapers/_common/utils/{scraperFetch.d.ts → promptbookFetch.d.ts} +2 -2
  40. package/esm/typings/src/storage/env-storage/$EnvStorage.d.ts +40 -0
  41. package/esm/typings/src/types/typeAliases.d.ts +19 -0
  42. package/esm/typings/src/utils/organization/TODO_narrow.d.ts +6 -0
  43. package/package.json +11 -5
  44. package/umd/index.umd.js +430 -166
  45. package/umd/index.umd.js.map +1 -1
  46. package/esm/typings/src/cli/test/ptbk2.d.ts +0 -5
  47. package/esm/typings/src/playground/BrjappConnector.d.ts +0 -67
  48. package/esm/typings/src/playground/brjapp-api-schema.d.ts +0 -12879
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('http'), require('socket.io'), require('spacetrim'), 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', '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.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, 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('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';
6
6
 
7
7
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
8
 
@@ -10,6 +10,8 @@
10
10
  var express__default = /*#__PURE__*/_interopDefaultLegacy(express);
11
11
  var http__default = /*#__PURE__*/_interopDefaultLegacy(http);
12
12
  var spaceTrim__default = /*#__PURE__*/_interopDefaultLegacy(spaceTrim);
13
+ var swaggerJsdoc__default = /*#__PURE__*/_interopDefaultLegacy(swaggerJsdoc);
14
+ var swaggerUi__default = /*#__PURE__*/_interopDefaultLegacy(swaggerUi);
13
15
  var parserHtml__default = /*#__PURE__*/_interopDefaultLegacy(parserHtml);
14
16
  var hexEncoder__default = /*#__PURE__*/_interopDefaultLegacy(hexEncoder);
15
17
  var sha256__default = /*#__PURE__*/_interopDefaultLegacy(sha256);
@@ -28,7 +30,7 @@
28
30
  * @generated
29
31
  * @see https://github.com/webgptorg/promptbook
30
32
  */
31
- const PROMPTBOOK_ENGINE_VERSION = '0.89.0-2';
33
+ const PROMPTBOOK_ENGINE_VERSION = '0.89.0-21';
32
34
  /**
33
35
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
34
36
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -82,6 +84,7 @@
82
84
  * @public exported from `@promptbook/core`
83
85
  */
84
86
  const CLAIM = `It's time for a paradigm shift. The future of software in plain English, French or Latin`;
87
+ // <- TODO: [🐊] Pick the best claim
85
88
  /**
86
89
  * When the title is not provided, the default title is used
87
90
  *
@@ -114,6 +117,7 @@
114
117
  infinity: '(infinity; ∞)',
115
118
  negativeInfinity: '(negative infinity; -∞)',
116
119
  unserializable: '(unserializable value)',
120
+ circular: '(circular JSON)',
117
121
  };
118
122
  /**
119
123
  * Small number limit
@@ -153,6 +157,7 @@
153
157
  */
154
158
  const DEFAULT_MAX_EXECUTION_ATTEMPTS = 10; // <- TODO: [🤹‍♂️]
155
159
  // <- TODO: [🕝] Make also `BOOKS_DIRNAME_ALTERNATIVES`
160
+ // TODO: Just `.promptbook` in config, hardcode subfolders like `download-cache` or `execution-cache`
156
161
  /**
157
162
  * Where to store the temporary downloads
158
163
  *
@@ -204,9 +209,138 @@
204
209
  true);
205
210
  /**
206
211
  * Note: [💞] Ignore a discrepancy between file name and entity name
207
- * TODO: [🧠][🧜‍♂️] Maybe join remoteUrl and path into single value
212
+ * TODO: [🧠][🧜‍♂️] Maybe join remoteServerUrl and path into single value
208
213
  */
209
214
 
215
+ /**
216
+ * Make error report URL for the given error
217
+ *
218
+ * @private private within the repository
219
+ */
220
+ function getErrorReportUrl(error) {
221
+ const report = {
222
+ title: `🐜 Error report from ${NAME}`,
223
+ body: spaceTrim__default["default"]((block) => `
224
+
225
+
226
+ \`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
227
+
228
+ \`\`\`
229
+ ${block(error.message || '(no error message)')}
230
+ \`\`\`
231
+
232
+
233
+ ## More info:
234
+
235
+ - **Promptbook engine version:** ${PROMPTBOOK_ENGINE_VERSION}
236
+ - **Book language version:** ${BOOK_LANGUAGE_VERSION}
237
+ - **Time:** ${new Date().toISOString()}
238
+
239
+ <details>
240
+ <summary>Stack trace:</summary>
241
+
242
+ ## Stack trace:
243
+
244
+ \`\`\`stacktrace
245
+ ${block(error.stack || '(empty)')}
246
+ \`\`\`
247
+ </details>
248
+
249
+ `),
250
+ };
251
+ const reportUrl = new URL(`https://github.com/webgptorg/promptbook/issues/new`);
252
+ reportUrl.searchParams.set('labels', 'bug');
253
+ reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
254
+ reportUrl.searchParams.set('title', report.title);
255
+ reportUrl.searchParams.set('body', report.body);
256
+ return reportUrl;
257
+ }
258
+
259
+ /**
260
+ * This error type indicates that the error should not happen and its last check before crashing with some other error
261
+ *
262
+ * @public exported from `@promptbook/core`
263
+ */
264
+ class UnexpectedError extends Error {
265
+ constructor(message) {
266
+ super(spaceTrim.spaceTrim((block) => `
267
+ ${block(message)}
268
+
269
+ Note: This error should not happen.
270
+ It's probbably a bug in the pipeline collection
271
+
272
+ Please report issue:
273
+ ${block(getErrorReportUrl(new Error(message)).href)}
274
+
275
+ Or contact us on ${ADMIN_EMAIL}
276
+
277
+ `));
278
+ this.name = 'UnexpectedError';
279
+ Object.setPrototypeOf(this, UnexpectedError.prototype);
280
+ }
281
+ }
282
+
283
+ /**
284
+ * This error type indicates that somewhere in the code non-Error object was thrown and it was wrapped into the `WrappedError`
285
+ *
286
+ * @public exported from `@promptbook/core`
287
+ */
288
+ class WrappedError extends Error {
289
+ constructor(whatWasThrown) {
290
+ const tag = `[🤮]`;
291
+ console.error(tag, whatWasThrown);
292
+ super(spaceTrim.spaceTrim(`
293
+ Non-Error object was thrown
294
+
295
+ Note: Look for ${tag} in the console for more details
296
+ Please report issue on ${ADMIN_EMAIL}
297
+ `));
298
+ this.name = 'WrappedError';
299
+ Object.setPrototypeOf(this, WrappedError.prototype);
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Helper used in catch blocks to assert that the error is an instance of `Error`
305
+ *
306
+ * @param whatWasThrown Any object that was thrown
307
+ * @returns Nothing if the error is an instance of `Error`
308
+ * @throws `WrappedError` or `UnexpectedError` if the error is not standard
309
+ *
310
+ * @private within the repository
311
+ */
312
+ function assertsError(whatWasThrown) {
313
+ // Case 1: Handle error which was rethrown as `WrappedError`
314
+ if (whatWasThrown instanceof WrappedError) {
315
+ const wrappedError = whatWasThrown;
316
+ throw wrappedError;
317
+ }
318
+ // Case 2: Handle unexpected errors
319
+ if (whatWasThrown instanceof UnexpectedError) {
320
+ const unexpectedError = whatWasThrown;
321
+ throw unexpectedError;
322
+ }
323
+ // Case 3: Handle standard errors - keep them up to consumer
324
+ if (whatWasThrown instanceof Error) {
325
+ return;
326
+ }
327
+ // Case 4: Handle non-standard errors - wrap them into `WrappedError` and throw
328
+ throw new WrappedError(whatWasThrown);
329
+ }
330
+
331
+ /**
332
+ * AuthenticationError is thrown from login function which is dependency of remote server
333
+ *
334
+ * @public exported from `@promptbook/core`
335
+ */
336
+ class AuthenticationError extends Error {
337
+ constructor(message) {
338
+ super(message);
339
+ this.name = 'AuthenticationError';
340
+ Object.setPrototypeOf(this, AuthenticationError.prototype);
341
+ }
342
+ }
343
+
210
344
  /**
211
345
  * Generates random token
212
346
  *
@@ -238,7 +372,7 @@
238
372
  }
239
373
  }
240
374
  /**
241
- * TODO: !!!!!! Add id to all errors
375
+ * TODO: [🧠][🌂] Add id to all errors
242
376
  */
243
377
 
244
378
  /**
@@ -436,70 +570,15 @@
436
570
  }
437
571
 
438
572
  /**
439
- * Make error report URL for the given error
440
- *
441
- * @private private within the repository
442
- */
443
- function getErrorReportUrl(error) {
444
- const report = {
445
- title: `🐜 Error report from ${NAME}`,
446
- body: spaceTrim__default["default"]((block) => `
447
-
448
-
449
- \`${error.name || 'Error'}\` has occurred in the [${NAME}], please look into it @${ADMIN_GITHUB_NAME}.
450
-
451
- \`\`\`
452
- ${block(error.message || '(no error message)')}
453
- \`\`\`
454
-
455
-
456
- ## More info:
457
-
458
- - **Promptbook engine version:** ${PROMPTBOOK_ENGINE_VERSION}
459
- - **Book language version:** ${BOOK_LANGUAGE_VERSION}
460
- - **Time:** ${new Date().toISOString()}
461
-
462
- <details>
463
- <summary>Stack trace:</summary>
464
-
465
- ## Stack trace:
466
-
467
- \`\`\`stacktrace
468
- ${block(error.stack || '(empty)')}
469
- \`\`\`
470
- </details>
471
-
472
- `),
473
- };
474
- const reportUrl = new URL(`https://github.com/webgptorg/promptbook/issues/new`);
475
- reportUrl.searchParams.set('labels', 'bug');
476
- reportUrl.searchParams.set('assignees', ADMIN_GITHUB_NAME);
477
- reportUrl.searchParams.set('title', report.title);
478
- reportUrl.searchParams.set('body', report.body);
479
- return reportUrl;
480
- }
481
-
482
- /**
483
- * This error type indicates that the error should not happen and its last check before crashing with some other error
573
+ * Error thrown when a fetch request fails
484
574
  *
485
575
  * @public exported from `@promptbook/core`
486
576
  */
487
- class UnexpectedError extends Error {
577
+ class PromptbookFetchError extends Error {
488
578
  constructor(message) {
489
- super(spaceTrim.spaceTrim((block) => `
490
- ${block(message)}
491
-
492
- Note: This error should not happen.
493
- It's probbably a bug in the pipeline collection
494
-
495
- Please report issue:
496
- ${block(getErrorReportUrl(new Error(message)).href)}
497
-
498
- Or contact us on ${ADMIN_EMAIL}
499
-
500
- `));
501
- this.name = 'UnexpectedError';
502
- Object.setPrototypeOf(this, UnexpectedError.prototype);
579
+ super(message);
580
+ this.name = 'PromptbookFetchError';
581
+ Object.setPrototypeOf(this, PromptbookFetchError.prototype);
503
582
  }
504
583
  }
505
584
 
@@ -523,7 +602,10 @@
523
602
  PipelineExecutionError,
524
603
  PipelineLogicError,
525
604
  PipelineUrlError,
605
+ AuthenticationError,
606
+ PromptbookFetchError,
526
607
  UnexpectedError,
608
+ WrappedError,
527
609
  // TODO: [🪑]> VersionMismatchError,
528
610
  };
529
611
  /**
@@ -575,6 +657,10 @@
575
657
 
576
658
  Cannot serialize error with name "${name}"
577
659
 
660
+ Authors of Promptbook probably forgot to add this error into the list of errors:
661
+ https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
662
+
663
+
578
664
  ${block(stack || message)}
579
665
 
580
666
  `));
@@ -778,9 +864,7 @@
778
864
  return result.trim();
779
865
  }
780
866
  catch (error) {
781
- if (!(error instanceof Error)) {
782
- throw error;
783
- }
867
+ assertsError(error);
784
868
  return null;
785
869
  }
786
870
  }
@@ -858,9 +942,7 @@
858
942
  return result.trim() + toExec;
859
943
  }
860
944
  catch (error) {
861
- if (!(error instanceof Error)) {
862
- throw error;
863
- }
945
+ assertsError(error);
864
946
  return null;
865
947
  }
866
948
  }
@@ -891,9 +973,7 @@
891
973
  throw new Error(`Can not locate app ${appName} on Windows.`);
892
974
  }
893
975
  catch (error) {
894
- if (!(error instanceof Error)) {
895
- throw error;
896
- }
976
+ assertsError(error);
897
977
  return null;
898
978
  }
899
979
  }
@@ -1131,9 +1211,7 @@
1131
1211
  JSON.stringify(value); // <- TODO: [0]
1132
1212
  }
1133
1213
  catch (error) {
1134
- if (!(error instanceof Error)) {
1135
- throw error;
1136
- }
1214
+ assertsError(error);
1137
1215
  throw new UnexpectedError(spaceTrim__default["default"]((block) => `
1138
1216
  \`${name}\` is not serializable
1139
1217
 
@@ -1741,9 +1819,7 @@
1741
1819
  return true;
1742
1820
  }
1743
1821
  catch (error) {
1744
- if (!(error instanceof Error)) {
1745
- throw error;
1746
- }
1822
+ assertsError(error);
1747
1823
  if (error.message.includes('Unexpected token')) {
1748
1824
  return false;
1749
1825
  }
@@ -1881,8 +1957,8 @@
1881
1957
  updatedAt = new Date();
1882
1958
  errors.push(...executionResult.errors);
1883
1959
  warnings.push(...executionResult.warnings);
1884
- // <- TODO: !!! Only unique errors and warnings should be added (or filtered)
1885
- // TODO: [🧠] !!! errors, warning, isSuccessful are redundant both in `ExecutionTask` and `ExecutionTask.currentValue`
1960
+ // <- TODO: [🌂] Only unique errors and warnings should be added (or filtered)
1961
+ // TODO: [🧠] !! errors, warning, isSuccessful are redundant both in `ExecutionTask` and `ExecutionTask.currentValue`
1886
1962
  // Also maybe move `ExecutionTask.currentValue.usage` -> `ExecutionTask.usage`
1887
1963
  // And delete `ExecutionTask.currentValue.preparedPipeline`
1888
1964
  assertsTaskSuccessful(executionResult);
@@ -1892,6 +1968,7 @@
1892
1968
  partialResultSubject.next(executionResult);
1893
1969
  }
1894
1970
  catch (error) {
1971
+ assertsError(error);
1895
1972
  status = 'ERROR';
1896
1973
  errors.push(error);
1897
1974
  partialResultSubject.error(error);
@@ -2702,14 +2779,15 @@
2702
2779
  }
2703
2780
  }
2704
2781
  catch (error) {
2705
- if (!(error instanceof Error) || error instanceof UnexpectedError) {
2782
+ assertsError(error);
2783
+ if (error instanceof UnexpectedError) {
2706
2784
  throw error;
2707
2785
  }
2708
2786
  errors.push({ llmExecutionTools, error });
2709
2787
  }
2710
2788
  }
2711
2789
  if (errors.length === 1) {
2712
- throw errors[0];
2790
+ throw errors[0].error;
2713
2791
  }
2714
2792
  else if (errors.length > 1) {
2715
2793
  throw new PipelineExecutionError(
@@ -3543,17 +3621,22 @@
3543
3621
  /**
3544
3622
  * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
3545
3623
  *
3546
- * @private as default `fetch` function used in Promptbook scrapers
3624
+ * @public exported from `@promptbook/core`
3547
3625
  */
3548
- const scraperFetch = async (url, init) => {
3626
+ const promptbookFetch = async (urlOrRequest, init) => {
3549
3627
  try {
3550
- return await fetch(url, init);
3628
+ return await fetch(urlOrRequest, init);
3551
3629
  }
3552
3630
  catch (error) {
3553
- if (!(error instanceof Error)) {
3554
- throw error;
3631
+ assertsError(error);
3632
+ let url;
3633
+ if (typeof urlOrRequest === 'string') {
3634
+ url = urlOrRequest;
3635
+ }
3636
+ else if (urlOrRequest instanceof Request) {
3637
+ url = urlOrRequest.url;
3555
3638
  }
3556
- throw new KnowledgeScrapeError(spaceTrim__default["default"]((block) => `
3639
+ throw new PromptbookFetchError(spaceTrim__default["default"]((block) => `
3557
3640
  Can not fetch "${url}"
3558
3641
 
3559
3642
  Fetch error:
@@ -3574,7 +3657,7 @@
3574
3657
  async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
3575
3658
  // console.log('!! makeKnowledgeSourceHandler', knowledgeSource);
3576
3659
  var _a;
3577
- const { fetch = scraperFetch } = tools;
3660
+ const { fetch = promptbookFetch } = tools;
3578
3661
  const { knowledgeSourceContent } = knowledgeSource;
3579
3662
  let { name } = knowledgeSource;
3580
3663
  const { rootDirname = null,
@@ -3776,9 +3859,7 @@
3776
3859
  knowledgePreparedUnflatten[index] = pieces;
3777
3860
  }
3778
3861
  catch (error) {
3779
- if (!(error instanceof Error)) {
3780
- throw error;
3781
- }
3862
+ assertsError(error);
3782
3863
  console.warn(error);
3783
3864
  // <- TODO: [🏮] Some standard way how to transform errors into warnings and how to handle non-critical fails during the tasks
3784
3865
  }
@@ -4070,13 +4151,19 @@
4070
4151
  return value.toISOString();
4071
4152
  }
4072
4153
  else {
4073
- return JSON.stringify(value);
4154
+ try {
4155
+ return JSON.stringify(value);
4156
+ }
4157
+ catch (error) {
4158
+ if (error instanceof TypeError && error.message.includes('circular structure')) {
4159
+ return VALUE_STRINGS.circular;
4160
+ }
4161
+ throw error;
4162
+ }
4074
4163
  }
4075
4164
  }
4076
4165
  catch (error) {
4077
- if (!(error instanceof Error)) {
4078
- throw error;
4079
- }
4166
+ assertsError(error);
4080
4167
  console.error(error);
4081
4168
  return VALUE_STRINGS.unserializable;
4082
4169
  }
@@ -4133,9 +4220,7 @@
4133
4220
  }
4134
4221
  }
4135
4222
  catch (error) {
4136
- if (!(error instanceof Error)) {
4137
- throw error;
4138
- }
4223
+ assertsError(error);
4139
4224
  throw new ParseError(spaceTrim.spaceTrim((block) => `
4140
4225
  Can not extract variables from the script
4141
4226
  ${block(error.stack || error.message)}
@@ -5017,9 +5102,7 @@
5017
5102
  break scripts;
5018
5103
  }
5019
5104
  catch (error) {
5020
- if (!(error instanceof Error)) {
5021
- throw error;
5022
- }
5105
+ assertsError(error);
5023
5106
  if (error instanceof UnexpectedError) {
5024
5107
  throw error;
5025
5108
  }
@@ -5089,9 +5172,7 @@
5089
5172
  break scripts;
5090
5173
  }
5091
5174
  catch (error) {
5092
- if (!(error instanceof Error)) {
5093
- throw error;
5094
- }
5175
+ assertsError(error);
5095
5176
  if (error instanceof UnexpectedError) {
5096
5177
  throw error;
5097
5178
  }
@@ -5712,9 +5793,7 @@
5712
5793
  await Promise.all(resolving);
5713
5794
  }
5714
5795
  catch (error /* <- Note: [3] */) {
5715
- if (!(error instanceof Error)) {
5716
- throw error;
5717
- }
5796
+ assertsError(error);
5718
5797
  // Note: No need to rethrow UnexpectedError
5719
5798
  // if (error instanceof UnexpectedError) {
5720
5799
  // Note: Count usage, [🧠] Maybe put to separate function executionReportJsonToUsage + DRY [🤹‍♂️]
@@ -6616,9 +6695,7 @@
6616
6695
  }
6617
6696
  }
6618
6697
  catch (error) {
6619
- if (!(error instanceof Error)) {
6620
- throw error;
6621
- }
6698
+ assertsError(error);
6622
6699
  if (error instanceof ReferenceError) {
6623
6700
  const undefinedName = error.message.split(' ')[0];
6624
6701
  /*
@@ -6708,29 +6785,14 @@
6708
6785
  * @public exported from `@promptbook/remote-server`
6709
6786
  */
6710
6787
  function startRemoteServer(options) {
6711
- const { port, collection, createLlmExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, } = {
6788
+ const { port, collection, createLlmExecutionTools, createExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, login, } = {
6712
6789
  isAnonymousModeAllowed: false,
6713
6790
  isApplicationModeAllowed: false,
6714
6791
  collection: null,
6715
6792
  createLlmExecutionTools: null,
6793
+ login: null,
6716
6794
  ...options,
6717
6795
  };
6718
- // <- TODO: [🦪] Some helper type to be able to use discriminant union types with destructuring
6719
- let { rootPath = '/' } = options;
6720
- if (!rootPath.startsWith('/')) {
6721
- rootPath = `/${rootPath}`;
6722
- } /* not else */
6723
- if (rootPath.endsWith('/')) {
6724
- rootPath = rootPath.slice(0, -1);
6725
- } /* not else */
6726
- if (rootPath === '/') {
6727
- rootPath = '';
6728
- }
6729
- const socketioPath = '/' +
6730
- `${rootPath}/socket.io`
6731
- .split('/')
6732
- .filter((part) => part !== '')
6733
- .join('/');
6734
6796
  const startupDate = new Date();
6735
6797
  async function getExecutionToolsFromIdentification(identification) {
6736
6798
  if (identification === null || identification === undefined) {
@@ -6755,6 +6817,7 @@
6755
6817
  // Note: Application mode
6756
6818
  const { appId, userId, customOptions } = identification;
6757
6819
  llm = await createLlmExecutionTools({
6820
+ isAnonymous: false,
6758
6821
  appId,
6759
6822
  userId,
6760
6823
  customOptions,
@@ -6763,13 +6826,20 @@
6763
6826
  else {
6764
6827
  throw new PipelineExecutionError(`You must provide either llmToolsConfiguration or non-anonymous mode must be propperly configured`);
6765
6828
  }
6766
- const fs = $provideFilesystemForNode();
6767
- const executables = await $provideExecutablesForNode();
6829
+ const customExecutionTools = createExecutionTools ? await createExecutionTools(identification) : {};
6830
+ const fs = customExecutionTools.fs || $provideFilesystemForNode();
6831
+ const executables = customExecutionTools.executables || (await $provideExecutablesForNode());
6832
+ const scrapers = customExecutionTools.scrapers || (await $provideScrapersForNode({ fs, llm, executables }));
6833
+ const script = customExecutionTools.script || (await $provideScriptingForNode({}));
6834
+ const fetch = customExecutionTools.fetch || promptbookFetch;
6835
+ const userInterface = customExecutionTools.userInterface || undefined;
6768
6836
  const tools = {
6769
6837
  llm,
6770
6838
  fs,
6771
- scrapers: await $provideScrapersForNode({ fs, llm, executables }),
6772
- script: await $provideScriptingForNode({}),
6839
+ scrapers,
6840
+ script,
6841
+ fetch,
6842
+ userInterface,
6773
6843
  };
6774
6844
  return tools;
6775
6845
  }
@@ -6779,9 +6849,42 @@
6779
6849
  response.setHeader('X-Powered-By', 'Promptbook engine');
6780
6850
  next();
6781
6851
  });
6852
+ const swaggerOptions = {
6853
+ definition: {
6854
+ openapi: '3.0.0',
6855
+ info: {
6856
+ title: 'Promptbook Remote Server API',
6857
+ version: '1.0.0',
6858
+ description: 'API documentation for the Promptbook Remote Server',
6859
+ },
6860
+ /*
6861
+ TODO:
6862
+ servers: [
6863
+ {
6864
+ url: `http://localhost:${port}${rootPath}`,
6865
+ // <- TODO: Pass some public URLs here
6866
+ },
6867
+ ],
6868
+ */
6869
+ },
6870
+ apis: ['./src/remote-server/**/*.ts'], // Adjust path as needed
6871
+ };
6872
+ const swaggerSpec = swaggerJsdoc__default["default"](swaggerOptions);
6873
+ const rootPath = ''; // <- TODO: !!!! Remove after merging into feature/elysia+openai+swagger-2
6874
+ app.use([`/api-docs`, `${rootPath}/api-docs`], swaggerUi__default["default"].serve, swaggerUi__default["default"].setup(swaggerSpec));
6782
6875
  const runningExecutionTasks = [];
6783
6876
  // <- TODO: [🤬] Identify the users
6784
6877
  // TODO: [🧠] Do here some garbage collection of finished tasks
6878
+ /**
6879
+ * @swagger
6880
+ * /:
6881
+ * get:
6882
+ * summary: Get server details
6883
+ * description: Returns details about the Promptbook server.
6884
+ * responses:
6885
+ * 200:
6886
+ * description: Server details in markdown format.
6887
+ */
6785
6888
  app.get(['/', rootPath], async (request, response) => {
6786
6889
  var _a;
6787
6890
  if ((_a = request.url) === null || _a === void 0 ? void 0 : _a.includes('socket.io')) {
@@ -6802,7 +6905,6 @@
6802
6905
 
6803
6906
  **Server port:** ${port}
6804
6907
  **Server root path:** ${rootPath}
6805
- **Socket.io path:** ${socketioPath}
6806
6908
  **Startup date:** ${startupDate.toISOString()}
6807
6909
  **Anonymouse mode:** ${isAnonymousModeAllowed ? 'enabled' : 'disabled'}
6808
6910
  **Application mode:** ${isApplicationModeAllowed ? 'enabled' : 'disabled'}
@@ -6818,9 +6920,12 @@
6818
6920
 
6819
6921
  ## Paths
6820
6922
 
6821
- ${block(app._router.stack
6822
- .map(({ route }) => (route === null || route === void 0 ? void 0 : route.path) || null)
6823
- .filter((path) => path !== null)
6923
+ ${block([
6924
+ ...app._router.stack
6925
+ .map(({ route }) => (route === null || route === void 0 ? void 0 : route.path) || null)
6926
+ .filter((path) => path !== null),
6927
+ '/api-docs',
6928
+ ]
6824
6929
  .map((path) => `- ${path}`)
6825
6930
  .join('\n'))}
6826
6931
 
@@ -6838,8 +6943,94 @@
6838
6943
  https://github.com/webgptorg/promptbook
6839
6944
  `));
6840
6945
  });
6841
- // TODO: !!!!!! Add login route
6842
- app.get(`${rootPath}/books`, async (request, response) => {
6946
+ /**
6947
+ * @swagger
6948
+ *
6949
+ * /login:
6950
+ * post:
6951
+ * summary: Login to the server
6952
+ * description: Login to the server and get identification.
6953
+ * requestBody:
6954
+ * required: true
6955
+ * content:
6956
+ * application/json:
6957
+ * schema:
6958
+ * type: object
6959
+ * properties:
6960
+ * username:
6961
+ * type: string
6962
+ * password:
6963
+ * type: string
6964
+ * appId:
6965
+ * type: string
6966
+ * responses:
6967
+ * 200:
6968
+ * description: Successful login
6969
+ * content:
6970
+ * application/json:
6971
+ * schema:
6972
+ * type: object
6973
+ * properties:
6974
+ * identification:
6975
+ * type: object
6976
+ */
6977
+ app.post([`/login`, `${rootPath}/login`], async (request, response) => {
6978
+ if (!isApplicationModeAllowed || login === null) {
6979
+ response.status(400).send('Application mode is not allowed');
6980
+ return;
6981
+ }
6982
+ try {
6983
+ const username = request.body.username;
6984
+ const password = request.body.password;
6985
+ const appId = request.body.appId;
6986
+ const { isSuccess, error, message, identification } = await login({
6987
+ username,
6988
+ password,
6989
+ appId,
6990
+ rawRequest: request,
6991
+ rawResponse: response,
6992
+ });
6993
+ response.status(201).send({
6994
+ isSuccess,
6995
+ message,
6996
+ error: error ? serializeError(error) : undefined,
6997
+ identification,
6998
+ });
6999
+ return;
7000
+ }
7001
+ catch (error) {
7002
+ assertsError(error);
7003
+ if (error instanceof AuthenticationError) {
7004
+ response.status(401).send({
7005
+ isSuccess: false,
7006
+ message: error.message,
7007
+ error: serializeError(error),
7008
+ });
7009
+ }
7010
+ console.warn(`Login function thrown different error than AuthenticationError`, {
7011
+ error,
7012
+ serializedError: serializeError(error),
7013
+ });
7014
+ response.status(400).send({ error: serializeError(error) });
7015
+ }
7016
+ });
7017
+ /**
7018
+ * @swagger
7019
+ * /books:
7020
+ * get:
7021
+ * summary: List all books
7022
+ * description: Returns a list of all available books in the collection.
7023
+ * responses:
7024
+ * 200:
7025
+ * description: A list of books.
7026
+ * content:
7027
+ * application/json:
7028
+ * schema:
7029
+ * type: array
7030
+ * items:
7031
+ * type: string
7032
+ */
7033
+ app.get([`/books`, `${rootPath}/books`], async (request, response) => {
6843
7034
  if (collection === null) {
6844
7035
  response.status(500).send('No collection available');
6845
7036
  return;
@@ -6849,7 +7040,30 @@
6849
7040
  response.send(pipelines);
6850
7041
  });
6851
7042
  // TODO: [🧠] Is it secure / good idea to expose source codes of hosted books
6852
- app.get(`${rootPath}/books/*`, async (request, response) => {
7043
+ /**
7044
+ * @swagger
7045
+ * /books/{bookId}:
7046
+ * get:
7047
+ * summary: Get book content
7048
+ * description: Returns the content of a specific book.
7049
+ * parameters:
7050
+ * - in: path
7051
+ * name: bookId
7052
+ * required: true
7053
+ * schema:
7054
+ * type: string
7055
+ * description: The ID of the book to retrieve.
7056
+ * responses:
7057
+ * 200:
7058
+ * description: The content of the book.
7059
+ * content:
7060
+ * text/markdown:
7061
+ * schema:
7062
+ * type: string
7063
+ * 404:
7064
+ * description: Book not found.
7065
+ */
7066
+ app.get([`/books/*`, `${rootPath}/books/*`], async (request, response) => {
6853
7067
  try {
6854
7068
  if (collection === null) {
6855
7069
  response.status(500).send('No collection nor books available');
@@ -6868,9 +7082,7 @@
6868
7082
  .send(source.content);
6869
7083
  }
6870
7084
  catch (error) {
6871
- if (!(error instanceof Error)) {
6872
- throw error;
6873
- }
7085
+ assertsError(error);
6874
7086
  response
6875
7087
  .status(404)
6876
7088
  .send({ error: serializeError(error) });
@@ -6903,10 +7115,26 @@
6903
7115
  };
6904
7116
  }
6905
7117
  }
6906
- app.get(`${rootPath}/executions`, async (request, response) => {
7118
+ /**
7119
+ * @swagger
7120
+ * /executions:
7121
+ * get:
7122
+ * summary: List all executions
7123
+ * description: Returns a list of all running execution tasks.
7124
+ * responses:
7125
+ * 200:
7126
+ * description: A list of execution tasks.
7127
+ * content:
7128
+ * application/json:
7129
+ * schema:
7130
+ * type: array
7131
+ * items:
7132
+ * type: object
7133
+ */
7134
+ app.get([`/executions`, `${rootPath}/executions`], async (request, response) => {
6907
7135
  response.send(runningExecutionTasks.map((runningExecutionTask) => exportExecutionTask(runningExecutionTask, false)));
6908
7136
  });
6909
- app.get(`${rootPath}/executions/last`, async (request, response) => {
7137
+ app.get([`/executions/last`, `${rootPath}/executions/last`], async (request, response) => {
6910
7138
  // TODO: [🤬] Filter only for user
6911
7139
  if (runningExecutionTasks.length === 0) {
6912
7140
  response.status(404).send('No execution tasks found');
@@ -6915,7 +7143,7 @@
6915
7143
  const lastExecutionTask = runningExecutionTasks[runningExecutionTasks.length - 1];
6916
7144
  response.send(exportExecutionTask(lastExecutionTask, true));
6917
7145
  });
6918
- app.get(`${rootPath}/executions/:taskId`, async (request, response) => {
7146
+ app.get([`/executions/:taskId`, `${rootPath}/executions/:taskId`], async (request, response) => {
6919
7147
  const { taskId } = request.params;
6920
7148
  // TODO: [🤬] Filter only for user
6921
7149
  const executionTask = runningExecutionTasks.find((executionTask) => executionTask.taskId === taskId);
@@ -6927,7 +7155,36 @@
6927
7155
  }
6928
7156
  response.send(exportExecutionTask(executionTask, true));
6929
7157
  });
6930
- app.post(`${rootPath}/executions/new`, async (request, response) => {
7158
+ /**
7159
+ * @swagger
7160
+ * /executions/new:
7161
+ * post:
7162
+ * summary: Start a new execution
7163
+ * description: Starts a new execution task for a given pipeline.
7164
+ * requestBody:
7165
+ * required: true
7166
+ * content:
7167
+ * application/json:
7168
+ * schema:
7169
+ * type: object
7170
+ * properties:
7171
+ * pipelineUrl:
7172
+ * type: string
7173
+ * inputParameters:
7174
+ * type: object
7175
+ * identification:
7176
+ * type: object
7177
+ * responses:
7178
+ * 200:
7179
+ * description: The newly created execution task.
7180
+ * content:
7181
+ * application/json:
7182
+ * schema:
7183
+ * type: object
7184
+ * 400:
7185
+ * description: Invalid input.
7186
+ */
7187
+ app.post([`/executions/new`, `${rootPath}/executions/new`], async (request, response) => {
6931
7188
  try {
6932
7189
  const { inputParameters, identification /* <- [🤬] */ } = request.body;
6933
7190
  const pipelineUrl = request.body.pipelineUrl || request.body.book;
@@ -6964,19 +7221,24 @@
6964
7221
  */
6965
7222
  }
6966
7223
  catch (error) {
6967
- if (!(error instanceof Error)) {
6968
- throw error;
6969
- }
7224
+ assertsError(error);
6970
7225
  response.status(400).send({ error: serializeError(error) });
6971
7226
  }
6972
7227
  });
7228
+ /**
7229
+ * Catch-all handler for unmatched routes
7230
+ */
7231
+ app.use((request, response) => {
7232
+ response.status(404).send(`URL "${request.originalUrl}" was not found on Promptbook server.`);
7233
+ });
6973
7234
  const httpServer = http__default["default"].createServer(app);
6974
7235
  const server = new socket_io.Server(httpServer, {
6975
- path: socketioPath,
6976
- transports: [/*'websocket', <- TODO: [🌬] Make websocket transport work */ 'polling'],
7236
+ path: '/socket.io',
7237
+ transports: ['polling', 'websocket' /*, <- TODO: [🌬] Allow to pass `transports`, add 'webtransport' */],
6977
7238
  cors: {
6978
7239
  origin: '*',
6979
7240
  methods: ['GET', 'POST'],
7241
+ // <- TODO: [🌡] Allow to pass
6980
7242
  },
6981
7243
  });
6982
7244
  server.on('connection', (socket) => {
@@ -7030,9 +7292,7 @@
7030
7292
  socket.emit('prompt-response', { promptResult } /* <- Note: [🤛] */);
7031
7293
  }
7032
7294
  catch (error) {
7033
- if (!(error instanceof Error)) {
7034
- throw error;
7035
- }
7295
+ assertsError(error);
7036
7296
  socket.emit('error', serializeError(error) /* <- Note: [🤛] */);
7037
7297
  }
7038
7298
  finally {
@@ -7054,9 +7314,7 @@
7054
7314
  socket.emit('listModels-response', { models } /* <- Note: [🤛] */);
7055
7315
  }
7056
7316
  catch (error) {
7057
- if (!(error instanceof Error)) {
7058
- throw error;
7059
- }
7317
+ assertsError(error);
7060
7318
  socket.emit('error', serializeError(error));
7061
7319
  }
7062
7320
  finally {
@@ -7077,9 +7335,7 @@
7077
7335
  socket.emit('preparePipeline-response', { preparedPipeline } /* <- Note: [🤛] */);
7078
7336
  }
7079
7337
  catch (error) {
7080
- if (!(error instanceof Error)) {
7081
- throw error;
7082
- }
7338
+ assertsError(error);
7083
7339
  socket.emit('error', serializeError(error));
7084
7340
  // <- TODO: [🚋] There is a problem with the remote server handling errors and sending them back to the client
7085
7341
  }
@@ -7104,6 +7360,15 @@
7104
7360
  }
7105
7361
  let isDestroyed = false;
7106
7362
  return {
7363
+ get httpServer() {
7364
+ return httpServer;
7365
+ },
7366
+ get expressApp() {
7367
+ return app;
7368
+ },
7369
+ get socketIoServer() {
7370
+ return server;
7371
+ },
7107
7372
  get isDestroyed() {
7108
7373
  return isDestroyed;
7109
7374
  },
@@ -7118,8 +7383,7 @@
7118
7383
  };
7119
7384
  }
7120
7385
  /**
7121
- * TODO: !! Add CORS and security - probbably via `helmet`
7122
- * TODO: [👩🏾‍🤝‍🧑🏾] Allow to pass custom fetch function here - PromptbookFetch
7386
+ * TODO: [🌡] Add CORS and security - probbably via `helmet`
7123
7387
  * TODO: Split this file into multiple functions - handler for each request
7124
7388
  * TODO: Maybe use `$exportJson`
7125
7389
  * TODO: [🧠][🛍] Maybe not `isAnonymous: boolean` BUT `mode: 'ANONYMOUS'|'COLLECTION'`