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

DeviceHost.ts

108 lines4.1 KB

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";
21import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
22​
23export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("DeviceHostError", {
24 hostId: Schema.String,
25 step: Schema.String,
26 cause: Schema.Defect(),
27}) {
28 override get message(): string {
29 return `Device host ${this.hostId} failed while ${this.step}.`;
30 }
32​
33export class DeviceHostTimeoutError extends Schema.TaggedError<DeviceHostTimeoutError>()(
34 "DeviceHostTimeoutError",
35 { hostId: Schema.String, timeoutMs: Schema.Number },
36) {
37 override get message(): string {
38 return `Device host ${this.hostId} did not start agent tools within ${this.timeoutMs} ms.`;
39 }
41​
42export interface DeviceHubEndpoint {
43 /** Loopback origin of expo-device-hub, e.g. `http://127.0.0.1:3400`. */
44 readonly origin: string;
46​
47export interface AgentDeviceEndpoint {
48 readonly baseUrl: string;
49 readonly token: string;
50 /** Host-local path of the agent-device entry script. The provider uses a separate local CLI install. */
51 readonly entryPath: string;
53​
54export interface DeviceHostReady {
55 readonly nodePath: string;
56 readonly hub: DeviceHubEndpoint;
57 /**
58 * Runs a host command (`xcrun`, `adb`, or a helper bundled with the hub)
59 * where the devices live. On the local host this is a plain spawn; a
60 * remote host would run it over its transport.
61 */
62 readonly run: (
63 command: string,
64 args: ReadonlyArray<string>,
65 options?: { readonly timeoutMs?: number; readonly stdin?: string },
66 ) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
67 /** Absolute paths of helper binaries vendored with the hub, when present. */
68 readonly helpers: {
69 readonly serveSimAxSettings: string | null;
70 readonly serveSimCli: string | null;
71 };
73​
74export interface DeviceHostAgentReady extends DeviceHostReady {
75 readonly agentDevice: AgentDeviceEndpoint;
77​
78export class DeviceHost extends Context.Service<
79 DeviceHost,
80 {
81 readonly id: DeviceHostId;
82 readonly summary: Effect.Effect<DeviceHostSummary>;
83 readonly inspect?: Effect.Effect<DeviceHostSummary, DeviceHostError>;
84 readonly platformAvailability: (
85 platform: DevicePlatform,
86 ) => Effect.Effect<DevicePlatformAvailability>;
87 /**
88 * Installs tools on first use and starts the helper processes. Idempotent:
89 * concurrent callers share one start, and a ready host returns immediately.
90 */
91 readonly ensureReady: (
92 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
93 ) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
94 /** Installs and starts agent-device after the user grants agent access. */
95 readonly ensureAgentReady: (
96 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
97 ) => Effect.Effect<
98 DeviceHostAgentReady,
99 DeviceHostError | DeviceHostTimeoutError | NodeRuntimeUnavailableError
100 >;
101 /** Current endpoints when already running, without starting anything. */
102 readonly current: Effect.Effect<DeviceHostReady | null>;
103 /** Stops only agent-device. Manual viewing through the hub stays available. */
104 readonly stopAgent: Effect.Effect<void>;
105 /** Stops helpers. Devices themselves keep running; the user owns those. */
106 readonly stop: Effect.Effect<void>;
107 }
108>()("t3/device/DeviceHost") {}