| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | import { abilityDescriptors, getAbilities } from '@agent/abilities/abilities'; |
| 2 | +import { findBlockEl, scopeOf } from '@agent/lib/block-el'; | |
| 2 | 3 | import { buildBlockSchema } from '@agent/lib/block-schema'; |
| 3 | 4 | import { |
| 4 | 5 | classifyBlockEdit, |
| 5 | 6 | IGNORED_BLOCKS, |
| @@ -4,13 +5,16 @@ | ||
| 4 | 5 | classifyBlockEdit, |
| 5 | 6 | IGNORED_BLOCKS, |
| 6 | 7 | } from '@agent/lib/classify-block-edit'; |
| 7 | 8 | import { getClientTools } from '@agent/lib/client-tools'; |
| 9 | +import { isExtendableHeader } from '@agent/lib/extendable-header'; | |
| 8 | 10 | import { INSERTABLE_BLOCK_TYPES } from '@agent/lib/insertable-blocks'; |
| 11 | +import { fetchLinkablePages } from '@agent/lib/linkable-pages'; | |
| 9 | 12 | import { ensureCoreBlocksRegistered } from '@agent/lib/register-blocks'; |
| 10 | 13 | import { |
| 11 | 14 | buildSubtreeManifest, |
| 12 | 15 | buildSubtreeTree, |
| 16 | + ownerAncestorFor, | |
| 13 | 17 | } from '@agent/lib/subtree-manifest'; |
| 14 | 18 | import { activeCanvasStep } from '@agent/state/canvas'; |
| 15 | 19 | import { useChatStore } from '@agent/state/chat'; |
| 16 | 20 | import { useGlobalStore } from '@agent/state/global'; |
| @@ -20,11 +24,19 @@ | ||
| 20 | 24 | import { digest } from '@shared/api/digest'; |
| 21 | 25 | import { reqDataBasics } from '@shared/lib/data'; |
| 22 | 26 | import { getBlockType } from '@wordpress/blocks'; |
| 23 | 27 | |
| 28 | +// page-templates:v1 declares that find-blocks and stage-block reach into parts. | |
| 29 | +// block-duplicate:v1 declares that update-blocks applies the duplicate op. | |
| 30 | +export const API_FEATURES = [ | |
| 31 | + 'qa-tool', | |
| 32 | + 'page-templates:v1', | |
| 33 | + 'block-duplicate:v1', | |
| 34 | +]; | |
| 35 | + | |
| 24 | 36 | const rootFor = (block) => |
| 25 | 37 | block?.id && block?.target |
| 26 | - ? document.querySelector(`[${block.target}="${block.id}"]`) | |
| 38 | + ? findBlockEl(block.id, document, scopeOf(block)) | |
| 27 | 39 | : null; |
| 28 | 40 | |
| 29 | 41 | // If greater than 5 blocks the agent will narrow the scope |
| 30 | 42 | const EAGER_LOAD_MAX = 5; |
| @@ -32,14 +44,21 @@ | ||
| 32 | 44 | // The staged selection's attribute schemas, with the install's palette baked in. |
| 33 | 45 | export const blockSchemasFor = async (block) => { |
| 34 | 46 | const root = rootFor(block); |
| 35 | 47 | const { bucket } = classifyBlockEdit({ block, root }); |
| 36 | - const types = | |
| 37 | - bucket === 'single' | |
| 38 | - ? [block.blockType] | |
| 39 | - : IGNORED_BLOCKS.has(block?.blockType) | |
| 40 | - ? [] | |
| 41 | - : [...new Set(buildSubtreeManifest(root).map(({ type }) => type))]; | |
| 48 | + const owner = ownerAncestorFor(root); | |
| 49 | + const types = [ | |
| 50 | + ...new Set( | |
| 51 | + [ | |
| 52 | + ...(bucket === 'single' | |
| 53 | + ? [block.blockType] | |
| 54 | + : IGNORED_BLOCKS.has(block?.blockType) | |
| 55 | + ? [] | |
| 56 | + : buildSubtreeManifest(root).map(({ type }) => type)), | |
| 57 | + owner?.type, | |
| 58 | + ].filter(Boolean), | |
| 59 | + ), | |
| 60 | + ]; | |
| 42 | 61 | if (!types.length || types.length > EAGER_LOAD_MAX) return []; |
| 43 | 62 | await ensureCoreBlocksRegistered(); |
| 44 | 63 | return types |
| 45 | 64 | .map((type) => ({ type, schema: buildBlockSchema(getBlockType(type)) })) |
| @@ -45,11 +64,28 @@ | ||
| 45 | 64 | .map((type) => ({ type, schema: buildBlockSchema(getBlockType(type)) })) |
| 46 | 65 | .filter(({ schema }) => schema); |
| 47 | 66 | }; |
| 48 | 67 | |
| 68 | +// Without the site's addresses the model links a named page to "#". | |
| 69 | +const withLinkablePages = async (data) => { | |
| 70 | + if (!data.blockSchemas?.some(({ type }) => type === 'core/navigation-link')) { | |
| 71 | + return data; | |
| 72 | + } | |
| 73 | + const pages = await fetchLinkablePages(); | |
| 74 | + return { | |
| 75 | + ...data, | |
| 76 | + linkablePages: pages.map(({ title, url }) => ({ title, url })), | |
| 77 | + }; | |
| 78 | +}; | |
| 79 | + | |
| 49 | 80 | // Only consumed for a multi-block edit; inert for single/combo. |
| 50 | -export const subtreeManifestFor = (block) => | |
| 51 | - buildSubtreeManifest(rootFor(block)); | |
| 81 | +export const subtreeManifestFor = (block) => { | |
| 82 | + const root = rootFor(block); | |
| 83 | + const owner = ownerAncestorFor(root); | |
| 84 | + const manifest = buildSubtreeManifest(root); | |
| 85 | + // buildSubtreeManifest treats the first entry as the selection's baseline. | |
| 86 | + return owner ? [...manifest, owner] : manifest; | |
| 87 | +}; | |
| 52 | 88 | |
| 53 | 89 | // Selection hierarchy for the backend's positional edits. |
| 54 | 90 | export const subtreeTreeFor = (block) => buildSubtreeTree(rootFor(block)); |
| 55 | 91 | |
| @@ -106,8 +142,9 @@ | ||
| 106 | 142 | clientTools: getClientTools(), |
| 107 | 143 | messages: messages.slice(-5), |
| 108 | 144 | hasBlock: Boolean(block), // todo: remove this |
| 109 | 145 | blockDetails: block, |
| 146 | + features: API_FEATURES, | |
| 110 | 147 | ...options, |
| 111 | 148 | extra: extra(), |
| 112 | 149 | }), |
| 113 | 150 | }); |
| @@ -113,12 +150,9 @@ | ||
| 113 | 150 | }); |
| 114 | 151 | |
| 115 | 152 | if (!response.ok) { |
| 116 | 153 | digest({ |
| 117 | - error: { | |
| 118 | - name: response.statusText, | |
| 119 | - messages: response.statusMessage, | |
| 120 | - }, | |
| 154 | + error: response, | |
| 121 | 155 | details: { source: 'agent', caller: 'pick-workflow' }, |
| 122 | 156 | }); |
| 123 | 157 | const error = new Error('Bad response from server'); |
| 124 | 158 | error.response = response; |
| @@ -137,14 +171,14 @@ | ||
| 137 | 171 | const { blockSearch: _spent, ...carried } = workflowData ?? {}; |
| 138 | 172 | // Schemas: pinned up front when the selection is small enough; otherwise |
| 139 | 173 | // empty until get-block-schemas fetches them into the same field. |
| 140 | 174 | const data = isBlockPatching |
| 141 | - ? { | |
| 175 | + ? await withLinkablePages({ | |
| 142 | 176 | ...carried, |
| 143 | 177 | blockSchemas: workflowData?.blockSchemas?.length |
| 144 | 178 | ? workflowData.blockSchemas |
| 145 | 179 | : await blockSchemasFor(block), |
| 146 | - } | |
| 180 | + }) | |
| 147 | 181 | : workflowData; |
| 148 | 182 | const response = await fetch(`${AI_HOST}/api/agent/handle-workflow`, { |
| 149 | 183 | method: 'POST', |
| 150 | 184 | headers: { 'Content-Type': 'application/json' }, |
| @@ -165,10 +199,13 @@ | ||
| 165 | 199 | blockTree: isBlockPatching ? subtreeTreeFor(block) : [], |
| 166 | 200 | // The plugin owns the add catalog so old fielded builds are never |
| 167 | 201 | // offered a type they can't build. |
| 168 | 202 | insertableBlockTypes: isBlockPatching ? INSERTABLE_BLOCK_TYPES : [], |
| 203 | + // The patcher writes dead CSS for these switches and claims success. | |
| 204 | + isExtendableHeader: | |
| 205 | + isBlockPatching && block?.template === 'header' && isExtendableHeader(), | |
| 169 | 206 | retry: options?.retry || false, |
| 170 | - features: ['qa-tool'], | |
| 207 | + features: API_FEATURES, | |
| 171 | 208 | extra: extra(), |
| 172 | 209 | }), |
| 173 | 210 | }); |
| 174 | 211 | |