The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { ServerConfig } from "@t3tools/contracts"; |
| 2 | import * as Option from "effect/Option"; |
| 3 | import { mFallback, msg } from "gt-i18n"; |
| 4 | import type { MFunctionType } from "gt-i18n/types"; |
| 5 | |
| 6 | import { type MarkedText, resolveMarkedText } from "../errors/translatable.ts"; |
| 7 | import type { ConnectionCatalogEntry } from "./catalog.ts"; |
| 8 | import type { ConnectionTarget, SupervisorConnectionState } from "./model.ts"; |
| 9 | import { connectionRouteId, connectionRoutes, routeHttpBaseUrl } from "./routes.ts"; |
| 10 | |
| 11 | export type EnvironmentConnectionPhase = |
| 12 | | "available" |
| 13 | | "offline" |
| 14 | | "connecting" |
| 15 | | "reconnecting" |
| 16 | | "connected" |
| 17 | | "error" |
| 18 | | "unsupported"; |
| 19 | |
| 20 | export interface EnvironmentConnectionPresentation { |
| 21 | readonly phase: EnvironmentConnectionPhase; |
| 22 | readonly error: string | null; |
| 23 | /** `error` as marked text, when the failure carries it. Read both through `connectionErrorText`. */ |
| 24 | readonly i18nError?: MarkedText; |
| 25 | readonly traceId: string | null; |
| 26 | /** |
| 27 | * Set while reconnecting because the host has not approved this device yet. `error` then |
| 28 | * says where to approve it and shows the code, and is not a failure. |
| 29 | */ |
| 30 | readonly awaitingApproval?: true; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Provider statuses are pushed every few minutes with fresh `checkedAt` stamps, and every root |
| 35 | * coordinator reads environment presentations. Leaving them out keeps a presentation stable |
| 36 | * across those pushes; provider readers subscribe to the server's providers atom instead. |
| 37 | */ |
| 38 | export type EnvironmentServerConfig = Omit<ServerConfig, "providers">; |
| 39 | |
| 40 | export interface EnvironmentPresentation { |
| 41 | readonly entry: ConnectionCatalogEntry; |
| 42 | readonly connection: EnvironmentConnectionPresentation; |
| 43 | readonly serverConfig: EnvironmentServerConfig | null; |
| 44 | /** See `SupervisorConnectionState.refusedRouteIds`. */ |
| 45 | readonly refusedRouteIds: ReadonlyArray<string>; |
| 46 | } |
| 47 | |
| 48 | function failureError( |
| 49 | failure: SupervisorConnectionState["lastFailure"], |
| 50 | ): Pick<EnvironmentConnectionPresentation, "error" | "i18nError" | "traceId"> { |
| 51 | return { |
| 52 | error: failure?.message ?? null, |
| 53 | ...(failure?.i18nMessage !== undefined ? { i18nError: failure.i18nMessage } : {}), |
| 54 | traceId: failure?.traceId ?? null, |
| 55 | }; |
| 56 | } |
| 57 | |
| 58 | export function presentConnectionState( |
| 59 | state: SupervisorConnectionState, |
| 60 | ): EnvironmentConnectionPresentation { |
| 61 | if ( |
| 62 | (state.phase === "connecting" || state.phase === "backoff") && |
| 63 | state.lastFailure?.reason === "approval-pending" |
| 64 | ) { |
| 65 | return { |
| 66 | phase: "reconnecting", |
| 67 | ...failureError(state.lastFailure), |
| 68 | awaitingApproval: true, |
| 69 | }; |
| 70 | } |
| 71 | switch (state.phase) { |
| 72 | case "available": |
| 73 | return { phase: "available", error: null, traceId: null }; |
| 74 | case "offline": |
| 75 | return { phase: "offline", error: null, traceId: null }; |
| 76 | case "connecting": |
| 77 | return { |
| 78 | phase: state.attempt <= 1 && state.lastFailure === null ? "connecting" : "reconnecting", |
| 79 | ...failureError(state.lastFailure), |
| 80 | }; |
| 81 | case "connected": |
| 82 | return { phase: "connected", error: null, traceId: null }; |
| 83 | case "backoff": |
| 84 | return { |
| 85 | phase: "reconnecting", |
| 86 | ...failureError(state.lastFailure), |
| 87 | }; |
| 88 | case "blocked": |
| 89 | return { |
| 90 | phase: state.lastFailure?.reason === "unsupported" ? "unsupported" : "error", |
| 91 | ...failureError(state.lastFailure), |
| 92 | }; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | /** The connection's error in the active language, or null when it has none. */ |
| 97 | export function connectionErrorText( |
| 98 | connection: Pick<EnvironmentConnectionPresentation, "error" | "i18nError">, |
| 99 | resolve: MFunctionType = mFallback, |
| 100 | ): string | null { |
| 101 | if (connection.error === null) return null; |
| 102 | return connection.i18nError !== undefined |
| 103 | ? resolveMarkedText(connection.i18nError, resolve) |
| 104 | : connection.error; |
| 105 | } |
| 106 | |
| 107 | export function connectionStatusText( |
| 108 | connection: EnvironmentConnectionPresentation, |
| 109 | resolve: MFunctionType = mFallback, |
| 110 | ): string { |
| 111 | const error = connectionErrorText(connection, resolve); |
| 112 | switch (connection.phase) { |
| 113 | case "available": |
| 114 | return resolve(msg("Available", { $context: "connection status" })); |
| 115 | case "offline": |
| 116 | return resolve(msg("Offline", { $context: "connection status" })); |
| 117 | case "connecting": |
| 118 | return resolve(msg("Connecting...")); |
| 119 | case "reconnecting": |
| 120 | if (connection.awaitingApproval) { |
| 121 | return error ?? resolve(msg("Waiting for approval on the host")); |
| 122 | } |
| 123 | return error |
| 124 | ? resolve(msg("Failed to connect. Reconnecting... Reason: {error}", { error })) |
| 125 | : resolve(msg("Reconnecting...")); |
| 126 | case "connected": |
| 127 | return resolve(msg("Connected", { $context: "connection status" })); |
| 128 | case "unsupported": |
| 129 | return resolve(msg("Client not supported")); |
| 130 | case "error": |
| 131 | return error |
| 132 | ? resolve(msg("Connection failed. Reason: {error}", { error })) |
| 133 | : resolve(msg("Connection failed")); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | export function connectionStatusTitle( |
| 138 | connection: EnvironmentConnectionPresentation, |
| 139 | resolve: MFunctionType = mFallback, |
| 140 | ): string { |
| 141 | if (connection.awaitingApproval) { |
| 142 | return resolve(msg("Waiting for approval")); |
| 143 | } |
| 144 | if (connection.phase === "reconnecting" && connection.error) { |
| 145 | return resolve(msg("Failed to connect. Reconnecting...")); |
| 146 | } |
| 147 | return connectionStatusText({ ...connection, error: null }, resolve); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * The T3 Connect address wins whenever it is known: it works for agents anywhere, while the |
| 152 | * connected route is often a LAN or loopback address. SSH routes ride a local forward that |
| 153 | * disappears with the client, so they have no stable address. |
| 154 | */ |
| 155 | export function environmentMcpUrl(input: { |
| 156 | readonly entry: ConnectionCatalogEntry; |
| 157 | readonly relayHttpBaseUrl?: string | undefined; |
| 158 | readonly connectedTarget?: ConnectionTarget | null | undefined; |
| 159 | }): string | null { |
| 160 | const relayMcpUrl = input.relayHttpBaseUrl ? mcpUrlFromBase(input.relayHttpBaseUrl) : null; |
| 161 | if (relayMcpUrl !== null) return relayMcpUrl; |
| 162 | const connectedRouteId = input.connectedTarget ? connectionRouteId(input.connectedTarget) : null; |
| 163 | const routes = connectionRoutes(input.entry); |
| 164 | const connectedRoute = routes.find( |
| 165 | (route) => connectionRouteId(route.target) === connectedRouteId, |
| 166 | ); |
| 167 | for (const route of connectedRoute ? [connectedRoute, ...routes] : routes) { |
| 168 | const httpBaseUrl = routeHttpBaseUrl(route); |
| 169 | const mcpUrl = httpBaseUrl === null ? null : mcpUrlFromBase(httpBaseUrl); |
| 170 | if (mcpUrl !== null) return mcpUrl; |
| 171 | } |
| 172 | return null; |
| 173 | } |
| 174 | |
| 175 | function mcpUrlFromBase(httpBaseUrl: string): string | null { |
| 176 | let url: URL; |
| 177 | try { |
| 178 | url = new URL(httpBaseUrl); |
| 179 | } catch { |
| 180 | return null; |
| 181 | } |
| 182 | url.pathname = "/mcp"; |
| 183 | url.search = ""; |
| 184 | url.hash = ""; |
| 185 | return url.toString(); |
| 186 | } |
| 187 | |
| 188 | export function connectionCatalogDisplayUrl(entry: ConnectionCatalogEntry): string | null { |
| 189 | switch (entry.target._tag) { |
| 190 | case "PrimaryConnectionTarget": |
| 191 | return entry.target.httpBaseUrl; |
| 192 | case "RelayConnectionTarget": |
| 193 | return null; |
| 194 | case "BearerConnectionTarget": |
| 195 | return Option.isSome(entry.profile) && entry.profile.value._tag === "BearerConnectionProfile" |
| 196 | ? entry.profile.value.httpBaseUrl |
| 197 | : null; |
| 198 | case "SshConnectionTarget": |
| 199 | return Option.isSome(entry.profile) && entry.profile.value._tag === "SshConnectionProfile" |
| 200 | ? `${entry.profile.value.target.username}@${entry.profile.value.target.hostname}` |
| 201 | : null; |
| 202 | } |
| 203 | } |