The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(desktop): forward client clipboard paste into Bot Screen
chelsealong2d6657d72cbb
diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane-clipboard.test.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane-clipboard.test.tsxnew file mode 100644index 00000000000..88579f6566f--- /dev/null+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane-clipboard.test.tsx@@ -0,0 +1,146 @@+/**+ * Explicit-paste clipboard bridge (#123089): a native `paste` gesture over the+ * live screen must forward the client's text clipboard to noVNC's+ * `clipboardPasteFrom`, but only while this viewer holds the lease — never on+ * a watch-only stream, and never via polling.+ */++import { fireEvent, render, waitFor } from '@testing-library/react'+import type { ReactNode } from 'react'+import { afterEach, beforeEach, expect, it, vi } from 'vitest'++import type { DisplayStatus } from './screen-connection'+import type * as ScreenConnection from './screen-connection'+import type { RosterRow } from './types'++const rfbs = vi.hoisted(+ () => [] as Array<{ target: HTMLElement; viewOnly: boolean; clipboardPasteFrom: (text: string) => void }>+)++vi.mock('@hermes/plugin-sdk', async () => {+ const { useStore } = await import('@nanostores/react')+ const { onGatewayEvent } = await import('../../contrib/events')++ return {+ Button: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => (+ <button {...props}>{children}</button>+ ),+ Codicon: () => null,+ GlyphSpinner: () => null,+ Tip: ({ children }: { children: ReactNode }) => <>{children}</>,+ EmptyState: () => null,+ useValue: useStore,+ host: { onEvent: onGatewayEvent, retainProfile: async () => () => {} }+ }+})+vi.mock('./routing', () => {+ const route = { connectionId: 'host-a', mode: 'remote', profile: 'default', targetProfile: 'default' }++ return { botConnectionRoute: () => route, resolveBotConnectionRoute: () => ({ status: 'resolved', route }) }+})+vi.mock('./data', () => ({ botSelectionKey: (bot: RosterRow) => bot.name }))+vi.mock('./i18n', () => ({+ useBots: () => ({+ screen: {+ title: 'Screen',+ youControl: 'You control',+ handBack: 'Hand back',+ takeOver: 'Take over',+ reconnect: 'Reconnect',+ streamLost: 'Stream lost'+ }+ })+}))+vi.mock('./screen-connection', async importActual => ({+ ...(await importActual<typeof ScreenConnection>()),+ displayRequest: vi.fn(),+ resolveScreenWsUrl: vi.fn(async () => 'ws://localhost/api/display/ws'),+ isEventForBotScreen: () => false+}))+vi.mock('@novnc/novnc', () => ({+ default: class {+ viewOnly = true+ clipboardPasteFrom = vi.fn()+ constructor(target: HTMLElement) {+ rfbs.push(this as never)+ ;(this as unknown as { target: HTMLElement }).target = target+ }+ addEventListener(type: string, callback: (event: { detail?: unknown }) => void) {+ if (type === 'connect') {+ queueMicrotask(() => callback({}))+ }+ }+ disconnect() {}+ focus() {}+ }+}))++import { displayRequest } from './screen-connection'+import { BotScreenPane } from './screen-pane'+import { $screenState } from './screen-state'++const bot: RosterRow = { name: 'default' }++// `viewer_hash` is the first 12 hex of sha256("this-viewer"); leaseHeldBy() matches on it,+// which is what makes this viewer the lease holder (`iHold`) and puts `client.viewOnly = false`.+const status: DisplayStatus = {+ profile: 'default',+ profile_key: '/home/hermes/.hermes',+ supported: true,+ installed: true,+ missing: [],+ running: true,+ pid: 42,+ display: ':20',+ socket: '/tmp/rfb.sock',+ geometry: '1440x900',+ install_command: null,+ lease: { holder: 'human', viewer_id: null, viewer_hash: 'e0f9a555d558', since: 1, reason: '', epoch: 1 }+}++beforeEach(() => {+ $screenState.set({})+ rfbs.length = 0+ vi.mocked(displayRequest)+ .mockReset()+ .mockResolvedValue({ ...status, ticket: 'test-ticket', viewer_id: 'this-viewer' })+ vi.stubGlobal(+ 'WebSocket',+ class {+ binaryType = ''+ addEventListener() {}+ close() {}+ }+ )+})++afterEach(() => vi.unstubAllGlobals())++it('forwards an explicit paste over the canvas to noVNC while holding the lease', async () => {+ const view = render(<BotScreenPane bot={bot} />)+ await waitFor(() => expect(rfbs).toHaveLength(1))+ await waitFor(() => expect(rfbs[0].viewOnly).toBe(false))++ fireEvent.paste(rfbs[0].target, { clipboardData: { getData: () => 'clipboard-test-123' } })++ expect(rfbs[0].clipboardPasteFrom).toHaveBeenCalledWith('clipboard-test-123')+ view.unmount()+})++it('drops a paste while this viewer only watches (no lease)', async () => {+ vi.mocked(displayRequest).mockResolvedValue({+ ...status,+ lease: { ...status.lease, viewer_hash: 'someone-else' },+ ticket: 'test-ticket',+ viewer_id: 'this-viewer'+ })++ const view = render(<BotScreenPane bot={bot} />)+ await waitFor(() => expect(rfbs).toHaveLength(1))+ await waitFor(() => expect(rfbs[0].viewOnly).toBe(true))++ fireEvent.paste(rfbs[0].target, { clipboardData: { getData: () => 'clipboard-test-123' } })++ expect(rfbs[0].clipboardPasteFrom).not.toHaveBeenCalled()+ view.unmount()+})diff --git a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsxindex 75da5808fe7..98415450006 100644--- a/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx+++ b/apps/desktop/src/plugins/hermes-bots/screen-pane.tsx@@ -52,6 +52,7 @@ type RfbLike = {addEventListener: (type: string, handler: (event: { detail?: { clean?: boolean; reason?: string } }) => void) => voiddisconnect: () => voidfocus: () => void+ clipboardPasteFrom: (text: string) => void}type ConnState = 'idle' | 'attaching' | 'live' | 'error'@@ -91,6 +92,9 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {// Pins the bot's pooled gateway socket for the attach lifetime so display.lease// events keep arriving for an inactive registry-routed bot.const retention = useRef<(() => void) | null>(null)+ // Removes the current attach's `paste` listener; torn down on every detach so a stale+ // one never outlives its RFB client.+ const pasteCleanup = useRef<(() => void) | null>(null)const [conn, setConn] = useState<ConnState>('idle')const [error, setError] = useState<null | string>(null)const [busy, setBusy] = useState(false)@@ -161,6 +165,8 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {socket.current = nullretention.current?.()retention.current = null+ pasteCleanup.current?.()+ pasteCleanup.current = null}, [])const attach = useCallback(@@ -274,6 +280,27 @@ export function BotScreenPane({ bot }: { bot: RosterRow }) {void refresh()})rfb.current = client+ // Explicit user paste only: a native `paste` event on the canvas (never polling, never+ // logged) forwarded as noVNC ClientCutText. `viewOnly` is read live off `client`, so a+ // paste after control changes hands mid-session is silently dropped, same as the gateway's+ // own lease-gated RFB filter would drop it.+ const pasteTarget = canvasHost.current++ const handlePaste = (event: ClipboardEvent) => {+ if (client.viewOnly) {+ return+ }++ const text = event.clipboardData?.getData('text')++ if (text) {+ event.preventDefault()+ client.clipboardPasteFrom(text)+ }+ }++ pasteTarget.addEventListener('paste', handlePaste)+ pasteCleanup.current = () => pasteTarget.removeEventListener('paste', handlePaste)} catch (err) {if (generation === attachGeneration.current) {setConn('error')