Open History to see the last commit
1import type { DpopFailureReason } from "@t3tools/contracts";
2import {
3 T3CHAT_PLAN_TUNNEL_LIMITS,
4 type RelayEnvironmentStatusResponse,
5 type RelayProtectedError,
6} from "@t3tools/contracts/relay";
7import { mFallback, msg } from "gt-i18n";
8import type { M } from "gt-i18n";
9
10import { type MarkedText, resolveMarkedText } from "../errors/translatable.ts";
11
12export 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. */
18export 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
22export const DPOP_RETRY_HINT = msg("Hint: Try again. If the problem continues, copy the trace ID.");
23
24function 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
30export 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`. */
38export 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 [msg`Relay rejected the environment link proof (${{ reason: error.reason }}).`];
54 case "RelayEnvironmentConnectNotAuthorizedError":
55 // "Not authorized" covers non-auth causes too; surface the reason so a
56 // missing link does not read as a credential problem.
57 if (error.reason === "environment_link_not_found") {
58 return [
59 msg(
60 "Relay has no active link for this environment. If it was deregistered from T3 Connect, link it again on that machine.",
61 ),
62 ];
63 }
64 if (error.reason === "managed_endpoint_allocation_not_ready") {
65 return [msg("The environment's T3 Connect tunnel is still being set up.")];
66 }
67 if (error.reason === "endpoint_provider_not_managed") {
68 return [
69 msg(
70 "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.",
71 ),
72 ];
73 }
74 return error.reason
75 ? [msg`Relay rejected the environment connection request (${{ reason: error.reason }}).`]
76 : [msg("Relay rejected the environment connection request.")];
77 case "RelayEnvironmentEndpointUnavailableError":
78 return [msg`Relay could not reach the environment endpoint (${{ reason: error.reason }}).`];
79 case "RelayEnvironmentEndpointTimedOutError":
80 return [msg("Relay timed out while contacting the environment endpoint.")];
81 case "RelayEnvironmentLinkFailedError":
82 return [msg`Relay could not link the environment (${{ reason: error.reason }}).`];
83 case "RelayEnvironmentLinkUnavailableError":
84 return error.provisioningStage
85 ? [
86 msg`Relay cannot provision the managed endpoint (${{ reason: error.reason }}). Failed stage: ${{ stage: error.provisioningStage }}.`,
87 ]
88 : [msg`Relay cannot provision the managed endpoint (${{ reason: error.reason }}).`];
89 case "RelayEnvironmentLinkLimitExceededError":
90 return error.maxTunnels < T3CHAT_PLAN_TUNNEL_LIMITS.premier
91 ? [
92 msg`Relay refused the link: this account already has its maximum of ${{ maxTunnels: error.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: T3CHAT_PLAN_TUNNEL_LIMITS.pro }} and Premier to ${{ premier: T3CHAT_PLAN_TUNNEL_LIMITS.premier }}.`,
93 ]
94 : [
95 msg`Relay refused the link: this account already has its maximum of ${{ maxTunnels: error.maxTunnels }} managed tunnels. Unlink an environment to free one up.`,
96 ];
97 case "RelayAgentActivityPublishProofExpiredError":
98 return [msg("Relay rejected an expired agent activity publish proof.")];
99 case "RelayAgentActivityPublishProofInvalidError":
100 return [msg`Relay rejected the agent activity publish proof (${{ reason: error.reason }}).`];
101 case "RelayInternalError":
102 return [msg`Relay encountered an internal error (${{ reason: error.reason }}).`];
103 }
104}
105
106export function relayProtectedErrorMessage(
107 error: RelayProtectedError,
108 resolve: M = mFallback,
109): string {
110 return resolveMarkedText(relayProtectedErrorText(error), resolve);
111}
112
113// The relay cuts off a request it cannot finish in time with a 504 that no
114// endpoint declares, so the client sees only the status.
115export const RELAY_GATEWAY_TIMEOUT_MESSAGE = msg(
116 "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.",
117);
118
119export function relayRequestFailureText(error: {
120 readonly relayError?: RelayProtectedError | undefined;
121 readonly status?: number | undefined;
122}): MarkedText | null {
123 if (error.relayError) return relayProtectedErrorText(error.relayError);
124 if (error.status === 504) return [RELAY_GATEWAY_TIMEOUT_MESSAGE];
125 if (error.status !== undefined)
126 return [msg`Relay returned an unexpected HTTP ${{ status: error.status }}.`];
127 return null;
128}
129
130export function relayRequestFailureDetail(
131 error: {
132 readonly relayError?: RelayProtectedError | undefined;
133 readonly status?: number | undefined;
134 },
135 resolve: M = mFallback,
136): string | null {
137 const text = relayRequestFailureText(error);
138 return text === null ? null : resolveMarkedText(text, resolve);
139}
140
141export function relayOfflineReasonMessage(
142 status: Pick<RelayEnvironmentStatusResponse, "offlineReason">,
143 resolve: M = mFallback,
144): string | null {
145 switch (status.offlineReason) {
146 case "tunnel_released":
147 return resolve(
148 msg(
149 "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.",
150 ),
151 );
152 case undefined:
153 return null;
154 }
155}