penpot/mcp/packages/plugin/src/task-handlers/ExecuteCodeTaskHandler.ts
Pablo Alba 73e7815817
🐛 Make execute_code wait for save before success (#13027)
* 🐛 Make execute_code wait for save before success

* 🐛 Address review feedback on waitForSave

Reject with an Error carrying a `code`: save-timeout, rolled-back,
save-retrying, or the failure code (save-failed by default). Lower the
default timeout from 90s to 60s, leaving room for the 30s layout wait
and the code under the MCP tool timeout.

When persistence is already in error at the start of the wait, keep the
stored failure code, and tell a failure Penpot keeps retrying apart from
one that needs the file reopened.

Share timeout validation with waitForLayoutUpdate through
app.plugins.utils, and make reject-not-valid reject with an Error, which
also fixes the string rejections of connectLibrary, restore, remove,
pin, export, findVersions and saveVersion.

The MCP execute_code handler now waits for the save only when the
workspace is ready, like the layout wait, so dashboard calls no longer
fail on a save error from an earlier file.

Move the waitForSave tests to frontend-tests.plugins, add tests for the
error codes, the retrying state and a failure during the wait, and add
plugin API suite and MCP handler tests. Update the docs, CHANGELOG and
the generated API files.

AI-assisted-by: claude-opus-5-5
2026-11-17 22:35:37 +01:00

134 lines
5.8 KiB
TypeScript

import { Task, TaskHandler } from "../TaskHandler";
import { ExecuteCodeTaskParams, ExecuteCodeTaskResultData } from "../../../common/src";
import { PenpotUtils } from "../PenpotUtils.ts";
import { ExecuteCodeTaskConsole } from "../ExecuteCodeTaskConsole.ts";
/**
* Task handler for executing JavaScript code in the plugin context.
*
* Maintains a persistent context object that preserves state between code executions
* and captures all console output during execution.
*/
export class ExecuteCodeTaskHandler extends TaskHandler<ExecuteCodeTaskParams> {
readonly taskType = "executeCode";
/**
* Persistent context object that maintains state between code executions.
* Contains the penpot API, storage object, and custom console implementation.
*/
private readonly context: any;
constructor() {
super();
// initialize context, making penpot, penpotUtils, storage and the custom console available
this.context = {
penpot: penpot,
penpotMgmt: typeof penpotMgmt === "undefined" ? null : penpotMgmt,
storage: {},
console: new ExecuteCodeTaskConsole(),
penpotUtils: PenpotUtils,
};
}
async handle(task: Task<ExecuteCodeTaskParams>): Promise<void> {
if (!task.params.code) {
task.sendError("executeCode task requires 'code' parameter");
return;
}
this.context.console.resetLog();
const context = this.context;
const code = task.params.code;
// set the flags naturalChildOrdering and throwValidationErrors to true during code execution.
let originalNaturalChildOrdering: any, originalThrowValidationErrors: any;
if (!penpot.flags || typeof penpot.waitForSave !== "function") {
throw new Error(
"The connected Penpot build is missing Plugin API support that this MCP " +
"plugin needs (for example `penpot.waitForSave`), or the MCP plugin is " +
"too new for that Penpot. Upgrade Penpot (and its plugins-runtime) so " +
"they match this MCP version. " +
"Read the documentation for instructions on which version to use: " +
"https://github.com/penpot/penpot/tree/develop/mcp\n" +
"If you are an LLM, tell the user about this!"
);
}
originalNaturalChildOrdering = penpot.flags.naturalChildOrdering;
penpot.flags.naturalChildOrdering = true;
originalThrowValidationErrors = penpot.flags.throwValidationErrors;
penpot.flags.throwValidationErrors = true;
// Layout and save waits only apply with a ready workspace: on the dashboard (or while a file
// loads) no edit of this call can be pending. Checked at each wait, since the code may navigate.
const management = this.context.penpotMgmt;
const workspaceReady = () => !management || management.workspace.status === "ready";
let result: any;
try {
// wait for layout updates prior to executing the supplied code (if method is available)
try {
// @ts-ignore - TODO Penpot.waitForLayoutUpdate is not yet in the released types
if (workspaceReady() && penpot.waitForLayoutUpdate) {
// @ts-ignore
await penpot.waitForLayoutUpdate();
}
} catch (e) {
console.error("Error waiting for layout update:", e);
}
// execute the code in an async function with the context variables as parameters
result = await (async (ctx) => {
const fn = new Function(...Object.keys(ctx), `return (async () => { ${code} })();`);
return fn(...Object.values(ctx));
})(context);
} finally {
// restore the original value of the flags
penpot.flags.naturalChildOrdering = originalNaturalChildOrdering;
penpot.flags.throwValidationErrors = originalThrowValidationErrors;
}
console.log("Code execution result:", result);
// transform a top-level Uint8Array result into a compact base64 envelope to avoid the
// ~10x JSON expansion that occurs when JSON.stringify serializes typed arrays as objects
// with numeric string keys (see penpot/penpot#9420)
if (result instanceof Uint8Array) {
result = ExecuteCodeTaskHandler.encodeBytesAsBase64Envelope(result);
}
// Wait for the save outside any swallow catch: a failed or rolled-back save must fail the task.
if (workspaceReady()) {
await penpot.waitForSave();
}
// return result and captured log
let resultData: ExecuteCodeTaskResultData<any> = {
result: result,
log: this.context.console.getLog(),
};
task.sendSuccess(resultData);
}
/**
* Base64-encodes the given bytes and wraps the result in a tagged envelope that the
* server side recognizes (see `ImageContent.byteData`).
*
* @param bytes - the raw binary data to encode
* @returns an envelope of the form `{ __type: "base64", data: <base64 string> }`
*/
private static encodeBytesAsBase64Envelope(bytes: Uint8Array): { __type: "base64"; data: string } {
// build the binary string in chunks; calling `String.fromCharCode(...bytes)` directly
// would overflow the call stack for large arrays
const chunkSize = 0x8000;
let binary = "";
for (let i = 0; i < bytes.length; i += chunkSize) {
const chunk = bytes.subarray(i, i + chunkSize);
binary += String.fromCharCode.apply(null, chunk as unknown as number[]);
}
return { __type: "base64", data: btoa(binary) };
}
}