fix(web): unlock the composer when preview capture fails #9127
Mergedt3dotgg(GitHub profile, opens in a new tab)merged 9 commits into
main fromrefs/pull/9127/headmerged by t3dotgg(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/desktop/src/preview/Manager.ts lines 344 to 348OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -331,3 +337,12 @@331337 cause,332338 }),333339 }).pipe(340+ Effect.timeoutOrElse({341+ duration: ANNOTATION_SCREENSHOT_TIMEOUT,342+ orElse: () =>343+ Effect.fail(344+ new PreviewOperationError({345+ operation: "captureAnnotationScreenshot",346+ tabId,347+ webContentsId: wc.id,348+ cause: new Error(`capturePage exceeded ${ANNOTATION_SCREENSHOT_TIMEOUT}`),
- apps/desktop/src/preview/Manager.ts line 2510Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -2449,3 +2477,7 @@24492477 if (isMainFrame) settle(null);24502478 };24512479 const registerPickElement = Effect.fn("PreviewManager.registerPickElement")(function* () {2480+ // Two picks on one tab can overlap. Take the slot, settle whoever2481+ // held it, and only then claim it, so the session we push out never2482+ // leaves its renderer awaiting a pick that no longer exists.2483+ const replaced = yield* Ref.modify(pickSessionsRef, (sessions) => [
- @@ -2452,12 +2486,16 @@2486+ copy.delete(tabId);2487+ }),2488+ ]);2489+ if (replaced) yield* replaced.cancel;2490+ yield* Ref.update(pickSessionsRef, (sessions) =>2491+ replaceMap(sessions, (copy) => {2492+ copy.set(tabId, { cancel });2493+ }),2494+ );24522495 yield* attempt({ operation: "pickElement.register", tabId, webContentsId: wc.id }, () => {24532496 wc.ipc.on(ELEMENT_PICKED_CHANNEL, onMessage);24542497 wc.once("destroyed", onDestroyed);24552498 wc.once("did-start-navigation", onNavigated);24562499 if (!wc.isFocused()) wc.focus();24572500 wc.send(START_PICK_CHANNEL, annotationTheme);24582501 });2459- yield* Ref.update(pickSessionsRef, (sessions) =>2460- replaceMap(sessions, (copy) => {2461- copy.set(tabId, { cancel });2462- }),2463- );apps/web/src/components/preview/PreviewView.tsx line 605Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -565,8 +565,15 @@565565 addPreviewAnnotation(threadRef, annotation);566- let screenshotFile: File | null = null;567- try {568- screenshotFile = await previewAnnotationScreenshotFile(annotation);569- } catch {570- // The structured annotation is still sendable when converting its571- // optional screenshot into a composer attachment fails.566+ // The structured annotation is still sendable when its optional crop567+ // stalls or fails, so tell the user what they lost and keep going568+ // instead of holding the composer for an attachment that never lands.569+ const capture = await capturePreviewAnnotationScreenshot(annotation);570+ if (capture.status === "failed") {571+ toastManager.add(572+ stackedThreadToast({573+ type: "error",574+ title: "Could not capture the picked element",575+ description: "Send again without it.",576+ }),577+ );572578 }579+ const screenshotFile = capture.status === "captured" ? capture.file : null;
- apps/web/src/components/preview/PreviewView.tsx line 605Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -568,5 +566,17 @@568- screenshotFile = await previewAnnotationScreenshotFile(annotation);569- } catch {570- // The structured annotation is still sendable when converting its571- // optional screenshot into a composer attachment fails.566+ // The structured annotation is still sendable when its optional crop567+ // stalls or fails, so tell the user what they lost and keep going568+ // instead of holding the composer for an attachment that never lands.569+ const capture = await capturePreviewAnnotationScreenshot(annotation);570+ if (capture.status === "failed") {571+ toastManager.add(572+ stackedThreadToast({573+ type: "error",574+ title: "Could not capture the picked element",575+ description:576+ submission === "send"577+ ? "The message was sent without the screenshot."578+ : "The annotation was added without the screenshot.",579+ }),580+ );572581 }582+ const screenshotFile = capture.status === "captured" ? capture.file : null;
- apps/web/src/components/preview/PreviewView.tsx lines 578 to 581OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -565,7 +567,15 @@567+ // instead of holding the composer for an attachment that never lands.568+ // The stored copy drops the screenshot on failure, otherwise the prompt569+ // would tell the agent a crop is attached when none was sent.570+ const capture = await capturePreviewAnnotationScreenshot(picked);571+ const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked;565572 addPreviewAnnotation(threadRef, annotation);566- let screenshotFile: File | null = null;567- try {568- screenshotFile = await previewAnnotationScreenshotFile(annotation);569- } catch {570- // The structured annotation is still sendable when converting its571- // optional screenshot into a composer attachment fails.573+ if (capture.status === "failed") {574+ toastManager.add(575+ stackedThreadToast({576+ type: "error",577+ title: "Could not capture the picked element",578+ description:579+ submission === "send"580+ ? "The message was sent without the screenshot."581+ : "The annotation was added without the screenshot.",
- apps/desktop/src/preview/PickPreload.ts line 1319Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1264,0 +1267,21 @@1267+ id: nextId("annotation"),1268+ pageUrl: location.href,1269+ pageTitle: document.title?.trim() || null,1270+ comment: comment.value.trim(),1271+ elements,1272+ regions: [...regions],1273+ strokes: [...strokes],1274+ styleChanges: Array.from(styleChanges.values()),1275+ screenshot: null,1276+ createdAt: new Date().toISOString(),1277+ };1278+ editor.style.display = "none";1279+ toolbar.style.display = "none";1280+ hoverOutline.style.display = "none";1281+ const screenshotRect = unionRects([1282+ ...elements.map((target) => target.rect),1283+ ...regions.map((region) => region.rect),1284+ ...strokes.map((stroke) => stroke.bounds),1285+ ]);1286+ ipcRenderer.send(ELEMENT_PICKED_CHANNEL, annotation, screenshotRect, submission);1287+ })
- @@ -2449,3 +2493,14 @@24492493 if (isMainFrame) settle(null);24502494 };24512495 const registerPickElement = Effect.fn("PreviewManager.registerPickElement")(function* () {2496+ // Two picks on one tab can overlap. Swap this session in and cancel2497+ // the previous holder in one step, so no third pick can slip into an2498+ // empty slot in between and the session we push out still resumes2499+ // its renderer.2500+ const replaced = yield* Ref.modify(pickSessionsRef, (sessions) => [2501+ sessions.get(tabId) ?? null,2502+ replaceMap(sessions, (copy) => {2503+ copy.set(tabId, session);2504+ }),2505+ ]);2506+ if (replaced) yield* replaced.cancel;
- apps/desktop/src/preview/PickPreload.ts line 1268OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -1250,15 +1263,6 @@1250- strokes: [...strokes],1251- styleChanges: Array.from(styleChanges.values()),1252- screenshot: null,1253- createdAt: new Date().toISOString(),1254- };1255- editor.style.display = "none";1256- toolbar.style.display = "none";1257- hoverOutline.style.display = "none";1258- const screenshotRect = unionRects([1259- ...elements.map((target) => target.rect),1260- ...regions.map((region) => region.rect),1261- ...strokes.map((stroke) => stroke.bounds),1262- ]);1263- ipcRenderer.send(ELEMENT_PICKED_CHANNEL, annotation, screenshotRect, submission);1264- });1263+ )1264+ .then((captured) => {1265+ // The overlay may have been cancelled or replaced while the capture1266+ // ran. A late submit must not deliver into the next pick's listener.1267+ if (finished) return;1268+ const elements = captured.filter((target) => target !== null);
- apps/desktop/src/preview/PickPreload.ts line 306OutdatedResolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -282,3 +287,20 @@287+ * pages leave it pending forever. Without a bound, the whole submit chain288+ * stalls and the overlay sits on "Capturing…".289+ */290+function withCaptureTimeout<A>(promise: Promise<A>, millis: number): Promise<A | null> {291+ let timer: ReturnType<typeof setTimeout> | undefined;292+ return Promise.race([293+ promise,294+ new Promise<null>((resolve) => {295+ timer = setTimeout(() => resolve(null), millis);296+ }),297+ ]).finally(() => clearTimeout(timer));298+}299+282300 async function captureElement(element: Element): Promise<PickedElementPayload | null> {283301 try {284- const context = await getElementContext(element);302+ const context = await withCaptureTimeout(303+ Promise.resolve(getElementContext(element)),304+ ELEMENT_CONTEXT_TIMEOUT_MS,305+ );306+ if (!context) return null;
- apps/web/src/components/preview/PreviewView.tsx line 603Resolvedby t3dotgg(GitHub profile, opens in a new tab)@@ -583,7 +586,15 @@586+ // The stored copy drops the screenshot on failure, otherwise the prompt587+ // would tell the agent a crop is attached when none was sent.588+ const capture = await capturePreviewAnnotationScreenshot(picked);589+ const annotation = capture.status === "failed" ? { ...picked, screenshot: null } : picked;583590 addPreviewAnnotation(threadRef, annotation);584- let screenshotFile: File | null = null;585- try {586- screenshotFile = await previewAnnotationScreenshotFile(annotation);587- } catch {588- // The structured annotation is still sendable when converting its589- // optional screenshot into a composer attachment fails.591+ if (capture.status === "failed") {592+ toastManager.add(593+ stackedThreadToast({594+ type: "error",595+ title: "Could not capture the picked element",596+ // The send path reports its own outcome, so only say what this597+ // handler knows: the crop was dropped.598+ description: "The annotation was kept without the screenshot.",599+ }),600+ );
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed