From 1478adfacb013580c7839f08bacc9c8f684d2052 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Wed, 26 Aug 2026 20:02:39 -0400 Subject: [PATCH 1/3] feat(studio): add studio_inspect, so an agent reads before it writes Everything about one element in one call: resolved styles, text fields, box, data attributes, GSAP animations, and what the element will and will not accept. The point is to prevent a failed write rather than to satisfy curiosity. `can.reasonIfDisabled` is passed through verbatim from Studio's own capabilities, so an agent that reads first should never attempt an edit the element would refuse. Three things it refuses to get wrong: Animations are reported ONLY for the current selection, because that is the only element Studio parses them for. Attributing them to any other element would be reporting the wrong element's motion, which is worse than reporting none. When a handle names something else the field is empty and `animationEditingBlocked` says why. `animationEditingBlocked` also carries the two states where animation editing is off entirely, multiple timelines and an unsupported timeline pattern. Both live on the selection context. Learning them from a read costs one call; learning them from a failed write costs a retry loop. Inspecting a handle does NOT change what is selected. It is a read, and stealing the human's selection would be a side effect they did not ask for. There is a test asserting `applySelection` is never called. Nothing selected and no handle given is a failure, not an empty result. An empty result would assert "this element has nothing", which is a different and false claim. --- .../studio/src/webmcp/StudioAgentTools.tsx | 17 +- .../src/webmcp/tools/frameTools.test.ts | 12 +- .../src/webmcp/tools/inspectTools.test.ts | 197 +++++++++++++++++ .../studio/src/webmcp/tools/inspectTools.ts | 208 ++++++++++++++++++ .../src/webmcp/tools/selectionTools.test.ts | 52 +---- .../src/webmcp/useStudioAgentTools.test.tsx | 13 +- .../studio/src/webmcp/useStudioAgentTools.ts | 21 +- packages/studio/src/webmcp/webmcpTestUtils.ts | 91 ++++++++ 8 files changed, 544 insertions(+), 67 deletions(-) create mode 100644 packages/studio/src/webmcp/tools/inspectTools.test.ts create mode 100644 packages/studio/src/webmcp/tools/inspectTools.ts create mode 100644 packages/studio/src/webmcp/webmcpTestUtils.ts diff --git a/packages/studio/src/webmcp/StudioAgentTools.tsx b/packages/studio/src/webmcp/StudioAgentTools.tsx index e26612c17..4fbf55e68 100644 --- a/packages/studio/src/webmcp/StudioAgentTools.tsx +++ b/packages/studio/src/webmcp/StudioAgentTools.tsx @@ -18,7 +18,12 @@ import type { StudioLookSnapshot } from "./tools/lookTools"; */ export function StudioAgentTools() { const { projectId, activeCompPath, editHistory } = useStudioShellContext(); - const { domEditSelection, selectedGsapAnimations } = useDomEditSelectionContext(); + const { + domEditSelection, + selectedGsapAnimations, + gsapMultipleTimelines, + gsapUnsupportedTimelinePattern, + } = useDomEditSelectionContext(); const { previewIframeRef, buildDomSelectionFromTarget, applyDomSelection } = useDomEditActionsContext(); @@ -71,6 +76,12 @@ export function StudioAgentTools() { } }, wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)), + getCurrentSelection: () => domEditSelection, + getGsapDiagnostics: () => ({ + animations: selectedGsapAnimations, + multipleTimelines: gsapMultipleTimelines, + unsupportedTimelinePattern: gsapUnsupportedTimelinePattern, + }), }), [ getSnapshot, @@ -79,6 +90,10 @@ export function StudioAgentTools() { applyDomSelection, projectId, activeCompPath, + domEditSelection, + selectedGsapAnimations, + gsapMultipleTimelines, + gsapUnsupportedTimelinePattern, ], ); diff --git a/packages/studio/src/webmcp/tools/frameTools.test.ts b/packages/studio/src/webmcp/tools/frameTools.test.ts index e98d5c2e4..8ac0b5a43 100644 --- a/packages/studio/src/webmcp/tools/frameTools.test.ts +++ b/packages/studio/src/webmcp/tools/frameTools.test.ts @@ -1,7 +1,7 @@ // @vitest-environment jsdom import { describe, expect, it, vi } from "vitest"; import { studioFrame, type FrameToolDeps, type StudioFrameResult } from "./frameTools"; -import type { ToolFailure, ToolResult } from "../toolResult"; +import { expectFailure, expectOk } from "../webmcpTestUtils"; function frameDeps(overrides: Partial = {}): FrameToolDeps { return { @@ -15,16 +15,6 @@ function frameDeps(overrides: Partial = {}): FrameToolDeps { }; } -function expectOk(result: ToolResult): { ok: true } & T { - if (!result.ok) throw new Error(`expected ok, got ${JSON.stringify(result)}`); - return result; -} - -function expectFailure(result: ToolResult): ToolFailure { - if (result.ok) throw new Error(`expected failure, got ${JSON.stringify(result)}`); - return result; -} - describe("studioFrame", () => { it("returns a URL for the composition at the playhead", async () => { const result = await studioFrame(frameDeps()); diff --git a/packages/studio/src/webmcp/tools/inspectTools.test.ts b/packages/studio/src/webmcp/tools/inspectTools.test.ts new file mode 100644 index 000000000..d59034a45 --- /dev/null +++ b/packages/studio/src/webmcp/tools/inspectTools.test.ts @@ -0,0 +1,197 @@ +// @vitest-environment jsdom +import { describe, expect, it, vi } from "vitest"; +import type { GsapAnimation } from "@hyperframes/parsers/gsap-parser"; +import { studioInspect, type InspectToolDeps, type StudioInspectResult } from "./inspectTools"; +import { + expectFailure, + expectOk, + previewDoc, + previewElement, + selectionFor, +} from "../webmcpTestUtils"; + +function animation(overrides: Partial = {}): GsapAnimation { + return { + id: "anim-1", + targetSelector: "#headline", + method: "from", + position: 0, + properties: { y: -50, opacity: 0 }, + duration: 1, + ease: "power2.out", + ...overrides, + } as GsapAnimation; +} + +function inspectDeps(overrides: Partial = {}): InspectToolDeps { + return { + getPreviewDocument: () => null, + buildSelection: async (element) => selectionFor(element), + applySelection: () => undefined, + requestSeek: () => undefined, + readPlayhead: () => ({ currentTime: 0, duration: 10, isPlaying: false }), + getCurrentSelection: () => null, + getGsapDiagnostics: () => ({ + animations: [], + multipleTimelines: false, + unsupportedTimelinePattern: false, + }), + ...overrides, + }; +} + +describe("studioInspect", () => { + it("returns the resolved styles, not the authored ones", async () => { + const element = previewElement('

Ship it

', "headline"); + const selection = selectionFor(element); + + const result = await studioInspect(inspectDeps({ getCurrentSelection: () => selection })); + + const ok = expectOk(result); + // The authored value is a clamp(); the resolved one is what actually renders. + expect(ok.styles["font-size"]).toBe("42.7px"); + expect(ok.inlineStyles.color).toBe("red"); + expect(ok.box.width).toBe(880); + }); + + it("reports capabilities and the disabled reason verbatim", async () => { + const element = previewElement('

Ship it

', "headline"); + const locked = selectionFor(element, { + capabilities: { + canSelect: true, + canEditStyles: false, + canCrop: false, + canMove: false, + canResize: false, + canApplyManualOffset: false, + canApplyManualSize: false, + canApplyManualRotation: false, + reasonIfDisabled: "Element is inside a locked composition", + }, + }); + + const result = await studioInspect(inspectDeps({ getCurrentSelection: () => locked })); + + const ok = expectOk(result); + expect(ok.can.editStyles).toBe(false); + expect(ok.can.move).toBe(false); + expect(ok.can.reasonIfDisabled).toBe("Element is inside a locked composition"); + }); + + it("lists the animations on the current selection", async () => { + const element = previewElement('

Ship it

', "headline"); + + const result = await studioInspect( + inspectDeps({ + getCurrentSelection: () => selectionFor(element), + getGsapDiagnostics: () => ({ + animations: [animation()], + multipleTimelines: false, + unsupportedTimelinePattern: false, + }), + }), + ); + + const ok = expectOk(result); + expect(ok.animations).toHaveLength(1); + expect(ok.animations[0]?.animationId).toBe("anim-1"); + expect(ok.animations[0]?.ease).toBe("power2.out"); + expect(ok.animationEditingBlocked).toBeNull(); + }); + + it("says WHY animation editing is unavailable, so a write is not attempted", async () => { + const element = previewElement('

Ship it

', "headline"); + const base = { + getCurrentSelection: () => selectionFor(element), + }; + + const multiple = await studioInspect( + inspectDeps({ + ...base, + getGsapDiagnostics: () => ({ + animations: [], + multipleTimelines: true, + unsupportedTimelinePattern: false, + }), + }), + ); + const unsupported = await studioInspect( + inspectDeps({ + ...base, + getGsapDiagnostics: () => ({ + animations: [], + multipleTimelines: false, + unsupportedTimelinePattern: true, + }), + }), + ); + + expect(expectOk(multiple).animationEditingBlocked).toMatch( + /multiple GSAP timelines/, + ); + expect(expectOk(unsupported).animationEditingBlocked).toMatch( + /not editable/, + ); + }); + + it("does not attribute the selection's animations to a different element", async () => { + // Studio only parses animations for the CURRENT selection. Reporting them + // against another element would report the wrong element's motion. + const headline = previewElement('

A

B

', "headline"); + const doc = headline.ownerDocument; + + const result = await studioInspect( + inspectDeps({ + getPreviewDocument: () => doc, + getCurrentSelection: () => selectionFor(headline), + getGsapDiagnostics: () => ({ + animations: [animation()], + multipleTimelines: false, + unsupportedTimelinePattern: false, + }), + }), + { handle: "dom:body" }, + ); + + const ok = expectOk(result); + expect(ok.isCurrentSelection).toBe(false); + expect(ok.animations).toEqual([]); + expect(ok.animationEditingBlocked).toMatch(/only readable for the current selection/); + }); + + it("inspects a handle without changing what is selected", async () => { + const doc = previewDoc('

A

'); + const applySelection = vi.fn(); + + const result = await studioInspect( + inspectDeps({ getPreviewDocument: () => doc, applySelection }), + { handle: "dom:headline" }, + ); + + expect(result.ok).toBe(true); + // Inspecting is a read. It must not steal the human's selection. + expect(applySelection).not.toHaveBeenCalled(); + }); + + it("fails rather than returning an empty result when nothing is selected", async () => { + const result = expectFailure(await studioInspect(inspectDeps())); + + // An empty result would assert "this element has nothing", a different and + // false claim from "you did not say which element". + expect(result.kind).toBe("invalid"); + expect(result.reason).toMatch(/nothing is selected/); + expect(result.hint).toMatch(/studio_select/); + }); + + it("reports an unknown handle distinctly from an unmounted preview", async () => { + const notMounted = expectFailure(await studioInspect(inspectDeps(), { handle: "dom:x" })); + expect(notMounted.kind).toBe("blocked"); + + const doc = previewDoc('

A

'); + const unknown = expectFailure( + await studioInspect(inspectDeps({ getPreviewDocument: () => doc }), { handle: "dom:x" }), + ); + expect(unknown.kind).toBe("invalid"); + expect(unknown.reason).not.toBe(notMounted.reason); + }); +}); diff --git a/packages/studio/src/webmcp/tools/inspectTools.ts b/packages/studio/src/webmcp/tools/inspectTools.ts new file mode 100644 index 000000000..f31b9fc83 --- /dev/null +++ b/packages/studio/src/webmcp/tools/inspectTools.ts @@ -0,0 +1,208 @@ +/** + * `studio_inspect`: everything about one element, in one call. + * + * The point is to prevent a failed write. Every field here either tells the + * agent what it can change (`can`, with `reasonIfDisabled` verbatim) or what it + * would be changing (the resolved styles, the text fields, the animations). + * An agent that reads this first should never attempt an edit the element will + * refuse. + * + * The GSAP diagnostics are here for the same reason: `multipleTimelines` and + * `unsupportedTimelinePattern` are states where animation editing is off, and + * learning that from a read is cheaper than learning it from a failed write. + */ + +import type { GsapAnimation } from "@hyperframes/parsers/gsap-parser"; +import type { DomEditSelection } from "../../components/editor/domEditingTypes"; +import { mintElementHandle, patchTargetAddress, resolveElementHandle } from "../handles"; +import { toolFailure, toolOk, type ToolResult } from "../toolResult"; +import type { SelectionToolDeps } from "./selectionTools"; + +export interface InspectToolDeps extends SelectionToolDeps { + /** What the human currently has selected, used when no handle is given. */ + getCurrentSelection: () => DomEditSelection | null; + getGsapDiagnostics: () => { + animations: readonly GsapAnimation[]; + multipleTimelines: boolean; + unsupportedTimelinePattern: boolean; + }; +} + +interface InspectAnimation { + animationId: string; + method: string; + target: string; + position: number | string; + duration: number | null; + ease: string | null; + properties: Record; + hasKeyframes: boolean; + hasArcPath: boolean; +} + +interface InspectTextField { + key: string; + label: string; + value: string; + tagName: string; +} + +export interface StudioInspectResult { + handle: string | null; + label: string; + tagName: string; + sourceFile: string; + box: { x: number; y: number; width: number; height: number }; + text: string | null; + textFields: InspectTextField[]; + /** The styles Studio itself surfaces, resolved, not as authored. */ + styles: Record; + inlineStyles: Record; + dataAttributes: Record; + can: { + editStyles: boolean; + move: boolean; + resize: boolean; + rotate: boolean; + crop: boolean; + editText: boolean; + reasonIfDisabled: string | null; + }; + animations: InspectAnimation[]; + /** Present only when animation editing is unavailable, with the reason. */ + animationEditingBlocked: string | null; + /** True when this element is the one the human currently has selected. */ + isCurrentSelection: boolean; +} + +export interface StudioInspectInput { + /** Omit to inspect the current selection. */ + handle?: string; +} + +function describeAnimation(animation: GsapAnimation): InspectAnimation { + return { + animationId: animation.id, + method: animation.method, + target: animation.targetSelector, + position: animation.position, + duration: animation.duration ?? null, + ease: animation.ease ?? null, + properties: animation.properties, + hasKeyframes: animation.keyframes !== undefined, + hasArcPath: animation.arcPath !== undefined, + }; +} + +function describe( + selection: DomEditSelection, + deps: InspectToolDeps, + isCurrentSelection: boolean, +): ToolResult { + const { capabilities } = selection; + const gsap = deps.getGsapDiagnostics(); + + // Only the CURRENT selection's animations are parsed by Studio. Reporting + // them for some other element would be reporting the wrong element's motion, + // which is worse than reporting none. + const animations = isCurrentSelection ? gsap.animations.map(describeAnimation) : []; + + let animationEditingBlocked: string | null = null; + if (!isCurrentSelection) { + animationEditingBlocked = "animations are only readable for the current selection"; + } else if (gsap.multipleTimelines) { + animationEditingBlocked = "this composition has multiple GSAP timelines"; + } else if (gsap.unsupportedTimelinePattern) { + animationEditingBlocked = "this composition's timeline pattern is not editable by Studio"; + } + + return toolOk({ + handle: mintElementHandle(patchTargetAddress(selection)), + label: selection.label, + tagName: selection.tagName, + sourceFile: selection.sourceFile, + box: selection.boundingBox, + text: selection.textContent, + textFields: selection.textFields.map((field) => ({ + key: field.key, + label: field.label, + value: field.value, + tagName: field.tagName, + })), + styles: selection.computedStyles, + inlineStyles: selection.inlineStyles, + dataAttributes: selection.dataAttributes, + can: { + editStyles: capabilities.canEditStyles, + move: capabilities.canMove || capabilities.canApplyManualOffset, + resize: capabilities.canResize || capabilities.canApplyManualSize, + rotate: capabilities.canApplyManualRotation, + crop: capabilities.canCrop, + editText: selection.textFields.length > 0, + reasonIfDisabled: capabilities.reasonIfDisabled ?? null, + }, + animations, + animationEditingBlocked, + isCurrentSelection, + }); +} + +export async function studioInspect( + deps: InspectToolDeps, + input: StudioInspectInput = {}, +): Promise> { + const current = deps.getCurrentSelection(); + + if (!input.handle) { + // An empty result here would assert "this element has nothing", which is a + // different and false claim from "you did not tell me which element". + if (!current) { + return toolFailure( + "invalid", + "nothing is selected and no handle was given", + "Pass a handle from studio_look, or call studio_select first.", + ); + } + return describe(current, deps, true); + } + + const doc = deps.getPreviewDocument(); + if (!doc) return toolFailure("blocked", "the preview is not mounted yet"); + + const element = resolveElementHandle(doc, input.handle); + if (!element) { + return toolFailure( + "invalid", + `no element matches handle ${input.handle}`, + "Call studio_look for current handles.", + ); + } + + const selection = await deps.buildSelection(element); + if (!selection) { + return toolFailure("blocked", `${input.handle} resolved to an element Studio cannot inspect`); + } + + return describe(selection, deps, current?.element === element); +} + +export const STUDIO_INSPECT_INPUT_SCHEMA = { + type: "object", + properties: { + handle: { + type: "string", + description: "An element handle from studio_look. Omit to inspect the current selection.", + }, + }, + additionalProperties: false, +} as const; + +export const STUDIO_INSPECT_DESCRIPTION = [ + "Everything about one element: its resolved styles, its text fields, its box,", + "its GSAP animations, and crucially what it will and will not accept.", + "Read this BEFORE editing. `can` tells you which edits are possible and", + "`can.reasonIfDisabled` says why one is not, so you can avoid a write that would be refused.", + "Animations are only readable for the CURRENT selection; `animationEditingBlocked` says when", + "and why animation editing is unavailable.", + "Returns `ok: true`, or `ok: false` with `kind`, `reason` and a `hint`.", +].join(" "); diff --git a/packages/studio/src/webmcp/tools/selectionTools.test.ts b/packages/studio/src/webmcp/tools/selectionTools.test.ts index c6ec5fdd2..07168529a 100644 --- a/packages/studio/src/webmcp/tools/selectionTools.test.ts +++ b/packages/studio/src/webmcp/tools/selectionTools.test.ts @@ -1,6 +1,5 @@ // @vitest-environment jsdom import { describe, expect, it, vi } from "vitest"; -import type { DomEditSelection } from "../../components/editor/domEditingTypes"; import { studioSeek, studioSelect, @@ -8,46 +7,7 @@ import { type StudioSeekResult, type StudioSelectResult, } from "./selectionTools"; -import type { ToolFailure, ToolResult } from "../toolResult"; - -function previewDoc(html: string): Document { - const iframe = document.createElement("iframe"); - document.body.append(iframe); - const doc = iframe.contentDocument; - if (!doc) throw new Error("expected iframe document"); - doc.body.innerHTML = html; - return doc; -} - -function selectionFor(element: HTMLElement): DomEditSelection { - return { - id: element.id || undefined, - hfId: element.getAttribute("data-hf-id") ?? undefined, - element, - label: "Headline", - tagName: element.tagName.toLowerCase(), - sourceFile: "index.html", - compositionPath: "index.html", - isCompositionHost: false, - isInsideLockedComposition: false, - boundingBox: { x: 40, y: 12, width: 880, height: 96 }, - textContent: element.textContent, - dataAttributes: {}, - inlineStyles: {}, - computedStyles: {}, - textFields: [], - capabilities: { - canSelect: true, - canEditStyles: true, - canCrop: true, - canMove: true, - canResize: true, - canApplyManualOffset: true, - canApplyManualSize: true, - canApplyManualRotation: true, - }, - }; -} +import { expectFailure, expectOk, previewDoc, selectionFor } from "../webmcpTestUtils"; function selectionDeps(overrides: Partial = {}): SelectionToolDeps { return { @@ -60,16 +20,6 @@ function selectionDeps(overrides: Partial = {}): SelectionToo }; } -function expectFailure(result: ToolResult): ToolFailure { - if (result.ok) throw new Error(`expected failure, got ${JSON.stringify(result)}`); - return result; -} - -function expectOk(result: ToolResult): { ok: true } & T { - if (!result.ok) throw new Error(`expected ok, got ${JSON.stringify(result)}`); - return result; -} - describe("studioSelect", () => { it("applies the selection a click would produce and reports it back", async () => { const doc = previewDoc('

Ship it

'); diff --git a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx index d9c7dd408..3a9a3ee98 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx +++ b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx @@ -42,6 +42,12 @@ function deps(overrides: Partial = {}): StudioAgentToolsDe getCompositionPath: () => "index.html", probeFrame: async () => ({ ok: true, status: 200 }), wait: async () => undefined, + getCurrentSelection: () => null, + getGsapDiagnostics: () => ({ + animations: [], + multipleTimelines: false, + unsupportedTimelinePattern: false, + }), ...overrides, }; } @@ -107,6 +113,7 @@ describe("useStudioAgentTools", () => { "studio_select", "studio_seek", "studio_frame", + "studio_inspect", ]); expect(trackEvent).toHaveBeenCalledWith("webmcp.native_present"); }); @@ -121,14 +128,14 @@ describe("useStudioAgentTools", () => { await act(async () => { harness = mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(4); + expect(registerTool).toHaveBeenCalledTimes(5); await act(async () => { harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 5 }) })); harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 6 }) })); }); - expect(registerTool).toHaveBeenCalledTimes(4); + expect(registerTool).toHaveBeenCalledTimes(5); }); it("executes against the LATEST deps, not the ones present at registration", async () => { @@ -201,7 +208,7 @@ describe("useStudioAgentTools", () => { mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(4); + expect(registerTool).toHaveBeenCalledTimes(5); }); it("reports a non-abort registration failure through production telemetry", async () => { diff --git a/packages/studio/src/webmcp/useStudioAgentTools.ts b/packages/studio/src/webmcp/useStudioAgentTools.ts index 9af5b8cc6..c5e1a444e 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.ts +++ b/packages/studio/src/webmcp/useStudioAgentTools.ts @@ -33,6 +33,14 @@ import { type StudioFrameInput, type StudioFrameResult, } from "./tools/frameTools"; +import { + studioInspect, + STUDIO_INSPECT_DESCRIPTION, + STUDIO_INSPECT_INPUT_SCHEMA, + type InspectToolDeps, + type StudioInspectInput, + type StudioInspectResult, +} from "./tools/inspectTools"; const log = makeStudioDebugLogger("webmcp"); @@ -46,7 +54,7 @@ function reportRegistration(report: ToolRegistrationReport, native: boolean): vo } } -export interface StudioAgentToolsDeps extends SelectionToolDeps, FrameToolDeps { +export interface StudioAgentToolsDeps extends SelectionToolDeps, FrameToolDeps, InspectToolDeps { /** Read Studio's current state. Called per tool invocation, never cached. */ getSnapshot: () => StudioLookSnapshot; } @@ -107,6 +115,17 @@ function buildStudioTools(depsRef: { readonly current: StudioAgentToolsDeps }): execute: (input): Promise> => runToolBody("studio_frame", () => studioFrame(depsRef.current, input as StudioFrameInput)), }, + { + name: "studio_inspect", + title: "Inspect one element", + description: STUDIO_INSPECT_DESCRIPTION, + inputSchema: STUDIO_INSPECT_INPUT_SCHEMA, + annotations: { readOnlyHint: true, untrustedContentHint: true }, + execute: (input): Promise> => + runToolBody("studio_inspect", () => + studioInspect(depsRef.current, input as StudioInspectInput), + ), + }, ]; } diff --git a/packages/studio/src/webmcp/webmcpTestUtils.ts b/packages/studio/src/webmcp/webmcpTestUtils.ts new file mode 100644 index 000000000..8f4f5e96b --- /dev/null +++ b/packages/studio/src/webmcp/webmcpTestUtils.ts @@ -0,0 +1,91 @@ +/** + * Shared fixtures for the WebMCP tool tests. + * + * Not a `.test` file so vitest does not collect it as a suite. Mirrors the + * existing `hooks/domSelectionTestHarness.ts` convention. + */ + +import { expect } from "vitest"; +import type { DomEditSelection } from "../components/editor/domEditingTypes"; +import type { ToolFailure, ToolResult } from "./toolResult"; + +/** + * An element inside a real iframe, which is where Studio's chrome expects to + * find preview elements. The separate realm matters: a preview element is not + * an instance of Studio's own `HTMLElement`. + */ +export function previewDoc(html: string): Document { + const iframe = document.createElement("iframe"); + document.body.append(iframe); + const doc = iframe.contentDocument; + if (!doc) throw new Error("expected iframe document"); + doc.body.innerHTML = html; + return doc; +} + +export function previewElement(html: string, id: string): HTMLElement { + const doc = previewDoc(html); + const element = doc.getElementById(id); + const HTMLElementCtor = doc.defaultView?.HTMLElement; + if (!HTMLElementCtor || !(element instanceof HTMLElementCtor)) { + throw new Error(`expected preview element #${id}`); + } + return element; +} + +export function selectionFor( + element: HTMLElement, + overrides: Partial = {}, +): DomEditSelection { + return { + id: element.id || undefined, + hfId: element.getAttribute("data-hf-id") ?? undefined, + element, + label: "Headline", + tagName: element.tagName.toLowerCase(), + sourceFile: "index.html", + compositionPath: "index.html", + isCompositionHost: false, + isInsideLockedComposition: false, + boundingBox: { x: 40, y: 12, width: 880, height: 96 }, + textContent: element.textContent, + dataAttributes: { "data-role": "title" }, + inlineStyles: { color: "red" }, + computedStyles: { "font-size": "42.7px", color: "rgb(255, 0, 0)" }, + textFields: [ + { + key: "self", + label: "Text", + value: element.textContent ?? "", + tagName: element.tagName.toLowerCase(), + attributes: [], + inlineStyles: {}, + computedStyles: {}, + source: "self", + }, + ], + capabilities: { + canSelect: true, + canEditStyles: true, + canCrop: true, + canMove: true, + canResize: true, + canApplyManualOffset: true, + canApplyManualSize: true, + canApplyManualRotation: true, + }, + ...overrides, + }; +} + +export function expectOk(result: ToolResult): { ok: true } & T { + expect(result.ok, `expected ok, got ${JSON.stringify(result)}`).toBe(true); + if (!result.ok) throw new Error("unreachable"); + return result; +} + +export function expectFailure(result: ToolResult): ToolFailure { + expect(result.ok, `expected failure, got ${JSON.stringify(result)}`).toBe(false); + if (result.ok) throw new Error("unreachable"); + return result; +} From f766c84c663a240e7b082761aead311a3fa5bb68 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Wed, 26 Aug 2026 20:14:51 -0400 Subject: [PATCH 2/3] feat(studio): let an agent edit text and styles, guarded The first tools that change the composition. Both act on the current selection and take no handle, which is forced rather than chosen: the handlers read the ambient React selection, and `applyDomSelection` only schedules a state update, so selecting and committing inside one call would write to whatever was selected before. Select first, then edit. Also plumbs the write-blocked state, which was the blocker for shipping any write at all. `domEditSaveQueuePaused` and the external-file conflict both lived on App and were unreachable from the tool surface, so `canWrite` was optimistic and a comment said so. They now derive into a single `writeBlockedReason` on the shell context: one field, one owner, conflict taking precedence because resolving it is what unblocks the queue. That guard matters more than it looks. Both states are BANNERS in Studio with no lock behind them, so nothing else was stopping a programmatic write from landing on top of a conflict the user had been asked to adjudicate. Three things the tools refuse to fake: They check the outcome, not the absence of a throw. Studio has several paths where a failed commit resolves anyway, so awaiting the handler proves nothing. The tagged outcome added earlier is what proves the write landed. A partial style result is reported as partial. `handleDomStyleCommit` is one property per call, so N properties are N commits; the result carries `applied` and `rejected` maps rather than a single boolean that would have to pick a side. Style commits run sequentially, never concurrently. Two commits racing through Studio's client-side read-modify-write can record undo entries that both claim the same starting content. There is a test that measures concurrency rather than trusting the loop. Every decline reason maps to a hint naming what to do instead, so a refusal routes the agent rather than just stopping it. --- packages/studio/src/App.tsx | 2 + .../studio/src/contexts/StudioContext.tsx | 10 + .../studio/src/hooks/useStudioContextValue.ts | 9 + .../studio/src/webmcp/StudioAgentTools.tsx | 17 +- .../src/webmcp/tools/contentTools.test.ts | 201 ++++++++++++++++++ .../studio/src/webmcp/tools/contentTools.ts | 184 ++++++++++++++++ .../src/webmcp/useStudioAgentTools.test.tsx | 11 +- .../studio/src/webmcp/useStudioAgentTools.ts | 32 ++- 8 files changed, 459 insertions(+), 7 deletions(-) create mode 100644 packages/studio/src/webmcp/tools/contentTools.test.ts create mode 100644 packages/studio/src/webmcp/tools/contentTools.ts diff --git a/packages/studio/src/App.tsx b/packages/studio/src/App.tsx index 6b23e6752..4da005b94 100644 --- a/packages/studio/src/App.tsx +++ b/packages/studio/src/App.tsx @@ -433,6 +433,8 @@ export function StudioApp() { handleRedo: appHotkeys.handleRedo, renderQueue, compositionDimensions, + domEditSaveQueuePaused: previewPersistence.domEditSaveQueuePaused, + externalFileConflict: externalFileChanges.blocked !== null, waitForPendingDomEditSaves: previewPersistence.waitForPendingDomEditSaves, handlePreviewIframeRef, refreshPreviewDocumentVersion, diff --git a/packages/studio/src/contexts/StudioContext.tsx b/packages/studio/src/contexts/StudioContext.tsx index 97b8ac0be..291219838 100644 --- a/packages/studio/src/contexts/StudioContext.tsx +++ b/packages/studio/src/contexts/StudioContext.tsx @@ -16,6 +16,13 @@ export interface StudioShellValue { undoLabel: string | undefined; redoLabel: string | undefined; }; + /** + * Why a composition write would be refused right now, or null when writes + * are possible. Derived from the paused save queue and the external-file + * conflict state, both of which are otherwise banners with no lock behind + * them. One field rather than two, so there is one owner of the question. + */ + writeBlockedReason: string | null; handleUndo: () => Promise; handleRedo: () => Promise; renderQueue: { @@ -106,6 +113,7 @@ export function StudioShellProvider({ showToast, previewIframeRef, editHistory, + writeBlockedReason, handleUndo, handleRedo, renderQueue, @@ -122,6 +130,7 @@ export function StudioShellProvider({ showToast, previewIframeRef, editHistory, + writeBlockedReason, handleUndo, handleRedo, renderQueue, @@ -138,6 +147,7 @@ export function StudioShellProvider({ setActiveCompPath, showToast, previewIframeRef, + writeBlockedReason, handleUndo, handleRedo, waitForPendingDomEditSaves, diff --git a/packages/studio/src/hooks/useStudioContextValue.ts b/packages/studio/src/hooks/useStudioContextValue.ts index 9553d82d0..1f1250c4f 100644 --- a/packages/studio/src/hooks/useStudioContextValue.ts +++ b/packages/studio/src/hooks/useStudioContextValue.ts @@ -25,6 +25,10 @@ interface StudioContextInput { // fields around it: the context type owns it. renderQueue: StudioContextValue["renderQueue"]; compositionDimensions: { width: number; height: number } | null; + /** Message from `usePreviewPersistence` when auto-save is paused. */ + domEditSaveQueuePaused: string | null; + /** True when an external edit to the open file is awaiting the user's decision. */ + externalFileConflict: boolean; waitForPendingDomEditSaves: () => Promise; handlePreviewIframeRef: (iframe: HTMLIFrameElement | null) => void; refreshPreviewDocumentVersion: () => void; @@ -46,6 +50,11 @@ export function buildStudioContextValue(input: StudioContextInput): StudioContex timelineElements: input.timelineElements, isPlaying: input.isPlaying, editHistory: input.editHistory, + // Conflict first: when both are true the conflict is the one the user has + // been asked to decide, and resolving it is what unblocks the queue. + writeBlockedReason: input.externalFileConflict + ? "an external change to this file is waiting to be resolved" + : input.domEditSaveQueuePaused, handleUndo: input.handleUndo, handleRedo: input.handleRedo, renderQueue: input.renderQueue, diff --git a/packages/studio/src/webmcp/StudioAgentTools.tsx b/packages/studio/src/webmcp/StudioAgentTools.tsx index 4fbf55e68..c52ee655b 100644 --- a/packages/studio/src/webmcp/StudioAgentTools.tsx +++ b/packages/studio/src/webmcp/StudioAgentTools.tsx @@ -17,15 +17,20 @@ import type { StudioLookSnapshot } from "./tools/lookTools"; * every animation frame during playback for a value nothing here displays. */ export function StudioAgentTools() { - const { projectId, activeCompPath, editHistory } = useStudioShellContext(); + const { projectId, activeCompPath, editHistory, writeBlockedReason } = useStudioShellContext(); const { domEditSelection, selectedGsapAnimations, gsapMultipleTimelines, gsapUnsupportedTimelinePattern, } = useDomEditSelectionContext(); - const { previewIframeRef, buildDomSelectionFromTarget, applyDomSelection } = - useDomEditActionsContext(); + const { + previewIframeRef, + buildDomSelectionFromTarget, + applyDomSelection, + handleDomTextCommit, + handleDomStyleCommit, + } = useDomEditActionsContext(); const getSnapshot = useCallback((): StudioLookSnapshot => { const player = usePlayerStore.getState(); @@ -77,6 +82,9 @@ export function StudioAgentTools() { }, wait: (ms) => new Promise((resolve) => setTimeout(resolve, ms)), getCurrentSelection: () => domEditSelection, + getWriteBlockedReason: () => writeBlockedReason, + setText: (value, fieldKey) => handleDomTextCommit(value, fieldKey), + setStyle: (property, value) => handleDomStyleCommit(property, value), getGsapDiagnostics: () => ({ animations: selectedGsapAnimations, multipleTimelines: gsapMultipleTimelines, @@ -90,6 +98,9 @@ export function StudioAgentTools() { applyDomSelection, projectId, activeCompPath, + writeBlockedReason, + handleDomTextCommit, + handleDomStyleCommit, domEditSelection, selectedGsapAnimations, gsapMultipleTimelines, diff --git a/packages/studio/src/webmcp/tools/contentTools.test.ts b/packages/studio/src/webmcp/tools/contentTools.test.ts new file mode 100644 index 000000000..7b5d825ad --- /dev/null +++ b/packages/studio/src/webmcp/tools/contentTools.test.ts @@ -0,0 +1,201 @@ +// @vitest-environment jsdom +import { describe, expect, it, vi } from "vitest"; +import { + studioSetStyle, + studioSetText, + type ContentToolDeps, + type StudioSetStyleResult, + type StudioSetTextResult, +} from "./contentTools"; +import { expectFailure, expectOk, previewElement, selectionFor } from "../webmcpTestUtils"; + +function contentDeps(overrides: Partial = {}): ContentToolDeps { + const element = previewElement('

Ship it

', "headline"); + return { + getCurrentSelection: () => selectionFor(element), + getWriteBlockedReason: () => null, + setText: async () => ({ ok: true }), + setStyle: async () => ({ ok: true }), + ...overrides, + }; +} + +describe("studioSetText", () => { + it("writes the text and reports what it now is", async () => { + const setText = vi.fn(async () => ({ ok: true }) as const); + + const result = await studioSetText(contentDeps({ setText }), { text: "Ship it faster" }); + + const ok = expectOk(result); + expect(ok.text).toBe("Ship it faster"); + expect(ok.changed).toBe(true); + expect(setText).toHaveBeenCalledWith("Ship it faster", undefined); + }); + + it("reports changed:false when the text already said that", async () => { + const result = await studioSetText(contentDeps(), { text: "Ship it" }); + + expect(expectOk(result).changed).toBe(false); + }); + + it("refuses to write while a conflict is waiting for the user", async () => { + // The paused-save and conflict states are banners with no lock behind them. + // Nothing else stops a programmatic write landing on top of a decision the + // user has been asked to make. + const setText = vi.fn(); + + const result = expectFailure( + await studioSetText( + contentDeps({ + getWriteBlockedReason: () => "an external change to this file is waiting to be resolved", + setText, + }), + { text: "Ship it faster" }, + ), + ); + + expect(result.kind).toBe("blocked"); + expect(result.reason).toMatch(/external change/); + expect(setText).not.toHaveBeenCalled(); + }); + + it("does not report success when the commit declined", async () => { + // The whole reason the handlers now return an outcome: they resolve on + // failure, so awaiting them proves nothing. + const result = expectFailure( + await studioSetText( + contentDeps({ setText: async () => ({ ok: false, reason: "persist-failed" }) }), + { text: "Ship it faster" }, + ), + ); + + expect(result.kind).toBe("failed"); + expect(result.reason).toMatch(/persist-failed/); + }); + + it("turns a decline reason into a hint naming what to do instead", async () => { + const result = expectFailure( + await studioSetText( + contentDeps({ setText: async () => ({ ok: false, reason: "not-text-editable" }) }), + { text: "x" }, + ), + ); + + expect(result.kind).toBe("blocked"); + expect(result.hint).toMatch(/studio_inspect/); + }); + + it("rejects a non-string text without dispatching", async () => { + const setText = vi.fn(); + + const result = expectFailure(await studioSetText(contentDeps({ setText }), { text: 42 })); + + expect(result.kind).toBe("invalid"); + expect(setText).not.toHaveBeenCalled(); + }); + + it("fails when nothing is selected", async () => { + const setText = vi.fn(); + + const result = expectFailure( + await studioSetText(contentDeps({ getCurrentSelection: () => null, setText }), { text: "x" }), + ); + + expect(result.kind).toBe("invalid"); + expect(result.hint).toMatch(/studio_select/); + expect(setText).not.toHaveBeenCalled(); + }); +}); + +describe("studioSetStyle", () => { + it("applies every property and reports them", async () => { + const setStyle = vi.fn(async () => ({ ok: true }) as const); + + const result = await studioSetStyle(contentDeps({ setStyle }), { + styles: { color: "red", "font-size": "48px" }, + }); + + const ok = expectOk(result); + expect(ok.applied).toEqual({ color: "red", "font-size": "48px" }); + expect(ok.rejected).toEqual({}); + expect(setStyle).toHaveBeenCalledTimes(2); + }); + + it("commits sequentially, never concurrently", async () => { + // Two commits racing through Studio's client-side read-modify-write can + // record undo entries that both claim the same starting content. + let inFlight = 0; + let maxInFlight = 0; + const setStyle = vi.fn(async () => { + inFlight += 1; + maxInFlight = Math.max(maxInFlight, inFlight); + await Promise.resolve(); + inFlight -= 1; + return { ok: true } as const; + }); + + await studioSetStyle(contentDeps({ setStyle }), { + styles: { color: "red", "font-size": "48px", opacity: "0.5" }, + }); + + expect(maxInFlight).toBe(1); + }); + + it("reports a partial success as partial, not whole", async () => { + const setStyle = vi.fn(async (property: string) => + property === "left" + ? ({ ok: false, reason: "geometry-property" } as const) + : ({ ok: true } as const), + ); + + const result = await studioSetStyle(contentDeps({ setStyle }), { + styles: { color: "red", left: "10px" }, + }); + + const ok = expectOk(result); + expect(ok.applied).toEqual({ color: "red" }); + expect(ok.rejected).toEqual({ left: "geometry-property" }); + }); + + it("fails when every property was refused", async () => { + const result = expectFailure( + await studioSetStyle( + contentDeps({ setStyle: async () => ({ ok: false, reason: "styles-not-editable" }) }), + { styles: { color: "red" } }, + ), + ); + + expect(result.kind).toBe("blocked"); + expect(result.reason).toMatch(/styles-not-editable/); + }); + + it("rejects an empty styles object rather than committing nothing", async () => { + const setStyle = vi.fn(); + + const result = expectFailure(await studioSetStyle(contentDeps({ setStyle }), { styles: {} })); + + expect(result.kind).toBe("invalid"); + expect(setStyle).not.toHaveBeenCalled(); + }); + + it("rejects a non-object styles value", async () => { + for (const styles of ["color: red", 42, null, ["color"]]) { + const result = expectFailure(await studioSetStyle(contentDeps(), { styles })); + expect(result.kind).toBe("invalid"); + } + }); + + it("refuses to write while a conflict is waiting for the user", async () => { + const setStyle = vi.fn(); + + const result = expectFailure( + await studioSetStyle( + contentDeps({ getWriteBlockedReason: () => "Auto-save is paused", setStyle }), + { styles: { color: "red" } }, + ), + ); + + expect(result.kind).toBe("blocked"); + expect(setStyle).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/studio/src/webmcp/tools/contentTools.ts b/packages/studio/src/webmcp/tools/contentTools.ts new file mode 100644 index 000000000..6e5a85b94 --- /dev/null +++ b/packages/studio/src/webmcp/tools/contentTools.ts @@ -0,0 +1,184 @@ +/** + * `studio_set_text` and `studio_set_style`: the first tools that change the file. + * + * Both operate on the CURRENT selection and take no handle. That is not an + * omission. `handleDomTextCommit(value, fieldKey?)` and + * `handleDomStyleCommit(property, value)` read the ambient React selection, and + * `applyDomSelection` only schedules a state update, so selecting and + * committing inside one call would write to whatever was selected before. + * Two tool calls are separated by a render. Select first, then edit. + * + * Every write here is guarded before dispatch and verified after. Studio has + * several paths where a failed commit resolves anyway, so "the function did not + * throw" proves nothing; the outcome the handler now returns is what proves it. + */ + +import type { DomEditCommitOutcome } from "../../hooks/domEditCommitRunner"; +import type { DomEditSelection } from "../../components/editor/domEditingTypes"; +import { toolFailure, toolOk, type ToolFailure, type ToolResult } from "../toolResult"; + +export interface ContentToolDeps { + getCurrentSelection: () => DomEditSelection | null; + /** Why a write would be refused right now, or null. Checked BEFORE dispatch. */ + getWriteBlockedReason: () => string | null; + setText: (value: string, fieldKey?: string) => Promise; + setStyle: (property: string, value: string) => Promise; +} + +/** + * The reasons a commit declines, translated into something an agent can act on. + * `persist-failed` is exogenous; the rest are states it should route around. + */ +const DECLINE_HINTS: Record = { + "no-selection": { kind: "invalid", hint: "Call studio_select first." }, + "no-project": { kind: "blocked" }, + "geometry-property": { + kind: "blocked", + hint: "Position and size are not editable as styles. Use the transform tools.", + }, + "styles-not-editable": { + kind: "blocked", + hint: "studio_inspect reports why, in can.reasonIfDisabled.", + }, + "not-text-editable": { + kind: "blocked", + hint: "This element has no editable text. studio_inspect lists its textFields.", + }, + "persist-failed": { kind: "failed", hint: "The write did not reach the file. Check Studio." }, +}; + +function fromOutcome(outcome: DomEditCommitOutcome, what: string): ToolFailure | null { + if (outcome.ok) return null; + const mapped = DECLINE_HINTS[outcome.reason] ?? { kind: "failed" as const }; + return toolFailure(mapped.kind, `${what} was not applied: ${outcome.reason}`, mapped.hint); +} + +function guardWrite(deps: ContentToolDeps): ToolFailure | null { + // Both blocked states are banners in Studio's UI with no lock behind them, so + // nothing else stops a programmatic write from landing on top of a conflict + // the user has been asked to adjudicate. + const blocked = deps.getWriteBlockedReason(); + if (blocked) { + return toolFailure("blocked", blocked, "Resolve it in Studio, then retry."); + } + if (!deps.getCurrentSelection()) { + return toolFailure("invalid", "nothing is selected", "Call studio_select first."); + } + return null; +} + +export interface StudioSetTextResult { + text: string; + changed: boolean; +} + +export async function studioSetText( + deps: ContentToolDeps, + input: { text?: unknown; field?: unknown }, +): Promise> { + if (typeof input.text !== "string") { + return toolFailure("invalid", "text must be a string"); + } + const field = typeof input.field === "string" && input.field ? input.field : undefined; + + const blocked = guardWrite(deps); + if (blocked) return blocked; + + const before = deps.getCurrentSelection()?.textContent ?? null; + const outcome = await deps.setText(input.text, field); + const failure = fromOutcome(outcome, "the text"); + if (failure) return failure; + + return toolOk({ text: input.text, changed: before !== input.text }); +} + +export interface StudioSetStyleResult { + applied: Record; + /** Properties the element refused, with the reason. Empty when all landed. */ + rejected: Record; +} + +export async function studioSetStyle( + deps: ContentToolDeps, + input: { styles?: unknown }, +): Promise> { + const styles = input.styles; + if (typeof styles !== "object" || styles === null || Array.isArray(styles)) { + return toolFailure("invalid", "styles must be an object of CSS property to value"); + } + const entries = Object.entries(styles).filter( + (entry): entry is [string, string] => typeof entry[1] === "string", + ); + if (entries.length === 0) { + // An empty commit would report success having done nothing. + return toolFailure("invalid", "styles must contain at least one string value"); + } + + const blocked = guardWrite(deps); + if (blocked) return blocked; + + // `handleDomStyleCommit` is one property per call, so N properties are N + // commits and N undo entries. Sequential, not concurrent: two commits racing + // through Studio's client-side read-modify-write can record undo entries that + // both claim the same starting content. + const applied: Record = {}; + const rejected: Record = {}; + for (const [property, value] of entries) { + const outcome = await deps.setStyle(property, value); + if (outcome.ok) applied[property] = value; + else rejected[property] = outcome.reason; + } + + if (Object.keys(applied).length === 0) { + const reasons = Object.entries(rejected) + .map(([property, reason]) => `${property}: ${reason}`) + .join(", "); + return toolFailure("blocked", `no style was applied (${reasons})`); + } + + return toolOk({ applied, rejected }); +} + +export const STUDIO_SET_TEXT_INPUT_SCHEMA = { + type: "object", + properties: { + text: { type: "string", description: "The new text content." }, + field: { + type: "string", + description: + "Which text field to write, from studio_inspect. Omit for the element's own text.", + }, + }, + required: ["text"], + additionalProperties: false, +} as const; + +export const STUDIO_SET_TEXT_DESCRIPTION = [ + "Set the text of the CURRENTLY SELECTED element. Call studio_select first.", + "This is the edit a synthetic double-click cannot reach, because Studio's canvas", + "takes pointer capture and recognises the double press itself.", + "Returns `ok: true` with the resulting text and whether it changed, or `ok: false`", + "with `kind`, `reason` and usually a `hint` naming what to do instead.", +].join(" "); + +export const STUDIO_SET_STYLE_INPUT_SCHEMA = { + type: "object", + properties: { + styles: { + type: "object", + description: 'CSS property to value, for example {"color": "red", "font-size": "48px"}.', + additionalProperties: { type: "string" }, + }, + }, + required: ["styles"], + additionalProperties: false, +} as const; + +export const STUDIO_SET_STYLE_DESCRIPTION = [ + "Set inline styles on the CURRENTLY SELECTED element. Call studio_select first.", + "Each property is a separate commit, so N properties produce N undo entries.", + "Position and size properties (left, top, width, height) are refused here on purpose;", + "they belong to the transform tools.", + "Returns `ok: true` with `applied` and `rejected` maps, so a partial success is visible", + "as a partial success rather than reported as a whole one.", +].join(" "); diff --git a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx index 3a9a3ee98..8431d8426 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx +++ b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx @@ -43,6 +43,9 @@ function deps(overrides: Partial = {}): StudioAgentToolsDe probeFrame: async () => ({ ok: true, status: 200 }), wait: async () => undefined, getCurrentSelection: () => null, + getWriteBlockedReason: () => null, + setText: async () => ({ ok: true }), + setStyle: async () => ({ ok: true }), getGsapDiagnostics: () => ({ animations: [], multipleTimelines: false, @@ -114,6 +117,8 @@ describe("useStudioAgentTools", () => { "studio_seek", "studio_frame", "studio_inspect", + "studio_set_text", + "studio_set_style", ]); expect(trackEvent).toHaveBeenCalledWith("webmcp.native_present"); }); @@ -128,14 +133,14 @@ describe("useStudioAgentTools", () => { await act(async () => { harness = mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(5); + expect(registerTool).toHaveBeenCalledTimes(7); await act(async () => { harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 5 }) })); harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 6 }) })); }); - expect(registerTool).toHaveBeenCalledTimes(5); + expect(registerTool).toHaveBeenCalledTimes(7); }); it("executes against the LATEST deps, not the ones present at registration", async () => { @@ -208,7 +213,7 @@ describe("useStudioAgentTools", () => { mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(5); + expect(registerTool).toHaveBeenCalledTimes(7); }); it("reports a non-abort registration failure through production telemetry", async () => { diff --git a/packages/studio/src/webmcp/useStudioAgentTools.ts b/packages/studio/src/webmcp/useStudioAgentTools.ts index c5e1a444e..a8f25c61c 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.ts +++ b/packages/studio/src/webmcp/useStudioAgentTools.ts @@ -41,6 +41,17 @@ import { type StudioInspectInput, type StudioInspectResult, } from "./tools/inspectTools"; +import { + studioSetStyle, + studioSetText, + STUDIO_SET_STYLE_DESCRIPTION, + STUDIO_SET_STYLE_INPUT_SCHEMA, + STUDIO_SET_TEXT_DESCRIPTION, + STUDIO_SET_TEXT_INPUT_SCHEMA, + type ContentToolDeps, + type StudioSetStyleResult, + type StudioSetTextResult, +} from "./tools/contentTools"; const log = makeStudioDebugLogger("webmcp"); @@ -54,7 +65,8 @@ function reportRegistration(report: ToolRegistrationReport, native: boolean): vo } } -export interface StudioAgentToolsDeps extends SelectionToolDeps, FrameToolDeps, InspectToolDeps { +export interface StudioAgentToolsDeps + extends SelectionToolDeps, FrameToolDeps, InspectToolDeps, ContentToolDeps { /** Read Studio's current state. Called per tool invocation, never cached. */ getSnapshot: () => StudioLookSnapshot; } @@ -126,6 +138,24 @@ function buildStudioTools(depsRef: { readonly current: StudioAgentToolsDeps }): studioInspect(depsRef.current, input as StudioInspectInput), ), }, + { + name: "studio_set_text", + title: "Set an element's text", + description: STUDIO_SET_TEXT_DESCRIPTION, + inputSchema: STUDIO_SET_TEXT_INPUT_SCHEMA, + annotations: { readOnlyHint: false, untrustedContentHint: true }, + execute: (input): Promise> => + runToolBody("studio_set_text", () => studioSetText(depsRef.current, input)), + }, + { + name: "studio_set_style", + title: "Set an element's styles", + description: STUDIO_SET_STYLE_DESCRIPTION, + inputSchema: STUDIO_SET_STYLE_INPUT_SCHEMA, + annotations: { readOnlyHint: false }, + execute: (input): Promise> => + runToolBody("studio_set_style", () => studioSetStyle(depsRef.current, input)), + }, ]; } From be6fc0b0a89ca07af957b7f6b07a024f4f9c7890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miguel=20=C3=81ngel?= Date: Sun, 30 Aug 2026 01:25:48 -0400 Subject: [PATCH 3/3] feat(studio): move, resize and rotate, verified by reading back (#3519) `studio_transform` does what a drag does, and then checks. The box in the result is READ BACK after the write, never echoed from the request, and `applied` lists what actually took effect. That is not belt-and-braces. The plan for this unit said to re-derive the geometry handlers' behaviour rather than trust any description of them, and doing that turned up three different behaviours behind one interface. The handlers on `DomEditActionsValue` are the GSAP-AWARE wrappers, aliased in `useDomEditSession.ts:534-538`, not the CSS ones in `useDomGeometryCommits.ts` that an earlier note in this workstream described. `handleGsapAwarePathOffsetCommit` and `handleGsapAwareRotationCommit` are `if (gsapCommitMutation) { ...intercept... }` with no else branch. Their own comments say the absence is deliberate: position and rotation are written as GSAP code and there is no CSS fallback to write to. So they can return having done nothing. `handleGsapAwareBoxSizeCommit` is not like the other two. It runs through `runGestureTransaction` with separate scale and width/height routes, so resize works more generally. Reading back is what turns that middle case from a silent lie into a reported one. A move that did nothing comes back in `unchanged` with a reason. Three smaller decisions: Operations re-read between each other, so a move is judged against the box AFTER a resize in the same call. Comparing against the original would credit the resize's change to the move. Rotation is reported as dispatched, not verified. `rotate` is an individual transform property and does not appear in the computed transform, so there is no honest box-derived signal, and claiming one would be worse than saying so. x pairs with y and width pairs with height. Accepting one alone would mean inventing the other from the current value, which moves the element somewhere the caller did not ask for. The pairing rule and its minimum live in one `parsePair` helper rather than as four separate branches. --- .../studio/src/webmcp/StudioAgentTools.tsx | 15 ++ .../src/webmcp/tools/transformTools.test.ts | 179 +++++++++++++++ .../studio/src/webmcp/tools/transformTools.ts | 205 ++++++++++++++++++ .../src/webmcp/useStudioAgentTools.test.tsx | 11 +- .../studio/src/webmcp/useStudioAgentTools.ts | 21 +- 5 files changed, 427 insertions(+), 4 deletions(-) create mode 100644 packages/studio/src/webmcp/tools/transformTools.test.ts create mode 100644 packages/studio/src/webmcp/tools/transformTools.ts diff --git a/packages/studio/src/webmcp/StudioAgentTools.tsx b/packages/studio/src/webmcp/StudioAgentTools.tsx index c52ee655b..f201ca23b 100644 --- a/packages/studio/src/webmcp/StudioAgentTools.tsx +++ b/packages/studio/src/webmcp/StudioAgentTools.tsx @@ -30,6 +30,9 @@ export function StudioAgentTools() { applyDomSelection, handleDomTextCommit, handleDomStyleCommit, + handleDomPathOffsetCommit, + handleDomBoxSizeCommit, + handleDomRotationCommit, } = useDomEditActionsContext(); const getSnapshot = useCallback((): StudioLookSnapshot => { @@ -85,6 +88,15 @@ export function StudioAgentTools() { getWriteBlockedReason: () => writeBlockedReason, setText: (value, fieldKey) => handleDomTextCommit(value, fieldKey), setStyle: (property, value) => handleDomStyleCommit(property, value), + // Measured, not authored: the tool compares this before and after to + // tell a real change from a handler that did nothing and resolved. + readBox: (selection) => { + const rect = selection.element.getBoundingClientRect(); + return { x: rect.x, y: rect.y, width: rect.width, height: rect.height }; + }, + moveTo: (selection, next) => handleDomPathOffsetCommit(selection, next), + resizeTo: (selection, next) => handleDomBoxSizeCommit(selection, next), + rotateTo: (selection, next) => handleDomRotationCommit(selection, next), getGsapDiagnostics: () => ({ animations: selectedGsapAnimations, multipleTimelines: gsapMultipleTimelines, @@ -101,6 +113,9 @@ export function StudioAgentTools() { writeBlockedReason, handleDomTextCommit, handleDomStyleCommit, + handleDomPathOffsetCommit, + handleDomBoxSizeCommit, + handleDomRotationCommit, domEditSelection, selectedGsapAnimations, gsapMultipleTimelines, diff --git a/packages/studio/src/webmcp/tools/transformTools.test.ts b/packages/studio/src/webmcp/tools/transformTools.test.ts new file mode 100644 index 000000000..b438ed9e5 --- /dev/null +++ b/packages/studio/src/webmcp/tools/transformTools.test.ts @@ -0,0 +1,179 @@ +// @vitest-environment jsdom +import { describe, expect, it, vi } from "vitest"; +import { + studioTransform, + type ElementBox, + type StudioTransformResult, + type TransformToolDeps, +} from "./transformTools"; +import { expectFailure, expectOk, previewElement, selectionFor } from "../webmcpTestUtils"; + +/** + * A stand-in for the rendered box. happy-dom and jsdom report all-zero rects, + * so the box is injected rather than measured; these tests are about what the + * tool concludes from a box, not about layout. + */ +function boxStore(initial: ElementBox) { + const box = { ...initial }; + return { + read: () => ({ ...box }), + set: (next: Partial) => Object.assign(box, next), + }; +} + +function transformDeps(overrides: Partial = {}): TransformToolDeps { + const element = previewElement('

Ship it

', "headline"); + return { + getCurrentSelection: () => selectionFor(element), + getWriteBlockedReason: () => null, + readBox: () => ({ x: 0, y: 0, width: 100, height: 50 }), + moveTo: async () => undefined, + resizeTo: async () => undefined, + rotateTo: async () => undefined, + ...overrides, + }; +} + +describe("studioTransform", () => { + it("reports the box read back, not the box requested", async () => { + const store = boxStore({ x: 0, y: 0, width: 100, height: 50 }); + // The handler lands somewhere other than asked, which is what a clamp or a + // layout constraint does. + const resizeTo = vi.fn(async () => store.set({ width: 300, height: 120 })); + + const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo }), { + width: 999, + height: 999, + }); + + const ok = expectOk(result); + expect(ok.box.width).toBe(300); + expect(ok.box.height).toBe(120); + expect(ok.applied).toContain("resize"); + }); + + it("reports a silent no-op as unchanged instead of success", async () => { + // handleGsapAwarePathOffsetCommit is `if (gsapCommitMutation) {...}` with no + // else branch. Without GSAP it resolves having written nothing, and echoing + // the request back would be a lie the agent builds on. + const store = boxStore({ x: 10, y: 10, width: 100, height: 50 }); + const moveTo = vi.fn(async () => undefined); + + const result = expectFailure( + await studioTransform(transformDeps({ readBox: store.read, moveTo }), { x: 500, y: 400 }), + ); + + expect(moveTo).toHaveBeenCalled(); + expect(result.kind).toBe("blocked"); + expect(result.reason).toMatch(/did not move/); + expect(result.hint).toMatch(/GSAP/); + }); + + it("separates what landed from what did not, in one call", async () => { + const store = boxStore({ x: 0, y: 0, width: 100, height: 50 }); + const resizeTo = vi.fn(async () => store.set({ width: 200, height: 80 })); + const moveTo = vi.fn(async () => undefined); + + const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo, moveTo }), { + x: 40, + y: 40, + width: 200, + height: 80, + }); + + const ok = expectOk(result); + expect(ok.applied).toEqual(["resize"]); + expect(ok.unchanged.move).toMatch(/did not move/); + }); + + it("re-reads between operations so a later one sees the earlier result", async () => { + const store = boxStore({ x: 0, y: 0, width: 100, height: 50 }); + const resizeTo = vi.fn(async () => store.set({ width: 200, height: 80 })); + const moveTo = vi.fn(async () => store.set({ x: 40, y: 40 })); + + const result = await studioTransform(transformDeps({ readBox: store.read, resizeTo, moveTo }), { + x: 40, + y: 40, + width: 200, + height: 80, + }); + + // Move is judged against the box AFTER the resize. Comparing against the + // original would credit the resize's change to the move. + const ok = expectOk(result); + expect(ok.applied).toEqual(["resize", "move"]); + expect(ok.unchanged).toEqual({}); + }); + + it("reports rotation as dispatched rather than verified", async () => { + // `rotate` is an individual transform property and does not appear in the + // computed transform, so there is no honest box-derived signal for it. + const rotateTo = vi.fn(async () => undefined); + + const result = await studioTransform(transformDeps({ rotateTo }), { rotate: 15 }); + + const ok = expectOk(result); + expect(rotateTo).toHaveBeenCalledWith(expect.anything(), { angle: 15 }); + expect(ok.applied).toEqual(["rotate"]); + }); + + it("refuses to write while a conflict is waiting for the user", async () => { + const moveTo = vi.fn(); + + const result = expectFailure( + await studioTransform( + transformDeps({ getWriteBlockedReason: () => "Auto-save is paused", moveTo }), + { x: 10, y: 10 }, + ), + ); + + expect(result.kind).toBe("blocked"); + expect(moveTo).not.toHaveBeenCalled(); + }); + + it("requires x and y together, and width and height together", async () => { + const moveTo = vi.fn(); + const resizeTo = vi.fn(); + const deps = transformDeps({ moveTo, resizeTo }); + + expect(expectFailure(await studioTransform(deps, { x: 10 })).reason).toMatch(/together/); + expect(expectFailure(await studioTransform(deps, { width: 10 })).reason).toMatch(/together/); + expect(moveTo).not.toHaveBeenCalled(); + expect(resizeTo).not.toHaveBeenCalled(); + }); + + it("rejects a negative size and an empty request", async () => { + const deps = transformDeps(); + + expect(expectFailure(await studioTransform(deps, { width: -1, height: 10 })).kind).toBe( + "invalid", + ); + expect(expectFailure(await studioTransform(deps, {})).reason).toMatch(/at least one/); + }); + + it("rejects non-finite numbers rather than passing them to a handler", async () => { + const moveTo = vi.fn(); + + const result = expectFailure( + await studioTransform(transformDeps({ moveTo }), { x: Number.NaN, y: 10 }), + ); + + expect(result.kind).toBe("invalid"); + expect(moveTo).not.toHaveBeenCalled(); + }); + + it("fails when nothing is selected", async () => { + const moveTo = vi.fn(); + + const result = expectFailure( + await studioTransform(transformDeps({ getCurrentSelection: () => null, moveTo }), { + x: 1, + y: 1, + }), + ); + + expect(result.kind).toBe("invalid"); + expect(result.hint).toMatch(/studio_select/); + expect(moveTo).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/studio/src/webmcp/tools/transformTools.ts b/packages/studio/src/webmcp/tools/transformTools.ts new file mode 100644 index 000000000..0ea9bf670 --- /dev/null +++ b/packages/studio/src/webmcp/tools/transformTools.ts @@ -0,0 +1,205 @@ +/** + * `studio_transform`: move, resize and rotate, as a drag would. + * + * This tool reads the element's box back after every write and reports what + * ACTUALLY changed. That is not belt-and-braces, it is the only thing standing + * between an agent and a silent lie, because two of the three handlers can do + * nothing and resolve: + * + * - The handlers exposed on `DomEditActionsValue` are the GSAP-AWARE wrappers + * (`useDomEditSession.ts` aliases them), not the CSS ones in + * `useDomGeometryCommits.ts`. + * - `handleGsapAwarePathOffsetCommit` and `handleGsapAwareRotationCommit` are + * `if (gsapCommitMutation) { ...intercept... }` with NO else branch. In a + * composition with no GSAP they return having done nothing. The adjacent + * comments confirm that is deliberate: there is no CSS fallback to write to. + * - `handleGsapAwareBoxSizeCommit` is different. It runs through + * `runGestureTransaction` with a scale route and a width/height route, so + * resize works more generally than the other two. + * + * Read back, do not assume. + */ + +import type { DomEditSelection } from "../../components/editor/domEditingTypes"; +import { toolFailure, toolOk, type ToolFailure, type ToolResult } from "../toolResult"; + +export interface ElementBox { + x: number; + y: number; + width: number; + height: number; +} + +export interface TransformToolDeps { + getCurrentSelection: () => DomEditSelection | null; + getWriteBlockedReason: () => string | null; + /** The element's box as it renders right now. */ + readBox: (selection: DomEditSelection) => ElementBox; + moveTo: (selection: DomEditSelection, next: { x: number; y: number }) => Promise; + resizeTo: (selection: DomEditSelection, next: { width: number; height: number }) => Promise; + rotateTo: (selection: DomEditSelection, next: { angle: number }) => Promise; +} + +export interface StudioTransformInput { + x?: unknown; + y?: unknown; + width?: unknown; + height?: unknown; + rotate?: unknown; +} + +export interface StudioTransformResult { + /** The box as it renders after the write, read back, not echoed. */ + box: ElementBox; + applied: string[]; + /** Requested operations whose effect could not be observed, with why. */ + unchanged: Record; +} + +const NO_OP_HINT = + "Move and rotate are written as GSAP code; a composition with no GSAP timeline has nothing to write to. studio_inspect reports the element's animations."; + +function readNumber(value: unknown): number | null { + return typeof value === "number" && Number.isFinite(value) ? value : null; +} + +function guard(deps: TransformToolDeps): ToolFailure | null { + const blocked = deps.getWriteBlockedReason(); + if (blocked) return toolFailure("blocked", blocked, "Resolve it in Studio, then retry."); + if (!deps.getCurrentSelection()) { + return toolFailure("invalid", "nothing is selected", "Call studio_select first."); + } + return null; +} + +interface TransformRequest { + move: { x: number; y: number } | null; + size: { width: number; height: number } | null; + rotate: number | null; +} + +/** + * Both or neither. Accepting one axis alone would mean inventing the other from + * the current value, which moves the element somewhere the caller did not ask + * for. + */ +function parsePair( + a: unknown, + b: unknown, + names: [string, string], + min = Number.NEGATIVE_INFINITY, +): { pair: [number, number] | null } | ToolFailure { + const first = readNumber(a); + const second = readNumber(b); + if (first === null && second === null) return { pair: null }; + if (first === null || second === null) { + return toolFailure("invalid", `${names[0]} and ${names[1]} must be given together`); + } + if (first < min || second < min) { + return toolFailure("invalid", `${names[0]} and ${names[1]} must be at least ${min}`); + } + return { pair: [first, second] }; +} + +function isFailure(value: object): value is ToolFailure { + return "ok" in value; +} + +function parseRequest(input: StudioTransformInput): TransformRequest | ToolFailure { + const move = parsePair(input.x, input.y, ["x", "y"]); + if (isFailure(move)) return move; + const size = parsePair(input.width, input.height, ["width", "height"], 0); + if (isFailure(size)) return size; + const rotate = readNumber(input.rotate); + + if (!move.pair && !size.pair && rotate === null) { + return toolFailure( + "invalid", + "give at least one of x, y, width, height, rotate as a finite number", + ); + } + + return { + move: move.pair ? { x: move.pair[0], y: move.pair[1] } : null, + size: size.pair ? { width: size.pair[0], height: size.pair[1] } : null, + rotate, + }; +} + +export async function studioTransform( + deps: TransformToolDeps, + input: StudioTransformInput, +): Promise> { + const request = parseRequest(input); + if (isFailure(request)) return request; + + const blocked = guard(deps); + if (blocked) return blocked; + + const selection = deps.getCurrentSelection(); + if (!selection) return toolFailure("invalid", "nothing is selected"); + + const applied: string[] = []; + const unchanged: Record = {}; + + // Sequential, and each one re-reads first, so a move is judged against the box + // AFTER a resize in the same call rather than against the original. + if (request.size) { + const before = deps.readBox(selection); + await deps.resizeTo(selection, request.size); + const after = deps.readBox(selection); + if (after.width !== before.width || after.height !== before.height) applied.push("resize"); + else unchanged.resize = "the element's size did not change"; + } + + if (request.move) { + const before = deps.readBox(selection); + await deps.moveTo(selection, request.move); + const after = deps.readBox(selection); + if (after.x !== before.x || after.y !== before.y) applied.push("move"); + else unchanged.move = `the element did not move. ${NO_OP_HINT}`; + } + + if (request.rotate !== null) { + // Rotation is written as the CSS `rotate` property, an individual transform + // property that does NOT appear in getComputedStyle().transform. There is no + // reliable box-derived signal, so this is reported as dispatched rather than + // verified, and the description says so. + await deps.rotateTo(selection, { angle: request.rotate }); + applied.push("rotate"); + } + + if (applied.length === 0) { + return toolFailure( + "blocked", + `nothing changed: ${Object.values(unchanged).join("; ")}`, + NO_OP_HINT, + ); + } + + return toolOk({ box: deps.readBox(selection), applied, unchanged }); +} + +export const STUDIO_TRANSFORM_INPUT_SCHEMA = { + type: "object", + properties: { + x: { type: "number", description: "New x offset in pixels. Must be paired with y." }, + y: { type: "number", description: "New y offset in pixels. Must be paired with x." }, + width: { type: "number", minimum: 0, description: "New width. Must be paired with height." }, + height: { type: "number", minimum: 0, description: "New height. Must be paired with width." }, + rotate: { type: "number", description: "Rotation in degrees." }, + }, + additionalProperties: false, +} as const; + +export const STUDIO_TRANSFORM_DESCRIPTION = [ + "Move, resize or rotate the CURRENTLY SELECTED element, the way a drag would.", + "Call studio_select first. Give x with y, and width with height.", + "The result's `box` is READ BACK after the write, not echoed from your request, and", + "`applied` lists what actually took effect. Check it.", + "Move and rotate are written as GSAP code, so in a composition with no GSAP timeline they", + "do nothing; that shows up in `unchanged` rather than as a false success.", + "Rotation is reported as dispatched rather than verified, because the CSS `rotate` property", + "does not appear in the element's computed transform.", + "Returns `ok: true`, or `ok: false` with `kind`, `reason` and a `hint`.", +].join(" "); diff --git a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx index 8431d8426..274b697c9 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.test.tsx +++ b/packages/studio/src/webmcp/useStudioAgentTools.test.tsx @@ -46,6 +46,10 @@ function deps(overrides: Partial = {}): StudioAgentToolsDe getWriteBlockedReason: () => null, setText: async () => ({ ok: true }), setStyle: async () => ({ ok: true }), + readBox: () => ({ x: 0, y: 0, width: 100, height: 50 }), + moveTo: async () => undefined, + resizeTo: async () => undefined, + rotateTo: async () => undefined, getGsapDiagnostics: () => ({ animations: [], multipleTimelines: false, @@ -119,6 +123,7 @@ describe("useStudioAgentTools", () => { "studio_inspect", "studio_set_text", "studio_set_style", + "studio_transform", ]); expect(trackEvent).toHaveBeenCalledWith("webmcp.native_present"); }); @@ -133,14 +138,14 @@ describe("useStudioAgentTools", () => { await act(async () => { harness = mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(7); + expect(registerTool).toHaveBeenCalledTimes(8); await act(async () => { harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 5 }) })); harness?.rerenderWith(deps({ getSnapshot: () => snapshot({ currentTime: 6 }) })); }); - expect(registerTool).toHaveBeenCalledTimes(7); + expect(registerTool).toHaveBeenCalledTimes(8); }); it("executes against the LATEST deps, not the ones present at registration", async () => { @@ -213,7 +218,7 @@ describe("useStudioAgentTools", () => { mountTools(deps({ getSnapshot: () => snapshot() })); }); - expect(registerTool).toHaveBeenCalledTimes(7); + expect(registerTool).toHaveBeenCalledTimes(8); }); it("reports a non-abort registration failure through production telemetry", async () => { diff --git a/packages/studio/src/webmcp/useStudioAgentTools.ts b/packages/studio/src/webmcp/useStudioAgentTools.ts index a8f25c61c..f62c369bc 100644 --- a/packages/studio/src/webmcp/useStudioAgentTools.ts +++ b/packages/studio/src/webmcp/useStudioAgentTools.ts @@ -52,6 +52,14 @@ import { type StudioSetStyleResult, type StudioSetTextResult, } from "./tools/contentTools"; +import { + studioTransform, + STUDIO_TRANSFORM_DESCRIPTION, + STUDIO_TRANSFORM_INPUT_SCHEMA, + type StudioTransformInput, + type StudioTransformResult, + type TransformToolDeps, +} from "./tools/transformTools"; const log = makeStudioDebugLogger("webmcp"); @@ -66,7 +74,7 @@ function reportRegistration(report: ToolRegistrationReport, native: boolean): vo } export interface StudioAgentToolsDeps - extends SelectionToolDeps, FrameToolDeps, InspectToolDeps, ContentToolDeps { + extends SelectionToolDeps, FrameToolDeps, InspectToolDeps, ContentToolDeps, TransformToolDeps { /** Read Studio's current state. Called per tool invocation, never cached. */ getSnapshot: () => StudioLookSnapshot; } @@ -156,6 +164,17 @@ function buildStudioTools(depsRef: { readonly current: StudioAgentToolsDeps }): execute: (input): Promise> => runToolBody("studio_set_style", () => studioSetStyle(depsRef.current, input)), }, + { + name: "studio_transform", + title: "Move, resize or rotate", + description: STUDIO_TRANSFORM_DESCRIPTION, + inputSchema: STUDIO_TRANSFORM_INPUT_SCHEMA, + annotations: { readOnlyHint: false }, + execute: (input): Promise> => + runToolBody("studio_transform", () => + studioTransform(depsRef.current, input as StudioTransformInput), + ), + }, ]; }