Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

device.ts

664 lines22.3 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import * as SchemaTransformation from "effect/SchemaTransformation";
4​
5import { ThreadId, TrimmedNonEmptyString, TrimmedString } from "./baseSchemas.ts";
6​
7export const DevicePlatform = Schema.Literals(["ios", "android"]);
8export type DevicePlatform = typeof DevicePlatform.Type;
9​
10export const DeviceHostId = TrimmedNonEmptyString.check(Schema.isMaxLength(128));
11export type DeviceHostId = typeof DeviceHostId.Type;
12​
13export const LOCAL_DEVICE_HOST_ID = "local" as DeviceHostId;
14export const LOCAL_DEVICE_HOST_LABEL = "This machine";
15​
16const ConfiguredDeviceHostId = DeviceHostId.check(
17 Schema.isPattern(/^[a-zA-Z0-9][a-zA-Z0-9_-]*$/),
18 Schema.makeFilter((id) => id !== "local" || "The local host id is reserved."),
19);
20​
21export const SshDeviceHostConfig = Schema.Struct({
22 // Hosts saved before EAS hosts existed carry no kind.
23 kind: Schema.Literal("ssh").pipe(Schema.withDecodingDefaultKey(Effect.succeed("ssh" as const))),
24 id: ConfiguredDeviceHostId,
25 label: TrimmedNonEmptyString,
26 target: TrimmedNonEmptyString.check(Schema.isPattern(/^[^\s-][^\s]*$/)),
27 identityFile: Schema.optional(TrimmedNonEmptyString),
28 port: Schema.optional(Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 65535 }))),
29});
30export type SshDeviceHostConfig = typeof SshDeviceHostConfig.Type;
31​
32/**
33 * Cloud simulators from EAS Simulator. Sessions bill the Expo account until
34 * they stop, so every one is bounded. The Expo credential is not stored here.
35 */
36export const EasDeviceHostConfig = Schema.Struct({
37 kind: Schema.Literal("eas"),
38 id: ConfiguredDeviceHostId,
39 label: TrimmedNonEmptyString,
40 /** EAS project that sessions run under, when the project's app config does not name one. */
41 appId: Schema.optional(TrimmedNonEmptyString),
42 /**
43 * Expo access token, kept in the secret store. When empty the server uses
44 * EXPO_TOKEN, then the `eas login` session. This is `secretSetting` from
45 * settings.ts, which imports this module.
46 */
47 accessToken: TrimmedString.pipe(
48 Schema.withDecodingDefaultKey(Effect.succeed("")),
49 Schema.annotateKey({ settingsSecret: true }),
50 ),
51 /** EAS allows at most 40 minutes on free accounts and 115 on paid ones. */
52 maxSessionMinutes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 115 })).pipe(
53 Schema.withDecodingDefaultKey(Effect.succeed(30)),
54 ),
55 idleMinutes: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 60 })).pipe(
56 Schema.withDecodingDefaultKey(Effect.succeed(10)),
57 ),
58 maxConcurrentSessions: Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 20 })).pipe(
59 Schema.withDecodingDefaultKey(Effect.succeed(2)),
60 ),
61}).check(
62 // EAS requires the idle limit to be smaller than the session limit.
63 Schema.makeFilter(
64 (host: { readonly idleMinutes: number; readonly maxSessionMinutes: number }) =>
65 host.idleMinutes < host.maxSessionMinutes ||
66 "Idle minutes must be smaller than session minutes.",
67 ),
68);
69export type EasDeviceHostConfig = typeof EasDeviceHostConfig.Type;
70​
71export type DeviceHostConfig = SshDeviceHostConfig | EasDeviceHostConfig;
72​
73const uniqueHostIds = Schema.makeFilter(
74 (hosts: ReadonlyArray<{ readonly id: string }>) =>
75 new Set(hosts.map((host) => host.id)).size === hosts.length ||
76 "Device host ids must be unique.",
77);
78​
79export const SshDeviceHostConfigs = Schema.Array(SshDeviceHostConfig).check(uniqueHostIds);
80​
81/**
82 * Kept apart from the SSH hosts in settings: servers and clients from before
83 * EAS hosts decode `deviceHosts` as SSH hosts and would reject the whole file
84 * or snapshot over one EAS entry.
85 */
86export const EasDeviceHostConfigs = Schema.Array(EasDeviceHostConfig).check(uniqueHostIds);
87​
88export const DeviceEasAccountInput = Schema.Struct({
89 /** A saved EAS host whose stored token is checked when no new token is given. */
90 hostId: Schema.optional(DeviceHostId),
91 accessToken: Schema.optional(TrimmedString),
92});
93export type DeviceEasAccountInput = typeof DeviceEasAccountInput.Type;
94​
95export const DeviceEasAccount = Schema.Struct({
96 signedInAs: Schema.String,
97 credentialSource: Schema.Literals(["settings", "env", "eas-login"]),
98 apps: Schema.Array(Schema.Struct({ id: Schema.String, fullName: Schema.String })),
99});
100export type DeviceEasAccount = typeof DeviceEasAccount.Type;
101​
102/** Checking an Expo credential failed; no device host is involved yet. */
103export class DeviceEasAccountError extends Schema.TaggedError<DeviceEasAccountError>()(
104 "DeviceEasAccountError",
105 // No cause: this error reaches the browser, and the reason already says what Expo answered.
106 { reason: Schema.String },
107) {
108 override get message(): string {
109 return this.reason;
110 }
112​
113export const DeviceId = TrimmedNonEmptyString.check(Schema.isMaxLength(256));
114export type DeviceId = typeof DeviceId.Type;
115​
116export const DeviceSummary = Schema.Struct({
117 hostId: DeviceHostId,
118 id: DeviceId,
119 platform: DevicePlatform,
120 name: TrimmedNonEmptyString,
121 version: Schema.String,
122 booted: Schema.Boolean,
123 physical: Schema.Boolean,
124});
125export type DeviceSummary = typeof DeviceSummary.Type;
126​
127export const DevicePlatformAvailability = Schema.Struct({
128 platform: DevicePlatform,
129 available: Schema.Boolean,
130 reason: Schema.optional(Schema.String),
131});
132export type DevicePlatformAvailability = typeof DevicePlatformAvailability.Type;
133​
134export const DeviceToolVersion = Schema.Struct({
135 requiredVersion: Schema.String,
136 installedVersions: Schema.Array(Schema.String),
137 runningVersion: Schema.NullOr(Schema.String),
138});
139export type DeviceToolVersion = typeof DeviceToolVersion.Type;
140​
141export const DeviceToolVersions = Schema.Struct({
142 hub: DeviceToolVersion,
143 agent: DeviceToolVersion,
144});
145export type DeviceToolVersions = typeof DeviceToolVersions.Type;
146​
147const deviceHostSummaryFields = {
148 id: DeviceHostId,
149 label: TrimmedNonEmptyString,
150 platforms: Schema.Array(DevicePlatformAvailability),
151 tools: Schema.optional(DeviceToolVersions),
152 toolInspectionError: Schema.optional(Schema.String),
153 hubInstalled: Schema.Boolean,
154 agentDeviceInstalled: Schema.Boolean,
155};
156const MachineDeviceHostSummary = Schema.Struct({
157 ...deviceHostSummaryFields,
158 kind: Schema.Literals(["local", "ssh"]),
159});
160const EasDeviceHostSummary = Schema.Struct({
161 ...deviceHostSummaryFields,
162 kind: Schema.Literal("eas"),
163});
164export const DeviceHostSummary = Schema.Union([MachineDeviceHostSummary, EasDeviceHostSummary]);
165export type DeviceHostSummary = typeof DeviceHostSummary.Type;
166​
167export const DeviceHostStatus = Schema.Literals([
168 "disabled",
169 "idle",
170 "installing",
171 "starting",
172 "ready",
173 "failed",
174]);
175export type DeviceHostStatus = typeof DeviceHostStatus.Type;
176​
177export const DeviceSession = Schema.Struct({
178 threadId: ThreadId,
179 hostId: DeviceHostId,
180 deviceId: DeviceId,
181 platform: DevicePlatform,
182 openedAt: Schema.String,
183});
184export type DeviceSession = typeof DeviceSession.Type;
185​
186export const DeviceHostStatuses = Schema.Record(
187 DeviceHostId,
188 Schema.Struct({
189 status: DeviceHostStatus,
190 detail: Schema.optional(Schema.String),
191 androidDiscoveryIncomplete: Schema.optional(Schema.Boolean),
192 }),
193);
194​
195const deviceServiceStateFields = {
196 supportsHostRetry: Schema.optional(Schema.Boolean),
197 supportsToolUpdate: Schema.optional(Schema.Boolean),
198 supportsToolInspection: Schema.optional(Schema.Boolean),
199 /** Servers from before EAS hosts ignore `easDeviceHosts`, so clients offer them only when set. */
200 supportsEasHosts: Schema.optional(Schema.Boolean),
201 hostStatus: DeviceHostStatus,
202 hostStatusDetail: Schema.optional(Schema.String),
203 hostStatuses: DeviceHostStatuses,
204 devices: Schema.Array(DeviceSummary),
205 sessions: Schema.Array(DeviceSession),
206 bootingDevices: Schema.optional(
207 Schema.Array(Schema.Struct({ ...DeviceSummary.fields, threadId: ThreadId })),
208 ),
209 onboardingCompleted: Schema.Boolean,
210 agentAccessEnabled: Schema.Boolean,
211 hubBasePath: Schema.String,
212 revision: Schema.Int,
213};
214​
215/**
216 * EAS host summaries travel as `easHosts`: clients from before EAS hosts
217 * decode `hosts` with a `local | ssh` kind and would reject the whole state.
218 * Decoded, every host is in `hosts`.
219 */
220const DeviceServiceStateWire = Schema.Struct({
221 ...deviceServiceStateFields,
222 hosts: Schema.Array(MachineDeviceHostSummary),
223 easHosts: Schema.optional(Schema.Array(EasDeviceHostSummary)),
224});
225const DeviceServiceStateDecoded = Schema.Struct({
226 ...deviceServiceStateFields,
227 hosts: Schema.Array(DeviceHostSummary),
228});
229​
230export const DeviceServiceState = DeviceServiceStateWire.pipe(
231 Schema.decodeTo(
232 Schema.toType(DeviceServiceStateDecoded),
233 SchemaTransformation.transform<
234 typeof DeviceServiceStateDecoded.Type,
235 typeof DeviceServiceStateWire.Type
236 >({
237 decode: ({ easHosts, ...state }) => ({
238 ...state,
239 hosts: [...state.hosts, ...(easHosts ?? [])],
240 }),
241 encode: (state) => ({
242 ...state,
243 hosts: state.hosts.filter(
244 (host): host is typeof MachineDeviceHostSummary.Type => host.kind !== "eas",
245 ),
246 easHosts: state.hosts.filter(
247 (host): host is typeof EasDeviceHostSummary.Type => host.kind === "eas",
248 ),
249 }),
250 }),
251 ),
252);
253export type DeviceServiceState = typeof DeviceServiceState.Type;
254​
255export const DeviceListInput = Schema.Struct({
256 updateTool: Schema.optional(Schema.Literals(["hub", "agent"])),
257 inspectOnly: Schema.optional(Schema.Boolean),
258 retryHostId: Schema.optional(DeviceHostId),
259});
260export type DeviceListInput = typeof DeviceListInput.Type;
261​
262export const DeviceConfigureInput = Schema.Struct({
263 enabled: Schema.optional(Schema.Boolean),
264 agentAccessEnabled: Schema.optional(Schema.Boolean),
265 onboardingCompleted: Schema.optional(Schema.Boolean),
266});
267export type DeviceConfigureInput = typeof DeviceConfigureInput.Type;
268​
269export const DeviceOpenInput = Schema.Struct({
270 threadId: ThreadId,
271 hostId: Schema.optional(DeviceHostId),
272 deviceId: DeviceId,
273 platform: DevicePlatform,
274 /** Boot the simulator or emulator when it is not running. Defaults to true. */
275 boot: Schema.optional(Schema.Boolean),
276});
277export type DeviceOpenInput = typeof DeviceOpenInput.Type;
278​
279export const DeviceCloseInput = Schema.Struct({
280 hostId: Schema.optional(DeviceHostId),
281 threadId: ThreadId,
282 /** Omit to close every device session for the thread. */
283 deviceId: Schema.optional(DeviceId),
284 /** Also shut the simulator or emulator down. Defaults to false. */
285 shutdown: Schema.optional(Schema.Boolean),
286});
287export type DeviceCloseInput = typeof DeviceCloseInput.Type;
288​
289export const DeviceShutdownInput = Schema.Struct({
290 hostId: Schema.optional(DeviceHostId),
291 deviceId: DeviceId,
292 platform: DevicePlatform,
293});
294export type DeviceShutdownInput = typeof DeviceShutdownInput.Type;
295​
296export const DeviceAppearance = Schema.Literals(["light", "dark"]);
297export type DeviceAppearance = typeof DeviceAppearance.Type;
298​
299export const DeviceTextSize = Schema.Literals(["small", "default", "large", "extra-large"]);
300export type DeviceTextSize = typeof DeviceTextSize.Type;
301​
302export const DeviceColorFilter = Schema.Literals([
303 "none",
304 "grayscale",
305 "red-green",
306 "green-red",
307 "blue-yellow",
308]);
309export type DeviceColorFilter = typeof DeviceColorFilter.Type;
310​
311export const DeviceOrientation = Schema.Literals([
312 "portrait",
313 "landscape_left",
314 "portrait_upside_down",
315 "landscape_right",
316]);
317export type DeviceOrientation = typeof DeviceOrientation.Type;
318​
319export const DeviceSettings = Schema.Struct({
320 appearance: Schema.optional(DeviceAppearance),
321 textSize: Schema.optional(DeviceTextSize),
322 reduceMotion: Schema.optional(Schema.Boolean),
323 increaseContrast: Schema.optional(Schema.Boolean),
324 reduceTransparency: Schema.optional(Schema.Boolean),
325 showBorders: Schema.optional(Schema.Boolean),
326 voiceOver: Schema.optional(Schema.Boolean),
327 liquidGlass: Schema.optional(Schema.Literals(["clear", "tinted"])),
328 colorFilter: Schema.optional(DeviceColorFilter),
329 networkEnabled: Schema.optional(Schema.Boolean),
330 location: Schema.optional(
331 Schema.NullOr(Schema.Struct({ latitude: Schema.Number, longitude: Schema.Number })),
332 ),
333});
334export type DeviceSettings = typeof DeviceSettings.Type;
335​
336export const DeviceForegroundApp = Schema.Struct({
337 id: Schema.String,
338 name: Schema.optional(Schema.String),
339 version: Schema.optional(Schema.String),
340});
341export type DeviceForegroundApp = typeof DeviceForegroundApp.Type;
342​
343export const DeviceDetail = Schema.Struct({
344 hostId: DeviceHostId,
345 deviceId: DeviceId,
346 settings: DeviceSettings,
347 foregroundApp: Schema.NullOr(DeviceForegroundApp),
348 readAt: Schema.String,
349});
350export type DeviceDetail = typeof DeviceDetail.Type;
351​
352export const DevicePermission = Schema.Literals([
353 "camera",
354 "microphone",
355 "photos",
356 "contacts",
357 "calendar",
358 "reminders",
359 "location",
360 "notifications",
361 "motion",
362 "media-library",
363 "faceid",
364]);
365export type DevicePermission = typeof DevicePermission.Type;
366​
367const DeviceTarget = {
368 hostId: Schema.optional(DeviceHostId),
369 deviceId: DeviceId,
370};
371​
372export const DeviceActionInput = Schema.Union([
373 Schema.Struct({
374 ...DeviceTarget,
375 type: Schema.Literal("setAppearance"),
376 value: DeviceAppearance,
377 }),
378 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("setTextSize"), value: DeviceTextSize }),
379 Schema.Struct({
380 ...DeviceTarget,
381 type: Schema.Literal("setToggle"),
382 setting: Schema.Literals([
383 "reduceMotion",
384 "increaseContrast",
385 "reduceTransparency",
386 "showBorders",
387 "voiceOver",
388 "networkEnabled",
389 ]),
390 value: Schema.Boolean,
391 }),
392 Schema.Struct({
393 ...DeviceTarget,
394 type: Schema.Literal("setLiquidGlass"),
395 value: Schema.Literals(["clear", "tinted"]),
396 }),
397 Schema.Struct({
398 ...DeviceTarget,
399 type: Schema.Literal("setColorFilter"),
400 value: DeviceColorFilter,
401 }),
402 Schema.Struct({
403 ...DeviceTarget,
404 type: Schema.Literal("setOrientation"),
405 value: DeviceOrientation,
406 }),
407 Schema.Struct({
408 ...DeviceTarget,
409 type: Schema.Literal("setLocation"),
410 latitude: Schema.Number.check(Schema.isBetween({ minimum: -90, maximum: 90 })),
411 longitude: Schema.Number.check(Schema.isBetween({ minimum: -180, maximum: 180 })),
412 }),
413 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("clearLocation") }),
414 Schema.Struct({
415 ...DeviceTarget,
416 type: Schema.Literal("setPermission"),
417 appId: TrimmedNonEmptyString,
418 permission: DevicePermission,
419 decision: Schema.Literals(["grant", "revoke", "reset"]),
420 }),
421 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("openUrl"), url: TrimmedNonEmptyString }),
422 Schema.Struct({
423 ...DeviceTarget,
424 type: Schema.Literal("launchApp"),
425 appId: TrimmedNonEmptyString,
426 }),
427 Schema.Struct({
428 ...DeviceTarget,
429 type: Schema.Literal("terminateApp"),
430 appId: TrimmedNonEmptyString,
431 }),
432 Schema.Struct({ ...DeviceTarget, type: Schema.Literal("shake") }),
433 Schema.Struct({
434 ...DeviceTarget,
435 type: Schema.Literal("sendPush"),
436 appId: TrimmedNonEmptyString,
437 payload: Schema.Union([Schema.String, Schema.Record(Schema.String, Schema.Unknown)]),
438 }),
439]);
440export type DeviceActionInput = typeof DeviceActionInput.Type;
441export type DeviceActionType = DeviceActionInput["type"];
442​
443export const DeviceDetailInput = Schema.Struct(DeviceTarget);
444export type DeviceDetailInput = typeof DeviceDetailInput.Type;
445​
446export class DeviceHostUnavailableError extends Schema.TaggedError<DeviceHostUnavailableError>()(
447 "DeviceHostUnavailableError",
448 {
449 hostId: DeviceHostId,
450 /** The host's name in Settings, which the message shows instead of its id. */
451 hostLabel: Schema.optional(Schema.String),
452 reason: Schema.String,
453 cause: Schema.optional(Schema.Defect()),
454 },
455) {
456 override get message(): string {
457 return `${this.hostLabel ?? `Device host ${this.hostId}`} is unavailable: ${this.reason}`;
458 }
460​
461export class DevicePlatformUnavailableError extends Schema.TaggedError<DevicePlatformUnavailableError>()(
462 "DevicePlatformUnavailableError",
463 {
464 hostId: DeviceHostId,
465 platform: DevicePlatform,
466 reason: Schema.String,
467 },
468) {
469 override get message(): string {
470 return `${this.platform} devices are unavailable on host ${this.hostId}: ${this.reason}`;
471 }
473​
474export class DeviceNotFoundError extends Schema.TaggedError<DeviceNotFoundError>()(
475 "DeviceNotFoundError",
476 {
477 hostId: DeviceHostId,
478 deviceId: DeviceId,
479 },
480) {
481 override get message(): string {
482 return `Device ${this.deviceId} was not found on host ${this.hostId}.`;
483 }
485​
486export class DeviceBootError extends Schema.TaggedError<DeviceBootError>()("DeviceBootError", {
487 hostId: DeviceHostId,
488 deviceId: DeviceId,
489 reason: Schema.Literals(["disk_space", "timeout", "launch_failed"]),
490 diagnostics: Schema.optional(Schema.String.check(Schema.isMaxLength(2_000))),
491 cause: Schema.Defect(),
492}) {
493 override get message(): string {
494 const explanation = {
495 disk_space: "There is not enough free disk space on the environment server.",
496 timeout: "The device did not become ready in time.",
497 launch_failed:
498 "The simulator or emulator could not start. Check its configuration on the environment server.",
499 }[this.reason];
500 const summary = `Device ${this.deviceId} failed to boot: ${explanation}`;
501 return this.diagnostics ? `${summary}\n\n${this.diagnostics}` : summary;
502 }
504​
505export class DeviceOperationError extends Schema.TaggedError<DeviceOperationError>()(
506 "DeviceOperationError",
507 {
508 operation: Schema.String,
509 reason: Schema.Literals([
510 "command_failed",
511 "request_failed",
512 "invalid_payload",
513 "settings_failed",
514 "hub_rejected",
515 ]),
516 exitCode: Schema.optional(Schema.Number),
517 cause: Schema.Defect(),
518 },
519) {
520 override get message(): string {
521 const explanation = {
522 command_failed: `The device command failed${this.exitCode === undefined ? "" : ` (exit code ${this.exitCode})`}.`,
523 request_failed: "Could not communicate with device support. Try refreshing devices.",
524 invalid_payload: "The device request could not be encoded.",
525 settings_failed: "Could not read or save device settings.",
526 hub_rejected: "The device hub could not complete the request.",
527 }[this.reason];
528 return `Device ${this.operation} failed: ${explanation}`;
529 }
531​
532export class DeviceActionUnavailableError extends Schema.TaggedError<DeviceActionUnavailableError>()(
533 "DeviceActionUnavailableError",
534 {
535 operation: Schema.String,
536 platform: DevicePlatform,
537 reason: Schema.Literals(["unsupported", "helper_missing"]),
538 },
539) {
540 override get message(): string {
541 return this.reason === "helper_missing"
542 ? `Device ${this.operation} requires a helper missing from this install. Set up device support again.`
543 : `Device ${this.operation} is not supported on ${this.platform}.`;
544 }
546​
547export const DeviceError = Schema.Union([
548 DeviceHostUnavailableError,
549 DevicePlatformUnavailableError,
550 DeviceNotFoundError,
551 DeviceBootError,
552 DeviceOperationError,
553 DeviceActionUnavailableError,
554]);
555export type DeviceError = typeof DeviceError.Type;
556​
557export const DeviceToolListResult = Schema.Struct({
558 hostStatuses: DeviceHostStatuses,
559 hosts: Schema.Array(DeviceHostSummary),
560 devices: Schema.Array(DeviceSummary),
561 open: Schema.Array(Schema.Struct({ hostId: DeviceHostId, deviceId: DeviceId })),
562});
563export type DeviceToolListResult = typeof DeviceToolListResult.Type;
564​
565export const DeviceToolOpenInput = Schema.Struct({
566 deviceId: Schema.optional(
567 DeviceId.annotate({
568 description:
569 "Simulator udid or emulator serial from device_list. Omit to use the booted device for the platform, or the most recently used one.",
570 }),
571 ),
572 platform: Schema.optional(
573 DevicePlatform.annotate({
574 description: "Required when deviceId is omitted and both platforms are available.",
575 }),
576 ),
577 hostId: Schema.optional(
578 DeviceHostId.annotate({ description: "Device host from device_list. Defaults to local." }),
579 ),
580}).annotate({
581 description:
582 "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.",
583});
584export type DeviceToolOpenInput = typeof DeviceToolOpenInput.Type;
585​
586export const DeviceToolOpenResult = Schema.Struct({
587 device: DeviceSummary,
588 agentDevice: Schema.Struct({
589 command: Schema.String,
590 targetArgs: Schema.Array(Schema.String),
591 }),
592 quickStart: Schema.String,
593});
594export type DeviceToolOpenResult = typeof DeviceToolOpenResult.Type;
595​
596export const DeviceToolTargetInput = Schema.Struct({
597 deviceId: Schema.optional(
598 DeviceId.annotate({
599 description:
600 "Device from device_list. Omit to use the device most recently opened in this thread.",
601 }),
602 ),
603 hostId: Schema.optional(DeviceHostId),
604});
605export type DeviceToolTargetInput = typeof DeviceToolTargetInput.Type;
606​
607export const DeviceToolScreenshotResult = Schema.Struct({
608 device: DeviceSummary,
609 screenshot: Schema.Struct({
610 mimeType: Schema.Literal("image/png"),
611 data: Schema.String,
612 width: Schema.Int,
613 height: Schema.Int,
614 }),
615});
616export type DeviceToolScreenshotResult = typeof DeviceToolScreenshotResult.Type;
617​
618export const DeviceToolCloseInput = Schema.Struct({
619 deviceId: Schema.optional(
620 DeviceId.annotate({
621 description: "Device to close. Omit to close every device in this thread.",
622 }),
623 ),
624 hostId: Schema.optional(DeviceHostId),
625 shutdown: Schema.optional(
626 Schema.Boolean.annotate({
627 description: "Also power the simulator or emulator off. Defaults to false.",
628 }),
629 ),
630});
631export type DeviceToolCloseInput = typeof DeviceToolCloseInput.Type;
632​
633export class DeviceToolUnavailableError extends Schema.TaggedError<DeviceToolUnavailableError>()(
634 "DeviceToolUnavailableError",
635 {
636 reason: Schema.String,
637 cause: Schema.optional(Schema.Defect()),
638 },
639) {
640 override get message(): string {
641 return this.reason;
642 }
644​
645export const DeviceToolError = Schema.Union([
646 DeviceToolUnavailableError,
647 DeviceHostUnavailableError,
648 DevicePlatformUnavailableError,
649 DeviceNotFoundError,
650 DeviceBootError,
651 DeviceOperationError,
652 DeviceActionUnavailableError,
653]);
654export type DeviceToolError = typeof DeviceToolError.Type;
655​
656export function deviceToolInstallMessage(name: string, tool: DeviceToolVersion | undefined) {
657 if (!tool) return `Installing ${name}…`;
658 const previous =
659 tool.runningVersion ??
660 [...tool.installedVersions].sort((a, b) => a.localeCompare(b, "en", { numeric: true })).at(-1);
661 return previous && !tool.installedVersions.includes(tool.requiredVersion)
662 ? `Updating ${name} from ${previous} to ${tool.requiredVersion}…`
663 : `Installing ${name} ${tool.requiredVersion}…`;