import { u as util, e as exec, o as os, a as env, w as window, p as path, b as workspace, U as Uri, l as logError } from "./agent.worker-DwbwPMEN.mjs";
import { w as wrapInActiveSpan, T as TokenCounterUtils, C as ContextItemSource } from "./browser-BOPUs1B6.mjs";
var define_process_default = { env: {} };
const _exec = util.promisify(exec);
async function getContextFileFromShell(command) {
  return wrapInActiveSpan("commands.context.command", async (span) => {
    var _a, _b, _c;
    const rootDir = os.homedir() || define_process_default.env.HOME || define_process_default.env.USERPROFILE || "";
    if (!env.shell) {
      void window.showErrorMessage("Shell command is not supported your current workspace.");
      return [];
    }
    const filteredCommand = command.replaceAll(/(\s~\/)/g, ` ${rootDir}${path.sep}`);
    const cwd = (_c = (_b = (_a = workspace.workspaceFolders) == null ? void 0 : _a[0]) == null ? void 0 : _b.uri) == null ? void 0 : _c.fsPath;
    try {
      const { stdout, stderr } = await _exec(filteredCommand, { cwd, encoding: "utf8" });
      const output = JSON.stringify(stdout ?? stderr).trim();
      if (!output) {
        throw new Error("Empty output");
      }
      const content = outputWrapper.replace("{command}", command).replace("{output}", output);
      const size = await TokenCounterUtils.countTokens(content);
      return [
        {
          type: "file",
          content,
          title: "Terminal Output",
          uri: Uri.file(command),
          source: ContextItemSource.Terminal,
          size
        }
      ];
    } catch (error) {
      logError("getContextFileFromShell", "failed", { verbose: error });
      void window.showErrorMessage(error.message);
      throw new Error("Failed to get shell output for Custom Command.");
    }
  });
}
const outputWrapper = `
Terminal output from the \`{command}\` command enclosed between <OUTPUT0412> tags:
<OUTPUT0412>
{output}
</OUTPUT0412>`;
export {
  getContextFileFromShell
};
