Open History to see the last commit
1import type {
2 DeviceHostId,
3 DeviceHostSummary,
4 DevicePlatform,
5 DevicePlatformAvailability,
6} from "@t3tools/contracts";
7import * as Context from "effect/Context";
8import * as Effect from "effect/Effect";
9import * as Schema from "effect/Schema";
10import type { NodeRuntimeUnavailableError } from "@t3tools/shared/nodeRuntime";
11
12export class DeviceHostError extends Schema.TaggedError<DeviceHostError>()("DeviceHostError", {
13 hostId: Schema.String,
14 step: Schema.String,
15 cause: Schema.Defect(),
16}) {
17 override get message(): string {
18 return `Device host ${this.hostId} failed while ${this.step}.`;
19 }
20}
21
22export class DeviceHostTimeoutError extends Schema.TaggedError<DeviceHostTimeoutError>()(
23 "DeviceHostTimeoutError",
24 { hostId: Schema.String, timeoutMs: Schema.Number },
25) {
26 override get message(): string {
27 return `Device host ${this.hostId} did not start agent tools within ${this.timeoutMs} ms.`;
28 }
29}
30
31export class AgentDeviceStartError extends Schema.TaggedError<AgentDeviceStartError>()(
32 "AgentDeviceStartError",
33 {
34 hostId: Schema.String,
35 stateDir: Schema.String,
36 exitCode: Schema.NullOr(Schema.Int),
37 reason: Schema.Literals([
38 "daemon_lock_not_directory",
39 "daemon_registration_unproven",
40 "daemon_retirement_unconfirmed",
41 "command_failed",
42 ]),
43 cause: Schema.Defect(),
44 },
45) {
46 override get message(): string {
47 const exit = this.exitCode === null ? "" : ` (exit code ${this.exitCode})`;
48 const detail =
49 this.reason === "daemon_lock_not_directory"
50 ? "Daemon registration ownership could not be verified: daemon.lock is not a directory."
51 : this.reason === "daemon_registration_unproven"
52 ? "Daemon registration ownership could not be verified."
53 : this.reason === "daemon_retirement_unconfirmed"
54 ? "Daemon replacement could not be confirmed."
55 : "The AgentDevice startup command failed.";
56 const recovery =
57 this.reason === "command_failed"
58 ? `Inspect daemon diagnostics in ${this.stateDir}.`
59 : `Inspect daemon.lock and daemon diagnostics in ${this.stateDir}. Stop older clients and daemons with their original CLI before manually repairing retained state.`;
60 return `Agent tools failed to start${exit}. ${detail} ${recovery}`;
61 }
62}
63
64export interface DeviceHubEndpoint {
65 readonly origin: string;
66}
67
68export interface AgentDeviceEndpoint {
69 readonly baseUrl: string;
70 readonly token: string;
71 readonly entryPath: string;
72}
73
74export interface DeviceHostReady {
75 readonly nodePath: string;
76 readonly hub: DeviceHubEndpoint;
77 readonly run: (
78 command: string,
79 args: ReadonlyArray<string>,
80 options?: { readonly timeoutMs?: number; readonly stdin?: string },
81 ) => Effect.Effect<{ readonly stdout: string; readonly stderr: string; readonly code: number }>;
82 readonly helpers: {
83 readonly serveSimAxSettings: string | null;
84 readonly serveSimCli: string | null;
85 };
86}
87
88export interface DeviceHostAgentReady extends DeviceHostReady {
89 readonly agentDevice: AgentDeviceEndpoint;
90}
91
92export class DeviceHost extends Context.Service<
93 DeviceHost,
94 {
95 readonly id: DeviceHostId;
96 readonly summary: Effect.Effect<DeviceHostSummary>;
97 readonly inspect?: Effect.Effect<DeviceHostSummary, DeviceHostError>;
98 readonly platformAvailability: (
99 platform: DevicePlatform,
100 ) => Effect.Effect<DevicePlatformAvailability>;
101 readonly ensureReady: (
102 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
103 ) => Effect.Effect<DeviceHostReady, DeviceHostError | NodeRuntimeUnavailableError>;
104 readonly ensureAgentReady: (
105 onPhase: (phase: "installing" | "starting", detail?: string) => Effect.Effect<void>,
106 ) => Effect.Effect<
107 DeviceHostAgentReady,
108 DeviceHostError | DeviceHostTimeoutError | AgentDeviceStartError | NodeRuntimeUnavailableError
109 >;
110 readonly current: Effect.Effect<DeviceHostReady | null>;
111 readonly stopAgent: Effect.Effect<void>;
112 /** Stops helpers. Devices themselves keep running; the user owns those. */
113 readonly stop: Effect.Effect<void>;
114 }
115>()("t3/device/DeviceHost") {}