@perkyzz/emvb 0.4.0 → 0.5.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 +19 -3
- package/package.json +4 -3
- package/src/admin/agents-api.ts +39 -0
- package/src/admin/editor/CommandPalette.tsx +43 -2
- package/src/admin/editor/Editor.tsx +27 -0
- package/src/admin/editor/Timeline.tsx +108 -6
- package/src/admin/editor/editor-css.ts +4 -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 +19 -8
- package/src/admin/editor/panels/ElementPanel.tsx +1 -1
- package/src/admin/editor/panels/TokenScales.tsx +4 -2
- 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 +6 -0
- 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 +2 -3
- package/src/admin/editor/panels/settings/QueryStyleRows.tsx +178 -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 +4 -2
- package/src/admin/editor/remote-change.ts +67 -0
- package/src/admin/index.tsx +3 -1
- package/src/admin/media-api.ts +20 -0
- package/src/admin/pages/AgentsPage.tsx +274 -0
- package/src/admin/ui.ts +28 -0
- package/src/astro/floats/runtime.ts +10 -0
- package/src/astro/popups/runtime.ts +15 -0
- package/src/astro/tabs/runtime.ts +60 -1
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +202 -26
- package/src/core/arrange.ts +11 -3
- package/src/core/css/generate.ts +9 -1
- package/src/core/elements/form-elements.ts +19 -3
- package/src/core/elements/index.ts +39 -11
- package/src/core/render/dynamic-post.ts +21 -5
- package/src/core/render/form.ts +12 -5
- package/src/core/render/index.ts +50 -11
- 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 +10 -3
- package/src/core/share/link.ts +4 -1
- package/src/core/theme/dynamic.ts +12 -7
- package/src/mcp/errors.ts +44 -0
- package/src/mcp/index.ts +4 -0
- package/src/mcp/ports.ts +78 -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 +419 -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 +582 -0
- package/src/server/agent-settings.ts +134 -0
- package/src/server/design-routes.ts +71 -17
- package/src/server/icon-routes.ts +5 -1
- package/src/server/mcp-routes.ts +265 -0
- package/src/server/plugin.ts +32 -2
package/src/core/render/index.ts
CHANGED
|
@@ -218,6 +218,13 @@ export function renderPage(
|
|
|
218
218
|
queryMin?: number;
|
|
219
219
|
}[] = [];
|
|
220
220
|
const layoutHasToc = treeHasType(layout.root, "toc");
|
|
221
|
+
// W-384: a table of contents lists the page's headings that actually render, with their
|
|
222
|
+
// rendered text, so a hidden A/B arm or audience heading is not a dead link.
|
|
223
|
+
const pageHeadings = layoutHasToc
|
|
224
|
+
? new Set(headingAnchors(layout.root).map((h) => h.node))
|
|
225
|
+
: undefined;
|
|
226
|
+
const renderedHeadings: { id: string; text: string }[] = [];
|
|
227
|
+
const tocNodes: { node: LayoutNode; vnode: VNode }[] = [];
|
|
221
228
|
const knownVariables = new Set(design.variables.colors.map((c) => c.id));
|
|
222
229
|
let backgroundVideo = false;
|
|
223
230
|
|
|
@@ -365,6 +372,10 @@ export function renderPage(
|
|
|
365
372
|
tablet: tablet.declarations,
|
|
366
373
|
mobile: mobile.declarations,
|
|
367
374
|
hiddenOn,
|
|
375
|
+
...(node.type === "image" &&
|
|
376
|
+
String((node.props as { caption?: unknown }).caption ?? "").trim() !== ""
|
|
377
|
+
? { figure: true }
|
|
378
|
+
: {}),
|
|
368
379
|
...(queryDeclarations.length > 0 && queryMin !== undefined
|
|
369
380
|
? { query: queryDeclarations, queryMin }
|
|
370
381
|
: {}),
|
|
@@ -426,13 +437,31 @@ export function renderPage(
|
|
|
426
437
|
if (isDynamicPostNode(node)) {
|
|
427
438
|
return finish(
|
|
428
439
|
node,
|
|
429
|
-
renderDynamicPost(
|
|
440
|
+
renderDynamicPost(
|
|
441
|
+
node,
|
|
442
|
+
attrs,
|
|
443
|
+
resolvePostForRender(dynamic, mode),
|
|
444
|
+
mode,
|
|
445
|
+
dynamic?.inLoopItem === true,
|
|
446
|
+
),
|
|
430
447
|
);
|
|
431
448
|
}
|
|
432
449
|
if (isTabsNode(node)) return finish(node, renderTabs(node, id, attrs, ctx, dynamic));
|
|
433
450
|
if (node.type === "toc") {
|
|
434
451
|
const built = def.build(node as never, attrs, []);
|
|
435
|
-
|
|
452
|
+
const vnode = { ...built, children: [] as (VNode | string)[] };
|
|
453
|
+
tocNodes.push({ node, vnode });
|
|
454
|
+
return finish(node, vnode);
|
|
455
|
+
}
|
|
456
|
+
if (node.type === "heading" && pageHeadings?.has(stored) && attrs.id) {
|
|
457
|
+
const text = String((node.props as { text?: unknown }).text ?? "").trim();
|
|
458
|
+
// W-409: a heading hidden on every device has nothing to jump to.
|
|
459
|
+
const hiddenEverywhere = (["desktop", "tablet", "mobile"] as const).every((device) =>
|
|
460
|
+
node.hiddenOn?.includes(device),
|
|
461
|
+
);
|
|
462
|
+
if (text && !hiddenEverywhere && !renderedHeadings.some((h) => h.id === attrs.id)) {
|
|
463
|
+
renderedHeadings.push({ id: attrs.id, text });
|
|
464
|
+
}
|
|
436
465
|
}
|
|
437
466
|
if (isParentNode(node)) {
|
|
438
467
|
return finish(node, def.build(node as never, attrs, ctx.children(node.children, dynamic)));
|
|
@@ -487,6 +516,16 @@ export function renderPage(
|
|
|
487
516
|
attrs: { class: "emvb-root" },
|
|
488
517
|
children: [],
|
|
489
518
|
};
|
|
519
|
+
for (const toc of tocNodes) {
|
|
520
|
+
toc.vnode.children = tocChildren(toc.node, renderedHeadings, mode);
|
|
521
|
+
// W-430: with no headings the public page had an empty "Table of contents" navigation
|
|
522
|
+
// landmark (or just its title). It's left as a plain empty box instead.
|
|
523
|
+
if (mode !== "editor" && renderedHeadings.length === 0) {
|
|
524
|
+
toc.vnode.tag = "div";
|
|
525
|
+
delete toc.vnode.attrs["aria-label"];
|
|
526
|
+
toc.vnode.children = [];
|
|
527
|
+
}
|
|
528
|
+
}
|
|
490
529
|
const baseCss = new Map(Object.entries(ELEMENTS).map(([type, def]) => [type, def.baseCss]));
|
|
491
530
|
const css = generateCss({
|
|
492
531
|
design,
|
|
@@ -518,24 +557,24 @@ function treeHasType(node: LayoutNode, type: string): boolean {
|
|
|
518
557
|
return someNode(node, (current) => current.type === type);
|
|
519
558
|
}
|
|
520
559
|
|
|
521
|
-
|
|
522
|
-
|
|
560
|
+
/** The page's own heading nodes (not synced section or loop item contents). */
|
|
561
|
+
function headingAnchors(root: LayoutNode): { node: LayoutNode }[] {
|
|
562
|
+
const out: { node: LayoutNode }[] = [];
|
|
523
563
|
forEachNode(root, (node) => {
|
|
524
|
-
if (node.type
|
|
525
|
-
const text = String((node.props as { text?: unknown }).text ?? "").trim();
|
|
526
|
-
if (!text) return;
|
|
527
|
-
const id = node.htmlId && HTML_ID.test(node.htmlId) ? node.htmlId : `emvb-h-${node.id}`;
|
|
528
|
-
out.push({ id, text });
|
|
564
|
+
if (node.type === "heading") out.push({ node });
|
|
529
565
|
});
|
|
530
566
|
return out;
|
|
531
567
|
}
|
|
532
568
|
|
|
533
|
-
function tocChildren(
|
|
569
|
+
function tocChildren(
|
|
570
|
+
node: LayoutNode,
|
|
571
|
+
items: { id: string; text: string }[],
|
|
572
|
+
mode: RenderMode,
|
|
573
|
+
): (VNode | string)[] {
|
|
534
574
|
const title =
|
|
535
575
|
node.type === "toc"
|
|
536
576
|
? String((node.props as { title?: unknown }).title ?? "").trim() || undefined
|
|
537
577
|
: undefined;
|
|
538
|
-
const items = headingAnchors(layout.root);
|
|
539
578
|
const children: (VNode | string)[] = [];
|
|
540
579
|
if (title) children.push({ tag: "p", attrs: { class: "emvb-toc-title" }, children: [title] });
|
|
541
580
|
if (items.length === 0) {
|
|
@@ -8,6 +8,13 @@ const EDITOR_SAMPLE: ArchivePagination = { page: 2, hasMore: true, basePath: "/p
|
|
|
8
8
|
|
|
9
9
|
const label = (text: string | undefined, fallback: string) => text?.trim() || fallback;
|
|
10
10
|
|
|
11
|
+
/**
|
|
12
|
+
* W-433: Previous/Next set to just an arrow ("←", "»") was read as "left arrow" or nothing; such
|
|
13
|
+
* texts get the plain name as an aria-label.
|
|
14
|
+
*/
|
|
15
|
+
const nameIfSymbols = (text: string, name: string): Record<string, string> =>
|
|
16
|
+
/[\p{L}\p{N}]/u.test(text) ? {} : { "aria-label": name };
|
|
17
|
+
|
|
11
18
|
/**
|
|
12
19
|
* Previous, page numbers and Next as plain links in a `nav` (W-222). No JS. Nothing on the page
|
|
13
20
|
* when there is no archive page data or only one page; the editor shows a sample.
|
|
@@ -45,13 +52,23 @@ export function renderPagination(
|
|
|
45
52
|
case "prev":
|
|
46
53
|
return {
|
|
47
54
|
tag: "a",
|
|
48
|
-
attrs: {
|
|
55
|
+
attrs: {
|
|
56
|
+
class: "emvb-pagination-prev",
|
|
57
|
+
href: href(item.href),
|
|
58
|
+
rel: "prev",
|
|
59
|
+
...nameIfSymbols(label(node.props.prevText, "Previous"), "Previous page"),
|
|
60
|
+
},
|
|
49
61
|
children: [label(node.props.prevText, "Previous")],
|
|
50
62
|
};
|
|
51
63
|
case "next":
|
|
52
64
|
return {
|
|
53
65
|
tag: "a",
|
|
54
|
-
attrs: {
|
|
66
|
+
attrs: {
|
|
67
|
+
class: "emvb-pagination-next",
|
|
68
|
+
href: href(item.href),
|
|
69
|
+
rel: "next",
|
|
70
|
+
...nameIfSymbols(label(node.props.nextText, "Next"), "Next page"),
|
|
71
|
+
},
|
|
55
72
|
children: [label(node.props.nextText, "Next")],
|
|
56
73
|
};
|
|
57
74
|
case "gap":
|
|
@@ -66,6 +83,8 @@ export function renderPagination(
|
|
|
66
83
|
attrs: {
|
|
67
84
|
class: "emvb-pagination-page",
|
|
68
85
|
href: href(item.href),
|
|
86
|
+
// W-426: a bare "2" says nothing out of context; screen readers hear "Page 2".
|
|
87
|
+
"aria-label": `Page ${item.n}`,
|
|
69
88
|
...(item.current ? { "aria-current": "page" } : {}),
|
|
70
89
|
},
|
|
71
90
|
children: [String(item.n)],
|
|
@@ -114,8 +114,14 @@ const PROVIDERS = [
|
|
|
114
114
|
* Resolves an editor-supplied URL to a safe embed target, or undefined if refused.
|
|
115
115
|
* YouTube always becomes `youtube-nocookie.com`; Vimeo becomes `player.vimeo.com`.
|
|
116
116
|
*/
|
|
117
|
+
// W-448: "youtube.com/watch?v=…" typed without https:// was taken for a video file path on this
|
|
118
|
+
// site, so the page showed a broken player. Known video hosts get their scheme.
|
|
119
|
+
const BARE_VIDEO_HOST =
|
|
120
|
+
/^(?:(?:www\.|m\.)?youtube\.com|youtu\.be|(?:www\.|player\.)?vimeo\.com)\//i;
|
|
121
|
+
|
|
117
122
|
export function resolveEmbedUrl(value: string): EmbedTarget | undefined {
|
|
118
|
-
const
|
|
123
|
+
const typed = value.trim();
|
|
124
|
+
const trimmed = BARE_VIDEO_HOST.test(typed) ? `https://${typed}` : typed;
|
|
119
125
|
if (trimmed.length === 0 || trimmed.length > 2000) return undefined;
|
|
120
126
|
|
|
121
127
|
const hasScheme = /^[a-z][a-z0-9+.-]*:/i.test(trimmed);
|
|
@@ -3,4 +3,16 @@ import { sanitizeUrl } from "./url.ts";
|
|
|
3
3
|
const HREF_SCHEMES: ReadonlySet<string> = new Set(["http:", "https:", "mailto:", "tel:"]);
|
|
4
4
|
|
|
5
5
|
/** Allowed public hrefs (R-032): http(s), mailto, tel, and relative paths, queries and hashes. */
|
|
6
|
-
export
|
|
6
|
+
export function sanitizeHref(value: string): string | undefined {
|
|
7
|
+
// W-417: phone numbers are usually written with spaces ("tel:+1 555 010 0199"); the spaces
|
|
8
|
+
// made the link fall back to "#". They're dropped, as phones ignore them anyway.
|
|
9
|
+
if (/^\s*tel:/i.test(value)) return sanitizeUrl(value.replace(/\s+/g, ""), HREF_SCHEMES);
|
|
10
|
+
// W-427: "www.example.com" is a site, not a page path next to the current one.
|
|
11
|
+
const trimmed = value.trim().replace(/^www\./i, (w) => `https://${w}`);
|
|
12
|
+
// W-428: a plain space in an email link ("mailto:me@x.com?subject=Hi there") is encoded, as a
|
|
13
|
+
// browser would; the link used to fall back to "#". Web addresses with spaces are still refused.
|
|
14
|
+
return sanitizeUrl(
|
|
15
|
+
/^mailto:/i.test(trimmed) ? trimmed.replaceAll(" ", "%20") : trimmed,
|
|
16
|
+
HREF_SCHEMES,
|
|
17
|
+
);
|
|
18
|
+
}
|
|
@@ -9,7 +9,8 @@ export type FieldKind =
|
|
|
9
9
|
| "href"
|
|
10
10
|
| "media"
|
|
11
11
|
| "icon"
|
|
12
|
-
| "list-items"
|
|
12
|
+
| "list-items"
|
|
13
|
+
| "image-sources";
|
|
13
14
|
|
|
14
15
|
export type FieldOption = { value: string | number | boolean; label: string };
|
|
15
16
|
|
|
@@ -23,6 +24,8 @@ export type FieldDescriptor = {
|
|
|
23
24
|
optional?: boolean;
|
|
24
25
|
/** Highest value a whole-number field accepts, matching its schema (W-223). */
|
|
25
26
|
max?: number;
|
|
27
|
+
/** Lowest value a whole-number field accepts; unset is 1 (W-388). */
|
|
28
|
+
min?: number;
|
|
26
29
|
/** Props that hold this field's tablet and mobile values (W-139); unset follows the wider device. */
|
|
27
30
|
devices?: { tablet: string; mobile: string };
|
|
28
31
|
};
|
|
@@ -104,7 +104,7 @@ export const AUDIENCE_DEVICES = ["mobile", "tablet", "desktop"] as const;
|
|
|
104
104
|
/** A visitor segment name the host passes and a Visitors rule matches (W-329). */
|
|
105
105
|
/** A host segment name: letters, digits, and `:` `_` `-`, up to 64 characters (`plan:pro`). */
|
|
106
106
|
export const AUDIENCE_SEGMENT = /^[a-z0-9][a-z0-9:_-]{0,63}$/;
|
|
107
|
-
const SEGMENT_HINT = "Segments are lowercase letters, digits, - or _, e.g.
|
|
107
|
+
const SEGMENT_HINT = "Segments are lowercase letters, digits, :, - or _, e.g. plan:pro";
|
|
108
108
|
|
|
109
109
|
/**
|
|
110
110
|
* Who sees an element (W-313): visitors from these countries, on these devices, new or
|
|
@@ -566,9 +566,13 @@ const FIELD_SLUG_HINT = "Use the field's slug, e.g. photo";
|
|
|
566
566
|
/** A site path with `{slug}` or `{id}`, and no other placeholders (W-330). */
|
|
567
567
|
export const LOOP_PERMALINK =
|
|
568
568
|
/^(?=.*\{(?:slug|id)\})\/(?!\/)(?:[A-Za-z0-9._~/-]|\{slug\}|\{id\})*$/;
|
|
569
|
-
/**
|
|
569
|
+
/**
|
|
570
|
+
* `field=value` pairs separated by `;` (W-330). Values have no `;`, `=` or angle brackets.
|
|
571
|
+
* W-391: a value starts and ends with a non-space, so the spaces around it can only be matched
|
|
572
|
+
* one way; the earlier pattern let both sides take them and a 265-character filter took ~50 s.
|
|
573
|
+
*/
|
|
570
574
|
export const LOOP_FILTER =
|
|
571
|
-
/^\s*[a-z][a-z0-9_]{0,63}\s*=\s*[^;=<>]{
|
|
575
|
+
/^\s*[a-z][a-z0-9_]{0,63}\s*=\s*[^;=<>\s](?:[^;=<>]{0,98}[^;=<>\s])?(?:\s*;\s*[a-z][a-z0-9_]{0,63}\s*=\s*[^;=<>\s](?:[^;=<>]{0,98}[^;=<>\s])?)*\s*;?\s*$/;
|
|
572
576
|
/** Which entries a collection Loop shows first (W-308). */
|
|
573
577
|
export const LOOP_ORDERS = ["newest", "oldest", "title"] as const;
|
|
574
578
|
|
|
@@ -769,6 +773,9 @@ const KnownLayoutNode = z.discriminatedUnion("type", [
|
|
|
769
773
|
MenuItemNode,
|
|
770
774
|
]);
|
|
771
775
|
|
|
776
|
+
/** One strict schema per known element type, for the MCP element catalogue (W-453). */
|
|
777
|
+
export const KNOWN_NODE_SCHEMAS = KnownLayoutNode.options;
|
|
778
|
+
|
|
772
779
|
export type HeadingNode = z.infer<typeof HeadingNode>;
|
|
773
780
|
export type TocNode = z.infer<typeof TocNode>;
|
|
774
781
|
export type SpacerNode = z.infer<typeof SpacerNode>;
|
package/src/core/share/link.ts
CHANGED
|
@@ -151,7 +151,10 @@ export async function decodeSharedPage(encoded: string): Promise<ShareRead> {
|
|
|
151
151
|
const text = encoded.trim();
|
|
152
152
|
if (!text) return { ok: false, reason: "empty" };
|
|
153
153
|
if (text.length > MAX_LINK_CHARS) return { ok: false, reason: "too-long" };
|
|
154
|
-
const [version,
|
|
154
|
+
const [version, raw] = text.split(".", 2);
|
|
155
|
+
// W-437: mail clients wrap long links and chat apps take a closing ")" or "." into them; none
|
|
156
|
+
// of those characters is base64url, so line breaks inside and trailing punctuation are dropped.
|
|
157
|
+
const body = raw?.replace(/\s+/g, "").replace(/[^A-Za-z0-9_-]+$/, "");
|
|
155
158
|
if (version !== VERSION || !body || !/^[A-Za-z0-9_-]+$/.test(body)) {
|
|
156
159
|
return { ok: false, reason: "unreadable" };
|
|
157
160
|
}
|
|
@@ -110,12 +110,13 @@ function spanNode(
|
|
|
110
110
|
? span["marks"].filter((m) => typeof m === "string")
|
|
111
111
|
: [];
|
|
112
112
|
let node: VNode | string = text;
|
|
113
|
-
|
|
113
|
+
// W-392: a link can't hold a link, so only the span's first (outermost) link mark is kept.
|
|
114
|
+
const link = marks.find((mark) => !DECORATORS[mark] && links.has(mark));
|
|
115
|
+
for (const mark of new Set(marks.toReversed())) {
|
|
114
116
|
const tag = DECORATORS[mark];
|
|
115
117
|
if (tag) node = { tag, attrs: {}, children: [node] };
|
|
116
|
-
else {
|
|
117
|
-
|
|
118
|
-
if (href) node = { tag: "a", attrs: { href }, children: [node] };
|
|
118
|
+
else if (mark === link) {
|
|
119
|
+
node = { tag: "a", attrs: { href: links.get(mark) as string }, children: [node] };
|
|
119
120
|
}
|
|
120
121
|
}
|
|
121
122
|
return node;
|
|
@@ -254,7 +255,11 @@ const MAX_LOOP_FILTERS = 5;
|
|
|
254
255
|
*/
|
|
255
256
|
export function parseLoopFilter(text: unknown): Record<string, string | string[]> | undefined {
|
|
256
257
|
if (typeof text !== "string" || !LOOP_FILTER.test(text)) return undefined;
|
|
257
|
-
|
|
258
|
+
// W-391: a field named like an Object method (`constructor`) starts empty too.
|
|
259
|
+
const where: Record<string, string | string[]> = Object.create(null) as Record<
|
|
260
|
+
string,
|
|
261
|
+
string | string[]
|
|
262
|
+
>;
|
|
258
263
|
let count = 0;
|
|
259
264
|
for (const pair of text.split(";")) {
|
|
260
265
|
const at = pair.indexOf("=");
|
|
@@ -263,10 +268,10 @@ export function parseLoopFilter(text: unknown): Record<string, string | string[]
|
|
|
263
268
|
const value = pair.slice(at + 1).trim();
|
|
264
269
|
if (!field || !value || count >= MAX_LOOP_FILTERS) continue;
|
|
265
270
|
count += 1;
|
|
266
|
-
const had = where[field];
|
|
271
|
+
const had = Object.hasOwn(where, field) ? where[field] : undefined;
|
|
267
272
|
where[field] = had === undefined ? value : [...(Array.isArray(had) ? had : [had]), value];
|
|
268
273
|
}
|
|
269
|
-
return count > 0 ? where : undefined;
|
|
274
|
+
return count > 0 ? Object.fromEntries(Object.entries(where)) : undefined;
|
|
270
275
|
}
|
|
271
276
|
|
|
272
277
|
/** An entry's link from a Loop's pattern (W-330): `{slug}` and `{id}` filled in, URL-encoded. */
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/** Error codes an MCP tool reports back to the agent (plan § 2 Errors). */
|
|
2
|
+
export type McpErrorCode =
|
|
3
|
+
| "INVALID_INPUT"
|
|
4
|
+
| "INVALID_LAYOUT"
|
|
5
|
+
| "NESTING"
|
|
6
|
+
| "NOT_FOUND"
|
|
7
|
+
| "CONFLICT"
|
|
8
|
+
| "PUBLISH_DISABLED"
|
|
9
|
+
| "FORBIDDEN"
|
|
10
|
+
| "TOO_LARGE"
|
|
11
|
+
| "REFUSED"
|
|
12
|
+
| "NOT_SET_UP"
|
|
13
|
+
| "RATE_LIMITED";
|
|
14
|
+
|
|
15
|
+
const STATUS: Record<McpErrorCode, number> = {
|
|
16
|
+
INVALID_INPUT: 400,
|
|
17
|
+
INVALID_LAYOUT: 422,
|
|
18
|
+
NESTING: 422,
|
|
19
|
+
NOT_FOUND: 404,
|
|
20
|
+
CONFLICT: 409,
|
|
21
|
+
PUBLISH_DISABLED: 403,
|
|
22
|
+
FORBIDDEN: 403,
|
|
23
|
+
TOO_LARGE: 413,
|
|
24
|
+
REFUSED: 422,
|
|
25
|
+
NOT_SET_UP: 409,
|
|
26
|
+
RATE_LIMITED: 429,
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A tool failure the agent can act on. Routes turn it into a `PluginRouteError`, which EmDash's
|
|
31
|
+
* MCP server reports as a tool result with `isError: true`, not a protocol error (SEP-1303).
|
|
32
|
+
*/
|
|
33
|
+
export class McpToolError extends Error {
|
|
34
|
+
readonly code: McpErrorCode;
|
|
35
|
+
readonly status: number;
|
|
36
|
+
readonly details?: Record<string, unknown>;
|
|
37
|
+
constructor(code: McpErrorCode, message: string, details?: Record<string, unknown>) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = "McpToolError";
|
|
40
|
+
this.code = code;
|
|
41
|
+
this.status = STATUS[code];
|
|
42
|
+
if (details) this.details = details;
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/mcp/index.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** `@perkyzz/emvb/mcp`: the shared MCP tool definitions the stdio bridge lists (D-053). */
|
|
2
|
+
export { TOOLS, visibleTools, type ToolDef } from "./tools.ts";
|
|
3
|
+
export { RATE_LIMITS, type RateClass } from "./rate-limit.ts";
|
|
4
|
+
export { PROMPTS, designJsonSchema, layoutJsonSchema, type PromptDef } from "./prompts.ts";
|
package/src/mcp/ports.ts
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import type { AgentSettings } from "./settings.ts";
|
|
2
|
+
import type { RateClass } from "./rate-limit.ts";
|
|
3
|
+
import type { DesignSystem, Layout } from "../core/index.ts";
|
|
4
|
+
|
|
5
|
+
/** A page as the tool core sees it: the current draft (or published) data and its revision. */
|
|
6
|
+
export type PageRecord = {
|
|
7
|
+
id: string;
|
|
8
|
+
title: string;
|
|
9
|
+
slug: string;
|
|
10
|
+
status: string;
|
|
11
|
+
/** True when a saved draft differs from what is live. */
|
|
12
|
+
hasDraft: boolean;
|
|
13
|
+
layout: unknown;
|
|
14
|
+
/** EmDash `_rev`; writes check it (D-058). */
|
|
15
|
+
rev: string;
|
|
16
|
+
updatedAt: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type PageSummary = Omit<PageRecord, "layout" | "rev">;
|
|
20
|
+
|
|
21
|
+
export type RevisionSummary = { id: string; createdAt: string; authorId: string | null };
|
|
22
|
+
|
|
23
|
+
export type DesignState = {
|
|
24
|
+
design: DesignSystem;
|
|
25
|
+
revision: string | null;
|
|
26
|
+
publishedRevision: string | null;
|
|
27
|
+
unpublished: boolean;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/** Who is calling. EmDash has already checked the token scope and the editor role (D-056). */
|
|
31
|
+
export type Caller = { userId: string | null; role: number; client?: string };
|
|
32
|
+
|
|
33
|
+
/** One agent change, shown in the Timeline and the Agents tab (D-057). */
|
|
34
|
+
export type AgentLogEntry = {
|
|
35
|
+
target: "page" | "styles";
|
|
36
|
+
tool: string;
|
|
37
|
+
pageId?: string;
|
|
38
|
+
rev?: string;
|
|
39
|
+
revisionId?: string;
|
|
40
|
+
summary: string;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Everything the tool core reads or writes goes through these ports (D-053). The plugin builds
|
|
45
|
+
* them on `ctx.content` and plugin storage; tests build them in memory.
|
|
46
|
+
*/
|
|
47
|
+
export interface EmvbPorts {
|
|
48
|
+
caller: Caller;
|
|
49
|
+
/** Counts the call against the caller's rate limit; throws RATE_LIMITED when full (W-478). */
|
|
50
|
+
rateLimit?(cls: RateClass): Promise<void>;
|
|
51
|
+
listPages(options: { cursor?: string; limit: number }): Promise<{
|
|
52
|
+
pages: PageSummary[];
|
|
53
|
+
nextCursor?: string;
|
|
54
|
+
}>;
|
|
55
|
+
getPage(id: string): Promise<PageRecord | null>;
|
|
56
|
+
listRevisions(id: string, limit: number): Promise<RevisionSummary[]>;
|
|
57
|
+
getDesign(): Promise<DesignState>;
|
|
58
|
+
getRevisionData(pageId: string, revisionId: string): Promise<Record<string, unknown> | null>;
|
|
59
|
+
/** Writes the draft when the stored `_rev` is still `expectedRev`; else McpToolError CONFLICT. */
|
|
60
|
+
savePage(
|
|
61
|
+
id: string,
|
|
62
|
+
patch: { layout?: Layout; title?: string },
|
|
63
|
+
expectedRev: string,
|
|
64
|
+
): Promise<{ rev: string; revisionId?: string }>;
|
|
65
|
+
createPage(input: {
|
|
66
|
+
title: string;
|
|
67
|
+
layout: Layout;
|
|
68
|
+
}): Promise<{ id: string; slug: string; rev: string }>;
|
|
69
|
+
/** Saves the style draft with compare-and-set; McpToolError CONFLICT when it moved. */
|
|
70
|
+
saveDesign(design: DesignSystem, expectedRevision: string | null): Promise<{ revision: string }>;
|
|
71
|
+
log(entry: AgentLogEntry): Promise<void>;
|
|
72
|
+
/** The admin's agent settings (D-055). Read-only for tools. */
|
|
73
|
+
settings(): Promise<AgentSettings>;
|
|
74
|
+
/** Publishes the page when its `_rev` is still `expectedRev`; else McpToolError CONFLICT. */
|
|
75
|
+
publishPage(id: string, expectedRev: string): Promise<{ rev: string }>;
|
|
76
|
+
/** Publishes the style draft when the published revision is still `expectedPublishedRevision`. */
|
|
77
|
+
publishDesign(expectedPublishedRevision: string | null): Promise<{ revision: string }>;
|
|
78
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { DesignSystem } from "../core/schema/design.ts";
|
|
3
|
+
import { Layout } from "../core/schema/layout.ts";
|
|
4
|
+
|
|
5
|
+
/** JSON Schema (2020-12) of a stored layout, for `emvb://schema/layout` (W-467). */
|
|
6
|
+
export const layoutJsonSchema = () =>
|
|
7
|
+
z.toJSONSchema(Layout, { io: "input", unrepresentable: "any" });
|
|
8
|
+
|
|
9
|
+
/** JSON Schema of the design system (site styles), for `emvb://schema/design`. */
|
|
10
|
+
export const designJsonSchema = () =>
|
|
11
|
+
z.toJSONSchema(DesignSystem, { io: "input", unrepresentable: "any" });
|
|
12
|
+
|
|
13
|
+
export type PromptDef = {
|
|
14
|
+
name: string;
|
|
15
|
+
title: string;
|
|
16
|
+
description: string;
|
|
17
|
+
args: Record<string, { description: string; required: boolean }>;
|
|
18
|
+
text(args: Record<string, string | undefined>): string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const list = (value: string | undefined) =>
|
|
22
|
+
value ? ` Sections to include: ${JSON.stringify(value)}.` : "";
|
|
23
|
+
|
|
24
|
+
/** Arguments are the person's data: quoted as JSON strings, never spliced in as instructions. */
|
|
25
|
+
const q = (value: string | undefined) => JSON.stringify(value ?? "");
|
|
26
|
+
|
|
27
|
+
/** Workflow prompts (plan § 3 Prompts). They only describe steps; the tools do the work. */
|
|
28
|
+
export const PROMPTS: PromptDef[] = [
|
|
29
|
+
{
|
|
30
|
+
name: "build_landing_page",
|
|
31
|
+
title: "Build a landing page",
|
|
32
|
+
description:
|
|
33
|
+
"Plan and build a new draft landing page from a topic, ready for a person to review.",
|
|
34
|
+
args: {
|
|
35
|
+
topic: { description: "What the page is about", required: true },
|
|
36
|
+
audience: { description: "Who it is for", required: false },
|
|
37
|
+
sections: { description: "Sections to include, comma-separated", required: false },
|
|
38
|
+
},
|
|
39
|
+
text: (a) =>
|
|
40
|
+
`Build a draft EmVB landing page about ${q(a["topic"])}${a["audience"] ? ` for ${q(a["audience"])}` : ""}.${list(a["sections"])}
|
|
41
|
+
1. Call get_site_styles and list_section_recipes. If the site has no Brand, Ink, Muted and Surface colours or fs-*/space-* sizes, propose them first (see the skill's design reference); recipes and your styles use them.
|
|
42
|
+
2. Call create_page with a clear title and the recipes that fit, hero first: the hero heading becomes the page's h1.
|
|
43
|
+
3. Replace placeholder copy with update_element, passing the rev each write returns as the next expectedRev. Real copy, real links, alt text that describes each image.
|
|
44
|
+
4. Call get_accessibility_findings and fix every error.
|
|
45
|
+
5. Do not publish. Finish with describe_page and a short summary for the person to review.`,
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
name: "fix_accessibility",
|
|
49
|
+
title: "Fix accessibility findings",
|
|
50
|
+
description: "Fix the accessibility findings on one page, then re-check.",
|
|
51
|
+
args: { pageId: { description: "The page id (from list_pages)", required: true } },
|
|
52
|
+
text: (a) =>
|
|
53
|
+
`Fix the accessibility findings on page ${q(a["pageId"])}.
|
|
54
|
+
1. Call get_layout (format "outline") for the rev, then get_accessibility_findings.
|
|
55
|
+
2. For each finding, apply its fix with update_element on its nodeId: fix.kind "heading-level" → set.props.level; "text-color" → set.style.color; "decorative" → set.props.decorative true, but only when the image adds nothing; otherwise set.props.alt to words that describe it; "drop-aria-hidden" → set.attributes to its list without the aria-hidden entry. A finding without a fix: follow its message.
|
|
56
|
+
3. Pass the rev from your last read as expectedRev on the first write, then the rev each write returns on the next one.
|
|
57
|
+
4. When several contrast findings share one site colour, darken or lighten that colour variable once with update_site_styles instead of overriding each element; keep the design's palette. Keep sibling headings at the same level.
|
|
58
|
+
5. Call get_accessibility_findings again and report what is left. Do not publish.`,
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: "restyle_site",
|
|
62
|
+
title: "Restyle the site",
|
|
63
|
+
description: "Propose and apply site style changes to the style draft only.",
|
|
64
|
+
args: { brief: { description: "The look you want", required: true } },
|
|
65
|
+
text: (a) =>
|
|
66
|
+
`Restyle the site to match: ${q(a["brief"])}. Call get_site_styles, propose variable and class changes, then apply them with update_site_styles (draft only). Keep the variable names recipes look for (Brand, Ink, Muted, Surface, On brand; fs-* and space-* ids; Button and Card classes). Check text colours against their backgrounds for 4.5:1 contrast. Never remove a variable that is in use. Do not publish; a person reviews the style draft.`,
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
name: "review_page",
|
|
70
|
+
title: "Review a page",
|
|
71
|
+
description: "Write a review of one page without changing it.",
|
|
72
|
+
args: { pageId: { description: "The page id (from list_pages)", required: true } },
|
|
73
|
+
text: (a) =>
|
|
74
|
+
`Review page ${q(a["pageId"])} without editing it: call describe_page (sections, heading outline, images with their alt text) and get_accessibility_findings, and get_layout when you need styles or ids. Then write a short review covering structure, copy, images and alt text, accessibility, and consistency with the site styles, with the element ids for each suggestion.`,
|
|
75
|
+
},
|
|
76
|
+
];
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { McpToolError } from "./errors.ts";
|
|
3
|
+
import type { EmvbPorts } from "./ports.ts";
|
|
4
|
+
import { queued } from "./queue.ts";
|
|
5
|
+
|
|
6
|
+
const off = (what: string) =>
|
|
7
|
+
new McpToolError(
|
|
8
|
+
"PUBLISH_DISABLED",
|
|
9
|
+
`Agents may not publish ${what} on this site. Leave the draft for a person to review, or ask an admin to allow it in Pages VisualBuilder → AI agents.`,
|
|
10
|
+
);
|
|
11
|
+
|
|
12
|
+
export const PublishPageInput = z.strictObject({
|
|
13
|
+
pageId: z.string().min(1).max(128),
|
|
14
|
+
/** Required: an agent can't publish a draft it hasn't read (plan § 3). */
|
|
15
|
+
expectedRev: z.string().min(1).max(256),
|
|
16
|
+
confirm: z.literal(true),
|
|
17
|
+
});
|
|
18
|
+
export const PublishPageOutput = z.object({
|
|
19
|
+
pageId: z.string(),
|
|
20
|
+
rev: z.string(),
|
|
21
|
+
summary: z.string(),
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
/** D-055: publishes one page only when the admin allowed agent publishing. */
|
|
25
|
+
export async function publishPage(ports: EmvbPorts, input: z.infer<typeof PublishPageInput>) {
|
|
26
|
+
if (!(await ports.settings()).publishPages) throw off("pages");
|
|
27
|
+
return queued(`page:${input.pageId}`, async () => {
|
|
28
|
+
const published = await ports.publishPage(input.pageId, input.expectedRev);
|
|
29
|
+
const summary = `Published page ${input.pageId}`;
|
|
30
|
+
await ports.log({
|
|
31
|
+
target: "page",
|
|
32
|
+
tool: "publish_page",
|
|
33
|
+
pageId: input.pageId,
|
|
34
|
+
rev: published.rev,
|
|
35
|
+
summary,
|
|
36
|
+
});
|
|
37
|
+
return { pageId: input.pageId, rev: published.rev, summary };
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export const PublishSiteStylesInput = z.strictObject({
|
|
42
|
+
/** The `publishedRevision` from get_site_styles (null when nothing was ever published). */
|
|
43
|
+
expectedPublishedRevision: z.string().max(256).nullable(),
|
|
44
|
+
confirm: z.literal(true),
|
|
45
|
+
});
|
|
46
|
+
export const PublishSiteStylesOutput = z.object({ revision: z.string(), summary: z.string() });
|
|
47
|
+
|
|
48
|
+
/** D-055: publishes the style draft only when the admin allowed it. Pages are not republished. */
|
|
49
|
+
export async function publishSiteStyles(
|
|
50
|
+
ports: EmvbPorts,
|
|
51
|
+
input: z.infer<typeof PublishSiteStylesInput>,
|
|
52
|
+
) {
|
|
53
|
+
if (!(await ports.settings()).publishStyles) throw off("site styles");
|
|
54
|
+
return queued("styles", async () => {
|
|
55
|
+
const published = await ports.publishDesign(input.expectedPublishedRevision);
|
|
56
|
+
const summary = "Published the site styles";
|
|
57
|
+
await ports.log({ target: "styles", tool: "publish_site_styles", summary });
|
|
58
|
+
return { revision: published.revision, summary };
|
|
59
|
+
});
|
|
60
|
+
}
|
package/src/mcp/queue.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* One write at a time per key in this isolate (D-058), like the editor's save queue: a write
|
|
3
|
+
* reads the latest `_rev` only after the previous write to the same page has finished.
|
|
4
|
+
*/
|
|
5
|
+
const tails = new Map<string, Promise<unknown>>();
|
|
6
|
+
|
|
7
|
+
export function queued<T>(key: string, work: () => Promise<T>): Promise<T> {
|
|
8
|
+
const previous = tails.get(key) ?? Promise.resolve();
|
|
9
|
+
const next = previous.then(work, work);
|
|
10
|
+
const settled = next.then(
|
|
11
|
+
() => undefined,
|
|
12
|
+
() => undefined,
|
|
13
|
+
);
|
|
14
|
+
tails.set(key, settled);
|
|
15
|
+
void settled.then(() => {
|
|
16
|
+
if (tails.get(key) === settled) tails.delete(key);
|
|
17
|
+
});
|
|
18
|
+
return next;
|
|
19
|
+
}
|