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