Make ExecuteCodeTaskHandler use async functions by default

facilitating calls to async functions (also triggered by the LLM)
This commit is contained in:
Dominik Jain 2025-10-07 20:27:59 +02:00
parent 3f7153c7e4
commit 094e436d80

View File

@ -175,7 +175,7 @@ export class ExecuteCodeTaskHandler extends TaskHandler<ExecuteCodeTaskParams> {
constructor() {
super();
// initialize context, making penpot object available with custom console
// initialize context, making penpot, penpotUtils, storage and the custom console available
this.context = {
penpot: penpot,
storage: {},
@ -195,16 +195,11 @@ export class ExecuteCodeTaskHandler extends TaskHandler<ExecuteCodeTaskParams> {
const context = this.context;
const code = task.params.code;
let result: any = (function (ctx) {
return Function(...Object.keys(ctx), code)(...Object.values(ctx));
let result: any = await (async (ctx) => {
const fn = new Function(...Object.keys(ctx), `return (async () => { ${code} })();`);
return fn(...Object.values(ctx));
})(context);
// if the result is a Promise, await it
if (result instanceof Promise) {
console.log("Code execution returned a Promise, awaiting result...");
result = await result;
}
console.log("Code execution result:", result);
// return result and captured log