Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript

feat(desktop): add keyboard command registry and input routing

Josué1202ed37c69f
diff --git a/desktop-app/src/__tests__/desktopCommands.test.ts b/desktop-app/src/__tests__/desktopCommands.test.ts
new file mode 100644
index 000000000..60b7e10d4
--- /dev/null
+++ b/desktop-app/src/__tests__/desktopCommands.test.ts
@@ -0,0 +1,71 @@
+import { describe, expect, it } from 'vitest'
+import {
+ DESKTOP_COMMANDS,
+ type DesktopShortcutInput,
+ desktopBindingCollisionKey,
+ formatDesktopShortcut,
+ getDesktopCommand,
+ matchDesktopCommand,
+} from '../desktopCommands.js'
+
+function input(overrides: Partial<DesktopShortcutInput> = {}): DesktopShortcutInput {
+ return {
+ type: 'keyDown',
+ key: 'f',
+ control: false,
+ meta: true,
+ alt: false,
+ shift: false,
+ isAutoRepeat: false,
+ isComposing: false,
+ ...overrides,
+ }
+}
+
+describe('Desktop command registry', () => {
+ it('has unique stable IDs and no binding collisions within a source', () => {
+ expect(new Set(DESKTOP_COMMANDS.map(command => command.id)).size).toBe(DESKTOP_COMMANDS.length)
+ for (const source of ['host', 'sandbox'] as const) {
+ const keys = DESKTOP_COMMANDS.filter(
+ command => command.defaultBinding && command.sources.includes(source)
+ ).map(command => desktopBindingCollisionKey(command.defaultBinding!))
+ expect(new Set(keys).size).toBe(keys.length)
+ }
+ })
+
+ it('keeps reserved Electron and OS roles outside the registry', () => {
+ const keys = new Set(
+ DESKTOP_COMMANDS.flatMap(command =>
+ command.defaultBinding ? [desktopBindingCollisionKey(command.defaultBinding)] : []
+ )
+ )
+ for (const reserved of ['mod+q', 'mod+h', 'mod+m', 'mod+p', 'mod+r']) {
+ expect(keys.has(reserved)).toBe(false)
+ }
+ })
+
+ it('formats and matches Command on macOS and Ctrl on Windows', () => {
+ const binding = getDesktopCommand('search.current').defaultBinding!
+ expect(formatDesktopShortcut(binding, 'darwin')).toBe('⌘⇧F')
+ expect(formatDesktopShortcut(binding, 'win32')).toBe('Ctrl+Shift+F')
+ expect(matchDesktopCommand(input({ shift: true }), 'darwin', 'host')?.id).toBe('search.current')
+ expect(
+ matchDesktopCommand(input({ meta: false, control: true, shift: true }), 'win32', 'sandbox')
+ ?.id
+ ).toBe('search.current')
+ })
+
+ it('ignores repeat, composition, AltGr-like input, and wrong platform modifiers', () => {
+ expect(matchDesktopCommand(input({ isAutoRepeat: true }), 'darwin', 'host')).toBeNull()
+ expect(matchDesktopCommand(input({ isComposing: true }), 'darwin', 'host')).toBeNull()
+ expect(
+ matchDesktopCommand(input({ meta: false, control: true, alt: true }), 'win32', 'host')
+ ).toBeNull()
+ expect(matchDesktopCommand(input({ meta: false, control: true }), 'darwin', 'host')).toBeNull()
+ })
+
+ it('maps Mod+9 to last-tab semantics rather than a ninth-index command', () => {
+ expect(matchDesktopCommand(input({ key: '9' }), 'darwin', 'host')?.id).toBe('tabs.selectLast')
+ expect(DESKTOP_COMMANDS.some(command => command.id === 'tabs.select9')).toBe(false)
+ })
+})
diff --git a/desktop-app/src/__tests__/shortcutRouter.test.ts b/desktop-app/src/__tests__/shortcutRouter.test.ts
new file mode 100644
index 000000000..7fb752a0e
--- /dev/null
+++ b/desktop-app/src/__tests__/shortcutRouter.test.ts
@@ -0,0 +1,89 @@
+import { describe, expect, it, vi } from 'vitest'
+import type { DesktopShortcutInput } from '../desktopCommands.js'
+import { type DesktopShortcutRoute, routeDesktopShortcut } from '../shortcutRouter.js'
+
+function input(overrides: Partial<DesktopShortcutInput> = {}): DesktopShortcutInput {
+ return {
+ type: 'keyDown',
+ key: 'f',
+ control: false,
+ meta: true,
+ alt: false,
+ shift: false,
+ isAutoRepeat: false,
+ isComposing: false,
+ ...overrides,
+ }
+}
+
+function route(source: 'host' | 'sandbox' = 'host'): DesktopShortcutRoute {
+ const trustedRenderer = {
+ id: 1,
+ isDestroyed: () => false,
+ isFocused: () => source === 'host',
+ on: vi.fn(),
+ removeListener: vi.fn(),
+ send: vi.fn(),
+ }
+ return {
+ source,
+ platform: 'darwin',
+ sourceWebContents:
+ source === 'host'
+ ? trustedRenderer
+ : {
+ id: 2,
+ isDestroyed: () => false,
+ isFocused: () => true,
+ on: vi.fn(),
+ removeListener: vi.fn(),
+ send: vi.fn(),
+ },
+ trustedRenderer,
+ } as DesktopShortcutRoute
+}
+
+describe('main-process Desktop shortcut routing', () => {
+ it('routes only from the actual focused WebContents', () => {
+ const current = route('sandbox')
+ current.isCurrentSource = () => true
+ const preventDefault = vi.fn()
+ expect(routeDesktopShortcut(current, { preventDefault }, input())).toBe(true)
+ expect(preventDefault).toHaveBeenCalledOnce()
+ expect(current.trustedRenderer.send).toHaveBeenCalledWith('shortcuts:command', {
+ commandId: 'search.open',
+ })
+
+ current.isCurrentSource = () => false
+ expect(routeDesktopShortcut(current, { preventDefault }, input())).toBe(false)
+ expect(current.trustedRenderer.send).toHaveBeenCalledTimes(1)
+ })
+
+ it('routes global and contextual search distinctly from sandbox focus', () => {
+ const current = route('sandbox')
+ routeDesktopShortcut(current, { preventDefault: vi.fn() }, input())
+ routeDesktopShortcut(current, { preventDefault: vi.fn() }, input({ shift: true }))
+ expect(current.trustedRenderer.send).toHaveBeenNthCalledWith(1, 'shortcuts:command', {
+ commandId: 'search.open',
+ })
+ expect(current.trustedRenderer.send).toHaveBeenNthCalledWith(2, 'shortcuts:command', {
+ commandId: 'search.current',
+ })
+ })
+
+ it('does not intercept reserved, ordinary editing, IME, AltGr, or key-up input', () => {
+ const current = route('host')
+ const preventDefault = vi.fn()
+ for (const candidate of [
+ input({ key: 'q' }),
+ input({ key: 'c' }),
+ input({ isComposing: true }),
+ input({ meta: false, control: true, alt: true }),
+ input({ type: 'keyUp' }),
+ ]) {
+ expect(routeDesktopShortcut(current, { preventDefault }, candidate)).toBe(false)
+ }
+ expect(preventDefault).not.toHaveBeenCalled()
+ expect(current.trustedRenderer.send).not.toHaveBeenCalled()
+ })
+})
diff --git a/desktop-app/src/desktopCommands.ts b/desktop-app/src/desktopCommands.ts
new file mode 100644
index 000000000..0c649511b
--- /dev/null
+++ b/desktop-app/src/desktopCommands.ts
@@ -0,0 +1,283 @@
+export type DesktopShortcutPlatform = 'darwin' | 'win32' | 'linux'
+export type DesktopCommandSource = 'host' | 'sandbox'
+export type DesktopCommandGroup = 'Chat' | 'Navigation' | 'Search' | 'Commands'
+export type DesktopCommandEligibility =
+ | 'always'
+ | 'tab-exists'
+ | 'multiple-tabs'
+ | 'tab-index'
+ | 'searchable-content'
+ | 'composer-available'
+
+export type SemanticShortcutBinding = {
+ key: string
+ mod: true
+ shift?: true
+}
+
+export type DesktopCommandDefinition = {
+ id: string
+ label: string
+ description: string
+ group: DesktopCommandGroup
+ order: number
+ defaultBinding: SemanticShortcutBinding | null
+ sources: readonly DesktopCommandSource[]
+ eligibility: DesktopCommandEligibility
+ tabIndex?: number
+ editingPolicy: 'allow' | 'suppress'
+ modalPolicy: 'block' | 'toggle-palette'
+ actionOwner: 'renderer'
+ visibleInPalette: boolean
+ visibleInSettings: boolean
+}
+
+const definitions = [
+ {
+ id: 'chat.newTab',
+ label: 'New chat tab',
+ description: 'Open a blank chat view tab.',
+ group: 'Chat',
+ order: 10,
+ defaultBinding: { key: 't', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'always',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'chat.closeTab',
+ label: 'Close current tab',
+ description: 'Close the active chat view without deleting its conversation.',
+ group: 'Chat',
+ order: 20,
+ defaultBinding: { key: 'w', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'tab-exists',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ ...Array.from({ length: 8 }, (_, index) => ({
+ id: `tabs.select${index + 1}` as const,
+ label: `Select tab ${index + 1}`,
+ description: `Switch to chat tab ${index + 1}.`,
+ group: 'Navigation' as const,
+ order: 30 + index,
+ defaultBinding: { key: String(index + 1), mod: true as const },
+ sources: ['host', 'sandbox'] as const,
+ eligibility: 'tab-index' as const,
+ tabIndex: index,
+ editingPolicy: 'allow' as const,
+ modalPolicy: 'block' as const,
+ actionOwner: 'renderer' as const,
+ visibleInPalette: true,
+ visibleInSettings: true,
+ })),
+ {
+ id: 'tabs.selectLast',
+ label: 'Select last tab',
+ description: 'Switch to the last open chat tab.',
+ group: 'Navigation',
+ order: 40,
+ defaultBinding: { key: '9', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'tab-exists',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'tabs.next',
+ label: 'Next tab',
+ description: 'Switch to the next chat tab, wrapping at the end.',
+ group: 'Navigation',
+ order: 50,
+ defaultBinding: { key: 'Tab', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'multiple-tabs',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'tabs.previous',
+ label: 'Previous tab',
+ description: 'Switch to the previous chat tab, wrapping at the start.',
+ group: 'Navigation',
+ order: 60,
+ defaultBinding: { key: 'Tab', mod: true, shift: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'multiple-tabs',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'search.open',
+ label: 'Focus global search',
+ description: 'Open and focus the existing Desktop application search.',
+ group: 'Search',
+ order: 70,
+ defaultBinding: { key: 'f', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'always',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'search.current',
+ label: 'Search current content',
+ description: 'Find text in the current chat or app.',
+ group: 'Search',
+ order: 80,
+ defaultBinding: { key: 'f', mod: true, shift: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'searchable-content',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'composer.focus',
+ label: 'Focus message composer',
+ description: 'Move keyboard focus to the current chat composer.',
+ group: 'Chat',
+ order: 90,
+ defaultBinding: { key: 'l', mod: true, shift: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'composer-available',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'commands.open',
+ label: 'Open command palette',
+ description: 'Search and run available Desktop commands.',
+ group: 'Commands',
+ order: 100,
+ defaultBinding: { key: 'k', mod: true },
+ sources: ['host', 'sandbox'],
+ eligibility: 'always',
+ editingPolicy: 'allow',
+ modalPolicy: 'toggle-palette',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: true,
+ },
+ {
+ id: 'settings.shortcuts',
+ label: 'Keyboard shortcuts',
+ description: 'Open the read-only keyboard shortcuts reference.',
+ group: 'Commands',
+ order: 110,
+ defaultBinding: null,
+ sources: ['host', 'sandbox'],
+ eligibility: 'always',
+ editingPolicy: 'allow',
+ modalPolicy: 'block',
+ actionOwner: 'renderer',
+ visibleInPalette: true,
+ visibleInSettings: false,
+ },
+] as const satisfies readonly DesktopCommandDefinition[]
+
+export type DesktopCommandId = (typeof definitions)[number]['id']
+
+export const DESKTOP_COMMANDS: readonly DesktopCommandDefinition[] = definitions
+
+export type DesktopShortcutInput = {
+ type: string
+ key: string
+ control: boolean
+ meta: boolean
+ alt: boolean
+ shift: boolean
+ isAutoRepeat: boolean
+ isComposing: boolean
+}
+
+export function isDesktopCommandId(value: unknown): value is DesktopCommandId {
+ return typeof value === 'string' && definitions.some(command => command.id === value)
+}
+
+export function getDesktopCommand(id: DesktopCommandId): DesktopCommandDefinition {
+ const command = definitions.find(candidate => candidate.id === id)
+ if (!command) throw new Error(`Unknown Desktop command: ${id}`)
+ return command
+}
+
+export function platformFromNode(platform: NodeJS.Platform): DesktopShortcutPlatform {
+ if (platform === 'darwin' || platform === 'win32') return platform
+ return 'linux'
+}
+
+export function platformFromNavigator(platform: string): DesktopShortcutPlatform {
+ if (/mac/i.test(platform)) return 'darwin'
+ if (/win/i.test(platform)) return 'win32'
+ return 'linux'
+}
+
+function normalizedKey(value: string): string {
+ return value.length === 1 ? value.toLowerCase() : value
+}
+
+export function bindingMatchesInput(
+ binding: SemanticShortcutBinding,
+ input: DesktopShortcutInput,
+ platform: DesktopShortcutPlatform
+): boolean {
+ if (input.type !== 'keyDown' || input.isAutoRepeat || input.isComposing || input.alt) return false
+ if (input.control && input.alt) return false
+ const usesMeta = platform === 'darwin'
+ if (usesMeta ? !input.meta || input.control : !input.control || input.meta) return false
+ if (Boolean(binding.shift) !== input.shift) return false
+ return normalizedKey(binding.key) === normalizedKey(input.key)
+}
+
+export function matchDesktopCommand(
+ input: DesktopShortcutInput,
+ platform: DesktopShortcutPlatform,
+ source: DesktopCommandSource
+): DesktopCommandDefinition | null {
+ return (
+ definitions.find(
+ command =>
+ command.defaultBinding &&
+ command.sources.includes(source) &&
+ bindingMatchesInput(command.defaultBinding, input, platform)
+ ) ?? null
+ )
+}
+
+export function formatDesktopShortcut(
+ binding: SemanticShortcutBinding,
+ platform: DesktopShortcutPlatform
+): string {
+ const key = binding.key === 'Tab' ? 'Tab' : binding.key.toUpperCase()
+ if (platform === 'darwin') return `⌘${binding.shift ? '⇧' : ''}${key}`
+ return `Ctrl+${binding.shift ? 'Shift+' : ''}${key}`
+}
+
+export function desktopBindingCollisionKey(binding: SemanticShortcutBinding): string {
+ return `mod+${binding.shift ? 'shift+' : ''}${normalizedKey(binding.key)}`
+}
diff --git a/desktop-app/src/main.ts b/desktop-app/src/main.ts
index 1a58669d0..9a318c3be 100644
--- a/desktop-app/src/main.ts
+++ b/desktop-app/src/main.ts
@@ -16,6 +16,7 @@ import {
parseSandboxUiDeepLink,
} from './sandboxUiDeepLinks.js'
import { shouldAcceptSandboxUiProtocolLink } from './sandboxUiProtocolWindowPolicy.js'
+import { wireDesktopShortcutRouting } from './shortcutRouter.js'
import { installAdaptiveSystemIcon, resolveSystemIconPath } from './systemIcon.js'
const EVENFIRE_APP_NAME = 'Evenfire'
@@ -301,6 +302,11 @@ async function createWindow(): Promise<void> {
},
})
mainWindow = window
+ wireDesktopShortcutRouting({
+ source: 'host',
+ sourceWebContents: window.webContents,
+ trustedRenderer: window.webContents,
+ })
mainWindowRendererReady = false
window.on('closed', () => {
if (mainWindow === window) {
diff --git a/desktop-app/src/preload.ts b/desktop-app/src/preload.ts
index 4bd823c18..29d591cbf 100644
--- a/desktop-app/src/preload.ts
+++ b/desktop-app/src/preload.ts
@@ -1,4 +1,5 @@
import { contextBridge, ipcRenderer } from 'electron'
+import { type DesktopCommandId, isDesktopCommandId } from './desktopCommands.js'
import type { PluginConsentRequest } from './pluginSdkProtocol.js'
import type {
HostMessageRequest,
@@ -7,6 +8,15 @@ import type {
} from './types.js'
const clerum = Object.freeze({
+ shortcuts: {
+ onCommand: (callback: (commandId: DesktopCommandId) => void) => {
+ const listener = (_event: Electron.IpcRendererEvent, payload: { commandId?: unknown }) => {
+ if (isDesktopCommandId(payload?.commandId)) callback(payload.commandId)
+ }
+ ipcRenderer.on('shortcuts:command', listener)
+ return () => ipcRenderer.off('shortcuts:command', listener)
+ },
+ },
auth: {
getSessionState: () => ipcRenderer.invoke('auth:getSessionState'),
getDependenciesHealth: () => ipcRenderer.invoke('auth:getDependenciesHealth'),
diff --git a/desktop-app/src/renderer.d.ts b/desktop-app/src/renderer.d.ts
index 85f97e70c..b37e1a8ce 100644
--- a/desktop-app/src/renderer.d.ts
+++ b/desktop-app/src/renderer.d.ts
@@ -1,3 +1,4 @@
+import type { DesktopCommandId } from './desktopCommands.js'
import type {
PluginAuditEntryView,
PluginConsentRequest,
@@ -53,6 +54,9 @@ import {
declare global {
interface Window {
clerum: {
+ shortcuts: {
+ onCommand: (callback: (commandId: DesktopCommandId) => void) => () => void
+ }
auth: {
getSessionState: () => Promise<SessionState>
getDependenciesHealth: () => Promise<{
diff --git a/desktop-app/src/sandboxUiDriver.ts b/desktop-app/src/sandboxUiDriver.ts
index 20438320c..d39f21764 100644
--- a/desktop-app/src/sandboxUiDriver.ts
+++ b/desktop-app/src/sandboxUiDriver.ts
@@ -8,6 +8,7 @@ import {
applySandboxUiPartitionPolicies,
isSandboxUiNavigationWithinPrefix as isNavigationWithinPrefix,
} from './sandboxUiPartitionPolicies.js'
+import { wireDesktopShortcutRouting } from './shortcutRouter.js'
/**
* Sandbox UI embed driver. Owns the lifecycle of the single active
@@ -106,6 +107,7 @@ type ActiveView = {
rpcProxyOrigin: string
cleanupClientRouteHandoff?: () => void
cleanupParentClosed?: () => void
+ cleanupShortcutRouting?: () => void
}
let active: ActiveView | null = null
@@ -277,6 +279,7 @@ async function teardownActive(reason: 'replaced' | 'closed' | 'parent_closed'):
try {
current.cleanupClientRouteHandoff?.()
current.cleanupParentClosed?.()
+ current.cleanupShortcutRouting?.()
// contentView.removeChildView is the documented teardown step; it both
// detaches the view from layout AND severs the parent's ownership ref.
if (!current.parentWindow.isDestroyed()) {
@@ -411,6 +414,13 @@ export async function mountSandboxUiView(args: MountSandboxUiArgs): Promise<void
cleanupParentClosed,
}
+ active.cleanupShortcutRouting = wireDesktopShortcutRouting({
+ source: 'sandbox',
+ sourceWebContents: view.webContents,
+ trustedRenderer: parentWindow.webContents,
+ isCurrentSource: () => active?.view.webContents.id === view.webContents.id,
+ })
+
parentWindow.contentView.addChildView(view)
// Re-apply bounds after attach. On macOS at fractional DPR (e.g. 2.4 on a
// "More Space" scaled display), setBounds called pre-attach lands the view
diff --git a/desktop-app/src/shortcutRouter.ts b/desktop-app/src/shortcutRouter.ts
new file mode 100644
index 000000000..5e55426f3
--- /dev/null
+++ b/desktop-app/src/shortcutRouter.ts
@@ -0,0 +1,54 @@
+import type { WebContents } from 'electron'
+import {
+ type DesktopCommandSource,
+ type DesktopShortcutInput,
+ type DesktopShortcutPlatform,
+ matchDesktopCommand,
+ platformFromNode,
+} from './desktopCommands.js'
+
+type ShortcutEvent = { preventDefault(): void }
+type ShortcutWebContents = Pick<
+ WebContents,
+ 'id' | 'isDestroyed' | 'isFocused' | 'on' | 'removeListener' | 'send'
+>
+
+export type DesktopShortcutRoute = {
+ source: DesktopCommandSource
+ sourceWebContents: ShortcutWebContents
+ trustedRenderer: ShortcutWebContents
+ platform?: DesktopShortcutPlatform
+ isCurrentSource?: () => boolean
+}
+
+export function routeDesktopShortcut(
+ route: DesktopShortcutRoute,
+ event: ShortcutEvent,
+ input: DesktopShortcutInput
+): boolean {
+ if (
+ route.sourceWebContents.isDestroyed() ||
+ route.trustedRenderer.isDestroyed() ||
+ !route.sourceWebContents.isFocused() ||
+ (route.isCurrentSource && !route.isCurrentSource())
+ ) {
+ return false
+ }
+ const command = matchDesktopCommand(
+ input,
+ route.platform ?? platformFromNode(process.platform),
+ route.source
+ )
+ if (!command) return false
+ event.preventDefault()
+ route.trustedRenderer.send('shortcuts:command', { commandId: command.id })
+ return true
+}
+
+export function wireDesktopShortcutRouting(route: DesktopShortcutRoute): () => void {
+ const listener = (event: Electron.Event, input: Electron.Input) => {
+ routeDesktopShortcut(route, event, input)
+ }
+ route.sourceWebContents.on('before-input-event', listener)
+ return () => route.sourceWebContents.removeListener('before-input-event', listener)
+}