@promptbook/node 0.86.30 → 0.86.31

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.
@@ -2,7 +2,9 @@ import { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION } from '../version';
2
2
  import { JavascriptEvalExecutionTools } from '../scripting/javascript/JavascriptEvalExecutionTools';
3
3
  import { JavascriptExecutionTools } from '../scripting/javascript/JavascriptExecutionTools';
4
4
  import { POSTPROCESSING_FUNCTIONS } from '../scripting/javascript/postprocessing-functions';
5
+ import { extractVariablesFromJavascript } from '../scripting/javascript/utils/extractVariablesFromJavascript';
5
6
  export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
6
7
  export { JavascriptEvalExecutionTools };
7
8
  export { JavascriptExecutionTools };
8
9
  export { POSTPROCESSING_FUNCTIONS };
10
+ export { extractVariablesFromJavascript };
@@ -0,0 +1,4 @@
1
+ export {};
2
+ /**
3
+ * TODO: Make shared test between JavascriptEvalExecutionTools and JavascriptExecutionTools to test the same functionality when implemented via vm2
4
+ */
@@ -0,0 +1,14 @@
1
+ import type { string_javascript } from '../../../types/typeAliases';
2
+ import type { string_javascript_name } from '../../../types/typeAliases';
3
+ /**
4
+ * Parses the given script and returns the list of all used variables that are not defined in the script
5
+ *
6
+ * @param script from which to extract the variables
7
+ * @returns the list of variable names
8
+ * @throws {ParseError} if the script is invalid
9
+ * @public exported from `@promptbook/execute-javascript`
10
+ */
11
+ export declare function extractVariablesFromJavascript(script: string_javascript): Set<string_javascript_name>;
12
+ /**
13
+ * TODO: [🔣] Support for multiple languages - python, java,...
14
+ */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@promptbook/node",
3
- "version": "0.86.30",
3
+ "version": "0.86.31",
4
4
  "description": "It's time for a paradigm shift. The future of software in plain English, French or Latin",
5
5
  "private": false,
6
6
  "sideEffects": false,
@@ -47,7 +47,7 @@
47
47
  "module": "./esm/index.es.js",
48
48
  "typings": "./esm/typings/src/_packages/node.index.d.ts",
49
49
  "peerDependencies": {
50
- "@promptbook/core": "0.86.30"
50
+ "@promptbook/core": "0.86.31"
51
51
  },
52
52
  "dependencies": {
53
53
  "colors": "1.4.0",
package/umd/index.umd.js CHANGED
@@ -46,7 +46,7 @@
46
46
  * @generated
47
47
  * @see https://github.com/webgptorg/promptbook
48
48
  */
49
- const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
49
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
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
@@ -2132,6 +2132,83 @@
2132
2132
  }, deepClone(ZERO_USAGE));
2133
2133
  }
2134
2134
 
2135
+ /**
2136
+ * Parses the given script and returns the list of all used variables that are not defined in the script
2137
+ *
2138
+ * @param script from which to extract the variables
2139
+ * @returns the list of variable names
2140
+ * @throws {ParseError} if the script is invalid
2141
+ * @public exported from `@promptbook/execute-javascript`
2142
+ */
2143
+ function extractVariablesFromJavascript(script) {
2144
+ const variables = new Set();
2145
+ const originalScript = script;
2146
+ script = `(()=>{${script}})()`;
2147
+ try {
2148
+ for (let i = 0; i < LOOP_LIMIT; i++)
2149
+ try {
2150
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
2151
+ }
2152
+ catch (error) {
2153
+ if (!(error instanceof ReferenceError)) {
2154
+ throw error;
2155
+ }
2156
+ /*
2157
+ Note: Parsing the error
2158
+ 🌟 Most devices:
2159
+ [PipelineUrlError: thing is not defined]
2160
+
2161
+ 🍏 iPhone`s Safari:
2162
+ [PipelineUrlError: Can't find variable: thing]
2163
+ */
2164
+ let variableName = undefined;
2165
+ if (error.message.startsWith(`Can't`)) {
2166
+ // 🍏 Case
2167
+ variableName = error.message.split(' ').pop();
2168
+ }
2169
+ else {
2170
+ // 🌟 Case
2171
+ variableName = error.message.split(' ').shift();
2172
+ }
2173
+ if (variableName === undefined) {
2174
+ throw error;
2175
+ }
2176
+ if (script.includes(variableName + '(')) {
2177
+ script = `const ${variableName} = ()=>'';` + script;
2178
+ }
2179
+ else {
2180
+ variables.add(variableName);
2181
+ script = `const ${variableName} = '';` + script;
2182
+ }
2183
+ }
2184
+ }
2185
+ catch (error) {
2186
+ if (!(error instanceof Error)) {
2187
+ throw error;
2188
+ }
2189
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
2190
+ Can not extract variables from the script
2191
+ ${block(error.stack || error.message)}
2192
+
2193
+ Found variables:
2194
+ ${Array.from(variables)
2195
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
2196
+ .join('\n')}
2197
+
2198
+
2199
+ The script:
2200
+
2201
+ \`\`\`javascript
2202
+ ${block(originalScript)}
2203
+ \`\`\`
2204
+ `));
2205
+ }
2206
+ return variables;
2207
+ }
2208
+ /**
2209
+ * TODO: [🔣] Support for multiple languages - python, java,...
2210
+ */
2211
+
2135
2212
  /**
2136
2213
  * Parses the task and returns the set of all used parameters
2137
2214
  *
@@ -2141,24 +2218,26 @@
2141
2218
  * @public exported from `@promptbook/core` <- Note: [👖] This utility is so tightly interconnected with the Promptbook that it is not exported as util but in core
2142
2219
  */
2143
2220
  function extractParameterNamesFromTask(task) {
2144
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
2221
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
2145
2222
  const parameterNames = new Set();
2223
+ let contentParameters;
2224
+ if (taskType !== 'SCRIPT_TASK') {
2225
+ contentParameters = extractParameterNames(content);
2226
+ }
2227
+ else {
2228
+ // TODO: What if script is not javascript?
2229
+ // const { contentLanguage } = task;
2230
+ // if (contentLanguage !== 'javascript') {
2231
+ contentParameters = extractVariablesFromJavascript(content);
2232
+ }
2146
2233
  for (const parameterName of [
2147
2234
  ...extractParameterNames(title),
2148
2235
  ...extractParameterNames(description || ''),
2149
- ...extractParameterNames(content),
2236
+ ...contentParameters,
2150
2237
  ...extractParameterNames(preparedContent || ''),
2151
2238
  ]) {
2152
2239
  parameterNames.add(parameterName);
2153
2240
  }
2154
- /*/
2155
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
2156
- if (taskType === 'SCRIPT_TASK') {
2157
- for (const parameterName of extractVariablesFromScript(content)) {
2158
- parameterNames.add(parameterName);
2159
- }
2160
- }
2161
- /**/
2162
2241
  for (const jokerName of jokerParameterNames || []) {
2163
2242
  parameterNames.add(jokerName);
2164
2243
  }
@@ -9949,7 +10028,6 @@
9949
10028
  `const ${functionName} = customFunctions.${functionName};`)
9950
10029
  .join('\n');
9951
10030
  script = templateParameters(script, parameters);
9952
- // <- Note: [🙊]
9953
10031
  const statementToEvaluate = spaceTrim__default["default"]((block) => `
9954
10032
 
9955
10033
  // Build-in functions: