Open History to see the last commit
1import { Schema } from "effect";
2import { NonNegativeInt, PositiveInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
3import { BROWSER_PROFILE_MAX_COUNT, BrowserProfile, BrowserProfileId } from "./browserProfile.ts";
4
5export const PREVIEW_URL_MAX_LENGTH = 2_048;
6export const CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS = 32;
7
8const Url = TrimmedNonEmptyString.check(Schema.isMaxLength(PREVIEW_URL_MAX_LENGTH));
9
10export const ConfiguredLocalServerUrls = Schema.Array(Url).check(
11 Schema.isMaxLength(CONFIGURED_LOCAL_SERVER_URLS_MAX_ITEMS),
12);
13const Title = Schema.String.check(Schema.isMaxLength(512));
14
15export const PreviewTabId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
16export type PreviewTabId = typeof PreviewTabId.Type;
17
18export const PREVIEW_VIEWPORT_MIN_DIMENSION = 240;
19export const PREVIEW_VIEWPORT_MAX_DIMENSION = 3840;
20export const PREVIEW_VIEWPORT_MAX_AREA = 3840 * 2160;
21
22const PreviewViewportDimension = Schema.Int.check(
23 Schema.isBetween({
24 minimum: PREVIEW_VIEWPORT_MIN_DIMENSION,
25 maximum: PREVIEW_VIEWPORT_MAX_DIMENSION,
26 }),
27);
28
29const viewportAreaFilter = Schema.makeFilter(
30 ({ width, height }: { readonly width: number; readonly height: number }) =>
31 width * height <= PREVIEW_VIEWPORT_MAX_AREA ||
32 `Viewport area must not exceed ${PREVIEW_VIEWPORT_MAX_AREA} pixels.`,
33);
34
35export const PreviewViewportSize = Schema.Struct({
36 width: PreviewViewportDimension,
37 height: PreviewViewportDimension,
38}).check(viewportAreaFilter);
39export type PreviewViewportSize = typeof PreviewViewportSize.Type;
40
41/** Fill mode can measure below selectable minimum sizes; validate measurements separately. */
42export const PreviewRenderedViewportSize = Schema.Struct({
43 width: Schema.Int.check(Schema.isGreaterThan(0)),
44 height: Schema.Int.check(Schema.isGreaterThan(0)),
45});
46export type PreviewRenderedViewportSize = typeof PreviewRenderedViewportSize.Type;
47
48export const PREVIEW_VIEWPORT_PRESET_IDS = [
49 "iphone-se",
50 "iphone-xr",
51 "iphone-12-pro",
52 "iphone-14-pro-max",
53 "pixel-7",
54 "samsung-galaxy-s8-plus",
55 "samsung-galaxy-s20-ultra",
56 "ipad-mini",
57 "ipad-air",
58 "ipad-pro",
59 "surface-pro-7",
60 "surface-duo",
61 "galaxy-z-fold-5",
62 "asus-zenbook-fold",
63 "samsung-galaxy-a51-71",
64 "nest-hub",
65 "nest-hub-max",
66] as const;
67
68export const PreviewViewportPresetId = Schema.Literals(PREVIEW_VIEWPORT_PRESET_IDS);
69export type PreviewViewportPresetId = typeof PreviewViewportPresetId.Type;
70
71const LEGACY_PREVIEW_VIEWPORT_PRESET_IDS = [
72 "desktop-1920x1080",
73 "desktop-1440x900",
74 "laptop-1366x768",
75 "laptop-1280x800",
76 "ipad-pro-11",
77 "iphone-15-pro",
78 "pixel-8",
79 "galaxy-s24",
80] as const;
81
82const StoredPreviewViewportPresetId = Schema.Literals([
83 ...PREVIEW_VIEWPORT_PRESET_IDS,
84 ...LEGACY_PREVIEW_VIEWPORT_PRESET_IDS,
85]);
86
87export const PreviewViewportSetting = Schema.Union([
88 Schema.TaggedStruct("fill", {}),
89 Schema.TaggedStruct("freeform", {
90 ...PreviewViewportSize.fields,
91 }).check(viewportAreaFilter),
92 Schema.TaggedStruct("preset", {
93 ...PreviewViewportSize.fields,
94 presetId: StoredPreviewViewportPresetId,
95 }).check(viewportAreaFilter),
96]);
97export type PreviewViewportSetting = typeof PreviewViewportSetting.Type;
98
99export const FILL_PREVIEW_VIEWPORT = {
100 _tag: "fill",
101} as const satisfies PreviewViewportSetting;
102
103/** Shared steps keep settings aligned with desktop Chromium zoom controls. */
104export const PREVIEW_ZOOM_LEVELS = [
105 0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1.0, 1.1, 1.25, 1.5, 1.75, 2.0, 2.5, 3.0, 4.0, 5.0,
106] as const;
107
108export const PreviewZoomFactor = Schema.Literals(PREVIEW_ZOOM_LEVELS);
109export type PreviewZoomFactor = typeof PreviewZoomFactor.Type;
110
111export const DEFAULT_PREVIEW_ZOOM_FACTOR: PreviewZoomFactor = 1.0;
112
113export const PreviewAppearancePreference = Schema.Literals(["system", "light", "dark"]);
114export type PreviewAppearancePreference = typeof PreviewAppearancePreference.Type;
115
116export const DEFAULT_PREVIEW_APPEARANCE: PreviewAppearancePreference = "system";
117
118const PreviewFileDownload = Schema.Struct({ id: Schema.String, fileName: Schema.String });
119
120export const PreviewNavStatus = Schema.Union([
121 Schema.TaggedStruct("Idle", {}),
122 Schema.TaggedStruct("Loading", {
123 url: Url,
124 title: Title,
125 }),
126 Schema.TaggedStruct("Success", {
127 url: Url,
128 title: Title,
129 }),
130 Schema.TaggedStruct("LoadFailed", {
131 url: Url,
132 title: Title,
133 code: Schema.Int,
134 description: Schema.String,
135 download: Schema.optional(PreviewFileDownload),
136 }),
137]);
138export type PreviewNavStatus = typeof PreviewNavStatus.Type;
139
140export const PreviewRuntime = Schema.Literals(["desktop", "server"]);
141export type PreviewRuntime = typeof PreviewRuntime.Type;
142
143/** JSON reason for preview stream close code 4503; must fit the 123-byte close-reason limit. */
144export const PreviewStreamHostSetup = Schema.Struct({
145 need: Schema.Literals(["sandbox", "libraries"]),
146 command: Schema.String,
147});
148export type PreviewStreamHostSetup = typeof PreviewStreamHostSetup.Type;
149export const PREVIEW_STREAM_HOST_SETUP_CLOSE_CODE = 4503;
150
151export const PreviewSessionSnapshot = Schema.Struct({
152 threadId: TrimmedNonEmptyString,
153 tabId: PreviewTabId,
154 navStatus: PreviewNavStatus,
155 canGoBack: Schema.Boolean,
156 canGoForward: Schema.Boolean,
157 viewport: Schema.optional(PreviewViewportSetting),
158 colorScheme: Schema.optional(PreviewAppearancePreference),
159 zoomFactor: Schema.optional(PreviewZoomFactor),
160 /** Fixed at open; Electron cannot change a webview partition after attach without losing page state. */
161 profileId: Schema.optional(BrowserProfileId),
162 runtime: Schema.optional(PreviewRuntime),
163 automationOwner: Schema.optional(Schema.String),
164 reveal: Schema.optional(Schema.Boolean),
165 revealRequest: Schema.optional(Schema.Struct({ id: Schema.String, force: Schema.Boolean })),
166 updatedAt: Schema.String,
167});
168export type PreviewSessionSnapshot = typeof PreviewSessionSnapshot.Type;
169
170export const PreviewOpenInput = Schema.Struct({
171 threadId: ThreadId,
172 url: Schema.optional(Url),
173 viewport: Schema.optional(PreviewViewportSetting),
174 profileId: Schema.optional(BrowserProfileId),
175 runtime: Schema.optional(PreviewRuntime),
176 reveal: Schema.optional(Schema.Boolean),
177});
178export type PreviewOpenInput = typeof PreviewOpenInput.Type;
179
180export const PreviewNavigateInput = Schema.Struct({
181 threadId: ThreadId,
182 tabId: PreviewTabId,
183 url: Url,
184 resolvedTitle: Schema.optional(Title),
185});
186export type PreviewNavigateInput = typeof PreviewNavigateInput.Type;
187
188export const PreviewReportStatusInput = Schema.Struct({
189 threadId: ThreadId,
190 tabId: PreviewTabId,
191 navStatus: PreviewNavStatus,
192 canGoBack: Schema.Boolean,
193 canGoForward: Schema.Boolean,
194});
195export type PreviewReportStatusInput = typeof PreviewReportStatusInput.Type;
196
197export const PreviewRefreshInput = Schema.Struct({
198 threadId: ThreadId,
199 tabId: PreviewTabId,
200});
201export type PreviewRefreshInput = typeof PreviewRefreshInput.Type;
202
203export const PreviewResizeInput = Schema.Struct({
204 threadId: ThreadId,
205 tabId: PreviewTabId,
206 viewport: PreviewViewportSetting,
207});
208export type PreviewResizeInput = typeof PreviewResizeInput.Type;
209
210/** Requires no tab control, unlike page input. */
211export const PreviewAdjustInput = Schema.Struct({
212 threadId: ThreadId,
213 tabId: PreviewTabId,
214 colorScheme: Schema.optional(PreviewAppearancePreference),
215 zoomFactor: Schema.optional(PreviewZoomFactor),
216 hardReload: Schema.optional(Schema.Boolean),
217 clear: Schema.optional(Schema.Literals(["cookies", "cache"])),
218});
219export type PreviewAdjustInput = typeof PreviewAdjustInput.Type;
220
221export const PreviewCloseInput = Schema.Struct({
222 threadId: ThreadId,
223 tabId: Schema.optional(PreviewTabId),
224});
225export type PreviewCloseInput = typeof PreviewCloseInput.Type;
226
227export const BROWSER_RECORDING_FRAME_RATES = [30, 60] as const;
228export const BrowserRecordingFrameRate = Schema.Literals(BROWSER_RECORDING_FRAME_RATES);
229export type BrowserRecordingFrameRate = typeof BrowserRecordingFrameRate.Type;
230export const DEFAULT_BROWSER_RECORDING_FRAME_RATE: BrowserRecordingFrameRate = 30;
231
232export const PreviewReportProfilesInput = Schema.Struct({
233 profiles: Schema.Array(BrowserProfile).check(Schema.isMaxLength(BROWSER_PROFILE_MAX_COUNT)),
234 defaultProfileId: BrowserProfileId,
235 /** Optional so older clients keep reporting; omitting it keeps the last reported rate. */
236 recordingFrameRate: Schema.optional(BrowserRecordingFrameRate),
237});
238export type PreviewReportProfilesInput = typeof PreviewReportProfilesInput.Type;
239
240export const PreviewClearProfileInput = Schema.Struct({
241 profileId: BrowserProfileId,
242});
243export type PreviewClearProfileInput = typeof PreviewClearProfileInput.Type;
244
245export const PreviewListInput = Schema.Struct({
246 threadId: ThreadId,
247});
248export type PreviewListInput = typeof PreviewListInput.Type;
249
250export const PreviewListResult = Schema.Struct({
251 sessions: Schema.Array(PreviewSessionSnapshot),
252 /** Identifies the current server process so revision resets are safe. */
253 serverEpoch: TrimmedNonEmptyString,
254 revision: NonNegativeInt,
255});
256export type PreviewListResult = typeof PreviewListResult.Type;
257
258const PreviewEventBaseSchema = Schema.Struct({
259 threadId: TrimmedNonEmptyString,
260 tabId: PreviewTabId,
261 createdAt: Schema.String,
262 serverEpoch: TrimmedNonEmptyString,
263 /** Monotonic server state revision shared with PreviewListResult. */
264 revision: PositiveInt,
265});
266
267const PreviewOpenedEvent = Schema.Struct({
268 ...PreviewEventBaseSchema.fields,
269 type: Schema.Literal("opened"),
270 snapshot: PreviewSessionSnapshot,
271});
272
273const PreviewNavigatedEvent = Schema.Struct({
274 ...PreviewEventBaseSchema.fields,
275 type: Schema.Literal("navigated"),
276 snapshot: PreviewSessionSnapshot,
277});
278
279const PreviewResizedEvent = Schema.Struct({
280 ...PreviewEventBaseSchema.fields,
281 type: Schema.Literal("resized"),
282 snapshot: PreviewSessionSnapshot,
283 request: Schema.optional(
284 Schema.Struct({
285 hardReload: Schema.optional(Schema.Boolean),
286 clear: Schema.optional(Schema.Literals(["cookies", "cache"])),
287 }),
288 ),
289});
290
291const PreviewFailedEvent = Schema.Struct({
292 ...PreviewEventBaseSchema.fields,
293 type: Schema.Literal("failed"),
294 url: Url,
295 title: Title,
296 code: Schema.Int,
297 description: Schema.String,
298 download: Schema.optional(PreviewFileDownload),
299});
300
301const PreviewClosedEvent = Schema.Struct({
302 ...PreviewEventBaseSchema.fields,
303 type: Schema.Literal("closed"),
304});
305
306export const PreviewEvent = Schema.Union([
307 PreviewOpenedEvent,
308 PreviewNavigatedEvent,
309 PreviewResizedEvent,
310 PreviewFailedEvent,
311 PreviewClosedEvent,
312]);
313export type PreviewEvent = typeof PreviewEvent.Type;
314
315export const DiscoveredLocalServer = Schema.Struct({
316 host: TrimmedNonEmptyString,
317 port: Schema.Int.check(Schema.isGreaterThan(0)).check(Schema.isLessThan(65536)),
318 url: Url,
319 processName: Schema.NullOr(TrimmedNonEmptyString),
320 pid: Schema.NullOr(Schema.Int.check(Schema.isGreaterThan(0))),
321 terminal: Schema.NullOr(
322 Schema.Struct({
323 threadId: ThreadId,
324 terminalId: TrimmedNonEmptyString,
325 }),
326 ),
327});
328export type DiscoveredLocalServer = typeof DiscoveredLocalServer.Type;
329
330export const DiscoveredLocalServerList = Schema.Struct({
331 servers: Schema.Array(DiscoveredLocalServer),
332 scannedAt: Schema.String,
333 configuredUrlProbing: Schema.optional(Schema.Literal(true)),
334});
335export type DiscoveredLocalServerList = typeof DiscoveredLocalServerList.Type;
336
337export class PreviewSessionLookupError extends Schema.TaggedError<PreviewSessionLookupError>()(
338 "PreviewSessionLookupError",
339 {
340 threadId: Schema.String,
341 tabId: Schema.String,
342 },
343) {
344 override get message() {
345 return `Unknown preview session: thread=${this.threadId}, tab=${this.tabId}`;
346 }
347}
348
349export class PreviewInvalidUrlError extends Schema.TaggedError<PreviewInvalidUrlError>()(
350 "PreviewInvalidUrlError",
351 {
352 inputLength: Schema.Number,
353 reason: Schema.Literals(["empty", "parse", "unsupported-protocol", "unexpected"]),
354 protocol: Schema.optional(Schema.String),
355 cause: Schema.Defect(),
356 },
357) {
358 override get message() {
359 const protocol = this.protocol === undefined ? "" : `: ${this.protocol}`;
360 return `Invalid preview URL (${this.reason}${protocol}; input length ${this.inputLength}).`;
361 }
362}
363
364export class PreviewControlRequiredError extends Schema.TaggedError<PreviewControlRequiredError>()(
365 "PreviewControlRequiredError",
366 { tabId: Schema.String },
367) {
368 override get message() {
369 return "Take control of the server browser and use its viewer controls.";
370 }
371}
372
373export class PreviewClearProfileError extends Schema.TaggedError<PreviewClearProfileError>()(
374 "PreviewClearProfileError",
375 { profileId: Schema.String, cause: Schema.Defect() },
376) {
377 override get message() {
378 return "The environment could not delete this browser profile's data.";
379 }
380}
381
382export const PreviewError = Schema.Union([
383 PreviewSessionLookupError,
384 PreviewInvalidUrlError,
385 PreviewControlRequiredError,
386]);
387export type PreviewError = typeof PreviewError.Type;