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// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
16// fallback for long. The cached shell renders while this runs.
17const DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS = 6_000;
18
19/**
20 * Load the environment shell snapshot (projects + thread shells) over HTTP
21 * instead of as the WebSocket subscription's first frame. The response is
22 * gzip-compressible by the transport and keeps the (potentially large) list off
23 * the socket.
24 */
25export const fetchEnvironmentShellSnapshot = Effect.fn(
26 "clientRuntime.state.fetchEnvironmentShellSnapshot",
27)(function* (input: {
28 readonly prepared: PreparedConnection;
29 readonly signer: Option.Option<ManagedRelayDpopSigner["Service"]>;
30 readonly remoteAuthorization?: Option.Option<RemoteEnvironmentAuthorization["Service"]>;
31 readonly timeoutMs?: number;
32}) {
33 return yield* executeAuthenticatedEnvironmentHttpRequest({
34 ...input,
35 method: "GET",
36 url: (httpBaseUrl) => environmentEndpointUrl(httpBaseUrl, "/api/orchestration/shell"),
37 timeoutMs: input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
38 request: ({ client, headers }) => client.orchestration.shellSnapshot({ headers }),
39 });
40});
41
42/**
43 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
44 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
45 * Decouples the shell state machine from the underlying HTTP + DPoP details and
46 * keeps them out of test contexts.
47 */
48export class ShellSnapshotLoader extends Context.Service<
49 ShellSnapshotLoader,
50 {
51 readonly load: (
52 prepared: PreparedConnection,
53 ) => Effect.Effect<Option.Option<OrchestrationShellSnapshot>>;
54 }
55>()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
56
57export const shellSnapshotLoaderLayer: Layer.Layer<
58 ShellSnapshotLoader,
59 never,
60 HttpClient.HttpClient
61> = Layer.effect(
62 ShellSnapshotLoader,
63 Effect.gen(function* () {
64 const httpClient = yield* HttpClient.HttpClient;
65 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
66 // connections, so the loader must not hard-require it.
67 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
68 const remoteAuthorization = yield* Effect.serviceOption(RemoteEnvironmentAuthorization);
69 return ShellSnapshotLoader.of({
70 load: (prepared: PreparedConnection) =>
71 fetchEnvironmentShellSnapshot({ prepared, signer, remoteAuthorization }).pipe(
72 Effect.map(Option.some<OrchestrationShellSnapshot>),
73 Effect.provideService(HttpClient.HttpClient, httpClient),
74 Effect.catchCause((cause) =>
75 Effect.logWarning(
76 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
77 ).pipe(
78 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
79 Effect.as(Option.none<OrchestrationShellSnapshot>()),
80 ),
81 ),
82 ),
83 });
84 }),
85);