The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { |
| 3 | type DeviceHostSummary, |
| 4 | type DevicePlatformAvailability, |
| 5 | type EnvironmentId, |
| 6 | SshDeviceHostConfig, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { msg } from "gt-react"; |
| 10 | |
| 11 | import { m } from "../../i18n/messages"; |
| 12 | |
| 13 | export interface DeviceHostCheckTarget { |
| 14 | environmentId: EnvironmentId; |
| 15 | label: string; |
| 16 | connected: boolean; |
| 17 | } |
| 18 | export type DeviceHostCheck = |
| 19 | | { status: "pending" } |
| 20 | | { status: "local" } |
| 21 | | { |
| 22 | status: "connected"; |
| 23 | platforms: ReadonlyArray<DevicePlatformAvailability>; |
| 24 | tools?: DeviceHostSummary["tools"]; |
| 25 | } |
| 26 | | { status: "failed"; error: string }; |
| 27 | |
| 28 | const decodeDeviceHostDraft = Schema.decodeUnknownOption(SshDeviceHostConfig); |
| 29 | |
| 30 | export function parseDeviceHostDraft(host: SshDeviceHostConfig) { |
| 31 | const { identityFile, ...rest } = host; |
| 32 | return decodeDeviceHostDraft({ |
| 33 | ...rest, |
| 34 | ...(identityFile?.trim() ? { identityFile: identityFile.trim() } : {}), |
| 35 | }); |
| 36 | } |
| 37 | |
| 38 | export function deviceHostConnectionKey(host: SshDeviceHostConfig) { |
| 39 | return JSON.stringify([host.target.trim(), host.port, host.identityFile?.trim() || undefined]); |
| 40 | } |
| 41 | |
| 42 | export async function checkDeviceHostConnections( |
| 43 | targets: ReadonlyArray<DeviceHostCheckTarget>, |
| 44 | host: SshDeviceHostConfig, |
| 45 | probe: (environmentId: EnvironmentId, host: SshDeviceHostConfig) => Promise<DeviceHostSummary>, |
| 46 | report: (environmentId: EnvironmentId, result: DeviceHostCheck) => void, |
| 47 | ) { |
| 48 | await Promise.all( |
| 49 | targets.map(async (target) => { |
| 50 | report(target.environmentId, { status: "pending" }); |
| 51 | try { |
| 52 | if (!target.connected) throw new Error(m(msg("Environment disconnected"))); |
| 53 | const result = await probe(target.environmentId, host); |
| 54 | report( |
| 55 | target.environmentId, |
| 56 | result.kind === "local" |
| 57 | ? { status: "local" } |
| 58 | : { |
| 59 | status: "connected", |
| 60 | platforms: result.platforms, |
| 61 | ...(result.tools ? { tools: result.tools } : {}), |
| 62 | }, |
| 63 | ); |
| 64 | } catch (error) { |
| 65 | report(target.environmentId, { |
| 66 | status: "failed", |
| 67 | error: |
| 68 | error instanceof Error |
| 69 | ? (thrownErrorMessage(error, m) ?? error.message) |
| 70 | : String(error), |
| 71 | }); |
| 72 | } |
| 73 | }), |
| 74 | ); |
| 75 | } |