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.

3.7 KB
Raw Blame
import { isWorktreePaletteQueryTooLarge } from './worktree-palette-query-bounds'
import { normalizePaletteText } from './normalized-text'

export const PALETTE_QUERY_MAX_TOKENS = 16

const DIGIT = /\p{N}/u
// Emoji/arrow symbols are query content; punctuation-only filtering must preserve the palette’s :rocket: expansion.
const ALPHANUMERIC = /[\p{L}\p{N}\p{S}\p{Extended_Pictographic}]/u
const LETTERS_ONLY = /^\p{L}+$/u
const LATIN_OR_DIGIT = /^[\p{Script=Latin}\p{Nd}]$/u
const IDENTIFIER_PUNCTUATION = /[./#!_-]/

export type PaletteQueryToken = {
  index: number
  text: string
  /** Maximal alphanumeric runs, e.g. `08/13` -> ['08','13']. */
  components: string[]
  /** Components joined, e.g. `1.4.182` -> `14182`. */
  compact: string
  /** Digit-bearing or identifier-punctuated tokens never fuzzy-match. */
  isIdentifierLike: boolean
  isLetterOnly: boolean
  isPunctuationOnly: boolean
  /** A lone Latin letter/digit may only equal or prefix a word. */
  isSingleLatinCharacter: boolean
  repoBranch: { repo: string; branch: string } | null
}

export type PreparedPaletteQuery =
  | { state: 'empty' }
  | { state: 'invalid'; reason: 'too-large' | 'too-many-tokens' }
  | {
      state: 'ready'
      normalized: string
      tokens: readonly PaletteQueryToken[]
      /** Count before duplicate-token removal; destination recognition uses the complete query. */
      tokenCountBeforeDeduplication: number
    }

function splitComponents(text: string): string[] {
  const components: string[] = []
  let current = ''
  for (const character of text) {
    if (ALPHANUMERIC.test(character)) {
      current += character
    } else if (current) {
      components.push(current)
      current = ''
    }
  }
  if (current) {
    components.push(current)
  }
  return components
}

function parseRepoBranch(text: string): { repo: string; branch: string } | null {
  const slashIndex = text.indexOf('/')
  if (slashIndex <= 0 || slashIndex >= text.length - 1) {
    return null
  }
  return { repo: text.slice(0, slashIndex), branch: text.slice(slashIndex + 1) }
}

export function createPaletteQueryToken(text: string, index: number): PaletteQueryToken {
  const components = splitComponents(text)
  const characters = [...text]
  return {
    index,
    text,
    components,
    compact: components.join(''),
    isIdentifierLike: DIGIT.test(text) || IDENTIFIER_PUNCTUATION.test(text),
    isLetterOnly: LETTERS_ONLY.test(text),
    isPunctuationOnly: !ALPHANUMERIC.test(text),
    isSingleLatinCharacter: characters.length === 1 && LATIN_OR_DIGIT.test(text),
    repoBranch: parseRepoBranch(text)
  }
}

/** Fold per keystroke and split only whitespace so identifier punctuation survives within tokens. */
export function preparePaletteQuery(query: string): PreparedPaletteQuery {
  if (isWorktreePaletteQueryTooLarge(query)) {
    return { state: 'invalid', reason: 'too-large' }
  }
  // Field text is single-spaced, and this value has no source-offset mapping to preserve.
  const normalized = normalizePaletteText(query).normalized.replace(/ +/g, ' ').trim()
  if (!normalized) {
    return { state: 'empty' }
  }

  const seen = new Set<string>()
  const tokens: PaletteQueryToken[] = []
  const rawTokens = normalized.split(' ').filter(Boolean)
  for (const raw of rawTokens) {
    if (!raw || seen.has(raw)) {
      continue
    }
    seen.add(raw)
    tokens.push(createPaletteQueryToken(raw, tokens.length))
    if (tokens.length > PALETTE_QUERY_MAX_TOKENS) {
      return { state: 'invalid', reason: 'too-many-tokens' }
    }
  }

  if (!tokens.length) {
    return { state: 'empty' }
  }
  return {
    state: 'ready',
    normalized,
    tokens,
    tokenCountBeforeDeduplication: rawTokens.length
  }
}

export function isLetterOnlyWord(word: string): boolean {
  return LETTERS_ONLY.test(word)
}