Open History to see the last commit
1import {
2 FILL_PREVIEW_VIEWPORT,
3 type PreviewAutomationOpenInput,
4 type PreviewSessionSnapshot,
5 type PreviewViewportSetting,
6} from "@t3tools/contracts";
7
8/**
9 * Viewport an agent-opened tab falls back to when the user has no configured
10 * browser default. Agents screenshot and assert against what they open, so a
11 * brand-new tab left in fill mode would be sized by whatever the panel happens
12 * to be — deterministic beats incidental here.
13 */
14export const DEFAULT_PREVIEW_AUTOMATION_VIEWPORT = {
15 _tag: "freeform",
16 width: 1280,
17 height: 800,
18} as const satisfies PreviewViewportSetting;
19
20/**
21 * An explicit `open`/`show` is the agent deliberately surfacing or suppressing
22 * its work, so it outranks the preference; the setting only decides what
23 * happens when the agent said nothing either way.
24 */
25export function shouldOpenPreviewMiniPlayer(
26 input: PreviewAutomationOpenInput,
27 autoShowFloatingPreview = true,
28): boolean {
29 return input.open ?? input.show ?? autoShowFloatingPreview;
30}
31
32export function previewAutomationOpenNeedsOverlay(
33 input: PreviewAutomationOpenInput,
34 snapshot: PreviewSessionSnapshot,
35): boolean {
36 return input.url !== undefined || snapshot.navStatus._tag !== "Idle";
37}
38
39/**
40 * Whether a freshly opened automation tab still needs a viewport applied.
41 *
42 * A configured browser default is sent with `preview.open`, so the snapshot
43 * already carries it and nothing is needed here. Fill means the user has no
44 * stated preference, which is where the agent fallback applies.
45 */
46export function previewAutomationDefaultViewport(
47 reusedExistingTab: boolean,
48 snapshot: PreviewSessionSnapshot,
49): PreviewViewportSetting | null {
50 const viewport = snapshot.viewport ?? FILL_PREVIEW_VIEWPORT;
51 return !reusedExistingTab && viewport._tag === "fill"
52 ? DEFAULT_PREVIEW_AUTOMATION_VIEWPORT
53 : null;
54}