@@ -2,6 +2,7 @@ import type {
22 EnvironmentCloudLinkStateResult ,
33 EnvironmentConnectReachability ,
44} from "@t3tools/contracts" ;
5+ import type { Discovery } from "@t3tools/client-runtime/relay" ;
56import type { RelayManagedEndpoint } from "@t3tools/contracts/relay" ;
67
78import { isManagedTunnelActive , t3ConnectMcpUrl } from "~/cloud/t3ConnectState" ;
@@ -15,8 +16,11 @@ type Unreachable = Extract<EnvironmentConnectReachability, { readonly status: "u
1516export type McpGate =
1617 | { readonly kind : "checking" }
1718 | { readonly kind : "sign-in" }
18- /** `offline`: T3 Connect reports the saved machine offline, so reconnecting cannot help. */
19- | { readonly kind : "disconnected" ; readonly offline : boolean }
19+ /**
20+ * `relay`: T3 Connect reports the saved machine offline, or its list could not be read; both
21+ * keep a retry. Null when that list shows nothing wrong, so the user should reconnect.
22+ */
23+ | { readonly kind : "disconnected" ; readonly relay : "offline" | "failed" | null }
2024 | { readonly kind : "update" }
2125 | { readonly kind : "no-relay-read" }
2226 | { readonly kind : "error" ; readonly message : string }
@@ -28,30 +32,42 @@ export function resolveMcpGate(input: {
2832 /** Undefined until T3 Connect sign-in has loaded. */
2933 readonly signedIn : boolean | undefined ;
3034 readonly connected : boolean ;
31- /** T3 Connect's list reports this saved machine offline. */
32- readonly relayOffline : boolean ;
3335 /** The environment's `mcpApprovalCodes` capability; undefined until its config loads. */
3436 readonly approvalCodes : boolean | undefined ;
3537 readonly canReadRelay : boolean ;
3638 readonly linkState : EnvironmentCloudLinkStateResult | null ;
3739 readonly linkStateError : string | null ;
3840 /** A backend this device runs; its link state names its own tunnel. */
3941 readonly thisMachine : boolean ;
40- /** The account's machines on T3 Connect, where a saved machine's address comes from. */
42+ /** The account's machines on T3 Connect, where a saved machine's address and health come from. */
4143 readonly relay :
4244 | { readonly status : "loading" | "failed" }
43- | { readonly status : "listed" ; readonly endpoint : RelayManagedEndpoint | undefined } ;
45+ | {
46+ readonly status : "listed" ;
47+ readonly endpoint : RelayManagedEndpoint | undefined ;
48+ readonly availability ?: Discovery . RelayEnvironmentAvailability | undefined ;
49+ } ;
4450 /** A check run for this gate: its answer, "checking" until it has one, or null if it failed. */
4551 readonly reachability : EnvironmentConnectReachability | "checking" | null ;
4652} ) : McpGate {
4753 if ( input . signedIn === undefined ) return { kind : "checking" } ;
4854 if ( ! input . signedIn ) return { kind : "sign-in" } ;
49- if ( ! input . connected ) return { kind : "disconnected" , offline : input . relayOffline } ;
55+ const { relay } = input ;
56+ if ( ! input . connected ) {
57+ if ( input . thisMachine ) return { kind : "disconnected" , relay : null } ;
58+ const availability = relay . status === "listed" ? relay . availability : undefined ;
59+ // A refresh empties the list first, so wait for it instead of flashing "Connect first".
60+ if ( relay . status === "loading" || availability === "checking" ) return { kind : "checking" } ;
61+ if ( relay . status === "failed" || availability === "error" ) {
62+ return { kind : "disconnected" , relay : "failed" } ;
63+ }
64+ return { kind : "disconnected" , relay : availability === "offline" ? "offline" : null } ;
65+ }
5066 if ( input . approvalCodes === undefined ) return { kind : "checking" } ;
5167 if ( ! input . approvalCodes ) return { kind : "update" } ;
5268 if ( ! input . canReadRelay ) return { kind : "no-relay-read" } ;
5369 if ( input . linkStateError !== null ) return { kind : "error" , message : input . linkStateError } ;
54- const { linkState, relay } = input ;
70+ const { linkState } = input ;
5571 if ( linkState === null ) return { kind : "checking" } ;
5672 // This machine reports its own tunnel and the probe proves it; the list speaks for saved ones.
5773 const endpoint = ! input . thisMachine && relay . status === "listed" ? relay . endpoint : undefined ;
0 commit comments