Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

linkEnvironment.ts

346 lines12.0 KB

1import * as Data from "effect/Data";
2import * as Effect from "effect/Effect";
3import * as Option from "effect/Option";
4import * as Schema from "effect/Schema";
5import * as Stream from "effect/Stream";
6import { HttpClient } from "effect/http";
7import {
8 EnvironmentCloudEndpointUnavailableError,
9 type EnvironmentCloudLinkStateResult,
10 EnvironmentHttpBadRequestError,
11 EnvironmentHttpConflictError,
12 EnvironmentHttpForbiddenError,
13 EnvironmentHttpInternalServerError,
14 EnvironmentHttpUnauthorizedError,
15 EnvironmentId,
16 WS_METHODS,
17} from "@t3tools/contracts";
18import {
19 type RelayEnvironmentLinkResponse,
20 type RelayManagedEndpointProviderKind,
21} from "@t3tools/contracts/relay";
22import { EnvironmentRegistry } from "@t3tools/client-runtime/connection";
23import { request, runStream } from "@t3tools/client-runtime/rpc";
24import { makeEnvironmentHttpApiClient } from "@t3tools/client-runtime/rpc";
25import { ManagedRelay, relayRequestFailureDetail } from "@t3tools/client-runtime/relay";
26​
27import * as PrimaryEnvironmentHttpLayer from "../environments/primary/httpLayer";
28import { resolveCloudPublicConfig } from "./publicConfig";
29import {
30 finishRelayClientInstall,
31 reportRelayClientInstallProgress,
32 requestRelayClientInstallConfirmation,
33} from "./relayClientInstallDialog";
34​
35function relayUrl(): string | null {
36 return resolveCloudPublicConfig().relayUrl;
38​
39export class CloudEnvironmentLinkError extends Data.TaggedError("CloudEnvironmentLinkError")<{
40 readonly message: string;
41 readonly cause?: unknown;
42 readonly traceId?: string;
43}> {}
44​
45const relayClientRpcError = (message: string) => (cause: unknown) =>
46 new CloudEnvironmentLinkError({
47 message,
48 cause,
49 });
50​
51function ensureRelayClientAvailable(
52 environmentId: EnvironmentId,
53): Effect.Effect<void, CloudEnvironmentLinkError, EnvironmentRegistry.EnvironmentRegistry> {
54 return Effect.gen(function* () {
55 const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
56 const status = yield* registry
57 .run(environmentId, request(WS_METHODS.cloudGetRelayClientStatus, {}))
58 .pipe(Effect.mapError(relayClientRpcError("Could not check relay client availability.")));
59 if (status.status === "available") return;
60 if (status.status === "unsupported") {
61 return yield* new CloudEnvironmentLinkError({
62 message: `T3 Code cannot install the relay client automatically on ${status.platform}-${status.arch}.`,
63 });
64 }
65​
66 const confirmed = yield* Effect.tryPromise({
67 try: () => requestRelayClientInstallConfirmation(status.version),
68 catch: relayClientRpcError("Could not confirm relay client installation."),
69 });
70 if (!confirmed) {
71 return yield* new CloudEnvironmentLinkError({
72 message: "Relay client installation was cancelled.",
73 });
74 }
75​
76 const installed = yield* registry
77 .runStream(
78 environmentId,
79 runStream(WS_METHODS.cloudInstallRelayClient, {}).pipe(
80 Stream.tap((event) => Effect.sync(() => reportRelayClientInstallProgress(event))),
81 ),
82 )
83 .pipe(
84 Stream.runLast,
85 Effect.mapError(relayClientRpcError("Could not install the relay client.")),
86 Effect.ensuring(Effect.sync(finishRelayClientInstall)),
87 );
88 if (Option.isNone(installed) || installed.value.type !== "complete") {
89 return yield* new CloudEnvironmentLinkError({
90 message: "The relay client install completed without a final status.",
91 });
92 }
93 const installedStatus = installed.value.status;
94 if (installedStatus.status !== "available") {
95 return yield* new CloudEnvironmentLinkError({
96 message:
97 installedStatus.status === "unsupported"
98 ? `T3 Code cannot install the relay client automatically on ${installedStatus.platform}-${installedStatus.arch}.`
99 : "The relay client is still unavailable after installation.",
100 });
101 }
102 });
104​
105const isEnvironmentCloudApiError = Schema.is(
106 Schema.Union([
107 EnvironmentHttpBadRequestError,
108 EnvironmentHttpUnauthorizedError,
109 EnvironmentHttpForbiddenError,
110 EnvironmentHttpConflictError,
111 EnvironmentHttpInternalServerError,
112 EnvironmentCloudEndpointUnavailableError,
113 ]),
114);
115​
116function decodedRelayClientError(message: string) {
117 return (cause: ManagedRelay.ManagedRelayClientError) => {
118 const failed = cause._tag === "ManagedRelayRequestFailedError" ? cause : undefined;
119 const traceId = failed?.traceId;
120 const detail = failed ? relayRequestFailureDetail(failed) : null;
121 return new CloudEnvironmentLinkError({
122 message: detail ? `${message}: ${detail}` : message,
123 cause,
124 ...(traceId ? { traceId } : {}),
125 });
126 };
128​
129function findEnvironmentCloudApiError(cause: unknown): { readonly message: string } | null {
130 if (isEnvironmentCloudApiError(cause)) {
131 return cause;
132 }
133 if (typeof cause !== "object" || cause === null) {
134 return null;
135 }
136 return "cause" in cause ? findEnvironmentCloudApiError(cause.cause) : null;
138​
139const environmentApiError = (message: string) => (cause: unknown) => {
140 const environmentError = findEnvironmentCloudApiError(cause);
141 return new CloudEnvironmentLinkError({
142 message: environmentError
143 ? `${message.replace(/[.:]$/, "")}: ${environmentError.message}`
144 : message,
145 cause,
146 });
147};
148​
149function endpointOrigin(httpBaseUrl: string) {
150 const url = new URL(httpBaseUrl);
151 return {
152 localHttpHost: "127.0.0.1",
153 localHttpPort: Number(url.port || (url.protocol === "https:" ? 443 : 80)),
154 };
156​
157const MANAGED_ENDPOINT_PROVIDER_KIND =
158 "cloudflare_tunnel" satisfies RelayManagedEndpointProviderKind;
159​
160function ensureLinkedEnvironmentMatches(input: {
161 readonly expectedEnvironmentId: string;
162 readonly expectedProviderKind: RelayManagedEndpointProviderKind;
163 readonly link: RelayEnvironmentLinkResponse;
164}): Effect.Effect<void, CloudEnvironmentLinkError> {
165 if (input.link.environmentId !== input.expectedEnvironmentId) {
166 return new CloudEnvironmentLinkError({
167 message: "Relay returned credentials for a different environment.",
168 });
169 }
170 if (input.link.endpoint.providerKind !== input.expectedProviderKind) {
171 return new CloudEnvironmentLinkError({
172 message: "Relay returned credentials for a different endpoint provider.",
173 });
174 }
175 return Effect.void;
177​
178export interface CloudLinkTarget {
179 readonly environmentId: string;
180 readonly label: string;
181 readonly httpBaseUrl: string;
182 readonly wsBaseUrl: string;
184​
185export type CloudLinkState = EnvironmentCloudLinkStateResult;
186​
187export function readPrimaryCloudLinkState(input: {
188 readonly target: CloudLinkTarget;
189}): Effect.Effect<CloudLinkState | null, CloudEnvironmentLinkError, HttpClient.HttpClient> {
190 return Effect.gen(function* () {
191 const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
192 return yield* client.connect
193 .linkState({ headers: {} })
194 .pipe(Effect.mapError(environmentApiError("Could not read environment cloud link state.")));
195 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layer));
197​
198export function updatePrimaryCloudPreferences(input: {
199 readonly target: CloudLinkTarget;
200 readonly publishAgentActivity: boolean;
201 readonly holdWebhooksWhileOffline?: boolean;
202}): Effect.Effect<CloudLinkState, CloudEnvironmentLinkError, HttpClient.HttpClient> {
203 return Effect.gen(function* () {
204 const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
205 const { target: _target, ...payload } = input;
206 return yield* client.connect
207 .preferences({
208 headers: {},
209 payload,
210 })
211 .pipe(
212 Effect.mapError(environmentApiError("Could not update environment cloud preferences.")),
213 );
214 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layer));
216​
217export function unlinkPrimaryEnvironmentFromCloud(input: {
218 readonly target: CloudLinkTarget;
219 readonly clerkToken: string | null;
220}): Effect.Effect<
221 void,
222 CloudEnvironmentLinkError,
223 HttpClient.HttpClient | ManagedRelay.ManagedRelayClient
224> {
225 return Effect.gen(function* () {
226 const client = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
227 yield* client.connect
228 .unlink({ headers: {} })
229 .pipe(Effect.mapError(environmentApiError("Could not unlink the environment from cloud.")));
230​
231 const configuredRelayUrl = relayUrl();
232 if (configuredRelayUrl && input.clerkToken) {
233 const relayClient = yield* ManagedRelay.ManagedRelayClient;
234 yield* relayClient
235 .unlinkEnvironment({
236 clerkToken: input.clerkToken,
237 environmentId: EnvironmentId.make(input.target.environmentId),
238 })
239 .pipe(
240 Effect.catch((cause) =>
241 Effect.logWarning("Could not revoke cloud environment link after local unlink.", {
242 cause,
243 }),
244 ),
245 );
246 }
247 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layer));
249​
250export type CloudLinkMode = "managed" | "publish_only";
251​
252const PUBLISH_ONLY_PROVIDER_KIND = "manual" satisfies RelayManagedEndpointProviderKind;
253​
254export function linkPrimaryEnvironmentToCloud(input: {
255 readonly target: CloudLinkTarget;
256 readonly clerkToken: string;
257 readonly mode?: CloudLinkMode;
258}): Effect.Effect<
259 void,
260 CloudEnvironmentLinkError,
261 EnvironmentRegistry.EnvironmentRegistry | HttpClient.HttpClient | ManagedRelay.ManagedRelayClient
262> {
263 return Effect.gen(function* () {
264 const configuredRelayUrl = relayUrl();
265 if (!configuredRelayUrl) {
266 return yield* new CloudEnvironmentLinkError({
267 message: "T3CODE_RELAY_URL is not configured.",
268 });
269 }
270 const managedTunnelsEnabled = (input.mode ?? "managed") === "managed";
271 const providerKind = managedTunnelsEnabled
272 ? MANAGED_ENDPOINT_PROVIDER_KIND
273 : PUBLISH_ONLY_PROVIDER_KIND;
274 const relayClient = yield* ManagedRelay.ManagedRelayClient;
275 const environmentClient = yield* makeEnvironmentHttpApiClient(input.target.httpBaseUrl);
276 if (managedTunnelsEnabled) {
277 yield* ensureRelayClientAvailable(EnvironmentId.make(input.target.environmentId));
278 }
279​
280 const challenge = yield* relayClient
281 .createEnvironmentLinkChallenge({
282 clerkToken: input.clerkToken,
283 payload: {
284 notificationsEnabled: true,
285 liveActivitiesEnabled: true,
286 managedTunnelsEnabled,
287 },
288 })
289 .pipe(
290 Effect.mapError(
291 decodedRelayClientError(
292 `${configuredRelayUrl}/v1/client/environment-link-challenges failed`,
293 ),
294 ),
295 );
296 const proof = yield* environmentClient.connect
297 .linkProof({
298 headers: {},
299 payload: {
300 challenge: challenge.challenge,
301 relayIssuer: configuredRelayUrl,
302 endpoint: {
303 httpBaseUrl: input.target.httpBaseUrl,
304 wsBaseUrl: input.target.wsBaseUrl,
305 providerKind,
306 },
307 origin: endpointOrigin(input.target.httpBaseUrl),
308 },
309 })
310 .pipe(Effect.mapError(environmentApiError("Could not obtain environment link proof.")));
311 const link = yield* relayClient
312 .linkEnvironment({
313 clerkToken: input.clerkToken,
314 payload: {
315 proof,
316 notificationsEnabled: true,
317 liveActivitiesEnabled: true,
318 managedTunnelsEnabled,
319 },
320 })
321 .pipe(
322 Effect.mapError(
323 decodedRelayClientError(`${configuredRelayUrl}/v1/client/environment-links failed`),
324 ),
325 );
326 yield* ensureLinkedEnvironmentMatches({
327 expectedEnvironmentId: input.target.environmentId,
328 expectedProviderKind: providerKind,
329 link,
330 });
331​
332 yield* environmentClient.connect
333 .relayConfig({
334 headers: {},
335 payload: {
336 relayUrl: configuredRelayUrl,
337 relayIssuer: link.relayIssuer,
338 cloudUserId: link.cloudUserId,
339 environmentCredential: link.environmentCredential,
340 cloudMintPublicKey: link.cloudMintPublicKey,
341 endpointRuntime: link.endpointRuntime,
342 },
343 })
344 .pipe(Effect.mapError(environmentApiError("Could not configure environment relay access.")));
345 }).pipe(Effect.provide(PrimaryEnvironmentHttpLayer.layer));