#!/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-QF32ROX2.js";
import "./chunk-3ZZXQ3CZ.js";
import "./chunk-JMBMLOBP.js";
import "./chunk-U5XQDCK7.js";
import {
  buildCodeToolset
} from "./chunk-AWEULQG6.js";
import "./chunk-MGTBP7GG.js";
import "./chunk-GNS7BAT2.js";
import "./chunk-EQATK2L2.js";
import {
  markPhase
} from "./chunk-ZZM6QJ4W.js";
import "./chunk-MQWO32ZD.js";
import "./chunk-7AST3QQ3.js";
import "./chunk-7JTKBJ2G.js";
import "./chunk-HIYTRCSW.js";
import "./chunk-O5LIHAMP.js";
import "./chunk-FEZK652I.js";
import "./chunk-W46ZMNKO.js";
import "./chunk-PXBQ6IZ7.js";
import "./chunk-7X4JJOO7.js";
import {
  presetNameForSettings,
  resolvePreset
} from "./chunk-LGEKVMMV.js";
import "./chunk-23ZPCIPR.js";
import "./chunk-Q46B3Z7H.js";
import "./chunk-6OWJV3YW.js";
import "./chunk-J2IHQGPQ.js";
import "./chunk-25T6CVUP.js";
import {
  loadDotenv
} from "./chunk-2UQP6H6T.js";
import "./chunk-ASOLXV67.js";
import "./chunk-SZ5XES2N.js";
import "./chunk-TEDWJKEI.js";
import "./chunk-E5WCLUIU.js";
import "./chunk-XJXDHAES.js";
import "./chunk-TAIKVL35.js";
import "./chunk-ZWHSHFDP.js";
import {
  detectForeignAgentPlatform
} from "./chunk-JNTMOX7G.js";
import "./chunk-PLHAZOLZ.js";
import "./chunk-PEMG6CUB.js";
import "./chunk-DFX5ZH5L.js";
import "./chunk-PB3MAFEI.js";
import "./chunk-S4XVGLRW.js";
import {
  sanitizeName
} from "./chunk-RRXUIPWG.js";
import "./chunk-ZAEJWKXB.js";
import "./chunk-QX5TWXRZ.js";
import {
  t
} from "./chunk-YEF7C4XI.js";
import {
  loadApiKey,
  loadPreset,
  readConfig
} from "./chunk-WMTMMSXU.js";
import "./chunk-XXC2BYTV.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 loadedPreset = loadPreset();
  const presetSettings = resolvePreset(loadedPreset);
  const resolvedModel = opts.model ?? presetSettings.model;
  loadDotenv();
  const cfgKey = loadApiKey();
  if (cfgKey && !process.env.DEEPSEEK_API_KEY) {
    process.env.DEEPSEEK_API_KEY = cfgKey;
  }
  const { codeSystemPrompt } = await import("./prompt-MC3U5KRP.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,
    preset: opts.model ? void 0 : presetNameForSettings(presetSettings),
    autoEscalate: opts.model ? false : presetSettings.autoEscalate,
    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: readConfig().mcp,
    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-R4IHI7SR.js.map