@promptbook/remote-server 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.
- package/esm/index.es.js +55 -109
- package/esm/index.es.js.map +1 -1
- package/esm/typings/src/_packages/execute-javascript.index.d.ts +2 -0
- package/esm/typings/src/_packages/utils.index.d.ts +0 -2
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromJavascript.d.ts +14 -0
- package/esm/typings/src/scripting/javascript/utils/extractVariablesFromScript.test.d.ts +1 -0
- package/package.json +2 -2
- package/umd/index.umd.js +55 -109
- package/umd/index.umd.js.map +1 -1
- package/esm/typings/src/conversion/utils/extractVariablesFromScript.d.ts +0 -14
- /package/esm/typings/src/{conversion/utils/extractVariablesFromScript.test.d.ts → scripting/javascript/utils/extractVariablesFromJavascript.test.d.ts} +0 -0
package/esm/index.es.js
CHANGED
|
@@ -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.86.
|
|
34
|
+
const PROMPTBOOK_ENGINE_VERSION = '0.86.31';
|
|
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
|
|
@@ -3967,119 +3967,60 @@ function valueToString(value) {
|
|
|
3967
3967
|
}
|
|
3968
3968
|
|
|
3969
3969
|
/**
|
|
3970
|
-
*
|
|
3970
|
+
* Parses the given script and returns the list of all used variables that are not defined in the script
|
|
3971
3971
|
*
|
|
3972
|
-
* @param script
|
|
3973
|
-
* @returns
|
|
3972
|
+
* @param script from which to extract the variables
|
|
3973
|
+
* @returns the list of variable names
|
|
3974
3974
|
* @throws {ParseError} if the script is invalid
|
|
3975
|
-
* @public exported from `@promptbook/
|
|
3975
|
+
* @public exported from `@promptbook/execute-javascript`
|
|
3976
3976
|
*/
|
|
3977
|
-
function
|
|
3978
|
-
if (script.trim() === '') {
|
|
3979
|
-
return new Set();
|
|
3980
|
-
}
|
|
3977
|
+
function extractVariablesFromJavascript(script) {
|
|
3981
3978
|
const variables = new Set();
|
|
3982
|
-
|
|
3983
|
-
|
|
3984
|
-
// Keywords
|
|
3985
|
-
'break',
|
|
3986
|
-
'case',
|
|
3987
|
-
'catch',
|
|
3988
|
-
'class',
|
|
3989
|
-
'const',
|
|
3990
|
-
'continue',
|
|
3991
|
-
'debugger',
|
|
3992
|
-
'default',
|
|
3993
|
-
'delete',
|
|
3994
|
-
'do',
|
|
3995
|
-
'else',
|
|
3996
|
-
'export',
|
|
3997
|
-
'extends',
|
|
3998
|
-
'false',
|
|
3999
|
-
'finally',
|
|
4000
|
-
'for',
|
|
4001
|
-
'function',
|
|
4002
|
-
'if',
|
|
4003
|
-
'import',
|
|
4004
|
-
'in',
|
|
4005
|
-
'instanceof',
|
|
4006
|
-
'let',
|
|
4007
|
-
'new',
|
|
4008
|
-
'null',
|
|
4009
|
-
'return',
|
|
4010
|
-
'super',
|
|
4011
|
-
'switch',
|
|
4012
|
-
'this',
|
|
4013
|
-
'throw',
|
|
4014
|
-
'true',
|
|
4015
|
-
'try',
|
|
4016
|
-
'typeof',
|
|
4017
|
-
'var',
|
|
4018
|
-
'void',
|
|
4019
|
-
'while',
|
|
4020
|
-
'with',
|
|
4021
|
-
'yield',
|
|
4022
|
-
// Common globals
|
|
4023
|
-
'console',
|
|
4024
|
-
'JSON',
|
|
4025
|
-
'Error',
|
|
4026
|
-
// Typescript types
|
|
4027
|
-
'string',
|
|
4028
|
-
'number',
|
|
4029
|
-
'boolean',
|
|
4030
|
-
'object',
|
|
4031
|
-
'symbol',
|
|
4032
|
-
// Common methods on built-in objects
|
|
4033
|
-
'test',
|
|
4034
|
-
'match',
|
|
4035
|
-
'exec',
|
|
4036
|
-
'replace',
|
|
4037
|
-
'search',
|
|
4038
|
-
'split',
|
|
4039
|
-
]);
|
|
3979
|
+
const originalScript = script;
|
|
3980
|
+
script = `(()=>{${script}})()`;
|
|
4040
3981
|
try {
|
|
4041
|
-
|
|
4042
|
-
|
|
4043
|
-
|
|
4044
|
-
while ((match = templateRegex.exec(script)) !== null) {
|
|
4045
|
-
const varName = match[1];
|
|
4046
|
-
if (!exclude.has(varName)) {
|
|
4047
|
-
variables.add(varName);
|
|
3982
|
+
for (let i = 0; i < LOOP_LIMIT; i++)
|
|
3983
|
+
try {
|
|
3984
|
+
eval(script); // <- TODO: Use `JavascriptExecutionTools.execute` here
|
|
4048
3985
|
}
|
|
4049
|
-
|
|
4050
|
-
|
|
4051
|
-
|
|
4052
|
-
|
|
4053
|
-
|
|
4054
|
-
|
|
4055
|
-
|
|
4056
|
-
|
|
4057
|
-
|
|
4058
|
-
|
|
4059
|
-
|
|
4060
|
-
|
|
4061
|
-
|
|
4062
|
-
|
|
4063
|
-
|
|
4064
|
-
|
|
4065
|
-
|
|
4066
|
-
|
|
4067
|
-
|
|
4068
|
-
|
|
4069
|
-
|
|
4070
|
-
|
|
4071
|
-
|
|
4072
|
-
|
|
4073
|
-
|
|
4074
|
-
|
|
3986
|
+
catch (error) {
|
|
3987
|
+
if (!(error instanceof ReferenceError)) {
|
|
3988
|
+
throw error;
|
|
3989
|
+
}
|
|
3990
|
+
/*
|
|
3991
|
+
Note: Parsing the error
|
|
3992
|
+
🌟 Most devices:
|
|
3993
|
+
[PipelineUrlError: thing is not defined]
|
|
3994
|
+
|
|
3995
|
+
🍏 iPhone`s Safari:
|
|
3996
|
+
[PipelineUrlError: Can't find variable: thing]
|
|
3997
|
+
*/
|
|
3998
|
+
let variableName = undefined;
|
|
3999
|
+
if (error.message.startsWith(`Can't`)) {
|
|
4000
|
+
// 🍏 Case
|
|
4001
|
+
variableName = error.message.split(' ').pop();
|
|
4002
|
+
}
|
|
4003
|
+
else {
|
|
4004
|
+
// 🌟 Case
|
|
4005
|
+
variableName = error.message.split(' ').shift();
|
|
4006
|
+
}
|
|
4007
|
+
if (variableName === undefined) {
|
|
4008
|
+
throw error;
|
|
4009
|
+
}
|
|
4010
|
+
if (script.includes(variableName + '(')) {
|
|
4011
|
+
script = `const ${variableName} = ()=>'';` + script;
|
|
4012
|
+
}
|
|
4013
|
+
else {
|
|
4014
|
+
variables.add(variableName);
|
|
4015
|
+
script = `const ${variableName} = '';` + script;
|
|
4016
|
+
}
|
|
4075
4017
|
}
|
|
4076
|
-
}
|
|
4077
4018
|
}
|
|
4078
4019
|
catch (error) {
|
|
4079
4020
|
if (!(error instanceof Error)) {
|
|
4080
4021
|
throw error;
|
|
4081
4022
|
}
|
|
4082
|
-
throw new ParseError(spaceTrim
|
|
4023
|
+
throw new ParseError(spaceTrim((block) => `
|
|
4083
4024
|
Can not extract variables from the script
|
|
4084
4025
|
${block(error.stack || error.message)}
|
|
4085
4026
|
|
|
@@ -4092,7 +4033,7 @@ function extractVariablesFromScript(script) {
|
|
|
4092
4033
|
The script:
|
|
4093
4034
|
|
|
4094
4035
|
\`\`\`javascript
|
|
4095
|
-
${block(
|
|
4036
|
+
${block(originalScript)}
|
|
4096
4037
|
\`\`\`
|
|
4097
4038
|
`));
|
|
4098
4039
|
}
|
|
@@ -4113,19 +4054,24 @@ function extractVariablesFromScript(script) {
|
|
|
4113
4054
|
function extractParameterNamesFromTask(task) {
|
|
4114
4055
|
const { title, description, taskType, content, preparedContent, jokerParameterNames, foreach } = task;
|
|
4115
4056
|
const parameterNames = new Set();
|
|
4057
|
+
let contentParameters;
|
|
4058
|
+
if (taskType !== 'SCRIPT_TASK') {
|
|
4059
|
+
contentParameters = extractParameterNames(content);
|
|
4060
|
+
}
|
|
4061
|
+
else {
|
|
4062
|
+
// TODO: What if script is not javascript?
|
|
4063
|
+
// const { contentLanguage } = task;
|
|
4064
|
+
// if (contentLanguage !== 'javascript') {
|
|
4065
|
+
contentParameters = extractVariablesFromJavascript(content);
|
|
4066
|
+
}
|
|
4116
4067
|
for (const parameterName of [
|
|
4117
4068
|
...extractParameterNames(title),
|
|
4118
4069
|
...extractParameterNames(description || ''),
|
|
4119
|
-
...
|
|
4070
|
+
...contentParameters,
|
|
4120
4071
|
...extractParameterNames(preparedContent || ''),
|
|
4121
4072
|
]) {
|
|
4122
4073
|
parameterNames.add(parameterName);
|
|
4123
4074
|
}
|
|
4124
|
-
if (taskType === 'SCRIPT_TASK') {
|
|
4125
|
-
for (const parameterName of extractVariablesFromScript(content)) {
|
|
4126
|
-
parameterNames.add(parameterName);
|
|
4127
|
-
}
|
|
4128
|
-
}
|
|
4129
4075
|
for (const jokerName of jokerParameterNames || []) {
|
|
4130
4076
|
parameterNames.add(jokerName);
|
|
4131
4077
|
}
|