@promptbook/remote-server 0.89.0-6 โ†’ 0.89.0-8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/esm/index.es.js CHANGED
@@ -33,7 +33,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
33
33
  * @generated
34
34
  * @see https://github.com/webgptorg/promptbook
35
35
  */
36
- const PROMPTBOOK_ENGINE_VERSION = '0.89.0-6';
36
+ const PROMPTBOOK_ENGINE_VERSION = '0.89.0-8';
37
37
  /**
38
38
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
39
39
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -158,6 +158,7 @@ const DEFAULT_MAX_PARALLEL_COUNT = 5; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
158
158
  */
159
159
  const DEFAULT_MAX_EXECUTION_ATTEMPTS = 10; // <- TODO: [๐Ÿคนโ€โ™‚๏ธ]
160
160
  // <- TODO: [๐Ÿ•] Make also `BOOKS_DIRNAME_ALTERNATIVES`
161
+ // TODO: !!!!!! Just .promptbook dir, hardocode others
161
162
  /**
162
163
  * Where to store the temporary downloads
163
164
  *
@@ -209,9 +210,22 @@ const IS_PIPELINE_LOGIC_VALIDATED = just(
209
210
  true);
210
211
  /**
211
212
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
212
- * TODO: [๐Ÿง ][๐Ÿงœโ€โ™‚๏ธ] Maybe join remoteUrl and path into single value
213
+ * TODO: [๐Ÿง ][๐Ÿงœโ€โ™‚๏ธ] Maybe join remoteServerUrl and path into single value
213
214
  */
214
215
 
216
+ /**
217
+ * AuthenticationError is thrown from login function which is dependency of remote server
218
+ *
219
+ * @public exported from `@promptbook/core`
220
+ */
221
+ class AuthenticationError extends Error {
222
+ constructor(message) {
223
+ super(message);
224
+ this.name = 'AuthenticationError';
225
+ Object.setPrototypeOf(this, AuthenticationError.prototype);
226
+ }
227
+ }
228
+
215
229
  /**
216
230
  * Generates random token
217
231
  *
@@ -545,6 +559,7 @@ const COMMON_JAVASCRIPT_ERRORS = {
545
559
  TypeError,
546
560
  URIError,
547
561
  AggregateError,
562
+ AuthenticationError,
548
563
  /*
549
564
  Note: Not widely supported
550
565
  > InternalError,
@@ -580,6 +595,10 @@ function serializeError(error) {
580
595
 
581
596
  Cannot serialize error with name "${name}"
582
597
 
598
+ Authors of Promptbook probably forgot to add this error into the list of errors:
599
+ https://github.com/webgptorg/promptbook/blob/main/src/errors/0-index.ts
600
+
601
+
583
602
  ${block(stack || message)}
584
603
 
585
604
  `));
@@ -6713,11 +6732,12 @@ async function $provideScriptingForNode(options) {
6713
6732
  * @public exported from `@promptbook/remote-server`
6714
6733
  */
6715
6734
  function startRemoteServer(options) {
6716
- const { port, collection, createLlmExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, } = {
6735
+ const { port, collection, createLlmExecutionTools, isAnonymousModeAllowed, isApplicationModeAllowed, isVerbose = DEFAULT_IS_VERBOSE, login, } = {
6717
6736
  isAnonymousModeAllowed: false,
6718
6737
  isApplicationModeAllowed: false,
6719
6738
  collection: null,
6720
6739
  createLlmExecutionTools: null,
6740
+ login: null,
6721
6741
  ...options,
6722
6742
  };
6723
6743
  // <- TODO: [๐Ÿฆช] Some helper type to be able to use discriminant union types with destructuring
@@ -6795,13 +6815,14 @@ function startRemoteServer(options) {
6795
6815
  servers: [
6796
6816
  {
6797
6817
  url: `http://localhost:${port}${rootPath}`,
6818
+ // <- TODO: !!!!! Probbably: Pass `remoteServerUrl` instead of `port` and `rootPath`
6798
6819
  },
6799
6820
  ],
6800
6821
  },
6801
6822
  apis: ['./src/remote-server/**/*.ts'], // Adjust path as needed
6802
6823
  };
6803
6824
  const swaggerSpec = swaggerJsdoc(swaggerOptions);
6804
- app.use(`${rootPath}/api-docs`, swaggerUi.serve, swaggerUi.setup(swaggerSpec));
6825
+ app.use([`/api-docs`, `${rootPath}/api-docs`], swaggerUi.serve, swaggerUi.setup(swaggerSpec));
6805
6826
  const runningExecutionTasks = [];
6806
6827
  // <- TODO: [๐Ÿคฌ] Identify the users
6807
6828
  // TODO: [๐Ÿง ] Do here some garbage collection of finished tasks
@@ -6851,9 +6872,12 @@ function startRemoteServer(options) {
6851
6872
 
6852
6873
  ## Paths
6853
6874
 
6854
- ${block(app._router.stack
6855
- .map(({ route }) => (route === null || route === void 0 ? void 0 : route.path) || null)
6856
- .filter((path) => path !== null)
6875
+ ${block([
6876
+ ...app._router.stack
6877
+ .map(({ route }) => (route === null || route === void 0 ? void 0 : route.path) || null)
6878
+ .filter((path) => path !== null),
6879
+ '/api-docs',
6880
+ ]
6857
6881
  .map((path) => `- ${path}`)
6858
6882
  .join('\n'))}
6859
6883
 
@@ -6871,7 +6895,79 @@ function startRemoteServer(options) {
6871
6895
  https://github.com/webgptorg/promptbook
6872
6896
  `));
6873
6897
  });
6874
- // TODO: !!!!!! Add login route
6898
+ /**
6899
+ * @swagger
6900
+ *
6901
+ * /login:
6902
+ * post:
6903
+ * summary: Login to the server
6904
+ * description: Login to the server and get identification.
6905
+ * requestBody:
6906
+ * required: true
6907
+ * content:
6908
+ * application/json:
6909
+ * schema:
6910
+ * type: object
6911
+ * properties:
6912
+ * username:
6913
+ * type: string
6914
+ * password:
6915
+ * type: string
6916
+ * appId:
6917
+ * type: string
6918
+ * responses:
6919
+ * 200:
6920
+ * description: Successful login
6921
+ * content:
6922
+ * application/json:
6923
+ * schema:
6924
+ * type: object
6925
+ * properties:
6926
+ * identification:
6927
+ * type: object
6928
+ */
6929
+ app.post([`/login`, `${rootPath}/login`], async (request, response) => {
6930
+ if (!isApplicationModeAllowed || login === null) {
6931
+ response.status(400).send('Application mode is not allowed');
6932
+ return;
6933
+ }
6934
+ try {
6935
+ const username = request.body.username;
6936
+ const password = request.body.password;
6937
+ const appId = request.body.appId;
6938
+ const { isSuccess, error, message, identification } = await login({
6939
+ username,
6940
+ password,
6941
+ appId,
6942
+ rawRequest: request,
6943
+ rawResponse: response,
6944
+ });
6945
+ response.status(201).send({
6946
+ isSuccess,
6947
+ message,
6948
+ error: error ? serializeError(error) : undefined,
6949
+ identification,
6950
+ });
6951
+ return;
6952
+ }
6953
+ catch (error) {
6954
+ if (!(error instanceof Error)) {
6955
+ throw error;
6956
+ }
6957
+ if (error instanceof AuthenticationError) {
6958
+ response.status(401).send({
6959
+ isSuccess: false,
6960
+ message: error.message,
6961
+ error: serializeError(error),
6962
+ });
6963
+ }
6964
+ console.warn(`Login function thrown different error than AuthenticationError`, {
6965
+ error,
6966
+ serializedError: serializeError(error),
6967
+ });
6968
+ response.status(400).send({ error: serializeError(error) });
6969
+ }
6970
+ });
6875
6971
  /**
6876
6972
  * @swagger
6877
6973
  * /books:
@@ -6888,7 +6984,7 @@ function startRemoteServer(options) {
6888
6984
  * items:
6889
6985
  * type: string
6890
6986
  */
6891
- app.get(`${rootPath}/books`, async (request, response) => {
6987
+ app.get([`/books`, `${rootPath}/books`], async (request, response) => {
6892
6988
  if (collection === null) {
6893
6989
  response.status(500).send('No collection available');
6894
6990
  return;
@@ -6921,7 +7017,7 @@ function startRemoteServer(options) {
6921
7017
  * 404:
6922
7018
  * description: Book not found.
6923
7019
  */
6924
- app.get(`${rootPath}/books/*`, async (request, response) => {
7020
+ app.get([`/books/*`, `${rootPath}/books/*`], async (request, response) => {
6925
7021
  try {
6926
7022
  if (collection === null) {
6927
7023
  response.status(500).send('No collection nor books available');
@@ -6991,10 +7087,10 @@ function startRemoteServer(options) {
6991
7087
  * items:
6992
7088
  * type: object
6993
7089
  */
6994
- app.get(`${rootPath}/executions`, async (request, response) => {
7090
+ app.get([`/executions`, `${rootPath}/executions`], async (request, response) => {
6995
7091
  response.send(runningExecutionTasks.map((runningExecutionTask) => exportExecutionTask(runningExecutionTask, false)));
6996
7092
  });
6997
- app.get(`${rootPath}/executions/last`, async (request, response) => {
7093
+ app.get([`/executions/last`, `${rootPath}/executions/last`], async (request, response) => {
6998
7094
  // TODO: [๐Ÿคฌ] Filter only for user
6999
7095
  if (runningExecutionTasks.length === 0) {
7000
7096
  response.status(404).send('No execution tasks found');
@@ -7003,7 +7099,7 @@ function startRemoteServer(options) {
7003
7099
  const lastExecutionTask = runningExecutionTasks[runningExecutionTasks.length - 1];
7004
7100
  response.send(exportExecutionTask(lastExecutionTask, true));
7005
7101
  });
7006
- app.get(`${rootPath}/executions/:taskId`, async (request, response) => {
7102
+ app.get([`/executions/:taskId`, `${rootPath}/executions/:taskId`], async (request, response) => {
7007
7103
  const { taskId } = request.params;
7008
7104
  // TODO: [๐Ÿคฌ] Filter only for user
7009
7105
  const executionTask = runningExecutionTasks.find((executionTask) => executionTask.taskId === taskId);
@@ -7044,7 +7140,7 @@ function startRemoteServer(options) {
7044
7140
  * 400:
7045
7141
  * description: Invalid input.
7046
7142
  */
7047
- app.post(`${rootPath}/executions/new`, async (request, response) => {
7143
+ app.post([`/executions/new`, `${rootPath}/executions/new`], async (request, response) => {
7048
7144
  try {
7049
7145
  const { inputParameters, identification /* <- [๐Ÿคฌ] */ } = request.body;
7050
7146
  const pipelineUrl = request.body.pipelineUrl || request.body.book;