Open History to see the last commit
1/**
2 * A device host is a machine with simulators or emulators on it. The service
3 * layer only ever talks to this interface, so a future SSH or cloud host slots
4 * in beside `LocalDeviceHost` without touching discovery, the proxy, or the
5 * MCP tools.
6 *
7 * Every ready host presents a loopback origin where expo-device-hub answers.
8 * Hosts add an agent-device daemon endpoint only after agent access is granted.
9 * For the local host both run on this machine; a remote host would forward
10 * them here.
11 */
12import type {
13 DeviceHostId,
14 DeviceHostSummary,
15 DevicePlatform,
16 DevicePlatformAvailability,
17} from "@t3tools/contracts";
18import * as Context from "effect/Context";
19import * as Effect from "effect/Effect";
20import * as Schema from "effect/Schema";
21
22export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("DeviceHostError", {
23 hostId: Schema.String,
24 step: Schema.String,
25 cause: Schema.Defect(),
26}) {
27 override get message(): string {
28 return `Device host ${this.hostId} failed while ${this.step}.`;
29 }
30}
31
32export class DeviceHostTimeoutError extends Schema.TaggedError<DeviceHostTimeoutError>()(
33 "DeviceHostTimeoutError",
34 { hostId: Schema.String, timeoutMs: Schema.Number },
35) {
36 override get message(): string {
37 return `Device host ${this.hostId} did not start agent tools within ${this.timeoutMs} ms.`;
38 }
39}
40
41export interface DeviceHubEndpoint {
42 /** Loopback origin of expo-device-hub, e.g. `http://127.0.0.1:3400`. */
43 readonly origin: string;
44}
45
46export interface AgentDeviceEndpoint {
47 readonly baseUrl: string;
48 readonly token: string;
49 /** Host-local path of the agent-device entry script. The provider uses a separate local CLI install. */
50 readonly entryPath: string;
51}
52
53export interface DeviceHostReady {
54 readonly nodePath: string;
55 readonly hub: DeviceHubEndpoint;
56 /**
57 * Runs a host command (`xcrun`, `adb`, or a helper bundled with the hub)
58 * where the devices live. On the local host this is a plain spawn; a
59 * remote host would run it over its transport.
60 */
61 readonly run: (
62 command: string,
63 args: ReadonlyArray<string>,
64 options?: { readonly timeoutMs?: number; readonly stdin?: string },
65 ) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
66 /** Absolute paths of helper binaries vendored with the hub, when present. */
67 readonly helpers: {
68 readonly serveSimAxSettings: string | null;
69 readonly serveSimCli: string | null;
70 };
71}
72
73export interface DeviceHostAgentReady extends DeviceHostReady {
74 readonly agentDevice: AgentDeviceEndpoint;
75}
76
77export class DeviceHost extends Context.Service<
78 DeviceHost,
79 {
80 readonly id: DeviceHostId;
81 readonly summary: Effect.Effect<DeviceHostSummary>;
82 readonly platformAvailability: (
83 platform: DevicePlatform,
84 ) => Effect.Effect<DevicePlatformAvailability>;
85 /**
86 * Installs tools on first use and starts the helper processes. Idempotent:
87 * concurrent callers share one start, and a ready host returns immediately.
88 */
89 readonly ensureReady: (
90 onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
91 ) => Effect.Effect<DeviceHostReady, DeviceHostError>;
92 /** Installs and starts agent-device after the user grants agent access. */
93 readonly ensureAgentReady: (
94 onPhase: (phase: "installing" | "starting") => Effect.Effect<void>,
95 ) => Effect.Effect<DeviceHostAgentReady, DeviceHostError | DeviceHostTimeoutError>;
96 /** Current endpoints when already running, without starting anything. */
97 readonly current: Effect.Effect<DeviceHostReady | null>;
98 /** Stops only agent-device. Manual viewing through the hub stays available. */
99 readonly stopAgent: Effect.Effect<void>;
100 /** Stops helpers. Devices themselves keep running; the user owns those. */
101 readonly stop: Effect.Effect<void>;
102 }
103>()("t3/device/DeviceHost") {}