From 094e436d80c9c9d030353e194a4d2240042f4955 Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Tue, 7 Oct 2025 20:27:59 +0200 Subject: [PATCH] Make ExecuteCodeTaskHandler use async functions by default facilitating calls to async functions (also triggered by the LLM) --- .../src/task-handlers/ExecuteCodeTaskHandler.ts | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/penpot-plugin/src/task-handlers/ExecuteCodeTaskHandler.ts b/penpot-plugin/src/task-handlers/ExecuteCodeTaskHandler.ts index a2bbe03..86fc627 100644 --- a/penpot-plugin/src/task-handlers/ExecuteCodeTaskHandler.ts +++ b/penpot-plugin/src/task-handlers/ExecuteCodeTaskHandler.ts @@ -175,7 +175,7 @@ export class ExecuteCodeTaskHandler extends TaskHandler { 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 { 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