From 70c37934af27d97b57ff5a2711953100f6ff2502 Mon Sep 17 00:00:00 2001 From: Alonso Torres Date: Mon, 23 Nov 2026 15:58:41 +0100 Subject: [PATCH] :sparkles: Expose the file features and active renderer to plugins (#13169) Plugins and the MCP could not tell whether the new render engine (WebGL) or the SVG engine draws the open file. Add file.features with the sorted features of the open file: its stored features plus the features active in the session. render-wasm/v1 comes from the session alone, so it is present only when WebGL draws the file, whether the renderer comes from the wasm query param, the profile setting or the workspace menu. The backend never stores that feature on the file, so the stored features alone cannot answer it. For any other file, such as a library or a File kept after opening another file, features is null. Add penpotUtils.isWasmRenderer() to the MCP plugin and list it in the MCP instructions. It throws a clear error when the Penpot version does not expose the file features. Regenerate the MCP API docs, which also picks up the missing penpot.location entry, and the plugin API test suite surface. Add plugin API test suite tests for the open file and for a file that is not open. Note in the plugins memory that every new or changed Plugin API member gets a suite test whenever the suite can reach the case. Closes #13167 AI-assisted-by: claude-opus-5-5 --- .../frontend/plugin-api-to-cljs-binding.md | 1 + .serena/memories/plugins/core.md | 1 + frontend/src/app/plugins/file.cljs | 20 +++- .../frontend_tests/plugins/file_test.cljs | 27 ++++++ mcp/packages/plugin/src/PenpotUtils.test.ts | 20 ++++ mcp/packages/plugin/src/PenpotUtils.ts | 19 ++++ mcp/packages/server/data/api_types.yml | 94 +++++++++++++++++++ .../server/data/initial_instructions.md | 4 + plugins/CHANGELOG.md | 1 + .../src/generated/api-surface.json | 7 ++ .../src/tests/file.test.ts | 14 +++ .../src/tests/management.test.ts | 13 +++ plugins/libs/plugin-types/index.d.ts | 17 ++++ 13 files changed, 237 insertions(+), 1 deletion(-) diff --git a/.serena/memories/frontend/plugin-api-to-cljs-binding.md b/.serena/memories/frontend/plugin-api-to-cljs-binding.md index a55d857bbd..cc30a92b45 100644 --- a/.serena/memories/frontend/plugin-api-to-cljs-binding.md +++ b/.serena/memories/frontend/plugin-api-to-cljs-binding.md @@ -5,6 +5,7 @@ - The Plugin API is a public facade over internal frontend/common data. Do not expect Plugin API property names, value shapes, or behavior boundaries to match internal CLJS attrs or helper APIs; inspect the relevant proxy and internal code path before using Plugin API observations in production internals or tests. - `plugins/libs/plugin-types/index.d.ts` contains TypeScript declarations only. Runtime objects are CLJS proxies built under `frontend/src/app/plugins/*.cljs` with `obj/reify`. - `shape.cljs` builds shape proxies with hidden ids and per-property CLJS implementations. `library.cljs` builds library proxies such as `LibraryComponentProxy`. +- A new or changed proxy member needs its type in `plugins/libs/plugin-types/index.d.ts`, a `plugins/CHANGELOG.md` entry and a Plugin API test suite test: see `mem:plugins/core`. - `shape.cljs`, `library.cljs`, and related namespaces break circular dependencies with mutable nil vars patched from `app.plugins` at load time. If a proxy constructor appears nil, check the patching path in `frontend/src/app/plugins.cljs`. ## Key Domain Namespaces diff --git a/.serena/memories/plugins/core.md b/.serena/memories/plugins/core.md index a793ba866b..01c0b52d29 100644 --- a/.serena/memories/plugins/core.md +++ b/.serena/memories/plugins/core.md @@ -18,6 +18,7 @@ - From `plugins/`: install `pnpm -r install`; runtime dev server `pnpm run start` or `pnpm run start:app:runtime`; sample plugin `pnpm run start:plugin:`; build runtime `pnpm run build:runtime`; build plugins `pnpm run build:plugins`; lint `pnpm run lint`; format `pnpm run format:check` / `pnpm run format`; tests `pnpm run test`; e2e `pnpm run test:e2e`. - If a change affects public Plugin API types or runtime, update `plugins/CHANGELOG.md`. Prefix type/signature entries with `**plugin-types:**`; runtime behavior entries with `**plugin-runtime:**`. +- Every new or changed Plugin API member gets a test in `apps/plugin-api-test-suite/src/tests/` in the same change, whenever the suite can reach the case (skip only what its README lists as not reachable headless, such as needing a second user or closing the plugin). Cover each new behavior, including null and error cases. Read `apps/plugin-api-test-suite/README.md` before writing one: call the API through `ctx.penpot`/`ctx.penpotMgmt`, tag backend-dependent tests `skipIfMocked`, and rerun `gen:api` (then prettier on `src/generated/api-surface.json`, since the generator writes it unformatted) after changing `libs/plugin-types/index.d.ts`. - Cross-cutting testing principles and anti-patterns: `mem:testing`. - JS Plugin API behavior inside Penpot app: `mem:frontend/plugin-api-to-cljs-binding`; TS declarations are not runtime code; many API objects are CLJS proxies in `frontend/src/app/plugins/*.cljs`. diff --git a/frontend/src/app/plugins/file.cljs b/frontend/src/app/plugins/file.cljs index 28c4f9a0a6..d80f55f1bc 100644 --- a/frontend/src/app/plugins/file.cljs +++ b/frontend/src/app/plugins/file.cljs @@ -13,6 +13,7 @@ [app.common.uuid :as uuid] [app.config :as cf] [app.main.data.exports.files :as exports.files] + [app.main.data.helpers :as dsh] [app.main.data.plugins :as dp] [app.main.data.workspace :as dw] [app.main.data.workspace.versions :as dwv] @@ -28,7 +29,8 @@ [app.plugins.utils :as u] [app.util.http :as http] [app.util.object :as obj] - [beicon.v2.core :as rx])) + [beicon.v2.core :as rx] + [clojure.set :as set])) (defn file-version-proxy? [proxy] @@ -129,6 +131,19 @@ :deleted-at nil))))) reject)))))))))) +(defn get-file-features + "Sorted features of the open file `file-id`: its stored features plus + the features active in the session. `render-wasm/v1` comes from the + session alone, so it is present only when the WebGL renderer draws the + file. Returns nil when `file-id` is not the open file." + [state file-id] + (when (= file-id (:current-file-id state)) + (let [file (dsh/lookup-file state file-id)] + (-> (set (:features file)) + (disj "render-wasm/v1") + (set/union (:features state)) + (sort))))) + (defn file-proxy? [p] (obj/type-of? p "FileProxy")) @@ -148,6 +163,9 @@ :revn {:get #(-> (u/locate-file id) :revn)} + :features + {:get #(some->> (get-file-features @st/state id) (apply array))} + :pages {:this true :get #(.getPages ^js %)} diff --git a/frontend/test/frontend_tests/plugins/file_test.cljs b/frontend/test/frontend_tests/plugins/file_test.cljs index ef8e6ae860..5a5dab144b 100644 --- a/frontend/test/frontend_tests/plugins/file_test.cljs +++ b/frontend/test/frontend_tests/plugins/file_test.cljs @@ -131,3 +131,30 @@ (.-label pinned))) (t/is (false? (.-isAutosave pinned))) (t/is (identical? created-at (.-createdAt pinned)))))))) + +(t/deftest open-file-features-add-the-session-features + (let [file-id (uuid/next) + state {:current-file-id file-id + :features #{"render-wasm/v1" "components/v2"} + :files {file-id {:id file-id + :features #{"variants/v1" "components/v2"}}}}] + (t/is (= ["components/v2" "render-wasm/v1" "variants/v1"] + (file/get-file-features state file-id))))) + +(t/deftest open-file-features-take-the-renderer-from-the-session + (let [file-id (uuid/next) + state {:current-file-id file-id + :features #{"components/v2"} + :files {file-id {:id file-id + :features #{"render-wasm/v1" "components/v2"}}}}] + (t/is (= ["components/v2"] + (file/get-file-features state file-id))))) + +(t/deftest other-file-features-are-nil + (let [file-id (uuid/next) + other-id (uuid/next) + state {:current-file-id file-id + :features #{"render-wasm/v1"} + :files {other-id {:id other-id + :features #{"variants/v1" "components/v2"}}}}] + (t/is (nil? (file/get-file-features state other-id))))) diff --git a/mcp/packages/plugin/src/PenpotUtils.test.ts b/mcp/packages/plugin/src/PenpotUtils.test.ts index 1ed2d51ee0..363362f70a 100644 --- a/mcp/packages/plugin/src/PenpotUtils.test.ts +++ b/mcp/packages/plugin/src/PenpotUtils.test.ts @@ -161,3 +161,23 @@ test("findShapes — excludes the root shape itself from the results", () => { assert.ok(!result.includes(root as any)); }); + +test("isWasmRenderer — true when the open file has render-wasm/v1", () => { + (globalThis as any).penpot = { currentFile: { features: ["components/v2", "render-wasm/v1"] } }; + assert.equal(PenpotUtils.isWasmRenderer(), true); +}); + +test("isWasmRenderer — false when the open file lacks render-wasm/v1", () => { + (globalThis as any).penpot = { currentFile: { features: ["components/v2"] } }; + assert.equal(PenpotUtils.isWasmRenderer(), false); +}); + +test("isWasmRenderer — throws when no file is open", () => { + (globalThis as any).penpot = { currentFile: null }; + assert.throws(() => PenpotUtils.isWasmRenderer(), /No file is open/); +}); + +test("isWasmRenderer — throws when the file features are not exposed", () => { + (globalThis as any).penpot = { currentFile: {} }; + assert.throws(() => PenpotUtils.isWasmRenderer(), /does not expose the file features/); +}); diff --git a/mcp/packages/plugin/src/PenpotUtils.ts b/mcp/packages/plugin/src/PenpotUtils.ts index 762e75c3dd..98279c65bd 100644 --- a/mcp/packages/plugin/src/PenpotUtils.ts +++ b/mcp/packages/plugin/src/PenpotUtils.ts @@ -186,6 +186,25 @@ export class PenpotUtils { return this.findPage((page) => page.name.toLowerCase() === name.toLowerCase()); } + /** + * Checks whether the new render engine (WebGL, `render-wasm/v1`) draws the open file. + * When it returns false, the file renders with the SVG engine. + * + * @returns true iff the open file has the `render-wasm/v1` feature active + * @throws Error if no file is open or the Penpot version does not expose the file features + */ + public static isWasmRenderer(): boolean { + const file = penpot.currentFile; + if (!file) { + throw new Error("No file is open"); + } + const features = file.features; + if (!Array.isArray(features)) { + throw new Error("This Penpot version does not expose the file features"); + } + return features.includes("render-wasm/v1"); + } + public static getPageForShape(shape: Shape): Page | null { for (const page of penpot.currentFile!.pages) { if (page.getShapeById(shape.id)) { diff --git a/mcp/packages/server/data/api_types.yml b/mcp/packages/server/data/api_types.yml index d86d6e1d94..3f01f5341c 100644 --- a/mcp/packages/server/data/api_types.yml +++ b/mcp/packages/server/data/api_types.yml @@ -38,6 +38,7 @@ Penpot: currentUser: User; activeUsers: ActiveUser[]; theme: Theme; + location: PenpotLocation; localStorage: LocalStorage; selection: Shape[]; shapesColors(shapes: Shape[]): (Color & ColorShapeInfo)[]; @@ -309,6 +310,20 @@ Penpot: const currentTheme = context.theme; console.log(currentTheme); ``` + location: |- + ``` + readonly location: PenpotLocation + ``` + + The Penpot instance URL and the current address in the app, in the + dashboard as well as in the workspace. Requires `content:read` permission. + + Example + ``` + const { baseUrl, href, params } = penpot.location; + console.log(baseUrl); // "https://design.penpot.app/" + console.log(params['file-id']); + ``` localStorage: |- ``` readonly localStorage: LocalStorage @@ -5917,6 +5932,7 @@ Context: currentUser: User; activeUsers: ActiveUser[]; theme: Theme; + location: PenpotLocation; localStorage: LocalStorage; selection: Shape[]; shapesColors(shapes: Shape[]): (Color & ColorShapeInfo)[]; @@ -6120,6 +6136,20 @@ Context: const currentTheme = context.theme; console.log(currentTheme); ``` + location: |- + ``` + readonly location: PenpotLocation + ``` + + The Penpot instance URL and the current address in the app, in the + dashboard as well as in the workspace. Requires `content:read` permission. + + Example + ``` + const { baseUrl, href, params } = penpot.location; + console.log(baseUrl); // "https://design.penpot.app/" + console.log(params['file-id']); + ``` localStorage: |- ``` readonly localStorage: LocalStorage @@ -8390,6 +8420,7 @@ File: id: string; name: string; revn: number; + features: string[] | null; pages: Page[]; export( exportType: "penpot" | "zip", @@ -8437,6 +8468,24 @@ File: ``` The `revn` will change for every document update + features: |- + ``` + readonly features: string[] | null + ``` + + The features of the open file, sorted by name, e.g. `"components/v2"` + or `"variants/v1"`. It also lists the features active in the session, + such as `"render-wasm/v1"` when the new render engine (WebGL) draws the + file. Each read returns a new array. + + Only `penpot.currentFile` answers it: for any other file, such as a + library, or a `File` kept after the user opens another file, it is + `null`. + + Example + ``` + const webgl = penpot.currentFile.features.includes('render-wasm/v1'); + ``` pages: |- ``` readonly pages: Page[] @@ -14204,6 +14253,51 @@ LibraryTypography: const sharedKeys = shape.getSharedPluginDataKeys('exampleNamespace'); console.log(sharedKeys); ``` +PenpotLocation: + overview: |- + Interface PenpotLocation + ======================== + + The Penpot instance URL and the current address in the app. + Each read of `penpot.location` returns a new snapshot. + + Query params that carry secrets (`share-id`, any param ending in + `token`, `secret` or `password`, and the OAuth params `request`, + `code` and `state`) are left out of `params` and `href`. + + ``` + interface PenpotLocation { + baseUrl: string; + href: string; + params: Record; + } + ``` + + Referenced by: Context, Penpot + members: + Properties: + baseUrl: |- + ``` + readonly baseUrl: string + ``` + + The public URL of the Penpot instance, ending in `/`, + e.g. `"https://design.penpot.app/"`. + href: |- + ``` + readonly href: string + ``` + + The URL of the current screen, e.g. the open file and page: + `"https://design.penpot.app/?screen=workspace&team-id=…&file-id=…&page-id=…"`. + Use it to link to what the user is looking at. + params: |- + ``` + readonly params: Record + ``` + + The query params of the current URL, e.g. `screen`, `team-id`, + `file-id` and `page-id`. A param repeated in the URL keeps its last value. LocalStorage: overview: |- Interface LocalStorage diff --git a/mcp/packages/server/data/initial_instructions.md b/mcp/packages/server/data/initial_instructions.md index 1def2cf07c..c6bed0e0fe 100644 --- a/mcp/packages/server/data/initial_instructions.md +++ b/mcp/packages/server/data/initial_instructions.md @@ -209,6 +209,8 @@ A key object for navigating the currently active Penpot file is the `penpot` obj * `penpot.location` gives the Penpot instance URL (`baseUrl`), the link to what the user is viewing (`href`, e.g. the open file and page) and the query params of that link (`params`, e.g. `params["file-id"]`). Use `href` when you need a link to the open file; never guess the instance domain or build the link by hand. + * `penpot.currentFile.features` lists the features active for the open file (e.g. `"components/v2"`, `"render-wasm/v1"`); + it is null on any other `File` object. Members of `penpot` (e.g. `penpot.currentFile` and `penpot.root`) will be null no Penpot file is currently opened. You can use `penpotMgmt` to navigate to a file if needed (see below). @@ -236,6 +238,8 @@ CRITICAL: The `penpotUtils` object provides essential utilities - USE THESE INST General-purpose utility for analyzing/validating descendants Calls evaluator on each descendant; collects non-null/undefined results Powerful pattern: evaluator can return corrector functions or diagnostic data + * isWasmRenderer(): boolean + Returns true iff the new render engine (WebGL) draws the open file; false means the SVG engine. * Further functions for specific tasks (described in the sections below) General pointers for working with Penpot designs: diff --git a/plugins/CHANGELOG.md b/plugins/CHANGELOG.md index 550c2ee12f..22948e3705 100644 --- a/plugins/CHANGELOG.md +++ b/plugins/CHANGELOG.md @@ -6,6 +6,7 @@ - **plugin-types, plugins-runtime:** `penpotMgmt` returns `Team`, `Project` and `ProjectFile` objects. Assigning `name` renames teams, projects and files, `project.pinned` pins a project and `file.shared` shares a file as a team library; `createProject`, `createFile`, `moveTo`, `duplicate`, `remove` and `restore` live on the objects. Added `penpotMgmt.listTeams`, `penpotMgmt.createTeam`, `penpotMgmt.getFile`, `listDeletedProjects` and `listDeletedFiles`, and the `manage:projects`, `manage:teams` and `manage:delete` permissions. Restoration uses the dashboard's Trash operation; projects need recoverable files and empty projects cannot be restored; the API exposes no permanent deletion method. Team deletion is not exposed. - **plugins-runtime, plugin-types**: Added global plugins (`scope: "global"`) that remain active across dashboard and workspace navigation until closed or logged out, with `penpotMgmt` for project and file discovery, file navigation and workspace events. Global scope adds the `allow:global` permission, which users must accept when installing the plugin. - **plugin-types, plugins-runtime:** Added `penpot.location` with the instance base URL (`baseUrl`), the URL of the current screen (`href`) and its query params (`params`), in the dashboard and the workspace. Params that carry secrets are left out. Requires `content:read`. Closes #13013. +- **plugin-types:** Added `file.features`, the sorted features of the open file, including the features active in the session, such as `render-wasm/v1` when the new render engine (WebGL) draws the file. It is `null` for any other file. Closes #13167. - **plugin-types:** Added the `contentrollback` event and the `ContentRollback` type. The event fires after the backend rejected some changes and the workspace undid them, with the ids of the files, pages and shapes affected. It fires once the undone data is in the workspace, also when the workspace has to reload the file to undo the changes. ### 🩹 Fixes diff --git a/plugins/apps/plugin-api-test-suite/src/generated/api-surface.json b/plugins/apps/plugin-api-test-suite/src/generated/api-surface.json index 18e9d0b9c5..3219f16a7c 100644 --- a/plugins/apps/plugin-api-test-suite/src/generated/api-surface.json +++ b/plugins/apps/plugin-api-test-suite/src/generated/api-surface.json @@ -152,6 +152,7 @@ "Export": ["scale", "skipChildren", "suffix", "type"], "File": [ "export", + "features", "findVersions", "id", "name", @@ -3141,6 +3142,12 @@ "type": null, "array": false }, + "features": { + "decl": "File", + "kind": "get", + "type": null, + "array": true + }, "pages": { "decl": "File", "kind": "get", diff --git a/plugins/apps/plugin-api-test-suite/src/tests/file.test.ts b/plugins/apps/plugin-api-test-suite/src/tests/file.test.ts index ccb2b68657..3d7148ed60 100644 --- a/plugins/apps/plugin-api-test-suite/src/tests/file.test.ts +++ b/plugins/apps/plugin-api-test-suite/src/tests/file.test.ts @@ -22,6 +22,20 @@ describe('File', () => { } }); + test('currentFile lists its features sorted', (ctx) => { + const file = ctx.penpot.currentFile; + expect(file).not.toBeNull(); + if (file) { + const { features } = file; + expect(features).not.toBeNull(); + if (!features) return; + expect(features.every((f) => typeof f === 'string')).toBe(true); + expect(features).toContain('components/v2'); + expect(features).toEqual([...features].sort()); + expect(file.features === features).toBe(false); + } + }); + test('file lists its pages', (ctx) => { const file = ctx.penpot.currentFile; if (file) { diff --git a/plugins/apps/plugin-api-test-suite/src/tests/management.test.ts b/plugins/apps/plugin-api-test-suite/src/tests/management.test.ts index 67e96d83b6..1ba735ee29 100644 --- a/plugins/apps/plugin-api-test-suite/src/tests/management.test.ts +++ b/plugins/apps/plugin-api-test-suite/src/tests/management.test.ts @@ -430,6 +430,19 @@ describe('Management', () => { expect(finished).toContain(other.id); }); + test('features is null on a file that is not open', async (ctx) => { + const home = ctx.penpot.currentFile; + expect(home).not.toBeNull(); + if (!home) return; + expect(home.features).not.toBeNull(); + const other = await temporaryFile(ctx, 'Features'); + + await inFile(ctx, other, () => { + expect(home.features).toBeNull(); + expect(ctx.penpot.currentFile?.features).toContain('components/v2'); + }); + }); + test('off stops workspacechange notifications', async (ctx) => { const other = await temporaryFile(ctx, 'Unsubscribed'); let count = 0; diff --git a/plugins/libs/plugin-types/index.d.ts b/plugins/libs/plugin-types/index.d.ts index 05a2d3faee..d9a8dbb575 100644 --- a/plugins/libs/plugin-types/index.d.ts +++ b/plugins/libs/plugin-types/index.d.ts @@ -1967,6 +1967,23 @@ export interface File extends PluginData { */ readonly revn: number; + /** + * The features of the open file, sorted by name, e.g. `"components/v2"` + * or `"variants/v1"`. It also lists the features active in the session, + * such as `"render-wasm/v1"` when the new render engine (WebGL) draws the + * file. Each read returns a new array. + * + * Only `penpot.currentFile` answers it: for any other file, such as a + * library, or a `File` kept after the user opens another file, it is + * `null`. + * + * @example + * ```js + * const webgl = penpot.currentFile.features.includes('render-wasm/v1'); + * ``` + */ + readonly features: string[] | null; + /** * List all the pages for the current file */