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.

2.4 KB
Raw Blame
import { getRepoExecutionHostId, normalizeExecutionHostId } from './execution-host'
import { getRepoHostIdentityForParts } from './repo-host-identity'
import type { Repo } from './repo-types'
import type { ManualRepoOrderEntry } from './ui-chrome-types'

function getEntryKey(entry: ManualRepoOrderEntry): string {
  return getRepoHostIdentityForParts(entry.repoId, entry.hostId)
}

export function normalizeManualRepoOrder(value: unknown): ManualRepoOrderEntry[] {
  if (!Array.isArray(value)) {
    return []
  }
  const entries: ManualRepoOrderEntry[] = []
  const seen = new Set<string>()
  for (const candidate of value) {
    if (!candidate || typeof candidate !== 'object') {
      continue
    }
    const raw = candidate as { hostId?: unknown; repoId?: unknown }
    const hostId = typeof raw.hostId === 'string' ? normalizeExecutionHostId(raw.hostId) : null
    const repoId = typeof raw.repoId === 'string' ? raw.repoId : ''
    if (!hostId || !repoId.trim()) {
      continue
    }
    const entry = { hostId, repoId }
    const key = getEntryKey(entry)
    if (seen.has(key)) {
      continue
    }
    seen.add(key)
    entries.push(entry)
  }
  return entries
}

export function getManualRepoOrder(repos: readonly Repo[]): ManualRepoOrderEntry[] {
  return normalizeManualRepoOrder(
    repos.map((repo) => ({ hostId: getRepoExecutionHostId(repo), repoId: repo.id }))
  )
}

export function applyManualRepoOrder(
  repos: readonly Repo[],
  order: readonly ManualRepoOrderEntry[] | null | undefined
): readonly Repo[] {
  const normalized = normalizeManualRepoOrder(order)
  // Preserve identity for no-op reorders: fresh arrays invalidate every identity-keyed repo memo downstream.
  if (normalized.length === 0) {
    return repos
  }
  const rankByKey = new Map(normalized.map((entry, index) => [getEntryKey(entry), index]))
  const ordered = repos
    .map((repo, index) => ({
      repo,
      index,
      rank: rankByKey.get(getEntryKey({ hostId: getRepoExecutionHostId(repo), repoId: repo.id }))
    }))
    .sort((a, b) => {
      if (a.rank === undefined && b.rank === undefined) {
        return a.index - b.index
      }
      if (a.rank === undefined) {
        return 1
      }
      if (b.rank === undefined) {
        return -1
      }
      return a.rank - b.rank || a.index - b.index
    })
    .map(({ repo }) => repo)
  return ordered.every((repo, index) => repo === repos[index]) ? repos : ordered
}