@promptbook/remote-server 0.86.31 → 0.88.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/umd/index.umd.js CHANGED
@@ -28,7 +28,7 @@
28
28
  * @generated
29
29
  * @see https://github.com/webgptorg/promptbook
30
30
  */
31
- const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
31
+ const PROMPTBOOK_ENGINE_VERSION = '0.88.0-8';
32
32
  /**
33
33
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
34
34
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -1790,21 +1790,41 @@
1790
1790
  function createTask(options) {
1791
1791
  const { taskType, taskProcessCallback } = options;
1792
1792
  const taskId = `${taskType.toLowerCase().substring(0, 4)}-${$randomToken(8 /* <- TODO: To global config + Use Base58 to avoid simmilar char conflicts */)}`;
1793
- const partialResultSubject = new rxjs.BehaviorSubject({});
1793
+ let status = 'RUNNING';
1794
+ const createdAt = new Date();
1795
+ let updatedAt = createdAt;
1796
+ const errors = [];
1797
+ const warnings = [];
1798
+ const currentValue = {};
1799
+ const partialResultSubject = new rxjs.Subject();
1800
+ // <- Note: Not using `BehaviorSubject` because on error we can't access the last value
1794
1801
  const finalResultPromise = /* not await */ taskProcessCallback((newOngoingResult) => {
1802
+ Object.assign(currentValue, newOngoingResult);
1795
1803
  partialResultSubject.next(newOngoingResult);
1796
1804
  });
1797
1805
  finalResultPromise
1798
1806
  .catch((error) => {
1807
+ errors.push(error);
1799
1808
  partialResultSubject.error(error);
1800
1809
  })
1801
- .then((value) => {
1802
- if (value) {
1810
+ .then((executionResult) => {
1811
+ if (executionResult) {
1803
1812
  try {
1804
- assertsTaskSuccessful(value);
1805
- partialResultSubject.next(value);
1813
+ updatedAt = new Date();
1814
+ errors.push(...executionResult.errors);
1815
+ warnings.push(...executionResult.warnings);
1816
+ // <- TODO: !!! Only unique errors and warnings should be added (or filtered)
1817
+ // TODO: [🧠] !!! errors, warning, isSuccessful are redundant both in `ExecutionTask` and `ExecutionTask.currentValue`
1818
+ // Also maybe move `ExecutionTask.currentValue.usage` -> `ExecutionTask.usage`
1819
+ // And delete `ExecutionTask.currentValue.preparedPipeline`
1820
+ assertsTaskSuccessful(executionResult);
1821
+ status = 'FINISHED';
1822
+ Object.assign(currentValue, executionResult);
1823
+ partialResultSubject.next(executionResult);
1806
1824
  }
1807
1825
  catch (error) {
1826
+ status = 'ERROR';
1827
+ errors.push(error);
1808
1828
  partialResultSubject.error(error);
1809
1829
  }
1810
1830
  }
@@ -1821,12 +1841,33 @@
1821
1841
  return {
1822
1842
  taskType,
1823
1843
  taskId,
1844
+ get status() {
1845
+ return status;
1846
+ // <- Note: [1] Theese must be getters to allow changing the value in the future
1847
+ },
1848
+ get createdAt() {
1849
+ return createdAt;
1850
+ // <- Note: [1]
1851
+ },
1852
+ get updatedAt() {
1853
+ return updatedAt;
1854
+ // <- Note: [1]
1855
+ },
1824
1856
  asPromise,
1825
1857
  asObservable() {
1826
1858
  return partialResultSubject.asObservable();
1827
1859
  },
1860
+ get errors() {
1861
+ return errors;
1862
+ // <- Note: [1]
1863
+ },
1864
+ get warnings() {
1865
+ return warnings;
1866
+ // <- Note: [1]
1867
+ },
1828
1868
  get currentValue() {
1829
- return partialResultSubject.value;
1869
+ return currentValue;
1870
+ // <- Note: [1]
1830
1871
  },
1831
1872
  };
1832
1873
  }
@@ -3969,7 +4010,7 @@
3969
4010
  * @param script from which to extract the variables
3970
4011
  * @returns the list of variable names
3971
4012
  * @throws {ParseError} if the script is invalid
3972
- * @public exported from `@promptbook/execute-javascript`
4013
+ * @public exported from `@promptbook/javascript`
3973
4014
  */
3974
4015
  function extractVariablesFromJavascript(script) {
3975
4016
  const variables = new Set();
@@ -5050,7 +5091,7 @@
5050
5091
  Last result:
5051
5092
  ${block($ongoingTaskResult.$resultString === null
5052
5093
  ? 'null'
5053
- : $ongoingTaskResult.$resultString
5094
+ : spaceTrim.spaceTrim($ongoingTaskResult.$resultString)
5054
5095
  .split('\n')
5055
5096
  .map((line) => `> ${line}`)
5056
5097
  .join('\n'))}
@@ -5943,6 +5984,623 @@
5943
5984
  * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
5944
5985
  */
5945
5986
 
5987
+ /**
5988
+ * @@@
5989
+ *
5990
+ * @param text @@@
5991
+ * @param _isFirstLetterCapital @@@
5992
+ * @returns @@@
5993
+ * @example 'helloWorld'
5994
+ * @example 'iLovePromptbook'
5995
+ * @public exported from `@promptbook/utils`
5996
+ */
5997
+ function normalizeTo_camelCase(text, _isFirstLetterCapital = false) {
5998
+ let charType;
5999
+ let lastCharType = null;
6000
+ let normalizedName = '';
6001
+ for (const char of text) {
6002
+ let normalizedChar;
6003
+ if (/^[a-z]$/.test(char)) {
6004
+ charType = 'LOWERCASE';
6005
+ normalizedChar = char;
6006
+ }
6007
+ else if (/^[A-Z]$/.test(char)) {
6008
+ charType = 'UPPERCASE';
6009
+ normalizedChar = char.toLowerCase();
6010
+ }
6011
+ else if (/^[0-9]$/.test(char)) {
6012
+ charType = 'NUMBER';
6013
+ normalizedChar = char;
6014
+ }
6015
+ else {
6016
+ charType = 'OTHER';
6017
+ normalizedChar = '';
6018
+ }
6019
+ if (!lastCharType) {
6020
+ if (_isFirstLetterCapital) {
6021
+ normalizedChar = normalizedChar.toUpperCase(); //TODO: DRY
6022
+ }
6023
+ }
6024
+ else if (charType !== lastCharType &&
6025
+ !(charType === 'LOWERCASE' && lastCharType === 'UPPERCASE') &&
6026
+ !(lastCharType === 'NUMBER') &&
6027
+ !(charType === 'NUMBER')) {
6028
+ normalizedChar = normalizedChar.toUpperCase(); //TODO: [🌺] DRY
6029
+ }
6030
+ normalizedName += normalizedChar;
6031
+ lastCharType = charType;
6032
+ }
6033
+ return normalizedName;
6034
+ }
6035
+ /**
6036
+ * TODO: [🌺] Use some intermediate util splitWords
6037
+ */
6038
+
6039
+ /**
6040
+ * Detects if the code is running in a browser environment in main thread (Not in a web worker)
6041
+ *
6042
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
6043
+ *
6044
+ * @public exported from `@promptbook/utils`
6045
+ */
6046
+ new Function(`
6047
+ try {
6048
+ return this === window;
6049
+ } catch (e) {
6050
+ return false;
6051
+ }
6052
+ `);
6053
+ /**
6054
+ * TODO: [🎺]
6055
+ */
6056
+
6057
+ /**
6058
+ * Detects if the code is running in jest environment
6059
+ *
6060
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
6061
+ *
6062
+ * @public exported from `@promptbook/utils`
6063
+ */
6064
+ new Function(`
6065
+ try {
6066
+ return process.env.JEST_WORKER_ID !== undefined;
6067
+ } catch (e) {
6068
+ return false;
6069
+ }
6070
+ `);
6071
+ /**
6072
+ * TODO: [🎺]
6073
+ */
6074
+
6075
+ /**
6076
+ * Detects if the code is running in a web worker
6077
+ *
6078
+ * Note: `$` is used to indicate that this function is not a pure function - it looks at the global object to determine the environment
6079
+ *
6080
+ * @public exported from `@promptbook/utils`
6081
+ */
6082
+ new Function(`
6083
+ try {
6084
+ if (typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope) {
6085
+ return true;
6086
+ } else {
6087
+ return false;
6088
+ }
6089
+ } catch (e) {
6090
+ return false;
6091
+ }
6092
+ `);
6093
+ /**
6094
+ * TODO: [🎺]
6095
+ */
6096
+
6097
+ /**
6098
+ * Makes first letter of a string uppercase
6099
+ *
6100
+ * @public exported from `@promptbook/utils`
6101
+ */
6102
+ function decapitalize(word) {
6103
+ return word.substring(0, 1).toLowerCase() + word.substring(1);
6104
+ }
6105
+
6106
+ /**
6107
+ * Parses keywords from a string
6108
+ *
6109
+ * @param {string} input
6110
+ * @returns {Set} of keywords without diacritics in lowercase
6111
+ * @public exported from `@promptbook/utils`
6112
+ */
6113
+ function parseKeywordsFromString(input) {
6114
+ const keywords = normalizeTo_SCREAMING_CASE(removeDiacritics(input))
6115
+ .toLowerCase()
6116
+ .split(/[^a-z0-9]+/gs)
6117
+ .filter((value) => value);
6118
+ return new Set(keywords);
6119
+ }
6120
+
6121
+ /**
6122
+ * @@@
6123
+ *
6124
+ * @param name @@@
6125
+ * @returns @@@
6126
+ * @example @@@
6127
+ * @public exported from `@promptbook/utils`
6128
+ */
6129
+ function nameToUriPart(name) {
6130
+ let uriPart = name;
6131
+ uriPart = uriPart.toLowerCase();
6132
+ uriPart = removeDiacritics(uriPart);
6133
+ uriPart = uriPart.replace(/[^a-zA-Z0-9]+/g, '-');
6134
+ uriPart = uriPart.replace(/^-+/, '');
6135
+ uriPart = uriPart.replace(/-+$/, '');
6136
+ return uriPart;
6137
+ }
6138
+
6139
+ /**
6140
+ * @@@
6141
+ *
6142
+ * @param name @@@
6143
+ * @returns @@@
6144
+ * @example @@@
6145
+ * @public exported from `@promptbook/utils`
6146
+ */
6147
+ function nameToUriParts(name) {
6148
+ return nameToUriPart(name)
6149
+ .split('-')
6150
+ .filter((value) => value !== '');
6151
+ }
6152
+
6153
+ /**
6154
+ *
6155
+ * @param text @public exported from `@promptbook/utils`
6156
+ * @returns
6157
+ * @example 'HelloWorld'
6158
+ * @example 'ILovePromptbook'
6159
+ * @public exported from `@promptbook/utils`
6160
+ */
6161
+ function normalizeTo_PascalCase(text) {
6162
+ return normalizeTo_camelCase(text, true);
6163
+ }
6164
+
6165
+ /**
6166
+ * Take every whitespace (space, new line, tab) and replace it with a single space
6167
+ *
6168
+ * @public exported from `@promptbook/utils`
6169
+ */
6170
+ function normalizeWhitespaces(sentence) {
6171
+ return sentence.replace(/\s+/gs, ' ').trim();
6172
+ }
6173
+
6174
+ /**
6175
+ * Removes quotes from a string
6176
+ *
6177
+ * Tip: This is very usefull for post-processing of the result of the LLM model
6178
+ * Note: This function removes only the same quotes from the beginning and the end of the string
6179
+ * Note: There are two simmilar functions:
6180
+ * - `removeQuotes` which removes only bounding quotes
6181
+ * - `unwrapResult` which removes whole introduce sentence
6182
+ *
6183
+ * @param text optionally quoted text
6184
+ * @returns text without quotes
6185
+ * @public exported from `@promptbook/utils`
6186
+ */
6187
+ function removeQuotes(text) {
6188
+ if (text.startsWith('"') && text.endsWith('"')) {
6189
+ return text.slice(1, -1);
6190
+ }
6191
+ if (text.startsWith('\'') && text.endsWith('\'')) {
6192
+ return text.slice(1, -1);
6193
+ }
6194
+ return text;
6195
+ }
6196
+
6197
+ /**
6198
+ * Function trimCodeBlock will trim starting and ending code block from the string if it is present.
6199
+ *
6200
+ * Note: This is usefull for post-processing of the result of the chat LLM model
6201
+ * when the model wraps the result in the (markdown) code block.
6202
+ *
6203
+ * @public exported from `@promptbook/utils`
6204
+ */
6205
+ function trimCodeBlock(value) {
6206
+ value = spaceTrim.spaceTrim(value);
6207
+ if (!/^```[a-z]*(.*)```$/is.test(value)) {
6208
+ return value;
6209
+ }
6210
+ value = value.replace(/^```[a-z]*/i, '');
6211
+ value = value.replace(/```$/i, '');
6212
+ value = spaceTrim.spaceTrim(value);
6213
+ return value;
6214
+ }
6215
+
6216
+ /**
6217
+ * Function trimEndOfCodeBlock will remove ending code block from the string if it is present.
6218
+ *
6219
+ * Note: This is usefull for post-processing of the result of the completion LLM model
6220
+ * if you want to start code block in the prompt but you don't want to end it in the result.
6221
+ *
6222
+ * @public exported from `@promptbook/utils`
6223
+ */
6224
+ function trimEndOfCodeBlock(value) {
6225
+ value = spaceTrim.spaceTrim(value);
6226
+ value = value.replace(/```$/g, '');
6227
+ value = spaceTrim.spaceTrim(value);
6228
+ return value;
6229
+ }
6230
+
6231
+ /**
6232
+ * Removes quotes and optional introduce text from a string
6233
+ *
6234
+ * Tip: This is very usefull for post-processing of the result of the LLM model
6235
+ * Note: This function trims the text and removes whole introduce sentence if it is present
6236
+ * Note: There are two simmilar functions:
6237
+ * - `removeQuotes` which removes only bounding quotes
6238
+ * - `unwrapResult` which removes whole introduce sentence
6239
+ *
6240
+ * @param text optionally quoted text
6241
+ * @returns text without quotes
6242
+ * @public exported from `@promptbook/utils`
6243
+ */
6244
+ function unwrapResult(text, options) {
6245
+ const { isTrimmed = true, isIntroduceSentenceRemoved = true } = options || {};
6246
+ let trimmedText = text;
6247
+ // Remove leading and trailing spaces and newlines
6248
+ if (isTrimmed) {
6249
+ trimmedText = spaceTrim.spaceTrim(trimmedText);
6250
+ }
6251
+ let processedText = trimmedText;
6252
+ if (isIntroduceSentenceRemoved) {
6253
+ const introduceSentenceRegex = /^[a-zěščřžýáíéúů:\s]*:\s*/i;
6254
+ if (introduceSentenceRegex.test(text)) {
6255
+ // Remove the introduce sentence and quotes by replacing it with an empty string
6256
+ processedText = processedText.replace(introduceSentenceRegex, '');
6257
+ }
6258
+ processedText = spaceTrim.spaceTrim(processedText);
6259
+ }
6260
+ if (processedText.length < 3) {
6261
+ return trimmedText;
6262
+ }
6263
+ if (processedText.includes('\n')) {
6264
+ return trimmedText;
6265
+ }
6266
+ // Remove the quotes by extracting the substring without the first and last characters
6267
+ const unquotedText = processedText.slice(1, -1);
6268
+ // Check if the text starts and ends with quotes
6269
+ if ([
6270
+ ['"', '"'],
6271
+ ["'", "'"],
6272
+ ['`', '`'],
6273
+ ['*', '*'],
6274
+ ['_', '_'],
6275
+ ['„', '“'],
6276
+ ['«', '»'] /* <- QUOTES to config */,
6277
+ ].some(([startQuote, endQuote]) => {
6278
+ if (!processedText.startsWith(startQuote)) {
6279
+ return false;
6280
+ }
6281
+ if (!processedText.endsWith(endQuote)) {
6282
+ return false;
6283
+ }
6284
+ if (unquotedText.includes(startQuote) && !unquotedText.includes(endQuote)) {
6285
+ return false;
6286
+ }
6287
+ if (!unquotedText.includes(startQuote) && unquotedText.includes(endQuote)) {
6288
+ return false;
6289
+ }
6290
+ return true;
6291
+ })) {
6292
+ return unwrapResult(unquotedText, { isTrimmed: false, isIntroduceSentenceRemoved: false });
6293
+ }
6294
+ else {
6295
+ return processedText;
6296
+ }
6297
+ }
6298
+ /**
6299
+ * TODO: [🧠] Should this also unwrap the (parenthesis)
6300
+ */
6301
+
6302
+ /**
6303
+ * Extracts exactly ONE code block from markdown.
6304
+ *
6305
+ * - When there are multiple or no code blocks the function throws a `ParseError`
6306
+ *
6307
+ * Note: There are multiple simmilar function:
6308
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
6309
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
6310
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
6311
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
6312
+ *
6313
+ * @param markdown any valid markdown
6314
+ * @returns code block with language and content
6315
+ * @public exported from `@promptbook/markdown-utils`
6316
+ * @throws {ParseError} if there is not exactly one code block in the markdown
6317
+ */
6318
+ function extractOneBlockFromMarkdown(markdown) {
6319
+ const codeBlocks = extractAllBlocksFromMarkdown(markdown);
6320
+ if (codeBlocks.length !== 1) {
6321
+ throw new ParseError(spaceTrim__default["default"]((block) => `
6322
+ There should be exactly 1 code block in task section, found ${codeBlocks.length} code blocks
6323
+
6324
+ ${block(codeBlocks.map((block, i) => `Block ${i + 1}:\n${block.content}`).join('\n\n\n'))}
6325
+ `));
6326
+ }
6327
+ return codeBlocks[0];
6328
+ }
6329
+ /***
6330
+ * TODO: [🍓][🌻] Decide of this is internal utility, external util OR validator/postprocessor
6331
+ */
6332
+
6333
+ /**
6334
+ * Extracts code block from markdown.
6335
+ *
6336
+ * - When there are multiple or no code blocks the function throws a `ParseError`
6337
+ *
6338
+ * Note: There are multiple simmilar function:
6339
+ * - `extractBlock` just extracts the content of the code block which is also used as build-in function for postprocessing
6340
+ * - `extractJsonBlock` extracts exactly one valid JSON code block
6341
+ * - `extractOneBlockFromMarkdown` extracts exactly one code block with language of the code block
6342
+ * - `extractAllBlocksFromMarkdown` extracts all code blocks with language of the code block
6343
+ *
6344
+ * @public exported from `@promptbook/markdown-utils`
6345
+ * @throws {ParseError} if there is not exactly one code block in the markdown
6346
+ */
6347
+ function extractBlock(markdown) {
6348
+ const { content } = extractOneBlockFromMarkdown(markdown);
6349
+ return content;
6350
+ }
6351
+
6352
+ /**
6353
+ * Does nothing, but preserves the function in the bundle
6354
+ * Compiler is tricked into thinking the function is used
6355
+ *
6356
+ * @param value any function to preserve
6357
+ * @returns nothing
6358
+ * @private internal function of `JavascriptExecutionTools` and `JavascriptEvalExecutionTools`
6359
+ */
6360
+ function preserve(func) {
6361
+ // Note: NOT calling the function
6362
+ (async () => {
6363
+ // TODO: [💩] Change to `await forEver` or something better
6364
+ await waitasecond.forTime(100000000);
6365
+ // [1]
6366
+ try {
6367
+ await func();
6368
+ }
6369
+ finally {
6370
+ // do nothing
6371
+ }
6372
+ })();
6373
+ }
6374
+ /**
6375
+ * TODO: Probbably remove in favour of `keepImported`
6376
+ * TODO: [1] This maybe does memory leak
6377
+ */
6378
+
6379
+ // Note: [💎]
6380
+ /**
6381
+ * ScriptExecutionTools for JavaScript implemented via eval
6382
+ *
6383
+ * Warning: It is used for testing and mocking
6384
+ * **NOT intended to use in the production** due to its unsafe nature, use `JavascriptExecutionTools` instead.
6385
+ *
6386
+ * @public exported from `@promptbook/javascript`
6387
+ */
6388
+ class JavascriptEvalExecutionTools {
6389
+ constructor(options) {
6390
+ this.options = options || {};
6391
+ }
6392
+ /**
6393
+ * Executes a JavaScript
6394
+ */
6395
+ async execute(options) {
6396
+ const { scriptLanguage, parameters } = options;
6397
+ let { script } = options;
6398
+ if (scriptLanguage !== 'javascript') {
6399
+ throw new PipelineExecutionError(`Script language ${scriptLanguage} not supported to be executed by JavascriptEvalExecutionTools`);
6400
+ }
6401
+ // Note: [💎]
6402
+ // Note: Using direct eval, following variables are in same scope as eval call so they are accessible from inside the evaluated script:
6403
+ const spaceTrim = (_) => spaceTrim__default["default"](_);
6404
+ preserve(spaceTrim);
6405
+ const removeQuotes$1 = removeQuotes;
6406
+ preserve(removeQuotes$1);
6407
+ const unwrapResult$1 = unwrapResult;
6408
+ preserve(unwrapResult$1);
6409
+ const trimEndOfCodeBlock$1 = trimEndOfCodeBlock;
6410
+ preserve(trimEndOfCodeBlock$1);
6411
+ const trimCodeBlock$1 = trimCodeBlock;
6412
+ preserve(trimCodeBlock$1);
6413
+ // TODO: DRY [🍯]
6414
+ const trim = (str) => str.trim();
6415
+ preserve(trim);
6416
+ // TODO: DRY [🍯]
6417
+ const reverse = (str) => str.split('').reverse().join('');
6418
+ preserve(reverse);
6419
+ const removeEmojis$1 = removeEmojis;
6420
+ preserve(removeEmojis$1);
6421
+ const prettifyMarkdown$1 = prettifyMarkdown;
6422
+ preserve(prettifyMarkdown$1);
6423
+ //-------[n12:]---
6424
+ const capitalize$1 = capitalize;
6425
+ const decapitalize$1 = decapitalize;
6426
+ const nameToUriPart$1 = nameToUriPart;
6427
+ const nameToUriParts$1 = nameToUriParts;
6428
+ const removeDiacritics$1 = removeDiacritics;
6429
+ const normalizeWhitespaces$1 = normalizeWhitespaces;
6430
+ const normalizeToKebabCase$1 = normalizeToKebabCase;
6431
+ const normalizeTo_camelCase$1 = normalizeTo_camelCase;
6432
+ const normalizeTo_snake_case$1 = normalizeTo_snake_case;
6433
+ const normalizeTo_PascalCase$1 = normalizeTo_PascalCase;
6434
+ const parseKeywords = (input) =>
6435
+ // TODO: DRY [🍯]
6436
+ Array.from(parseKeywordsFromString(input)).join(', '); /* <- TODO: [🧠] What is the best format comma list, bullet list,...? */
6437
+ const normalizeTo_SCREAMING_CASE$1 = normalizeTo_SCREAMING_CASE;
6438
+ preserve(capitalize$1);
6439
+ preserve(decapitalize$1);
6440
+ preserve(nameToUriPart$1);
6441
+ preserve(nameToUriParts$1);
6442
+ preserve(removeDiacritics$1);
6443
+ preserve(normalizeWhitespaces$1);
6444
+ preserve(normalizeToKebabCase$1);
6445
+ preserve(normalizeTo_camelCase$1);
6446
+ preserve(normalizeTo_snake_case$1);
6447
+ preserve(normalizeTo_PascalCase$1);
6448
+ preserve(parseKeywords);
6449
+ preserve(normalizeTo_SCREAMING_CASE$1);
6450
+ //-------[/n12]---
6451
+ if (!script.includes('return')) {
6452
+ script = `return ${script}`;
6453
+ }
6454
+ // TODO: DRY [🍯]
6455
+ const buildinFunctions = {
6456
+ // TODO: [🍯] DRY all these functions across the file
6457
+ spaceTrim,
6458
+ removeQuotes: removeQuotes$1,
6459
+ unwrapResult: unwrapResult$1,
6460
+ trimEndOfCodeBlock: trimEndOfCodeBlock$1,
6461
+ trimCodeBlock: trimCodeBlock$1,
6462
+ trim,
6463
+ reverse,
6464
+ removeEmojis: removeEmojis$1,
6465
+ prettifyMarkdown: prettifyMarkdown$1,
6466
+ capitalize: capitalize$1,
6467
+ decapitalize: decapitalize$1,
6468
+ nameToUriPart: nameToUriPart$1,
6469
+ nameToUriParts: nameToUriParts$1,
6470
+ removeDiacritics: removeDiacritics$1,
6471
+ normalizeWhitespaces: normalizeWhitespaces$1,
6472
+ normalizeToKebabCase: normalizeToKebabCase$1,
6473
+ normalizeTo_camelCase: normalizeTo_camelCase$1,
6474
+ normalizeTo_snake_case: normalizeTo_snake_case$1,
6475
+ normalizeTo_PascalCase: normalizeTo_PascalCase$1,
6476
+ parseKeywords,
6477
+ normalizeTo_SCREAMING_CASE: normalizeTo_SCREAMING_CASE$1,
6478
+ extractBlock, // <- [🍓] Remove balast in all other functions, use this one as example
6479
+ };
6480
+ const buildinFunctionsStatement = Object.keys(buildinFunctions)
6481
+ .map((functionName) =>
6482
+ // Note: Custom functions are exposed to the current scope as variables
6483
+ `const ${functionName} = buildinFunctions.${functionName};`)
6484
+ .join('\n');
6485
+ // TODO: DRY [🍯]
6486
+ const customFunctions = this.options.functions || {};
6487
+ const customFunctionsStatement = Object.keys(customFunctions)
6488
+ .map((functionName) =>
6489
+ // Note: Custom functions are exposed to the current scope as variables
6490
+ `const ${functionName} = customFunctions.${functionName};`)
6491
+ .join('\n');
6492
+ // script = templateParameters(script, parameters);
6493
+ // <- TODO: [🧠][🥳] Should be this is one of two variants how to use parameters in script
6494
+ const statementToEvaluate = spaceTrim__default["default"]((block) => `
6495
+
6496
+ // Build-in functions:
6497
+ ${block(buildinFunctionsStatement)}
6498
+
6499
+ // Custom functions:
6500
+ ${block(customFunctionsStatement || '// -- No custom functions --')}
6501
+
6502
+ // The script:
6503
+ ${block(Object.entries(parameters)
6504
+ .map(([key, value]) => `const ${key} = ${JSON.stringify(value)};`)
6505
+ .join('\n'))}
6506
+ (()=>{ ${script} })()
6507
+ `);
6508
+ if (this.options.isVerbose) {
6509
+ console.info(spaceTrim__default["default"]((block) => `
6510
+ 🚀 Evaluating ${scriptLanguage} script:
6511
+
6512
+ ${block(statementToEvaluate)}`));
6513
+ }
6514
+ let result;
6515
+ try {
6516
+ result = await eval(statementToEvaluate);
6517
+ if (typeof result !== 'string') {
6518
+ throw new PipelineExecutionError(`Script must return a string, but returned ${valueToString(result)}`);
6519
+ }
6520
+ }
6521
+ catch (error) {
6522
+ if (!(error instanceof Error)) {
6523
+ throw error;
6524
+ }
6525
+ if (error instanceof ReferenceError) {
6526
+ const undefinedName = error.message.split(' ')[0];
6527
+ /*
6528
+ Note: Remapping error
6529
+ From: [PipelineUrlError: thing is not defined],
6530
+ To: [PipelineExecutionError: Parameter `{thing}` is not defined],
6531
+ */
6532
+ if (!statementToEvaluate.includes(undefinedName + '(')) {
6533
+ throw new PipelineExecutionError(spaceTrim__default["default"]((block) => `
6534
+
6535
+ Parameter \`{${undefinedName}}\` is not defined
6536
+
6537
+ This happen during evaluation of the javascript, which has access to the following parameters as javascript variables:
6538
+
6539
+ ${block(Object.keys(parameters)
6540
+ .map((key) => ` - ${key}\n`)
6541
+ .join(''))}
6542
+
6543
+ The script is:
6544
+ \`\`\`javascript
6545
+ ${block(script)}
6546
+ \`\`\`
6547
+
6548
+ Original error message:
6549
+ ${block(error.message)}
6550
+
6551
+
6552
+ `));
6553
+ }
6554
+ else {
6555
+ throw new PipelineExecutionError(spaceTrim__default["default"]((block) => `
6556
+ Function ${undefinedName}() is not defined
6557
+
6558
+ - Make sure that the function is one of built-in functions
6559
+ - Or you have to defined the function during construction of JavascriptEvalExecutionTools
6560
+
6561
+ Original error message:
6562
+ ${block(error.message)}
6563
+
6564
+ `));
6565
+ }
6566
+ }
6567
+ throw error;
6568
+ }
6569
+ if (typeof result !== 'string') {
6570
+ throw new PipelineExecutionError(`Script must return a string, but ${valueToString(result)}`);
6571
+ }
6572
+ return result;
6573
+ }
6574
+ }
6575
+ /**
6576
+ * TODO: Put predefined functions (like removeQuotes, spaceTrim, etc.) into annotation OR pass into constructor
6577
+ * TODO: [🧠][💙] Distinct between options passed into ExecutionTools and to ExecutionTools.execute
6578
+ */
6579
+
6580
+ /**
6581
+ * Placeholder for better implementation of JavascriptExecutionTools - some propper sandboxing
6582
+ *
6583
+ * @alias JavascriptExecutionTools
6584
+ * @public exported from `@promptbook/javascript`
6585
+ */
6586
+ const JavascriptExecutionTools = JavascriptEvalExecutionTools;
6587
+
6588
+ /**
6589
+ * Provides script execution tools
6590
+ *
6591
+ * @public exported from `@promptbook/node`
6592
+ */
6593
+ async function $provideScriptingForNode(options) {
6594
+ if (!$isRunningInNode()) {
6595
+ throw new EnvironmentMismatchError('Function `$provideScriptingForNode` works only in Node.js environment');
6596
+ }
6597
+ // TODO: [🔱] Do here auto-installation
6598
+ return [new JavascriptExecutionTools(options)];
6599
+ }
6600
+ /**
6601
+ * Note: [🟢] Code in this file should never be never released in packages that could be imported into browser environment
6602
+ */
6603
+
5946
6604
  /**
5947
6605
  * Remote server is a proxy server that uses its execution tools internally and exposes the executor interface externally.
5948
6606
  *
@@ -6014,7 +6672,7 @@
6014
6672
  llm,
6015
6673
  fs,
6016
6674
  scrapers: await $provideScrapersForNode({ fs, llm, executables }),
6017
- // TODO: Allow when `JavascriptExecutionTools` more secure *(without eval)*> script: [new JavascriptExecutionTools()],
6675
+ script: await $provideScriptingForNode({}),
6018
6676
  };
6019
6677
  return tools;
6020
6678
  }
@@ -6025,6 +6683,7 @@
6025
6683
  next();
6026
6684
  });
6027
6685
  const runningExecutionTasks = [];
6686
+ // <- TODO: [🤬] Identify the users
6028
6687
  // TODO: [🧠] Do here some garbage collection of finished tasks
6029
6688
  app.get(['/', rootPath], async (request, response) => {
6030
6689
  var _a;
@@ -6119,23 +6778,60 @@
6119
6778
  .send({ error: serializeError(error) });
6120
6779
  }
6121
6780
  });
6781
+ function exportExecutionTask(executionTask, isFull) {
6782
+ // <- TODO: [🧠] This should be maybe method of `ExecutionTask` itself
6783
+ const { taskType, taskId, status, errors, warnings, createdAt, updatedAt, currentValue } = executionTask;
6784
+ if (isFull) {
6785
+ return {
6786
+ nonce: '✨',
6787
+ taskId,
6788
+ taskType,
6789
+ status,
6790
+ errors: errors.map(serializeError),
6791
+ warnings: warnings.map(serializeError),
6792
+ createdAt,
6793
+ updatedAt,
6794
+ currentValue,
6795
+ };
6796
+ }
6797
+ else {
6798
+ return {
6799
+ nonce: '✨',
6800
+ taskId,
6801
+ taskType,
6802
+ status,
6803
+ createdAt,
6804
+ updatedAt,
6805
+ };
6806
+ }
6807
+ }
6122
6808
  app.get(`${rootPath}/executions`, async (request, response) => {
6123
- response.send(runningExecutionTasks);
6809
+ response.send(runningExecutionTasks.map((runningExecutionTask) => exportExecutionTask(runningExecutionTask, false)));
6810
+ });
6811
+ app.get(`${rootPath}/executions/last`, async (request, response) => {
6812
+ // TODO: [🤬] Filter only for user
6813
+ if (runningExecutionTasks.length === 0) {
6814
+ response.status(404).send('No execution tasks found');
6815
+ return;
6816
+ }
6817
+ const lastExecutionTask = runningExecutionTasks[runningExecutionTasks.length - 1];
6818
+ response.send(exportExecutionTask(lastExecutionTask, true));
6124
6819
  });
6125
6820
  app.get(`${rootPath}/executions/:taskId`, async (request, response) => {
6126
6821
  const { taskId } = request.params;
6127
- const execution = runningExecutionTasks.find((executionTask) => executionTask.taskId === taskId);
6128
- if (execution === undefined) {
6822
+ // TODO: [🤬] Filter only for user
6823
+ const executionTask = runningExecutionTasks.find((executionTask) => executionTask.taskId === taskId);
6824
+ if (executionTask === undefined) {
6129
6825
  response
6130
6826
  .status(404)
6131
6827
  .send(`Execution "${taskId}" not found`);
6132
6828
  return;
6133
6829
  }
6134
- response.send(execution.currentValue);
6830
+ response.send(exportExecutionTask(executionTask, true));
6135
6831
  });
6136
6832
  app.post(`${rootPath}/executions/new`, async (request, response) => {
6137
6833
  try {
6138
- const { inputParameters, identification } = request.body;
6834
+ const { inputParameters, identification /* <- [🤬] */ } = request.body;
6139
6835
  const pipelineUrl = request.body.pipelineUrl || request.body.book;
6140
6836
  // TODO: [🧠] Check `pipelineUrl` and `inputParameters` here or it should be responsibility of `collection.getPipelineByUrl` and `pipelineExecutor`
6141
6837
  const pipeline = await (collection === null || collection === void 0 ? void 0 : collection.getPipelineByUrl(pipelineUrl));