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.7 KB
Raw Blame
import { recognizeAgentProcessFromCommandLine } from './agent-process-recognition'
import { shellReadyMarkerComesFromLineEditor } from './shell-ready-marker-timing'

export type StartupCommandDelivery = 'fast' | 'shell-ready'

type CommandToken = {
  value: string
  startsQuoted: boolean
}

function tokenizeCommandWithQuoteMetadata(command: string): CommandToken[] {
  const tokens: CommandToken[] = []
  let current = ''
  let inToken = false
  let startsQuoted = false
  let quote: '"' | "'" | null = null
  let escaped = false

  for (let index = 0; index < command.length; index += 1) {
    const char = command[index]
    if (escaped) {
      current += char
      escaped = false
      continue
    }
    if (char === '\\' && quote !== "'") {
      const next = command[index + 1]
      if (next && (/\s/.test(next) || next === '"' || next === "'" || next === '\\')) {
        escaped = true
        inToken = true
        continue
      }
    }
    if ((char === '"' || char === "'") && quote === null) {
      if (!inToken) {
        startsQuoted = true
      }
      quote = char
      inToken = true
      continue
    }
    if (quote === char) {
      quote = null
      continue
    }
    if (/\s/.test(char) && quote === null) {
      if (inToken) {
        tokens.push({ value: current, startsQuoted })
        current = ''
        inToken = false
        startsQuoted = false
      }
      continue
    }
    current += char
    inToken = true
  }

  if (inToken) {
    tokens.push({ value: current, startsQuoted })
  }
  return tokens
}

export function hasCodexNativeDraftFlag(command: string | null | undefined): boolean {
  if (recognizeAgentProcessFromCommandLine(command)?.agent !== 'codex' || !command) {
    return false
  }
  const tokens = tokenizeCommandWithQuoteMetadata(command)
  return tokens.some(
    (token, index) =>
      index > 0 &&
      !token.startsQuoted &&
      (token.value === '--prefill' || token.value.startsWith('--prefill='))
  )
}

export function isCodexStartupCommand(command: string | null | undefined): boolean {
  return recognizeAgentProcessFromCommandLine(command)?.agent === 'codex'
}

export function shouldUseShellReadyStartupDelivery(args: {
  command: string | null | undefined
  startupCommandDelivery?: StartupCommandDelivery
  /** Plain Codex waits for line-editor handshake markers to avoid an early launch write echoing twice; use the deciding side’s shell when known. */
  shellPath?: string
}): boolean {
  return (
    args.startupCommandDelivery === 'shell-ready' ||
    hasCodexNativeDraftFlag(args.command) ||
    (args.shellPath !== undefined &&
      shellReadyMarkerComesFromLineEditor(args.shellPath) &&
      isCodexStartupCommand(args.command))
  )
}