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

main
tsx 538 lines 19.7 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import {
3 type DesktopBridge,
4 type DesktopPendingSnapShot,
5 type DesktopSnapShotEvent,
6 PROVIDER_SEND_TURN_MAX_ATTACHMENTS,
7 PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
8 type ScopedThreadRef,
9 } from "@t3tools/contracts";
10 import { msg } from "gt-react";
11 import { useEffect, useEffectEvent, useRef, useState } from "react";
12
13 import { m } from "../../i18n/messages";
14
15 import {
16 type ComposerImageAttachment,
17 type DraftId,
18 type PersistedComposerImageAttachment,
19 useComposerDraftStore,
20 } from "../../composerDraftStore";
21 import {
22 LAST_USED_PROJECT_STORAGE_KEY,
23 lastUsedProjectKeySchema,
24 useHandleNewThread,
25 } from "../../hooks/useHandleNewThread";
26 import { scopedProjectKey } from "@t3tools/client-runtime/environment";
27 import { useLocalStorage } from "../../hooks/useLocalStorage";
28 import { useStableCallback } from "../../hooks/useStableCallback";
29 import { getClientSettings, useClientSettings } from "../../hooks/useSettings";
30 import { readThreadShell } from "../../state/entities";
31 import { resolveThreadActionProjectRef } from "../../lib/chatThreadActions";
32 import {
33 beginSnapShotAnimation,
34 dismissAllSnapShotAnimations,
35 dismissSnapShotAnimation,
36 finishSnapShotAnimation,
37 getPendingSnapShotAnimations,
38 updateSnapShotAnimationSource,
39 waitForSnapShotAnimationDestination,
40 } from "../../lib/snapShotAnimation";
41 import { resizeSnapShotSource } from "../../lib/snapShotSource";
42 import { playSnapShotSound } from "../../lib/snapShotSound";
43 import { dispatchSnapShotComposerFocus, getDesktopSnapShotBridge } from "../../lib/desktopSnapShot";
44 import {
45 countQuestionRequestAttachments,
46 openQuestionAttachmentDraft,
47 } from "../../questionAttachments";
48 import { stackedThreadToast, toastManager } from "../ui/toast";
49 import { readComposerImageBytes } from "../chat/composerAttachmentFiles";
50
51 type CaptureTarget = DraftId | ScopedThreadRef;
52 type CaptureFailure = "missing-project" | "delivery";
53
54 function isSameCaptureTarget(a: CaptureTarget, b: CaptureTarget): boolean {
55 if (typeof a === "string" || typeof b === "string") return a === b;
56 return a.environmentId === b.environmentId && a.threadId === b.threadId;
57 }
58
59 export function resolveExistingSnapShotTarget(
60 target: CaptureTarget,
61 routeThreadRef: ScopedThreadRef | null,
62 ): CaptureTarget | null {
63 const store = useComposerDraftStore.getState();
64 if (typeof target === "string") {
65 const draftSession = store.getDraftSession(target);
66 if (draftSession?.promotedTo) return draftSession.promotedTo;
67 return draftSession ? target : null;
68 }
69 const targetIsCurrentRoute =
70 routeThreadRef !== null &&
71 routeThreadRef.environmentId === target.environmentId &&
72 routeThreadRef.threadId === target.threadId;
73 return targetIsCurrentRoute ||
74 store.getDraftSessionByRef(target) !== null ||
75 readThreadShell(target) !== null
76 ? target
77 : null;
78 }
79
80 // Pin absence too: a later question must never claim an earlier capture.
81 export function resolveSnapShotAttachmentTarget(
82 pins: Map<string, DraftId | null>,
83 id: string,
84 target: CaptureTarget,
85 ): CaptureTarget {
86 const open = typeof target === "string" ? null : openQuestionAttachmentDraft(target);
87 if (!pins.has(id)) pins.set(id, open?.draftId ?? null);
88 const pinned = pins.get(id);
89 return pinned && pinned === open?.draftId ? pinned : target;
90 }
91
92 const NEXT_PAINT_FALLBACK_MS = 100;
93
94 export async function beginSnapShotAnimationWhenReady(
95 id: string,
96 target: Promise<CaptureTarget | null>,
97 pendingStarts: Set<string>,
98 ): Promise<void> {
99 pendingStarts.add(id);
100 try {
101 const resolvedTarget = await target;
102 if (pendingStarts.delete(id) && resolvedTarget) {
103 beginSnapShotAnimation(id, resolvedTarget);
104 }
105 } finally {
106 pendingStarts.delete(id);
107 }
108 }
109
110 export function dismissFailedSnapShot(
111 id: string | undefined,
112 soundedIds: Set<string>,
113 pendingStarts: Set<string>,
114 ): void {
115 if (id) {
116 pendingStarts.delete(id);
117 soundedIds.delete(id);
118 void dismissSnapShotAnimation(id);
119 } else {
120 pendingStarts.clear();
121 soundedIds.clear();
122 dismissAllSnapShotAnimations();
123 }
124 }
125
126 function resolveSnapShotTargetOnce(
127 resolutionRef: { current: Promise<CaptureTarget | null> | null },
128 resolveTarget: () => Promise<CaptureTarget | null>,
129 ): Promise<CaptureTarget | null> {
130 if (resolutionRef.current) return resolutionRef.current;
131 const resolution = resolveTarget().finally(() => {
132 if (resolutionRef.current === resolution) resolutionRef.current = null;
133 });
134 resolutionRef.current = resolution;
135 return resolution;
136 }
137
138 export function resolveSnapShotDeliveryTarget(
139 targets: Map<string, Promise<CaptureTarget | null>>,
140 id: string,
141 resolveTarget: () => Promise<CaptureTarget | null>,
142 ): Promise<CaptureTarget | null> {
143 const existing = targets.get(id);
144 if (existing) return existing;
145 const target = resolveTarget().catch(() => null);
146 targets.set(id, target);
147 void target.then((resolved) => {
148 if (!resolved && targets.get(id) === target) targets.delete(id);
149 });
150 return target;
151 }
152
153 async function afterNextPaint(): Promise<void> {
154 await new Promise<void>((resolve) => {
155 const fallback = window.setTimeout(resolve, NEXT_PAINT_FALLBACK_MS);
156 requestAnimationFrame(() => {
157 requestAnimationFrame(() => {
158 window.clearTimeout(fallback);
159 resolve();
160 });
161 });
162 });
163 }
164
165 export async function deliverSnapShot(
166 bridge: DesktopBridge,
167 item: DesktopPendingSnapShot,
168 resolveTarget: () => CaptureTarget,
169 ): Promise<void> {
170 const store = useComposerDraftStore.getState();
171 updateSnapShotAnimationSource(item.id, item.source);
172 const capture = await bridge.readSnapShot(item.id);
173 const { compressImageToByteLimit, dataUrlToFile, readFileAsDataUrl } =
174 await import("../../lib/imageCompression");
175 const original = dataUrlToFile(capture.dataUrl, capture.name, capture.mimeType);
176 const compressed = await compressImageToByteLimit(original, PROVIDER_SEND_TURN_MAX_IMAGE_BYTES);
177 if (!compressed.ok) {
178 finishSnapShotAnimation(item.id);
179 throw new Error(m(msg("The captured window is too large to attach.")));
180 }
181 const file = compressed.file;
182 await readComposerImageBytes(file);
183 const source = resizeSnapShotSource(capture.source, compressed.imageSize);
184 const dataUrl = compressed.recompressed ? await readFileAsDataUrl(file) : capture.dataUrl;
185 const target = resolveTarget();
186 const existingImage = store.getComposerDraft(target)?.images.find(({ id }) => id === capture.id);
187 const image: ComposerImageAttachment = existingImage ?? {
188 type: "image",
189 id: capture.id,
190 name: file.name,
191 mimeType: file.type,
192 sizeBytes: file.size,
193 previewUrl: dataUrl,
194 file,
195 source,
196 };
197 if (!existingImage) {
198 const questionAttachmentCount =
199 typeof target === "string" ? countQuestionRequestAttachments(target) : null;
200 // Only question drafts may bypass addImage's thread-session gate.
201 const added =
202 questionAttachmentCount === null
203 ? store.addImage(target, image)
204 : questionAttachmentCount < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&
205 store.addImages(target, [image]).includes(capture.id);
206 if (!added) throw new Error(m(msg("Remove an attachment, then try this capture again.")));
207 }
208 const persisted: PersistedComposerImageAttachment = {
209 id: capture.id,
210 name: file.name,
211 mimeType: file.type,
212 sizeBytes: file.size,
213 dataUrl,
214 source,
215 };
216 await store.appendPersistedAttachments(target, [image], [persisted]);
217 if (!store.getComposerDraft(target)?.persistedAttachments.some(({ id }) => id === capture.id)) {
218 throw new Error(m(msg("The captured window could not be saved to the draft.")));
219 }
220
221 // Paint the attachment before removing the capture overlay to avoid missing frames at landing.
222 if (getPendingSnapShotAnimations().some((animation) => animation.id === capture.id)) {
223 await afterNextPaint();
224 await waitForSnapShotAnimationDestination(capture.id).catch(() => undefined);
225 finishSnapShotAnimation(capture.id);
226 await afterNextPaint();
227 }
228 await bridge.acknowledgeSnapShot(capture.id);
229 dispatchSnapShotComposerFocus();
230 }
231
232 export function SnapShotCoordinator() {
233 const {
234 activeDraftThread,
235 activeThread,
236 defaultProjectRef,
237 defaultProjectFallbackReady,
238 handleNewThread,
239 routeDraftId,
240 routeThreadRef,
241 } = useHandleNewThread();
242 const [lastUsedProjectKey, setLastUsedProjectKey] = useLocalStorage(
243 LAST_USED_PROJECT_STORAGE_KEY,
244 null,
245 lastUsedProjectKeySchema,
246 );
247 const currentProject = activeThread ?? activeDraftThread;
248 const currentProjectKey = currentProject
249 ? scopedProjectKey({
250 environmentId: currentProject.environmentId,
251 projectId: currentProject.projectId,
252 })
253 : null;
254 const rememberProject = useEffectEvent((projectKey: string) => {
255 if (projectKey !== lastUsedProjectKey) setLastUsedProjectKey(projectKey);
256 });
257 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Persist route changes without overwriting project choices from other tabs.
258 useEffect(() => {
259 if (currentProjectKey) rememberProject(currentProjectKey);
260 }, [currentProjectKey]);
261 const captureSound = useClientSettings((settings) =>
262 settings.snapShotPlaySound ? settings.snapShotSound : null,
263 );
264 const animateCaptures = useClientSettings((settings) => settings.snapShotAnimations);
265 const enabled = useClientSettings((settings) => settings.snapShotEnabled);
266 const captureTargetsRef = useRef(new Map<string, Promise<CaptureTarget | null>>());
267 const questionPinsRef = useRef(new Map<string, DraftId | null>());
268 const targetResolutionRef = useRef<Promise<CaptureTarget | null> | null>(null);
269 const drainingRef = useRef<Promise<void> | null>(null);
270 const rerunRequestedRef = useRef(false);
271 const soundedCaptureIdsRef = useRef(new Set<string>());
272 const reportedCaptureFailuresRef = useRef(new Map<string, Set<CaptureFailure>>());
273 const pendingAnimationStartsRef = useRef(new Set<string>());
274
275 // Captures go to the last thread or draft on screen, even from routes that show neither.
276 const currentTarget = routeThreadRef ?? routeDraftId;
277 const [lastRouteTarget, setLastRouteTarget] = useState(currentTarget);
278 if (
279 currentTarget &&
280 (lastRouteTarget === null || !isSameCaptureTarget(currentTarget, lastRouteTarget))
281 ) {
282 setLastRouteTarget(currentTarget);
283 }
284 // Reuse the resolved target until navigation so captures in one drain share a draft.
285 const settledTargetRef = useRef<{
286 readonly routeTarget: CaptureTarget | null;
287 readonly target: CaptureTarget | null;
288 } | null>(null);
289 const readLastRouteTarget = useStableCallback(() => lastRouteTarget);
290 // Scope pending resolutions to their starting route so navigation cannot inherit another route's draft.
291 const readSettledTarget = (routeTarget: CaptureTarget | null) => {
292 const settled = settledTargetRef.current;
293 return settled && settled.routeTarget === routeTarget ? settled.target : routeTarget;
294 };
295 const settleTarget = (routeTarget: CaptureTarget | null, target: CaptureTarget | null) => {
296 settledTargetRef.current = { routeTarget, target };
297 };
298
299 const resolveTarget = async (): Promise<CaptureTarget | null> => {
300 const routeTarget = readLastRouteTarget();
301 const lastTarget = readSettledTarget(routeTarget);
302 if (lastTarget) {
303 const existingTarget = resolveExistingSnapShotTarget(lastTarget, routeThreadRef);
304 if (existingTarget) {
305 settleTarget(routeTarget, existingTarget);
306 return existingTarget;
307 }
308 settleTarget(routeTarget, null);
309 }
310 const projectRef = resolveThreadActionProjectRef({
311 activeDraftThread,
312 activeThread: activeThread ?? undefined,
313 defaultProjectRef,
314 handleNewThread,
315 });
316 if (!projectRef) return null;
317 const created = await handleNewThread(projectRef);
318 if (!created) return null;
319 settleTarget(routeTarget, created.draftId);
320 return created.draftId;
321 };
322
323 const resolveCaptureTarget = () => resolveSnapShotTargetOnce(targetResolutionRef, resolveTarget);
324
325 const playCaptureSound = (id: string) => {
326 if (!captureSound || soundedCaptureIdsRef.current.has(id)) return;
327 soundedCaptureIdsRef.current.add(id);
328 try {
329 playSnapShotSound(captureSound);
330 } catch {}
331 };
332
333 const reportOnce = (
334 id: string,
335 failure: CaptureFailure,
336 toast: Parameters<typeof toastManager.add>[0],
337 ) => {
338 if (!getClientSettings().snapShotEnabled) return;
339 const failures = reportedCaptureFailuresRef.current.get(id) ?? new Set<CaptureFailure>();
340 if (failures.has(failure)) return;
341 failures.add(failure);
342 reportedCaptureFailuresRef.current.set(id, failures);
343 toastManager.add(toast);
344 };
345
346 const drain = async () => {
347 if (!getClientSettings().snapShotEnabled) return;
348 const bridge = getDesktopSnapShotBridge();
349 if (!bridge) return;
350 if (drainingRef.current) {
351 rerunRequestedRef.current = true;
352 return drainingRef.current;
353 }
354
355 const operation = (async () => {
356 do {
357 rerunRequestedRef.current = false;
358 const pending = await bridge.listPendingSnapShots();
359 for (const item of pending) {
360 if (!getClientSettings().snapShotEnabled) return;
361 playCaptureSound(item.id);
362 const capturedTarget = await resolveSnapShotDeliveryTarget(
363 captureTargetsRef.current,
364 item.id,
365 resolveCaptureTarget,
366 );
367 if (!getClientSettings().snapShotEnabled) return;
368 const target = capturedTarget
369 ? resolveExistingSnapShotTarget(capturedTarget, routeThreadRef)
370 : null;
371 if (!target) {
372 if (!defaultProjectFallbackReady) continue;
373 await dismissSnapShotAnimation(item.id);
374 reportOnce(
375 item.id,
376 "missing-project",
377 stackedThreadToast({
378 type: "error",
379 title: m(msg("Snapshot taken, but no project is available")),
380 description: m(msg("Add a project, then capture the window again.")),
381 }),
382 );
383 continue;
384 }
385
386 try {
387 // Pending captures may have arrived without a requested event; pin before reading.
388 resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target);
389 await deliverSnapShot(bridge, item, () =>
390 resolveSnapShotAttachmentTarget(questionPinsRef.current, item.id, target),
391 );
392 captureTargetsRef.current.delete(item.id);
393 questionPinsRef.current.delete(item.id);
394 soundedCaptureIdsRef.current.delete(item.id);
395 reportedCaptureFailuresRef.current.delete(item.id);
396 } catch (error) {
397 await dismissSnapShotAnimation(item.id);
398 reportOnce(
399 item.id,
400 "delivery",
401 stackedThreadToast({
402 type: "error",
403 title: m(msg("Snapshot failed")),
404 description: m(
405 msg("Capture {id}: {error}", {
406 id: item.id,
407 error: thrownErrorMessage(error, m) ?? m(msg("Try the capture again.")),
408 }),
409 ),
410 }),
411 );
412 }
413 }
414 } while (rerunRequestedRef.current && getClientSettings().snapShotEnabled);
415 })()
416 .catch((error: unknown) => {
417 dismissAllSnapShotAnimations();
418 if (!getClientSettings().snapShotEnabled) return;
419 toastManager.add(
420 stackedThreadToast({
421 type: "error",
422 title: m(msg("Snapshot failed")),
423 description: thrownErrorMessage(error, m) ?? m(msg("Try the capture again.")),
424 }),
425 );
426 })
427 .finally(() => {
428 drainingRef.current = null;
429 });
430 drainingRef.current = operation;
431 return operation;
432 };
433
434 const drainPending = useEffectEvent(() => drain());
435 const hasDefaultProject = defaultProjectRef !== null;
436 const handleSnapShotEvent = useEffectEvent(
437 (bridge: DesktopBridge, event: DesktopSnapShotEvent) => {
438 if (!getClientSettings().snapShotEnabled) return;
439 switch (event.type) {
440 case "requested": {
441 const current = readSettledTarget(readLastRouteTarget());
442 const target = current ? resolveExistingSnapShotTarget(current, routeThreadRef) : null;
443 // Creating a new draft would navigate the renderer before a self-capture finishes.
444 if (target) {
445 resolveSnapShotAttachmentTarget(questionPinsRef.current, event.id, target);
446 void resolveSnapShotDeliveryTarget(captureTargetsRef.current, event.id, () =>
447 Promise.resolve(target),
448 );
449 } else if (!questionPinsRef.current.has(event.id)) {
450 questionPinsRef.current.set(event.id, null);
451 }
452 return;
453 }
454 case "started": {
455 playCaptureSound(event.id);
456 const target = resolveSnapShotDeliveryTarget(
457 captureTargetsRef.current,
458 event.id,
459 resolveCaptureTarget,
460 ).then(
461 (target) =>
462 target && resolveSnapShotAttachmentTarget(questionPinsRef.current, event.id, target),
463 );
464 if (animateCaptures && !window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
465 void beginSnapShotAnimationWhenReady(
466 event.id,
467 target,
468 pendingAnimationStartsRef.current,
469 );
470 }
471 return;
472 }
473 case "ready":
474 void drain();
475 return;
476 case "failed": {
477 if (event.id) {
478 captureTargetsRef.current.delete(event.id);
479 questionPinsRef.current.delete(event.id);
480 }
481 dismissFailedSnapShot(
482 event.id,
483 soundedCaptureIdsRef.current,
484 pendingAnimationStartsRef.current,
485 );
486 void bridge.getSnapShotState().then((state) => {
487 if (!getClientSettings().snapShotEnabled) return;
488 toastManager.add(
489 stackedThreadToast({
490 type: "error",
491 title: m(msg("Snapshot failed")),
492 description: state.message ?? m(msg("Try the capture again.")),
493 }),
494 );
495 });
496 return;
497 }
498 case "shortcut-changed":
499 return;
500 }
501 },
502 );
503 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to SnapShot events from the desktop bridge and drains pending ones while enabled
504 useEffect(() => {
505 const bridge = getDesktopSnapShotBridge();
506 if (
507 !bridge ||
508 !enabled ||
509 (!defaultProjectFallbackReady && !hasDefaultProject && !lastRouteTarget)
510 )
511 return;
512 void Promise.resolve(drainingRef.current).then(() => drainPending());
513 return bridge.onSnapShotEvent((event) => handleSnapShotEvent(bridge, event));
514 }, [enabled, defaultProjectFallbackReady, hasDefaultProject, lastRouteTarget]);
515
516 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- attaches window blur, focus and visibilitychange listeners to dismiss or drain SnapShot animations
517 useEffect(() => {
518 const dismissOnBlur = () => {
519 pendingAnimationStartsRef.current.clear();
520 dismissAllSnapShotAnimations();
521 };
522 const drainOnFocus = () => void drainPending();
523 const handleVisibilityChange = () => {
524 if (document.visibilityState === "hidden") dismissOnBlur();
525 else void drainPending();
526 };
527 window.addEventListener("blur", dismissOnBlur);
528 window.addEventListener("focus", drainOnFocus);
529 document.addEventListener("visibilitychange", handleVisibilityChange);
530 return () => {
531 window.removeEventListener("blur", dismissOnBlur);
532 window.removeEventListener("focus", drainOnFocus);
533 document.removeEventListener("visibilitychange", handleVisibilityChange);
534 };
535 }, []);
536
537 return null;
538 }