// Subscribe through the local renderer bridge or a headless host’s pairing offer. import type { Page } from '@stablyai/playwright-test' import { pairedRuntimeConnectionFromOffer } from '../../../src/shared/remote-runtime/pairing/paired-runtime-connection' import { decodePairingOffer } from '../../../src/shared/remote-runtime/pairing/pairing' import '../../../src/shared/remote-runtime/requests/remote-runtime-request-socket' import { subscribeRemoteRuntimeTransport } from '../../../src/shared/remote-runtime/requests/remote-runtime-subscription-transport' import type { RuntimeRpcResponse } from '../../../src/shared/runtime-rpc-envelope' import type { LiveLayoutFeed } from './workspace-layout-oracle-feed' declare global { // oxlint-disable-next-line typescript-eslint/consistent-type-definitions -- declaration merging requires interface interface Window { /** Forwards each layout stream response from the renderer to the oracle. */ __oracleLayoutFrame?: (response: RuntimeRpcResponse) => Promise } } function applyResponse(feed: LiveLayoutFeed, response: RuntimeRpcResponse): void { if (response.ok) { feed.apply(response.result) } else { feed.fail(`layout stream error: ${response.error.code}: ${response.error.message}`) } } // Bindings survive a reload, and a second registration on one page throws. const boundPages = new WeakSet() /** Subscribes in the window's renderer; call again after a reload, which ends the old stream. */ export async function subscribeWindowLayout(page: Page, feed: LiveLayoutFeed): Promise { feed.reopening() if (!boundPages.has(page)) { boundPages.add(page) await page.exposeFunction('__oracleLayoutFrame', (response: RuntimeRpcResponse) => applyResponse(feed, response) ) } await page.evaluate(async () => { await window.api.runtime.subscribe({ method: 'layout.subscribe', params: {} }, (response) => { void window.__oracleLayoutFrame?.(response) }) }) } /** Subscribes over a headless host's pairing offer; a serve restart ends it, so reopen after one. */ export async function subscribeHeadlessLayout( pairingUrl: string, feed: LiveLayoutFeed ): Promise<{ close: () => void }> { feed.reopening() const subscription = await subscribeRemoteRuntimeTransport( pairedRuntimeConnectionFromOffer(decodePairingOffer(pairingUrl)), 'layout.subscribe', {}, 30_000, { onResponse: (response) => applyResponse(feed, response), onError: (error) => feed.fail(`layout stream error: ${error.message}`), onClose: () => feed.reopening() } ) return { close: () => subscription.close() } }