@promptbook/cli 0.85.0-4 โ†’ 0.85.0-6

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.
@@ -12,6 +12,6 @@ const { _CLI } = require('../umd/index.umd.js');
12
12
  _CLI._initialize_promptbookCli();
13
13
 
14
14
  /**
15
- * TODO: !!! During the build check that this file exists
15
+ * TODO: [๐Ÿ‘ฉโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ] During the build check that this file exists
16
16
  * TODO: [๐Ÿ•Œ] When more functionalities, rename
17
17
  */
package/esm/index.es.js CHANGED
@@ -1,16 +1,16 @@
1
1
  import colors from 'colors';
2
2
  import commander from 'commander';
3
3
  import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
4
- import { forTime } from 'waitasecond';
5
- import { basename, join, dirname } from 'node:path';
6
- import { stat, access, constants, readFile, writeFile, readdir, mkdir, unlink, rm, rmdir, rename } from 'node:fs/promises';
4
+ import { forTime, forEver } from 'waitasecond';
5
+ import { basename, join, dirname } from 'path';
6
+ import { stat, access, constants, readFile, writeFile, readdir, mkdir, unlink, rm, rmdir, rename } from 'fs/promises';
7
7
  import hexEncoder from 'crypto-js/enc-hex';
8
8
  import sha256 from 'crypto-js/sha256';
9
9
  import * as dotenv from 'dotenv';
10
- import { spawn } from 'node:child_process';
10
+ import { spawn } from 'child_process';
11
11
  import { format } from 'prettier';
12
12
  import parserHtml from 'prettier/parser-html';
13
- import { BehaviorSubject, concat, from } from 'rxjs';
13
+ import { BehaviorSubject } from 'rxjs';
14
14
  import { randomBytes } from 'crypto';
15
15
  import { unparse, parse } from 'papaparse';
16
16
  import { SHA256 } from 'crypto-js';
@@ -19,7 +19,7 @@ import glob from 'glob-promise';
19
19
  import prompts from 'prompts';
20
20
  import moment from 'moment';
21
21
  import express from 'express';
22
- import http from 'node:http';
22
+ import http from 'http';
23
23
  import { Server } from 'socket.io';
24
24
  import { io } from 'socket.io-client';
25
25
  import Anthropic from '@anthropic-ai/sdk';
@@ -43,7 +43,7 @@ var BOOK_LANGUAGE_VERSION = '1.0.0';
43
43
  * @generated
44
44
  * @see https://github.com/webgptorg/promptbook
45
45
  */
46
- var PROMPTBOOK_ENGINE_VERSION = '0.85.0-3';
46
+ var PROMPTBOOK_ENGINE_VERSION = '0.85.0-5';
47
47
  /**
48
48
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
49
49
  * Note: [๐Ÿ’ž] Ignore a discrepancy between file name and entity name
@@ -4925,6 +4925,24 @@ function createTask(options) {
4925
4925
  var finalResultPromise = /* not await */ taskProcessCallback(function (newOngoingResult) {
4926
4926
  partialResultSubject.next(newOngoingResult);
4927
4927
  });
4928
+ finalResultPromise
4929
+ .catch(function (error) {
4930
+ // console.error('!!!!! Task failed:', error);
4931
+ partialResultSubject.error(error);
4932
+ })
4933
+ .then(function (value) {
4934
+ // console.error('!!!!! Task finished:', value);
4935
+ if (value) {
4936
+ try {
4937
+ assertsTaskSuccessful(value);
4938
+ partialResultSubject.next(value);
4939
+ }
4940
+ catch (error) {
4941
+ partialResultSubject.error(error);
4942
+ }
4943
+ }
4944
+ partialResultSubject.complete();
4945
+ });
4928
4946
  function asPromise(options) {
4929
4947
  return __awaiter(this, void 0, void 0, function () {
4930
4948
  var _a, isCrashedOnError, finalResult;
@@ -4935,7 +4953,9 @@ function createTask(options) {
4935
4953
  return [4 /*yield*/, finalResultPromise];
4936
4954
  case 1:
4937
4955
  finalResult = _b.sent();
4956
+ console.error('!!!!! finalResult:', finalResult);
4938
4957
  if (isCrashedOnError) {
4958
+ console.error('!!!!! isCrashedOnError:', finalResult);
4939
4959
  assertsTaskSuccessful(finalResult);
4940
4960
  }
4941
4961
  return [2 /*return*/, finalResult];
@@ -4948,9 +4968,7 @@ function createTask(options) {
4948
4968
  taskId: taskId,
4949
4969
  asPromise: asPromise,
4950
4970
  asObservable: function () {
4951
- return concat(partialResultSubject.asObservable(), from(asPromise({
4952
- isCrashedOnError: true,
4953
- })));
4971
+ return partialResultSubject.asObservable();
4954
4972
  },
4955
4973
  get currentValue() {
4956
4974
  return partialResultSubject.value;
@@ -13785,7 +13803,7 @@ function startRemoteServer(options) {
13785
13803
  next();
13786
13804
  });
13787
13805
  var runningExecutionTasks = [];
13788
- // TODO: !!!!!! Do here some garbage collection of finished tasks
13806
+ // TODO: [๐Ÿง ] Do here some garbage collection of finished tasks
13789
13807
  app.get(['/', rootPath], function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13790
13808
  var _a, _b;
13791
13809
  var _this = this;
@@ -13834,20 +13852,19 @@ function startRemoteServer(options) {
13834
13852
  });
13835
13853
  }); });
13836
13854
  app.get("".concat(rootPath, "/books"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13837
- var _a, _b, _c;
13838
- return __generator(this, function (_d) {
13839
- switch (_d.label) {
13855
+ var pipelines;
13856
+ return __generator(this, function (_a) {
13857
+ switch (_a.label) {
13840
13858
  case 0:
13841
- _b = (_a = response).send;
13842
- if (!(collection === null)) return [3 /*break*/, 1];
13843
- _c = [];
13844
- return [3 /*break*/, 3];
13845
- case 1: return [4 /*yield*/, collection.listPipelines()];
13846
- case 2:
13847
- _c = _d.sent();
13848
- _d.label = 3;
13849
- case 3:
13850
- _b.apply(_a, [_c]);
13859
+ if (collection === null) {
13860
+ response.status(500).send('No collection available');
13861
+ return [2 /*return*/];
13862
+ }
13863
+ return [4 /*yield*/, collection.listPipelines()];
13864
+ case 1:
13865
+ pipelines = _a.sent();
13866
+ // <- TODO: [๐Ÿง ][๐Ÿ‘ฉ๐Ÿพโ€๐Ÿคโ€๐Ÿง‘๐Ÿฟ] List `inputParameters` required for the execution
13867
+ response.send(pipelines);
13851
13868
  return [2 /*return*/];
13852
13869
  }
13853
13870
  });
@@ -13877,13 +13894,12 @@ function startRemoteServer(options) {
13877
13894
  return __generator(this, function (_b) {
13878
13895
  switch (_b.label) {
13879
13896
  case 0:
13880
- _b.trys.push([0, 5, , 6]);
13897
+ _b.trys.push([0, 6, , 7]);
13881
13898
  inputParameters = request.body.inputParameters;
13882
13899
  pipelineUrl = request.body.pipelineUrl || request.body.book;
13883
13900
  return [4 /*yield*/, (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl))];
13884
13901
  case 1:
13885
13902
  pipeline = _b.sent();
13886
- // <- TODO: !!!!!! NotFoundError
13887
13903
  if (pipeline === undefined) {
13888
13904
  response.status(404).send("Pipeline \"".concat(pipelineUrl, "\" not found"));
13889
13905
  return [2 /*return*/];
@@ -13910,20 +13926,21 @@ function startRemoteServer(options) {
13910
13926
  pipelineExecutor = createPipelineExecutor(__assign({ pipeline: pipeline, tools: tools }, options));
13911
13927
  executionTask = pipelineExecutor(inputParameters);
13912
13928
  runningExecutionTasks.push(executionTask);
13913
- response.send(executionTask);
13914
- // TODO: !!!!!! Remove this:
13915
- executionTask.asObservable().subscribe(function (_) {
13916
- console.log('!!!', _);
13917
- });
13918
- return [3 /*break*/, 6];
13929
+ return [4 /*yield*/, forTime(10)];
13919
13930
  case 5:
13931
+ _b.sent();
13932
+ // <- Note: Wait for a while to wait for quick responses or sudden but asynchronous errors
13933
+ // <- TODO: Put this into configuration
13934
+ response.send(executionTask);
13935
+ return [3 /*break*/, 7];
13936
+ case 6:
13920
13937
  error_1 = _b.sent();
13921
13938
  if (!(error_1 instanceof Error)) {
13922
13939
  throw error_1;
13923
13940
  }
13924
13941
  response.status(400).send({ error: serializeError(error_1) });
13925
- return [3 /*break*/, 6];
13926
- case 6: return [2 /*return*/];
13942
+ return [3 /*break*/, 7];
13943
+ case 7: return [2 /*return*/];
13927
13944
  }
13928
13945
  });
13929
13946
  }); });
@@ -14202,21 +14219,30 @@ function $initializeStartServerCommand(program) {
14202
14219
  // <- TODO: [๐ŸงŸโ€โ™‚๏ธ] Unite path to promptbook collection argument
14203
14220
  'Path to promptbook collection directory', DEFAULT_BOOKS_DIRNAME);
14204
14221
  startServerCommand.option('--port <port>', "Port to start the server on", '4460');
14205
- startServerCommand.option('-u, --url', spaceTrim("\n Public root url of the server\n It is used for following purposes:\n\n 1) It is suffixed with /books and used as rootUrl for all served books\n 2) Path (if not just /) is used as rootPath for the server\n "));
14222
+ startServerCommand.option('-u, --url <url>', spaceTrim("\n Public root url of the server\n It is used for following purposes:\n\n 1) It is suffixed with /books and used as rootUrl for all served books\n 2) Path (if not just /) is used as rootPath for the server\n "));
14206
14223
  startServerCommand.option('--allow-anonymous', "Is anonymous mode allowed", false);
14207
14224
  startServerCommand.option('-r, --reload', "Call LLM models even if same prompt with result is in the cache", false);
14208
14225
  startServerCommand.option('-v, --verbose', "Is output verbose", false);
14209
14226
  startServerCommand.description(spaceTrim("\n Starts a remote server to execute books\n "));
14210
14227
  startServerCommand.action(function (path, _a) {
14211
- var port = _a.port, rawUrl = _a.url, isAnonymousModeAllowed = _a.allowAnonymous, isCacheReloaded = _a.reload, isVerbose = _a.verbose;
14228
+ var portRaw = _a.port, rawUrl = _a.url, isAnonymousModeAllowed = _a.allowAnonymous, isCacheReloaded = _a.reload, isVerbose = _a.verbose;
14212
14229
  return __awaiter(_this, void 0, void 0, function () {
14213
- var url, rootUrl, rootPath, prepareAndScrapeOptions, fs, llm, executables, tools, collection;
14230
+ var port, url, rootUrl, rootPath, prepareAndScrapeOptions, fs, llm, executables, tools, collection;
14214
14231
  var _b;
14215
14232
  return __generator(this, function (_c) {
14216
14233
  switch (_c.label) {
14217
14234
  case 0:
14235
+ if (rawUrl && !isValidUrl(rawUrl)) {
14236
+ console.error(colors.red("Invalid URL: ".concat(rawUrl)));
14237
+ return [2 /*return*/, process.exit(1)];
14238
+ }
14239
+ port = parseInt(portRaw, 10);
14240
+ if (isNaN(port) || port <= 0 || port > 65535) {
14241
+ console.error(colors.red("Invalid port number: ".concat(portRaw)));
14242
+ return [2 /*return*/, process.exit(1)];
14243
+ }
14218
14244
  url = !rawUrl ? null : new URL(rawUrl);
14219
- if (url !== null && url.port !== port) {
14245
+ if (url !== null && url.port !== port.toString()) {
14220
14246
  console.warn(colors.yellow("Port in --url is different from --port which the server will listen on, this is ok only if you proxy from one port to another, for exaple via nginx or docker"));
14221
14247
  }
14222
14248
  rootUrl = undefined;
@@ -14261,7 +14287,7 @@ function $initializeStartServerCommand(program) {
14261
14287
  collection = _c.sent();
14262
14288
  startRemoteServer({
14263
14289
  rootPath: rootPath,
14264
- port: parseInt(port, 10),
14290
+ port: port,
14265
14291
  isAnonymousModeAllowed: isAnonymousModeAllowed,
14266
14292
  isApplicationModeAllowed: true,
14267
14293
  collection: collection,
@@ -14270,7 +14296,11 @@ function $initializeStartServerCommand(program) {
14270
14296
  return llm;
14271
14297
  },
14272
14298
  });
14273
- return [2 /*return*/];
14299
+ return [4 /*yield*/, forEver()];
14300
+ case 5:
14301
+ // Note: Already logged by `startRemoteServer`
14302
+ // console.error(colors.green(`Server started on port ${port}`));
14303
+ return [2 /*return*/, _c.sent()];
14274
14304
  }
14275
14305
  });
14276
14306
  });