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

main
ts 118 lines 4.77 KB
Raw
1 import { useAtomSet, useAtomValue } from "@effect/atom-react";
2 import {
3 connectLinkedEnvironment,
4 type LinkedThreadTarget,
5 } from "@t3tools/client-runtime/linked-thread";
6 import type { EnvironmentId, OrchestrationV2Subagent } from "@t3tools/contracts";
7 import { useState } from "react";
8
9 import { connectPairingUrl, reachesEnvironment } from "../connection/onboarding";
10 import { linkedEnvironmentPrefillAtom } from "../features/connection/linkedEnvironmentPrefill";
11 import { buildPairingUrl } from "../features/connection/pairing";
12 import { useEnvironments } from "./environments";
13 import { peerLinkEnvironment, useLinkedEnvironments } from "./peerLinks";
14 import { useEnvironmentQuery } from "./query";
15 import { useAtomCommand } from "./use-atom-command";
16
17 /**
18 * What places a delegated task's thread: this app's own connections, and the links `environmentId`
19 * holds. Links are listed only while a task runs somewhere this app is not connected to.
20 */
21 export function useLinkedThreadReach(
22 environmentId: EnvironmentId,
23 subagents: ReadonlyArray<Pick<OrchestrationV2Subagent, "childThreadId" | "remoteChild">>,
24 ) {
25 const { presentationById } = useEnvironments();
26 const ownConnection = (id: EnvironmentId) => {
27 const own = presentationById.get(id);
28 return own && { connected: own.connection.phase === "connected" };
29 };
30 const links = useLinkedEnvironments(
31 subagents.some(
32 (subagent) =>
33 subagent.childThreadId === null &&
34 subagent.remoteChild !== undefined &&
35 ownConnection(subagent.remoteChild.environmentId)?.connected !== true,
36 )
37 ? environmentId
38 : null,
39 );
40 return { ownConnection, isLinked: (id: EnvironmentId) => links.has(id) };
41 }
42
43 export type LinkedEnvironmentConnectState =
44 | { readonly phase: "idle" | "connecting" }
45 | { readonly phase: "unreachable"; readonly label: string };
46
47 /**
48 * Connects this app to the environment `target` lives in. One step when the linking environment
49 * can get a pairing for this app; otherwise the caller opens the add-environment screen, which
50 * reads what was learned about the machine.
51 */
52 export function useConnectLinkedEnvironment(target: LinkedThreadTarget) {
53 const setPrefill = useAtomSet(linkedEnvironmentPrefillAtom);
54 const mayPair = useAtomValue(
55 peerLinkEnvironment.pairClient.permissionAtom(target.viaEnvironmentId),
56 );
57 const quiet = { reportFailure: false, reportDefect: false } as const;
58 const requestPairing = useAtomCommand(peerLinkEnvironment.pairClient, quiet);
59 const reaches = useAtomCommand(reachesEnvironment, quiet);
60 const pair = useAtomCommand(connectPairingUrl, quiet);
61 const [state, setState] = useState<LinkedEnvironmentConnectState>({ phase: "idle" });
62 const connect = async () => {
63 setState({ phase: "connecting" });
64 const outcome = await connectLinkedEnvironment({
65 label: target.label,
66 requestPairing: async () => {
67 // Without it the linking environment refuses; the user can still pair by hand.
68 if (!mayPair) return null;
69 const result = await requestPairing({
70 environmentId: target.viaEnvironmentId,
71 input: { environmentId: target.environmentId },
72 });
73 return result._tag === "Success" ? result.value : null;
74 },
75 reaches: async (host) => {
76 const result = await reaches({ host, environmentId: target.environmentId });
77 return result._tag === "Success" && result.value;
78 },
79 pair: async ({ host, pairingCode }) => {
80 const result = await pair({
81 pairingUrl: buildPairingUrl(host, pairingCode),
82 expectedEnvironmentId: target.environmentId,
83 });
84 return result._tag === "Success";
85 },
86 });
87 setState(
88 outcome.type === "unreachable"
89 ? { phase: "unreachable", label: outcome.label }
90 : { phase: "idle" },
91 );
92 if (outcome.type === "manual") {
93 setPrefill({ environmentId: target.environmentId, label: outcome.label, host: outcome.host });
94 }
95 return outcome;
96 };
97 return { state, connect };
98 }
99
100 /**
101 * The thread of `target`, streamed through the environment that links to it. The stream runs only
102 * while `active`; what it last showed stays, so coming back to the screen shows it at once.
103 */
104 export function useLinkedThreadView(target: LinkedThreadTarget, active: boolean) {
105 const viewTarget = {
106 environmentId: target.viaEnvironmentId,
107 input: { environmentId: target.environmentId, threadId: target.threadId },
108 };
109 const live = useEnvironmentQuery(active ? peerLinkEnvironment.threadView(viewTarget) : null);
110 const view = useAtomValue(peerLinkEnvironment.lastThreadView(viewTarget));
111 return {
112 view,
113 /** Why the stream is not running: it ended, or it waits to ask again. */
114 error: live.failure ?? view?.error ?? null,
115 /** Asks again now. */
116 retry: live.refresh,
117 };
118 }