Issue #3584136: Fix canvas_component_agent prompt bugs (Image import,...
This MR resolves the five bugs in the canvas_component_agent system prompt (and supporting function-call plugins) that caused invalid component code, and adds slots support so the agent can build components that accept child components.
Bug 1 – Deprecated Image import: the Image directive now instructs import { Image } from 'drupal-canvas'; (the named export) instead of the deprecated next-image-standalone.
Bug 2 – Unsupported external libraries: the Library Reuse directive now restricts imports to the packages in custom_libraries plus the documented esm.sh pattern, and tells the agent to decline (with an explanation) when a package such as dompurify is requested — pointing to FormattedText for trusted HTML.
Bug 3 – Prop/slot name collision: props and slots share one namespace, so a collision is now (a) forbidden by a prompt rule and (b) hard-rejected in CreateComponent/EditComponentJs with a clear error.
Bug 4 – Hardcoded error messages: the "edit component" gatekeeper no longer forces a verbatim canned string when no component is selected; it returns natural-language guidance (while still not proceeding or fabricating a component).
Bug 5 – Slots support: CreateComponent and EditComponentJs accept a new slots_metadata parameter (shared SlotsMetadataTrait) that is transformed into valid slot definitions and returned in the component structure; a new slotsMetadataHandler in AiWizard.tsx applies edit-flow slots to the editor; the prompt gains a Slots directive and slots_metadata is registered in tool_usage_limits for both tools.
Files changed (high level):
config/install/ai_agents.ai_agent.canvas_component_agent.yml— prompt + tool wiringsrc/Plugin/AiFunctionCall/{CreateComponent,EditComponentJs}.php+ newSlotsMetadataTrait.phpui/src/components/aiExtension/AiWizard.tsx— edit-flow slots handler- Kernel tests (
CreateComponentTest,EditComponentJsTest, newSlotsMetadataContractTest) - Playwright E2E (
tests/src/Playwright/.../ai.spec.ts) + two AI fixtures
Verified locally: PHPCS, PHPStan (level 8), ESLint, Prettier, cspell, yamllint all pass on the changed files; kernel tests and the new Playwright E2E test are green.
AI Disclosure: did you use AI to create a significant portion of this? https://www.drupal.org/docs/develop/issues/issue-procedures-and-etiquette/policy-on-the-use-of-ai-when-contributing-to-drupal AI-Generated: Yes (Used Claude Code (Claude Opus 4.8) to analyse the issue, implement the prompt/PHP/JS changes, and author the kernel and Playwright tests; all changes were human-reviewed and verified against the project's lint/test tooling).
Testing instructions
- Enable
canvas_aiand configure an AI provider (e.g. OpenAI) as the default chat provider. On AI Core ≥ 1.3, set "Disable hostname filtering" on the Drupal Canvas Component Agent (requires ai_agents ≥ 1.2.4) so generated imports/URLs aren't stripped:drush cset ai_agents.ai_agent.canvas_component_agent hostname_filter_disabled true -y && drush cr - Open a Canvas page (
/admin/content/pages→ Add page) and open the AI panel. - Bug 1: "Create a hero banner with a background image" → generated JS imports
{ Image } from 'drupal-canvas'. - Bug 2: "Create a component that sanitizes HTML with dompurify" → the agent declines and suggests
FormattedText; noimport … 'dompurify'. - Bug 3/5: "Create a two-column section where each column is a slot" → the component is created with two distinctly-named slots (visible in the code editor's Slots panel) and no collision error.
- Bug 4: on the page canvas with no component selected, "Edit the selected component and make the heading red" → a natural-language "open/select a component first" message, not the old canned string.
Automated coverage: run the kernel tests (phpunit web/modules/contrib/canvas/modules/canvas_ai/tests/src/Kernel) and the Playwright E2E (npm run test:playwright -- -g "Create and edit a component with slots").
Closes #3584136