@promptbook/cli 0.89.0-7 → 0.89.0-9

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 (25) hide show
  1. package/esm/index.es.js +225 -149
  2. package/esm/index.es.js.map +1 -1
  3. package/esm/typings/src/_packages/core.index.d.ts +4 -0
  4. package/esm/typings/src/_packages/remote-client.index.d.ts +6 -6
  5. package/esm/typings/src/_packages/remote-server.index.d.ts +6 -6
  6. package/esm/typings/src/_packages/types.index.d.ts +10 -16
  7. package/esm/typings/src/errors/0-index.d.ts +3 -0
  8. package/esm/typings/src/errors/PromptbookFetchError.d.ts +9 -0
  9. package/esm/typings/src/llm-providers/_common/register/$provideEnvFilepath.d.ts +12 -0
  10. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsConfigurationFromEnv.d.ts +2 -8
  11. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForTestingAndScriptsAndPlayground.d.ts +2 -0
  12. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsForWizzardOrCli.d.ts +5 -3
  13. package/esm/typings/src/llm-providers/_common/register/$provideLlmToolsFromEnv.d.ts +1 -0
  14. package/esm/typings/src/remote-server/socket-types/_subtypes/{PromptbookServer_Identification.d.ts → Identification.d.ts} +3 -3
  15. package/esm/typings/src/remote-server/socket-types/listModels/PromptbookServer_ListModels_Request.d.ts +2 -2
  16. package/esm/typings/src/remote-server/socket-types/prepare/PromptbookServer_PreparePipeline_Request.d.ts +2 -2
  17. package/esm/typings/src/remote-server/socket-types/prompt/PromptbookServer_Prompt_Request.d.ts +2 -2
  18. package/esm/typings/src/remote-server/types/RemoteClientOptions.d.ts +2 -2
  19. package/esm/typings/src/remote-server/types/RemoteServerOptions.d.ts +63 -21
  20. package/esm/typings/src/scrapers/_common/utils/{scraperFetch.d.ts → promptbookFetch.d.ts} +2 -2
  21. package/package.json +1 -1
  22. package/umd/index.umd.js +225 -149
  23. package/umd/index.umd.js.map +1 -1
  24. package/esm/typings/src/playground/BrjappConnector.d.ts +0 -67
  25. package/esm/typings/src/playground/brjapp-api-schema.d.ts +0 -12879
package/esm/index.es.js CHANGED
@@ -46,7 +46,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
46
46
  * @generated
47
47
  * @see https://github.com/webgptorg/promptbook
48
48
  */
49
- const PROMPTBOOK_ENGINE_VERSION = '0.89.0-7';
49
+ const PROMPTBOOK_ENGINE_VERSION = '0.89.0-9';
50
50
  /**
51
51
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
52
52
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -919,97 +919,6 @@ function $sideEffect(...sideEffectSubjects) {
919
919
  keepUnused(...sideEffectSubjects);
920
920
  }
921
921
 
922
- /**
923
- * Checks if value is valid email
924
- *
925
- * @public exported from `@promptbook/utils`
926
- */
927
- function isValidEmail(email) {
928
- if (typeof email !== 'string') {
929
- return false;
930
- }
931
- if (email.split('\n').length > 1) {
932
- return false;
933
- }
934
- return /^.+@.+\..+$/.test(email);
935
- }
936
-
937
- /**
938
- * Tests if given string is valid URL.
939
- *
940
- * Note: Dataurl are considered perfectly valid.
941
- * Note: There are two simmilar functions:
942
- * - `isValidUrl` which tests any URL
943
- * - `isValidPipelineUrl` *(this one)* which tests just promptbook URL
944
- *
945
- * @public exported from `@promptbook/utils`
946
- */
947
- function isValidUrl(url) {
948
- if (typeof url !== 'string') {
949
- return false;
950
- }
951
- try {
952
- if (url.startsWith('blob:')) {
953
- url = url.replace(/^blob:/, '');
954
- }
955
- const urlObject = new URL(url /* because fail is handled */);
956
- if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
957
- return false;
958
- }
959
- return true;
960
- }
961
- catch (error) {
962
- return false;
963
- }
964
- }
965
-
966
- /**
967
- * Stores data in memory (HEAP)
968
- *
969
- * @public exported from `@promptbook/core`
970
- */
971
- class MemoryStorage {
972
- constructor() {
973
- this.storage = {};
974
- }
975
- /**
976
- * Returns the number of key/value pairs currently present in the list associated with the object.
977
- */
978
- get length() {
979
- return Object.keys(this.storage).length;
980
- }
981
- /**
982
- * Empties the list associated with the object of all key/value pairs, if there are any.
983
- */
984
- clear() {
985
- this.storage = {};
986
- }
987
- /**
988
- * Returns the current value associated with the given key, or null if the given key does not exist in the list associated with the object.
989
- */
990
- getItem(key) {
991
- return this.storage[key] || null;
992
- }
993
- /**
994
- * Returns the name of the nth key in the list, or null if n is greater than or equal to the number of key/value pairs in the object.
995
- */
996
- key(index) {
997
- return Object.keys(this.storage)[index] || null;
998
- }
999
- /**
1000
- * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
1001
- */
1002
- setItem(key, value) {
1003
- this.storage[key] = value;
1004
- }
1005
- /**
1006
- * Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
1007
- */
1008
- removeItem(key) {
1009
- delete this.storage[key];
1010
- }
1011
- }
1012
-
1013
922
  /**
1014
923
  * Just marks a place of place where should be something implemented
1015
924
  * No side effects.
@@ -1504,6 +1413,35 @@ function isValidFilePath(filename) {
1504
1413
  * TODO: [🍏] Implement for MacOs
1505
1414
  */
1506
1415
 
1416
+ /**
1417
+ * Tests if given string is valid URL.
1418
+ *
1419
+ * Note: Dataurl are considered perfectly valid.
1420
+ * Note: There are two simmilar functions:
1421
+ * - `isValidUrl` which tests any URL
1422
+ * - `isValidPipelineUrl` *(this one)* which tests just promptbook URL
1423
+ *
1424
+ * @public exported from `@promptbook/utils`
1425
+ */
1426
+ function isValidUrl(url) {
1427
+ if (typeof url !== 'string') {
1428
+ return false;
1429
+ }
1430
+ try {
1431
+ if (url.startsWith('blob:')) {
1432
+ url = url.replace(/^blob:/, '');
1433
+ }
1434
+ const urlObject = new URL(url /* because fail is handled */);
1435
+ if (!['http:', 'https:', 'data:'].includes(urlObject.protocol)) {
1436
+ return false;
1437
+ }
1438
+ return true;
1439
+ }
1440
+ catch (error) {
1441
+ return false;
1442
+ }
1443
+ }
1444
+
1507
1445
  const defaultDiacriticsRemovalMap = [
1508
1446
  {
1509
1447
  base: 'A',
@@ -1915,6 +1853,53 @@ class FileCacheStorage {
1915
1853
  * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
1916
1854
  */
1917
1855
 
1856
+ /**
1857
+ * Stores data in memory (HEAP)
1858
+ *
1859
+ * @public exported from `@promptbook/core`
1860
+ */
1861
+ class MemoryStorage {
1862
+ constructor() {
1863
+ this.storage = {};
1864
+ }
1865
+ /**
1866
+ * Returns the number of key/value pairs currently present in the list associated with the object.
1867
+ */
1868
+ get length() {
1869
+ return Object.keys(this.storage).length;
1870
+ }
1871
+ /**
1872
+ * Empties the list associated with the object of all key/value pairs, if there are any.
1873
+ */
1874
+ clear() {
1875
+ this.storage = {};
1876
+ }
1877
+ /**
1878
+ * Returns the current value associated with the given key, or null if the given key does not exist in the list associated with the object.
1879
+ */
1880
+ getItem(key) {
1881
+ return this.storage[key] || null;
1882
+ }
1883
+ /**
1884
+ * Returns the name of the nth key in the list, or null if n is greater than or equal to the number of key/value pairs in the object.
1885
+ */
1886
+ key(index) {
1887
+ return Object.keys(this.storage)[index] || null;
1888
+ }
1889
+ /**
1890
+ * Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
1891
+ */
1892
+ setItem(key, value) {
1893
+ this.storage[key] = value;
1894
+ }
1895
+ /**
1896
+ * Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
1897
+ */
1898
+ removeItem(key) {
1899
+ delete this.storage[key];
1900
+ }
1901
+ }
1902
+
1918
1903
  /**
1919
1904
  * This error indicates problems parsing the format value
1920
1905
  *
@@ -2120,6 +2105,19 @@ class PipelineUrlError extends Error {
2120
2105
  }
2121
2106
  }
2122
2107
 
2108
+ /**
2109
+ * Error thrown when a fetch request fails
2110
+ *
2111
+ * @public exported from `@promptbook/core`
2112
+ */
2113
+ class PromptbookFetchError extends Error {
2114
+ constructor(message) {
2115
+ super(message);
2116
+ this.name = 'PromptbookFetchError';
2117
+ Object.setPrototypeOf(this, PromptbookFetchError.prototype);
2118
+ }
2119
+ }
2120
+
2123
2121
  /**
2124
2122
  * Index of all custom errors
2125
2123
  *
@@ -2158,6 +2156,7 @@ const COMMON_JAVASCRIPT_ERRORS = {
2158
2156
  URIError,
2159
2157
  AggregateError,
2160
2158
  AuthenticationError,
2159
+ PromptbookFetchError,
2161
2160
  /*
2162
2161
  Note: Not widely supported
2163
2162
  > InternalError,
@@ -2656,21 +2655,15 @@ function isRootPath(value) {
2656
2655
  */
2657
2656
 
2658
2657
  /**
2659
- * @@@
2658
+ * Provides the path to the `.env` file
2660
2659
  *
2661
- * @@@ .env
2660
+ * Note: `$` is used to indicate that this function is not a pure function - it uses filesystem to access .env file
2662
2661
  *
2663
- * It looks for environment variables:
2664
- * - `process.env.OPENAI_API_KEY`
2665
- * - `process.env.ANTHROPIC_CLAUDE_API_KEY`
2666
- * - ...
2667
- *
2668
- * @returns @@@
2669
- * @public exported from `@promptbook/node`
2662
+ * @private within the repository - for CLI utils
2670
2663
  */
2671
- async function $provideLlmToolsConfigurationFromEnv() {
2664
+ async function $provideEnvFilepath() {
2672
2665
  if (!$isRunningInNode()) {
2673
- throw new EnvironmentMismatchError('Function `$provideLlmToolsFromEnv` works only in Node.js environment');
2666
+ throw new EnvironmentMismatchError('Function `$provideEnvFilepath` works only in Node.js environment');
2674
2667
  }
2675
2668
  const envFilePatterns = [
2676
2669
  '.env',
@@ -2690,8 +2683,7 @@ async function $provideLlmToolsConfigurationFromEnv() {
2690
2683
  const envFilename = join(rootDirname, pattern);
2691
2684
  if (await isFileExisting(envFilename, $provideFilesystemForNode())) {
2692
2685
  $setUsedEnvFilename(envFilename);
2693
- dotenv.config({ path: envFilename });
2694
- break up_to_root;
2686
+ return envFilename;
2695
2687
  }
2696
2688
  }
2697
2689
  if (isRootPath(rootDirname)) {
@@ -2700,6 +2692,34 @@ async function $provideLlmToolsConfigurationFromEnv() {
2700
2692
  // Note: If the directory does not exist, try the parent directory
2701
2693
  rootDirname = join(rootDirname, '..');
2702
2694
  }
2695
+ return null;
2696
+ }
2697
+ /**
2698
+ * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
2699
+ */
2700
+
2701
+ /**
2702
+ * @@@
2703
+ *
2704
+ * @@@ .env
2705
+ * Note: `$` is used to indicate that this function is not a pure function - it uses filesystem to access .env file
2706
+ *
2707
+ * It looks for environment variables:
2708
+ * - `process.env.OPENAI_API_KEY`
2709
+ * - `process.env.ANTHROPIC_CLAUDE_API_KEY`
2710
+ * - ...
2711
+ *
2712
+ * @returns @@@
2713
+ * @public exported from `@promptbook/node`
2714
+ */
2715
+ async function $provideLlmToolsConfigurationFromEnv() {
2716
+ if (!$isRunningInNode()) {
2717
+ throw new EnvironmentMismatchError('Function `$provideLlmToolsFromEnv` works only in Node.js environment');
2718
+ }
2719
+ const envFilepath = await $provideEnvFilepath();
2720
+ if (envFilepath !== null) {
2721
+ dotenv.config({ path: envFilepath });
2722
+ }
2703
2723
  const llmToolsConfiguration = $llmToolsMetadataRegister
2704
2724
  .list()
2705
2725
  .map((metadata) => metadata.createConfigurationFromEnv(process.env))
@@ -2707,15 +2727,8 @@ async function $provideLlmToolsConfigurationFromEnv() {
2707
2727
  return llmToolsConfiguration;
2708
2728
  }
2709
2729
  /**
2710
- * TODO: [🧠][🪁] Maybe do allow to do auto-install if package not registered and not found
2711
- * TODO: Add Azure OpenAI
2712
- * TODO: [🧠][🍛]
2713
- * TODO: [🧠] Is there some meaningfull way how to test this util
2714
2730
  * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
2715
- * TODO: [👷‍♂️] @@@ Manual about construction of llmTools
2716
- * TODO: This should be maybe not under `_common` but under `utils`
2717
- * TODO: [🧠][⚛] Maybe pass env as argument
2718
- * TODO: [®] DRY Register logic */
2731
+ */
2719
2732
 
2720
2733
  /**
2721
2734
  * Multiple LLM Execution Tools is a proxy server that uses multiple execution tools internally and exposes the executor interface externally.
@@ -2965,6 +2978,7 @@ function createLlmToolsFromConfiguration(configuration, options = {}) {
2965
2978
  * Note: This function is not cached, every call creates new instance of `MultipleLlmExecutionTools`
2966
2979
  *
2967
2980
  * @@@ .env
2981
+ * Note: `$` is used to indicate that this function is not a pure function - it uses filesystem to access .env file
2968
2982
  *
2969
2983
  * It looks for environment variables:
2970
2984
  * - `process.env.OPENAI_API_KEY`
@@ -3009,6 +3023,8 @@ async function $provideLlmToolsFromEnv(options = {}) {
3009
3023
  /**
3010
3024
  * Returns LLM tools for CLI
3011
3025
  *
3026
+ * Note: `$` is used to indicate that this function is not a pure function - it uses filesystem to access .env file and also writes this .env file
3027
+ *
3012
3028
  * @private within the repository - for CLI utils
3013
3029
  */
3014
3030
  async function $provideLlmToolsForWizzardOrCli(options) {
@@ -3020,6 +3036,8 @@ async function $provideLlmToolsForWizzardOrCli(options) {
3020
3036
  let llmExecutionTools;
3021
3037
  if (strategy === 'REMOTE_SERVER') {
3022
3038
  const { remoteServerUrl = DEFAULT_REMOTE_SERVER_URL, loginPrompt } = options;
3039
+ // TODO: !!!
3040
+ // const envFilepath = await $provideEnvFilepath();
3023
3041
  const storage = new MemoryStorage(); // <- TODO: !!!!!! Save to `.promptbook` folder
3024
3042
  const key = `${remoteServerUrl}-identification`;
3025
3043
  let identification = await storage.getItem(key);
@@ -3056,6 +3074,54 @@ async function $provideLlmToolsForWizzardOrCli(options) {
3056
3074
  * TODO: [®] DRY Register logic
3057
3075
  */
3058
3076
 
3077
+ /**
3078
+ * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
3079
+ *
3080
+ * @public exported from `@promptbook/core`
3081
+ */
3082
+ const promptbookFetch = async (urlOrRequest, init) => {
3083
+ try {
3084
+ return await fetch(urlOrRequest, init);
3085
+ }
3086
+ catch (error) {
3087
+ if (!(error instanceof Error)) {
3088
+ throw error;
3089
+ }
3090
+ let url;
3091
+ if (typeof urlOrRequest === 'string') {
3092
+ url = urlOrRequest;
3093
+ }
3094
+ else if (urlOrRequest instanceof Request) {
3095
+ url = urlOrRequest.url;
3096
+ }
3097
+ throw new PromptbookFetchError(spaceTrim((block) => `
3098
+ Can not fetch "${url}"
3099
+
3100
+ Fetch error:
3101
+ ${block(error.message)}
3102
+
3103
+ `));
3104
+ }
3105
+ };
3106
+ /**
3107
+ * TODO: [🧠] Maybe rename because it is not used only for scrapers but also in `$getCompiledBook`
3108
+ */
3109
+
3110
+ /**
3111
+ * Checks if value is valid email
3112
+ *
3113
+ * @public exported from `@promptbook/utils`
3114
+ */
3115
+ function isValidEmail(email) {
3116
+ if (typeof email !== 'string') {
3117
+ return false;
3118
+ }
3119
+ if (email.split('\n').length > 1) {
3120
+ return false;
3121
+ }
3122
+ return /^.+@.+\..+$/.test(email);
3123
+ }
3124
+
3059
3125
  /**
3060
3126
  * @private utility of CLI
3061
3127
  */
@@ -3109,7 +3175,9 @@ function $provideLlmToolsForCli(options) {
3109
3175
  },
3110
3176
  ]);
3111
3177
  const loginUrl = `${remoteServerUrl}/login`;
3112
- const response = await fetch(loginUrl, {
3178
+ console.log('!!!', { loginUrl });
3179
+ // TODO: [🧠] Should we use normal `fetch` or `scraperFetch`
3180
+ const response = await promptbookFetch(loginUrl, {
3113
3181
  method: 'POST',
3114
3182
  headers: {
3115
3183
  'Content-Type': 'application/json',
@@ -3127,12 +3195,31 @@ function $provideLlmToolsForCli(options) {
3127
3195
  // type: response.type,
3128
3196
  // text: await response.text(),
3129
3197
  });
3130
- const body = (await response.json());
3131
- if ('error' in body) {
3132
- console.log(colors.red(body.error.message));
3198
+ const { isSuccess, message, error, identification } = (await response.json());
3199
+ console.log('!!!', {
3200
+ isSuccess,
3201
+ message,
3202
+ error,
3203
+ identification,
3204
+ });
3205
+ if (message) {
3206
+ if (isSuccess) {
3207
+ console.log(colors.green(message));
3208
+ }
3209
+ else {
3210
+ console.log(colors.red(message));
3211
+ }
3212
+ }
3213
+ if (!isSuccess) {
3214
+ // Note: Login failed
3133
3215
  process.exit(1);
3134
3216
  }
3135
- return body.identification;
3217
+ if (!identification) {
3218
+ // Note: Do not get identification here, but server signalizes the success so exiting but with code 0
3219
+ // This can mean for example that user needs to verify email
3220
+ process.exit(0);
3221
+ }
3222
+ return identification;
3136
3223
  },
3137
3224
  });
3138
3225
  }
@@ -6794,32 +6881,6 @@ function mimeTypeToExtension(value) {
6794
6881
  return extension(value) || null;
6795
6882
  }
6796
6883
 
6797
- /**
6798
- * The built-in `fetch' function with a lightweight error handling wrapper as default fetch function used in Promptbook scrapers
6799
- *
6800
- * @private as default `fetch` function used in Promptbook scrapers
6801
- */
6802
- const scraperFetch = async (url, init) => {
6803
- try {
6804
- return await fetch(url, init);
6805
- }
6806
- catch (error) {
6807
- if (!(error instanceof Error)) {
6808
- throw error;
6809
- }
6810
- throw new KnowledgeScrapeError(spaceTrim((block) => `
6811
- Can not fetch "${url}"
6812
-
6813
- Fetch error:
6814
- ${block(error.message)}
6815
-
6816
- `));
6817
- }
6818
- };
6819
- /**
6820
- * TODO: [🧠] Maybe rename because it is not used only for scrapers but also in `$getCompiledBook`
6821
- */
6822
-
6823
6884
  /**
6824
6885
  * @@@
6825
6886
  *
@@ -6828,7 +6889,7 @@ const scraperFetch = async (url, init) => {
6828
6889
  async function makeKnowledgeSourceHandler(knowledgeSource, tools, options) {
6829
6890
  // console.log('!! makeKnowledgeSourceHandler', knowledgeSource);
6830
6891
  var _a;
6831
- const { fetch = scraperFetch } = tools;
6892
+ const { fetch = promptbookFetch } = tools;
6832
6893
  const { knowledgeSourceContent } = knowledgeSource;
6833
6894
  let { name } = knowledgeSource;
6834
6895
  const { rootDirname = null,
@@ -12717,7 +12778,7 @@ function $initializeRunCommand(program) {
12717
12778
  const tools = {
12718
12779
  llm,
12719
12780
  fs,
12720
- fetch: scraperFetch,
12781
+ fetch: promptbookFetch,
12721
12782
  scrapers: await $provideScrapersForNode({ fs, llm, executables }, prepareAndScrapeOptions),
12722
12783
  script: [new JavascriptExecutionTools(cliOptions)],
12723
12784
  };
@@ -13086,8 +13147,19 @@ function startRemoteServer(options) {
13086
13147
  const username = request.body.username;
13087
13148
  const password = request.body.password;
13088
13149
  const appId = request.body.appId;
13089
- const identification = await login({ username, password, appId });
13090
- response.status(201).send({ identification });
13150
+ const { isSuccess, error, message, identification } = await login({
13151
+ username,
13152
+ password,
13153
+ appId,
13154
+ rawRequest: request,
13155
+ rawResponse: response,
13156
+ });
13157
+ response.status(201).send({
13158
+ isSuccess,
13159
+ message,
13160
+ error: error ? serializeError(error) : undefined,
13161
+ identification,
13162
+ });
13091
13163
  return;
13092
13164
  }
13093
13165
  catch (error) {
@@ -13095,7 +13167,11 @@ function startRemoteServer(options) {
13095
13167
  throw error;
13096
13168
  }
13097
13169
  if (error instanceof AuthenticationError) {
13098
- response.status(401).send({ error: serializeError(error) });
13170
+ response.status(401).send({
13171
+ isSuccess: false,
13172
+ message: error.message,
13173
+ error: serializeError(error),
13174
+ });
13099
13175
  }
13100
13176
  console.warn(`Login function thrown different error than AuthenticationError`, {
13101
13177
  error,