Open History to see the last commit
1/**
2 * Preview - Schemas for the in-app browser preview surface.
3 *
4 * The preview is desktop-only (Chromium <webview>); the server tracks per-thread
5 * tab metadata so it survives client reconnects and multi-window. The desktop
6 * renderer mediates: it owns the actual <webview> and reports navigation back to
7 * the server via these RPCs, the server fans events to all subscribers.
8 *
9 * @module Preview
10 */
11import { Schema } from "effect";
12import { NonNegativeInt, PositiveInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
13
14export const PREVIEW_URL_MAX_LENGTH = 2_048;
15export const CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS = 32;
16
17const Url = TrimmedNonEmptyString.check(Schema.isMaxLength(PREVIEW_URL_MAX_LENGTH));
18
19export const ConfiguredLocalServerUrls = Schema.Array(Url).check(
20 Schema.isMaxLength(CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS),
21);
22const Title = Schema.String.check(Schema.isMaxLength(512));
23
24export const PreviewTabId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
25export type PreviewTabId = typeof PreviewTabId.Type;
26
27export const PREVIEW_VIEWPORT_MIN_DIMENSION = 240;
28export const PREVIEW_VIEWPORT_MAX_DIMENSION = 3840;
29export const PREVIEW_VIEWPORT_MAX_AREA = 3840 * 2160;
30
31const PreviewViewportDimension = Schema.Int.check(
32 Schema.isBetween({
33 minimum: PREVIEW_VIEWPORT_MIN_DIMENSION,
34 maximum: PREVIEW_VIEWPORT_MAX_DIMENSION,
35 }),
36);
37
38const viewportAreaFilter = Schema.makeFilter(
39 ({ width, height }: { readonly width: number; readonly height: number }) =>
40 width * height <= PREVIEW_VIEWPORT_MAX_AREA ||
41 `Viewport area must not exceed ${PREVIEW_VIEWPORT_MAX_AREA} pixels.`,
42);
43
44export const PreviewViewportSize = Schema.Struct({
45 width: PreviewViewportDimension,
46 height: PreviewViewportDimension,
47}).check(viewportAreaFilter);
48export type PreviewViewportSize = typeof PreviewViewportSize.Type;
49
50/**
51 * The page's measured viewport can be smaller than the minimum selectable
52 * fixed size while fill mode follows a narrow panel. Keep measurement
53 * validation separate from the stricter user-selectable size constraints.
54 */
55export const PreviewRenderedViewportSize = Schema.Struct({
56 width: Schema.Int.check(Schema.isGreaterThan(0)),
57 height: Schema.Int.check(Schema.isGreaterThan(0)),
58});
59export type PreviewRenderedViewportSize = typeof PreviewRenderedViewportSize.Type;
60
61export const PREVIEW_VIEWPORT_PRESET_IDS = [
62 "iphone-se",
63 "iphone-xr",
64 "iphone-12-pro",
65 "iphone-14-pro-max",
66 "pixel-7",
67 "samsung-galaxy-s8-plus",
68 "samsung-galaxy-s20-ultra",
69 "ipad-mini",
70 "ipad-air",
71 "ipad-pro",
72 "surface-pro-7",
73 "surface-duo",
74 "galaxy-z-fold-5",
75 "asus-zenbook-fold",
76 "samsung-galaxy-a51-71",
77 "nest-hub",
78 "nest-hub-max",
79] as const;
80
81export const PreviewViewportPresetId = Schema.Literals(PREVIEW_VIEWPORT_PRESET_IDS);
82export type PreviewViewportPresetId = typeof PreviewViewportPresetId.Type;
83
84/**
85 * Preset IDs shipped before the Chrome-compatible catalog. Existing sessions
86 * can still reconnect with these values, but new resize requests only expose
87 * PREVIEW_VIEWPORT_PRESET_IDS.
88 */
89const LEGACY_PREVIEW_VIEWPORT_PRESET_IDS = [
90 "desktop-1920x1080",
91 "desktop-1440x900",
92 "laptop-1366x768",
93 "laptop-1280x800",
94 "ipad-pro-11",
95 "iphone-15-pro",
96 "pixel-8",
97 "galaxy-s24",
98] as const;
99
100const StoredPreviewViewportPresetId = Schema.Literals([
101 ...PREVIEW_VIEWPORT_PRESET_IDS,
102 ...LEGACY_PREVIEW_VIEWPORT_PRESET_IDS,
103]);
104
105export const PreviewViewportSetting = Schema.Union([
106 Schema.TaggedStruct("fill", {}),
107 Schema.TaggedStruct("freeform", {
108 ...PreviewViewportSize.fields,
109 }).check(viewportAreaFilter),
110 Schema.TaggedStruct("preset", {
111 ...PreviewViewportSize.fields,
112 presetId: StoredPreviewViewportPresetId,
113 }).check(viewportAreaFilter),
114]);
115export type PreviewViewportSetting = typeof PreviewViewportSetting.Type;
116
117export const FILL_PREVIEW_VIEWPORT = {
118 _tag: "fill",
119} as const satisfies PreviewViewportSetting;
120
121export const PreviewNavStatus = Schema.Union([
122 Schema.TaggedStruct("Idle", {}),
123 Schema.TaggedStruct("Loading", {
124 url: Url,
125 title: Title,
126 }),
127 Schema.TaggedStruct("Success", {
128 url: Url,
129 title: Title,
130 }),
131 Schema.TaggedStruct("LoadFailed", {
132 url: Url,
133 title: Title,
134 code: Schema.Int,
135 description: Schema.String,
136 }),
137]);
138export type PreviewNavStatus = typeof PreviewNavStatus.Type;
139
140export const PreviewSessionSnapshot = Schema.Struct({
141 threadId: TrimmedNonEmptyString,
142 tabId: PreviewTabId,
143 navStatus: PreviewNavStatus,
144 canGoBack: Schema.Boolean,
145 canGoForward: Schema.Boolean,
146 /** Missing snapshots from older servers are treated as fill-panel mode. */
147 viewport: Schema.optional(PreviewViewportSetting),
148 updatedAt: Schema.String,
149});
150export type PreviewSessionSnapshot = typeof PreviewSessionSnapshot.Type;
151
152export const PreviewOpenInput = Schema.Struct({
153 threadId: ThreadId,
154 /** Omit to create an empty (Idle) tab the user can type into. */
155 url: Schema.optional(Url),
156});
157export type PreviewOpenInput = typeof PreviewOpenInput.Type;
158
159export const PreviewNavigateInput = Schema.Struct({
160 threadId: ThreadId,
161 tabId: PreviewTabId,
162 url: Url,
163 resolvedTitle: Schema.optional(Title),
164});
165export type PreviewNavigateInput = typeof PreviewNavigateInput.Type;
166
167export const PreviewReportStatusInput = Schema.Struct({
168 threadId: ThreadId,
169 tabId: PreviewTabId,
170 navStatus: PreviewNavStatus,
171 canGoBack: Schema.Boolean,
172 canGoForward: Schema.Boolean,
173});
174export type PreviewReportStatusInput = typeof PreviewReportStatusInput.Type;
175
176export const PreviewRefreshInput = Schema.Struct({
177 threadId: ThreadId,
178 tabId: PreviewTabId,
179});
180export type PreviewRefreshInput = typeof PreviewRefreshInput.Type;
181
182export const PreviewResizeInput = Schema.Struct({
183 threadId: ThreadId,
184 tabId: PreviewTabId,
185 viewport: PreviewViewportSetting,
186});
187export type PreviewResizeInput = typeof PreviewResizeInput.Type;
188
189export const PreviewCloseInput = Schema.Struct({
190 threadId: ThreadId,
191 tabId: Schema.optional(PreviewTabId),
192});
193export type PreviewCloseInput = typeof PreviewCloseInput.Type;
194
195export const PreviewListInput = Schema.Struct({
196 threadId: ThreadId,
197});
198export type PreviewListInput = typeof PreviewListInput.Type;
199
200export const PreviewListResult = Schema.Struct({
201 sessions: Schema.Array(PreviewSessionSnapshot),
202 /** Identifies the current server process so revision resets are safe. */
203 serverEpoch: TrimmedNonEmptyString,
204 /** Monotonic server state revision used to reject stale list responses. */
205 revision: NonNegativeInt,
206});
207export type PreviewListResult = typeof PreviewListResult.Type;
208
209const PreviewEventBaseSchema = Schema.Struct({
210 threadId: TrimmedNonEmptyString,
211 tabId: PreviewTabId,
212 createdAt: Schema.String,
213 /** Identifies the server process that emitted this event. */
214 serverEpoch: TrimmedNonEmptyString,
215 /** Monotonic server state revision shared with PreviewListResult. */
216 revision: PositiveInt,
217});
218
219const PreviewOpenedEvent = Schema.Struct({
220 ...PreviewEventBaseSchema.fields,
221 type: Schema.Literal("opened"),
222 snapshot: PreviewSessionSnapshot,
223});
224
225const PreviewNavigatedEvent = Schema.Struct({
226 ...PreviewEventBaseSchema.fields,
227 type: Schema.Literal("navigated"),
228 snapshot: PreviewSessionSnapshot,
229});
230
231const PreviewResizedEvent = Schema.Struct({
232 ...PreviewEventBaseSchema.fields,
233 type: Schema.Literal("resized"),
234 snapshot: PreviewSessionSnapshot,
235});
236
237const PreviewFailedEvent = Schema.Struct({
238 ...PreviewEventBaseSchema.fields,
239 type: Schema.Literal("failed"),
240 url: Url,
241 title: Title,
242 code: Schema.Int,
243 description: Schema.String,
244});
245
246const PreviewClosedEvent = Schema.Struct({
247 ...PreviewEventBaseSchema.fields,
248 type: Schema.Literal("closed"),
249});
250
251export const PreviewEvent = Schema.Union([
252 PreviewOpenedEvent,
253 PreviewNavigatedEvent,
254 PreviewResizedEvent,
255 PreviewFailedEvent,
256 PreviewClosedEvent,
257]);
258export type PreviewEvent = typeof PreviewEvent.Type;
259
260/**
261 * A localhost server detected by the port scanner. Used to populate the
262 * "Local" recommendations in the empty-state of the preview panel.
263 */
264export const DiscoveredLocalServer = Schema.Struct({
265 host: TrimmedNonEmptyString,
266 port: Schema.Int.check(Schema.isGreaterThan(0)).check(Schema.isLessThan(65536)),
267 url: Url,
268 processName: Schema.NullOr(TrimmedNonEmptyString),
269 pid: Schema.NullOr(Schema.Int.check(Schema.isGreaterThan(0))),
270 terminal: Schema.NullOr(
271 Schema.Struct({
272 threadId: ThreadId,
273 terminalId: TrimmedNonEmptyString,
274 }),
275 ),
276});
277export type DiscoveredLocalServer = typeof DiscoveredLocalServer.Type;
278
279export const DiscoveredLocalServerList = Schema.Struct({
280 servers: Schema.Array(DiscoveredLocalServer),
281 scannedAt: Schema.String,
282 configuredUrlProbing: Schema.optional(Schema.Literal(true)),
283});
284export type DiscoveredLocalServerList = typeof DiscoveredLocalServerList.Type;
285
286export class PreviewSessionLookupError extends Schema.TaggedErrorClass<PreviewSessionLookupError>()(
287 "PreviewSessionLookupError",
288 {
289 threadId: Schema.String,
290 tabId: Schema.String,
291 },
292) {
293 override get message() {
294 return `Unknown preview session: thread=${this.threadId}, tab=${this.tabId}`;
295 }
296}
297
298export class PreviewInvalidUrlError extends Schema.TaggedErrorClass<PreviewInvalidUrlError>()(
299 "PreviewInvalidUrlError",
300 {
301 inputLength: Schema.Number,
302 reason: Schema.Literals(["empty", "parse", "unsupported-protocol", "unexpected"]),
303 protocol: Schema.optional(Schema.String),
304 cause: Schema.Defect(),
305 },
306) {
307 override get message() {
308 const protocol = this.protocol === undefined ? "" : `: ${this.protocol}`;
309 return `Invalid preview URL (${this.reason}${protocol}; input length ${this.inputLength}).`;
310 }
311}
312
313export const PreviewError = Schema.Union([PreviewSessionLookupError, PreviewInvalidUrlError]);
314export type PreviewError = typeof PreviewError.Type;