The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): migrate clipboard handlers to the Electron 44 async W3C clipboard API
Brooklyn Nicholsoned68bf3c5757
diff --git a/apps/desktop/electron/clipboard-image.test.ts b/apps/desktop/electron/clipboard-image.test.tsnew file mode 100644index 00000000000..1d457a7145e--- /dev/null+++ b/apps/desktop/electron/clipboard-image.test.ts@@ -0,0 +1,83 @@+import assert from 'node:assert/strict'++import { test } from 'vitest'++import { readClipboardImagePng } from './clipboard-image'++const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])++function fakePngBuffer(extraBytes = 16) {+ return Buffer.concat([PNG_SIGNATURE, Buffer.alloc(extraBytes, 0x42)])+}++function blobOf(buffer) {+ return {+ arrayBuffer: async () => buffer.buffer.slice(buffer.byteOffset, buffer.byteOffset + buffer.byteLength)+ }+}++function clipboardWith(items) {+ return {+ read: async () => items+ }+}++function itemOf(types, getType) {+ return { types, getType }+}++test('readClipboardImagePng returns PNG bytes from an image/png clipboard item', async () => {+ const png = fakePngBuffer()+ const clipboard = clipboardWith([itemOf(['text/plain', 'image/png'], (type) => {+ assert.equal(type, 'image/png')++ return Promise.resolve(blobOf(png))+ })])++ const result = await readClipboardImagePng(clipboard)++ assert.ok(Buffer.isBuffer(result))+ assert.ok(result.equals(png))+})++test('readClipboardImagePng skips items without an image/png type', async () => {+ const clipboard = clipboardWith([itemOf(['text/plain'], (type) => Promise.reject(new Error(`unexpected ${type}`)))])++ assert.equal(await readClipboardImagePng(clipboard), null)+})++test('readClipboardImagePng keeps scanning when getType rejects for a vanished type', async () => {+ const png = fakePngBuffer()+ const clipboard = clipboardWith([+ itemOf(['image/png'], () => Promise.reject(new Error('type vanished'))),+ itemOf(['image/png'], () => Promise.resolve(blobOf(png)))+ ])++ const result = await readClipboardImagePng(clipboard)++ assert.ok(result && result.equals(png))+})++test('readClipboardImagePng treats an empty PNG payload as no image', async () => {+ const clipboard = clipboardWith([itemOf(['image/png'], () => Promise.resolve(blobOf(Buffer.alloc(0))))])++ assert.equal(await readClipboardImagePng(clipboard), null)+})++test('readClipboardImagePng returns null when clipboard.read() rejects', async () => {+ const clipboard = {+ read: () => Promise.reject(new Error('clipboard busy'))+ }++ assert.equal(await readClipboardImagePng(clipboard), null)+})++test('readClipboardImagePng tolerates malformed items in the read result', async () => {+ const clipboard = clipboardWith([null, { types: 'not-an-array' }, itemOf([], () => Promise.resolve(blobOf(fakePngBuffer())))])++ assert.equal(await readClipboardImagePng(clipboard), null)+})++test('readClipboardImagePng returns null for an empty clipboard', async () => {+ assert.equal(await readClipboardImagePng(clipboardWith([])), null)+})diff --git a/apps/desktop/electron/clipboard-image.ts b/apps/desktop/electron/clipboard-image.tsnew file mode 100644index 00000000000..84c2b953ab7--- /dev/null+++ b/apps/desktop/electron/clipboard-image.ts@@ -0,0 +1,50 @@+// Electron 44 rearchitected the clipboard onto the W3C async API: readText /+// writeText return Promises, and image reads no longer have a readImage()+// shortcut — the bytes arrive through clipboard.read() (ClipboardItem[]) and+// ClipboardItem.getType('image/png') as a Blob. This helper keeps the old+// handler contract ("PNG bytes or null") behind the new API.++const PNG_MIME = 'image/png'++// Read the system clipboard's image as PNG bytes, or null when the clipboard+// carries no image. Depends on the injected clipboard so tests can drive it.+async function readClipboardImagePng(clipped) {+ let items++ try {+ items = await clipped.read()+ } catch {+ // A clipboard read can reject while a clipboard owner is mid-write; the+ // old sync readImage() returned an empty NativeImage instead.+ return null+ }++ for (const item of items) {+ if (!Array.isArray(item?.types) || !item.types.includes(PNG_MIME)) {+ continue+ }++ try {+ const blob = await item.getType(PNG_MIME)++ if (!blob || typeof blob.arrayBuffer !== 'function') {+ continue+ }++ const buffer = Buffer.from(await blob.arrayBuffer())++ // Guard against a degenerate empty payload: an empty image must behave+ // like "no image" so the WSL fallback still runs.+ if (buffer.length > 0) {+ return buffer+ }+ } catch {+ // getType rejects when the type vanished between read() and getType();+ // keep scanning the remaining items.+ }+ }++ return null+}++export { readClipboardImagePng }diff --git a/apps/desktop/electron/main.ts b/apps/desktop/electron/main.tsindex f56ce759b60..c47ecf03906 100644--- a/apps/desktop/electron/main.ts+++ b/apps/desktop/electron/main.ts@@ -126,6 +126,7 @@ import {import { CHALLENGE_PARTITION } from './challenge-window'import { registerChallengeWindowIpc } from './challenge-window-ipc'import { provisionCliLinks } from './cli-provision'+import { readClipboardImagePng } from './clipboard-image'import { closeStopFailureMessage, finishWindowsCloseStop, type RuntimeLock } from './close-stop-kill'import { shouldAttemptCloudBootCascade } from './cloud-boot-cascade'import { discoverWithTeamFallback } from './cloud-discovery'@@ -16510,8 +16511,10 @@ ipcMain.handle('hermes:selectPaths', async (_event, options: any = {}) => {return result.filePaths})-ipcMain.handle('hermes:writeClipboard', (_event, text) => {- clipboard.writeText(String(text || ''))+ipcMain.handle('hermes:writeClipboard', async (_event, text) => {+ // Electron 44: clipboard.writeText is async (W3C clipboard API); the old+ // call was fire-and-forget, and the renderer only needs the ack.+ await clipboard.writeText(String(text || ''))return true})@@ -16526,6 +16529,8 @@ registerSaveDialogIpc(ipcMain, {// navigator.clipboard.readText() throws "Document is not focused" whenever a// portaled overlay has focus, and there's no way to route a read through the// canvas. The main process has no such gate.+// Electron 44: readText returns a Promise now (W3C clipboard API); the+// renderer already awaited the invoke round-trip either way.ipcMain.handle('hermes:readClipboard', () => clipboard.readText())ipcMain.handle('hermes:saveGatewayFile', (_event, payload) => saveGatewayFile(payload))@@ -16613,10 +16618,12 @@ ipcMain.handle('hermes:savePastedText', async (_event, payload) => {})ipcMain.handle('hermes:saveClipboardImage', async () => {- const image = clipboard.readImage()+ // Electron 44 removed clipboard.readImage(); PNG bytes now arrive through+ // clipboard.read() + ClipboardItem.getType('image/png') as a Blob.+ const png = await readClipboardImagePng(clipboard)- if (image && !image.isEmpty()) {- return writeComposerImage(image.toPNG(), '.png')+ if (png) {+ return writeComposerImage(png, '.png')}// WSL2/WSLg doesn't bridge clipboard *images* from the Windows host to thediff --git a/apps/desktop/package.json b/apps/desktop/package.jsonindex 967a68de746..3dc630edcd0 100644--- a/apps/desktop/package.json+++ b/apps/desktop/package.json@@ -158,7 +158,8 @@"use-stick-to-bottom": "1.1.6","vfile": "6.0.3","web-haptics": "0.0.6",- "yaml": "2.9.1"+ "yaml": "2.9.1",+ "ws": "^8.21.1"},"optionalDependencies": {"get-windows": "9.3.0"diff --git a/package-lock.json b/package-lock.jsonindex 974f6f7c5cd..26349929bfa 100644--- a/package-lock.json+++ b/package-lock.json@@ -154,7 +154,8 @@"use-stick-to-bottom": "1.1.6","vfile": "6.0.3","web-haptics": "0.0.6",- "yaml": "2.9.1"+ "yaml": "2.9.1",+ "ws": "^8.21.1"},"devDependencies": {"@babel/core": "8.0.1",