Open History to see the last commit
1import { Schema } from "effect";
2
3import { ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
4
5export const DevicePlatform = Schema.Literals(["ios", "android"]);
6export type DevicePlatform = typeof DevicePlatform.Type;
7
8export const DeviceHostId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
9export type DeviceHostId = typeof DeviceHostId.Type;
10
11export const LOCAL_DEVICE_HOST_ID = "local" as DeviceHostId;
12
13export const SshDeviceHostConfig = Schema.Struct({
14 id: DeviceHostId.check(
15 Schema.isPattern(/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/),
16 Schema.makeFilter((id) => id !== "local" || "The local host id is reserved."),
17 ),
18 label: TrimmedNonEmptyString,
19 target: TrimmedNonEmptyString.check(Schema.isPattern(/^[^\s-][^\s]*$/)),
20 identityFile: Schema.optional(TrimmedNonEmptyString),
21 port: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }))),
22});
23export type SshDeviceHostConfig = typeof SshDeviceHostConfig.Type;
24
25export const SshDeviceHostConfigs = Schema.Array(SshDeviceHostConfig).check(
26 Schema.makeFilter(
27 (hosts) =>
28 new Set(hosts.map((host) => host.id)).size === hosts.length ||
29 "Device host ids must be unique.",
30 ),
31);
32
33export const DeviceId = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
34export type DeviceId = typeof DeviceId.Type;
35
36export const DeviceSummary = Schema.Struct({
37 hostId: DeviceHostId,
38 id: DeviceId,
39 platform: DevicePlatform,
40 name: TrimmedNonEmptyString,
41 version: Schema.String,
42 booted: Schema.Boolean,
43 physical: Schema.Boolean,
44});
45export type DeviceSummary = typeof DeviceSummary.Type;
46
47export const DevicePlatformAvailability = Schema.Struct({
48 platform: DevicePlatform,
49 available: Schema.Boolean,
50 reason: Schema.optional(Schema.String),
51});
52export type DevicePlatformAvailability = typeof DevicePlatformAvailability.Type;
53
54export const DeviceToolVersion = Schema.Struct({
55 requiredVersion: Schema.String,
56 installedVersions: Schema.Array(Schema.String),
57 runningVersion: Schema.NullOr(Schema.String),
58});
59export type DeviceToolVersion = typeof DeviceToolVersion.Type;
60
61export const DeviceToolVersions = Schema.Struct({
62 hub: DeviceToolVersion,
63 agent: DeviceToolVersion,
64});
65export type DeviceToolVersions = typeof DeviceToolVersions.Type;
66
67export const DeviceHostSummary = Schema.Struct({
68 id: DeviceHostId,
69 kind: Schema.Literals(["local", "ssh"]),
70 label: TrimmedNonEmptyString,
71 platforms: Schema.Array(DevicePlatformAvailability),
72 tools: Schema.optional(DeviceToolVersions),
73 toolInspectionError: Schema.optional(Schema.String),
74 hubInstalled: Schema.Boolean,
75 agentDeviceInstalled: Schema.Boolean,
76});
77export type DeviceHostSummary = typeof DeviceHostSummary.Type;
78
79export const DeviceHostStatus = Schema.Literals([
80 "disabled",
81 "idle",
82 "installing",
83 "starting",
84 "ready",
85 "failed",
86]);
87export type DeviceHostStatus = typeof DeviceHostStatus.Type;
88
89export const DeviceSession = Schema.Struct({
90 threadId: ThreadId,
91 hostId: DeviceHostId,
92 deviceId: DeviceId,
93 platform: DevicePlatform,
94 openedAt: Schema.String,
95});
96export type DeviceSession = typeof DeviceSession.Type;
97
98export const DeviceServiceState = Schema.Struct({
99 supportsHostRetry: Schema.optional(Schema.Boolean),
100 supportsToolUpdate: Schema.optional(Schema.Boolean),
101 supportsToolInspection: Schema.optional(Schema.Boolean),
102 hosts: Schema.Array(DeviceHostSummary),
103 hostStatus: DeviceHostStatus,
104 hostStatusDetail: Schema.optional(Schema.String),
105 hostStatuses: Schema.Record(
106 DeviceHostId,
107 Schema.Struct({
108 status: DeviceHostStatus,
109 detail: Schema.optional(Schema.String),
110 androidDiscoveryIncomplete: Schema.optional(Schema.Boolean),
111 }),
112 ),
113 devices: Schema.Array(DeviceSummary),
114 sessions: Schema.Array(DeviceSession),
115 bootingDevices: Schema.optional(
116 Schema.Array(Schema.Struct({ ...DeviceSummary.fields, threadId: ThreadId })),
117 ),
118 onboardingCompleted: Schema.Boolean,
119 agentAccessEnabled: Schema.Boolean,
120 hubBasePath: Schema.String,
121 revision: Schema.Int,
122});
123export type DeviceServiceState = typeof DeviceServiceState.Type;
124
125export const DeviceListInput = Schema.Struct({
126 updateTool: Schema.optional(Schema.Literals(["hub", "agent"])),
127 inspectOnly: Schema.optional(Schema.Boolean),
128 retryHostId: Schema.optional(DeviceHostId),
129});
130export type DeviceListInput = typeof DeviceListInput.Type;
131
132export const DeviceConfigureInput = Schema.Struct({
133 enabled: Schema.optional(Schema.Boolean),
134 agentAccessEnabled: Schema.optional(Schema.Boolean),
135 onboardingCompleted: Schema.optional(Schema.Boolean),
136});
137export type DeviceConfigureInput = typeof DeviceConfigureInput.Type;
138
139export const DeviceOpenInput = Schema.Struct({
140 threadId: ThreadId,
141 hostId: Schema.optional(DeviceHostId),
142 deviceId: DeviceId,
143 platform: DevicePlatform,
144 /** Boot the simulator or emulator when it is not running. Defaults to true. */
145 boot: Schema.optional(Schema.Boolean),
146});
147export type DeviceOpenInput = typeof DeviceOpenInput.Type;
148
149export const DeviceCloseInput = Schema.Struct({
150 hostId: Schema.optional(DeviceHostId),
151 threadId: ThreadId,
152 /** Omit to close every device session for the thread. */
153 deviceId: Schema.optional(DeviceId),
154 /** Also shut the simulator or emulator down. Defaults to false. */
155 shutdown: Schema.optional(Schema.Boolean),
156});
157export type DeviceCloseInput = typeof DeviceCloseInput.Type;
158
159export const DeviceShutdownInput = Schema.Struct({
160 hostId: Schema.optional(DeviceHostId),
161 deviceId: DeviceId,
162 platform: DevicePlatform,
163});
164export type DeviceShutdownInput = typeof DeviceShutdownInput.Type;
165
166export const DeviceAppearance = Schema.Literals(["light", "dark"]);
167export type DeviceAppearance = typeof DeviceAppearance.Type;
168
169export const DeviceTextSize = Schema.Literals(["small", "default", "large", "extra-large"]);
170export type DeviceTextSize = typeof DeviceTextSize.Type;
171
172export const DeviceColorFilter = Schema.Literals([
173 "none",
174 "grayscale",
175 "red-green",
176 "green-red",
177 "blue-yellow",
178]);
179export type DeviceColorFilter = typeof DeviceColorFilter.Type;
180
181export const DeviceOrientation = Schema.Literals([
182 "portrait",
183 "landscape_left",
184 "portrait_upside_down",
185 "landscape_right",
186]);
187export type DeviceOrientation = typeof DeviceOrientation.Type;
188
189export const DeviceSettings = Schema.Struct({
190 appearance: Schema.optional(DeviceAppearance),
191 textSize: Schema.optional(DeviceTextSize),
192 reduceMotion: Schema.optional(Schema.Boolean),
193 increaseContrast: Schema.optional(Schema.Boolean),
194 reduceTransparency: Schema.optional(Schema.Boolean),
195 showBorders: Schema.optional(Schema.Boolean),
196 voiceOver: Schema.optional(Schema.Boolean),
197 liquidGlass: Schema.optional(Schema.Literals(["clear", "tinted"])),
198 colorFilter: Schema.optional(DeviceColorFilter),
199 networkEnabled: Schema.optional(Schema.Boolean),
200 location: Schema.optional(
201 Schema.NullOr(Schema.Struct({ latitude: Schema.Number, longitude: Schema.Number })),
202 ),
203});
204export type DeviceSettings = typeof DeviceSettings.Type;
205
206export const DeviceForegroundApp = Schema.Struct({
207 id: Schema.String,
208 name: Schema.optional(Schema.String),
209 version: Schema.optional(Schema.String),
210});
211export type DeviceForegroundApp = typeof DeviceForegroundApp.Type;
212
213export const DeviceDetail = Schema.Struct({
214 hostId: DeviceHostId,
215 deviceId: DeviceId,
216 settings: DeviceSettings,
217 foregroundApp: Schema.NullOr(DeviceForegroundApp),
218 readAt: Schema.String,
219});
220export type DeviceDetail = typeof DeviceDetail.Type;
221
222export const DevicePermission = Schema.Literals([
223 "camera",
224 "microphone",
225 "photos",
226 "contacts",
227 "calendar",
228 "reminders",
229 "location",
230 "notifications",
231 "motion",
232 "media-library",
233 "faceid",
234]);
235export type DevicePermission = typeof DevicePermission.Type;
236
237const DeviceTarget = {
238 hostId: Schema.optional(DeviceHostId),
239 deviceId: DeviceId,
240};
241
242export const DeviceActionInput = Schema.Union([
243 Schema.Struct({
244 ...DeviceTarget,
245 type: Schema.Literal("setAppearance"),
246 value: DeviceAppearance,
247 }),
248 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("setTextSize"), value: DeviceTextSize }),
249 Schema.Struct({
250 ...DeviceTarget,
251 type: Schema.Literal("setToggle"),
252 setting: Schema.Literals([
253 "reduceMotion",
254 "increaseContrast",
255 "reduceTransparency",
256 "showBorders",
257 "voiceOver",
258 "networkEnabled",
259 ]),
260 value: Schema.Boolean,
261 }),
262 Schema.Struct({
263 ...DeviceTarget,
264 type: Schema.Literal("setLiquidGlass"),
265 value: Schema.Literals(["clear", "tinted"]),
266 }),
267 Schema.Struct({
268 ...DeviceTarget,
269 type: Schema.Literal("setColorFilter"),
270 value: DeviceColorFilter,
271 }),
272 Schema.Struct({
273 ...DeviceTarget,
274 type: Schema.Literal("setOrientation"),
275 value: DeviceOrientation,
276 }),
277 Schema.Struct({
278 ...DeviceTarget,
279 type: Schema.Literal("setLocation"),
280 latitude: Schema.Number.check(Schema.isBetween({ minimum: -90, maximum: 90 })),
281 longitude: Schema.Number.check(Schema.isBetween({ minimum: -180, maximum: 180 })),
282 }),
283 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("clearLocation") }),
284 Schema.Struct({
285 ...DeviceTarget,
286 type: Schema.Literal("setPermission"),
287 appId: TrimmedNonEmptyString,
288 permission: DevicePermission,
289 decision: Schema.Literals(["grant", "revoke", "reset"]),
290 }),
291 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("openUrl"), url: TrimmedNonEmptyString }),
292 Schema.Struct({
293 ...DeviceTarget,
294 type: Schema.Literal("launchApp"),
295 appId: TrimmedNonEmptyString,
296 }),
297 Schema.Struct({
298 ...DeviceTarget,
299 type: Schema.Literal("terminateApp"),
300 appId: TrimmedNonEmptyString,
301 }),
302 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("shake") }),
303 Schema.Struct({
304 ...DeviceTarget,
305 type: Schema.Literal("sendPush"),
306 appId: TrimmedNonEmptyString,
307 payload: Schema.Union([Schema.String, Schema.Record(Schema.String, Schema.Unknown)]),
308 }),
309]);
310export type DeviceActionInput = typeof DeviceActionInput.Type;
311export type DeviceActionType = DeviceActionInput["type"];
312
313export const DeviceDetailInput = Schema.Struct(DeviceTarget);
314export type DeviceDetailInput = typeof DeviceDetailInput.Type;
315
316export class DeviceHostUnavailableError extends Schema.TaggedError<DeviceHostUnavailableError>()(
317 "DeviceHostUnavailableError",
318 {
319 hostId: DeviceHostId,
320 reason: Schema.String,
321 cause: Schema.optional(Schema.Defect()),
322 },
323) {
324 override get message(): string {
325 return `Device host ${this.hostId} is unavailable: ${this.reason}`;
326 }
327}
328
329export class DevicePlatformUnavailableError extends Schema.TaggedError<DevicePlatformUnavailableError>()(
330 "DevicePlatformUnavailableError",
331 {
332 hostId: DeviceHostId,
333 platform: DevicePlatform,
334 reason: Schema.String,
335 },
336) {
337 override get message(): string {
338 return `${this.platform} devices are unavailable on host ${this.hostId}: ${this.reason}`;
339 }
340}
341
342export class DeviceNotFoundError extends Schema.TaggedError<DeviceNotFoundError>()(
343 "DeviceNotFoundError",
344 {
345 hostId: DeviceHostId,
346 deviceId: DeviceId,
347 },
348) {
349 override get message(): string {
350 return `Device ${this.deviceId} was not found on host ${this.hostId}.`;
351 }
352}
353
354export class DeviceBootError extends Schema.TaggedError<DeviceBootError>()("DeviceBootError", {
355 hostId: DeviceHostId,
356 deviceId: DeviceId,
357 reason: Schema.Literals(["disk_space", "timeout", "launch_failed"]),
358 diagnostics: Schema.optional(Schema.String.check(Schema.isMaxLength(2_000))),
359 cause: Schema.Defect(),
360}) {
361 override get message(): string {
362 const explanation = {
363 disk_space: "There is not enough free disk space on the environment server.",
364 timeout: "The device did not become ready in time.",
365 launch_failed:
366 "The simulator or emulator could not start. Check its configuration on the environment server.",
367 }[this.reason];
368 const summary = `Device ${this.deviceId} failed to boot: ${explanation}`;
369 return this.diagnostics ? `${summary}\n\n${this.diagnostics}` : summary;
370 }
371}
372
373export class DeviceOperationError extends Schema.TaggedError<DeviceOperationError>()(
374 "DeviceOperationError",
375 {
376 operation: Schema.String,
377 reason: Schema.Literals([
378 "command_failed",
379 "request_failed",
380 "invalid_payload",
381 "settings_failed",
382 "hub_rejected",
383 ]),
384 exitCode: Schema.optional(Schema.Number),
385 cause: Schema.Defect(),
386 },
387) {
388 override get message(): string {
389 const explanation = {
390 command_failed: `The device command failed${this.exitCode === undefined ? "" : ` (exit code ${this.exitCode})`}.`,
391 request_failed: "Could not communicate with device support. Try refreshing devices.",
392 invalid_payload: "The device request could not be encoded.",
393 settings_failed: "Could not read or save device settings.",
394 hub_rejected: "The device hub could not complete the request.",
395 }[this.reason];
396 return `Device ${this.operation} failed: ${explanation}`;
397 }
398}
399
400export class DeviceActionUnavailableError extends Schema.TaggedError<DeviceActionUnavailableError>()(
401 "DeviceActionUnavailableError",
402 {
403 operation: Schema.String,
404 platform: DevicePlatform,
405 reason: Schema.Literals(["unsupported", "helper_missing"]),
406 },
407) {
408 override get message(): string {
409 return this.reason === "helper_missing"
410 ? `Device ${this.operation} requires a helper missing from this install. Set up device support again.`
411 : `Device ${this.operation} is not supported on ${this.platform}.`;
412 }
413}
414
415export const DeviceError = Schema.Union([
416 DeviceHostUnavailableError,
417 DevicePlatformUnavailableError,
418 DeviceNotFoundError,
419 DeviceBootError,
420 DeviceOperationError,
421 DeviceActionUnavailableError,
422]);
423export type DeviceError = typeof DeviceError.Type;
424
425export const DeviceToolListResult = Schema.Struct({
426 hostStatuses: DeviceServiceState.fields.hostStatuses,
427 hosts: Schema.Array(DeviceHostSummary),
428 devices: Schema.Array(DeviceSummary),
429 open: Schema.Array(Schema.Struct({ hostId: DeviceHostId, deviceId: DeviceId })),
430});
431export type DeviceToolListResult = typeof DeviceToolListResult.Type;
432
433export const DeviceToolOpenInput = Schema.Struct({
434 deviceId: Schema.optional(
435 DeviceId.annotate({
436 description:
437 "Simulator udid or emulator serial from device_list. Omit to use the booted device for the platform, or the most recently used one.",
438 }),
439 ),
440 platform: Schema.optional(
441 DevicePlatform.annotate({
442 description: "Required when deviceId is omitted and both platforms are available.",
443 }),
444 ),
445 hostId: Schema.optional(
446 DeviceHostId.annotate({ description: "Device host from device_list. Defaults to local." }),
447 ),
448}).annotate({
449 description:
450 "Boots the device if needed, starts its live stream, and opens the Device panel so the user can watch. Returns how to drive it with the agent-device CLI.",
451});
452export type DeviceToolOpenInput = typeof DeviceToolOpenInput.Type;
453
454export const DeviceToolOpenResult = Schema.Struct({
455 device: DeviceSummary,
456 agentDevice: Schema.Struct({
457 command: Schema.String,
458 targetArgs: Schema.Array(Schema.String),
459 }),
460 quickStart: Schema.String,
461});
462export type DeviceToolOpenResult = typeof DeviceToolOpenResult.Type;
463
464export const DeviceToolTargetInput = Schema.Struct({
465 deviceId: Schema.optional(
466 DeviceId.annotate({
467 description:
468 "Device from device_list. Omit to use the device most recently opened in this thread.",
469 }),
470 ),
471 hostId: Schema.optional(DeviceHostId),
472});
473export type DeviceToolTargetInput = typeof DeviceToolTargetInput.Type;
474
475export const DeviceToolScreenshotResult = Schema.Struct({
476 device: DeviceSummary,
477 screenshot: Schema.Struct({
478 mimeType: Schema.Literal("image/png"),
479 data: Schema.String,
480 width: Schema.Int,
481 height: Schema.Int,
482 }),
483});
484export type DeviceToolScreenshotResult = typeof DeviceToolScreenshotResult.Type;
485
486export const DeviceToolCloseInput = Schema.Struct({
487 deviceId: Schema.optional(
488 DeviceId.annotate({
489 description: "Device to close. Omit to close every device in this thread.",
490 }),
491 ),
492 hostId: Schema.optional(DeviceHostId),
493 shutdown: Schema.optional(
494 Schema.Boolean.annotate({
495 description: "Also power the simulator or emulator off. Defaults to false.",
496 }),
497 ),
498});
499export type DeviceToolCloseInput = typeof DeviceToolCloseInput.Type;
500
501export class DeviceToolUnavailableError extends Schema.TaggedError<DeviceToolUnavailableError>()(
502 "DeviceToolUnavailableError",
503 {
504 reason: Schema.String,
505 cause: Schema.optional(Schema.Defect()),
506 },
507) {
508 override get message(): string {
509 return this.reason;
510 }
511}
512
513export const DeviceToolError = Schema.Union([
514 DeviceToolUnavailableError,
515 DeviceHostUnavailableError,
516 DevicePlatformUnavailableError,
517 DeviceNotFoundError,
518 DeviceBootError,
519 DeviceOperationError,
520 DeviceActionUnavailableError,
521]);
522export type DeviceToolError = typeof DeviceToolError.Type;
523
524export function deviceToolInstallMessage(name: string, tool: DeviceToolVersion | undefined) {
525 if (!tool) return `Installing ${name}…`;
526 const previous =
527 tool.runningVersion ??
528 [...tool.installedVersions].sort((a, b) => a.localeCompare(b, "en", { numeric: true })).at(-1);
529 return previous && !tool.installedVersions.includes(tool.requiredVersion)
530 ? `Updating ${name} from ${previous} to ${tool.requiredVersion}…`
531 : `Installing ${name} ${tool.requiredVersion}…`;
532}