The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Layer from "effect/Layer"; |
| 3 | import * as ManagedRuntime from "effect/ManagedRuntime"; |
| 4 | |
| 5 | import { layerRemoteHttpClient } from "@t3tools/client-runtime/rpc"; |
| 6 | |
| 7 | import * as Dpop from "../features/cloud/dpop"; |
| 8 | import * as ManagedRelayLayer from "../features/cloud/managedRelayLayer"; |
| 9 | import { resolveCloudPublicConfig } from "../features/cloud/publicConfig"; |
| 10 | import * as Tracing from "../features/observability/tracing"; |
| 11 | import * as NativeWebSocket from "../native/nativeWebSocket"; |
| 12 | import * as Persistence from "../persistence/layer"; |
| 13 | import { disposeOnFoundationReplace, type FoundationHotModule } from "./foundation-fast-refresh"; |
| 14 | import { whenProtectedDataAvailable } from "./protectedData"; |
| 15 | |
| 16 | declare const module: { readonly hot?: FoundationHotModule } | undefined; |
| 17 | |
| 18 | function configuredRelayUrl(): string { |
| 19 | return resolveCloudPublicConfig().relay.url ?? "http://relay.invalid"; |
| 20 | } |
| 21 | |
| 22 | const layerHttpClient = layerRemoteHttpClient(fetch); |
| 23 | |
| 24 | type RuntimeLayerSource = |
| 25 | | ReturnType<typeof ManagedRelayLayer.layer> |
| 26 | | typeof NativeWebSocket.layer |
| 27 | | typeof Dpop.layer |
| 28 | | typeof layerHttpClient |
| 29 | | typeof Persistence.layer |
| 30 | | typeof Tracing.layer; |
| 31 | |
| 32 | const layerRuntime = Layer.merge( |
| 33 | ManagedRelayLayer.layer(configuredRelayUrl()), |
| 34 | NativeWebSocket.layer, |
| 35 | ).pipe( |
| 36 | Layer.provideMerge(Dpop.layer), |
| 37 | Layer.provideMerge(layerHttpClient), |
| 38 | Layer.provideMerge(Tracing.layer.pipe(Layer.provide(layerHttpClient))), |
| 39 | Layer.provideMerge(Persistence.layer), |
| 40 | // These layers read the keychain and the database while they build, and a |
| 41 | // failed build is kept for the life of the process. |
| 42 | Layer.provide(Layer.effectDiscard(Effect.promise(whenProtectedDataAvailable))), |
| 43 | ); |
| 44 | |
| 45 | export const runtime: ManagedRuntime.ManagedRuntime< |
| 46 | Layer.Success<RuntimeLayerSource>, |
| 47 | Layer.Error<RuntimeLayerSource> |
| 48 | > = ManagedRuntime.make(layerRuntime); |
| 49 | |
| 50 | export const layer: Layer.Layer< |
| 51 | Layer.Success<RuntimeLayerSource>, |
| 52 | Layer.Error<RuntimeLayerSource> |
| 53 | > = Layer.effectContext(runtime.contextEffect); |
| 54 | |
| 55 | disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.hot, () => |
| 56 | runtime.dispose(), |
| 57 | ); |