@promptbook/cli 0.85.0-0 → 0.85.0-1

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
@@ -2,12 +2,12 @@ import colors from 'colors';
2
2
  import commander from 'commander';
3
3
  import spaceTrim, { spaceTrim as spaceTrim$1 } from 'spacetrim';
4
4
  import { forTime } 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';
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';
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 'child_process';
10
+ import { spawn } from 'node:child_process';
11
11
  import { format } from 'prettier';
12
12
  import parserHtml from 'prettier/parser-html';
13
13
  import { BehaviorSubject, concat, from } from 'rxjs';
@@ -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 'http';
22
+ import http from 'node: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.84.0';
46
+ var PROMPTBOOK_ENGINE_VERSION = '0.85.0-0';
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
@@ -4921,9 +4921,9 @@ function assertsTaskSuccessful(executionResult) {
4921
4921
  function createTask(options) {
4922
4922
  var taskType = options.taskType, taskProcessCallback = options.taskProcessCallback;
4923
4923
  var taskId = "".concat(taskType.toLowerCase(), "-").concat($randomToken(256 /* <- TODO: !!! To global config */));
4924
- var resultSubject = new BehaviorSubject({});
4924
+ var partialResultSubject = new BehaviorSubject({});
4925
4925
  var finalResultPromise = /* not await */ taskProcessCallback(function (newOngoingResult) {
4926
- resultSubject.next(newOngoingResult);
4926
+ partialResultSubject.next(newOngoingResult);
4927
4927
  });
4928
4928
  function asPromise(options) {
4929
4929
  return __awaiter(this, void 0, void 0, function () {
@@ -4948,7 +4948,7 @@ function createTask(options) {
4948
4948
  taskId: taskId,
4949
4949
  asPromise: asPromise,
4950
4950
  asObservable: function () {
4951
- return concat(resultSubject.asObservable(), from(asPromise({
4951
+ return concat(partialResultSubject.asObservable(), from(asPromise({
4952
4952
  isCrashedOnError: true,
4953
4953
  })));
4954
4954
  },
@@ -13508,9 +13508,9 @@ function $initializeRunCommand(program) {
13508
13508
  case 17:
13509
13509
  executionTask = _m.sent();
13510
13510
  if (isVerbose) {
13511
- executionTask.asObservable().subscribe(function (progress) {
13511
+ executionTask.asObservable().subscribe(function (partialResult) {
13512
13512
  console.info(colors.gray('--- Progress ---'));
13513
- console.info(progress);
13513
+ console.info(partialResult);
13514
13514
  });
13515
13515
  }
13516
13516
  return [4 /*yield*/, executionTask.asPromise({
@@ -13746,11 +13746,33 @@ function $initializeTestCommand(program) {
13746
13746
  */
13747
13747
  function startRemoteServer(options) {
13748
13748
  var _this = this;
13749
- var _a = __assign({ isAnonymousModeAllowed: false, isApplicationModeAllowed: false, collection: null, createLlmExecutionTools: null }, options), port = _a.port, path = _a.path, collection = _a.collection, createLlmExecutionTools = _a.createLlmExecutionTools, isAnonymousModeAllowed = _a.isAnonymousModeAllowed, isApplicationModeAllowed = _a.isApplicationModeAllowed, _b = _a.isVerbose, isVerbose = _b === void 0 ? DEFAULT_IS_VERBOSE : _b;
13749
+ var _a = __assign({ isAnonymousModeAllowed: false, isApplicationModeAllowed: false, collection: null, createLlmExecutionTools: null }, options), port = _a.port, collection = _a.collection, createLlmExecutionTools = _a.createLlmExecutionTools, isAnonymousModeAllowed = _a.isAnonymousModeAllowed, isApplicationModeAllowed = _a.isApplicationModeAllowed, _b = _a.isVerbose, isVerbose = _b === void 0 ? DEFAULT_IS_VERBOSE : _b;
13750
13750
  // <- TODO: [🦪] Some helper type to be able to use discriminant union types with destructuring
13751
+ var _c = options.rootPath, rootPath = _c === void 0 ? '/' : _c;
13752
+ if (!rootPath.startsWith('/')) {
13753
+ rootPath = "/".concat(rootPath);
13754
+ } /* not else */
13755
+ if (rootPath.endsWith('/')) {
13756
+ rootPath = rootPath.slice(0, -1);
13757
+ } /* not else */
13758
+ if (rootPath === '/') {
13759
+ rootPath = '';
13760
+ }
13761
+ var socketioPath = '/' +
13762
+ "".concat(rootPath, "/socket.io")
13763
+ .split('/')
13764
+ .filter(function (part) { return part !== ''; })
13765
+ .join('/');
13766
+ var startupDate = new Date();
13751
13767
  var app = express();
13752
13768
  app.use(express.json());
13753
- app.get('/', function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13769
+ app.use(function (request, response, next) {
13770
+ response.setHeader('X-Powered-By', 'Promptbook engine');
13771
+ next();
13772
+ });
13773
+ var runningExecutionTasks = [];
13774
+ // TODO: !!!!!! Do here some garbage collection of finished tasks
13775
+ app.get(['/', rootPath], function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13754
13776
  var _a, _b;
13755
13777
  var _this = this;
13756
13778
  var _c;
@@ -13760,20 +13782,19 @@ function startRemoteServer(options) {
13760
13782
  if ((_c = request.url) === null || _c === void 0 ? void 0 : _c.includes('socket.io')) {
13761
13783
  return [2 /*return*/];
13762
13784
  }
13763
- _b = (_a = response).send;
13764
- // TODO: !!!!!! Make this either valid html or text - http://localhost:4460/
13785
+ _b = (_a = response.type('text/markdown')).send;
13765
13786
  return [4 /*yield*/, spaceTrim$1(function (block) { return __awaiter(_this, void 0, void 0, function () {
13766
13787
  var _a, _b, _c, _d, _e;
13767
13788
  return __generator(this, function (_f) {
13768
13789
  switch (_f.label) {
13769
13790
  case 0:
13770
- _b = (_a = "\n Server for processing promptbook remote requests is running.\n\n Version: ".concat(PROMPTBOOK_ENGINE_VERSION, "\n Socket.io path: ").concat(path, "/socket.io\n Anonymouse mode: ").concat(isAnonymousModeAllowed ? 'enabled' : 'disabled', "\n Application mode: ").concat(isApplicationModeAllowed ? 'enabled' : 'disabled', "\n ")).concat;
13791
+ _b = (_a = "\n # Promptbook\n\n > ".concat(block(CLAIM), "\n\n **Book language version:** ").concat(BOOK_LANGUAGE_VERSION, "\n **Promptbook engine version:** ").concat(PROMPTBOOK_ENGINE_VERSION, "\n **Node.js version:** ").concat(process.version /* <- TODO: [🧠] Is it secure to expose this */, "\n\n ---\n\n ## Details\n\n **Server port:** ").concat(port, "\n **Server root path:** ").concat(rootPath, "\n **Socket.io path:** ").concat(socketioPath, "\n **Startup date:** ").concat(startupDate.toISOString(), "\n **Anonymouse mode:** ").concat(isAnonymousModeAllowed ? 'enabled' : 'disabled', "\n **Application mode:** ").concat(isApplicationModeAllowed ? 'enabled' : 'disabled', "\n ")).concat;
13771
13792
  _c = block;
13772
13793
  if (!(!isApplicationModeAllowed || collection === null)) return [3 /*break*/, 1];
13773
13794
  _d = '';
13774
13795
  return [3 /*break*/, 3];
13775
13796
  case 1:
13776
- _e = 'Pipelines in collection:\n';
13797
+ _e = '**Pipelines in collection:**\n';
13777
13798
  return [4 /*yield*/, collection.listPipelines()];
13778
13799
  case 2:
13779
13800
  _d = _e +
@@ -13781,27 +13802,49 @@ function startRemoteServer(options) {
13781
13802
  .map(function (pipelineUrl) { return "- ".concat(pipelineUrl); })
13782
13803
  .join('\n');
13783
13804
  _f.label = 3;
13784
- case 3: return [2 /*return*/, _b.apply(_a, [_c.apply(void 0, [_d]), "\n\n For more information look at:\n https://github.com/webgptorg/promptbook\n "])];
13805
+ case 3: return [2 /*return*/, _b.apply(_a, [_c.apply(void 0, [_d]), "\n **Running executions:** "]).concat(runningExecutionTasks.length, "\n\n ---\n\n ## Paths\n\n ").concat(block(app._router.stack
13806
+ .map(function (_a) {
13807
+ var route = _a.route;
13808
+ return (route === null || route === void 0 ? void 0 : route.path) || null;
13809
+ })
13810
+ .filter(function (path) { return path !== null; })
13811
+ .map(function (path) { return "- ".concat(path); })
13812
+ .join('\n')), "\n\n ---\n\n ## Instructions\n\n To connect to this server use:\n\n 1) The client https://www.npmjs.com/package/@promptbook/remote-client\n 2) OpenAI compatible client *(Not wotking yet)*\n 3) REST API\n\n For more information look at:\n https://github.com/webgptorg/promptbook\n ")];
13785
13813
  }
13786
13814
  });
13787
13815
  }); })];
13788
13816
  case 1:
13789
- _b.apply(_a, [
13790
- // TODO: !!!!!! Make this either valid html or text - http://localhost:4460/
13791
- _d.sent()]);
13817
+ _b.apply(_a, [_d.sent()]);
13792
13818
  return [2 /*return*/];
13793
13819
  }
13794
13820
  });
13795
13821
  }); });
13796
- var runningExecutionTasks = [];
13797
- // TODO: !!!!!! Do here some garbage collection of finished tasks
13798
- app.get('/executions', function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13822
+ app.get("".concat(rootPath, "/books"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13823
+ var _a, _b, _c;
13824
+ return __generator(this, function (_d) {
13825
+ switch (_d.label) {
13826
+ case 0:
13827
+ _b = (_a = response).send;
13828
+ if (!(collection === null)) return [3 /*break*/, 1];
13829
+ _c = [];
13830
+ return [3 /*break*/, 3];
13831
+ case 1: return [4 /*yield*/, collection.listPipelines()];
13832
+ case 2:
13833
+ _c = _d.sent();
13834
+ _d.label = 3;
13835
+ case 3:
13836
+ _b.apply(_a, [_c]);
13837
+ return [2 /*return*/];
13838
+ }
13839
+ });
13840
+ }); });
13841
+ app.get("".concat(rootPath, "/executions"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13799
13842
  return __generator(this, function (_a) {
13800
13843
  response.send(runningExecutionTasks);
13801
13844
  return [2 /*return*/];
13802
13845
  });
13803
13846
  }); });
13804
- app.get('/executions/:taskId', function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13847
+ app.get("".concat(rootPath, "/executions/:taskId"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13805
13848
  var taskId, execution;
13806
13849
  return __generator(this, function (_a) {
13807
13850
  taskId = request.query.taskId;
@@ -13814,16 +13857,17 @@ function startRemoteServer(options) {
13814
13857
  return [2 /*return*/];
13815
13858
  });
13816
13859
  }); });
13817
- app.post('/executions/new', function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13818
- var _a, pipelineUrl, inputParameters, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask;
13819
- var _b;
13820
- return __generator(this, function (_c) {
13821
- switch (_c.label) {
13860
+ app.post("".concat(rootPath, "/executions/new"), function (request, response) { return __awaiter(_this, void 0, void 0, function () {
13861
+ var inputParameters, pipelineUrl, pipeline, llm, fs, executables, tools, pipelineExecutor, executionTask;
13862
+ var _a;
13863
+ return __generator(this, function (_b) {
13864
+ switch (_b.label) {
13822
13865
  case 0:
13823
- _a = request.body, pipelineUrl = _a.pipelineUrl, inputParameters = _a.inputParameters;
13866
+ inputParameters = request.body.inputParameters;
13867
+ pipelineUrl = request.body.pipelineUrl || request.body.book;
13824
13868
  return [4 /*yield*/, (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl))];
13825
13869
  case 1:
13826
- pipeline = _c.sent();
13870
+ pipeline = _b.sent();
13827
13871
  if (pipeline === undefined) {
13828
13872
  response.status(404).send("Pipeline \"".concat(pipelineUrl, "\" not found"));
13829
13873
  return [2 /*return*/];
@@ -13834,19 +13878,19 @@ function startRemoteServer(options) {
13834
13878
  customOptions: {},
13835
13879
  })];
13836
13880
  case 2:
13837
- llm = _c.sent();
13881
+ llm = _b.sent();
13838
13882
  fs = $provideFilesystemForNode();
13839
13883
  return [4 /*yield*/, $provideExecutablesForNode()];
13840
13884
  case 3:
13841
- executables = _c.sent();
13842
- _b = {
13885
+ executables = _b.sent();
13886
+ _a = {
13843
13887
  llm: llm,
13844
13888
  fs: fs
13845
13889
  };
13846
13890
  return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables })];
13847
13891
  case 4:
13848
- tools = (_b.scrapers = _c.sent(),
13849
- _b);
13892
+ tools = (_a.scrapers = _b.sent(),
13893
+ _a);
13850
13894
  pipelineExecutor = createPipelineExecutor(__assign({ pipeline: pipeline, tools: tools }, options));
13851
13895
  executionTask = pipelineExecutor(inputParameters);
13852
13896
  runningExecutionTasks.push(executionTask);
@@ -13857,7 +13901,7 @@ function startRemoteServer(options) {
13857
13901
  }); });
13858
13902
  var httpServer = http.createServer(app);
13859
13903
  var server = new Server(httpServer, {
13860
- path: path,
13904
+ path: socketioPath,
13861
13905
  transports: [/*'websocket', <- TODO: [🌬] Make websocket transport work */ 'polling'],
13862
13906
  cors: {
13863
13907
  origin: '*',
@@ -14101,6 +14145,8 @@ function startRemoteServer(options) {
14101
14145
  };
14102
14146
  }
14103
14147
  /**
14148
+ * TODO: !!!!!!! CORS and security
14149
+ * TODO: !!!!!!! Allow to pass tokem here
14104
14150
  * TODO: [👩🏾‍🤝‍🧑🏾] Allow to pass custom fetch function here - PromptbookFetch
14105
14151
  * TODO: Split this file into multiple functions - handler for each request
14106
14152
  * TODO: Maybe use `$exportJson`
@@ -14124,21 +14170,69 @@ function startRemoteServer(options) {
14124
14170
  function $initializeStartServerCommand(program) {
14125
14171
  var _this = this;
14126
14172
  var startServerCommand = program.command('start-server');
14173
+ startServerCommand.argument('[path]',
14174
+ // <- TODO: [🧟‍♂️] Unite path to promptbook collection argument
14175
+ 'Path to promptbook collection directory', DEFAULT_BOOKS_DIRNAME);
14127
14176
  startServerCommand.option('--port <port>', "Port to start the server on", '4460');
14128
- startServerCommand.description(spaceTrim("\n @@@\n "));
14129
- startServerCommand.action(function (_a) {
14130
- var port = _a.port;
14177
+ startServerCommand.option('--allow-anonymous', "Is anonymous mode allowed", false);
14178
+ startServerCommand.option('-r, --reload', "Call LLM models even if same prompt with result is in the cache", false);
14179
+ startServerCommand.option('-v, --verbose', "Is output verbose", false);
14180
+ startServerCommand.description(spaceTrim("\n Starts a remote server to execute books\n "));
14181
+ startServerCommand.action(function (path, _a) {
14182
+ var port = _a.port, isAnonymousModeAllowed = _a.allowAnonymous, isCacheReloaded = _a.reload, isVerbose = _a.verbose;
14131
14183
  return __awaiter(_this, void 0, void 0, function () {
14132
- return __generator(this, function (_b) {
14133
- startRemoteServer({
14134
- path: '/promptbook',
14135
- port: parseInt(port, 10),
14136
- isAnonymousModeAllowed: true,
14137
- isApplicationModeAllowed: true,
14138
- // <- TODO: !!!!!!
14139
- });
14140
- console.error(colors.green("Server started on port ".concat(port)));
14141
- return [2 /*return*/];
14184
+ var prepareAndScrapeOptions, fs, llm, executables, tools, collection;
14185
+ var _b;
14186
+ return __generator(this, function (_c) {
14187
+ switch (_c.label) {
14188
+ case 0:
14189
+ console.log('startServerCommand.action', { port: port, isAnonymousModeAllowed: isAnonymousModeAllowed, isCacheReloaded: isCacheReloaded, isVerbose: isVerbose });
14190
+ prepareAndScrapeOptions = {
14191
+ isVerbose: isVerbose,
14192
+ isCacheReloaded: isCacheReloaded,
14193
+ };
14194
+ fs = $provideFilesystemForNode(prepareAndScrapeOptions);
14195
+ return [4 /*yield*/, $provideLlmToolsForWizzardOrCli(prepareAndScrapeOptions)];
14196
+ case 1:
14197
+ llm = _c.sent();
14198
+ return [4 /*yield*/, $provideExecutablesForNode(prepareAndScrapeOptions)];
14199
+ case 2:
14200
+ executables = _c.sent();
14201
+ _b = {
14202
+ llm: llm,
14203
+ fs: fs
14204
+ };
14205
+ return [4 /*yield*/, $provideScrapersForNode({ fs: fs, llm: llm, executables: executables }, prepareAndScrapeOptions)];
14206
+ case 3:
14207
+ tools = (_b.scrapers = _c.sent(),
14208
+ _b.script = [
14209
+ /*new JavascriptExecutionTools(options)*/
14210
+ ],
14211
+ _b);
14212
+ return [4 /*yield*/, createCollectionFromDirectory(path, tools, {
14213
+ isVerbose: isVerbose,
14214
+ // TODO: [🧠] Utilize implicit urls for books
14215
+ // rootUrl: `http://localhost:${port}/books`,
14216
+ isRecursive: true,
14217
+ isLazyLoaded: false,
14218
+ isCrashedOnError: true,
14219
+ // <- TODO: [🍖] Add `intermediateFilesStrategy`
14220
+ })];
14221
+ case 4:
14222
+ collection = _c.sent();
14223
+ startRemoteServer({
14224
+ rootPath: '/',
14225
+ port: parseInt(port, 10),
14226
+ isAnonymousModeAllowed: isAnonymousModeAllowed,
14227
+ isApplicationModeAllowed: true,
14228
+ collection: collection,
14229
+ createLlmExecutionTools: function (options) {
14230
+ options.appId; options.userId;
14231
+ return llm;
14232
+ },
14233
+ });
14234
+ return [2 /*return*/];
14235
+ }
14142
14236
  });
14143
14237
  });
14144
14238
  });