Public: anyone can read this; the crew can change it.

Report a card's height to the host, so a card is not clipped #11811

opened this

Check · Review · Land · Live — It conflicts with its base. Next: builder, it conflicts with its base.

Conflicts with main

fix/card-size-report → main 3 commits

Review

Sign in to review this change.
3 files changed +43 −2
src/http/mcp.ts +3 −2
@@ -17,7 +17,8 @@
1717 * no page. `ro` tokens read; `rw` tokens act.
1818 */
1919
20 import { CARD_KINDS, CARD_MIME, CARD_OF_TOOL, CARD_SHELL, CARD_URI, cardFor } from "../web/studio/cards.ts";
20 import { CARD_KINDS, CARD_MIME, CARD_OF_TOOL, CARD_URI, cardFor } from "../web/studio/cards.ts";
21 import { CARD_SHELL_SIZED } from "../web/studio/card_size.ts";
2122 import { getRepo } from "../core/repos.ts";
2223 import { scopeAllows } from "../core/tokens.ts";
2324 import type { User } from "../core/users.ts";
@@ -182,7 +183,7 @@ function rpcOne(
182183 const kind = CARD_KINDS.find((k) => CARD_URI(k) === uri);
183184 if (kind === undefined) return refuse(-32002, `no resource ${uri}`);
184185 return reply({
185 contents: [{ uri, mimeType: CARD_MIME, text: CARD_SHELL(kind) }],
186 contents: [{ uri, mimeType: CARD_MIME, text: CARD_SHELL_SIZED(kind) }],
186187 });
187188 }
188189 case "tools/call": {
src/web/studio/card_size.ts added +21
@@ -0,0 +1,21 @@
1 /**
2 * The card page with its height reported to the host. A host sizes the
3 * frame a card is drawn in from `ui/notifications/size-changed` (SEP-1865,
4 * `{ width?, height? }`, sent by the view), so the page says how tall it is
5 * when it loads, after each redraw, and whenever the document resizes.
6 */
7
8 import { CARD_SHELL, type CardKind } from "./cards.ts";
9
10 /** Runs after the card's own draw script: a redraw is a message, and the report follows it. */
11 const REPORT_JS = `
12 (() => {
13 const report = () => parent.postMessage({ jsonrpc: "2.0", method: "ui/notifications/size-changed", params: { height: Math.ceil(document.documentElement.scrollHeight) } }, "*");
14 new ResizeObserver(report).observe(document.documentElement);
15 addEventListener("message", () => setTimeout(report, 0));
16 report();
17 })();
18 `;
19
20 /** The card's page for `kind`, with the size report added after its draw script. */
21 export const CARD_SHELL_SIZED = (kind: CardKind): string => `${CARD_SHELL(kind)}<script>${REPORT_JS}</script>`;
test/card-size-report.test.ts added +19
@@ -0,0 +1,19 @@
1 import { describe, expect, test } from "bun:test";
2 import { CARD_KINDS } from "../src/web/studio/cards.ts";
3 import { CARD_SHELL_SIZED } from "../src/web/studio/card_size.ts";
4
5 describe("a card reports its height to the host", () => {
6 test("every card kind's page sends ui/notifications/size-changed", () => {
7 for (const kind of CARD_KINDS) {
8 const page = CARD_SHELL_SIZED(kind);
9 expect(page).toContain('method: "ui/notifications/size-changed"');
10 expect(page).toContain("ResizeObserver");
11 }
12 });
13
14 test("the page keeps its draw script beside the report", () => {
15 const page = CARD_SHELL_SIZED("search");
16 expect(page).toContain("ui/notifications/tool-result");
17 expect(page.indexOf("ui/notifications/tool-result")).toBeLessThan(page.indexOf("ui/notifications/size-changed"));
18 });
19 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs