// tools/warp_grep/agent/parser.ts
var VALID_COMMANDS = ["list_directory", "ripgrep", "read", "finish"];
function isValidCommand(name) {
  return VALID_COMMANDS.includes(name);
}
function parseQwen3ToolCalls(text) {
  const tools = [];
  const toolCallRegex = /<tool_call>\s*<function=([a-z_][a-z0-9_]*)>([\s\S]*?)<\/function>\s*<\/tool_call>/gi;
  let match;
  while ((match = toolCallRegex.exec(text)) !== null) {
    const funcName = match[1].toLowerCase();
    const body = match[2];
    if (!isValidCommand(funcName)) continue;
    const params = {};
    const paramRegex = /<parameter=([a-z_][a-z0-9_]*)>([\s\S]*?)<\/parameter>/gi;
    let paramMatch;
    while ((paramMatch = paramRegex.exec(body)) !== null) {
      params[paramMatch[1].toLowerCase()] = paramMatch[2].trim();
    }
    if (funcName === "ripgrep") {
      const pattern = params.pattern;
      if (!pattern) continue;
      const args = {
        pattern,
        path: params.path || ".",
        ...params.glob && { glob: params.glob },
        ...params.context_lines && { context_lines: parseInt(params.context_lines, 10) },
        ...params.case_sensitive && { case_sensitive: params.case_sensitive === "true" }
      };
      tools.push({ name: "grep", arguments: args });
    } else if (funcName === "list_directory") {
      const command = params.command;
      const directPath = params.path;
      let dirPath = directPath || ".";
      if (!directPath && command) {
        const tokens = command.trim().split(/\s+/);
        const pathTokens = tokens.slice(1).filter((t) => !t.startsWith("-") && !t.startsWith("|") && !t.startsWith("\\("));
        if (pathTokens.length > 0) {
          dirPath = pathTokens[0];
        }
      }
      tools.push({ name: "list_directory", arguments: { path: dirPath, pattern: params.pattern || null } });
    } else if (funcName === "read") {
      const filePath = params.path;
      if (!filePath) continue;
      const args = { path: filePath };
      const linesStr = params.lines;
      if (linesStr) {
        const ranges = [];
        for (const rangeStr of linesStr.split(",")) {
          const trimmed = rangeStr.trim();
          if (!trimmed) continue;
          const [s, e] = trimmed.split("-").map((v) => parseInt(v.trim(), 10));
          if (Number.isFinite(s) && Number.isFinite(e)) {
            ranges.push([s, e]);
          } else if (Number.isFinite(s)) {
            ranges.push([s, s]);
          }
        }
        if (ranges.length === 1) {
          args.start = ranges[0][0];
          args.end = ranges[0][1];
        } else if (ranges.length > 1) {
          args.lines = ranges;
        }
      }
      tools.push({ name: "read", arguments: args });
    } else if (funcName === "finish") {
      if (params.result && !params.files) {
        tools.push({ name: "finish", arguments: { files: [], textResult: params.result } });
        continue;
      }
      const filesStr = params.files;
      if (!filesStr) {
        tools.push({ name: "finish", arguments: { files: [], textResult: "No relevant code found." } });
        continue;
      }
      const files = [];
      for (const line of filesStr.split("\n")) {
        const trimmed = line.trim();
        if (!trimmed) continue;
        const colonIdx = trimmed.indexOf(":");
        if (colonIdx === -1) {
          files.push({ path: trimmed, lines: "*" });
        } else {
          const filePath = trimmed.slice(0, colonIdx);
          const rangesPart = trimmed.slice(colonIdx + 1);
          const ranges = [];
          for (const rangeStr of rangesPart.split(",")) {
            const rt = rangeStr.trim();
            if (!rt || rt === "*") {
              files.push({ path: filePath, lines: "*" });
              break;
            }
            const [s, e] = rt.split("-").map((v) => parseInt(v.trim(), 10));
            if (Number.isFinite(s) && Number.isFinite(e)) {
              ranges.push([s, e]);
            } else if (Number.isFinite(s)) {
              ranges.push([s, s]);
            }
          }
          if (ranges.length > 0) {
            files.push({ path: filePath, lines: ranges });
          } else if (!files.some((f) => f.path === filePath)) {
            files.push({ path: filePath, lines: "*" });
          }
        }
      }
      if (files.length > 0) {
        tools.push({ name: "finish", arguments: { files } });
      } else {
        tools.push({ name: "finish", arguments: { files: [], textResult: filesStr } });
      }
    }
  }
  return tools;
}
var LLMResponseParser = class {
  parse(text) {
    if (typeof text !== "string") {
      throw new TypeError("Command text must be a string.");
    }
    const withoutThink = text.replace(/<think>[\s\S]*?<\/think>/gi, "");
    return parseQwen3ToolCalls(withoutThink);
  }
};

export {
  LLMResponseParser
};
//# sourceMappingURL=chunk-VCKJ22DX.js.map