Files
webstudio/apps/builder/app/shared/sync/patch/patch-service.server.ts
Oleg IsonenandClaude Fable 5 071a4d9a85 feat: MCP update 3 (#5864)
## Summary

This update adds new MCP and CLI capabilities for project auditing,
structured data, project-specific agent instructions, focused project
search, safer bulk editing, image descriptions, font discovery, visual
verification, and external registry inspection.

## New Capabilities

### Audit projects

- Run a single `audit` command across accessibility, SEO, security,
assets, styles, and performance.
- Limit an audit to selected scopes, pages, or severity levels.
- Receive stable findings with locations and suggested fixes.
- Run rendered audits across desktop, mobile, and Builder breakpoint
widths.
- Detect horizontal overflow, broken images, oversized images, eager
below-fold images, render-blocking resources, and legacy font formats.
- Identify unused assets, tokens, CSS variables, local styles, and
breakpoints.
- Identify duplicate token declarations.
- Detect accessibility issues involving names, labels, headings,
landmarks, IDs, keyboard focus, ARIA, iframes, and autoplaying media.
- Detect SEO issues involving titles, descriptions, language, social
images, and structured data.
- Detect unsafe new-tab links and write resources exposed during page
rendering.

### Search an entire project

- Search project text, labels, props, resources, assets, and styles
through `search-project`.
- Find relevant records without downloading and reparsing the full
project snapshot.
- Scope searches to the relevant project-data areas.

### Add validated JSON-LD

- Add structured data with a dedicated JSON-LD component.
- Place JSON-LD inside the page Head Slot.
- Edit JSON in a purpose-built code editor.
- Prevent structurally invalid JSON-LD from being saved.
- Validate Schema.org types, properties, and values.
- Find invalid, missing, unknown, deprecated, or incompatible structured
data through the SEO audit.
- Let agents discover the correct JSON-LD workflow without using page
custom metadata or a generic HTML embed.

### Configure project-specific agent instructions

- Add AI-agent instructions from the new **Agents** section in Project
Settings.
- Synchronize those instructions into a managed root `AGENTS.md`.
- Update or remove the managed file automatically during project sync.
- Protect existing user-owned `AGENTS.md` files from being overwritten.
- Share the instructions with authenticated CLI and MCP agents.
- Keep agent instructions out of publicly readable production bundles.

### Generate and manage image descriptions

- Audit image assets for missing descriptions.
- Let a vision-capable agent inspect each image in its rendered context.
- Save generated descriptions to multiple image assets in one operation.
- Mark images as intentionally decorative.
- Reuse the saved asset description wherever the image is used.
- Re-run the audit to verify that missing-description findings are
resolved.

### Perform bounded bulk replacements

- Replace matching literal text across selected pages with
`replace-text`.
- Replace matching fixed prop text with `replace-prop-text`.
- Replace matching resource names or URLs with `replace-resource-text`.
- Restrict replacements by page, instance, prop, resource, field, match
mode, and maximum change count.
- Leave dynamic expressions unchanged.

### Inspect element ancestry

- Return an element’s direct parent in instance listings.
- Request the complete ancestor chain when inspecting an element.
- Understand tree placement without rebuilding the instance hierarchy
from raw project data.

### Discover available fonts

- List uploaded font families through `list-fonts`.
- Discover available system font stacks.
- Inspect font family, style, weight, format, and associated asset
information before applying typography.

### Inspect external component registries

- Inspect a local or remote shadcn-style registry item.
- Read its description, files, dependencies, targets, and documentation.
- Receive an explicit Webstudio compatibility report.
- Inspect registry metadata without installing or executing external
code.

### Capture generated project routes directly

- Run `webstudio screenshot --path /route` against the configured
project.
- Generate a temporary production-style preview automatically.
- Capture the requested route.
- Stop the temporary preview after capture.
- Avoid manually building and starting a preview for a single
screenshot.

### Assert visual changes

- Add expected text to screenshot comparisons.
- Set maximum pixel mismatch limits.
- Set minimum or maximum changed-region counts.
- Assert whether overall brightness or a color channel moved in the
expected direction.
- Use these assertions as pass/fail evidence for visual changes.

### Require compatible CLI versions

- Send the project-bundle contract version with CLI synchronization
requests.
- Stop incompatible CLI versions before they receive incomplete project
data.
- Tell users exactly how to update the CLI or run the latest version
once.
- Prevent older clients from silently dropping project settings.

## Upgrade Note

This branch introduces a new synchronized project-bundle contract. Older
CLI versions must update before synchronizing against the new API.

```sh
npm install -g webstudio@latest
```

Or run the latest version once:

```sh
npx webstudio@latest <command>
```

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-07-12 11:27:58 +02:00

401 lines
9.8 KiB
TypeScript

import {
AuthorizationError,
type AppContext,
} from "@webstudio-is/trpc-interface/index.server";
import { patchLoadedBuild } from "@webstudio-is/project/index.server";
import type { Database } from "@webstudio-is/postgrest/index.server";
import {
assertProjectPermit,
authorizePatchEntries,
createContentModeCapabilities,
createWriterContext,
type AuthorizedPatchEntry,
} from "./patch-auth.server";
import type {
NormalizedPatchRequest,
PatchEntry,
} from "./patch-normalize.server";
type BuildRow = Database["public"]["Tables"]["Build"]["Row"];
type BuildColumn = keyof BuildRow;
const baseBuildPatchColumns = [
"projectId",
"version",
"lastTransactionId",
] as const satisfies BuildColumn[];
const contentModeBuildColumns = [
"instances",
"props",
"styleSources",
"styleSourceSelections",
"styles",
"breakpoints",
] as const satisfies BuildColumn[];
const namespaceBuildColumns = {
pages: ["pages"],
projectSettings: ["projectSettings"],
breakpoints: ["breakpoints"],
instances: ["instances"],
props: ["props"],
assets: [],
styleSourceSelections: ["styleSourceSelections"],
styleSources: ["styleSources"],
styles: ["styles"],
dataSources: ["dataSources"],
resources: ["resources"],
marketplaceProduct: ["marketplaceProduct"],
} as const satisfies Record<string, readonly BuildColumn[]>;
type BuildPatchNamespace = keyof typeof namespaceBuildColumns;
const isBuildPatchNamespace = (
namespace: string
): namespace is BuildPatchNamespace => {
return Object.hasOwn(namespaceBuildColumns, namespace);
};
const getNamespaceBuildColumns = (namespace: string) => {
if (isBuildPatchNamespace(namespace)) {
return namespaceBuildColumns[namespace];
}
return [];
};
const getBuildPatchColumns = (authorized: AuthorizedPatchEntry[]) => {
const columns = new Set<BuildColumn>(baseBuildPatchColumns);
for (const { entry } of authorized) {
for (const change of entry.transaction.payload) {
for (const column of getNamespaceBuildColumns(change.namespace)) {
columns.add(column);
}
}
}
return Array.from(columns);
};
const selectBuildColumns = async (
context: AppContext,
buildId: string,
columns: readonly BuildColumn[]
) => {
const build = await context.postgrest.client
.from("Build")
.select(columns.join(", "))
.eq("id", buildId);
if (build.error) {
throw build.error;
}
if (build.data.length !== 1) {
throw new Error(
`Results contain ${build.data.length} row(s) requires 1 row`
);
}
return build.data[0] as unknown as Partial<BuildRow> &
Pick<BuildRow, (typeof columns)[number]>;
};
type PatchEntryResult =
| { seq?: number; transactionId: string; status: "accepted" }
| {
seq?: number;
transactionId: string;
status: "rejected" | "failed";
errors: string;
};
type PatchResult =
| { status: "ok"; version?: number; entries?: PatchEntryResult[] }
| { status: "partial"; version: number; entries: PatchEntryResult[] }
| { status: "version_mismatched"; errors: string }
| { status: "authorization_error"; errors: string }
| { status: "error"; errors: string };
export const toPatchResult = async (error: unknown): Promise<PatchResult> => {
if (error instanceof AuthorizationError) {
return {
status: "authorization_error",
errors: error.message,
};
}
if (error instanceof Response && error.ok === false) {
return {
status: "authorization_error",
errors: error.statusText,
};
}
if (error instanceof Response) {
return {
status: "error",
errors: await error.text(),
};
}
console.error(error);
return {
status: "error",
errors: error instanceof Error ? error.message : JSON.stringify(error),
};
};
const loadBuildState = async (context: AppContext, buildId: string) => {
const build = await context.postgrest.client
.from("Build")
.select("projectId, version")
.eq("id", buildId)
.single();
if (build.error) {
throw build.error;
}
return {
projectId: String(build.data.projectId),
version: Number(build.data.version),
};
};
const loadBuildForContentMode = async (
context: AppContext,
buildId: string
) => {
return selectBuildColumns(context, buildId, contentModeBuildColumns);
};
const loadBuildForPatch = async ({
authorized,
context,
state,
buildId,
}: {
authorized: AuthorizedPatchEntry[];
context: AppContext;
state: Awaited<ReturnType<typeof loadBuildState>>;
buildId: string;
}) => {
const build = await selectBuildColumns(
context,
buildId,
getBuildPatchColumns(authorized)
);
return {
...build,
projectId: state.projectId,
version: state.version,
} as BuildRow;
};
const assertBuildProject = (
state: { projectId: string },
patch: Pick<NormalizedPatchRequest, "buildId" | "projectId">
) => {
if (state.projectId !== patch.projectId) {
throw new Error("Build does not belong to project");
}
};
const acceptedResult = (entry: PatchEntry): PatchEntryResult => ({
seq: entry.seq,
transactionId: entry.transaction.id,
status: "accepted",
});
const failedResult = (entry: PatchEntry, errors: string): PatchEntryResult => ({
seq: entry.seq,
transactionId: entry.transaction.id,
status: "failed",
errors,
});
const rejectedResult = ({
entry,
errors,
}: {
entry: PatchEntry;
errors: string;
}): PatchEntryResult => ({
seq: entry.seq,
transactionId: entry.transaction.id,
status: "rejected",
errors,
});
const applyAuthorizedEntries = async ({
authorized,
build,
patch,
version,
}: {
authorized: AuthorizedPatchEntry[];
build?: BuildRow;
patch: NormalizedPatchRequest;
version: number;
}) => {
if (authorized.length === 0) {
return {
version,
entries: [] satisfies PatchEntryResult[],
status: "ok" as const,
};
}
if (build === undefined) {
throw new Error("Build data is required to apply patch entries.");
}
let currentBuild = build;
const applyContext = authorized[0].context;
const hasSharedContext = authorized.every(
({ context }) => context === applyContext
);
if (hasSharedContext) {
const batchResult = await patchLoadedBuild(
{
build: currentBuild,
buildId: patch.buildId,
projectId: patch.projectId,
clientVersion: version,
transactions: authorized.map(({ entry }) => entry.transaction),
},
applyContext
);
if (batchResult.status === "ok") {
currentBuild = batchResult.build;
return {
status: "ok" as const,
version: batchResult.version,
entries: authorized.map(({ entry }) => acceptedResult(entry)),
};
}
if (batchResult.status === "version_mismatched") {
return batchResult;
}
}
const entries: PatchEntryResult[] = [];
let currentVersion = version;
for (const { entry, context: entryContext } of authorized) {
const entryResult = await patchLoadedBuild(
{
build: currentBuild,
buildId: patch.buildId,
projectId: patch.projectId,
clientVersion: currentVersion,
transactions: [entry.transaction],
},
entryContext
);
if (entryResult.status === "ok") {
currentVersion = entryResult.version;
currentBuild = entryResult.build;
entries.push(acceptedResult(entry));
continue;
}
if (entryResult.status === "version_mismatched") {
return entryResult;
}
entries.push(failedResult(entry, entryResult.errors));
}
return {
status: "partial" as const,
version: currentVersion,
entries,
};
};
export const applyPatchRequest = async (
context: AppContext,
patch: NormalizedPatchRequest
): Promise<PatchResult> => {
const state = await loadBuildState(context, patch.buildId);
assertBuildProject(state, patch);
const { authorized, rejected } = await authorizePatchEntries(
context,
patch,
async () => {
const build = await loadBuildForContentMode(context, patch.buildId);
return createContentModeCapabilities({
instances: build.instances,
props: build.props,
styleSources: build.styleSources,
styleSourceSelections: build.styleSourceSelections,
styles: build.styles,
breakpoints: build.breakpoints,
});
}
);
const applied = await applyAuthorizedEntries({
authorized,
build:
authorized.length === 0
? undefined
: await loadBuildForPatch({
authorized,
context,
state,
buildId: patch.buildId,
}),
patch,
version: authorized.length === 0 ? state.version : patch.clientVersion,
});
if (applied.status === "version_mismatched") {
return applied;
}
const entriesByTransactionId = new Map<string, PatchEntryResult[]>();
for (const entryResult of [
...rejected.map(rejectedResult),
...applied.entries,
]) {
const entries = entriesByTransactionId.get(entryResult.transactionId) ?? [];
entries.push(entryResult);
entriesByTransactionId.set(entryResult.transactionId, entries);
}
const entries = patch.entries.flatMap((entry) => {
return entriesByTransactionId.get(entry.transaction.id)?.shift() ?? [];
});
const hasPartialResult = entries.some((entry) => entry.status !== "accepted");
if (hasPartialResult) {
return {
status: "partial",
version: applied.version,
entries,
};
}
return { status: "ok", version: applied.version, entries };
};
export const loadAuthorizedPatchState = async ({
authToken,
buildId,
context,
}: {
authToken: string;
buildId: string;
context: AppContext;
}) => {
const state = await loadBuildState(context, buildId);
const writerContext = await createWriterContext(context, authToken);
await assertProjectPermit({
context: writerContext,
permit: "edit",
projectId: state.projectId,
});
return state;
};