@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.
package/esm/index.es.js CHANGED
@@ -30,7 +30,7 @@ const BOOK_LANGUAGE_VERSION = '1.0.0';
30
30
  * @generated
31
31
  * @see https://github.com/webgptorg/promptbook
32
32
  */
33
- const PROMPTBOOK_ENGINE_VERSION = '0.86.30';
33
+ const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
34
34
  /**
35
35
  * TODO: string_promptbook_version should be constrained to the all versions of Promptbook engine
36
36
  * Note: [💞] Ignore a discrepancy between file name and entity name
@@ -2116,6 +2116,83 @@ function addUsage(...usageItems) {
2116
2116
  }, deepClone(ZERO_USAGE));
2117
2117
  }
2118
2118
 
2119
+ /**
2120
+ * Parses the given script and returns the list of all used variables that are not defined in the script
2121
+ *
2122
+ * @param script from which to extract the variables
2123
+ * @returns the list of variable names
2124
+ * @throws {ParseError} if the script is invalid
2125
+ * @public exported from `@promptbook/execute-javascript`
2126
+ */
2127
+ function extractVariablesFromJavascript(script) {
2128
+ const variables = new Set();
2129
+ const originalScript = script;
2130
+ script = `(()=>{${script}})()`;
2131
+ try {
2132
+ for (let i = 0; i < LOOP_LIMIT; i++)
2133
+ try {
2134
+ eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
2135
+ }
2136
+ catch (error) {
2137
+ if (!(error instanceof ReferenceError)) {
2138
+ throw error;
2139
+ }
2140
+ /*
2141
+ Note: Parsing the error
2142
+ 🌟 Most devices:
2143
+ [PipelineUrlError: thing is not defined]
2144
+
2145
+ 🍏 iPhone`s Safari:
2146
+ [PipelineUrlError: Can't find variable: thing]
2147
+ */
2148
+ let variableName = undefined;
2149
+ if (error.message.startsWith(`Can't`)) {
2150
+ // 🍏 Case
2151
+ variableName = error.message.split(' ').pop();
2152
+ }
2153
+ else {
2154
+ // 🌟 Case
2155
+ variableName = error.message.split(' ').shift();
2156
+ }
2157
+ if (variableName === undefined) {
2158
+ throw error;
2159
+ }
2160
+ if (script.includes(variableName + '(')) {
2161
+ script = `const ${variableName} = ()=>'';` + script;
2162
+ }
2163
+ else {
2164
+ variables.add(variableName);
2165
+ script = `const ${variableName} = '';` + script;
2166
+ }
2167
+ }
2168
+ }
2169
+ catch (error) {
2170
+ if (!(error instanceof Error)) {
2171
+ throw error;
2172
+ }
2173
+ throw new ParseError(spaceTrim$1((block) => `
2174
+ Can not extract variables from the script
2175
+ ${block(error.stack || error.message)}
2176
+
2177
+ Found variables:
2178
+ ${Array.from(variables)
2179
+ .map((variableName, i) => `${i + 1}) ${variableName}`)
2180
+ .join('\n')}
2181
+
2182
+
2183
+ The script:
2184
+
2185
+ \`\`\`javascript
2186
+ ${block(originalScript)}
2187
+ \`\`\`
2188
+ `));
2189
+ }
2190
+ return variables;
2191
+ }
2192
+ /**
2193
+ * TODO: [🔣] Support for multiple languages - python, java,...
2194
+ */
2195
+
2119
2196
  /**
2120
2197
  * Parses the task and returns the set of all used parameters
2121
2198
  *
@@ -2125,24 +2202,26 @@ function addUsage(...usageItems) {
2125
2202
  * @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
2126
2203
  */
2127
2204
  function extractParameterNamesFromTask(task) {
2128
- const { title, description, /* [🙊] taskType,*/ content, preparedContent, jokerParameterNames, foreach } = task;
2205
+ const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
2129
2206
  const parameterNames = new Set();
2207
+ let contentParameters;
2208
+ if (taskType !== 'SCRIPT_TASK') {
2209
+ contentParameters = extractParameterNames(content);
2210
+ }
2211
+ else {
2212
+ // TODO: What if script is not javascript?
2213
+ // const { contentLanguage } = task;
2214
+ // if (contentLanguage !== 'javascript') {
2215
+ contentParameters = extractVariablesFromJavascript(content);
2216
+ }
2130
2217
  for (const parameterName of [
2131
2218
  ...extractParameterNames(title),
2132
2219
  ...extractParameterNames(description || ''),
2133
- ...extractParameterNames(content),
2220
+ ...contentParameters,
2134
2221
  ...extractParameterNames(preparedContent || ''),
2135
2222
  ]) {
2136
2223
  parameterNames.add(parameterName);
2137
2224
  }
2138
- /*/
2139
- // TODO: [🙊] Fix `extractVariablesFromScript` or delete
2140
- if (taskType === 'SCRIPT_TASK') {
2141
- for (const parameterName of extractVariablesFromScript(content)) {
2142
- parameterNames.add(parameterName);
2143
- }
2144
- }
2145
- /**/
2146
2225
  for (const jokerName of jokerParameterNames || []) {
2147
2226
  parameterNames.add(jokerName);
2148
2227
  }
@@ -9933,7 +10012,6 @@ class JavascriptEvalExecutionTools {
9933
10012
  `const ${functionName} = customFunctions.${functionName};`)
9934
10013
  .join('\n');
9935
10014
  script = templateParameters(script, parameters);
9936
- // <- Note: [🙊]
9937
10015
  const statementToEvaluate = spaceTrim((block) => `
9938
10016
 
9939
10017
  // Build-in functions: