The best way to control Claude Code, Codex and more from any computer or phone

main
ts 72 lines 2.35 KB
Raw
1 import { Connection } from "@t3tools/client-runtime/connection";
2 import { ShellSnapshotLoader } from "@t3tools/client-runtime/state/shell";
3 import {
4 BoundedThreadSnapshotLoader,
5 ThreadHistoryController,
6 } from "@t3tools/client-runtime/state/threads";
7 import { PullRequestDiffLoader } from "@t3tools/client-runtime/state/pull-requests";
8 import * as Effect from "effect/Effect";
9 import * as Layer from "effect/Layer";
10 import { Atom } from "effect/reactivity";
11
12 import { isGuestPage } from "../guestPage";
13
14 import * as Runtime from "../lib/runtime";
15 import * as BackgroundActivityReporter from "../lib/backgroundActivityReporter";
16 import * as ConnectionPlatform from "./platform";
17
18 const layerProvidedConnectionPlatform = ConnectionPlatform.layer.pipe(Layer.provide(Runtime.layer));
19
20 const layerSnapshotLoader = Layer.mergeAll(
21 BoundedThreadSnapshotLoader.layer,
22 ShellSnapshotLoader.layer,
23 ThreadHistoryController.layer,
24 PullRequestDiffLoader.layer,
25 );
26
27 type ConnectionLayerSource =
28 | typeof Connection.layer
29 | typeof layerSnapshotLoader
30 | typeof Runtime.layer
31 | typeof ConnectionPlatform.layer
32 | typeof BackgroundActivityReporter.layerObserver
33 | typeof BackgroundActivityReporter.layer;
34
35 const layerProvidedClientConnection = layerSnapshotLoader.pipe(
36 Layer.provideMerge(
37 Connection.layerWithOptions({
38 environmentThemes: true,
39 usageLimitSources: true,
40 usageLimitsCommand: true,
41 }),
42 ),
43 Layer.provideMerge(
44 Layer.mergeAll(
45 Runtime.layer,
46 layerProvidedConnectionPlatform,
47 BackgroundActivityReporter.layerObserver,
48 ),
49 ),
50 );
51
52 const layerConnection = BackgroundActivityReporter.layer.pipe(
53 Layer.provideMerge(layerProvidedClientConnection),
54 );
55
56 /**
57 * A guest has no session here. Building the connection would open the page origin's environment
58 * with whatever cookie this browser holds, plus every environment saved in it, so on the guest
59 * page the runtime fails to build and every atom on it keeps its empty value.
60 */
61 const layerForPage = Layer.unwrap(
62 Effect.suspend((): Effect.Effect<typeof layerConnection> =>
63 isGuestPage()
64 ? Effect.die(new Error("The guest page has no environment connection."))
65 : Effect.succeed(layerConnection),
66 ),
67 );
68
69 export const connectionAtomRuntime: Atom.AtomRuntime<
70 Layer.Success<ConnectionLayerSource>,
71 Layer.Error<ConnectionLayerSource>
72 > = Atom.runtime(layerForPage);