Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

Page card: the page's first word on its heading comes with its size

luishron1237f81c70fc145
diff --git a/shared/app-frame/heading.test.ts b/shared/app-frame/heading.test.ts
index 27acc0a22f..f88fcc59a8 100644
--- a/shared/app-frame/heading.test.ts
+++ b/shared/app-frame/heading.test.ts
@@ -59,10 +59,15 @@ describe("the page helper's heading report", () => {
function boot(body: string) {
const page = open(body);
const reports: (boolean | null)[] = [];
+ const sizes: unknown[] = [];
const listeners = new Set<(event: { source: unknown; data: unknown }) => void>();
const parent = {
postMessage(data: { id?: string; method?: string; params?: { heading?: boolean | null } }) {
if (data.method === APP_HEADING_METHOD) reports.push(data.params!.heading!);
+ if (data.method === "ui/notifications/size-changed") {
+ sizes.push(data.params);
+ if (data.params!.heading !== undefined) reports.push(data.params!.heading);
+ }
if (data.method === "ui/initialize") setTimeout(() => { for (const listener of listeners) listener({ source: parent, data: { jsonrpc: "2.0", id: data.id, result: {} } }); }, 0);
},
};
@@ -74,7 +79,7 @@ describe("the page helper's heading report", () => {
Object.defineProperty(page.document, "readyState", { configurable: true, get: () => "complete" });
new Function("window", "document", "MutationObserver", "ResizeObserver", "requestAnimationFrame", APP_HELPER_SOURCE)(
frame, page.document, page.MutationObserver, undefined, (callback: () => void) => setTimeout(callback, 0));
- return { page, frame, reports };
+ return { page, frame, reports, sizes };
}
it("says once whether the page opens with a heading", async () => {
@@ -84,6 +89,13 @@ describe("the page helper's heading report", () => {
await expect.poll(() => plain.reports).toEqual([false]);
});
+ it("sends its first answer on the size report, so the host hears both at once", async () => {
+ const titled = boot("<h1>Pricing</h1><p>body</p>");
+ await expect.poll(() => titled.sizes).toEqual([{ height: expect.any(Number), heading: true }]);
+ const drawn = boot("<div id=\"root\"></div>");
+ await expect.poll(() => drawn.sizes).toEqual([{ height: expect.any(Number) }]);
+ });
+
it("waits for a page that draws itself from a script, and reports the heading the script inserts", async () => {
const { page, reports } = boot("<div id=\"root\"></div>");
await new Promise((resolve) => setTimeout(resolve, 50));
diff --git a/shared/app-frame/helper.ts b/shared/app-frame/helper.ts
index ed0159bbb7..578f1e29c4 100644
--- a/shared/app-frame/helper.ts
+++ b/shared/app-frame/helper.ts
@@ -180,7 +180,7 @@ const helperSource = (room: boolean) => `(function () {
filling = start > 0 && flow < SHORT_FLOW && drawsToWindow();
return filling ? start : flow;
};
- var watchSize = function () {
+ var watchSize = function (heading) {
var last = -1;
var queued = false;
var later = typeof requestAnimationFrame === "function" ? requestAnimationFrame : function (fn) { return setTimeout(fn, 16); };
@@ -190,7 +190,10 @@ const helperSource = (room: boolean) => `(function () {
later(function () {
queued = false;
var height = heightToReport();
- if (height !== last) { last = height; notify("ui/notifications/size-changed", { height: height }); }
+ if (height === last) return;
+ last = height;
+ var opens = heading();
+ notify("ui/notifications/size-changed", opens === undefined ? { height: height } : { height: height, heading: opens });
});
};
if (typeof ResizeObserver === "function") {
@@ -237,6 +240,13 @@ const helperSource = (room: boolean) => `(function () {
document.addEventListener("animationend", look, true);
document.addEventListener("transitionend", look, true);
look();
+ return function () {
+ if (last !== undefined || waiting !== null) return undefined;
+ var found = opensWithHeading(window, document);
+ if (found === null) return undefined;
+ last = found === true;
+ return last;
+ };
};
// What the person selects, and where it ends, so the host can offer to cite it as chat does. Not in a room.
var watchSelection = function () {
@@ -270,9 +280,11 @@ const helperSource = (room: boolean) => `(function () {
}).then(function (result) {
apply(result && result.hostContext);
notify("ui/notifications/initialized");
- // Before the size, so the host hears both in one go and lays the card out once.
- watchHeading();
- watchSize();
+ // The size report looks first and takes the page's first answer on its heading along, so the host lays the card
+ // out once: two messages could land either side of a frame.
+ var opening;
+ watchSize(function () { return opening ? opening() : undefined; });
+ opening = watchHeading();
watchSelection();
});
var checked = function (result) {
diff --git a/src/components/app/app-frame-host.test.ts b/src/components/app/app-frame-host.test.ts
index f008d2f9cf..d1ed157ac7 100644
--- a/src/components/app/app-frame-host.test.ts
+++ b/src/components/app/app-frame-host.test.ts
@@ -194,6 +194,20 @@ describe("a page's word on whether it opens with a heading", () => {
host.destroy();
});
+ it("is taken from the page's size report too, after the size", () => {
+ const calls: unknown[] = [];
+ const frame = { ...adapter(), resize: vi.fn((height: number) => { calls.push(height); }) };
+ const host = mountAppFrame({ container, doc, title: "Coffee counter", height: 200, hostContext: {}, hostVersion: "1", adapter: frame, onHeading: (heading) => { calls.push(heading); } });
+ const send = (data: object) => window.dispatchEvent(new MessageEvent("message", { data: { jsonrpc: "2.0", ...data }, source: host.iframe.contentWindow as MessageEventSource }));
+ send({ id: 1, method: "ui/initialize", params: {} });
+ send({ method: "ui/notifications/initialized", params: {} });
+ send({ method: "ui/notifications/size-changed", params: { height: 240, heading: false } });
+ send({ method: "ui/notifications/size-changed", params: { height: 260 } });
+ send({ method: "ui/notifications/size-changed", params: { height: 280, heading: null } });
+ expect(calls).toEqual([240, false, 260, 280]);
+ host.destroy();
+ });
+
it("is passed on once however often the page repeats it", () => {
const onHeading = vi.fn();
const host = mountAppFrame({ container, doc, title: "Coffee counter", height: 200, hostContext: {}, hostVersion: "1", adapter: adapter(), onHeading });
diff --git a/src/components/app/app-frame-host.ts b/src/components/app/app-frame-host.ts
index d057582cbd..98dbfc85bd 100644
--- a/src/components/app/app-frame-host.ts
+++ b/src/components/app/app-frame-host.ts
@@ -110,13 +110,17 @@ export function readPageSelection(data: unknown): AppSelection | null | undefine
return finite(left) && finite(bottom) ? { text, left, bottom } : undefined;
}
+const APP_SIZE_METHOD = "ui/notifications/size-changed";
+
/** The page helper's word on whether the page opens with a heading, null while it is still drawing itself; undefined
- * for any other message. */
+ * for any other message. Its first answer may come on the page's size report. */
export function readPageHeading(data: unknown): boolean | null | undefined {
if (!data || typeof data !== "object") return undefined;
const message = data as { jsonrpc?: unknown; method?: unknown; id?: unknown; params?: { heading?: unknown } };
- if (message.jsonrpc !== "2.0" || message.method !== APP_HEADING_METHOD || message.id !== undefined) return undefined;
+ if (message.jsonrpc !== "2.0" || message.id !== undefined) return undefined;
const heading = message.params?.heading;
+ if (message.method === APP_SIZE_METHOD) return typeof heading === "boolean" ? heading : undefined;
+ if (message.method !== APP_HEADING_METHOD) return undefined;
return typeof heading === "boolean" || heading === null ? heading : undefined;
}
@@ -217,6 +221,8 @@ export function mountAppFrame(options: AppFrameHostOptions): AppFrameHost {
const onMessage = (event: MessageEvent) => {
const heading = event.source === iframe.contentWindow ? readPageHeading(event.data) : undefined;
if (heading !== undefined) {
+ // The size first, in the same turn, so the card is laid out once.
+ if ((event.data as { method?: unknown }).method === APP_SIZE_METHOD) bridge.handleMessage({ source: event.source, data: event.data });
if (leaving || heading === said) return;
said = heading;
options.onHeading?.(heading);