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
87export interface AgentDeviceBinding extends AgentDaemonEndpoint {
88 /** Null when the session alone picks the device and selector flags would be refused. */
89 readonly agentDeviceId: string | null;
90 /** The one session the device is leased to, shared by every thread. */
91 readonly session?: string;
92 /** agent-device state holding that session's connection. */
93 readonly stateDir?: string;
94 /** agent-device accepts installs into the session only from the directory that opened it. */
95 readonly installDirectory?: string;
96}
97
98/** Where the proxy sends a device hub request, for hosts that serve several upstreams. */
99export interface HubUpstream {
100 readonly url: string;
101 readonly headers: Readonly<Record<string, string>>;
102 readonly protocols: ReadonlyArray<string>;
103}
104
105/** Discovery and lifecycle for the devices a ready host can run. */
106export interface DeviceOperations {
107 readonly list: Effect.Effect<
108 {
109 readonly devices: ReadonlyArray<HostDevice>;
110 readonly detail: string | undefined;
111 /** Some Android virtual devices could not be listed; the rest are. */
112 readonly androidDiscoveryIncomplete?: boolean;
113 },
114 DeviceOperationError
115 >;
116 /**
117 * Boots the device and prepares its stream. Android AVDs change id when they
118 * boot (AVD name to emulator serial), so the returned id is authoritative.
119 */
120 readonly boot: (
121 device: HostDevice,
122 context?: DeviceOpenContext,
123 ) => Effect.Effect<string, DeviceOperationError | DeviceBootError | DeviceHostUnavailableError>;
124 /** Prepares the stream of a device that is already booted. */
125 readonly attach: (device: HostDevice) => Effect.Effect<void, DeviceOperationError>;
126 readonly shutdown: (device: {
127 readonly id: string;
128 readonly platform: DevicePlatform;
129 }) => Effect.Effect<void, DeviceOperationError>;
130 readonly screenshot: (device: HostDevice) => Effect.Effect<Uint8Array, DeviceOperationError>;
131 /**
132 * For hosts that run one agent-device daemon per device instead of one per
133 * host. `agentDeviceId` is the id agent-device selects the device by, when
134 * it differs from the host's device id.
135 */
136 readonly agentEndpoint?: (
137 deviceId: string,
138 ) => Effect.Effect<AgentDeviceBinding, DeviceHostUnavailableError>;
139 /**
140 * Set by hosts that end devices on their own (EAS stops sessions at their
141 * limits), so a session on a device the host stops listing is dropped.
142 */
143 readonly endsDevices?: boolean;
144}
145
146export interface DeviceHostReady {
147 readonly nodePath: string;
148 /**
149 * Ends an agent-device session on the daemon that serves `deviceId`. Hosts
150 * with a shared daemon provide it only while that daemon runs; hosts with a
151 * daemon per device succeed when the device's daemon is gone. Cleanup never
152 * starts a daemon or a device.
153 */
154 readonly releaseAgentSession?: (
155 session: string,
156 deviceId: string,
157 ) => Effect.Effect<void, DeviceOperationError>;
158 readonly hub: DeviceHubEndpoint;
159 readonly devices: DeviceOperations;
160 readonly run: (
161 command: string,
162 args: ReadonlyArray<string>,
163 options?: { readonly timeoutMs?: number; readonly stdin?: string },
164 ) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
165 readonly helpers: {
166 readonly serveSimAxSettings: string | null;
167 readonly serveSimCli: string | null;
168 };
169 /** Routes proxied hub requests itself; otherwise they go to `hub.origin`. */
170 readonly upstream?: (
171 hubPath: string,
172 search: URLSearchParams,
173 ) => Effect.Effect<HubUpstream | null>;
174}
175
176export interface DeviceHostAgentReady extends DeviceHostReady {
177 /** Null when each device has its own daemon; see `DeviceOperations.agentEndpoint`. */
178 readonly agentDevice: AgentDeviceEndpoint | null;
179}
180
181export class DeviceHost extends Context.Service<
182 DeviceHost,
183 {
184 readonly id: DeviceHostId;
185 /** The name errors and the Device panel show for this host. */
186 readonly label: string;
187 readonly summary: Effect.Effect<DeviceHostSummary>;
188 readonly inspect?: Effect.Effect<DeviceHostSummary, DeviceHostError>;
189 readonly platformAvailability: (
190 platform: DevicePlatform,
191 ) => Effect.Effect<DevicePlatformAvailability>;
192 readonly ensureReady: (
193 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
194 ) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
195 readonly ensureAgentReady: (
196 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
197 ) => Effect.Effect<
198 DeviceHostAgentReady,
199 DeviceHostError | DeviceHostTimeoutError | AgentDeviceStartError | NodeRuntimeUnavailableError
200 >;
201 readonly current: Effect.Effect<DeviceHostReady | null>;
202 readonly stopAgent: Effect.Effect<void>;
203 /** Stops helpers. Devices themselves keep running; the user owns those. */
204 readonly stop: Effect.Effect<void>;
205 }
206>()("t3/device/DeviceHost") {}