@perkyzz/emvb 0.3.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 +22 -4
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/package.json +5 -2
- package/src/admin/access/role.ts +9 -3
- package/src/admin/agents-api.ts +39 -0
- package/src/admin/api.ts +17 -7
- package/src/admin/content-api.ts +7 -51
- package/src/admin/content-item.ts +22 -0
- package/src/admin/design-api.ts +30 -0
- package/src/admin/editor/A11yCopilot.tsx +1 -1
- package/src/admin/editor/CommandPalette.tsx +43 -2
- package/src/admin/editor/Editor.tsx +133 -332
- package/src/admin/editor/EditorOverlay.tsx +3 -1
- package/src/admin/editor/PerfMeter.tsx +1 -1
- package/src/admin/editor/ShortcutsDialog.tsx +4 -1
- package/src/admin/editor/Timeline.tsx +124 -9
- package/src/admin/editor/canvas/CanvasFrame.tsx +51 -368
- package/src/admin/editor/canvas/DeviceBar.tsx +4 -1
- package/src/admin/editor/canvas/SelectionOverlay.tsx +86 -46
- package/src/admin/editor/canvas/canvas-scale.ts +20 -0
- package/src/admin/editor/canvas/drop-line.ts +24 -0
- package/src/admin/editor/canvas/use-canvas-dnd.ts +207 -0
- package/src/admin/editor/canvas/use-canvas-session.ts +216 -0
- package/src/admin/editor/collection-previews.ts +28 -5
- package/src/admin/editor/editor-chrome.tsx +346 -0
- package/src/admin/editor/editor-commands.ts +360 -0
- package/src/admin/editor/editor-css.ts +4 -0
- package/src/admin/editor/element-chrome.ts +89 -0
- package/src/admin/editor/feature-css.ts +37 -18
- package/src/admin/editor/host.ts +33 -0
- package/src/admin/editor/icons/IconLibraryDialog.tsx +64 -218
- package/src/admin/editor/icons/library.ts +29 -19
- package/src/admin/editor/icons/use-icon-library.ts +315 -0
- package/src/admin/editor/palette-items.ts +36 -5
- package/src/admin/editor/palette.ts +9 -4
- package/src/admin/editor/panels/AddPanel.tsx +2 -67
- package/src/admin/editor/panels/ColorControl.tsx +21 -18
- package/src/admin/editor/panels/ElementPanel.tsx +40 -99
- package/src/admin/editor/panels/LayersPanel.tsx +34 -62
- package/src/admin/editor/panels/SettingsPanel.tsx +1 -0
- package/src/admin/editor/panels/SiteStylesDrawer.tsx +16 -10
- package/src/admin/editor/panels/TokenScales.tsx +4 -2
- package/src/admin/editor/panels/VariableSection.tsx +1 -10
- package/src/admin/editor/panels/color-space.ts +12 -1
- package/src/admin/editor/panels/inline-error.tsx +11 -0
- package/src/admin/editor/panels/settings/AdvancedFields.tsx +143 -1
- package/src/admin/editor/panels/settings/AudienceEditor.tsx +115 -7
- package/src/admin/editor/panels/settings/DraftTextField.tsx +42 -9
- package/src/admin/editor/panels/settings/FieldControl.tsx +6 -0
- package/src/admin/editor/panels/settings/FormBindControl.tsx +8 -4
- 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/ItemList.tsx +34 -1
- 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/StyleRow.tsx +14 -2
- package/src/admin/editor/panels/settings/advanced-rules.ts +3 -0
- package/src/admin/editor/panels/settings/form-names.ts +7 -11
- package/src/admin/editor/panels/settings/length-units.ts +15 -4
- 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/settings/style-sections.ts +5 -1
- package/src/admin/editor/panels/style-target.ts +105 -0
- package/src/admin/editor/panels/variable-values.ts +4 -2
- package/src/admin/editor/remote-change.ts +67 -0
- package/src/admin/editor/section-templates.ts +43 -21
- package/src/admin/editor/shortcuts.ts +4 -0
- package/src/admin/editor/store.ts +10 -1
- package/src/admin/editor/useClipboardActions.ts +1 -1
- package/src/admin/editor/useEditorCommands.ts +61 -13
- package/src/admin/editor/useEditorData.ts +7 -1
- package/src/admin/editor/useEditorShortcuts.ts +23 -15
- package/src/admin/editor/useNodeActions.ts +6 -1
- package/src/admin/forms-api.ts +17 -6
- package/src/admin/icon-uploads-api.ts +18 -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/theme-api.ts +5 -18
- package/src/admin/ui.ts +28 -0
- package/src/astro/content-port.ts +24 -0
- package/src/astro/floats/runtime.ts +10 -0
- package/src/astro/forms/EmVBFormsClient.astro +11 -0
- package/src/astro/forms/EmVBFormsRuntime.astro +6 -7
- package/src/astro/forms/client.ts +1 -0
- package/src/astro/forms-definitions.ts +24 -11
- package/src/astro/index.ts +1 -0
- package/src/astro/popups/runtime.ts +15 -0
- package/src/astro/render-session.ts +85 -0
- package/src/astro/request-data.ts +20 -8
- package/src/astro/resolve-theme.ts +166 -76
- package/src/astro/resolve.ts +57 -16
- package/src/astro/tabs/runtime.ts +60 -1
- package/src/astro/theme-posts.ts +16 -5
- package/src/astro/visitor.ts +67 -5
- package/src/constants.ts +1 -1
- package/src/core/a11y/audit.ts +202 -26
- package/src/core/arrange.ts +71 -38
- package/src/core/audience/rules.ts +22 -2
- package/src/core/canvas-styles.ts +24 -0
- package/src/core/clipboard.ts +11 -32
- package/src/core/css/generate.ts +18 -1
- package/src/core/css/print.ts +12 -0
- package/src/core/css/scope.ts +3 -3
- package/src/core/css-keywords.ts +83 -0
- package/src/core/data/bindings.ts +4 -6
- package/src/core/design/classes.ts +13 -15
- package/src/core/design/diff.ts +55 -0
- package/src/core/design/merge.ts +127 -0
- package/src/core/design/variables.ts +25 -21
- package/src/core/elements/dynamic-elements.ts +30 -0
- package/src/core/elements/form-elements.ts +20 -3
- package/src/core/elements/index.ts +211 -7
- package/src/core/forms/binding.ts +4 -13
- package/src/core/index.ts +10 -0
- package/src/core/local-style.ts +27 -0
- package/src/core/migrate/index.ts +9 -4
- package/src/core/nesting.ts +4 -23
- package/src/core/perf/scripts.ts +9 -6
- package/src/core/record.ts +3 -0
- package/src/core/render/context.ts +2 -0
- package/src/core/render/dynamic-post.ts +22 -7
- package/src/core/render/form.ts +145 -15
- package/src/core/render/index.ts +195 -14
- package/src/core/render/pagination.ts +21 -2
- package/src/core/render/vnode.ts +12 -2
- package/src/core/sanitize/css.ts +90 -77
- package/src/core/sanitize/embed-url.ts +7 -1
- package/src/core/sanitize/href.ts +13 -1
- package/src/core/sanitize/svg.ts +8 -3
- package/src/core/sanitize/url.ts +10 -1
- package/src/core/schema/descriptors.ts +4 -1
- package/src/core/schema/design.ts +5 -7
- package/src/core/schema/layout.ts +160 -181
- package/src/core/schema/style.ts +53 -63
- package/src/core/share/link.ts +72 -29
- package/src/core/tabs/presence.ts +7 -17
- package/src/core/theme/dynamic.ts +205 -39
- package/src/core/theme/float.ts +1 -1
- package/src/core/tree-ops.ts +57 -0
- package/src/index.ts +14 -2
- 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 +75 -18
- package/src/server/icon-routes.ts +72 -16
- package/src/server/mcp-routes.ts +265 -0
- package/src/server/plugin.ts +40 -4
- package/src/admin/editor/icons/sets/fontawesome.json +0 -1
- package/src/admin/editor/icons/sets/lucide.json +0 -1
- package/src/admin/editor/icons/sets/remix.json +0 -1
- package/src/admin/editor/icons/sets/tabler.json +0 -1
package/src/mcp/write.ts
ADDED
|
@@ -0,0 +1,582 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import {
|
|
3
|
+
byteLength,
|
|
4
|
+
defaultElement,
|
|
5
|
+
findNode,
|
|
6
|
+
MAX_LAYOUT_BYTES,
|
|
7
|
+
nestingIssues,
|
|
8
|
+
recipeNode,
|
|
9
|
+
SECTION_RECIPES,
|
|
10
|
+
summarizeIssues,
|
|
11
|
+
validateDesign,
|
|
12
|
+
validateLayout,
|
|
13
|
+
type DesignSystem,
|
|
14
|
+
type ElementType,
|
|
15
|
+
type Layout,
|
|
16
|
+
type LayoutNode,
|
|
17
|
+
} from "../core/index.ts";
|
|
18
|
+
import { addNode, moveNode, withFreshIds } from "../core/arrange.ts";
|
|
19
|
+
import { ELEMENT_DESCRIPTORS } from "../core/elements/index.ts";
|
|
20
|
+
import { removeNode, slugify, starterLayout, updateNode } from "../core/tree-ops.ts";
|
|
21
|
+
import { McpToolError } from "./errors.ts";
|
|
22
|
+
import type { EmvbPorts } from "./ports.ts";
|
|
23
|
+
import { queued } from "./queue.ts";
|
|
24
|
+
import { resolveDesignRefs } from "./refs.ts";
|
|
25
|
+
import {
|
|
26
|
+
findVariableUsages,
|
|
27
|
+
findVariableUsagesInDesign,
|
|
28
|
+
type VariableKind,
|
|
29
|
+
} from "../core/design/variables.ts";
|
|
30
|
+
|
|
31
|
+
const PageId = z.string().min(1).max(128);
|
|
32
|
+
const NodeIdInput = z.string().min(1).max(64);
|
|
33
|
+
const Rev = z.string().min(1).max(256);
|
|
34
|
+
|
|
35
|
+
/** The editor's save checks, run here because plugin writes skip `content:beforeSave` (R1 P0b). */
|
|
36
|
+
function checkLayout(candidate: unknown): Layout {
|
|
37
|
+
// Size first, so an oversized page reports TOO_LARGE rather than a generic INVALID_LAYOUT.
|
|
38
|
+
const size = byteLength(candidate);
|
|
39
|
+
if (size > MAX_LAYOUT_BYTES) {
|
|
40
|
+
throw new McpToolError(
|
|
41
|
+
"TOO_LARGE",
|
|
42
|
+
`The page would be ${size} bytes; the limit is ${MAX_LAYOUT_BYTES}. Split it into smaller pages or remove content.`,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
const result = validateLayout(candidate);
|
|
46
|
+
if (!result.ok) {
|
|
47
|
+
throw new McpToolError(
|
|
48
|
+
"INVALID_LAYOUT",
|
|
49
|
+
`The layout is invalid. ${summarizeIssues(result.issues)}`,
|
|
50
|
+
{ issues: result.issues.slice(0, 10) },
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const misplaced = nestingIssues(result.layout, 3);
|
|
54
|
+
if (misplaced.length > 0) {
|
|
55
|
+
throw new McpToolError(
|
|
56
|
+
"NESTING",
|
|
57
|
+
`Elements are in the wrong place. ${misplaced.map((issue) => issue.message).join("; ")}`,
|
|
58
|
+
);
|
|
59
|
+
}
|
|
60
|
+
const bytes = byteLength(result.layout);
|
|
61
|
+
if (bytes > MAX_LAYOUT_BYTES) {
|
|
62
|
+
throw new McpToolError(
|
|
63
|
+
"TOO_LARGE",
|
|
64
|
+
`The page would be ${bytes} bytes; the limit is ${MAX_LAYOUT_BYTES}.`,
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
return result.layout;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
type WriteResult = { pageId: string; rev: string; changed: string[]; summary: string };
|
|
71
|
+
|
|
72
|
+
export const WriteOutput = z.object({
|
|
73
|
+
pageId: z.string(),
|
|
74
|
+
rev: z.string(),
|
|
75
|
+
changed: z.array(z.string()),
|
|
76
|
+
summary: z.string(),
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Read → apply → validate → save with `_rev`, one write per page at a time (D-058). Without
|
|
81
|
+
* `expectedRev`, a conflict re-reads and re-applies the operation once; with it, never.
|
|
82
|
+
*/
|
|
83
|
+
async function mutatePage(
|
|
84
|
+
ports: EmvbPorts,
|
|
85
|
+
tool: string,
|
|
86
|
+
pageId: string,
|
|
87
|
+
expectedRev: string | undefined,
|
|
88
|
+
apply: (layout: Layout) => { layout: Layout; changed: string[]; summary: string },
|
|
89
|
+
): Promise<WriteResult> {
|
|
90
|
+
return queued(`page:${pageId}`, async () => {
|
|
91
|
+
// The retry must read, write and log in order.
|
|
92
|
+
/* oxlint-disable no-await-in-loop */
|
|
93
|
+
for (let attempt = 0; ; attempt++) {
|
|
94
|
+
const page = await ports.getPage(pageId);
|
|
95
|
+
if (!page)
|
|
96
|
+
throw new McpToolError("NOT_FOUND", `No EmVB page with id ${pageId}. Call list_pages.`);
|
|
97
|
+
if (expectedRev !== undefined && page.rev !== expectedRev) {
|
|
98
|
+
throw new McpToolError(
|
|
99
|
+
"CONFLICT",
|
|
100
|
+
"The page changed since you read it. Every write returns a new rev: pass that one to your next write. If someone else changed it, call get_layout again and retry.",
|
|
101
|
+
{ rev: page.rev },
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
const current = validateLayout(page.layout);
|
|
105
|
+
if (!current.ok) {
|
|
106
|
+
throw new McpToolError(
|
|
107
|
+
"INVALID_LAYOUT",
|
|
108
|
+
"The stored layout can't be read. Repair it in the editor.",
|
|
109
|
+
);
|
|
110
|
+
}
|
|
111
|
+
const next = apply(current.layout);
|
|
112
|
+
const layout = checkLayout(next.layout);
|
|
113
|
+
try {
|
|
114
|
+
const saved = await ports.savePage(pageId, { layout }, page.rev);
|
|
115
|
+
await ports.log({
|
|
116
|
+
target: "page",
|
|
117
|
+
tool,
|
|
118
|
+
pageId,
|
|
119
|
+
rev: saved.rev,
|
|
120
|
+
...(saved.revisionId ? { revisionId: saved.revisionId } : {}),
|
|
121
|
+
summary: next.summary,
|
|
122
|
+
});
|
|
123
|
+
return { pageId, rev: saved.rev, changed: next.changed, summary: next.summary };
|
|
124
|
+
} catch (error) {
|
|
125
|
+
const retry = error instanceof McpToolError && error.code === "CONFLICT";
|
|
126
|
+
if (!retry || expectedRev !== undefined || attempt >= 1) throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const refused = (reason: string): never => {
|
|
133
|
+
throw new McpToolError("REFUSED", reason);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const KNOWN_TYPES = new Set(ELEMENT_DESCRIPTORS.map((d) => d.type));
|
|
137
|
+
|
|
138
|
+
/** An element from the agent: its type's defaults, the given props and fields, fresh ids. */
|
|
139
|
+
const ElementSpec: z.ZodType<ElementSpecT> = z.lazy(() =>
|
|
140
|
+
z.looseObject({
|
|
141
|
+
type: z.string().min(1).max(64),
|
|
142
|
+
props: z.record(z.string(), z.unknown()).optional(),
|
|
143
|
+
children: z.array(ElementSpec).max(200).optional(),
|
|
144
|
+
}),
|
|
145
|
+
);
|
|
146
|
+
type ElementSpecT = { type: string; props?: Record<string, unknown>; children?: ElementSpecT[] };
|
|
147
|
+
|
|
148
|
+
function buildNode(spec: ElementSpecT): LayoutNode {
|
|
149
|
+
if (!KNOWN_TYPES.has(spec.type)) {
|
|
150
|
+
refused(`Unknown element type ${spec.type}. Call list_element_types.`);
|
|
151
|
+
}
|
|
152
|
+
const { type, props, children, ...rest } = spec;
|
|
153
|
+
delete (rest as { id?: unknown }).id;
|
|
154
|
+
const base = defaultElement(type as ElementType, "new") as LayoutNode & {
|
|
155
|
+
props?: Record<string, unknown>;
|
|
156
|
+
};
|
|
157
|
+
const node = { ...base, ...rest, props: { ...base.props, ...props } } as LayoutNode;
|
|
158
|
+
if (children) (node as { children: LayoutNode[] }).children = children.map(buildNode);
|
|
159
|
+
return node;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const arranged = (result: { ok: true; layout: Layout } | { ok: false; reason: string }): Layout =>
|
|
163
|
+
result.ok ? result.layout : refused(result.reason);
|
|
164
|
+
|
|
165
|
+
// ---------------------------------------------------------------- create_page
|
|
166
|
+
|
|
167
|
+
export const CreatePageInput = z.strictObject({
|
|
168
|
+
title: z.string().trim().min(1).max(200),
|
|
169
|
+
recipes: z
|
|
170
|
+
.array(z.enum(SECTION_RECIPES.map((r) => r.id) as [string, ...string[]]))
|
|
171
|
+
.max(12)
|
|
172
|
+
.optional(),
|
|
173
|
+
});
|
|
174
|
+
export const CreatePageOutput = z.object({ pageId: z.string(), slug: z.string(), rev: z.string() });
|
|
175
|
+
|
|
176
|
+
/** The first heading inside `node`, depth first. */
|
|
177
|
+
const firstHeading = (node: LayoutNode): LayoutNode | undefined =>
|
|
178
|
+
node.type === "heading"
|
|
179
|
+
? node
|
|
180
|
+
: ("children" in node && Array.isArray(node.children) ? node.children : [])
|
|
181
|
+
.map(firstHeading)
|
|
182
|
+
.find(Boolean);
|
|
183
|
+
|
|
184
|
+
/** The hero's first heading becomes the page's h1 with the page title as its text. */
|
|
185
|
+
function promoteHeroHeading(layout: Layout, title: string): Layout {
|
|
186
|
+
const hero = layout.root.children?.[0];
|
|
187
|
+
const heading = hero ? firstHeading(hero) : undefined;
|
|
188
|
+
if (!heading) return layout;
|
|
189
|
+
return updateNode(
|
|
190
|
+
layout,
|
|
191
|
+
heading.id,
|
|
192
|
+
(node) =>
|
|
193
|
+
({
|
|
194
|
+
...node,
|
|
195
|
+
props: { ...(node.props as Record<string, unknown>), text: title, level: 1 },
|
|
196
|
+
}) as LayoutNode,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export async function createPage(ports: EmvbPorts, input: z.infer<typeof CreatePageInput>) {
|
|
201
|
+
// EmDash derives the slug from the title; refuse a title whose slug is already taken.
|
|
202
|
+
const slug = slugify(input.title);
|
|
203
|
+
if (!slug) refused("Give the page a title with letters or digits.");
|
|
204
|
+
const { design } = await ports.getDesign();
|
|
205
|
+
let layout = starterLayout(input.title);
|
|
206
|
+
const recipes = input.recipes ?? [];
|
|
207
|
+
if (recipes.length > 0) {
|
|
208
|
+
// W-474: sections sit edge to edge, and a hero first carries the page's one h1 itself
|
|
209
|
+
// instead of a bare title heading above it.
|
|
210
|
+
layout = {
|
|
211
|
+
...layout,
|
|
212
|
+
root: { ...layout.root, style: { ...layout.root.style, gap: { value: 0, unit: "px" } } },
|
|
213
|
+
};
|
|
214
|
+
if (recipes[0] === "hero") layout = { ...layout, root: { ...layout.root, children: [] } };
|
|
215
|
+
}
|
|
216
|
+
for (const id of recipes) {
|
|
217
|
+
const node = recipeNode(id, design, layout);
|
|
218
|
+
if (node)
|
|
219
|
+
layout = arranged(
|
|
220
|
+
addNode(layout, withFreshIds(layout, node), {
|
|
221
|
+
parentId: layout.root.id,
|
|
222
|
+
index: Number.MAX_SAFE_INTEGER,
|
|
223
|
+
}),
|
|
224
|
+
);
|
|
225
|
+
}
|
|
226
|
+
if (recipes[0] === "hero") layout = promoteHeroHeading(layout, input.title);
|
|
227
|
+
const checked = checkLayout(layout);
|
|
228
|
+
const taken = await ports.listPages({ limit: 50 });
|
|
229
|
+
if (taken.pages.some((page) => page.slug === slug)) {
|
|
230
|
+
throw new McpToolError("REFUSED", `The slug "${slug}" is taken. Choose another title.`);
|
|
231
|
+
}
|
|
232
|
+
const created = await ports.createPage({ title: input.title, layout: checked });
|
|
233
|
+
await ports.log({
|
|
234
|
+
target: "page",
|
|
235
|
+
tool: "create_page",
|
|
236
|
+
pageId: created.id,
|
|
237
|
+
rev: created.rev,
|
|
238
|
+
summary: `Created page "${input.title}"`,
|
|
239
|
+
});
|
|
240
|
+
return { pageId: created.id, slug: created.slug || slug, rev: created.rev };
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// ---------------------------------------------------------------- add / move / update / remove
|
|
244
|
+
|
|
245
|
+
export const AddElementInput = z.strictObject({
|
|
246
|
+
pageId: PageId,
|
|
247
|
+
parentId: NodeIdInput,
|
|
248
|
+
index: z.number().int().min(0).optional(),
|
|
249
|
+
element: ElementSpec,
|
|
250
|
+
expectedRev: Rev.optional(),
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
export async function addElement(ports: EmvbPorts, input: z.infer<typeof AddElementInput>) {
|
|
254
|
+
const element = resolveDesignRefs(input.element, (await ports.getDesign()).design);
|
|
255
|
+
return mutatePage(ports, "add_element", input.pageId, input.expectedRev, (layout) => {
|
|
256
|
+
if (!findNode(layout, input.parentId)) {
|
|
257
|
+
refused(
|
|
258
|
+
`No element with id ${input.parentId} on this page to add into. Call get_layout for the ids.`,
|
|
259
|
+
);
|
|
260
|
+
}
|
|
261
|
+
const node = withFreshIds(layout, buildNode(element));
|
|
262
|
+
const next = arranged(
|
|
263
|
+
addNode(layout, node, {
|
|
264
|
+
parentId: input.parentId,
|
|
265
|
+
index: input.index ?? Number.MAX_SAFE_INTEGER,
|
|
266
|
+
}),
|
|
267
|
+
);
|
|
268
|
+
return { layout: next, changed: [node.id], summary: `Added ${node.type} ${node.id}` };
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
export const MoveElementInput = z.strictObject({
|
|
273
|
+
pageId: PageId,
|
|
274
|
+
nodeId: NodeIdInput,
|
|
275
|
+
parentId: NodeIdInput,
|
|
276
|
+
index: z.number().int().min(0),
|
|
277
|
+
expectedRev: Rev.optional(),
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
export const moveElement = (ports: EmvbPorts, input: z.infer<typeof MoveElementInput>) =>
|
|
281
|
+
mutatePage(ports, "move_element", input.pageId, input.expectedRev, (layout) => {
|
|
282
|
+
for (const id of [input.nodeId, input.parentId]) {
|
|
283
|
+
if (!findNode(layout, id))
|
|
284
|
+
refused(`No element with id ${id} on this page. Call get_layout for the ids.`);
|
|
285
|
+
}
|
|
286
|
+
return {
|
|
287
|
+
layout: arranged(moveNode(layout, input.nodeId, input.parentId, input.index)),
|
|
288
|
+
changed: [input.nodeId],
|
|
289
|
+
summary: `Moved ${input.nodeId}`,
|
|
290
|
+
};
|
|
291
|
+
});
|
|
292
|
+
|
|
293
|
+
const SETTABLE = [
|
|
294
|
+
"props",
|
|
295
|
+
"style",
|
|
296
|
+
"states",
|
|
297
|
+
"devices",
|
|
298
|
+
"attributes",
|
|
299
|
+
"hiddenOn",
|
|
300
|
+
"classes",
|
|
301
|
+
"htmlId",
|
|
302
|
+
"label",
|
|
303
|
+
"bind",
|
|
304
|
+
"audience",
|
|
305
|
+
"dir",
|
|
306
|
+
"container",
|
|
307
|
+
"query",
|
|
308
|
+
] as const;
|
|
309
|
+
|
|
310
|
+
export const UpdateElementInput = z.strictObject({
|
|
311
|
+
pageId: PageId,
|
|
312
|
+
nodeId: NodeIdInput,
|
|
313
|
+
set: z
|
|
314
|
+
.strictObject(Object.fromEntries(SETTABLE.map((key) => [key, z.unknown().optional()])))
|
|
315
|
+
.optional(),
|
|
316
|
+
unset: z
|
|
317
|
+
.array(z.string().regex(/^[a-zA-Z]+(\.[a-zA-Z0-9_-]+)?$/))
|
|
318
|
+
.max(50)
|
|
319
|
+
.optional(),
|
|
320
|
+
expectedRev: Rev.optional(),
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const isRecord = (v: unknown): v is Record<string, unknown> =>
|
|
324
|
+
typeof v === "object" && v !== null && !Array.isArray(v);
|
|
325
|
+
|
|
326
|
+
export async function updateElement(ports: EmvbPorts, input: z.infer<typeof UpdateElementInput>) {
|
|
327
|
+
const set = resolveDesignRefs(input.set ?? {}, (await ports.getDesign()).design);
|
|
328
|
+
return mutatePage(ports, "update_element", input.pageId, input.expectedRev, (layout) => {
|
|
329
|
+
if (!findNode(layout, input.nodeId))
|
|
330
|
+
refused(`No element with id ${input.nodeId} on this page.`);
|
|
331
|
+
const next = updateNode(layout, input.nodeId, (node) => {
|
|
332
|
+
const copy = structuredClone(node) as Record<string, unknown>;
|
|
333
|
+
for (const [key, value] of Object.entries(set)) {
|
|
334
|
+
if (value === undefined) continue;
|
|
335
|
+
// Objects merge one level deep, like the editor's panels; anything else replaces.
|
|
336
|
+
copy[key] = isRecord(value) && isRecord(copy[key]) ? { ...copy[key], ...value } : value;
|
|
337
|
+
}
|
|
338
|
+
for (const path of input.unset ?? []) {
|
|
339
|
+
const [top, sub] = path.split(".") as [string, string | undefined];
|
|
340
|
+
if (top === "id" || top === "type" || top === "children") continue;
|
|
341
|
+
if (sub === undefined) delete copy[top];
|
|
342
|
+
else if (isRecord(copy[top])) delete copy[top][sub];
|
|
343
|
+
}
|
|
344
|
+
return copy as LayoutNode;
|
|
345
|
+
});
|
|
346
|
+
return { layout: next, changed: [input.nodeId], summary: `Updated ${input.nodeId}` };
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
export const RemoveElementInput = z.strictObject({
|
|
351
|
+
pageId: PageId,
|
|
352
|
+
nodeId: NodeIdInput,
|
|
353
|
+
expectedRev: Rev.optional(),
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
export const removeElement = (ports: EmvbPorts, input: z.infer<typeof RemoveElementInput>) =>
|
|
357
|
+
mutatePage(ports, "remove_element", input.pageId, input.expectedRev, (layout) => {
|
|
358
|
+
if (layout.root.id === input.nodeId) refused("The page root can't be removed.");
|
|
359
|
+
const { layout: next, removed } = removeNode(layout, input.nodeId);
|
|
360
|
+
if (!removed) refused(`No element with id ${input.nodeId} on this page.`);
|
|
361
|
+
return {
|
|
362
|
+
layout: next,
|
|
363
|
+
changed: [input.nodeId],
|
|
364
|
+
summary: `Removed ${removed?.node.type} ${input.nodeId}`,
|
|
365
|
+
};
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
// ---------------------------------------------------------------- apply_section_recipe
|
|
369
|
+
|
|
370
|
+
export const ApplyRecipeInput = z.strictObject({
|
|
371
|
+
pageId: PageId,
|
|
372
|
+
recipeId: z.enum(SECTION_RECIPES.map((r) => r.id) as [string, ...string[]]),
|
|
373
|
+
parentId: NodeIdInput.optional(),
|
|
374
|
+
index: z.number().int().min(0).optional(),
|
|
375
|
+
expectedRev: Rev.optional(),
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
export async function applySectionRecipe(
|
|
379
|
+
ports: EmvbPorts,
|
|
380
|
+
input: z.infer<typeof ApplyRecipeInput>,
|
|
381
|
+
) {
|
|
382
|
+
const { design } = await ports.getDesign();
|
|
383
|
+
return mutatePage(ports, "apply_section_recipe", input.pageId, input.expectedRev, (layout) => {
|
|
384
|
+
const built = recipeNode(input.recipeId, design, layout);
|
|
385
|
+
if (!built) return refused(`Unknown recipe ${input.recipeId}.`);
|
|
386
|
+
const node = withFreshIds(layout, built);
|
|
387
|
+
const next = arranged(
|
|
388
|
+
addNode(layout, node, {
|
|
389
|
+
parentId: input.parentId ?? layout.root.id,
|
|
390
|
+
index: input.index ?? Number.MAX_SAFE_INTEGER,
|
|
391
|
+
}),
|
|
392
|
+
);
|
|
393
|
+
return {
|
|
394
|
+
layout: next,
|
|
395
|
+
changed: [node.id],
|
|
396
|
+
summary: `Added the ${input.recipeId} section ${node.id}`,
|
|
397
|
+
};
|
|
398
|
+
});
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
// ---------------------------------------------------------------- restore_version
|
|
402
|
+
|
|
403
|
+
export const RestoreVersionInput = z.strictObject({
|
|
404
|
+
pageId: PageId,
|
|
405
|
+
revisionId: z.string().min(1).max(128),
|
|
406
|
+
expectedRev: Rev.optional(),
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
export async function restoreVersion(ports: EmvbPorts, input: z.infer<typeof RestoreVersionInput>) {
|
|
410
|
+
const data = await ports.getRevisionData(input.pageId, input.revisionId);
|
|
411
|
+
if (!data)
|
|
412
|
+
throw new McpToolError(
|
|
413
|
+
"NOT_FOUND",
|
|
414
|
+
`No version ${input.revisionId} of this page. Call list_versions.`,
|
|
415
|
+
);
|
|
416
|
+
let raw = data["layout"];
|
|
417
|
+
if (typeof raw === "string") {
|
|
418
|
+
try {
|
|
419
|
+
raw = JSON.parse(raw) as unknown;
|
|
420
|
+
} catch {
|
|
421
|
+
raw = undefined;
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
const restored = checkLayout(raw);
|
|
425
|
+
return mutatePage(ports, "restore_version", input.pageId, input.expectedRev, () => ({
|
|
426
|
+
layout: restored,
|
|
427
|
+
changed: [restored.root.id],
|
|
428
|
+
summary: `Restored version ${input.revisionId}`,
|
|
429
|
+
}));
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// ---------------------------------------------------------------- update_site_styles
|
|
433
|
+
|
|
434
|
+
const VARIABLE_KINDS = ["colors", "fonts", "fontSizes", "spacings"] as const;
|
|
435
|
+
type Item = { id: string } & Record<string, unknown>;
|
|
436
|
+
const Items = z.array(z.looseObject({ id: z.string().min(1).max(64) })).max(100);
|
|
437
|
+
|
|
438
|
+
export const UpdateSiteStylesInput = z.strictObject({
|
|
439
|
+
variables: z
|
|
440
|
+
.strictObject(Object.fromEntries(VARIABLE_KINDS.map((k) => [k, Items.optional()])))
|
|
441
|
+
.optional(),
|
|
442
|
+
removeVariables: z
|
|
443
|
+
.array(z.strictObject({ kind: z.enum(VARIABLE_KINDS), id: z.string().min(1).max(64) }))
|
|
444
|
+
.max(100)
|
|
445
|
+
.optional(),
|
|
446
|
+
classes: Items.optional(),
|
|
447
|
+
removeClasses: z.array(z.string().min(1).max(64)).max(100).optional(),
|
|
448
|
+
defaults: z.record(z.string(), z.unknown()).optional(),
|
|
449
|
+
direction: z.enum(["ltr", "rtl", "auto"]).optional(),
|
|
450
|
+
/** Remove variables even when pages or classes still use them (their references fall back). */
|
|
451
|
+
force: z.boolean().optional(),
|
|
452
|
+
expectedRevision: z.string().max(256).nullable().optional(),
|
|
453
|
+
});
|
|
454
|
+
export const UpdateSiteStylesOutput = z.object({ revision: z.string(), summary: z.string() });
|
|
455
|
+
|
|
456
|
+
const KIND_OF: Record<(typeof VARIABLE_KINDS)[number], VariableKind> = {
|
|
457
|
+
colors: "color",
|
|
458
|
+
fonts: "font",
|
|
459
|
+
fontSizes: "fontSize",
|
|
460
|
+
spacings: "spacing",
|
|
461
|
+
};
|
|
462
|
+
|
|
463
|
+
/** Where a variable is still used: class ids and page titles (W-458 in-use guard). */
|
|
464
|
+
async function variableUses(
|
|
465
|
+
ports: EmvbPorts,
|
|
466
|
+
design: DesignSystem,
|
|
467
|
+
id: string,
|
|
468
|
+
kind: VariableKind,
|
|
469
|
+
): Promise<string[]> {
|
|
470
|
+
const where = findVariableUsagesInDesign(design, id, kind).map((u) => `class ${u.classId}`);
|
|
471
|
+
const pages: { id: string; title: string }[] = [];
|
|
472
|
+
let cursor: string | undefined;
|
|
473
|
+
for (let i = 0; i < 20; i++) {
|
|
474
|
+
// oxlint-disable-next-line no-await-in-loop -- cursor paging is sequential
|
|
475
|
+
const batch = await ports.listPages({ limit: 50, ...(cursor ? { cursor } : {}) });
|
|
476
|
+
pages.push(...batch.pages);
|
|
477
|
+
if (!batch.nextCursor) break;
|
|
478
|
+
cursor = batch.nextCursor;
|
|
479
|
+
}
|
|
480
|
+
const records = await Promise.all(pages.map((page) => ports.getPage(page.id)));
|
|
481
|
+
records.forEach((record, i) => {
|
|
482
|
+
const layout = record ? validateLayout(record.layout) : null;
|
|
483
|
+
if (layout?.ok && findVariableUsages(layout.layout, id, kind).length > 0) {
|
|
484
|
+
where.push(`page "${pages[i]?.title ?? ""}"`);
|
|
485
|
+
}
|
|
486
|
+
});
|
|
487
|
+
return where;
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
const upsert = (list: Item[] | undefined, items: Item[]): Item[] => {
|
|
491
|
+
const next = [...(list ?? [])];
|
|
492
|
+
for (const item of items) {
|
|
493
|
+
const at = next.findIndex((existing) => existing.id === item.id);
|
|
494
|
+
if (at === -1) next.push(item);
|
|
495
|
+
else next[at] = { ...next[at], ...item };
|
|
496
|
+
}
|
|
497
|
+
return next;
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/** Writes the style draft only; publishing site styles is a separate, gated tool (D-055). */
|
|
501
|
+
export async function updateSiteStyles(
|
|
502
|
+
ports: EmvbPorts,
|
|
503
|
+
input: z.infer<typeof UpdateSiteStylesInput>,
|
|
504
|
+
) {
|
|
505
|
+
return queued("styles", async () => {
|
|
506
|
+
const state = await ports.getDesign();
|
|
507
|
+
if (input.expectedRevision !== undefined && input.expectedRevision !== state.revision) {
|
|
508
|
+
throw new McpToolError(
|
|
509
|
+
"CONFLICT",
|
|
510
|
+
"Site styles changed since you read them. Call get_site_styles again.",
|
|
511
|
+
);
|
|
512
|
+
}
|
|
513
|
+
const draft = structuredClone(state.design) as unknown as Record<string, unknown> & {
|
|
514
|
+
variables: Record<string, Item[] | undefined>;
|
|
515
|
+
classes?: Item[];
|
|
516
|
+
defaults?: Record<string, unknown>;
|
|
517
|
+
};
|
|
518
|
+
const changes: string[] = [];
|
|
519
|
+
for (const kind of VARIABLE_KINDS) {
|
|
520
|
+
const items = input.variables?.[kind] as Item[] | undefined;
|
|
521
|
+
if (items?.length) {
|
|
522
|
+
draft.variables[kind] = upsert(draft.variables[kind], items);
|
|
523
|
+
changes.push(`${items.length} ${kind}`);
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
if (!input.force) {
|
|
527
|
+
const inUse = await Promise.all(
|
|
528
|
+
(input.removeVariables ?? []).map(async ({ kind, id }) => ({
|
|
529
|
+
id,
|
|
530
|
+
where: await variableUses(ports, state.design, id, KIND_OF[kind]),
|
|
531
|
+
})),
|
|
532
|
+
);
|
|
533
|
+
const blocked = inUse.filter((u) => u.where.length > 0);
|
|
534
|
+
if (blocked.length > 0) {
|
|
535
|
+
throw new McpToolError(
|
|
536
|
+
"REFUSED",
|
|
537
|
+
`Still in use: ${blocked.map((u) => `${u.id} (${u.where.slice(0, 5).join(", ")})`).join("; ")}. Replace those references first, or pass force: true.`,
|
|
538
|
+
);
|
|
539
|
+
}
|
|
540
|
+
}
|
|
541
|
+
for (const { kind, id } of input.removeVariables ?? []) {
|
|
542
|
+
draft.variables[kind] = (draft.variables[kind] ?? []).filter((v) => v.id !== id);
|
|
543
|
+
changes.push(`removed ${kind} ${id}`);
|
|
544
|
+
}
|
|
545
|
+
// Refs in new classes and tag defaults resolve against the variables after this change.
|
|
546
|
+
const withVars = { ...state.design, variables: draft.variables } as unknown as DesignSystem;
|
|
547
|
+
if (input.classes?.length) {
|
|
548
|
+
draft.classes = upsert(draft.classes, resolveDesignRefs(input.classes, withVars) as Item[]);
|
|
549
|
+
changes.push(`${input.classes.length} classes`);
|
|
550
|
+
}
|
|
551
|
+
if (input.removeClasses?.length) {
|
|
552
|
+
draft.classes = (draft.classes ?? []).filter((c) => !input.removeClasses?.includes(c.id));
|
|
553
|
+
changes.push(`removed ${input.removeClasses.length} classes`);
|
|
554
|
+
}
|
|
555
|
+
if (input.defaults) {
|
|
556
|
+
// Per tag, like update_element's style: { h1: { color } } keeps h1's other properties.
|
|
557
|
+
const merged: Record<string, unknown> = { ...draft.defaults };
|
|
558
|
+
for (const [tag, style] of Object.entries(resolveDesignRefs(input.defaults, withVars))) {
|
|
559
|
+
merged[tag] =
|
|
560
|
+
isRecord(style) && isRecord(merged[tag]) ? { ...merged[tag], ...style } : style;
|
|
561
|
+
}
|
|
562
|
+
draft.defaults = merged;
|
|
563
|
+
changes.push("tag defaults");
|
|
564
|
+
}
|
|
565
|
+
if (input.direction) {
|
|
566
|
+
draft["direction"] = input.direction;
|
|
567
|
+
changes.push(`direction ${input.direction}`);
|
|
568
|
+
}
|
|
569
|
+
if (changes.length === 0) refused("Nothing to change.");
|
|
570
|
+
const result = validateDesign(draft);
|
|
571
|
+
if (!result.ok) {
|
|
572
|
+
throw new McpToolError(
|
|
573
|
+
"INVALID_INPUT",
|
|
574
|
+
`The site styles would be invalid. ${summarizeIssues(result.issues)}`,
|
|
575
|
+
);
|
|
576
|
+
}
|
|
577
|
+
const saved = await ports.saveDesign(result.design as DesignSystem, state.revision);
|
|
578
|
+
const summary = `Site styles draft: ${changes.join(", ")}`;
|
|
579
|
+
await ports.log({ target: "styles", tool: "update_site_styles", summary });
|
|
580
|
+
return { revision: saved.revision, summary };
|
|
581
|
+
});
|
|
582
|
+
}
|