Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

7.4 KB
Raw Blame
import type {
  KeybindingActionId,
  KeybindingDefinition,
  KeybindingMatchOptions,
  KeybindingOverrides,
  TerminalShortcutPolicy
} from './types'
import {
  DEFINITIONS_BY_ID,
  KEYBINDING_DEFINITIONS,
  getKeybindingPlatform,
  isDigitIndexActionId
} from './definitions'
import { keybindingConflictIdentities } from './matching-key'
import {
  normalizeKeybindingWithOptions,
  normalizeOptionsForAction,
  canonicalizeDigitIndexBinding
} from './normalization'

export function getDefaultBindings(
  definition: KeybindingDefinition,
  platform: NodeJS.Platform
): string[] {
  return definition.defaultBindings[getKeybindingPlatform(platform)].map((binding) => {
    const normalized = normalizeKeybindingWithOptions(binding, {
      allowBareKeybindings: definition.allowBareKeybindings === true,
      allowShiftOnlyKeybindings: definition.allowShiftOnlyKeybindings === true
    })
    return normalized.ok ? normalized.value : binding
  })
}

function getConfiguredBindings(
  actionId: KeybindingActionId,
  platform: NodeJS.Platform,
  overrides?: KeybindingOverrides,
  definition = DEFINITIONS_BY_ID.get(actionId)
): string[] {
  const override = overrides?.[actionId]
  if (Array.isArray(override)) {
    // Canonicalize hand-edited digit overrides to <mods>+1 so display/conflicts agree across the range.
    if (isDigitIndexActionId(actionId)) {
      const canonical: string[] = []
      for (const binding of override) {
        const normalized = canonicalizeDigitIndexBinding(binding)
        if (normalized.ok && !canonical.includes(normalized.value)) {
          canonical.push(normalized.value)
        }
      }
      return canonical
    }
    return override.flatMap((binding) => {
      const normalized = normalizeKeybindingWithOptions(
        binding,
        normalizeOptionsForAction(actionId)
      )
      return normalized.ok ? [normalized.value] : []
    })
  }
  return definition ? getDefaultBindings(definition, platform) : []
}

export function keybindingConflictGroups(definition: KeybindingDefinition): readonly string[] {
  return (
    definition.conflictGroups ?? [definition.conflictGroup ?? definition.scope, definition.scope]
  )
}

export function bindingsOverlap(
  left: KeybindingDefinition,
  a: string,
  right: KeybindingDefinition,
  b: string,
  platform: NodeJS.Platform
): boolean {
  return (
    keybindingConflictGroups(left).some((group) =>
      keybindingConflictGroups(right).includes(group)
    ) &&
    keybindingConflictIdentities(left.id, a, platform).some((identity) =>
      keybindingConflictIdentities(right.id, b, platform).includes(identity)
    )
  )
}

export function getEffectiveKeybindingsForAction(
  actionId: KeybindingActionId,
  platform: NodeJS.Platform,
  overrides?: KeybindingOverrides
): string[] {
  const definition = DEFINITIONS_BY_ID.get(actionId)
  return definition
    ? getEffectiveKeybindingsForDefinition(definition, platform, overrides)
    : getConfiguredBindings(actionId, platform, overrides)
}

const implicitBindings = new WeakMap<KeybindingOverrides, Map<NodeJS.Platform, string[]>>()
const emptyOverrides: KeybindingOverrides = {}

export function getEffectiveKeybindingsForDefinition(
  definition: KeybindingDefinition,
  platform: NodeJS.Platform,
  overrides: KeybindingOverrides = emptyOverrides,
  definitions: readonly KeybindingDefinition[] = KEYBINDING_DEFINITIONS
): string[] {
  const configured = getConfiguredBindings(definition.id, platform, overrides, definition)
  if (!definition.implicitDefaultYields || Object.hasOwn(overrides, definition.id)) {
    return configured
  }
  const cacheable =
    definition === DEFINITIONS_BY_ID.get('terminal.toggleNativeChat') &&
    definitions === KEYBINDING_DEFINITIONS
  const cached = cacheable ? implicitBindings.get(overrides)?.get(platform) : undefined
  if (cached) {
    return cached
  }
  const effective = configured.filter(
    (binding) =>
      !definitions.some(
        (other) =>
          other.id !== definition.id &&
          Object.hasOwn(overrides, other.id) &&
          getConfiguredBindings(other.id, platform, overrides, other).some(
            (claim) =>
              bindingsOverlap(definition, binding, other, claim, platform) &&
              !getDefaultBindings(other, platform).some((ownDefault) =>
                bindingsOverlap(other, claim, other, ownDefault, platform)
              )
          )
      )
  )
  if (cacheable) {
    const byPlatform = implicitBindings.get(overrides) ?? new Map()
    byPlatform.set(platform, effective)
    implicitBindings.set(overrides, byPlatform)
  }
  return effective
}

export function normalizeTerminalShortcutPolicy(
  policy: TerminalShortcutPolicy | null | undefined
): TerminalShortcutPolicy {
  return policy === 'terminal-first' ? 'terminal-first' : 'orca-first'
}

const TUI_CONFLICT_ACTIONS = new Set<KeybindingActionId>([
  'terminal.search',
  'terminal.clear',
  'terminal.selectAll',
  'terminal.splitRight',
  'terminal.splitDown',
  'terminal.closePane',
  'worktree.history.back',
  'worktree.history.forward'
])

export function isKeybindingAllowedInTerminal(definition: KeybindingDefinition): boolean {
  // Keep clipboard, input-source and pane navigation controls available.
  return (
    !TUI_CONFLICT_ACTIONS.has(definition.id) &&
    (definition.scope === 'terminal' || definition.allowInTerminal === true)
  )
}

export function isKeybindingPotentialTerminalConflict(definition: KeybindingDefinition): boolean {
  return !isKeybindingAllowedInTerminal(definition)
}

// Dictation passes through when voice is off; tests pin the other main and guest routing priorities.
const MAIN_PROCESS_INTERCEPTED_ACTIONS: Partial<
  Record<KeybindingActionId, 'beforeBrowserChords' | 'afterBrowserChords'>
> = {
  'worktree.history.back': 'beforeBrowserChords',
  'worktree.history.forward': 'beforeBrowserChords',
  'floatingTerminal.toggle': 'beforeBrowserChords',
  'zoom.in': 'beforeBrowserChords',
  'zoom.out': 'beforeBrowserChords',
  'zoom.reset': 'beforeBrowserChords',
  'app.settings': 'afterBrowserChords',
  'app.forceReload': 'afterBrowserChords',
  'worktree.palette': 'afterBrowserChords',
  'sidebar.left.toggle': 'afterBrowserChords',
  'sidebar.right.toggle': 'afterBrowserChords',
  'worktree.quickOpen': 'afterBrowserChords',
  'workspace.create': 'afterBrowserChords',
  'workspace.delete': 'afterBrowserChords',
  'workspace.openBoard': 'afterBrowserChords',
  'view.tasks': 'afterBrowserChords',
  'dashboard.toggle': 'afterBrowserChords',
  'tab.previousRecent': 'beforeBrowserChords',
  'workspace.selectByIndex': 'afterBrowserChords',
  'tab.selectByIndex': 'afterBrowserChords',
  'tab.openQuickCommandsMenu': 'afterBrowserChords'
}

export function isKeybindingInterceptedByMainProcess(definition: KeybindingDefinition): boolean {
  return MAIN_PROCESS_INTERCEPTED_ACTIONS[definition.id] !== undefined
}

export function isKeybindingInterceptedBeforeBrowserChords(
  definition: KeybindingDefinition
): boolean {
  return MAIN_PROCESS_INTERCEPTED_ACTIONS[definition.id] === 'beforeBrowserChords'
}

export function keybindingIsActiveInContext(
  definition: KeybindingDefinition,
  options: KeybindingMatchOptions = {}
): boolean {
  if (options.context !== 'terminal') {
    return true
  }
  // Why: Orca-first keeps app shortcuts inside terminals; terminal-first is the escape hatch for shells and TUIs.
  if (normalizeTerminalShortcutPolicy(options.terminalShortcutPolicy) === 'orca-first') {
    return true
  }
  return isKeybindingAllowedInTerminal(definition)
}