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.5 KB
Raw Blame
import { MARINE_CREATURES } from './marine-creatures'

// Bound generated leaves after generation; leave prompts general so user naming instructions own style.
export const MAX_BRANCH_NAME_WORDS = 4

const CREATURE_NAMES_LOWER = new Set(MARINE_CREATURES.map((name) => name.toLowerCase()))

function normalizeName(name: string): string {
  return name.trim().toLowerCase()
}

/** Auto-rename only Orca-picked creature leaves, including numbered collision variants; never overwrite a deliberately user-named branch. */
export function isAutoGeneratedCreatureBranchName(branchLeaf: string): boolean {
  const normalized = normalizeName(branchLeaf)
  if (!normalized) {
    return false
  }
  // Strip one creation collision suffix before testing creature membership.
  const base = normalized.replace(/-\d+$/, '')
  return CREATURE_NAMES_LOWER.has(base)
}

/**
 * Turn raw model output into a safe, short kebab-case branch leaf. Returns ''
 * when nothing usable remains, which callers treat as "skip the rename".
 */
export function sanitizeBranchSlug(raw: string, maxWords = MAX_BRANCH_NAME_WORDS): string {
  const words = raw
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .split('-')
    .filter(Boolean)
  if (words.length === 0) {
    return ''
  }
  return words.slice(0, maxWords).join('-')
}

/** Strip only a matching configured prefix from generated slugs to avoid doubled prefixes/display names. Prefix-only output becomes empty so callers skip rename; genuine leading content survives. */
export function stripConfiguredBranchPrefix(
  slug: string,
  prefix: string | null | undefined
): string {
  if (!prefix) {
    return slug
  }
  const prefixSlug = prefix
    .trim()
    .toLowerCase()
    .replace(/[^a-z0-9]+/g, '-')
    .replace(/^-+|-+$/g, '')
  if (!prefixSlug) {
    return slug
  }
  if (slug === prefixSlug) {
    return ''
  }
  return slug.startsWith(`${prefixSlug}-`) ? slug.slice(prefixSlug.length + 1) : slug
}

/** Humanize the branch slug for sidebar naming so workspace labels stay aligned with renamed branches. */
export function humanizeBranchSlug(slug: string): string {
  const joined = slug.split('-').filter(Boolean).join(' ')
  if (!joined) {
    return ''
  }
  return joined.charAt(0).toUpperCase() + joined.slice(1)
}

export type BranchNameWorkContext = {
  /** The user's first prompt to the agent in this workspace. */
  firstPrompt: string
  /** The agent's first response, when it has already arrived. Optional because
   *  the rename fires as soon as work begins, before a reply may exist. */
  assistantMessage?: string
}

/** Share naming prompts across local/SSH. Keep defaults general for user Source Control AI/template guidance; enforce Git safety/length after generation via sanitizeBranchSlug. */
export function buildBranchNamePrompt(context: BranchNameWorkContext, customPrompt = ''): string {
  const sections: string[] = []
  const prompt = customPrompt.trim()
  // Lead with user guidance so it owns naming style over the default.
  if (prompt) {
    sections.push(prompt, '')
  }
  sections.push(
    prompt
      ? 'Generate a git branch name that summarizes the coding task described below.'
      : 'Generate a short git branch name that summarizes the coding task described below.',
    'Output ONLY the branch name on a single line, nothing else.',
    ''
  )
  sections.push('User request:', context.firstPrompt.trim())
  const assistant = context.assistantMessage?.trim()
  if (assistant) {
    sections.push('', "Agent's initial response:", assistant)
  }
  return sections.join('\n')
}