@promptbook/node 0.86.22 → 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 };
@@ -2,7 +2,6 @@ import { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION } from '../version';
2
2
  import { VALUE_STRINGS } from '../config';
3
3
  import { SMALL_NUMBER } from '../config';
4
4
  import { renderPromptbookMermaid } from '../conversion/prettify/renderPipelineMermaidOptions';
5
- import { extractVariablesFromScript } from '../conversion/utils/extractVariablesFromScript';
6
5
  import { deserializeError } from '../errors/utils/deserializeError';
7
6
  import { serializeError } from '../errors/utils/serializeError';
8
7
  import { forEachAsync } from '../execution/utils/forEachAsync';
@@ -84,7 +83,6 @@ export { BOOK_LANGUAGE_VERSION, PROMPTBOOK_ENGINE_VERSION };
84
83
  export { VALUE_STRINGS };
85
84
  export { SMALL_NUMBER };
86
85
  export { renderPromptbookMermaid };
87
- export { extractVariablesFromScript };
88
86
  export { deserializeError };
89
87
  export { serializeError };
90
88
  export { forEachAsync };
@@ -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.22",
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.22"
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.22';
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
@@ -2133,119 +2133,60 @@
2133
2133
  }
2134
2134
 
2135
2135
  /**
2136
- * Extract all used variable names from ginen JavaScript/TypeScript script
2136
+ * Parses the given script and returns the list of all used variables that are not defined in the script
2137
2137
  *
2138
- * @param script JavaScript/TypeScript script
2139
- * @returns Set of variable names
2138
+ * @param script from which to extract the variables
2139
+ * @returns the list of variable names
2140
2140
  * @throws {ParseError} if the script is invalid
2141
- * @public exported from `@promptbook/utils` <- Note: [👖] This is usable elsewhere than in Promptbook, so keeping in utils
2141
+ * @public exported from `@promptbook/execute-javascript`
2142
2142
  */
2143
- function extractVariablesFromScript(script) {
2144
- if (script.trim() === '') {
2145
- return new Set();
2146
- }
2143
+ function extractVariablesFromJavascript(script) {
2147
2144
  const variables = new Set();
2148
- // JS keywords and builtins to exclude
2149
- const exclude = new Set([
2150
- // Keywords
2151
- 'break',
2152
- 'case',
2153
- 'catch',
2154
- 'class',
2155
- 'const',
2156
- 'continue',
2157
- 'debugger',
2158
- 'default',
2159
- 'delete',
2160
- 'do',
2161
- 'else',
2162
- 'export',
2163
- 'extends',
2164
- 'false',
2165
- 'finally',
2166
- 'for',
2167
- 'function',
2168
- 'if',
2169
- 'import',
2170
- 'in',
2171
- 'instanceof',
2172
- 'let',
2173
- 'new',
2174
- 'null',
2175
- 'return',
2176
- 'super',
2177
- 'switch',
2178
- 'this',
2179
- 'throw',
2180
- 'true',
2181
- 'try',
2182
- 'typeof',
2183
- 'var',
2184
- 'void',
2185
- 'while',
2186
- 'with',
2187
- 'yield',
2188
- // Common globals
2189
- 'console',
2190
- 'JSON',
2191
- 'Error',
2192
- // Typescript types
2193
- 'string',
2194
- 'number',
2195
- 'boolean',
2196
- 'object',
2197
- 'symbol',
2198
- // Common methods on built-in objects
2199
- 'test',
2200
- 'match',
2201
- 'exec',
2202
- 'replace',
2203
- 'search',
2204
- 'split',
2205
- ]);
2145
+ const originalScript = script;
2146
+ script = `(()=>{${script}})()`;
2206
2147
  try {
2207
- // Note: Extract variables from template literals like ${variable}
2208
- const templateRegex = /\$\{([a-zA-Z_$][a-zA-Z0-9_$]*)\}/g;
2209
- let match;
2210
- while ((match = templateRegex.exec(script)) !== null) {
2211
- const varName = match[1];
2212
- if (!exclude.has(varName)) {
2213
- variables.add(varName);
2148
+ for (let i = 0; i < LOOP_LIMIT; i++)
2149
+ try {
2150
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
2214
2151
  }
2215
- }
2216
- // Note: Process the script to handle normal variable usage
2217
- const processedScript = script
2218
- .replace(/'(?:\\.|[^'\\])*'/g, "''") // <- Note: Remove string literals
2219
- .replace(/"(?:\\.|[^"\\])*"/g, '""')
2220
- .replace(/`(?:\\.|[^`\\])*`/g, '``')
2221
- .replace(/\/(?:\\.|[^/\\])*\/[gimsuy]*/g, '{}'); // <- Note: Remove regex literals
2222
- // Note: Find identifiers in function arguments
2223
- const funcArgRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\s*\(/g;
2224
- const funcNames = new Set();
2225
- while ((match = funcArgRegex.exec(processedScript)) !== null) {
2226
- funcNames.add(match[1]);
2227
- }
2228
- // Find variable declarations to exclude them
2229
- const declaredVars = new Set();
2230
- const declRegex = /\b(const|let|var)\s+([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
2231
- while ((match = declRegex.exec(processedScript)) !== null) {
2232
- declaredVars.add(match[2]);
2233
- }
2234
- // Note: Find identifiers in the script
2235
- const identifierRegex = /\b([a-zA-Z_$][a-zA-Z0-9_$]*)\b/g;
2236
- while ((match = identifierRegex.exec(processedScript)) !== null) {
2237
- const name = match[1];
2238
- // Add if not excluded, not a function name, and not a declared variable
2239
- if (!exclude.has(name) && !funcNames.has(name) && !declaredVars.has(name)) {
2240
- variables.add(name);
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
+ }
2241
2183
  }
2242
- }
2243
2184
  }
2244
2185
  catch (error) {
2245
2186
  if (!(error instanceof Error)) {
2246
2187
  throw error;
2247
2188
  }
2248
- throw new ParseError(spaceTrim__default["default"]((block) => `
2189
+ throw new ParseError(spaceTrim.spaceTrim((block) => `
2249
2190
  Can not extract variables from the script
2250
2191
  ${block(error.stack || error.message)}
2251
2192
 
@@ -2258,7 +2199,7 @@
2258
2199
  The script:
2259
2200
 
2260
2201
  \`\`\`javascript
2261
- ${block(script)}
2202
+ ${block(originalScript)}
2262
2203
  \`\`\`
2263
2204
  `));
2264
2205
  }
@@ -2279,19 +2220,24 @@
2279
2220
  function extractParameterNamesFromTask(task) {
2280
2221
  const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
2281
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
+ }
2282
2233
  for (const parameterName of [
2283
2234
  ...extractParameterNames(title),
2284
2235
  ...extractParameterNames(description || ''),
2285
- ...extractParameterNames(content),
2236
+ ...contentParameters,
2286
2237
  ...extractParameterNames(preparedContent || ''),
2287
2238
  ]) {
2288
2239
  parameterNames.add(parameterName);
2289
2240
  }
2290
- if (taskType === 'SCRIPT_TASK') {
2291
- for (const parameterName of extractVariablesFromScript(content)) {
2292
- parameterNames.add(parameterName);
2293
- }
2294
- }
2295
2241
  for (const jokerName of jokerParameterNames || []) {
2296
2242
  parameterNames.add(jokerName);
2297
2243
  }
@@ -10081,6 +10027,7 @@
10081
10027
  // Note: Custom functions are exposed to the current scope as variables
10082
10028
  `const ${functionName} = customFunctions.${functionName};`)
10083
10029
  .join('\n');
10030
+ script = templateParameters(script, parameters);
10084
10031
  const statementToEvaluate = spaceTrim__default["default"]((block) => `
10085
10032
 
10086
10033
  // Build-in functions: