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
/** Carries the sign-in link of an in-flight `codex login` from main to every window. */
export const CODEX_PENDING_LOGIN_URL_CHANGED_CHANNEL = 'codexAccounts:pendingLoginUrlChanged'

/** The rejection a cancelled `codex login` produces; the Accounts pane reads it to keep a cancellation out of the error toast. */
export const CODEX_LOGIN_CANCELLED_MESSAGE = 'Codex sign-in was cancelled.'

const CODEX_AUTH_ERROR_PATTERNS = [
  /access token could not be refreshed/i,
  /authentication session could not be refreshed/i,
  /refresh token (?:has expired|was already used|was revoked)/i,
  /you have since logged out or signed in to another account/i,
  /please (?:log out and )?sign in again/i,
  /please reauthenticate/i,
  /not logged in/i,
  /sign in with chatgpt/i,
  /token data is not available/i,
  /auth (?:is missing|tokens are missing|does not expose)/i,
  // API-key-only homes lack ChatGPT tokens, so account/rateLimits/read cannot recover through a fallback read.
  /chatgpt authentication required/i
]
const ANSI_ESCAPE_RE = new RegExp(`${String.fromCharCode(27)}\\[[0-9;]*[a-zA-Z]`, 'g')

export function isCodexAuthError(error: string | null | undefined): boolean {
  const message = error?.trim()
  if (!message) {
    return false
  }
  return CODEX_AUTH_ERROR_PATTERNS.some((pattern) => pattern.test(message))
}

export function extractCodexAuthError(output: string | null | undefined): string | null {
  if (!output) {
    return null
  }

  let cleanPrefix = ''
  for (const rawLine of iterateCodexOutputLines(output)) {
    const line = rawLine.replace(ANSI_ESCAPE_RE, '').trim()
    if (!line) {
      continue
    }
    if (isCodexAuthError(line)) {
      return line.slice(0, 4_000)
    }
    if (cleanPrefix.length < 4_000) {
      cleanPrefix = cleanPrefix ? `${cleanPrefix}\n${line}` : line
      cleanPrefix = cleanPrefix.slice(0, 4_000)
    }
  }

  return isCodexAuthError(cleanPrefix) ? cleanPrefix : null
}

function* iterateCodexOutputLines(output: string): Generator<string> {
  let lineStart = 0

  for (let index = 0; index < output.length; index++) {
    const code = output.charCodeAt(index)
    if (code !== 10 && code !== 13) {
      continue
    }

    yield output.slice(lineStart, index)
    if (code === 13 && output.charCodeAt(index + 1) === 10) {
      index++
    }
    lineStart = index + 1
  }

  if (lineStart <= output.length) {
    yield output.slice(lineStart)
  }
}