#!/usr/bin/env node
// Force strict mode and setup for ESM
"use strict";
import {
  initCpuProfiler
} from "./chunks/chunk-RHZUI73P.js";
import "./chunks/chunk-YDUDLS3E.js";
import {
  initStartupProfiler
} from "./chunks/chunk-ITOGNELQ.js";
import {
  isServeFastPathArgv
} from "./chunks/chunk-FXGLL2HL.js";
import {
  init_esbuild_shims
} from "./chunks/chunk-A4BMJM77.js";
import {
  __name
} from "./chunks/chunk-J2S4EL5Y.js";

// packages/cli/index.ts
init_esbuild_shims();
initStartupProfiler();
initCpuProfiler();
function writeStderrLine(line) {
  process.stderr.write(line.endsWith("\n") ? line : `${line}
`);
}
__name(writeStderrLine, "writeStderrLine");
var getErrnoCode = /* @__PURE__ */ __name((error) => {
  if (!error || typeof error !== "object") {
    return void 0;
  }
  const code = error.code;
  return typeof code === "string" ? code : void 0;
}, "getErrnoCode");
var isExpectedPtyRaceError = /* @__PURE__ */ __name((error) => {
  if (!(error instanceof Error)) {
    return false;
  }
  const message = error.message;
  const code = getErrnoCode(error);
  if (code === "EIO" && message.includes("read") || message.includes("read EIO")) {
    return true;
  }
  if (code === "EAGAIN" && message.includes("read") || message.includes("read EAGAIN")) {
    return true;
  }
  if (message.includes("ioctl(2) failed, EBADF") || message.includes("Cannot resize a pty that has already exited")) {
    return true;
  }
  return false;
}, "isExpectedPtyRaceError");
async function runCliEntry() {
  if (isServeFastPathArgv(process.argv.slice(2))) {
    const { tryRunServeFastPath } = await import("./chunks/fast-path-NZN7D2ES.js");
    if (await tryRunServeFastPath()) return;
  }
  const { main } = await import("./chunks/gemini-HOOOA2N2.js");
  await main();
}
__name(runCliEntry, "runCliEntry");
async function handleCriticalError(error) {
  const [{ FatalError }, { AlreadyReportedError }] = await Promise.all([
    import("./chunks/src-XFYLECN4.js"),
    import("./chunks/errors-KXECHZYF.js")
  ]);
  if (error instanceof FatalError) {
    let errorMessage = error.message;
    if (!process.env["NO_COLOR"]) {
      errorMessage = `\x1B[31m${errorMessage}\x1B[0m`;
    }
    console.error(errorMessage);
    process.exit(error.exitCode);
  }
  if (error instanceof AlreadyReportedError) {
    process.exit(error.exitCode);
  }
  console.error("An unexpected critical error occurred:");
  if (error instanceof Error) {
    console.error(error.stack);
  } else {
    console.error(String(error));
  }
  process.exit(1);
}
__name(handleCriticalError, "handleCriticalError");
process.on("uncaughtException", (error) => {
  if (isExpectedPtyRaceError(error)) {
    return;
  }
  if (error instanceof Error) {
    writeStderrLine(error.stack ?? error.message);
  } else {
    writeStderrLine(String(error));
  }
  process.exit(1);
});
runCliEntry().catch((error) => {
  void handleCriticalError(error).catch((handlerError) => {
    console.error("An unexpected critical error occurred:");
    console.error("Original error:");
    if (error instanceof Error) {
      console.error(error.stack);
    } else {
      console.error(String(error));
    }
    console.error("Error handler failed:");
    if (handlerError instanceof Error) {
      console.error(handlerError.stack);
    } else {
      console.error(String(handlerError));
    }
    process.exit(1);
  });
});
/**
 * @license
 * Copyright 2025 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */
