Open History to see the last commit
1import type {
2 DeviceBootError,
3 DeviceHostId,
4 DeviceHostUnavailableError,
5 DeviceHostSummary,
6 DeviceOperationError,
7 DevicePlatform,
8 DevicePlatformAvailability,
9 DeviceSummary,
10} from "@t3tools/contracts";
11import * as Context from "effect/Context";
12import * as Effect from "effect/Effect";
13import * as Schema from "effect/Schema";
14import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
15
16export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("DeviceHostError", {
17 hostId: Schema.String,
18 step: Schema.String,
19 cause: Schema.Defect(),
20}) {
21 override get message(): string {
22 return `Device host ${this.hostId} failed while ${this.step}.`;
23 }
24}
25
26export class DeviceHostTimeoutError extends Schema.TaggedError<DeviceHostTimeoutError>()(
27 "DeviceHostTimeoutError",
28 { hostId: Schema.String, timeoutMs: Schema.Number },
29) {
30 override get message(): string {
31 return `Device host ${this.hostId} did not start agent tools within ${this.timeoutMs} ms.`;
32 }
33}
34
35export class AgentDeviceStartError extends Schema.TaggedError<AgentDeviceStartError>()(
36 "AgentDeviceStartError",
37 {
38 hostId: Schema.String,
39 stateDir: Schema.String,
40 exitCode: Schema.NullOr(Schema.Int),
41 reason: Schema.Literals([
42 "daemon_lock_not_directory",
43 "daemon_registration_unproven",
44 "daemon_retirement_unconfirmed",
45 "command_failed",
46 ]),
47 cause: Schema.Defect(),
48 },
49) {
50 override get message(): string {
51 const exit = this.exitCode === null ? "" : ` (exit code ${this.exitCode})`;
52 const detail =
53 this.reason === "daemon_lock_not_directory"
54 ? "Daemon registration ownership could not be verified: daemon.lock is not a directory."
55 : this.reason === "daemon_registration_unproven"
56 ? "Daemon registration ownership could not be verified."
57 : this.reason === "daemon_retirement_unconfirmed"
58 ? "Daemon replacement could not be confirmed."
59 : "The AgentDevice startup command failed.";
60 const recovery =
61 this.reason === "command_failed"
62 ? `Inspect daemon diagnostics in ${this.stateDir}.`
63 : `Inspect daemon.lock and daemon diagnostics in ${this.stateDir}. Stop older clients and daemons with their original CLI before manually repairing retained state.`;
64 return `Agent tools failed to start${exit}. ${detail} ${recovery}`;
65 }
66}
67
68export interface DeviceHubEndpoint {
69 readonly origin: string;
70}
71
72export interface AgentDeviceEndpoint {
73 readonly baseUrl: string;
74 readonly token: string;
75 readonly entryPath: string;
76}
77
78export type HostDevice = Omit<DeviceSummary, "hostId">;
79
80/** Where a device is being opened from, for hosts whose devices depend on the project. */
81export interface DeviceOpenContext {
82 readonly workspaceRoot?: string | undefined;
83}
84
85export type AgentDaemonEndpoint = Pick<AgentDeviceEndpoint, "baseUrl" | "token">;
86
87/** Where the proxy sends a device hub request, for hosts that serve several upstreams. */
88export interface HubUpstream {
89 readonly url: string;
90 readonly headers: Readonly<Record<string, string>>;
91 readonly protocols: ReadonlyArray<string>;
92}
93
94/** Discovery and lifecycle for the devices a ready host can run. */
95export interface DeviceOperations {
96 readonly list: Effect.Effect<
97 {
98 readonly devices: ReadonlyArray<HostDevice>;
99 readonly detail: string | undefined;
100 /** Some Android virtual devices could not be listed; the rest are. */
101 readonly androidDiscoveryIncomplete?: boolean;
102 },
103 DeviceOperationError
104 >;
105 /**
106 * Boots the device and prepares its stream. Android AVDs change id when they
107 * boot (AVD name to emulator serial), so the returned id is authoritative.
108 */
109 readonly boot: (
110 device: HostDevice,
111 context?: DeviceOpenContext,
112 ) => Effect.Effect<string, DeviceOperationError | DeviceBootError | DeviceHostUnavailableError>;
113 /** Prepares the stream of a device that is already booted. */
114 readonly attach: (device: HostDevice) => Effect.Effect<void, DeviceOperationError>;
115 readonly shutdown: (device: {
116 readonly id: string;
117 readonly platform: DevicePlatform;
118 }) => Effect.Effect<void, DeviceOperationError>;
119 readonly screenshot: (device: HostDevice) => Effect.Effect<Uint8Array, DeviceOperationError>;
120 /**
121 * For hosts that run one agent-device daemon per device instead of one per
122 * host. `agentDeviceId` is the id agent-device selects the device by, when
123 * it differs from the host's device id.
124 */
125 readonly agentEndpoint?: (
126 deviceId: string,
127 ) => Effect.Effect<
128 AgentDaemonEndpoint & { readonly agentDeviceId: string },
129 DeviceHostUnavailableError
130 >;
131 /**
132 * Set by hosts that end devices on their own (EAS stops sessions at their
133 * limits), so a session on a device the host stops listing is dropped.
134 */
135 readonly endsDevices?: boolean;
136}
137
138export interface DeviceHostReady {
139 readonly nodePath: string;
140 readonly hub: DeviceHubEndpoint;
141 readonly devices: DeviceOperations;
142 readonly run: (
143 command: string,
144 args: ReadonlyArray<string>,
145 options?: { readonly timeoutMs?: number; readonly stdin?: string },
146 ) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
147 readonly helpers: {
148 readonly serveSimAxSettings: string | null;
149 readonly serveSimCli: string | null;
150 };
151 /** Routes proxied hub requests itself; otherwise they go to `hub.origin`. */
152 readonly upstream?: (
153 hubPath: string,
154 search: URLSearchParams,
155 ) => Effect.Effect<HubUpstream | null>;
156}
157
158export interface DeviceHostAgentReady extends DeviceHostReady {
159 /** Null when each device has its own daemon; see `DeviceOperations.agentEndpoint`. */
160 readonly agentDevice: AgentDeviceEndpoint | null;
161}
162
163export class DeviceHost extends Context.Service<
164 DeviceHost,
165 {
166 readonly id: DeviceHostId;
167 /** The name errors and the Device panel show for this host. */
168 readonly label: string;
169 readonly summary: Effect.Effect<DeviceHostSummary>;
170 readonly inspect?: Effect.Effect<DeviceHostSummary, DeviceHostError>;
171 readonly platformAvailability: (
172 platform: DevicePlatform,
173 ) => Effect.Effect<DevicePlatformAvailability>;
174 readonly ensureReady: (
175 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
176 ) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
177 readonly ensureAgentReady: (
178 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
179 ) => Effect.Effect<
180 DeviceHostAgentReady,
181 DeviceHostError | DeviceHostTimeoutError | AgentDeviceStartError | NodeRuntimeUnavailableError
182 >;
183 readonly current: Effect.Effect<DeviceHostReady | null>;
184 readonly stopAgent: Effect.Effect<void>;
185 /** Stops helpers. Devices themselves keep running; the user owns those. */
186 readonly stop: Effect.Effect<void>;
187 }
188>()("t3/device/DeviceHost") {}