Skip to content

The agent that grows with you

fix(desktop): read explicitly local-owned media through the native bridge

BearHuddleston77bb69516524
diff --git a/apps/desktop/src/lib/media-owner-routing.test.ts b/apps/desktop/src/lib/media-owner-routing.test.ts
new file mode 100644
index 00000000000..04861c66439
--- /dev/null
+++ b/apps/desktop/src/lib/media-owner-routing.test.ts
@@ -0,0 +1,56 @@
+import { afterEach, beforeEach, expect, it, vi } from 'vitest'
+
+import { setApiRequestConnection } from '@/api/client'
+import { $connection } from '@/store/session'
+
+import { resolveMediaDisplaySrc } from './media'
+
+const localData = 'data:image/png;base64,bG9jYWw='
+const remoteData = 'data:image/png;base64,cmVtb3Rl'
+const read = vi.fn(async () => localData)
+const api = vi.fn(async () => ({ dataUrl: remoteData }))
+
+beforeEach(() => {
+ read.mockClear()
+ api.mockReset()
+ api.mockResolvedValue({ dataUrl: remoteData })
+ vi.stubGlobal('hermesDesktop', { readFileDataUrl: read, api })
+ $connection.set({ connectionId: 'foreground-remote', mode: 'remote', profile: 'active' } as never)
+ setApiRequestConnection('foreground-remote')
+})
+
+afterEach(() => {
+ $connection.set(null)
+ setApiRequestConnection(null)
+ vi.unstubAllGlobals()
+})
+
+it('uses the native bridge for an explicit local owner, even with a remote foreground', async () => {
+ await expect(resolveMediaDisplaySrc('/local/image.png', { connectionId: 'local', profile: 'work' })).resolves.toBe(
+ localData
+ )
+ expect(read).toHaveBeenCalledTimes(1)
+ expect(read).toHaveBeenCalledWith('/local/image.png')
+ expect(api).not.toHaveBeenCalled()
+})
+
+it('keeps an explicit remote owner pinned to the API', async () => {
+ await expect(
+ resolveMediaDisplaySrc('/remote/image.png', { connectionId: 'owner-remote', profile: 'work' })
+ ).resolves.toBe(remoteData)
+ expect(api).toHaveBeenCalledTimes(1)
+ expect(api).toHaveBeenCalledWith({
+ connectionId: 'owner-remote',
+ profile: 'work',
+ path: '/api/fs/read-data-url?path=%2Fremote%2Fimage.png'
+ })
+ expect(read).not.toHaveBeenCalled()
+})
+
+it('never falls back to this device after a remote owner read fails', async () => {
+ api.mockRejectedValueOnce(new Error('remote read failed'))
+ await expect(
+ resolveMediaDisplaySrc('/same/path.png', { connectionId: 'owner-remote', profile: 'work' })
+ ).rejects.toThrow('remote read failed')
+ expect(read).not.toHaveBeenCalled()
+})
diff --git a/apps/desktop/src/lib/media.ts b/apps/desktop/src/lib/media.ts
index 830603e53ab..a9e0b041090 100644
--- a/apps/desktop/src/lib/media.ts
+++ b/apps/desktop/src/lib/media.ts
@@ -1,3 +1,5 @@
+import { LOCAL_CONNECTION_ID } from '@hermes/shared'
+
import { hermesApi, type OwnerScope } from '@/api/client'
import type { HermesConnection } from '@/global'
import { desktopFsCacheKey, readDesktopFileDataUrl } from '@/lib/desktop-fs'
@@ -92,6 +94,13 @@ export async function resolveMediaDisplaySrc(path: string, owner?: OwnerScope):
return path
}
+ // An explicit local owner is this device, even with a remote foreground.
+ // Keep the native reader and its configured size cap; the backend preview
+ // endpoint has a separate fixed limit.
+ if (owner?.connectionId === LOCAL_CONNECTION_ID && window.hermesDesktop?.readFileDataUrl) {
+ return window.hermesDesktop.readFileDataUrl(filePathFromMediaPath(path))
+ }
+
// A tile can belong to a different gateway than the foreground. Pin both
// halves at read admission rather than resolving them when the read settles.
if (window.hermesDesktop && (owner?.connectionId || owner?.profile)) {