Open History to see the last commit
1import type { OrchestrationShellSnapshot } from "@t3tools/contracts";
2import * as Cause from "effect/Cause";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Option from "effect/Option";
7import { HttpClient } from "effect/unstable/http";
8
9import { RemoteEnvironmentAuthorization } from "../authorization/service.ts";
10import type { PreparedConnection } from "../connection/model.ts";
11import { environmentEndpointUrl } from "../environment/endpoint.ts";
12import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
13import { executeAuthenticatedEnvironmentHttpRequest } from "./environmentHttpAuth.ts";
14
15// Long enough for a slow but alive server to finish. On timeout the socket asks
16// the same server for the same full snapshot, so a short deadline only throws
17// the first build away. The socket fallback is for setups where /api fails but
18// /ws works, such as a proxy that blocks /api. A dead server is caught by the
19// socket ping, which drops the session and interrupts this load. The cached
20// shell renders while this runs.
21const DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS = 20_000;
22
23/**
24 * Load the environment shell snapshot (projects + thread shells) over HTTP
25 * instead of as the WebSocket subscription's first frame. The response is
26 * gzip-compressible by the transport and keeps the (potentially large) list off
27 * the socket.
28 */
29export const fetchEnvironmentShellSnapshot = Effect.fn(
30 "clientRuntime.state.fetchEnvironmentShellSnapshot",
31)(function* (input: {
32 readonly prepared: PreparedConnection;
33 readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
34 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
35 readonly timeoutMs?: number;
36}) {
37 return yield* executeAuthenticatedEnvironmentHttpRequest({
38 ...input,
39 group: "orchestration",
40 method: "GET",
41 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"),
42 timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
43 request: ({ client, headers }) => client.shellSnapshot({ headers }),
44 });
45});
46
47/**
48 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
49 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
50 * Decouples the shell state machine from the underlying HTTP + DPoP details and
51 * keeps them out of test contexts.
52 */
53export class ShellSnapshotLoader extends Context.Service<
54 ShellSnapshotLoader,
55 {
56 readonly load: (
57 prepared: PreparedConnection,
58 ) => Effect.Effect<Option.Option<OrchestrationShellSnapshot>>;
59 }
60>()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
61
62export const shellSnapshotLoaderLayer: Layer.Layer<
63 ShellSnapshotLoader,
64 never,
65 HttpClient.HttpClient
66> = Layer.effect(
67 ShellSnapshotLoader,
68 Effect.gen(function* () {
69 const httpClient = yield* HttpClient.HttpClient;
70 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
71 // connections, so the loader must not hard-require it.
72 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
73 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
74 return ShellSnapshotLoader.of({
75 load: (prepared: PreparedConnection) =>
76 fetchEnvironmentShellSnapshot({ prepared, signer, remoteAuthorization }).pipe(
77 Effect.map(Option.some<OrchestrationShellSnapshot>),
78 Effect.provideService(HttpClient.HttpClient, httpClient),
79 Effect.catchCause((cause) =>
80 Effect.logWarning(
81 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
82 ).pipe(
83 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
84 Effect.as(Option.none<OrchestrationShellSnapshot>()),
85 ),
86 ),
87 ),
88 });
89 }),
90);