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 type { EnvironmentCloudLinkStateResult } from "@t3tools/contracts"; |
| 2 | import type { |
| 3 | RelayManagedEndpoint, |
| 4 | RelayManagedEndpointProviderKind, |
| 5 | } from "@t3tools/contracts/relay"; |
| 6 | import { parseUrl } from "@t3tools/shared/url"; |
| 7 | |
| 8 | // Require managed discovery: stale tunnel config after a publish-only switch causes endpoint_provider_not_managed. |
| 9 | export function isManagedTunnelActive( |
| 10 | linkState: EnvironmentCloudLinkStateResult, |
| 11 | relayProviderKind: RelayManagedEndpointProviderKind | undefined, |
| 12 | ) { |
| 13 | const localManagedTunnel = linkState.managedTunnelActive ?? linkState.linked; |
| 14 | return ( |
| 15 | linkState.linkRevoked !== true && |
| 16 | localManagedTunnel && |
| 17 | (relayProviderKind === undefined || relayProviderKind === "cloudflare_tunnel") |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Whether reaching `desired` links or relinks the environment, which needs `relay:configure`. |
| 23 | * Unlinking and preference changes on the current link only need `relay:write`. |
| 24 | */ |
| 25 | export function cloudChangeReplacesLink( |
| 26 | linkState: EnvironmentCloudLinkStateResult, |
| 27 | relayProviderKind: RelayManagedEndpointProviderKind | undefined, |
| 28 | desired: { readonly managedTunnel: boolean; readonly publish: boolean }, |
| 29 | ) { |
| 30 | return ( |
| 31 | (desired.managedTunnel || desired.publish) && |
| 32 | (!linkState.linked || |
| 33 | linkState.linkRevoked === true || |
| 34 | isManagedTunnelActive(linkState, relayProviderKind) !== desired.managedTunnel) |
| 35 | ); |
| 36 | } |
| 37 | |
| 38 | // The URL ends up in commands people paste into a shell, so only plain DNS names pass. |
| 39 | const TUNNEL_HOSTNAME = /^[a-z0-9-]+(\.[a-z0-9-]+)+$/i; |
| 40 | |
| 41 | /** |
| 42 | * An environment's MCP URL on its T3 Connect tunnel, or null. This machine reports its own |
| 43 | * tunnel hostname; a saved machine's comes from T3 Connect, and only a tunnel counts there, |
| 44 | * since a publish-only link registers its local address as `manual`. |
| 45 | */ |
| 46 | export function t3ConnectMcpUrl(input: { |
| 47 | readonly thisMachine: boolean; |
| 48 | readonly tunnelHostname: string | undefined; |
| 49 | readonly relayEndpoint: RelayManagedEndpoint | undefined; |
| 50 | }): string | null { |
| 51 | const url = input.thisMachine |
| 52 | ? parseUrl(`https://${input.tunnelHostname ?? ""}`) |
| 53 | : input.relayEndpoint?.providerKind === "cloudflare_tunnel" |
| 54 | ? parseUrl(input.relayEndpoint.httpBaseUrl) |
| 55 | : null; |
| 56 | return url?.protocol === "https:" && TUNNEL_HOSTNAME.test(url.hostname) |
| 57 | ? `${url.origin}/mcp` |
| 58 | : null; |
| 59 | } |
| 60 | |
| 61 | /** Where T3 Connect can be turned on for an environment: from here, only by policy, or on its host. */ |
| 62 | export type T3ConnectTurnOn = |
| 63 | | { readonly kind: "here" } |
| 64 | | { readonly kind: "policy"; readonly reason: string } |
| 65 | | { readonly kind: "on-host" }; |
| 66 | |
| 67 | /** |
| 68 | * Settings and the MCP wizard share this, so a Turn on control shows only where linking works. |
| 69 | * `linkableHere` is the environment's own answer for this session (its link state). |
| 70 | */ |
| 71 | export function resolveT3ConnectTurnOn(input: { |
| 72 | readonly linkableHere: boolean; |
| 73 | readonly cloudConfigured: boolean; |
| 74 | readonly canReadRelay: boolean; |
| 75 | readonly canWriteRelay: boolean; |
| 76 | readonly connectDenial: string | undefined; |
| 77 | }): T3ConnectTurnOn { |
| 78 | if (input.connectDenial !== undefined) return { kind: "policy", reason: input.connectDenial }; |
| 79 | return input.linkableHere && input.cloudConfigured && input.canReadRelay && input.canWriteRelay |
| 80 | ? { kind: "here" } |
| 81 | : { kind: "on-host" }; |
| 82 | } |