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 { ClientConnectionMethod, EnvironmentId } from "@t3tools/contracts"; |
| 2 | import type { RelayProtectedError } from "@t3tools/contracts/relay"; |
| 3 | import type { ManagedRelayClientError } from "../relay/managedRelay.ts"; |
| 4 | import { msg } from "gt-i18n"; |
| 5 | import { dpopFailureText, relayProtectedErrorText } from "../relay/errorPresentation.ts"; |
| 6 | import type { RemoteEnvironmentAuthError } from "../authorization/remote.ts"; |
| 7 | import { NETWORK_BLOCKING_HINT } from "../errors/network.ts"; |
| 8 | import { |
| 9 | detailFromError, |
| 10 | errorI18nMessage, |
| 11 | markedTextEnglish, |
| 12 | translatableDetail, |
| 13 | } from "../errors/translatable.ts"; |
| 14 | import { |
| 15 | ConnectionBlockedError, |
| 16 | type ConnectionAttemptError, |
| 17 | ConnectionTransientError, |
| 18 | } from "./model.ts"; |
| 19 | |
| 20 | export function profileMissingError(connectionId: string): ConnectionBlockedError { |
| 21 | return new ConnectionBlockedError({ |
| 22 | reason: "configuration", |
| 23 | ...translatableDetail( |
| 24 | msg("Connection profile {connectionId} is unavailable.", { connectionId }), |
| 25 | ), |
| 26 | hop: "device", |
| 27 | }); |
| 28 | } |
| 29 | |
| 30 | export function credentialMissingError(connectionId: string): ConnectionBlockedError { |
| 31 | return new ConnectionBlockedError({ |
| 32 | reason: "authentication", |
| 33 | ...translatableDetail( |
| 34 | msg("Connection credential {connectionId} is unavailable.", { connectionId }), |
| 35 | ), |
| 36 | hop: "device", |
| 37 | }); |
| 38 | } |
| 39 | |
| 40 | /** The host itself answered and refused this device's credential, which pairing again fixes. */ |
| 41 | export function isHostCredentialRefusal(error: ConnectionAttemptError): boolean { |
| 42 | return ( |
| 43 | error._tag === "ConnectionBlockedError" && |
| 44 | error.hop === "server" && |
| 45 | (error.reason === "authentication" || error.reason === "permission") && |
| 46 | error.failureCause !== "clock_skew" |
| 47 | ); |
| 48 | } |
| 49 | |
| 50 | export function environmentMismatchError(input: { |
| 51 | readonly expected: EnvironmentId; |
| 52 | readonly actual: EnvironmentId; |
| 53 | }): ConnectionBlockedError { |
| 54 | return new ConnectionBlockedError({ |
| 55 | reason: "configuration", |
| 56 | ...translatableDetail( |
| 57 | msg("Connected environment {actual} does not match {expected}.", { |
| 58 | actual: input.actual, |
| 59 | expected: input.expected, |
| 60 | }), |
| 61 | ), |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | function relayProtectedError(error: RelayProtectedError): ConnectionAttemptError { |
| 66 | const text = relayProtectedErrorText(error); |
| 67 | const detail = markedTextEnglish(text); |
| 68 | const i18nMessage = text; |
| 69 | const traceId = error.traceId; |
| 70 | switch (error._tag) { |
| 71 | case "RelayAuthInvalidError": |
| 72 | return new ConnectionBlockedError({ |
| 73 | reason: "authentication", |
| 74 | detail, |
| 75 | i18nMessage, |
| 76 | traceId, |
| 77 | hop: "relay", |
| 78 | ...(error.dpopFailureReason === "time_window" ? { failureCause: "clock_skew" } : {}), |
| 79 | }); |
| 80 | case "RelayEnvironmentLinkProofExpiredError": |
| 81 | case "RelayAgentActivityPublishProofExpiredError": |
| 82 | case "RelayAgentActivityPublishProofInvalidError": |
| 83 | return new ConnectionBlockedError({ |
| 84 | reason: "authentication", |
| 85 | detail, |
| 86 | i18nMessage, |
| 87 | traceId, |
| 88 | hop: "relay", |
| 89 | }); |
| 90 | case "RelayEnvironmentConnectNotAuthorizedError": |
| 91 | // A host swapping in a new tunnel leaves its allocation unready until |
| 92 | // the tunnel is up, and nothing on the client changes when it is. |
| 93 | if (error.reason === "managed_endpoint_allocation_not_ready") { |
| 94 | return new ConnectionTransientError({ |
| 95 | reason: "endpoint-unavailable", |
| 96 | detail, |
| 97 | i18nMessage, |
| 98 | traceId, |
| 99 | hop: "tunnel", |
| 100 | failureCause: "tunnel_pending", |
| 101 | }); |
| 102 | } |
| 103 | if (error.reason === "endpoint_provider_not_managed") { |
| 104 | return new ConnectionBlockedError({ |
| 105 | reason: "permission", |
| 106 | detail, |
| 107 | i18nMessage, |
| 108 | traceId, |
| 109 | hop: "tunnel", |
| 110 | failureCause: "tunnel_not_enabled", |
| 111 | }); |
| 112 | } |
| 113 | return new ConnectionBlockedError({ |
| 114 | reason: "permission", |
| 115 | detail, |
| 116 | i18nMessage, |
| 117 | traceId, |
| 118 | hop: "relay", |
| 119 | ...(error.reason === "environment_link_not_found" ? { failureCause: "not_linked" } : {}), |
| 120 | }); |
| 121 | case "RelayEnvironmentLinkProofInvalidError": |
| 122 | case "RelayEnvironmentLinkLimitExceededError": |
| 123 | return new ConnectionBlockedError({ |
| 124 | reason: "permission", |
| 125 | detail, |
| 126 | i18nMessage, |
| 127 | traceId, |
| 128 | hop: "relay", |
| 129 | }); |
| 130 | case "RelayEnvironmentEndpointTimedOutError": |
| 131 | return new ConnectionTransientError({ |
| 132 | reason: "timeout", |
| 133 | detail, |
| 134 | i18nMessage, |
| 135 | traceId, |
| 136 | hop: "tunnel", |
| 137 | failureCause: "timeout", |
| 138 | }); |
| 139 | case "RelayEnvironmentEndpointUnavailableError": { |
| 140 | const endpointFailure = |
| 141 | error.endpointFailure ?? |
| 142 | (error.reason === "endpoint_response_invalid" ? "invalid_response" : undefined); |
| 143 | return new ConnectionTransientError({ |
| 144 | reason: "endpoint-unavailable", |
| 145 | detail, |
| 146 | i18nMessage, |
| 147 | traceId, |
| 148 | // The tunnel carried the request when T3 Code is what failed to answer. |
| 149 | hop: |
| 150 | endpointFailure === "origin_unreachable" || endpointFailure === "invalid_response" |
| 151 | ? "server" |
| 152 | : "tunnel", |
| 153 | via: "relay", |
| 154 | ...(endpointFailure ? { failureCause: endpointFailure } : {}), |
| 155 | ...(error.endpointStatus ? { httpStatus: error.endpointStatus } : {}), |
| 156 | }); |
| 157 | } |
| 158 | case "RelayEnvironmentLinkUnavailableError": |
| 159 | return new ConnectionTransientError({ |
| 160 | reason: "endpoint-unavailable", |
| 161 | detail, |
| 162 | i18nMessage, |
| 163 | traceId, |
| 164 | hop: "tunnel", |
| 165 | }); |
| 166 | case "RelayEnvironmentLinkFailedError": |
| 167 | case "RelayInternalError": |
| 168 | return new ConnectionTransientError({ |
| 169 | reason: "relay-unavailable", |
| 170 | detail, |
| 171 | i18nMessage, |
| 172 | traceId, |
| 173 | hop: "relay", |
| 174 | failureCause: "relay_error", |
| 175 | }); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | export function mapManagedRelayError(error: ManagedRelayClientError): ConnectionAttemptError { |
| 180 | switch (error._tag) { |
| 181 | case "ManagedRelayRequestFailedError": |
| 182 | if (error.relayError) { |
| 183 | return relayProtectedError(error.relayError); |
| 184 | } |
| 185 | return new ConnectionTransientError({ |
| 186 | reason: "relay-unavailable", |
| 187 | ...detailFromError(error), |
| 188 | hop: "relay", |
| 189 | // A status means the relay answered, so this device did reach it. |
| 190 | ...(error.status !== undefined |
| 191 | ? { httpStatus: error.status, failureCause: "relay_error" } |
| 192 | : {}), |
| 193 | ...(error.traceId ? { traceId: error.traceId } : {}), |
| 194 | }); |
| 195 | case "ManagedRelayRequestTimeoutError": |
| 196 | return new ConnectionTransientError({ |
| 197 | reason: "timeout", |
| 198 | ...detailFromError(error), |
| 199 | hop: "relay", |
| 200 | }); |
| 201 | case "ManagedRelayUrlInvalidError": |
| 202 | return new ConnectionBlockedError({ |
| 203 | reason: "configuration", |
| 204 | ...detailFromError(error), |
| 205 | hop: "relay", |
| 206 | }); |
| 207 | case "ManagedRelayAccessTokenScopesUnexpectedError": |
| 208 | return new ConnectionBlockedError({ |
| 209 | reason: "permission", |
| 210 | ...detailFromError(error), |
| 211 | hop: "relay", |
| 212 | }); |
| 213 | case "ManagedRelayDpopKeyLoadError": |
| 214 | case "ManagedRelayTokenProofCreationError": |
| 215 | case "ManagedRelayRequestProofCreationError": |
| 216 | return new ConnectionBlockedError({ |
| 217 | reason: "authentication", |
| 218 | ...detailFromError(error), |
| 219 | hop: "device", |
| 220 | }); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | export function mapRemoteEnvironmentError( |
| 225 | error: RemoteEnvironmentAuthError, |
| 226 | connectionMethod: ClientConnectionMethod = "direct", |
| 227 | ): ConnectionAttemptError { |
| 228 | const withNetworkHint = (cause: { readonly message: string }) => { |
| 229 | if (connectionMethod !== "relay") return detailFromError(cause); |
| 230 | const text = errorI18nMessage(cause); |
| 231 | return text === undefined |
| 232 | ? { detail: `${cause.message} ${NETWORK_BLOCKING_HINT}` } |
| 233 | : translatableDetail(...text, NETWORK_BLOCKING_HINT); |
| 234 | }; |
| 235 | // Over T3 Connect the relay already reached the host, so a request that never |
| 236 | // arrives was blocked between this device and the tunnel. |
| 237 | const unreachableHop = connectionMethod === "relay" ? "tunnel" : "server"; |
| 238 | const via = connectionMethod === "relay" ? "relay" : "direct"; |
| 239 | switch (error._tag) { |
| 240 | case "EnvironmentAuthInvalidError": |
| 241 | return new ConnectionBlockedError({ |
| 242 | reason: "authentication", |
| 243 | ...translatableDetail(msg("The environment credential is invalid.")), |
| 244 | traceId: error.traceId, |
| 245 | hop: "server", |
| 246 | via, |
| 247 | }); |
| 248 | case "EnvironmentScopeRequiredError": |
| 249 | case "EnvironmentOperationForbiddenError": |
| 250 | return new ConnectionBlockedError({ |
| 251 | reason: "permission", |
| 252 | ...translatableDetail( |
| 253 | msg("The environment credential does not grant the required access."), |
| 254 | ), |
| 255 | traceId: error.traceId, |
| 256 | hop: "server", |
| 257 | via, |
| 258 | }); |
| 259 | case "EnvironmentRequestInvalidError": |
| 260 | return new ConnectionBlockedError({ |
| 261 | reason: "configuration", |
| 262 | ...translatableDetail(msg("The environment rejected the authentication request.")), |
| 263 | traceId: error.traceId, |
| 264 | hop: "server", |
| 265 | via, |
| 266 | }); |
| 267 | case "EnvironmentResourceNotFoundError": |
| 268 | return new ConnectionBlockedError({ |
| 269 | reason: "configuration", |
| 270 | ...translatableDetail(msg("The environment endpoint could not be found.")), |
| 271 | traceId: error.traceId, |
| 272 | hop: "server", |
| 273 | via, |
| 274 | }); |
| 275 | case "RemoteEnvironmentAuthTimeoutError": |
| 276 | return new ConnectionTransientError({ |
| 277 | reason: "timeout", |
| 278 | ...withNetworkHint(error), |
| 279 | hop: unreachableHop, |
| 280 | via, |
| 281 | }); |
| 282 | case "RemoteEnvironmentAuthFetchError": |
| 283 | return new ConnectionTransientError({ |
| 284 | reason: "network", |
| 285 | ...withNetworkHint(error), |
| 286 | hop: unreachableHop, |
| 287 | via, |
| 288 | }); |
| 289 | case "EnvironmentConnectApprovalPendingError": |
| 290 | return new ConnectionTransientError({ |
| 291 | reason: "approval-pending", |
| 292 | ...translatableDetail(msg("This device is waiting for approval on the host.")), |
| 293 | traceId: error.traceId, |
| 294 | hop: "server", |
| 295 | via, |
| 296 | failureCause: "approval_pending", |
| 297 | }); |
| 298 | case "EnvironmentInternalError": |
| 299 | return new ConnectionTransientError({ |
| 300 | reason: "remote-unavailable", |
| 301 | ...translatableDetail(msg("The environment could not authorize the connection.")), |
| 302 | traceId: error.traceId, |
| 303 | hop: "server", |
| 304 | via, |
| 305 | }); |
| 306 | case "RemoteEnvironmentAuthInvalidJsonError": |
| 307 | case "RemoteEnvironmentAuthUndeclaredStatusError": |
| 308 | return new ConnectionTransientError({ |
| 309 | reason: "remote-unavailable", |
| 310 | ...detailFromError(error), |
| 311 | hop: "server", |
| 312 | via, |
| 313 | }); |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * An older environment server reports a DPoP clock failure as the same generic |
| 319 | * invalid-credential response as other failures, so keep the compatibility hint |
| 320 | * cautious when the server omits the category. |
| 321 | */ |
| 322 | export function mapRemoteDpopEnvironmentError( |
| 323 | error: RemoteEnvironmentAuthError, |
| 324 | connectionMethod: "relay" | "direct" = "relay", |
| 325 | ): ConnectionAttemptError { |
| 326 | if (error._tag === "EnvironmentAuthInvalidError" && error.reason === "invalid_credential") { |
| 327 | return new ConnectionBlockedError({ |
| 328 | reason: "authentication", |
| 329 | ...translatableDetail( |
| 330 | ...dpopFailureText(msg("The environment credential is invalid."), error.dpopFailureReason), |
| 331 | ), |
| 332 | traceId: error.traceId, |
| 333 | hop: "server", |
| 334 | via: connectionMethod, |
| 335 | ...(error.dpopFailureReason === "time_window" ? { failureCause: "clock_skew" } : {}), |
| 336 | }); |
| 337 | } |
| 338 | return mapRemoteEnvironmentError(error, connectionMethod); |
| 339 | } |