Open History to see the last commit
1import type { OrchestrationV2ShellSnapshot } 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 type { PreparedConnection } from "../connection/model.ts";
10import { environmentEndpointUrl } from "../environment/endpoint.ts";
11import { ManagedRelayDpopSigner } from "../relay/managedRelay.ts";
12import { executeEnvironmentHttpRequest, makeEnvironmentHttpApiClient } from "../rpc/http.ts";
13import {
14 buildEnvironmentAuthHeaders,
15 withEnvironmentCredentials,
16 withOrchestrationProtocolHeader,
17} from "./environmentHttpAuth.ts";
18
19// Bounded so a pathologically slow endpoint cannot block the (cheaper) socket
20// fallback for long. The cached shell renders while this runs.
21const DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS = 6_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 timeoutMs?: number;
35}) {
36 const requestUrl = environmentEndpointUrl(input.prepared.httpBaseUrl, "/api/orchestration/shell");
37 const client = yield* makeEnvironmentHttpApiClient(input.prepared.httpBaseUrl);
38 const headers = yield* buildEnvironmentAuthHeaders(
39 input.prepared.httpAuthorization,
40 "GET",
41 requestUrl,
42 input.signer,
43 );
44 return yield* executeEnvironmentHttpRequest(
45 requestUrl,
46 input.timeoutMs ?? DEFAULT_SHELL_SNAPSHOT_TIMEOUT_MS,
47 withEnvironmentCredentials(
48 input.prepared.httpAuthorization,
49 client.orchestration.shellSnapshot({ headers: withOrchestrationProtocolHeader(headers) }),
50 ),
51 );
52});
53
54/**
55 * Loads the environment shell snapshot over HTTP, returning `Option.none()` when
56 * it cannot be loaded (so the caller falls back to the socket-embedded snapshot).
57 * Decouples the shell state machine from the underlying HTTP + DPoP details and
58 * keeps them out of test contexts.
59 */
60export class ShellSnapshotLoader extends Context.Service<
61 ShellSnapshotLoader,
62 {
63 readonly load: (
64 prepared: PreparedConnection,
65 ) => Effect.Effect<Option.Option<OrchestrationV2ShellSnapshot>>;
66 }
67>()("@t3tools/client-runtime/state/shellSnapshotHttp/ShellSnapshotLoader") {}
68
69export const shellSnapshotLoaderLayer: Layer.Layer<
70 ShellSnapshotLoader,
71 never,
72 HttpClient.HttpClient
73> = Layer.effect(
74 ShellSnapshotLoader,
75 Effect.gen(function* () {
76 const httpClient = yield* HttpClient.HttpClient;
77 // Resolve the DPoP signer optionally: it is only needed for relay/DPoP
78 // connections, so the loader must not hard-require it.
79 const signer = yield* Effect.serviceOption(ManagedRelayDpopSigner);
80 return ShellSnapshotLoader.of({
81 load: (prepared: PreparedConnection) =>
82 fetchEnvironmentShellSnapshot({ prepared, signer }).pipe(
83 Effect.map(Option.some<OrchestrationV2ShellSnapshot>),
84 Effect.provideService(HttpClient.HttpClient, httpClient),
85 Effect.catchCause((cause) =>
86 Effect.logWarning(
87 "Could not load the environment shell snapshot over HTTP; using the socket snapshot instead.",
88 ).pipe(
89 Effect.annotateLogs({ cause: Cause.pretty(cause) }),
90 Effect.as(Option.none<OrchestrationV2ShellSnapshot>()),
91 ),
92 ),
93 ),
94 });
95 }),
96);