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.

4.1 KB
Raw Blame
import { OMP_DRAFT_LAUNCH_PREFIX } from './omp-draft-launch'
import { tokenizeStartupCommand, type AgentStartupShell } from './tui-agent-startup-shell'

export const ORCA_OMP_FRESH_CONFIG_ENV = 'ORCA_OMP_FRESH_CONFIG'
export const OMP_FRESH_CONFIG_FILENAME = 'fresh-session.yml'
export const OMP_FRESH_CONFIG_SOURCE = 'autoResume: false\n'

// Unknown flags may consume values or select a subcommand; leave those commands intact.
const VALUE_FLAGS = new Set([
  '--model',
  '--provider',
  '--thinking',
  '--config',
  '--profile',
  '--extension',
  '-e',
  '--system-prompt',
  '--append-system-prompt',
  '--tools',
  '--skill',
  '--theme',
  '--api-key'
])
const SWITCH_FLAGS = new Set(['--no-extensions', '--no-skills', '--no-prompt-templates'])
// `@` keeps an echo-on prompt from printing the substituted body.
const CMD_FRESH_CONFIG_FOR = 'for %! in (.) do @'

/** Apply fresh intent to one launch command, never the saved resume configuration. */
export function withFreshOmpLaunch(command: string, shell: AgentStartupShell, suffix = ''): string {
  const parsed = tokenizeStartupCommand(command, shell)
  if (!parsed.ok || parsed.spans.some((span) => span.divergesFromShell)) {
    return command + suffix
  }
  const executable = parsed.tokens[0]?.split(/[\\/]/).at(-1)?.toLowerCase()
  if (!['omp', 'omp.exe', 'omp.cmd', 'omp.bat', 'omp.sh', 'omp.js'].includes(executable ?? '')) {
    return command + suffix
  }
  let index = parsed.tokens[1] === 'launch' ? 2 : 1
  for (; index < parsed.tokens.length; index++) {
    const token = parsed.tokens[index]
    const equals = token.indexOf('=')
    const flag = equals === -1 ? token : token.slice(0, equals)
    if (VALUE_FLAGS.has(flag)) {
      if (equals === -1 && ++index >= parsed.tokens.length) {
        return command + suffix
      }
    } else if (!SWITCH_FLAGS.has(token)) {
      return command + suffix
    }
  }
  const path =
    shell === 'cmd'
      ? `"%~$${ORCA_OMP_FRESH_CONFIG_ENV}:!"`
      : shell === 'powershell'
        ? `"$env:${ORCA_OMP_FRESH_CONFIG_ENV}"`
        : `"$${ORCA_OMP_FRESH_CONFIG_ENV}"`
  const launch = `${command} --config ${path}${suffix}`
  const error =
    'Orca: fresh OMP settings are unavailable on this host. Restart the terminal after updating Orca.'
  if (shell === 'powershell') {
    return `if (${path} -and (Test-Path -LiteralPath ${path} -PathType Leaf)) { ${launch} } else { $global:LASTEXITCODE = 1; Write-Error '${error}' }`
  }
  if (shell === 'cmd') {
    // By-name %~$NAME:! reads paths even inside expanded startup variables, where %NAME% stays literal.
    // The FOR variable substitutes only %!/%~…! across the body; quoted prompt ! is escaped, although raw overrides could form it.
    return `${CMD_FRESH_CONFIG_FOR}if exist ${path} (if not exist "${path.slice(1, -1)}\\*" (${launch}) else (echo ${error} 1>&2 & verify invalid 2>nul)) else (echo ${error} 1>&2 & verify invalid 2>nul)`
  }
  const available = `printenv ${ORCA_OMP_FRESH_CONFIG_ENV} >/dev/null && test -f ${path}`
  // && is supported by bash, zsh, and fish; invoking directly preserves shell aliases/functions.
  return `${available} || printf '%s\\n' '${error}' >&2; ${available} && ${launch}`
}

/** Recognize only the host-settings guards generated by this module. */
export function isFreshOmpLaunchCommand(command: string | undefined): boolean {
  if (command?.startsWith(OMP_DRAFT_LAUNCH_PREFIX)) {
    return true
  }
  const guarded = command?.startsWith('( ') ? command.slice(2) : command
  return Boolean(
    guarded &&
    [
      `printenv ${ORCA_OMP_FRESH_CONFIG_ENV} >/dev/null && test -f "$${ORCA_OMP_FRESH_CONFIG_ENV}"`,
      `if ("$env:${ORCA_OMP_FRESH_CONFIG_ENV}" -and (Test-Path -LiteralPath "$env:${ORCA_OMP_FRESH_CONFIG_ENV}" -PathType Leaf))`,
      `${CMD_FRESH_CONFIG_FOR}if exist "%~$${ORCA_OMP_FRESH_CONFIG_ENV}:!" (if not exist "%~$${ORCA_OMP_FRESH_CONFIG_ENV}:!\\*"`,
      // Lines built before the by-name guard can still be persisted or come from an older host.
      `if exist "%${ORCA_OMP_FRESH_CONFIG_ENV}%" (if not exist "%${ORCA_OMP_FRESH_CONFIG_ENV}%\\*"`
    ].some((prefix) => guarded.startsWith(prefix))
  )
}