Open History to see the last commit
1import { type DeviceToolVersions, WS_METHODS } from "@t3tools/contracts";
2import { Atom } from "effect/reactivity";
3
4import type { EnvironmentRegistry } from "../connection/registry.ts";
5import {
6 createAtomCommandScheduler,
7 createEnvironmentRpcCommand,
8 createEnvironmentRpcSubscriptionAtomFamily,
9} from "./runtime.ts";
10
11export function createDeviceEnvironmentAtoms<R, E>(
12 runtime: Atom.AtomRuntime<EnvironmentRegistry | R, E>,
13) {
14 const scheduler = createAtomCommandScheduler();
15 const concurrency = {
16 mode: "serial" as const,
17 key: ({ environmentId }: { environmentId: string }) => environmentId,
18 };
19 return {
20 state: createEnvironmentRpcSubscriptionAtomFamily(runtime, {
21 label: "environment-data:device:state",
22 tag: WS_METHODS.subscribeDeviceState,
23 }),
24 configure: createEnvironmentRpcCommand(runtime, {
25 label: "environment-data:device:configure",
26 tag: WS_METHODS.deviceConfigure,
27 scheduler,
28 concurrency,
29 }),
30 testHost: createEnvironmentRpcCommand(runtime, {
31 label: "environment-data:device:test-host",
32 tag: WS_METHODS.deviceTestHost,
33 }),
34 list: createEnvironmentRpcCommand(runtime, {
35 label: "environment-data:device:list",
36 tag: WS_METHODS.deviceList,
37 scheduler,
38 concurrency,
39 }),
40 open: createEnvironmentRpcCommand(runtime, {
41 label: "environment-data:device:open",
42 tag: WS_METHODS.deviceOpen,
43 scheduler,
44 concurrency,
45 }),
46 close: createEnvironmentRpcCommand(runtime, {
47 label: "environment-data:device:close",
48 tag: WS_METHODS.deviceClose,
49 scheduler,
50 concurrency,
51 }),
52 shutdown: createEnvironmentRpcCommand(runtime, {
53 label: "environment-data:device:shutdown",
54 tag: WS_METHODS.deviceShutdown,
55 scheduler,
56 concurrency,
57 }),
58 detail: createEnvironmentRpcCommand(runtime, {
59 label: "environment-data:device:detail",
60 tag: WS_METHODS.deviceDetail,
61 scheduler,
62 concurrency,
63 }),
64 action: createEnvironmentRpcCommand(runtime, {
65 label: "environment-data:device:action",
66 tag: WS_METHODS.deviceAction,
67 scheduler,
68 concurrency,
69 }),
70 };
71}
72
73export function deviceToolVersionLabels(tools: DeviceToolVersions | undefined) {
74 if (!tools) return ["Device tool versions have not been checked."];
75 return (
76 [
77 ["Device hub", tools.hub],
78 ["Agent tools", tools.agent],
79 ] as const
80 ).map(([name, tool]) => {
81 const installed = tool.installedVersions.length ? tool.installedVersions.join(", ") : "none";
82 return `${name}: installed ${installed}; required ${tool.requiredVersion}${tool.runningVersion ? `; running ${tool.runningVersion}` : ""}.`;
83 });
84}
85
86export function deviceToolUpdatePolicy(tools: DeviceToolVersions | undefined) {
87 if (!tools) return "Versions have not been checked. Reconnect the host and check versions.";
88 const outdated = [tools.hub, tools.agent].filter(
89 (tool) =>
90 tool.installedVersions.length > 0 && !tool.installedVersions.includes(tool.requiredVersion),
91 );
92 return outdated.length > 0
93 ? "Update pending. Required tools will install automatically when next used. The host needs network access; an older install is not used as a fallback."
94 : "Required tools are installed automatically when needed. Checking versions does not install or start anything.";
95}
96export const deviceToolUpdateOwnership =
97 "This environment's T3 server chooses device tool versions for itself and its SSH hosts. Update that server to receive newer tool versions; updating only your browser or mobile app does not update a remote server.";