#!/usr/bin/env node
import { createRequire as __cr } from 'node:module'; if (typeof globalThis.require === 'undefined') { globalThis.require = __cr(import.meta.url); }
import {
  chatCommand
} from "./chunk-BW4P3JYO.js";
import "./chunk-M4LM5SLT.js";
import "./chunk-JMBMLOBP.js";
import "./chunk-CXZCSPIU.js";
import "./chunk-VMZW3YHZ.js";
import "./chunk-32YKCY2Y.js";
import "./chunk-PFB4LEVB.js";
import "./chunk-7DHACDSF.js";
import "./chunk-F7JD4QYH.js";
import {
  buildCodeToolset
} from "./chunk-IW4A2JR3.js";
import "./chunk-B6JKNF2A.js";
import "./chunk-ZGOT2XWK.js";
import "./chunk-ZEOTMLIQ.js";
import "./chunk-4GQNVO65.js";
import {
  markPhase
} from "./chunk-ZZM6QJ4W.js";
import "./chunk-OYWLLOF7.js";
import "./chunk-J3X7DDCJ.js";
import "./chunk-S27PR2TP.js";
import "./chunk-AMRYJH5Y.js";
import "./chunk-C3LX42AG.js";
import "./chunk-AA6VB4SX.js";
import "./chunk-UMTBSIXZ.js";
import "./chunk-WJZ3CDLL.js";
import {
  detectForeignAgentPlatform
} from "./chunk-QCO3QTUI.js";
import "./chunk-7JV6OMXH.js";
import "./chunk-N3KCKIVR.js";
import "./chunk-U25OJR4Y.js";
import "./chunk-PLHAZOLZ.js";
import "./chunk-L3VPEESB.js";
import "./chunk-T34EQHMF.js";
import "./chunk-6B42HNEQ.js";
import "./chunk-7IRUJE52.js";
import "./chunk-YMYX6QTC.js";
import "./chunk-BOWSNGQC.js";
import "./chunk-ZN46HA7M.js";
import "./chunk-25T6CVUP.js";
import "./chunk-Z2FWA3WK.js";
import "./chunk-6UNHNVJR.js";
import {
  sanitizeName
} from "./chunk-BQ77G77G.js";
import "./chunk-UZJ23BIP.js";
import {
  loadDotenv
} from "./chunk-2UQP6H6T.js";
import {
  t
} from "./chunk-UPXKHQCB.js";
import {
  DEFAULT_MODEL,
  bridgeEndpointEnv,
  loadModel,
  normalizeMcpConfig,
  readConfig,
  specToRaw
} from "./chunk-3VTKGIKG.js";
import "./chunk-TUK7OWJA.js";

// src/cli/commands/code.tsx
import { readFileSync } from "fs";
import { basename, resolve } from "path";
async function codeCommand(opts = {}) {
  markPhase("code_command_enter");
  const resolvedModel = opts.model?.trim() || loadModel() || DEFAULT_MODEL;
  loadDotenv();
  bridgeEndpointEnv();
  const { codeSystemPrompt } = await import("./prompt-L3E7PGM7.js");
  const rootDir = resolve(opts.dir ?? process.cwd());
  const session = opts.noSession ? void 0 : `code-${sanitizeName(basename(rootDir))}`;
  markPhase("semantic_bootstrap_start");
  const { tools, jobs, registerRooted, reBootstrapSemantic, semantic } = await buildCodeToolset({
    rootDir
  });
  markPhase(
    semantic.enabled ? "semantic_bootstrap_done_enabled" : "semantic_bootstrap_done_skipped"
  );
  process.stderr.write(
    `${t("startup.codeRooted", {
      rootDir,
      session: session ?? t("startup.ephemeral"),
      tools: tools.size,
      semantic: semantic.enabled ? t("startup.semanticOn") : ""
    })}
`
  );
  const foreign = detectForeignAgentPlatform(rootDir);
  if (foreign) {
    process.stderr.write(t("code.workspaceConflict", { platforms: foreign.join(", ") }));
  }
  process.once("exit", () => {
    void jobs.shutdown();
  });
  let systemAppendFileContents;
  if (opts.systemAppend !== void 0 && opts.systemAppend.trim().length === 0) {
    process.stderr.write(t("code.systemAppendEmpty"));
  }
  if (opts.systemAppendFile) {
    const filePath = resolve(opts.systemAppendFile);
    try {
      systemAppendFileContents = readFileSync(filePath, "utf8");
    } catch (err) {
      const e = err;
      const errorDetails = e.code ? `[${e.code}] ${e.message}` : e.message;
      process.stderr.write(t("code.systemAppendFileReadError", { filePath, errorDetails }));
      process.exit(1);
    }
  }
  let currentRoot = rootDir;
  let semanticEnabled = semantic.enabled;
  const codeRebuildSystem = () => codeSystemPrompt(currentRoot, {
    hasSemanticSearch: semanticEnabled,
    systemAppend: opts.systemAppend,
    systemAppendFile: systemAppendFileContents,
    modelId: resolvedModel
  });
  await chatCommand({
    model: resolvedModel,
    budgetUsd: opts.budgetUsd,
    system: codeRebuildSystem(),
    rebuildSystem: codeRebuildSystem,
    transcript: opts.transcript,
    session,
    seedTools: tools,
    codeMode: {
      rootDir,
      jobs,
      reregisterTools: registerRooted,
      reBootstrapSemantic: async (root) => {
        const r = await reBootstrapSemantic(root);
        semanticEnabled = r.enabled;
        return r;
      },
      onRootChange: (newRoot) => {
        currentRoot = newRoot;
      }
    },
    mcp: normalizeMcpConfig(readConfig()).map(specToRaw),
    forceResume: opts.forceResume,
    forceNew: opts.forceNew,
    noDashboard: opts.noDashboard,
    openDashboard: opts.openDashboard,
    dashboardPort: opts.dashboardPort,
    dashboardHost: opts.dashboardHost,
    dashboardToken: opts.dashboardToken,
    noMouse: opts.noMouse
  });
}
export {
  codeCommand
};
//# sourceMappingURL=code-VWQOG7VS.js.map