import {
  RemoteCommandsProvider
} from "./chunk-PUGSTXLO.js";
import {
  runWarpGrep,
  runWarpGrepStreaming
} from "./chunk-OO7ZYRV6.js";
import {
  LocalRipgrepProvider
} from "./chunk-4KMBU6T3.js";

// tools/warp_grep/client.ts
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 ?? new LocalRipgrepProvider(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
  }));
  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
  }));
  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 ?? new LocalRipgrepProvider(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 lines = [];
  lines.push(`Found ${result.contexts.length} relevant code sections:
`);
  result.contexts.forEach((ctx, i) => {
    lines.push(`${i + 1}. ${ctx.file}`);
    lines.push("```");
    lines.push(ctx.content);
    lines.push("```");
    lines.push("");
  });
  if (result.summary) {
    lines.push(`Summary: ${result.summary}`);
  }
  return lines.join("\n");
}

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