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

main
ts 274 lines 10.5 KB
Raw
1 import {
2 parseScopedThreadKey,
3 scopedThreadKey,
4 scopeThreadRef,
5 } from "@t3tools/client-runtime/environment";
6 import type {
7 DeviceServiceState,
8 EnvironmentId,
9 PreviewSessionSnapshot,
10 ScopedThreadRef,
11 ThreadId,
12 } from "@t3tools/contracts";
13 import { AsyncResult } from "effect/reactivity";
14
15 import { getClientSettings } from "../../hooks/useSettings";
16 import {
17 browserMiniPlayerSource,
18 type PreviewMiniPlayerSource,
19 usePreviewMiniPlayerStore,
20 } from "../../previewMiniPlayerStore";
21 import {
22 previewStateAtom,
23 previewThreadRefsAtom,
24 type ThreadPreviewState,
25 } from "../../previewStateStore";
26 import { useRightPanelStore } from "../../rightPanelStore";
27 import { RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY } from "../../rightPanelLayout";
28 import { appAtomRegistry } from "../../rpc/atomRegistry";
29 import { deviceEnvironment } from "../../state/device";
30 import { readEnvironmentSupportsServerBrowser } from "../../state/entities";
31 import { environmentSummaries } from "../../state/presentation";
32
33 /**
34 * Shows browsers an agent or another client revealed for a thread: floating
35 * over chat, or focusing an existing panel tab when the reveal is forced.
36 */
37 export function revealServerTabs(
38 threadRef: ScopedThreadRef,
39 sessions: ReadonlyArray<PreviewSessionSnapshot>,
40 options: { readonly autoShowFloatingPreview: boolean },
41 ): void {
42 if (!readEnvironmentSupportsServerBrowser(threadRef.environmentId)) return;
43 for (const session of sessions) {
44 const force = session.revealRequest?.force === true;
45 if (!options.autoShowFloatingPreview && !force) continue;
46 const surface = useRightPanelStore
47 .getState()
48 .byThreadKey[scopedThreadKey(threadRef)]?.surfaces.find(
49 (surface) => surface.kind === "preview" && surface.resourceId === session.tabId,
50 );
51 if (surface && force) {
52 useRightPanelStore.getState().revealSurface(threadRef, surface.id);
53 } else if (!surface) {
54 usePreviewMiniPlayerStore.getState().open(threadRef, browserMiniPlayerSource(session.tabId));
55 }
56 }
57 }
58
59 /**
60 * Keeps a thread's browser panel tabs and floating browser in step with its
61 * preview sessions. Server-hosted sessions are only complete once the first
62 * list has loaded; before that, a missing session is not a closed one.
63 */
64 export function followPreviewSessions(threadRef: ScopedThreadRef, state: ThreadPreviewState): void {
65 if (readEnvironmentSupportsServerBrowser(threadRef.environmentId) && !state.listLoaded) return;
66 const sessions = Object.values(state.sessions);
67 useRightPanelStore.getState().reconcileBrowserSurfaces(
68 threadRef,
69 sessions.map((session) => session.tabId),
70 new Set(
71 sessions
72 .filter((session) => session.runtime === "server" && session.reveal === false)
73 .map((session) => session.tabId),
74 ),
75 );
76 const miniPlayer = usePreviewMiniPlayerStore.getState();
77 const source = miniPlayer.byThreadKey[scopedThreadKey(threadRef)]?.source;
78 if (source?.kind === "browser" && !state.sessions[source.tabId]) miniPlayer.close(threadRef);
79 }
80
81 /**
82 * Server tabs that are new, or carry a new reveal request, since `previous`.
83 * Nothing is fresh until the first list has loaded: that list is the baseline,
84 * so a reload never reopens previews the user dismissed. A tab this client was
85 * closing comes back without a reveal if the close fails.
86 */
87 function freshlyRevealedServerTabs(
88 previous: ThreadPreviewState,
89 next: ThreadPreviewState,
90 ): ReadonlyArray<PreviewSessionSnapshot> {
91 if (!previous.listLoaded || previous.sessions === next.sessions) return [];
92 return Object.values(next.sessions).filter((session) => {
93 if (session.runtime !== "server" || session.reveal !== true) return false;
94 if (previous.suppressedTabIds.has(session.tabId)) return false;
95 const before = previous.sessions[session.tabId];
96 const requested = session.revealRequest;
97 return requested ? before?.revealRequest?.id !== requested.id : before === undefined;
98 });
99 }
100
101 function followThread(threadKey: string, threadRef: ScopedThreadRef): () => void {
102 const atom = previewStateAtom(threadKey);
103 let previous = appAtomRegistry.get(atom);
104 return appAtomRegistry.subscribe(atom, (next) => {
105 const before = previous;
106 previous = next;
107 // Reveals run first so a revealed tab floats before it gains a panel tab.
108 const revealed = freshlyRevealedServerTabs(before, next);
109 if (revealed.length > 0) {
110 revealServerTabs(threadRef, revealed, {
111 autoShowFloatingPreview: getClientSettings().browserAutoShowFloatingPreview,
112 });
113 }
114 if (before.sessions !== next.sessions || before.listLoaded !== next.listLoaded) {
115 followPreviewSessions(threadRef, next);
116 }
117 });
118 }
119
120 // `openedAt` tells a session apart from one that ended and reopened on the same device while
121 // this client was disconnected.
122 const sessionKey = (session: DeviceServiceState["sessions"][number]) =>
123 `${session.threadId}:${session.hostId}:${session.deviceId}:${session.openedAt}`;
124
125 /**
126 * Per environment: the device sessions seen in the last snapshot. The first
127 * snapshot is a baseline, so persisted tabs restore themselves and existing
128 * sessions never resurrect tabs the user closed. Baselines outlive a
129 * disconnect, so a device an agent opened during the gap still shows.
130 */
131 const deviceSessionBaselines = new Map<EnvironmentId, ReadonlySet<string>>();
132
133 function hasLiveSession(
134 state: DeviceServiceState,
135 threadId: ThreadId,
136 target: { readonly hostId: string; readonly deviceId: string },
137 ): boolean {
138 return state.sessions.some(
139 (session) =>
140 session.threadId === threadId &&
141 session.hostId === target.hostId &&
142 session.deviceId === target.deviceId,
143 );
144 }
145
146 /**
147 * A device the agent opens floats over its thread's chat, or becomes a panel tab
148 * when floating previews are off; sheet layouts get neither. A session without a
149 * device summary yet stays out of the baseline so a later snapshot opens it.
150 * Floating devices whose session ended close here.
151 */
152 export function applyDeviceSnapshot(
153 environmentId: EnvironmentId,
154 state: DeviceServiceState,
155 options: { readonly sheetLayout: boolean; readonly autoShowFloatingPreview: boolean },
156 ): void {
157 const deviceFor = (session: DeviceServiceState["sessions"][number]) =>
158 state.devices.find((entry) => entry.hostId === session.hostId && entry.id === session.deviceId);
159
160 const miniPlayer = usePreviewMiniPlayerStore.getState();
161 for (const [threadKey, entry] of Object.entries(miniPlayer.byThreadKey)) {
162 if (entry.source.kind !== "device") continue;
163 const ref = parseScopedThreadKey(threadKey);
164 if (ref?.environmentId === environmentId && !hasLiveSession(state, ref.threadId, entry.source))
165 miniPlayer.close(ref);
166 }
167
168 const previous = deviceSessionBaselines.get(environmentId);
169 deviceSessionBaselines.set(
170 environmentId,
171 new Set(state.sessions.filter((session) => deviceFor(session) !== undefined).map(sessionKey)),
172 );
173 if (!previous || options.sheetLayout) return;
174 for (const session of state.sessions) {
175 if (previous.has(sessionKey(session))) continue;
176 const device = deviceFor(session);
177 if (!device) continue;
178 const threadRef = scopeThreadRef(environmentId, session.threadId);
179 const target = {
180 hostId: session.hostId,
181 deviceId: session.deviceId,
182 platform: device.platform,
183 name: device.name,
184 };
185 if (options.autoShowFloatingPreview) {
186 usePreviewMiniPlayerStore.getState().open(threadRef, { kind: "device", ...target });
187 continue;
188 }
189 const existing = useRightPanelStore
190 .getState()
191 .byThreadKey[scopedThreadKey(threadRef)]?.surfaces.some(
192 (surface) =>
193 surface.kind === "device" &&
194 surface.target?.hostId === session.hostId &&
195 surface.target.deviceId === session.deviceId,
196 );
197 if (existing) continue;
198 useRightPanelStore.getState().openDevice(threadRef, target, true);
199 }
200 }
201
202 /**
203 * Floats a device panel tab over chat. A tab whose device session already
204 * ended has nothing to stream, so it only closes the panel. Before the first
205 * device snapshot the tab floats, and that snapshot closes it if needed.
206 */
207 export function floatDeviceSurface(
208 threadRef: ScopedThreadRef,
209 source: Extract<PreviewMiniPlayerSource, { kind: "device" }>,
210 state: DeviceServiceState | null,
211 ): void {
212 if (state !== null && !hasLiveSession(state, threadRef.threadId, source)) return;
213 usePreviewMiniPlayerStore.getState().open(threadRef, source);
214 }
215
216 export function resetDeviceSessionBaselinesForTests(): void {
217 deviceSessionBaselines.clear();
218 }
219
220 function watchEnvironmentDevices(environmentId: EnvironmentId): () => void {
221 return appAtomRegistry.subscribe(
222 deviceEnvironment.state({ environmentId, input: {} }),
223 (result) => {
224 const state = AsyncResult.getOrElse(result, () => null);
225 if (state === null) return;
226 applyDeviceSnapshot(environmentId, state, {
227 sheetLayout: window.matchMedia(RIGHT_PANEL_INLINE_LAYOUT_MEDIA_QUERY).matches,
228 autoShowFloatingPreview: getClientSettings().browserAutoShowFloatingPreview,
229 });
230 },
231 { immediate: true },
232 );
233 }
234
235 /**
236 * Follows every thread's browser previews and every connected environment's
237 * device sessions for the life of the page, not just the open thread's: an
238 * agent in a background thread can open either.
239 */
240 export function startAgentOpenedPreviews(): () => void {
241 const followed = new Map<string, () => void>();
242 const followNewThreads = (refs: ReadonlyMap<string, ScopedThreadRef>) => {
243 for (const [threadKey, threadRef] of refs) {
244 if (!followed.has(threadKey)) followed.set(threadKey, followThread(threadKey, threadRef));
245 }
246 };
247 const stopIndex = appAtomRegistry.subscribe(previewThreadRefsAtom, followNewThreads, {
248 immediate: true,
249 });
250 const watchedEnvironments = new Map<EnvironmentId, () => void>();
251 const watchConnectedEnvironments = (environmentIds: ReadonlyArray<EnvironmentId>) => {
252 for (const [environmentId, stop] of watchedEnvironments) {
253 if (environmentIds.includes(environmentId)) continue;
254 stop();
255 watchedEnvironments.delete(environmentId);
256 }
257 for (const environmentId of environmentIds) {
258 if (!watchedEnvironments.has(environmentId)) {
259 watchedEnvironments.set(environmentId, watchEnvironmentDevices(environmentId));
260 }
261 }
262 };
263 const stopEnvironments = appAtomRegistry.subscribe(
264 environmentSummaries.connectedEnvironmentIdsAtom,
265 watchConnectedEnvironments,
266 { immediate: true },
267 );
268 return () => {
269 stopIndex();
270 stopEnvironments();
271 for (const stop of followed.values()) stop();
272 for (const stop of watchedEnvironments.values()) stop();
273 };
274 }