// Force strict mode and setup for ESM
"use strict";
import {
  initCpuProfiler
} from "./chunks/chunk-5YAXXN3N.js";
import {
  initStartupProfiler
} from "./chunks/chunk-QSWDGAIC.js";
import {
  normalizeServeFastPathArgv
} from "./chunks/chunk-Q7YDKOGY.js";
import "./chunks/chunk-B3ILMEBG.js";
import {
  init_esbuild_shims
} from "./chunks/chunk-5O2XNYP6.js";
import {
  __name
} from "./chunks/chunk-J2S4EL5Y.js";

// packages/cli/src/cli.ts
init_esbuild_shims();
import { pathToFileURL } from "node:url";
initStartupProfiler();
initCpuProfiler();
var TOP_LEVEL_COMMANDS = [
  ["auth", "Configure authentication (removed)"],
  ["channel <command>", "Manage messaging channels (Telegram, Discord, etc.)"],
  ["extensions <command>", "Manage Qwen Code extensions."],
  ["hooks", "Manage Qwen Code hooks (use /hooks in interactive mode)."],
  ["mcp", "Manage MCP servers"],
  [
    "review <command>",
    "Internal helpers used by the /review skill (PR worktree setup, context fetch, rules loading, presubmit checks, cleanup)"
  ],
  [
    "serve",
    "Run Qwen Code as a local HTTP daemon (Stage 1 experimental: --http-bridge)"
  ],
  ["sessions <command>", "Manage Qwen Code sessions"],
  ["update", "Check for Qwen Code updates and install if available"]
];
var MCP_COMMANDS = [
  ["add <name> <commandOrUrl> [args...]", "Add a server"],
  ["remove <name>", "Remove a server"],
  ["list", "List all configured MCP servers"],
  ["reconnect [server-name]", "Reconnect to MCP servers"],
  ["approve [name]", "Approve a pending MCP server"],
  ["reject [name]", "Reject a pending MCP server"]
];
var TOP_LEVEL_HELP_OPTIONS = [
  ["model", { alias: "m", type: "string", description: "Model" }],
  [
    "fallback-model",
    {
      type: "array",
      description: "Fallback model(s) for capacity errors, repeatable or comma-separated (max 3)"
    }
  ],
  [
    "prompt",
    {
      alias: "p",
      type: "string",
      description: "Prompt. Appended to input on stdin (if any)."
    }
  ],
  [
    "prompt-interactive",
    {
      alias: "i",
      type: "string",
      description: "Execute the provided prompt and continue in interactive mode"
    }
  ],
  [
    "safe-mode",
    {
      type: "boolean",
      description: "Disable all customizations (context files, hooks, extensions, skills, MCP servers) for troubleshooting."
    }
  ],
  [
    "sandbox",
    {
      alias: "s",
      type: "boolean",
      description: "Run in sandbox?"
    }
  ],
  [
    "output-format",
    {
      alias: "o",
      type: "string",
      choices: ["text", "json", "stream-json"],
      description: "The format of the CLI output."
    }
  ],
  [
    "continue",
    {
      alias: "c",
      type: "boolean",
      description: "Resume the most recent session for the current project."
    }
  ],
  [
    "resume",
    {
      alias: "r",
      type: "string",
      description: "Resume a specific session by its ID. Use without an ID to show session picker."
    }
  ]
];
var VALUE_FLAGS = /* @__PURE__ */ new Set([
  "--model",
  "-m",
  "--fallback-model",
  "--prompt",
  "-p",
  "--prompt-interactive",
  "-i",
  "--output-format",
  "-o",
  "--resume",
  "-r"
]);
function writeStdoutLine(line) {
  process.stdout.write(line.endsWith("\n") ? line : `${line}
`);
}
__name(writeStdoutLine, "writeStdoutLine");
function hasFlag(argv, long, short) {
  for (let i = 0; i < argv.length; i++) {
    const arg = argv[i];
    if (arg === "--") {
      return false;
    }
    if (VALUE_FLAGS.has(arg)) {
      i++;
      continue;
    }
    if (arg === long || arg === short) {
      return true;
    }
  }
  return false;
}
__name(hasFlag, "hasFlag");
async function buildTopLevelHelpParser() {
  const { default: yargs } = await import("./chunks/yargs-6H2AUULL.js");
  const parser = yargs([]).scriptName("qwen").usage(
    "Usage: qwen [options] [command]\n\nQwen Code - Launch an interactive CLI, use -p/--prompt for non-interactive mode"
  ).version("0.19.11").alias("v", "version").help().alias("h", "help").strict().demandCommand(0, 0);
  for (const [option, config] of TOP_LEVEL_HELP_OPTIONS) {
    parser.option(option, config);
  }
  for (const [command, description] of TOP_LEVEL_COMMANDS) {
    parser.command(command, description);
  }
  return parser;
}
__name(buildTopLevelHelpParser, "buildTopLevelHelpParser");
function firstPositionalArg(argv) {
  for (let i = 0; i < argv.length; i++) {
    const arg = argv[i];
    if (arg === "--") {
      return void 0;
    }
    if (VALUE_FLAGS.has(arg)) {
      i++;
      continue;
    }
    if (!arg.startsWith("-")) {
      return arg;
    }
  }
  return void 0;
}
__name(firstPositionalArg, "firstPositionalArg");
function normalizeMcpFastPathArgv(argv) {
  if (argv[0] === "mcp" && argv[1] === "--") {
    return [argv[0], ...argv.slice(2)];
  }
  return argv;
}
__name(normalizeMcpFastPathArgv, "normalizeMcpFastPathArgv");
function resolveBootstrapRoute(rawArgv) {
  const argv = normalizeServeFastPathArgv(rawArgv);
  if (hasFlag(argv, "--version", "-v")) {
    return "version";
  }
  const firstArg = argv[0];
  if (firstArg === "serve") {
    return "serve";
  }
  if (firstArg === "mcp") {
    return "mcp";
  }
  const firstPositional = firstPositionalArg(argv);
  if (hasFlag(argv, "--help", "-h") && firstPositional === void 0) {
    return "help";
  }
  return "default";
}
__name(resolveBootstrapRoute, "resolveBootstrapRoute");
async function printTopLevelHelp() {
  const help = await (await buildTopLevelHelpParser()).getHelp();
  writeStdoutLine(help);
}
__name(printTopLevelHelp, "printTopLevelHelp");
function printMcpHelp() {
  const lines = [
    "Usage: qwen mcp <command>",
    "",
    "Manage MCP servers",
    "",
    "Commands:",
    ...MCP_COMMANDS.map(
      ([command, description]) => `  qwen mcp ${command}  ${description}`
    )
  ];
  writeStdoutLine(lines.join("\n"));
}
__name(printMcpHelp, "printMcpHelp");
async function printBootstrapVersion() {
  if ("0.19.11") {
    writeStdoutLine("0.19.11");
    return;
  }
  const { getCliVersion } = await import("./chunks/version-LIFSOGSE.js");
  writeStdoutLine(await getCliVersion());
}
__name(printBootstrapVersion, "printBootstrapVersion");
async function runMcpFastPath(rawArgv) {
  const argv = normalizeMcpFastPathArgv(normalizeServeFastPathArgv(rawArgv));
  const hasSubcommand = argv.length > 1 && !argv[1].startsWith("-");
  if (!hasSubcommand) {
    printMcpHelp();
    return;
  }
  const [{ default: yargsInstance }, { mcpCommand }] = await Promise.all([
    import("./chunks/yargs-6H2AUULL.js"),
    import("./chunks/mcp-VSNQTL7J.js")
  ]);
  const parser = yargsInstance([]).scriptName("qwen").command(mcpCommand).version(false).help().alias("h", "help").strict().strictCommands().demandCommand(1, "You need at least one command before continuing.").fail((message, error, yargs) => {
    writeStderrLine(message || error?.message || "Unknown argument error");
    yargs.showHelp();
    process.exitCode = 1;
  }).exitProcess(false);
  if (hasFlag(argv.slice(2), "--help", "-h")) {
    await parseYargsHelp(parser, argv);
    return;
  }
  await parseYargsCommand(parser, argv);
}
__name(runMcpFastPath, "runMcpFastPath");
async function parseYargsHelp(parser, argv) {
  await new Promise((resolve, reject) => {
    parser.parse(
      argv,
      (error, _argv, output) => {
        if (output) {
          writeStdoutLine(output);
        }
        if (error) {
          reject(error);
          return;
        }
        resolve();
      }
    );
  });
}
__name(parseYargsHelp, "parseYargsHelp");
async function parseYargsCommand(parser, argv) {
  await new Promise((resolve) => {
    parser.parse(
      argv,
      (error, _argv, output) => {
        if (output) {
          writeStdoutLine(output);
        }
        if (error) {
          writeStderrLine(error.message);
          process.exitCode = 1;
        }
        resolve();
      }
    );
  });
}
__name(parseYargsCommand, "parseYargsCommand");
async function runCliEntry(rawArgv = process.argv.slice(2)) {
  const argv = normalizeServeFastPathArgv(rawArgv);
  const route = resolveBootstrapRoute(argv);
  if (route === "version") {
    await printBootstrapVersion();
    return;
  }
  if (route === "serve") {
    const { tryRunServeFastPath } = await import("./chunks/fast-path-T5XY7SYC.js");
    if (await tryRunServeFastPath(argv)) {
      return;
    }
  } else if (route === "mcp") {
    await runMcpFastPath(argv);
    return;
  } else if (route === "help") {
    await printTopLevelHelp();
    return;
  }
  const { main } = await import("./chunks/gemini-IHBCN7MO.js");
  await main();
}
__name(runCliEntry, "runCliEntry");
function getErrnoCode(error) {
  if (!error || typeof error !== "object") {
    return void 0;
  }
  const code = error.code;
  return typeof code === "string" ? code : void 0;
}
__name(getErrnoCode, "getErrnoCode");
function isExpectedPtyRaceError(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;
  }
  return message.includes("ioctl(2) failed, EBADF") || message.includes("Cannot resize a pty that has already exited");
}
__name(isExpectedPtyRaceError, "isExpectedPtyRaceError");
async function handleCriticalError(error) {
  const [{ FatalError }, { AlreadyReportedError }] = await Promise.all([
    import("./chunks/src-O6NJAI32.js"),
    import("./chunks/errors-CM35VNDY.js")
  ]);
  if (error instanceof FatalError) {
    let errorMessage = error.message;
    if (!process.env["NO_COLOR"]) {
      errorMessage = `\x1B[31m${errorMessage}\x1B[0m`;
    }
    writeStderrLine(errorMessage);
    process.exit(error.exitCode);
  }
  if (error instanceof AlreadyReportedError) {
    process.exit(error.exitCode);
  }
  writeStderrLine("An unexpected critical error occurred:");
  if (error instanceof Error) {
    writeStderrLine(error.stack ?? error.message);
  } else {
    writeStderrLine(String(error));
  }
  process.exit(1);
}
__name(handleCriticalError, "handleCriticalError");
function writeStderrLine(line) {
  process.stderr.write(line.endsWith("\n") ? line : `${line}
`);
}
__name(writeStderrLine, "writeStderrLine");
async function runCliEntryPoint(run = runCliEntry, handleError = 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);
  });
  try {
    await run();
  } catch (error) {
    try {
      await handleError(error);
    } catch (handlerError) {
      writeStderrLine("An unexpected critical error occurred:");
      writeStderrLine("Original error:");
      if (error instanceof Error) {
        writeStderrLine(error.stack ?? error.message);
      } else {
        writeStderrLine(String(error));
      }
      writeStderrLine("Error handler failed:");
      if (handlerError instanceof Error) {
        writeStderrLine(handlerError.stack ?? handlerError.message);
      } else {
        writeStderrLine(String(handlerError));
      }
      process.exit(1);
    }
  }
}
__name(runCliEntryPoint, "runCliEntryPoint");
if (process.argv[1] !== void 0 && import.meta.url === pathToFileURL(process.argv[1]).href) {
  void runCliEntryPoint();
}
export {
  MCP_COMMANDS,
  TOP_LEVEL_COMMANDS,
  handleCriticalError,
  isExpectedPtyRaceError,
  resolveBootstrapRoute,
  runCliEntry,
  runCliEntryPoint
};
/**
 * @license
 * Copyright 2026 Qwen Team
 * SPDX-License-Identifier: Apache-2.0
 */
