"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
  if (from && typeof from === "object" || typeof from === "function") {
    for (let key of __getOwnPropNames(from))
      if (!__hasOwnProp.call(to, key) && key !== except)
        __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
  }
  return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// tools/warp_grep/agent/formatter.ts
var formatter_exports = {};
__export(formatter_exports, {
  ToolOutputFormatter: () => ToolOutputFormatter,
  formatAgentToolOutput: () => formatAgentToolOutput
});
module.exports = __toCommonJS(formatter_exports);
var ToolOutputFormatter = class {
  format(toolName, args, output, options = {}) {
    const name = (toolName ?? "").trim();
    if (!name) {
      return "";
    }
    const payload = output?.toString?.()?.trim?.() ?? "";
    const isError = Boolean(options.isError);
    const safeArgs = args ?? {};
    if (!payload && !isError) {
      return "";
    }
    switch (name) {
      case "read":
        return this.formatRead(safeArgs, payload, isError);
      case "list_directory":
        return this.formatListDirectory(safeArgs, payload, isError);
      case "grep":
        return this.formatGrep(safeArgs, payload, isError);
      default:
        return payload ? `<tool_output>
${payload}
</tool_output>` : "";
    }
  }
  formatRead(args, payload, isError) {
    if (isError) {
      return payload;
    }
    const path = this.asString(args.path) || "...";
    const start = args.start;
    const end = args.end;
    const linesArray = args.lines;
    const attributes = [`path="${path}"`];
    if (linesArray && linesArray.length > 0) {
      const rangeStr = linesArray.map(([s, e]) => `${s}-${e}`).join(",");
      attributes.push(`lines="${rangeStr}"`);
    } else if (start !== void 0 && end !== void 0) {
      attributes.push(`lines="${start}-${end}"`);
    }
    return `<read ${attributes.join(" ")}>
${payload}
</read>`;
  }
  formatListDirectory(args, payload, isError) {
    const path = this.asString(args.path) || ".";
    const pattern = this.asString(args.pattern);
    const attributes = [`path="${path}"`];
    if (pattern) {
      attributes.push(`pattern="${pattern}"`);
    }
    if (isError) {
      attributes.push('status="error"');
    }
    return `<list_directory ${attributes.join(" ")}>
${payload}
</list_directory>`;
  }
  formatGrep(args, payload, isError) {
    const pattern = this.asString(args.pattern);
    const subDir = this.asString(args.path);
    const glob = this.asString(args.glob);
    const attributes = [];
    if (pattern !== void 0) {
      attributes.push(`pattern="${pattern}"`);
    }
    if (subDir !== void 0) {
      attributes.push(`sub_dir="${subDir}"`);
    }
    if (glob !== void 0) {
      attributes.push(`glob="${glob}"`);
    }
    if (isError) {
      attributes.push('status="error"');
    }
    const attrText = attributes.length ? ` ${attributes.join(" ")}` : "";
    return `<grep${attrText}>
${payload}
</grep>`;
  }
  asString(value) {
    if (value === null || value === void 0) {
      return void 0;
    }
    return String(value);
  }
};
var sharedFormatter = new ToolOutputFormatter();
function formatAgentToolOutput(toolName, args, output, options = {}) {
  return sharedFormatter.format(toolName, args, output, options);
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
  ToolOutputFormatter,
  formatAgentToolOutput
});
//# sourceMappingURL=formatter.cjs.map