import {
  runWarpGrep,
  runWarpGrepStreaming
} from "./chunk-YY7NZLAI.js";
import {
  RemoteCommandsProvider
} from "./chunk-PUGSTXLO.js";

// tools/warp_grep/client.ts
async function getLocalProvider(repoRoot, excludes) {
  const { LocalRipgrepProvider } = await import("./tools/warp_grep/providers/local.js");
  return new LocalRipgrepProvider(repoRoot, excludes);
}
var WarpGrepClient = class {
  config;
  constructor(config = {}) {
    this.config = {
      morphApiKey: config.morphApiKey,
      morphApiUrl: config.morphApiUrl,
      debug: config.debug,
      timeout: config.timeout,
      retryConfig: config.retryConfig
    };
  }
  execute(input) {
    const toolConfig = {
      repoRoot: input.repoRoot,
      remoteCommands: input.remoteCommands,
      provider: input.provider,
      excludes: input.excludes,
      includes: input.includes,
      debug: input.debug ?? this.config.debug,
      morphApiKey: this.config.morphApiKey,
      morphApiUrl: this.config.morphApiUrl,
      retryConfig: this.config.retryConfig,
      timeout: this.config.timeout
    };
    if (input.streamSteps) {
      return executeToolCallStreaming({ query: input.query }, toolConfig);
    }
    return executeToolCall({ query: input.query }, toolConfig);
  }
};
function executeWarpGrep(input, config) {
  const client = new WarpGrepClient(config);
  if (input.streamSteps) {
    return client.execute({ ...input, streamSteps: true });
  }
  return client.execute({ ...input, streamSteps: false });
}
async function executeToolCall(input, config) {
  const parsed = typeof input === "string" ? JSON.parse(input) : input;
  const provider = config.remoteCommands ? new RemoteCommandsProvider(config.repoRoot, config.remoteCommands) : config.provider ?? await getLocalProvider(config.repoRoot, config.excludes);
  const result = await runWarpGrep({
    query: parsed.query,
    repoRoot: config.repoRoot,
    provider,
    excludes: config.excludes,
    includes: config.includes,
    debug: config.debug ?? false,
    morphApiKey: config.morphApiKey,
    morphApiUrl: config.morphApiUrl,
    retryConfig: config.retryConfig,
    timeout: config.timeout
  });
  const finish = result.finish;
  if (result.terminationReason !== "completed" || !finish?.metadata) {
    return { success: false, error: "Search did not complete" };
  }
  const contexts = (finish.resolved ?? []).map((r) => ({
    file: r.path,
    content: r.content,
    lines: r.ranges
  }));
  return { success: true, contexts, summary: finish.payload };
}
function processAgentResult(result) {
  const finish = result.finish;
  if (result.terminationReason !== "completed" || !finish?.metadata) {
    return { success: false, error: "Search did not complete" };
  }
  const contexts = (finish.resolved ?? []).map((r) => ({
    file: r.path,
    content: r.content,
    lines: r.ranges
  }));
  return { success: true, contexts, summary: finish.payload };
}
async function* executeToolCallStreaming(input, config) {
  const parsed = typeof input === "string" ? JSON.parse(input) : input;
  const provider = config.remoteCommands ? new RemoteCommandsProvider(config.repoRoot, config.remoteCommands) : config.provider ?? await getLocalProvider(config.repoRoot, config.excludes);
  const generator = runWarpGrepStreaming({
    query: parsed.query,
    repoRoot: config.repoRoot,
    provider,
    excludes: config.excludes,
    includes: config.includes,
    debug: config.debug ?? false,
    morphApiKey: config.morphApiKey,
    morphApiUrl: config.morphApiUrl,
    retryConfig: config.retryConfig,
    timeout: config.timeout
  });
  let agentResult;
  for (; ; ) {
    const { value, done } = await generator.next();
    if (done) {
      agentResult = value;
      break;
    }
    yield value;
  }
  return processAgentResult(agentResult);
}
function formatResult(result) {
  if (!result.success) {
    return `Search failed: ${result.error}`;
  }
  if (!result.contexts || result.contexts.length === 0) {
    return "No relevant code found. Try rephrasing your query.";
  }
  const parts = [];
  parts.push(`Morph Fast Context subagent performed search on repository:
`);
  parts.push("Relevant context found:");
  for (const ctx of result.contexts) {
    const rangeStr = !ctx.lines || ctx.lines === "*" ? "*" : ctx.lines.map(([s, e]) => `${s}-${e}`).join(",");
    parts.push(`- ${ctx.file}:${rangeStr}`);
  }
  parts.push("\nHere is the content of files:\n");
  for (const ctx of result.contexts) {
    const rangeStr = !ctx.lines || ctx.lines === "*" ? "" : ` lines="${ctx.lines.map(([s, e]) => `${s}-${e}`).join(",")}"`;
    parts.push(`<file path="${ctx.file}"${rangeStr}>`);
    parts.push(ctx.content);
    parts.push("</file>\n");
  }
  return parts.join("\n");
}

export {
  WarpGrepClient,
  executeWarpGrep,
  executeToolCall,
  formatResult
};
//# sourceMappingURL=chunk-23R562QJ.js.map