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 { DpopFailureReason } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | T3CHAT_PLAN_TUNNEL_LIMITS, |
| 4 | type RelayEnvironmentStatusResponse, |
| 5 | type RelayProtectedError, |
| 6 | } from "@t3tools/contracts/relay"; |
| 7 | import { mFallback, msg } from "gt-i18n"; |
| 8 | import type { MFunctionType } from "gt-i18n/types"; |
| 9 | |
| 10 | import { type MarkedText, resolveMarkedText } from "../errors/translatable.ts"; |
| 11 | |
| 12 | export const DPOP_CLOCK_HINT = msg( |
| 13 | "Hint: Check that automatic date and time is enabled on both devices, then try again.", |
| 14 | ); |
| 15 | |
| 16 | /** Older servers omit the DPoP category, but newer servers can also omit it for |
| 17 | * a credential failure that happens after proof verification. */ |
| 18 | export const DPOP_UNKNOWN_HINT = msg( |
| 19 | "Hint: Try again. If it still fails, clock skew may be the cause; check that automatic date and time is enabled on both devices.", |
| 20 | ); |
| 21 | |
| 22 | export const DPOP_RETRY_HINT = msg("Hint: Try again. If the problem continues, copy the trace ID."); |
| 23 | |
| 24 | function dpopFailureHint(reason: DpopFailureReason | undefined): string { |
| 25 | if (reason === "time_window") return DPOP_CLOCK_HINT; |
| 26 | if (reason === undefined) return DPOP_UNKNOWN_HINT; |
| 27 | return DPOP_RETRY_HINT; |
| 28 | } |
| 29 | |
| 30 | export function dpopFailureText( |
| 31 | message: string, |
| 32 | reason: DpopFailureReason | undefined, |
| 33 | ): MarkedText { |
| 34 | return [message, dpopFailureHint(reason)]; |
| 35 | } |
| 36 | |
| 37 | /** The relay's refusal as marked sentences, for errors that carry `i18nMessage`. */ |
| 38 | export function relayProtectedErrorText(error: RelayProtectedError): MarkedText { |
| 39 | switch (error._tag) { |
| 40 | case "RelayAuthInvalidError": |
| 41 | switch (error.reason) { |
| 42 | case "missing_bearer": |
| 43 | case "invalid_bearer": |
| 44 | return [msg("Relay rejected the cloud session token.")]; |
| 45 | case "invalid_dpop": |
| 46 | return dpopFailureText(msg("Relay rejected the DPoP proof."), error.dpopFailureReason); |
| 47 | case "not_authorized": |
| 48 | return [msg("Relay rejected the authenticated request.")]; |
| 49 | } |
| 50 | case "RelayEnvironmentLinkProofExpiredError": |
| 51 | return [msg("Relay rejected an expired environment link proof.")]; |
| 52 | case "RelayEnvironmentLinkProofInvalidError": |
| 53 | return [ |
| 54 | msg("Relay rejected the environment link proof ({reason}).", { reason: error.reason }), |
| 55 | ]; |
| 56 | case "RelayEnvironmentConnectNotAuthorizedError": |
| 57 | // "Not authorized" covers non-auth causes too; surface the reason so a |
| 58 | // missing link does not read as a credential problem. |
| 59 | if (error.reason === "environment_link_not_found") { |
| 60 | return [ |
| 61 | msg( |
| 62 | "Relay has no active link for this environment. If it was deregistered from T3 Connect, link it again on that machine.", |
| 63 | ), |
| 64 | ]; |
| 65 | } |
| 66 | if (error.reason === "managed_endpoint_allocation_not_ready") { |
| 67 | return [msg("The environment's T3 Connect tunnel is still being set up.")]; |
| 68 | } |
| 69 | if (error.reason === "endpoint_provider_not_managed") { |
| 70 | return [ |
| 71 | msg( |
| 72 | "This environment is linked only to publish agent activity, so it cannot accept connections. On that computer, open Settings > Sharing & access and turn T3 Connect on.", |
| 73 | ), |
| 74 | ]; |
| 75 | } |
| 76 | return error.reason |
| 77 | ? [ |
| 78 | msg("Relay rejected the environment connection request ({reason}).", { |
| 79 | reason: error.reason, |
| 80 | }), |
| 81 | ] |
| 82 | : [msg("Relay rejected the environment connection request.")]; |
| 83 | case "RelayEnvironmentEndpointUnavailableError": |
| 84 | return [ |
| 85 | msg("Relay could not reach the environment endpoint ({reason}).", { reason: error.reason }), |
| 86 | ]; |
| 87 | case "RelayEnvironmentEndpointTimedOutError": |
| 88 | return [msg("Relay timed out while contacting the environment endpoint.")]; |
| 89 | case "RelayEnvironmentLinkFailedError": |
| 90 | return [msg("Relay could not link the environment ({reason}).", { reason: error.reason })]; |
| 91 | case "RelayEnvironmentLinkUnavailableError": |
| 92 | return error.provisioningStage |
| 93 | ? [ |
| 94 | msg("Relay cannot provision the managed endpoint ({reason}). Failed stage: {stage}.", { |
| 95 | reason: error.reason, |
| 96 | stage: error.provisioningStage, |
| 97 | }), |
| 98 | ] |
| 99 | : [ |
| 100 | msg("Relay cannot provision the managed endpoint ({reason}).", { |
| 101 | reason: error.reason, |
| 102 | }), |
| 103 | ]; |
| 104 | case "RelayEnvironmentLinkLimitExceededError": |
| 105 | return error.maxTunnels < T3CHAT_PLAN_TUNNEL_LIMITS.premier |
| 106 | ? [ |
| 107 | msg( |
| 108 | "Relay refused the link: this account already has its maximum of {maxTunnels} managed tunnels. Unlink an environment, or link a paid T3 Chat account in your T3 Connect account settings on web or desktop. Pro raises the default limit to {pro} and Premier to {premier}.", |
| 109 | { |
| 110 | maxTunnels: error.maxTunnels, |
| 111 | pro: T3CHAT_PLAN_TUNNEL_LIMITS.pro, |
| 112 | premier: T3CHAT_PLAN_TUNNEL_LIMITS.premier, |
| 113 | }, |
| 114 | ), |
| 115 | ] |
| 116 | : [ |
| 117 | msg( |
| 118 | "Relay refused the link: this account already has its maximum of {maxTunnels} managed tunnels. Unlink an environment to free one up.", |
| 119 | { maxTunnels: error.maxTunnels }, |
| 120 | ), |
| 121 | ]; |
| 122 | case "RelayAgentActivityPublishProofExpiredError": |
| 123 | return [msg("Relay rejected an expired agent activity publish proof.")]; |
| 124 | case "RelayAgentActivityPublishProofInvalidError": |
| 125 | return [ |
| 126 | msg("Relay rejected the agent activity publish proof ({reason}).", { |
| 127 | reason: error.reason, |
| 128 | }), |
| 129 | ]; |
| 130 | case "RelayInternalError": |
| 131 | return [msg("Relay encountered an internal error ({reason}).", { reason: error.reason })]; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | export function relayProtectedErrorMessage( |
| 136 | error: RelayProtectedError, |
| 137 | resolve: MFunctionType = mFallback, |
| 138 | ): string { |
| 139 | return resolveMarkedText(relayProtectedErrorText(error), resolve); |
| 140 | } |
| 141 | |
| 142 | // The relay cuts off a request it cannot finish in time with a 504 that no |
| 143 | // endpoint declares, so the client sees only the status. |
| 144 | export const RELAY_GATEWAY_TIMEOUT_MESSAGE = msg( |
| 145 | "Relay timed out before it finished the request (HTTP 504). Try again in a few minutes. If it keeps failing, copy the trace ID when you report it.", |
| 146 | ); |
| 147 | |
| 148 | export function relayRequestFailureText(error: { |
| 149 | readonly relayError?: RelayProtectedError | undefined; |
| 150 | readonly status?: number | undefined; |
| 151 | }): MarkedText | null { |
| 152 | if (error.relayError) return relayProtectedErrorText(error.relayError); |
| 153 | if (error.status === 504) return [RELAY_GATEWAY_TIMEOUT_MESSAGE]; |
| 154 | if (error.status !== undefined) |
| 155 | return [msg("Relay returned an unexpected HTTP {status}.", { status: error.status })]; |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | export function relayRequestFailureDetail( |
| 160 | error: { |
| 161 | readonly relayError?: RelayProtectedError | undefined; |
| 162 | readonly status?: number | undefined; |
| 163 | }, |
| 164 | resolve: MFunctionType = mFallback, |
| 165 | ): string | null { |
| 166 | const text = relayRequestFailureText(error); |
| 167 | return text === null ? null : resolveMarkedText(text, resolve); |
| 168 | } |
| 169 | |
| 170 | export function relayOfflineReasonMessage( |
| 171 | status: Pick<RelayEnvironmentStatusResponse, "offlineReason">, |
| 172 | resolve: MFunctionType = mFallback, |
| 173 | ): string | null { |
| 174 | switch (status.offlineReason) { |
| 175 | case "tunnel_released": |
| 176 | return resolve( |
| 177 | msg( |
| 178 | "Offline for a while, so its T3 Connect tunnel was removed. Start T3 Code on that computer and update it to the latest version to reconnect.", |
| 179 | ), |
| 180 | ); |
| 181 | case undefined: |
| 182 | return null; |
| 183 | } |
| 184 | } |