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 { resolveLoginShellEnvironment } from '../main/startup/login-shell-environment'
import { readSessionShellStartupEnvVar } from '../main/pty/shell-startup-env'
import {
  PRIMARY_AGENT_DIR_ENV_BY_KIND,
  SOURCE_AGENT_DIR_ENV_BY_KIND,
  type PiAgentKind
} from '../shared/pi-agent-kind'

function firstNonEmpty(...values: (string | undefined)[]): string | undefined {
  return values.find((value) => typeof value === 'string' && value.length > 0)
}

function readStartupEnv(
  name: string,
  env: Record<string, string>,
  shell: string | undefined
): string | undefined {
  // Why the session env first: it is closer to the user's shell than the relay
  // process env, and fish config lives under its XDG_CONFIG_HOME.
  return readSessionShellStartupEnvVar(name, env, shell)
}

export function resolveOpenCodeSourceConfigDir(
  env: Record<string, string>,
  shell: string | undefined
): string | undefined {
  return firstNonEmpty(
    env.ORCA_OPENCODE_SOURCE_CONFIG_DIR,
    readStartupEnv('OPENCODE_CONFIG_DIR', env, shell),
    env.OPENCODE_CONFIG_DIR
  )
}

export function resolvePiSourceAgentDir(
  env: Record<string, string>,
  shell: string | undefined,
  kind: PiAgentKind
): string | undefined {
  const sourceKey = SOURCE_AGENT_DIR_ENV_BY_KIND[kind]
  const primaryKey = PRIMARY_AGENT_DIR_ENV_BY_KIND[kind]

  const sourceDir = firstNonEmpty(env[sourceKey])
  if (sourceDir) {
    return sourceDir
  }

  const startupDir = readStartupEnv(primaryKey, env, shell)
  if (startupDir) {
    return startupDir
  }

  if (kind === 'prime-agent') {
    return firstNonEmpty(env[primaryKey])
  }

  const overlayKey = kind === 'omp' ? 'ORCA_OMP_CODING_AGENT_DIR' : 'ORCA_PI_CODING_AGENT_DIR'
  const otherOverlayKey = kind === 'omp' ? 'ORCA_PI_CODING_AGENT_DIR' : 'ORCA_OMP_CODING_AGENT_DIR'

  // Reject another Pi-compatible agent’s inherited overlay so the chosen agent resolves its own home.
  if (
    env[primaryKey] &&
    env[primaryKey] !== env[overlayKey] &&
    env[primaryKey] !== env[otherOverlayKey]
  ) {
    return env[primaryKey]
  }
  return undefined
}

export async function resolveOmpConfigDirName(
  env: Record<string, string>,
  shell: string | undefined
): Promise<string | undefined> {
  if (env.PI_CONFIG_DIR !== undefined) {
    return env.PI_CONFIG_DIR || '.omp'
  }
  const profile = await resolveLoginShellEnvironment({
    shellOverride: shell ?? env.SHELL ?? null,
    env
  })
  return profile.PI_CONFIG_DIR === '' ? '.omp' : profile.PI_CONFIG_DIR
}