@perkyzz/emvb 0.4.0 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +23 -3
- package/THIRD_PARTY_NOTICES.md +23 -1
- package/licenses/exif-js.LICENSE +21 -0
- package/licenses/fflate.LICENSE +21 -0
- package/licenses/glfx.js.LICENSE +19 -0
- package/licenses/mini-photo-editor.LICENSE +21 -0
- package/licenses/photocn.LICENSE +21 -0
- package/package.json +5 -3
- package/src/admin/agents-api.ts +39 -0
- package/src/admin/editor/CommandPalette.tsx +43 -2
- package/src/admin/editor/Editor.tsx +33 -0
- package/src/admin/editor/Timeline.tsx +108 -6
- package/src/admin/editor/collection-previews.ts +6 -1
- package/src/admin/editor/editor-chrome.tsx +49 -0
- package/src/admin/editor/editor-commands.ts +13 -0
- package/src/admin/editor/editor-css.ts +7 -0
- package/src/admin/editor/feature-css.ts +22 -3
- package/src/admin/editor/palette.ts +6 -2
- package/src/admin/editor/panels/ColorControl.tsx +24 -12
- package/src/admin/editor/panels/ElementPanel.tsx +1 -1
- package/src/admin/editor/panels/LayersPanel.tsx +13 -3
- package/src/admin/editor/panels/TokenScales.tsx +4 -2
- package/src/admin/editor/panels/VariableSection.tsx +5 -3
- package/src/admin/editor/panels/color-space.ts +12 -1
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +84 -28
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +42 -4
- package/src/admin/editor/panels/settings/DraftTextField.tsx +38 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +24 -7
- package/src/admin/editor/panels/settings/IconPicker.tsx +21 -1
- package/src/admin/editor/panels/settings/ImageSourcesField.tsx +111 -0
- package/src/admin/editor/panels/settings/MediaPicker.tsx +156 -7
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -0
- package/src/admin/editor/panels/settings/RequiredFieldsNotice.tsx +76 -0
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/panels/settings/link-rules.ts +19 -0
- package/src/admin/editor/panels/settings/number-input.ts +11 -0
- package/src/admin/editor/panels/settings/query-style.ts +65 -0
- package/src/admin/editor/panels/variable-values.ts +18 -2
- package/src/admin/editor/remote-change.ts +67 -0
- package/src/admin/editor/store.ts +7 -3
- package/src/admin/image-editor/BlurFocus.tsx +112 -0
- package/src/admin/image-editor/Canvas.tsx +261 -0
- package/src/admin/image-editor/CropOverlay.tsx +390 -0
- package/src/admin/image-editor/CurvesPanel.tsx +394 -0
- package/src/admin/image-editor/ExportDialog.tsx +354 -0
- package/src/admin/image-editor/ImageEditButton.tsx +83 -0
- package/src/admin/image-editor/ImageEditorDialog.tsx +786 -0
- package/src/admin/image-editor/MetadataPanel.tsx +183 -0
- package/src/admin/image-editor/announce.tsx +65 -0
- package/src/admin/image-editor/crop-geometry.ts +161 -0
- package/src/admin/image-editor/curve-math.ts +177 -0
- package/src/admin/image-editor/data-url-fetch.ts +59 -0
- package/src/admin/image-editor/engine.ts +55 -0
- package/src/admin/image-editor/exif.ts +82 -0
- package/src/admin/image-editor/flatten.ts +54 -0
- package/src/admin/image-editor/gpu-fit.ts +81 -0
- package/src/admin/image-editor/image-editor-css.ts +398 -0
- package/src/admin/image-editor/load.ts +109 -0
- package/src/admin/image-editor/panels.tsx +287 -0
- package/src/admin/image-editor/parts.tsx +565 -0
- package/src/admin/image-editor/save.ts +125 -0
- package/src/admin/image-editor/tone-panels.tsx +338 -0
- package/src/admin/image-editor/tools.ts +59 -0
- package/src/admin/image-editor/worker.ts +111 -0
- package/src/admin/index.tsx +3 -1
- package/src/admin/media-api.ts +132 -2
- package/src/admin/pages/AgentsPage.tsx +274 -0
- package/src/admin/ui.ts +33 -0
- package/src/astro/EmVBPage.astro +11 -2
- package/src/astro/floats/runtime.ts +10 -0
- package/src/astro/popups/runtime.ts +15 -0
- package/src/astro/render.ts +3 -0
- package/src/astro/resolve-theme.ts +15 -4
- package/src/astro/tabs/runtime.ts +60 -1
- package/src/astro/visitor.ts +4 -3
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +202 -26
- package/src/core/arrange.ts +13 -3
- package/src/core/css/generate.ts +9 -1
- package/src/core/data/bindings.ts +12 -6
- package/src/core/design/classes.ts +5 -3
- package/src/core/design/diff.ts +3 -1
- package/src/core/design/merge.ts +3 -1
- package/src/core/design/transfer.ts +13 -2
- package/src/core/design/variables.ts +2 -1
- package/src/core/elements/dynamic-elements.ts +9 -1
- package/src/core/elements/form-elements.ts +18 -3
- package/src/core/elements/index.ts +39 -11
- package/src/core/elements/plain-text.ts +2 -2
- package/src/core/forms/binding.ts +148 -1
- package/src/core/history/diff.ts +32 -4
- package/src/core/image-edit/schema.ts +145 -0
- package/src/core/image-edit/summary.ts +58 -0
- package/src/core/index.ts +14 -0
- package/src/core/limits.ts +10 -0
- package/src/core/migrate/index.ts +3 -1
- package/src/core/perf/meter.ts +9 -1
- package/src/core/render/dynamic-post.ts +23 -6
- package/src/core/render/form.ts +36 -11
- package/src/core/render/index.ts +75 -15
- package/src/core/render/pagination.ts +21 -2
- package/src/core/sanitize/embed-url.ts +7 -1
- package/src/core/sanitize/href.ts +13 -1
- package/src/core/schema/descriptors.ts +4 -1
- package/src/core/schema/layout.ts +21 -4
- package/src/core/share/link.ts +9 -3
- package/src/core/stable-json.ts +15 -0
- package/src/core/theme/dynamic.ts +32 -24
- package/src/core/theme/pagination.ts +30 -3
- package/src/core/theme/popup-rules.ts +10 -5
- package/src/mcp/errors.ts +44 -0
- package/src/mcp/index.ts +4 -0
- package/src/mcp/ports.ts +85 -0
- package/src/mcp/prompts.ts +76 -0
- package/src/mcp/publish.ts +60 -0
- package/src/mcp/queue.ts +19 -0
- package/src/mcp/rate-limit.ts +80 -0
- package/src/mcp/read.ts +486 -0
- package/src/mcp/refs.ts +84 -0
- package/src/mcp/settings.ts +18 -0
- package/src/mcp/tools.ts +265 -0
- package/src/mcp/write.ts +664 -0
- package/src/server/agent-settings.ts +159 -0
- package/src/server/design-routes.ts +71 -17
- package/src/server/icon-routes.ts +13 -6
- package/src/server/mcp-routes.ts +323 -0
- package/src/server/plugin.ts +32 -2
package/src/mcp/read.ts
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
auditPage,
|
|
4
|
+
BUNDLED_ICON_IDS,
|
|
5
|
+
byteLength,
|
|
6
|
+
defaultElement,
|
|
7
|
+
ELEMENT_DESCRIPTORS,
|
|
8
|
+
findNode,
|
|
9
|
+
isParentNode,
|
|
10
|
+
LAYOUT_SCHEMA_VERSION,
|
|
11
|
+
SECTION_RECIPES,
|
|
12
|
+
siteTokens,
|
|
13
|
+
validateLayout,
|
|
14
|
+
type DesignSystem,
|
|
15
|
+
type ElementType,
|
|
16
|
+
type Layout,
|
|
17
|
+
type LayoutNode,
|
|
18
|
+
} from "../core/index.ts";
|
|
19
|
+
import { parentChildRefusal } from "../core/arrange.ts";
|
|
20
|
+
import { KNOWN_NODE_SCHEMAS } from "../core/schema/layout.ts";
|
|
21
|
+
import { requiredFieldIssues, requiredFieldIssueText } from "../core/forms/binding.ts";
|
|
22
|
+
import { isFormNode } from "../core/schema/layout.ts";
|
|
23
|
+
import { forEachNode, nodeChildren } from "../core/tree-ops.ts";
|
|
24
|
+
import { McpToolError } from "./errors.ts";
|
|
25
|
+
import type { EmvbPorts, PageRecord } from "./ports.ts";
|
|
26
|
+
|
|
27
|
+
/** A stored layout the tools can work on, or a NOT_FOUND / INVALID_LAYOUT error the agent can read. */
|
|
28
|
+
async function loadPage(
|
|
29
|
+
ports: EmvbPorts,
|
|
30
|
+
pageId: string,
|
|
31
|
+
): Promise<{
|
|
32
|
+
page: PageRecord;
|
|
33
|
+
layout: Layout;
|
|
34
|
+
}> {
|
|
35
|
+
const page = await ports.getPage(pageId);
|
|
36
|
+
if (!page)
|
|
37
|
+
throw new McpToolError("NOT_FOUND", `No EmVB page with id ${pageId}. Call list_pages.`);
|
|
38
|
+
const result = validateLayout(page.layout);
|
|
39
|
+
if (!result.ok) {
|
|
40
|
+
throw new McpToolError(
|
|
41
|
+
"INVALID_LAYOUT",
|
|
42
|
+
"The stored layout of this page can't be read. Open it in the editor to repair it.",
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return { page, layout: result.layout };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export type OutlineNode = { id: string; type: string; label?: string; children?: OutlineNode[] };
|
|
49
|
+
|
|
50
|
+
/** The tree without props or styles: cheap orientation for an agent (plan § 3 get_layout). */
|
|
51
|
+
function outlineOf(node: LayoutNode): OutlineNode {
|
|
52
|
+
const kids = nodeChildren(node);
|
|
53
|
+
const text = summaryText(node);
|
|
54
|
+
return {
|
|
55
|
+
id: node.id,
|
|
56
|
+
type: node.type,
|
|
57
|
+
...(node.label || text ? { label: node.label ?? text } : {}),
|
|
58
|
+
...(kids.length > 0 ? { children: kids.map(outlineOf) } : {}),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
function summaryText(node: LayoutNode): string | undefined {
|
|
63
|
+
const props = (node as { props?: Record<string, unknown> }).props ?? {};
|
|
64
|
+
const text = typeof props["text"] === "string" ? props["text"] : undefined;
|
|
65
|
+
if (!text) return undefined;
|
|
66
|
+
return text.length > 60 ? `${text.slice(0, 57)}…` : text;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function stats(layout: Layout) {
|
|
70
|
+
let nodes = 0;
|
|
71
|
+
let depth = 0;
|
|
72
|
+
const walk = (node: LayoutNode, d: number) => {
|
|
73
|
+
nodes += 1;
|
|
74
|
+
depth = Math.max(depth, d);
|
|
75
|
+
for (const child of nodeChildren(node)) walk(child, d + 1);
|
|
76
|
+
};
|
|
77
|
+
walk(layout.root, 1);
|
|
78
|
+
return { nodes, depth, bytes: byteLength(layout) };
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------------------------------------------------------------- list_pages
|
|
82
|
+
|
|
83
|
+
export const ListPagesInput = z.strictObject({
|
|
84
|
+
cursor: z.string().max(512).optional(),
|
|
85
|
+
limit: z.number().int().min(1).max(50).optional(),
|
|
86
|
+
});
|
|
87
|
+
export const ListPagesOutput = z.object({
|
|
88
|
+
pages: z.array(
|
|
89
|
+
z.object({
|
|
90
|
+
id: z.string(),
|
|
91
|
+
title: z.string(),
|
|
92
|
+
slug: z.string(),
|
|
93
|
+
status: z.string(),
|
|
94
|
+
hasDraft: z.boolean(),
|
|
95
|
+
updatedAt: z.string(),
|
|
96
|
+
}),
|
|
97
|
+
),
|
|
98
|
+
nextCursor: z.string().optional(),
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
export async function listPages(ports: EmvbPorts, input: z.infer<typeof ListPagesInput>) {
|
|
102
|
+
const result = await ports.listPages({
|
|
103
|
+
limit: input.limit ?? 20,
|
|
104
|
+
...(input.cursor ? { cursor: input.cursor } : {}),
|
|
105
|
+
});
|
|
106
|
+
return { pages: result.pages, ...(result.nextCursor ? { nextCursor: result.nextCursor } : {}) };
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// ---------------------------------------------------------------- get_layout
|
|
110
|
+
|
|
111
|
+
export const GetLayoutInput = z.strictObject({
|
|
112
|
+
pageId: z.string().min(1).max(128),
|
|
113
|
+
format: z.enum(["full", "outline"]).optional(),
|
|
114
|
+
nodeId: z.string().min(1).max(64).optional(),
|
|
115
|
+
});
|
|
116
|
+
export const GetLayoutOutput = z.object({
|
|
117
|
+
pageId: z.string(),
|
|
118
|
+
rev: z.string(),
|
|
119
|
+
schemaVersion: z.number(),
|
|
120
|
+
title: z.string(),
|
|
121
|
+
slug: z.string(),
|
|
122
|
+
layout: z.unknown().optional(),
|
|
123
|
+
outline: z.unknown().optional(),
|
|
124
|
+
stats: z.object({ nodes: z.number(), depth: z.number(), bytes: z.number() }),
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
export async function getLayout(ports: EmvbPorts, input: z.infer<typeof GetLayoutInput>) {
|
|
128
|
+
const { page, layout } = await loadPage(ports, input.pageId);
|
|
129
|
+
const node = input.nodeId ? findNode(layout, input.nodeId) : layout.root;
|
|
130
|
+
if (!node)
|
|
131
|
+
throw new McpToolError("NOT_FOUND", `No element with id ${input.nodeId} on this page.`);
|
|
132
|
+
const base = {
|
|
133
|
+
pageId: page.id,
|
|
134
|
+
rev: page.rev,
|
|
135
|
+
schemaVersion: LAYOUT_SCHEMA_VERSION,
|
|
136
|
+
title: page.title,
|
|
137
|
+
slug: page.slug,
|
|
138
|
+
stats: stats(layout),
|
|
139
|
+
};
|
|
140
|
+
if (input.format === "outline") return { ...base, outline: outlineOf(node) };
|
|
141
|
+
return { ...base, layout: input.nodeId ? node : layout };
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// ---------------------------------------------------------------- list_element_types
|
|
145
|
+
|
|
146
|
+
const SCHEMAS = new Map(
|
|
147
|
+
KNOWN_NODE_SCHEMAS.map((schema) => [
|
|
148
|
+
(schema.shape.type as unknown as { value: string }).value,
|
|
149
|
+
schema.shape.props,
|
|
150
|
+
]),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const sample = (type: string): LayoutNode =>
|
|
154
|
+
Object.assign({}, defaultElement(type as ElementType, "sample"), { id: "sample" }) as LayoutNode;
|
|
155
|
+
|
|
156
|
+
/** Which parent types accept `type`, by the same rule drag-and-drop and save use (W-354). */
|
|
157
|
+
function allowedParents(type: string): string[] {
|
|
158
|
+
const child = sample(type);
|
|
159
|
+
return ELEMENT_DESCRIPTORS.map((d) => d.type).filter((parentType) => {
|
|
160
|
+
const parent = sample(parentType);
|
|
161
|
+
if (!isParentNode(parent)) return false;
|
|
162
|
+
return parentChildRefusal(parent, child, parentType === "form") === undefined;
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
export const ListElementTypesInput = z.strictObject({ type: z.string().max(64).optional() });
|
|
167
|
+
export const ListElementTypesOutput = z.object({
|
|
168
|
+
elements: z.array(
|
|
169
|
+
z.object({
|
|
170
|
+
type: z.string(),
|
|
171
|
+
name: z.string(),
|
|
172
|
+
group: z.string(),
|
|
173
|
+
container: z.boolean(),
|
|
174
|
+
allowedParents: z.array(z.string()),
|
|
175
|
+
props: z.unknown(),
|
|
176
|
+
example: z.unknown(),
|
|
177
|
+
notes: z.string().optional(),
|
|
178
|
+
}),
|
|
179
|
+
),
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* W-474: what an agent can't learn from the props schema alone, found in the live skill run.
|
|
184
|
+
* Icon ids, where form and loop ids come from, and which types belong together.
|
|
185
|
+
*/
|
|
186
|
+
const ELEMENT_NOTES: Record<string, string> = {
|
|
187
|
+
icon: `iconId is one of the bundled icons: ${BUNDLED_ICON_IDS.join(", ")}. Colour it with style.color; size is in px. Set decorative: true next to a text label, or give it a title.`,
|
|
188
|
+
button: `iconId takes the same bundled icon ids as the icon element (${BUNDLED_ICON_IDS.slice(0, 6).join(", ")}, …). Style buttons with a site class (see get_site_styles classes) rather than per-button styles.`,
|
|
189
|
+
form: "formId is the id of a form made in the EmDash admin under Forms; there is no tool to list or create forms, so ask the person for it. An unbound form (empty formId) shows only in the editor, not on the live page. Each field's field prop must match a field name in that form; add a submit last.",
|
|
190
|
+
loop: "A collection Loop needs collection (a collection slug such as posts); ask the person which one if unsure. The children are the item template (post-title, post-image, post-excerpt, post-link, post-date), plus an optional loop-empty shown when there are no entries.",
|
|
191
|
+
tabs: "Holds only tab-panel children; each tab-panel's label is its tab. Put content inside the panels.",
|
|
192
|
+
accordion:
|
|
193
|
+
"Holds only accordion-item children; summary is the visible question, the children are the answer. Set open: true on at most one item.",
|
|
194
|
+
"layout-section":
|
|
195
|
+
"Use one per page band. props.contentWidth caps the inner width (for example 1200 px) while the background spans the screen.",
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export function listElementTypes(input: z.infer<typeof ListElementTypesInput>) {
|
|
199
|
+
const descriptors = input.type
|
|
200
|
+
? ELEMENT_DESCRIPTORS.filter((d) => d.type === input.type)
|
|
201
|
+
: ELEMENT_DESCRIPTORS;
|
|
202
|
+
if (input.type && descriptors.length === 0) {
|
|
203
|
+
throw new McpToolError("NOT_FOUND", `Unknown element type ${input.type}.`);
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
elements: descriptors.map((d) => {
|
|
207
|
+
const props = SCHEMAS.get(d.type);
|
|
208
|
+
const example = sample(d.type);
|
|
209
|
+
const entry: Record<string, unknown> = {
|
|
210
|
+
type: d.type,
|
|
211
|
+
name: d.name,
|
|
212
|
+
group: d.group,
|
|
213
|
+
container: isParentNode(example),
|
|
214
|
+
allowedParents: allowedParents(d.type),
|
|
215
|
+
props: props ? z.toJSONSchema(props, { unrepresentable: "any" }) : {},
|
|
216
|
+
example: { type: example.type, props: (example as { props?: unknown }).props ?? {} },
|
|
217
|
+
};
|
|
218
|
+
if (ELEMENT_NOTES[d.type]) entry["notes"] = ELEMENT_NOTES[d.type];
|
|
219
|
+
return entry;
|
|
220
|
+
}),
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
// ---------------------------------------------------------------- get_site_styles
|
|
225
|
+
|
|
226
|
+
export const GetSiteStylesInput = z.strictObject({});
|
|
227
|
+
export const GetSiteStylesOutput = z.object({
|
|
228
|
+
revision: z.string().nullable(),
|
|
229
|
+
publishedRevision: z.string().nullable(),
|
|
230
|
+
unpublished: z.boolean(),
|
|
231
|
+
design: z.unknown(),
|
|
232
|
+
tokens: z.unknown(),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
export async function getSiteStyles(ports: EmvbPorts) {
|
|
236
|
+
const state = await ports.getDesign();
|
|
237
|
+
return { ...state, tokens: siteTokens(state.design) };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// ---------------------------------------------------------------- list_section_recipes
|
|
241
|
+
|
|
242
|
+
export const ListRecipesInput = z.strictObject({});
|
|
243
|
+
export const ListRecipesOutput = z.object({
|
|
244
|
+
recipes: z.array(z.object({ id: z.string(), name: z.string(), description: z.string() })),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
export const listSectionRecipes = () => ({
|
|
248
|
+
recipes: SECTION_RECIPES.map((r) => ({ id: r.id, name: r.name, description: r.description })),
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
// ---------------------------------------------------------------- describe_page
|
|
252
|
+
|
|
253
|
+
const TEXT_TYPES = new Set(["heading", "text", "label", "link", "button", "list"]);
|
|
254
|
+
|
|
255
|
+
function textOf(node: LayoutNode): string {
|
|
256
|
+
const props = (node as { props?: Record<string, unknown> }).props ?? {};
|
|
257
|
+
if (Array.isArray(props["items"])) return props["items"].join(" · ");
|
|
258
|
+
return typeof props["text"] === "string" ? props["text"] : "";
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
function collect(node: LayoutNode, out: { text: string[]; headings: string[]; media: number }) {
|
|
262
|
+
if (node.type === "heading") {
|
|
263
|
+
const level = (node as { props: { level: number } }).props.level;
|
|
264
|
+
out.headings.push(`H${level} ${textOf(node)}`);
|
|
265
|
+
}
|
|
266
|
+
if (TEXT_TYPES.has(node.type)) out.text.push(textOf(node));
|
|
267
|
+
if (node.type === "image" || node.type === "video") out.media += 1;
|
|
268
|
+
for (const child of nodeChildren(node)) collect(child, out);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export const DescribePageInput = z.strictObject({ pageId: z.string().min(1).max(128) });
|
|
272
|
+
export const DescribePageOutput = z.object({
|
|
273
|
+
pageId: z.string(),
|
|
274
|
+
title: z.string(),
|
|
275
|
+
summary: z.string(),
|
|
276
|
+
sections: z.array(
|
|
277
|
+
z.object({
|
|
278
|
+
id: z.string(),
|
|
279
|
+
type: z.string(),
|
|
280
|
+
label: z.string().optional(),
|
|
281
|
+
headings: z.array(z.string()),
|
|
282
|
+
excerpt: z.string(),
|
|
283
|
+
media: z.number(),
|
|
284
|
+
}),
|
|
285
|
+
),
|
|
286
|
+
headingOutline: z.array(z.string()),
|
|
287
|
+
/** W-500: things that make content vanish without an error, such as hidden on every device. */
|
|
288
|
+
warnings: z.array(z.object({ nodeId: z.string(), message: z.string() })).optional(),
|
|
289
|
+
wordCount: z.number(),
|
|
290
|
+
images: z
|
|
291
|
+
.array(z.object({ id: z.string(), alt: z.string(), src: z.string(), decorative: z.boolean() }))
|
|
292
|
+
.optional(),
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
/** W-474: a review needs the alt texts next to what the images are, which only get_layout had. */
|
|
296
|
+
function imagesOf(root: LayoutNode) {
|
|
297
|
+
const found: { id: string; alt: string; src: string; decorative: boolean }[] = [];
|
|
298
|
+
const walk = (node: LayoutNode) => {
|
|
299
|
+
if (node.type === "image" && found.length < 50) {
|
|
300
|
+
const props = node.props as { alt?: unknown; src?: unknown; decorative?: unknown };
|
|
301
|
+
found.push({
|
|
302
|
+
id: node.id,
|
|
303
|
+
alt: typeof props.alt === "string" ? clip(props.alt, 300) : "",
|
|
304
|
+
src: typeof props.src === "string" ? clip(props.src, 300) : "",
|
|
305
|
+
decorative: props.decorative === true,
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
for (const child of nodeChildren(node)) walk(child);
|
|
309
|
+
};
|
|
310
|
+
walk(root);
|
|
311
|
+
return found;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
/** W-500: elements hidden on desktop, tablet and mobile never show; the agent should know. */
|
|
315
|
+
function hiddenEverywhere(root: LayoutNode) {
|
|
316
|
+
const warnings: { nodeId: string; message: string }[] = [];
|
|
317
|
+
const walk = (node: LayoutNode) => {
|
|
318
|
+
const hidden = new Set((node as { hiddenOn?: string[] }).hiddenOn ?? []);
|
|
319
|
+
if (hidden.has("desktop") && hidden.has("tablet") && hidden.has("mobile")) {
|
|
320
|
+
warnings.push({
|
|
321
|
+
nodeId: node.id,
|
|
322
|
+
message:
|
|
323
|
+
node.id === root.id
|
|
324
|
+
? "The whole page is hidden on every device, so visitors see an empty page. Unset hiddenOn on the root."
|
|
325
|
+
: `${node.label ?? node.type} ${node.id} is hidden on every device, so it and everything inside it never show. Unset hiddenOn, or remove it.`,
|
|
326
|
+
});
|
|
327
|
+
return;
|
|
328
|
+
}
|
|
329
|
+
for (const child of nodeChildren(node)) walk(child);
|
|
330
|
+
};
|
|
331
|
+
walk(root);
|
|
332
|
+
return warnings.slice(0, 50);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* W-511: required form fields visitors can't fill (missing from the form, or hidden on a device),
|
|
337
|
+
* so the form can't be sent there. Needs the forms plugin's definitions; none without them.
|
|
338
|
+
*/
|
|
339
|
+
async function requiredFieldWarnings(ports: EmvbPorts, root: LayoutNode) {
|
|
340
|
+
const forms: LayoutNode[] = [];
|
|
341
|
+
forEachNode(root, (node) => {
|
|
342
|
+
if (isFormNode(node) && node.props.formId.trim() && forms.length < 20) forms.push(node);
|
|
343
|
+
});
|
|
344
|
+
if (forms.length === 0 || !ports.formFields) return [];
|
|
345
|
+
let fields: ReadonlyMap<string, readonly { name: string; label: string; required: boolean }[]>;
|
|
346
|
+
try {
|
|
347
|
+
fields = await ports.formFields([
|
|
348
|
+
...new Set(forms.map((form) => (form.props as { formId: string }).formId.trim())),
|
|
349
|
+
]);
|
|
350
|
+
} catch {
|
|
351
|
+
return [];
|
|
352
|
+
}
|
|
353
|
+
const warnings: { nodeId: string; message: string }[] = [];
|
|
354
|
+
for (const form of forms) {
|
|
355
|
+
const defined = fields.get((form.props as { formId: string }).formId.trim());
|
|
356
|
+
if (!defined) continue;
|
|
357
|
+
for (const issue of requiredFieldIssues(form, defined)) {
|
|
358
|
+
warnings.push({
|
|
359
|
+
nodeId: issue.kind === "missing" ? issue.formNodeId : issue.nodeId,
|
|
360
|
+
message:
|
|
361
|
+
issue.kind === "hidden"
|
|
362
|
+
? `${requiredFieldIssueText(issue)} (Unset hiddenOn on ${issue.nodeId} or the box around it.)`
|
|
363
|
+
: issue.kind === "audience"
|
|
364
|
+
? `${requiredFieldIssueText(issue)} (Unset audience or variant on ${issue.nodeId} or the box around it.)`
|
|
365
|
+
: `${requiredFieldIssueText(issue)} (Form ${issue.formNodeId}, field name ${issue.field}.)`,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
return warnings;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
const clip = (text: string, max: number) =>
|
|
373
|
+
text.length > max ? `${text.slice(0, max - 1)}…` : text;
|
|
374
|
+
|
|
375
|
+
export async function describePage(ports: EmvbPorts, input: z.infer<typeof DescribePageInput>) {
|
|
376
|
+
const { page, layout } = await loadPage(ports, input.pageId);
|
|
377
|
+
const top = nodeChildren(layout.root);
|
|
378
|
+
const all = { text: [] as string[], headings: [] as string[], media: 0 };
|
|
379
|
+
collect(layout.root, all);
|
|
380
|
+
const sections = top.map((node) => {
|
|
381
|
+
const part = { text: [] as string[], headings: [] as string[], media: 0 };
|
|
382
|
+
collect(node, part);
|
|
383
|
+
const section: {
|
|
384
|
+
id: string;
|
|
385
|
+
type: string;
|
|
386
|
+
label?: string;
|
|
387
|
+
headings: string[];
|
|
388
|
+
excerpt: string;
|
|
389
|
+
media: number;
|
|
390
|
+
} = {
|
|
391
|
+
id: node.id,
|
|
392
|
+
type: node.type,
|
|
393
|
+
headings: part.headings,
|
|
394
|
+
excerpt: clip(part.text.join(" "), 240),
|
|
395
|
+
media: part.media,
|
|
396
|
+
};
|
|
397
|
+
if (node.label) section.label = node.label;
|
|
398
|
+
return section;
|
|
399
|
+
});
|
|
400
|
+
const wordCount = all.text.join(" ").split(/\s+/).filter(Boolean).length;
|
|
401
|
+
const warnings = [
|
|
402
|
+
...hiddenEverywhere(layout.root),
|
|
403
|
+
...(await requiredFieldWarnings(ports, layout.root)),
|
|
404
|
+
].slice(0, 50);
|
|
405
|
+
const summary = clip(
|
|
406
|
+
`"${page.title}" (/${page.slug}, ${page.status}${page.hasDraft ? ", unpublished changes" : ""}): ` +
|
|
407
|
+
`${sections.length} top-level section${sections.length === 1 ? "" : "s"}, ` +
|
|
408
|
+
`${all.headings.length} heading${all.headings.length === 1 ? "" : "s"}, ${all.media} image or video, ` +
|
|
409
|
+
`${wordCount} words.`,
|
|
410
|
+
1200,
|
|
411
|
+
);
|
|
412
|
+
return {
|
|
413
|
+
pageId: page.id,
|
|
414
|
+
title: page.title,
|
|
415
|
+
summary,
|
|
416
|
+
sections,
|
|
417
|
+
headingOutline: all.headings,
|
|
418
|
+
wordCount,
|
|
419
|
+
images: imagesOf(layout.root),
|
|
420
|
+
...(warnings.length ? { warnings } : {}),
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// ---------------------------------------------------------------- get_accessibility_findings
|
|
425
|
+
|
|
426
|
+
export const A11yInput = z.strictObject({ pageId: z.string().min(1).max(128) });
|
|
427
|
+
export const A11yOutput = z.object({
|
|
428
|
+
pageId: z.string(),
|
|
429
|
+
score: z.number(),
|
|
430
|
+
findings: z.array(
|
|
431
|
+
z.object({
|
|
432
|
+
rule: z.string(),
|
|
433
|
+
severity: z.enum(["error", "warning"]),
|
|
434
|
+
nodeId: z.string(),
|
|
435
|
+
message: z.string(),
|
|
436
|
+
fix: z.unknown().optional(),
|
|
437
|
+
}),
|
|
438
|
+
),
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
export async function accessibilityFindings(ports: EmvbPorts, input: z.infer<typeof A11yInput>) {
|
|
442
|
+
const { page, layout } = await loadPage(ports, input.pageId);
|
|
443
|
+
const { design } = await ports.getDesign();
|
|
444
|
+
const report = auditPage(layout, design as DesignSystem);
|
|
445
|
+
return {
|
|
446
|
+
pageId: page.id,
|
|
447
|
+
score: report.score,
|
|
448
|
+
findings: report.issues.map((issue) => {
|
|
449
|
+
const finding: {
|
|
450
|
+
rule: string;
|
|
451
|
+
severity: "error" | "warning";
|
|
452
|
+
nodeId: string;
|
|
453
|
+
message: string;
|
|
454
|
+
fix?: unknown;
|
|
455
|
+
} = {
|
|
456
|
+
rule: issue.rule,
|
|
457
|
+
severity: issue.severity,
|
|
458
|
+
nodeId: issue.nodeId,
|
|
459
|
+
message: issue.message,
|
|
460
|
+
};
|
|
461
|
+
if (issue.fix) finding.fix = issue.fix;
|
|
462
|
+
return finding;
|
|
463
|
+
}),
|
|
464
|
+
};
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// ---------------------------------------------------------------- list_versions
|
|
468
|
+
|
|
469
|
+
export const ListVersionsInput = z.strictObject({
|
|
470
|
+
pageId: z.string().min(1).max(128),
|
|
471
|
+
limit: z.number().int().min(1).max(50).optional(),
|
|
472
|
+
});
|
|
473
|
+
export const ListVersionsOutput = z.object({
|
|
474
|
+
versions: z.array(
|
|
475
|
+
z.object({ revisionId: z.string(), at: z.string(), authorId: z.string().nullable() }),
|
|
476
|
+
),
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
export async function listVersions(ports: EmvbPorts, input: z.infer<typeof ListVersionsInput>) {
|
|
480
|
+
const page = await ports.getPage(input.pageId);
|
|
481
|
+
if (!page) throw new McpToolError("NOT_FOUND", `No EmVB page with id ${input.pageId}.`);
|
|
482
|
+
const revisions = await ports.listRevisions(input.pageId, input.limit ?? 20);
|
|
483
|
+
return {
|
|
484
|
+
versions: revisions.map((r) => ({ revisionId: r.id, at: r.createdAt, authorId: r.authorId })),
|
|
485
|
+
};
|
|
486
|
+
}
|
package/src/mcp/refs.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import type { DesignSystem } from "../core/index.ts";
|
|
2
|
+
import { McpToolError } from "./errors.ts";
|
|
3
|
+
|
|
4
|
+
type From = "color" | "font" | "fontSize" | "spacing";
|
|
5
|
+
|
|
6
|
+
const KINDS: [keyof DesignSystem["variables"], From][] = [
|
|
7
|
+
["colors", "color"],
|
|
8
|
+
["fonts", "font"],
|
|
9
|
+
["fontSizes", "fontSize"],
|
|
10
|
+
["spacings", "spacing"],
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
|
14
|
+
typeof v === "object" && v !== null && !Array.isArray(v);
|
|
15
|
+
|
|
16
|
+
const isRef = (v: Record<string, unknown>) =>
|
|
17
|
+
typeof v["var"] === "string" && Object.keys(v).every((k) => k === "var" || k === "from");
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Live MCP test (W-474): agents wrote `{ "var": "display" }` for a font and the style was
|
|
21
|
+
* silently dropped, because only colours may leave out `from`. This fills in `from` from the
|
|
22
|
+
* variable's kind and refuses ids that don't exist, so a typo can't fall back without a word.
|
|
23
|
+
* Class names in `classes` are checked the same way.
|
|
24
|
+
*/
|
|
25
|
+
export function resolveDesignRefs<T>(value: T, design: DesignSystem): T {
|
|
26
|
+
const kinds = new Map<string, From[]>();
|
|
27
|
+
for (const [key, from] of KINDS) {
|
|
28
|
+
for (const v of design.variables[key] ?? [])
|
|
29
|
+
kinds.set(v.id, [...(kinds.get(v.id) ?? []), from]);
|
|
30
|
+
}
|
|
31
|
+
const classIds = new Set((design.classes ?? []).map((c) => c.id));
|
|
32
|
+
const unknown = new Set<string>();
|
|
33
|
+
const unknownClasses = new Set<string>();
|
|
34
|
+
const ambiguous = new Set<string>();
|
|
35
|
+
const walk = (v: unknown, key?: string): unknown => {
|
|
36
|
+
if (Array.isArray(v)) {
|
|
37
|
+
if (key === "classes") {
|
|
38
|
+
for (const c of v) if (typeof c === "string" && !classIds.has(c)) unknownClasses.add(c);
|
|
39
|
+
return v;
|
|
40
|
+
}
|
|
41
|
+
return v.map((x) => walk(x));
|
|
42
|
+
}
|
|
43
|
+
if (!isRecord(v)) return v;
|
|
44
|
+
if (isRef(v)) {
|
|
45
|
+
const id = v["var"] as string;
|
|
46
|
+
const found = kinds.get(id);
|
|
47
|
+
if (!found) {
|
|
48
|
+
unknown.add(id);
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
const given = v["from"] as From | undefined;
|
|
52
|
+
const from =
|
|
53
|
+
given && found.includes(given) ? given : found.length === 1 ? found[0] : undefined;
|
|
54
|
+
if (!from) {
|
|
55
|
+
ambiguous.add(id);
|
|
56
|
+
return v;
|
|
57
|
+
}
|
|
58
|
+
return from === "color" ? { var: id } : { var: id, from };
|
|
59
|
+
}
|
|
60
|
+
return Object.fromEntries(Object.entries(v).map(([k, x]) => [k, walk(x, k)]));
|
|
61
|
+
};
|
|
62
|
+
const out = walk(value) as T;
|
|
63
|
+
const problems: string[] = [];
|
|
64
|
+
if (unknown.size > 0) {
|
|
65
|
+
const list = KINDS.map(
|
|
66
|
+
([key]) => `${key}: ${(design.variables[key] ?? []).map((x) => x.id).join(", ") || "none"}`,
|
|
67
|
+
).join("; ");
|
|
68
|
+
problems.push(
|
|
69
|
+
`Unknown variable ${[...unknown].map((id) => `"${id}"`).join(", ")}. Variables are ${list}.`,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
if (ambiguous.size > 0) {
|
|
73
|
+
problems.push(
|
|
74
|
+
`${[...ambiguous].map((id) => `"${id}"`).join(", ")} names more than one kind of variable; add "from" ("color", "font", "fontSize" or "spacing").`,
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
if (unknownClasses.size > 0) {
|
|
78
|
+
problems.push(
|
|
79
|
+
`Unknown class ${[...unknownClasses].map((id) => `"${id}"`).join(", ")}. Classes are ${[...classIds].join(", ") || "none"}; add one with update_site_styles first.`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
if (problems.length > 0) throw new McpToolError("INVALID_INPUT", problems.join(" "));
|
|
83
|
+
return out;
|
|
84
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
|
|
3
|
+
/** Site-owner settings for AI agents (plan § 4, D-055). Only admins change them; no tool does. */
|
|
4
|
+
export const AgentSettings = z.object({
|
|
5
|
+
/** Master switch "Let AI agents edit EmVB pages" (off by default). */
|
|
6
|
+
mcpEnabled: z.boolean(),
|
|
7
|
+
/** Agents may publish pages (`publish_page`). */
|
|
8
|
+
publishPages: z.boolean(),
|
|
9
|
+
/** Agents may publish the site styles (`publish_site_styles`). */
|
|
10
|
+
publishStyles: z.boolean(),
|
|
11
|
+
});
|
|
12
|
+
export type AgentSettings = z.infer<typeof AgentSettings>;
|
|
13
|
+
|
|
14
|
+
export const DEFAULT_AGENT_SETTINGS: AgentSettings = {
|
|
15
|
+
mcpEnabled: false,
|
|
16
|
+
publishPages: false,
|
|
17
|
+
publishStyles: false,
|
|
18
|
+
};
|