@convo-lang/convo-lang
Version:
The language of AI
42 lines • 1.45 kB
JavaScript
import { ConvoError } from "../ConvoError.js";
import { createConvoScopeFunction } from "../convo-lib.js";
export const convoScopeFunctionEvalJavascript = createConvoScopeFunction(async (scope, ctx) => {
const js = scope.paramValues?.[0];
if (js === undefined) {
return undefined;
}
if (typeof js !== 'string') {
throw new ConvoError('invalid-args', { statement: scope.s }, 'The first arg must be string of javascript or undefined');
}
const argNames = [];
const args = [];
const addVar = (name, value) => {
if (argNames.includes(name)) {
return;
}
argNames.push(name);
args.push(value);
};
addVar('scope', scope);
addVar('ctx', ctx);
addVar('convo', ctx.convo);
addVar('setVar', (name, value) => {
if (typeof name !== 'string') {
throw new Error('name must be a string');
}
return ctx.setVar(true, value, name);
});
addVar('print', (...args) => ctx.print(...args));
for (const e in ctx.sharedVars) {
addVar(e, ctx.sharedVars[e]);
}
// The line below is used to disable esbuild warnings. I know what I'm doing 👍
const ev = globalThis['eval'];
const fn = ev(`async (${argNames.join(',')})=>{
let result=undefined;
${js}
return result;
}`);
return await fn(...args);
});
//# sourceMappingURL=convoScopeFunctionEvalJavascript.js.map