@hyperframes/studio 0.9.52 → 0.9.54

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.
Files changed (70) hide show
  1. package/dist/assets/{hyperframes-player-D7LhzIg1.js → hyperframes-player-Oju91F6x.js} +2 -2
  2. package/dist/assets/{index-D3WxJaqm.js → index-B0VEzzD2.js} +180 -180
  3. package/dist/assets/index-Dv67Vrl6.js +74 -0
  4. package/dist/assets/{index-CQ7o0CBU.js → index-XEm_N45R.js} +1 -1
  5. package/dist/assets/{pausedVideoDecoder-BVUmBgn0.js → pausedVideoDecoder-p6zibxVM.js} +1 -1
  6. package/dist/{chunk-F6EHWNZU.js → chunk-K3IOYISK.js} +121 -94
  7. package/dist/{chunk-VHV2ZDKY.js → chunk-OOGQ5KCC.js} +7 -0
  8. package/dist/{chunk-GVW7AU57.js → chunk-UM4CLD4M.js} +1 -1
  9. package/dist/chunk-USW3YBOG.js +70 -0
  10. package/dist/{domEditingLayers-SA5KLSSS.js → domEditingLayers-IHYA42AJ.js} +2 -2
  11. package/dist/index.d.ts +14 -7
  12. package/dist/index.html +1 -1
  13. package/dist/index.js +110 -98
  14. package/dist/keys.d.ts +72 -1
  15. package/dist/keys.js +5 -4
  16. package/dist/player.d.ts +1 -0
  17. package/dist/player.js +2 -2
  18. package/package.json +9 -9
  19. package/src/captions/keyboard.ts +1 -1
  20. package/src/components/RightPanelTabs.test.tsx +1 -1
  21. package/src/components/StudioHeader.dom.test.tsx +1 -1
  22. package/src/components/TimelineHistoryButtons.dom.test.tsx +1 -1
  23. package/src/components/TimelineToolbar.tsx +1 -1
  24. package/src/components/editor/SnapToolbar.tsx +1 -1
  25. package/src/components/editor/gsapLivePreview.test.ts +19 -0
  26. package/src/components/editor/gsapLivePreview.ts +2 -0
  27. package/src/components/editor/manualEditingAvailability.ts +7 -0
  28. package/src/components/editor/useDomEditNudge.ts +1 -1
  29. package/src/components/nle/NLEPreview.test.ts +25 -0
  30. package/src/components/nle/NLEPreview.tsx +5 -0
  31. package/src/components/nle/PreviewPane.tsx +2 -0
  32. package/src/components/panels/SlideshowPanel.tsx +1 -1
  33. package/src/components/renders/RenderQueue.test.tsx +1 -1
  34. package/src/components/sidebar/CompositionsTab.tsx +4 -8
  35. package/src/components/ui/Button.test.tsx +1 -1
  36. package/src/components/ui/Menu.test.tsx +1 -1
  37. package/src/components/ui/valueControls.test.tsx +1 -1
  38. package/src/hooks/appHotkeysDispatch.ts +1 -1
  39. package/src/hooks/gsapParsedTween.test.ts +19 -0
  40. package/src/hooks/gsapParsedTween.ts +3 -1
  41. package/src/hooks/linkShortcuts.ts +1 -1
  42. package/src/hooks/useAutomationSelectionKeyboard.ts +1 -1
  43. package/src/hooks/useConsoleErrorCapture.test.tsx +69 -1
  44. package/src/hooks/useConsoleErrorCapture.ts +47 -11
  45. package/src/hooks/useKeyframeKeyboard.ts +1 -1
  46. package/src/index.ts +3 -2
  47. package/src/keys.test.ts +5 -7
  48. package/src/keys.ts +1 -6
  49. package/src/player/components/Player.test.ts +49 -0
  50. package/src/player/components/Player.tsx +13 -4
  51. package/src/player/components/ShortcutsPanel.tsx +1 -1
  52. package/src/player/components/studioCombo.test.ts +1 -1
  53. package/src/player/components/studioShortcuts.ts +1 -1
  54. package/src/player/lib/frameToken.ts +20 -0
  55. package/src/player/lib/playbackShortcuts.ts +3 -28
  56. package/src/player/lib/previewChannel.ts +10 -0
  57. package/src/utils/studioHelpers.ts +1 -1
  58. package/src/utils/studioPendingEdits.ts +1 -1
  59. package/src/utils/timelineDiscovery.ts +1 -1
  60. package/src/utils/typingTarget.test.ts +1 -1
  61. package/src/webmcp/polyfill.transport.test.ts +13 -3
  62. package/src/webmcp/types.ts +3 -3
  63. package/src/webmcp/useStudioAgentTools.polyfill.test.tsx +89 -121
  64. package/src/webmcp/useStudioAgentTools.test.tsx +2 -2
  65. package/src/webmcp/useStudioAgentTools.ts +2 -3
  66. package/dist/assets/index-BX3KHhGX.js +0 -71
  67. package/dist/chunk-5ZHKYODQ.js +0 -149
  68. package/dist/keys-B163OjLN.d.ts +0 -87
  69. package/src/player/components/studioCombo.ts +0 -108
  70. package/src/utils/typingTarget.ts +0 -56
package/src/index.ts CHANGED
@@ -14,8 +14,8 @@ export {
14
14
  studioKeyClash,
15
15
  useStudioKeys,
16
16
  } from "./player/components/studioShortcuts";
17
- export { formatCombo, matchCombo, parseCombo } from "./player/components/studioCombo";
18
- export type { Combo } from "./player/components/studioCombo";
17
+ export { formatCombo, matchCombo, parseCombo } from "@hyperframes/core/key-combo";
18
+ export type { Combo } from "@hyperframes/core/key-combo";
19
19
  export type { StudioKeyAction, StudioKeys } from "./player/components/studioShortcuts";
20
20
  export { ShortcutsButton } from "./player/components/ShortcutsPanel";
21
21
  export { dispatchLinkShortcut } from "./hooks/linkShortcuts";
@@ -45,6 +45,7 @@ export {
45
45
  formatTime,
46
46
  } from "./player";
47
47
  export { usePreviewIframeStore } from "./player/store/previewIframeStore";
48
+ export { previewEditingClient, previewFrameOpaque } from "./player/lib/previewChannel";
48
49
  export { PREVIEW_QUALITY_LEVELS, type PreviewQuality } from "@hyperframes/core/preview-quality";
49
50
  export type {
50
51
  PlaybackHealth,
package/src/keys.test.ts CHANGED
@@ -31,15 +31,13 @@ function importGraph(entry: string): { files: string[]; packages: string[] } {
31
31
  }
32
32
 
33
33
  describe("@hyperframes/studio/keys", () => {
34
- it("imports only the key helpers: no package and no editor module", () => {
34
+ it("imports only the key helpers: core's import-free matcher and no editor module", () => {
35
35
  expect(importGraph("keys.ts")).toEqual({
36
- files: [
37
- "keys.ts",
38
- "player/components/studioCombo.ts",
39
- "player/components/studioKeyDefaults.ts",
40
- ],
41
- packages: [],
36
+ files: ["keys.ts", "player/components/studioKeyDefaults.ts"],
37
+ packages: ["@hyperframes/core/key-combo"],
42
38
  });
39
+ const matcher = readFileSync(join(src, "../../core/src/keyCombo.ts"), "utf8");
40
+ expect(ts.preProcessFile(matcher).importedFiles).toEqual([]);
43
41
  });
44
42
 
45
43
  it("is a published subpath sharing the defaults the root entry exports", async () => {
package/src/keys.ts CHANGED
@@ -1,9 +1,4 @@
1
- export {
2
- matchCombo,
3
- parseCombo,
4
- type Combo,
5
- type ComboEvent,
6
- } from "./player/components/studioCombo";
1
+ export { matchCombo, parseCombo, type Combo, type ComboEvent } from "@hyperframes/core/key-combo";
7
2
  export {
8
3
  DEFAULT_STUDIO_KEYS,
9
4
  type StudioKeyAction,
@@ -198,6 +198,55 @@ describe("preview errors", () => {
198
198
  });
199
199
  });
200
200
 
201
+ describe("a host's sandbox origin", () => {
202
+ const token = "T".repeat(43);
203
+ const stubFrameToken = (body: { token?: string } | null) =>
204
+ vi.spyOn(globalThis, "fetch").mockImplementation(async (input) => {
205
+ expect(String(input)).toBe("/api/projects/demo/frame-token");
206
+ return { ok: body !== null, json: async () => body } as Response;
207
+ });
208
+ const settle = () =>
209
+ act(async () => {
210
+ for (let i = 0; i < 10; i += 1) await Promise.resolve();
211
+ });
212
+ afterEach(() => vi.restoreAllMocks());
213
+
214
+ it.each([
215
+ ["/api/projects/demo/preview", `/api/projects/demo/preview/~${token}`],
216
+ [
217
+ "/api/projects/demo/preview/comp/scene.html",
218
+ `/api/projects/demo/preview/~${token}/comp/scene.html`,
219
+ ],
220
+ ["/api/projects/demo/preview?_t=1", `/api/projects/demo/preview/~${token}?_t=1`],
221
+ ])(
222
+ "seals an opaque preview of %s and loads it under its project's frame token",
223
+ async (directUrl, sealed) => {
224
+ stubFrameToken({ token });
225
+ const { player } = await mountPlayer({ directUrl, sandboxOrigin: "opaque" });
226
+ await settle();
227
+ expect(player.hasAttribute("sandbox-origin")).toBe(true);
228
+ expect(player.getAttribute("src")).toBe(sealed);
229
+ },
230
+ );
231
+
232
+ it("still seals the preview when the project has no frame token to give", async () => {
233
+ stubFrameToken(null);
234
+ const { player } = await mountPlayer({ sandboxOrigin: "opaque" });
235
+ await settle();
236
+ expect(player.hasAttribute("sandbox-origin")).toBe(true);
237
+ expect(player.getAttribute("src")).toBe("/api/projects/demo/preview");
238
+ });
239
+
240
+ it("leaves a same-origin preview on its plain path without asking for a token", async () => {
241
+ const fetch = vi.spyOn(globalThis, "fetch");
242
+ const { player } = await mountPlayer();
243
+ await settle();
244
+ expect(player.hasAttribute("sandbox-origin")).toBe(false);
245
+ expect(player.getAttribute("src")).toBe("/api/projects/demo/preview");
246
+ expect(fetch).not.toHaveBeenCalled();
247
+ });
248
+ });
249
+
201
250
  describe("callbacks after the player is already mounted", () => {
202
251
  it("runs the latest onLoad on a later load, not the one captured at mount", async () => {
203
252
  const first = vi.fn();
@@ -9,6 +9,7 @@ import { usePlayerStore } from "../store/playerStore";
9
9
  import { announcePreviewDocumentLoaded } from "../sceneSwap";
10
10
  import type { IframeWindow } from "../lib/playbackTypes";
11
11
  import { watchPreviewFrame } from "../lib/bridgePlayback";
12
+ import { previewUnderFrameToken } from "../lib/frameToken";
12
13
  // Importing "@hyperframes/player" registers a class extending HTMLElement at
13
14
  // module load, which throws under SSR, hence the dynamic import behind a
14
15
  // `typeof window` guard. Kicking it here rather than in the mount effect puts
@@ -43,6 +44,7 @@ interface PlayerProps {
43
44
  suppressLoadingOverlay?: boolean;
44
45
  loadingUi?: "studio" | "host";
45
46
  noLoadTimeout?: boolean;
47
+ sandboxOrigin?: "opaque";
46
48
  }
47
49
 
48
50
  interface HyperframesPlayerElement extends HTMLElement {
@@ -139,6 +141,13 @@ export function hasUnloadedAssets(iframe: HTMLIFrameElement, lastResult: boolean
139
141
  }
140
142
  }
141
143
 
144
+ async function playerSrc(previewSource: string, opaque: boolean): Promise<string> {
145
+ const url = new URL(previewSource, window.location.origin);
146
+ applyPreviewVariablesToUrl(url);
147
+ const src = url.pathname + url.search;
148
+ return opaque ? previewUnderFrameToken(src) : src;
149
+ }
150
+
142
151
  const runtimeBootedIn = (iframe: HTMLIFrameElement) =>
143
152
  (iframe.contentWindow as IframeWindow | null)?.__playerReady === true;
144
153
 
@@ -219,14 +228,13 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
219
228
  let canceled = false;
220
229
  let cleanup: (() => void) | undefined;
221
230
 
222
- void loadPlayerModule().then(() => {
231
+ const opaque = host.sandboxOrigin === "opaque";
232
+
233
+ void Promise.all([loadPlayerModule(), playerSrc(previewSource, opaque)]).then(([, src]) => {
223
234
  if (canceled) return;
224
235
 
225
236
  // Create the web component imperatively to avoid JSX custom-element typing.
226
237
  const player = document.createElement("hyperframes-player") as HyperframesPlayerElement;
227
- const srcUrl = new URL(previewSource, window.location.origin);
228
- applyPreviewVariablesToUrl(srcUrl);
229
- const src = srcUrl.pathname + srcUrl.search;
230
238
  const retryPreview = () => {
231
239
  retryCountRef.current += 1;
232
240
  const retryUrl = new URL(src, window.location.origin);
@@ -344,6 +352,7 @@ export const Player = forwardRef<HTMLIFrameElement, PlayerProps>(
344
352
  player.setAttribute("shader-loading", "player");
345
353
  player.setAttribute("low-power-idle", "");
346
354
  player.toggleAttribute("no-load-timeout", noLoadTimeout === true);
355
+ player.toggleAttribute("sandbox-origin", opaque);
347
356
  player.setAttribute("width", String(portrait ? 1080 : 1920));
348
357
  player.setAttribute("height", String(portrait ? 1920 : 1080));
349
358
  player.style.width = "100%";
@@ -5,7 +5,7 @@ import { flatActive, flatIdle } from "../../components/timelineToolbarStyles";
5
5
  import { usePlayerStore } from "../store/playerStore";
6
6
  import { useCompositionFps } from "../../utils/linkedClipPreferences";
7
7
  import { matchesKey, useShortcutSections, type ShortcutSection } from "./studioShortcuts";
8
- import { isTypingTarget } from "../../utils/typingTarget";
8
+ import { isTypingTarget } from "@hyperframes/core/typing-target";
9
9
 
10
10
  interface ShortcutsPanelProps {
11
11
  disabled: boolean;
@@ -1,6 +1,6 @@
1
1
  // @vitest-environment happy-dom
2
2
  import { afterEach, describe, expect, it, vi } from "vitest";
3
- import { formatCombo, matchCombo, parseCombo } from "./studioCombo";
3
+ import { formatCombo, matchCombo, parseCombo } from "@hyperframes/core/key-combo";
4
4
  import {
5
5
  getStudioKey,
6
6
  getStudioKeys,
@@ -6,7 +6,7 @@ import {
6
6
  formatCombo,
7
7
  matchCombo,
8
8
  parseCombo,
9
- } from "./studioCombo";
9
+ } from "@hyperframes/core/key-combo";
10
10
 
11
11
  import { DEFAULT_STUDIO_KEYS, type StudioKeyAction, type StudioKeys } from "./studioKeyDefaults";
12
12
 
@@ -0,0 +1,20 @@
1
+ import { studioApiFetch } from "../../utils/studioApiFetch";
2
+
3
+ const PREVIEW_PATH = /^(\/api\/projects\/[^/?#]+)\/preview(?=[/?#]|$)/;
4
+
5
+ export function fetchFrameToken(frameTokenPath: string): Promise<string | null> {
6
+ return studioApiFetch(frameTokenPath)
7
+ .then((res) => (res.ok ? res.json() : null))
8
+ .then((body: { token?: unknown } | null) =>
9
+ typeof body?.token === "string" ? body.token : null,
10
+ )
11
+ .catch(() => null);
12
+ }
13
+
14
+ /** The preview path under its project's frame token, where an opaque frame's own loads are let through. */
15
+ export async function previewUnderFrameToken(src: string): Promise<string> {
16
+ const project = PREVIEW_PATH.exec(src)?.[1];
17
+ if (!project) return src;
18
+ const token = await fetchFrameToken(`${project}/frame-token`);
19
+ return token ? src.replace(PREVIEW_PATH, `$1/preview/~${token}`) : src;
20
+ }
@@ -6,8 +6,8 @@
6
6
  * is active and the user is navigating caption segments).
7
7
  */
8
8
 
9
- import { ownsPlainKeys } from "../../utils/typingTarget";
10
- import type { ComboEvent } from "../components/studioCombo";
9
+ import { shouldIgnorePlaybackShortcutTarget } from "@hyperframes/core/typing-target";
10
+ import type { ComboEvent } from "@hyperframes/core/key-combo";
11
11
  import { matchesKey } from "../components/studioShortcuts";
12
12
  import { usePlayerStore } from "../store/playerStore";
13
13
 
@@ -18,32 +18,7 @@ const FRAME_STEP_ACTIONS = [
18
18
  "frameForwardTen",
19
19
  ] as const;
20
20
 
21
- const PLAYBACK_SHORTCUT_IGNORED_SELECTOR = [
22
- "button",
23
- "a[href]",
24
- "[role='button']",
25
- "[role='checkbox']",
26
- "[role='menuitem']",
27
- // Base UI's menu radio item is a `<div>`, so `button` above no longer catches it.
28
- "[role='menuitemradio']",
29
- "[role='radio']",
30
- "[role='spinbutton']",
31
- ].join(",");
32
-
33
- export function shouldIgnorePlaybackShortcutTarget(target: EventTarget | null): boolean {
34
- // Anything the user is typing into owns its keys outright, editable elements
35
- // included: a letter claimed here never reaches the text. So does a native player.
36
- if (ownsPlainKeys(target)) return true;
37
- if (!target || typeof target !== "object") return false;
38
- const candidate = target as { closest?: unknown };
39
- if (typeof candidate.closest !== "function") return false;
40
- return (
41
- (candidate.closest as (selector: string) => Element | null).call(
42
- target,
43
- PLAYBACK_SHORTCUT_IGNORED_SELECTOR,
44
- ) !== null
45
- );
46
- }
21
+ export { shouldIgnorePlaybackShortcutTarget };
47
22
 
48
23
  const MODAL_DIALOG_SELECTOR = "[role=dialog][aria-modal=true]";
49
24
 
@@ -97,6 +97,16 @@ export async function previewClipManifest(iframe: HTMLIFrameElement): Promise<Cl
97
97
  return manifest as ClipManifest;
98
98
  }
99
99
 
100
+ export async function previewConsoleErrors(
101
+ iframe: HTMLIFrameElement,
102
+ from: number,
103
+ ): Promise<{ errors: string[]; truncated: boolean } | null> {
104
+ const reply = await previewRequest(iframe, "console-errors", { from });
105
+ const errors: unknown = reply?.errors;
106
+ if (!Array.isArray(errors) || typeof reply?.truncated !== "boolean") return null;
107
+ return { errors: errors.map(String), truncated: reply.truncated };
108
+ }
109
+
100
110
  /** The runtime's computed `properties` for the element `target` names; null when it has none. */
101
111
  export async function previewComputedStyles(
102
112
  iframe: HTMLIFrameElement,
@@ -1,5 +1,5 @@
1
1
  import { buildProjectApiPath } from "./projectRouting";
2
- import { isTypingTarget } from "./typingTarget";
2
+ import { isTypingTarget } from "@hyperframes/core/typing-target";
3
3
  import type { TimelineElement } from "../player/store/playerStore";
4
4
  import type { DomEditSelection } from "../components/editor/domEditing";
5
5
  import type { TimelineAssetKind } from "./timelineAssetDrop";
@@ -1,5 +1,5 @@
1
1
  import { StudioFileConflictError, type StudioSaveDrainResult } from "./studioSaveDiagnostics";
2
- import { isTypingTarget } from "./typingTarget";
2
+ import { isTypingTarget } from "@hyperframes/core/typing-target";
3
3
 
4
4
  const STUDIO_FLUSH_PENDING_EDITS_EVENT = "hf-studio-flush-pending-edits";
5
5
 
@@ -1,3 +1,3 @@
1
1
  // Compatibility name for downstream imports. The typing decision itself has
2
2
  // one owner; new Studio callers import `isTypingTarget` directly.
3
- export { isTypingTarget as isEditableTarget } from "./typingTarget";
3
+ export { isTypingTarget as isEditableTarget } from "@hyperframes/core/typing-target";
@@ -1,7 +1,7 @@
1
1
  // @vitest-environment happy-dom
2
2
 
3
3
  import { afterEach, describe, expect, it } from "vitest";
4
- import { isTypingTarget, ownsPlainKeys } from "./typingTarget";
4
+ import { isTypingTarget, ownsPlainKeys } from "@hyperframes/core/typing-target";
5
5
  import { isEditableTarget } from "./timelineDiscovery";
6
6
 
7
7
  afterEach(() => {
@@ -1,24 +1,34 @@
1
1
  // @vitest-environment jsdom
2
2
  /**
3
3
  * Loads the REAL `@mcp-b/global` through Studio's loader, inside a page that is
4
- * framed by another site, and checks that neither of the package's message
5
- * transports answers anyone but Studio's own page.
4
+ * framed by another site, and checks that neither of the package's MCP
5
+ * message transports answers anyone but Studio's own page. The W3C polyfill's
6
+ * frame protocol is not exercised here: the fake framer is not `window.top`.
6
7
  */
7
8
  import { afterAll, expect, it, vi } from "vitest";
8
9
 
9
10
  vi.mock("../telemetry/client", () => ({ trackEvent: vi.fn() }));
10
11
 
11
- const framer = { postMessage: vi.fn() };
12
+ // A top-level window: the polyfill walks `parent` until it reaches itself.
13
+ const framer = {
14
+ postMessage: vi.fn(),
15
+ get parent(): object {
16
+ return framer;
17
+ },
18
+ };
12
19
  const ownParent = Object.getOwnPropertyDescriptor(window, "parent");
13
20
  afterAll(() => {
14
21
  if (ownParent) Object.defineProperty(window, "parent", ownParent);
15
22
  else Reflect.deleteProperty(window, "parent");
23
+ Reflect.deleteProperty(window, "isSecureContext");
16
24
  });
17
25
 
18
26
  const settle = () => new Promise((resolve) => setTimeout(resolve, 20));
19
27
 
20
28
  it("answers no MCP client but Studio's own page when another site frames Studio", async () => {
21
29
  Object.defineProperty(window, "parent", { value: framer, configurable: true });
30
+ // jsdom leaves this undefined and the polyfill installs only in a secure context.
31
+ Object.defineProperty(window, "isSecureContext", { value: true, configurable: true });
22
32
  const ownMessages = vi.spyOn(window, "postMessage");
23
33
  const { loadModelContextPolyfill } = await import("./polyfill");
24
34
 
@@ -14,8 +14,8 @@
14
14
  * Only the surface Studio registers against is declared. `getTools` and
15
15
  * `executeTool` are the consumer side; Studio registers, it does not call.
16
16
  *
17
- * These stay hand-written rather than imported from `@mcp-b/webmcp-types`,
18
- * which the polyfill pulls in. That package's `registerTool` is overloaded to
17
+ * These stay hand-written rather than imported from `webmcp-types`, which
18
+ * the polyfill pulls in. That package's `registerTool` is overloaded to
19
19
  * infer argument types from a literal `inputSchema`, which is useful when you
20
20
  * register one tool inline and actively hostile when you register a uniform
21
21
  * list of them, as `registerStudioTools` does. Narrower is the safe operation
@@ -59,7 +59,7 @@ export interface ModelContextTool {
59
59
  * instead. See `toolResult.ts`.
60
60
  *
61
61
  * `options` is optional here although the spec always passes it: the
62
- * `@mcp-b/global` polyfill (through 5.1.0) calls `execute(input)` with no
62
+ * `@mcp-b/global` polyfill before 6.0 called `execute(input)` with no
63
63
  * second argument, so a Studio handler must not depend on receiving one.
64
64
  */
65
65
  execute: (input: object, options?: ToolExecuteCallbackOptions) => Promise<unknown>;
@@ -3,24 +3,24 @@
3
3
  * Drives the write tools through the REAL `@mcp-b/global` package rather than
4
4
  * a fake `document.modelContext`.
5
5
  *
6
- * The package invokes a registered `execute` with the input alone, on both of
7
- * its paths: the in-page `BrowserMcpServer` wrapper that agents reach through
8
- * the registry and `executeTool`, and the descriptor it mirrors into a native
9
- * `document.modelContext` when the browser ships one. A Studio handler that
10
- * destructured `{ signal }` from the missing second argument threw before it
11
- * ran, so every write tool failed with "Cannot destructure property 'signal'
12
- * of 'undefined'" while the read tools kept working. This file proves the fix
13
- * against the code that ships in the polyfill chunk. The spec-shaped call and
14
- * the abort path live in `useStudioAgentTools.test.tsx`; the polyfill cannot
15
- * exercise them because it drops the caller's signal before the handler.
16
- *
17
- * A native-looking `modelContext` is installed BEFORE the import so the bridge
18
- * wraps it and mirrors every registration into it, which is how both paths get
19
- * covered from one setup. Its own file because the import is a one-shot side
20
- * effect that the sibling suite's fake would otherwise have to fight.
6
+ * The package installs the W3C polyfill and wraps it in an in-page MCP server
7
+ * that an out-of-browser agent reaches over the tab transport. This file
8
+ * proves a write tool saves when an MCP `tools/call` drives it through that
9
+ * chain, against the code that ships in the polyfill chunk. Its own file
10
+ * because the import is a one-shot side effect that the sibling suite's fake
11
+ * would otherwise have to fight.
21
12
  */
22
13
  import { act } from "react";
23
- import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
14
+ import {
15
+ afterAll,
16
+ afterEach,
17
+ beforeAll,
18
+ describe,
19
+ expect,
20
+ it,
21
+ vi,
22
+ type MockInstance,
23
+ } from "vitest";
24
24
  import { mountReactHarness } from "../hooks/domSelectionTestHarness";
25
25
  import { mintElementHandle } from "./handles";
26
26
  import { useStudioAgentTools, type StudioAgentToolsDeps } from "./useStudioAgentTools";
@@ -30,71 +30,37 @@ vi.mock("../telemetry/client", () => ({ trackEvent: vi.fn() }));
30
30
 
31
31
  Reflect.set(globalThis, "IS_REACT_ACT_ENVIRONMENT", true);
32
32
 
33
- /** What a browser's own `registerTool` receives from the bridge. */
34
- interface MirroredTool {
35
- name: string;
36
- description: string;
37
- execute: (input: object, options?: { signal?: AbortSignal }) => Promise<unknown>;
38
- }
39
-
40
- /** What `getTools()` returns and `executeTool()` accepts. */
41
- interface RegisteredTool {
42
- name: string;
43
- description: string;
44
- window: Window;
45
- origin: string;
46
- }
47
-
48
- /** The bridge's registry entry, as the reporter of the bug drove it. */
49
- interface BridgeToolEntry {
50
- item: { name: string };
51
- execute: (input: object, signal?: AbortSignal) => Promise<unknown>;
52
- }
53
-
54
33
  interface BridgeModelContext {
55
- tools: Map<string, BridgeToolEntry>;
56
- getTools(): Promise<RegisteredTool[]>;
57
- executeTool(
58
- tool: RegisteredTool,
59
- inputArguments: string,
60
- options?: { signal?: AbortSignal },
61
- ): Promise<string | null>;
34
+ listTools(): { name: string }[];
35
+ getTools(): Promise<{ name: string }[]>;
62
36
  }
63
37
 
64
- const mirrored: MirroredTool[] = [];
65
- /** Enough of a browser's `modelContext` for the bridge to wrap and mirror into. */
66
- const fakeNative = {
67
- registerTool: async (tool: MirroredTool): Promise<void> => {
68
- mirrored.push(tool);
69
- },
70
- // The bridge delegates `getTools()` to the native context when there is one,
71
- // and its own `executeTool` then insists on a full `RegisteredTool`.
72
- getTools: async (): Promise<RegisteredTool[]> =>
73
- mirrored.map((tool) => ({
74
- name: tool.name,
75
- description: tool.description,
76
- window,
77
- origin: window.location.origin,
78
- })),
79
- };
38
+ interface JsonRpcResponse {
39
+ id: number;
40
+ result?: { structuredContent?: unknown; content?: { type: string; text?: string }[] };
41
+ error?: unknown;
42
+ }
80
43
 
81
44
  let cleanup: (() => void) | null = null;
82
45
 
83
46
  beforeAll(async () => {
84
- // Configurable, so the bridge is allowed to replace it with itself.
85
- Object.defineProperty(document, "modelContext", {
86
- value: fakeNative,
87
- configurable: true,
88
- writable: true,
89
- });
47
+ // jsdom leaves this undefined and the polyfill installs only in a secure context.
48
+ Object.defineProperty(window, "isSecureContext", { value: true, configurable: true });
90
49
  await import("@mcp-b/global");
91
- expect(Reflect.get(document, "modelContext")).not.toBe(fakeNative);
50
+ expect(typeof bridge().listTools).toBe("function");
51
+ });
52
+
53
+ afterAll(async () => {
54
+ // Close the server so its deferred tools-changed event doesn't fire after jsdom is torn down.
55
+ const { cleanupWebModelContext } = await import("@mcp-b/global");
56
+ cleanupWebModelContext();
57
+ await new Promise((resolve) => setTimeout(resolve, 0));
58
+ Reflect.deleteProperty(window, "isSecureContext");
92
59
  });
93
60
 
94
61
  afterEach(async () => {
95
62
  cleanup?.();
96
63
  cleanup = null;
97
- mirrored.length = 0;
98
64
  // The polyfill watches the document with a MutationObserver that reads jsdom
99
65
  // globals. Empty the document and let the observer settle now, while those
100
66
  // globals still exist, instead of when vitest closes the window.
@@ -112,28 +78,14 @@ function bridge(): BridgeModelContext {
112
78
  /** Registration is async, so wait for the whole tool set to land. */
113
79
  async function waitForTools(count: number): Promise<void> {
114
80
  const deadline = Date.now() + 5_000;
115
- while (bridge().tools.size !== count || mirrored.length !== count) {
81
+ while (bridge().listTools().length !== count || (await bridge().getTools()).length !== count) {
116
82
  if (Date.now() > deadline) {
117
- throw new Error(
118
- `expected ${count} tools, saw ${bridge().tools.size} in the bridge and ${mirrored.length} mirrored`,
119
- );
83
+ throw new Error(`expected ${count} tools, saw ${bridge().listTools().length} in the bridge`);
120
84
  }
121
85
  await new Promise((resolve) => setTimeout(resolve, 10));
122
86
  }
123
87
  }
124
88
 
125
- function bridgeEntry(name: string): BridgeToolEntry {
126
- const entry = [...bridge().tools.values()].find((candidate) => candidate.item.name === name);
127
- if (!entry) throw new Error(`expected ${name} in the bridge registry`);
128
- return entry;
129
- }
130
-
131
- function mirroredTool(name: string): MirroredTool {
132
- const tool = mirrored.find((candidate) => candidate.name === name);
133
- if (!tool) throw new Error(`expected ${name} to be mirrored into the native context`);
134
- return tool;
135
- }
136
-
137
89
  /** Mount Studio's tools against a preview whose `#agent` element can be edited. */
138
90
  async function mountEditableAgent() {
139
91
  const doc = previewDoc('<h1 id="agent">Agent</h1>');
@@ -170,49 +122,65 @@ async function mountEditableAgent() {
170
122
  return { agent, handle, setText };
171
123
  }
172
124
 
173
- describe("useStudioAgentTools through @mcp-b/global", () => {
174
- it("saves through studio_set_text when the in-page server calls execute with the input alone", async () => {
175
- const { agent, handle, setText } = await mountEditableAgent();
176
-
177
- // Registry entry, as `[...document.modelContext.tools.values()]` exposes it.
178
- const viaEntry = await bridgeEntry("studio_set_text").execute(
179
- { handle, text: "Through the registry" },
180
- new AbortController().signal,
181
- );
182
- expect(viaEntry).toMatchObject({ ok: true, stage: "saved", changed: true });
183
-
184
- // Chromium's `executeTool` extension, which the bridge also implements.
185
- const descriptor = (await bridge().getTools()).find((tool) => tool.name === "studio_set_text");
186
- if (!descriptor) throw new Error("expected studio_set_text in getTools()");
187
- const viaExecuteTool = await bridge().executeTool(
188
- descriptor,
189
- JSON.stringify({ handle, text: "Through executeTool" }),
190
- { signal: new AbortController().signal },
191
- );
192
- expect(JSON.parse(viaExecuteTool ?? "null")).toMatchObject({ ok: true, stage: "saved" });
125
+ /** Post one JSON-RPC message to the in-page server the way the tab client does. */
126
+ function postToServer(payload: object): void {
127
+ window.dispatchEvent(
128
+ new MessageEvent("message", {
129
+ data: { channel: "mcp-default", type: "mcp", direction: "client-to-server", payload },
130
+ origin: window.location.origin,
131
+ source: window,
132
+ }),
133
+ );
134
+ }
193
135
 
194
- expect(setText).toHaveBeenCalledTimes(2);
195
- expect(setText).toHaveBeenCalledWith(
196
- expect.objectContaining({ element: agent }),
197
- "Through the registry",
198
- "self",
199
- );
200
- });
136
+ /** The server answers through `window.postMessage`; wait for the reply to `id`. */
137
+ async function replyTo(sent: MockInstance, id: number): Promise<JsonRpcResponse> {
138
+ const deadline = Date.now() + 5_000;
139
+ for (;;) {
140
+ for (const [message] of sent.mock.calls) {
141
+ const payload = (message as { payload?: unknown } | undefined)?.payload;
142
+ if (typeof payload === "object" && payload !== null && Reflect.get(payload, "id") === id) {
143
+ return payload as JsonRpcResponse;
144
+ }
145
+ }
146
+ if (Date.now() > deadline) throw new Error(`no MCP reply to request ${id}`);
147
+ await new Promise((resolve) => setTimeout(resolve, 10));
148
+ }
149
+ }
201
150
 
202
- it("saves through the descriptor the bridge mirrors into a native modelContext", async () => {
151
+ describe("useStudioAgentTools through @mcp-b/global", () => {
152
+ it("saves through studio_set_text when an MCP client calls it", async () => {
203
153
  const { agent, handle, setText } = await mountEditableAgent();
204
-
205
- // The browser would call this with `(input, { signal })`; the mirror drops
206
- // the options before Studio's handler sees them.
207
- const result = await mirroredTool("studio_set_text").execute(
208
- { handle, text: "Through the native mirror" },
209
- { signal: new AbortController().signal },
210
- );
211
-
154
+ const sent = vi.spyOn(window, "postMessage");
155
+
156
+ postToServer({
157
+ jsonrpc: "2.0",
158
+ id: 1,
159
+ method: "initialize",
160
+ params: {
161
+ protocolVersion: "2025-06-18",
162
+ capabilities: {},
163
+ clientInfo: { name: "studio-test", version: "1.0.0" },
164
+ },
165
+ });
166
+ expect((await replyTo(sent, 1)).error).toBeUndefined();
167
+ postToServer({ jsonrpc: "2.0", method: "notifications/initialized" });
168
+
169
+ postToServer({
170
+ jsonrpc: "2.0",
171
+ id: 2,
172
+ method: "tools/call",
173
+ params: { name: "studio_set_text", arguments: { handle, text: "Through MCP" } },
174
+ });
175
+ const reply = await replyTo(sent, 2);
176
+ sent.mockRestore();
177
+
178
+ const text = reply.result?.content?.[0]?.text;
179
+ const result = reply.result?.structuredContent ?? JSON.parse(text ?? "null");
212
180
  expect(result).toMatchObject({ ok: true, stage: "saved", changed: true });
213
181
  expect(setText).toHaveBeenCalledWith(
214
182
  expect.objectContaining({ element: agent }),
215
- "Through the native mirror",
183
+ "Through MCP",
216
184
  "self",
217
185
  );
218
186
  });