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.

1.5 KB
Raw Blame
import type { OpenInApplication } from './ui-chrome-types'

export const OPEN_IN_APPLICATIONS_MAX = 8
export const DEFAULT_OPEN_IN_APPLICATIONS: OpenInApplication[] = [
  { id: 'vscode', label: 'VS Code', command: 'code' }
]

type NormalizeOpenInApplicationsOptions = {
  createId?: () => string
  seedDefaults?: boolean
}

function normalizeToken(value: unknown): string {
  return typeof value === 'string' ? value.trim() : ''
}

function makeFallbackId(index: number): string {
  return `open-in-${index + 1}`
}

export function normalizeOpenInApplications(
  value: unknown,
  options: NormalizeOpenInApplicationsOptions = {}
): OpenInApplication[] {
  if (!Array.isArray(value)) {
    return options.seedDefaults ? [...DEFAULT_OPEN_IN_APPLICATIONS] : []
  }

  const normalized: OpenInApplication[] = []
  const seenIds = new Set<string>()

  for (const [index, row] of value.entries()) {
    if (normalized.length >= OPEN_IN_APPLICATIONS_MAX) {
      break
    }
    if (!row || typeof row !== 'object') {
      continue
    }

    const label = normalizeToken((row as { label?: unknown }).label)
    const command = normalizeToken((row as { command?: unknown }).command)
    if (!label || !command) {
      continue
    }

    let id = normalizeToken((row as { id?: unknown }).id)
    if (!id) {
      id = normalizeToken(options.createId?.())
      if (!id) {
        id = makeFallbackId(index)
      }
    }

    if (seenIds.has(id)) {
      continue
    }
    seenIds.add(id)
    normalized.push({ id, label, command })
  }

  return normalized
}