Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mcpGate.ts

121 lines5.5 KB

1import type {
2 EnvironmentCloudLinkStateResult,
3 EnvironmentConnectReachability,
4} from "@t3tools/contracts";
5import type { Discovery } from "@t3tools/client-runtime/relay";
6import type { RelayManagedEndpoint } from "@t3tools/contracts/relay";
7​
8import { isManagedTunnelActive, t3ConnectMcpUrl } from "~/cloud/t3ConnectState";
9​
10type Unreachable = Extract<EnvironmentConnectReachability, { readonly status: "unreachable" }>;
11​
12/**
13 * What stands between the user and the wizard's steps. Only `open` lets them go on: cloud apps
14 * need a public address, so the environment must answer on its T3 Connect tunnel.
15 */
16export type McpGate =
17 | { readonly kind: "checking" }
18 | { readonly kind: "sign-in" }
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 }
24 | { readonly kind: "update" }
25 | { readonly kind: "no-relay-read" }
26 | { readonly kind: "error"; readonly message: string }
27 | { readonly kind: "turn-on" }
28 | { readonly kind: "offline"; readonly reachability: Unreachable | null }
29 | { readonly kind: "open"; readonly mcpUrl: string };
30​
31export function resolveMcpGate(input: {
32 /** Undefined until T3 Connect sign-in has loaded. */
33 readonly signedIn: boolean | undefined;
34 readonly connected: boolean;
35 /** The environment's `mcpApprovalCodes` capability; undefined until its config loads. */
36 readonly approvalCodes: boolean | undefined;
37 readonly canReadRelay: boolean;
38 readonly linkState: EnvironmentCloudLinkStateResult | null;
39 readonly linkStateError: string | null;
40 /** A backend this device runs; its link state names its own tunnel. */
41 readonly thisMachine: boolean;
42 /** The account's machines on T3 Connect, where a saved machine's address and health come from. */
43 readonly relay:
44 | { readonly status: "loading" | "failed" }
45 | {
46 readonly status: "listed";
47 readonly endpoint: RelayManagedEndpoint | undefined;
48 readonly availability?: Discovery.RelayEnvironmentAvailability | undefined;
49 };
50 /** A check run for this gate: its answer, "checking" until it has one, or null if it failed. */
51 readonly reachability: EnvironmentConnectReachability | "checking" | null;
52}): McpGate {
53 if (input.signedIn === undefined) return { kind: "checking" };
54 if (!input.signedIn) return { kind: "sign-in" };
55 const { relay } = input;
56 if (!input.connected) {
57 if (input.thisMachine) return { kind: "disconnected", relay: null };
58 // T3 Connect checks only its own tunnels; a publish-only link's check always fails.
59 const availability =
60 relay.status === "listed" && relay.endpoint?.providerKind === "cloudflare_tunnel"
61 ? relay.availability
62 : undefined;
63 // A refresh empties the list first, so wait for it instead of flashing "Connect first".
64 if (relay.status === "loading" || availability === "checking") return { kind: "checking" };
65 if (relay.status === "failed" || availability === "error") {
66 return { kind: "disconnected", relay: "failed" };
67 }
68 return { kind: "disconnected", relay: availability === "offline" ? "offline" : null };
69 }
70 if (input.approvalCodes === undefined) return { kind: "checking" };
71 if (!input.approvalCodes) return { kind: "update" };
72 if (!input.canReadRelay) return { kind: "no-relay-read" };
73 if (input.linkStateError !== null) return { kind: "error", message: input.linkStateError };
74 const { linkState } = input;
75 if (linkState === null) return { kind: "checking" };
76 // This machine reports its own tunnel and the probe proves it; the list speaks for saved ones.
77 const endpoint = !input.thisMachine && relay.status === "listed" ? relay.endpoint : undefined;
78 if (!isManagedTunnelActive(linkState, endpoint?.providerKind)) return { kind: "turn-on" };
79 if (!input.thisMachine) {
80 if (relay.status === "loading") return { kind: "checking" };
81 if (relay.status === "failed") return { kind: "error", message: "Could not reach T3 Connect." };
82 if (endpoint === undefined) return { kind: "turn-on" };
83 }
84 const { reachability } = input;
85 if (reachability === "checking") return { kind: "checking" };
86 const mcpUrl = t3ConnectMcpUrl({
87 thisMachine: input.thisMachine,
88 tunnelHostname: linkState.tunnelHostname,
89 relayEndpoint: endpoint,
90 });
91 if (reachability?.status === "reachable" && mcpUrl !== null) return { kind: "open", mcpUrl };
92 return {
93 kind: "offline",
94 reachability: reachability?.status === "unreachable" ? reachability : null,
95 };
97​
98/**
99 * What a failed check request leaves: `prior` is the link state from before it, `latest` a read
100 * after it (null when unread). The server may still have checked, so a result newer than `prior`
101 * counts. Otherwise only a later server check does; with nothing read at all, only a retry does.
102 */
103export function afterFailedCheck(
104 prior: EnvironmentCloudLinkStateResult | null,
105 latest: EnvironmentCloudLinkStateResult | null,
106): { readonly result: EnvironmentConnectReachability | null; readonly since: string | null } {
107 const fresh = latest?.reachability;
108 const known = prior?.reachability;
109 if (
110 fresh !== undefined &&
111 prior !== null &&
112 (known === undefined || fresh.checkedAt > known.checkedAt)
113 ) {
114 return { result: fresh, since: fresh.checkedAt };
115 }
116 const baseline = latest ?? prior;
117 return {
118 result: null,
119 since: baseline === null ? null : (baseline.reachability?.checkedAt ?? ""),
120 };