@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

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