Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 51 additions & 2 deletions apps/desktop/src/preview/DesktopBrowserHost.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @effect-diagnostics nodeBuiltinImport:off - Stands in for an Electron debugger.
import { describe, expect, it, vi } from "@effect/vitest";
import { DesktopBrowserEvent } from "@t3tools/contracts";
import { DesktopBrowserEvent, PREVIEW_TITLE_WATCH_OBJECT_GROUP } from "@t3tools/contracts";
import * as Effect from "effect/Effect";
import * as Fiber from "effect/Fiber";
import * as Queue from "effect/Queue";
Expand Down Expand Up @@ -36,12 +36,13 @@ const command = (
host: DesktopBrowserHost.DesktopBrowserHost["Service"],
id: number,
method: string,
params: Record<string, unknown> = {},
) =>
host.handleCommandLine(
encodeJson({
type: "cdp",
...key,
message: encodeJson({ id, method, params: { id }, sessionId: "t3-preview-page" }),
message: encodeJson({ id, method, params: { ...params, id }, sessionId: "t3-preview-page" }),
}),
);

Expand Down Expand Up @@ -440,6 +441,24 @@ describe("DesktopBrowserHost", () => {
}),
);

it.effect("neither focuses nor paints a guest for the server's pending title watch", () =>
Effect.gen(function* () {
const host = yield* makeDrawnHost;
const debuggee = makeDebuggee();
host.attach(key, debuggee.tab);
const painting = yield* watchPainting(host);

yield* command(host, 1, "Runtime.evaluate", {
objectGroup: PREVIEW_TITLE_WATCH_OBJECT_GROUP,
});
yield* settle;
yield* TestClock.adjust("1 minute");
expect(debuggee.sent).toEqual([1]);
expect(debuggee.focusEmulation).toEqual([]);
expect(painting).toEqual([]);
}).pipe(Effect.scoped),
);

it.effect("keeps a guest focused while a command it is running is still pending", () =>
Effect.gen(function* () {
const host = yield* makeDrawnHost;
Expand Down Expand Up @@ -780,6 +799,36 @@ describe("DesktopBrowserHost", () => {
}),
);

it.effect("keeps painting while the agent's turn is using the tab", () =>
Effect.gen(function* () {
const host = yield* DesktopBrowserHost.make;
const debuggee = makeDebuggee();
host.attach(key, debuggee.tab);
const painting = yield* watchPainting(host);
const turn = (active: boolean) =>
host.handleCommandLine(encodeJson({ type: "turn", threadId: key.threadId, active }));

yield* turn(true);
// Playwright's actionability check waits for the page's next frame.
yield* command(host, 1, "Runtime.callFunctionOn");
yield* settle;
expect(painting).toEqual([true]);

yield* turn(false);
yield* settle;
expect(painting).toEqual([true, false]);

// Outside a turn the guest paints until the agent goes idle.
yield* command(host, 2, "Input.dispatchMouseEvent");
yield* settle;
expect(painting).toEqual([true, false, true]);
debuggee.release();
yield* settle;
yield* TestClock.adjust("1 minute");
expect(painting).toEqual([true, false, true, false]);
}).pipe(Effect.scoped),
);

it.effect("lets a guest throttle when Chromium refuses a viewer's screencast", () =>
Effect.gen(function* () {
const host = yield* makeDrawnHost;
Expand Down
19 changes: 15 additions & 4 deletions apps/desktop/src/preview/DesktopBrowserHost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
DesktopBrowserCommand,
DesktopBrowserEvent,
type DesktopBrowserEvent as DesktopBrowserEventType,
PREVIEW_TITLE_WATCH_OBJECT_GROUP,
type PreviewAutomationLoadError,
} from "@t3tools/contracts";
import * as Context from "effect/Context";
Expand Down Expand Up @@ -237,20 +238,22 @@ export const make = Effect.gen(function* () {
// The agent pauses to think between steps; its tab stays focused until the turn ends.
if (tab.agentUsed && agentTurns.has(tab.key.threadId)) return;
tab.agentUsed = false;
syncPainting(tab);
if (tabs.get(keyOf(tab.key)) === tab) setFocusEmulation(tab, false);
}),
);
};

/**
* Electron composites no offscreen guest, so its screenshot or screencast waits for a frame
* that never comes. The client paints the guest behind the app while one is pending; the
* capture then finishes on its own, and the guest goes back offscreen once none is.
* Electron composites no offscreen guest, so a capture, or agent input that waits on the
* next frame, waits forever. The client paints the guest behind the app while one is pending
* or the agent drives it, and the guest goes back offscreen once neither holds.
*/
const syncPainting = (tab: AttachedTab) => {
const active =
tabs.get(keyOf(tab.key)) === tab &&
(tab.screencasts.size > 0 ||
(tab.agentUsed ||
tab.screencasts.size > 0 ||
tab.capturing.size > 0 ||
tab.waitingScreenshots.some((waiting) => waiting.live()));
if (tab.painting === active) return;
Expand Down Expand Up @@ -382,11 +385,19 @@ export const make = Effect.gen(function* () {
syncPainting(tab);
if (tab.screencasts.size > 0) return Promise.resolve({});
}
// Pending until the title changes, it would hold focus and painting for as long as the page lives.
if (
method === "Runtime.evaluate" &&
params["objectGroup"] === PREVIEW_TITLE_WATCH_OBJECT_GROUP
) {
return send(method, params, sessionId);
}
// A person's input through a viewer, or Playwright's own housekeeping, is not the agent's.
if (AGENT_INPUT_METHOD.test(method)) {
if (owner === undefined || !streamingOwners.has(owner)) {
tab.agentUsed = true;
tab.agentLedAt = performance.now();
syncPainting(tab);
} else if (method.startsWith("Input.") && !POINTER_TRAVEL.has(params["type"])) {
tab.agentLedAt = Number.NEGATIVE_INFINITY;
}
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/preview/ServerBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
type PreviewNavStatus,
type PreviewSessionSnapshot,
type PreviewViewportSetting,
PREVIEW_TITLE_WATCH_OBJECT_GROUP,
ThreadId,
SERVER_BROWSER_AUTOMATION_CLIENT_ID,
type PreviewAppearancePreference,
Expand Down Expand Up @@ -808,6 +809,7 @@ const make = Effect.gen(function* () {
contextId: watch.contextId,
expression: titleChange(title),
awaitPromise: true,
objectGroup: PREVIEW_TITLE_WATCH_OBJECT_GROUP,
})
.then(
({ exceptionDetails }) => exceptionDetails === undefined,
Expand Down
4 changes: 1 addition & 3 deletions apps/web/src/browser/HostedBrowserWebview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import { usePreviewBridge } from "~/components/preview/usePreviewBridge";
import { useClientSettingsHydrated } from "~/hooks/useSettings";
import { useViewportSize } from "~/hooks/useViewportSize";
import { cn, isMacPlatform } from "~/lib/utils";
import { cn } from "~/lib/utils";

import { resolveBrowserSurfacePanelRect, useBrowserSurfaceStore } from "./browserSurfaceStore";
import { useActiveBrowserRecordingTabIds } from "./browserRecording";
Expand Down Expand Up @@ -352,8 +352,6 @@ export function HostedBrowserWebview(props: {
const wrapperStyle = resolveHostedBrowserWebviewWrapperStyle({
active,
renderingActive,
// Electron 43 can permanently blank a macOS webview after `visibility: hidden`.
keepPaintableWhenInactive: isMacPlatform(navigator.platform),
cornerRadius: presentation.cornerRadius,
zIndex: presentation.zIndex,
rect: lastRect,
Expand Down
13 changes: 2 additions & 11 deletions apps/web/src/browser/hostedBrowserWebviewStyle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,12 @@ const HIDDEN_BROWSER_WEBVIEW_OFFSET = -100_000;
export function resolveHostedBrowserWebviewWrapperStyle(input: {
readonly active: boolean;
readonly renderingActive: boolean;
readonly keepPaintableWhenInactive?: boolean;
readonly cornerRadius?: number;
readonly zIndex?: number;
readonly rect: BrowserSurfaceRect | null;
readonly hiddenSize: HostedBrowserWebviewSize;
}): HostedBrowserWebviewWrapperStyle {
const {
active,
cornerRadius = 0,
hiddenSize,
keepPaintableWhenInactive = false,
rect,
renderingActive,
zIndex = 30,
} = input;
const { active, cornerRadius = 0, hiddenSize, rect, renderingActive, zIndex = 30 } = input;
if (active && rect) {
return {
left: rect.x,
Expand Down Expand Up @@ -68,6 +59,6 @@ export function resolveHostedBrowserWebviewWrapperStyle(input: {
height: hiddenSize.height,
zIndex: -1,
pointerEvents: "none",
visibility: keepPaintableWhenInactive ? "visible" : "hidden",
visibility: "hidden",
};
}
6 changes: 6 additions & 0 deletions packages/contracts/src/desktopBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ export const DesktopBrowserEvent = Schema.Union([
]);
export type DesktopBrowserEvent = typeof DesktopBrowserEvent.Type;

/**
* The `objectGroup` of the server's title watch, a `Runtime.evaluate` that stays pending until
* the page's title changes. It only waits, so the desktop host neither focuses nor paints for it.
*/
export const PREVIEW_TITLE_WATCH_OBJECT_GROUP = "t3-preview-title-watch";

export const DesktopBrowserCommand = Schema.Union([
Schema.Struct({ type: Schema.Literal("cdp"), ...TabKey, message: Schema.String }),
Schema.Struct({ type: Schema.Literal("release"), ...TabKey }),
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/src/ipc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ export interface DesktopPreviewTabState {
/** Chromium reports playback even when audioMuted is true. */
audible: boolean;
controller: "human" | "agent" | "none";
/** A screenshot or stream of the guest is pending, so the client keeps it painting while hidden. */
/** A screenshot, stream, or agent command needs the guest, so the client keeps it painting while hidden. */
capturing: boolean;
favicon?: DesktopPreviewFavicon;
updatedAt: string;
Expand Down
Loading