import {
  createCodeStorageHttpCommands
} from "./chunk-GVGJIXV2.js";
import {
  RemoteCommandsProvider
} from "./chunk-PUGSTXLO.js";
import {
  parseGitHubUrl
} from "./chunk-63VHBANJ.js";
import {
  runWarpGrep,
  runWarpGrepStreaming
} from "./chunk-HHRHUUGN.js";

// tools/warp_grep/client.ts
var DEFAULT_CODE_SEARCH_URL = "https://morphllm.com";
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 = { ...config };
  }
  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);
  }
  searchGitHub(input) {
    if (input.streamSteps) {
      return this._searchGitHubStreaming(input);
    }
    return this._searchGitHubAsync(input);
  }
  /**
   * Read a single file from a public GitHub repository
   *
   * @example
   * ```typescript
   * const result = await client.readGitHubFile({
   *   github: 'vercel/next.js',
   *   path: 'src/server/app-render/index.tsx',
   *   startLine: 1,
   *   endLine: 50,
   * });
   * if (result.success) console.log(result.content);
   * ```
   */
  async readGitHubFile(input) {
    return executeGitHubReadFile(input, { timeout: this.config.timeout });
  }
  async _resolveGitHubRepo(input) {
    const { owner, repo } = parseGitHubUrl(input.github);
    const baseUrl = this.config.codeSearchUrl || DEFAULT_CODE_SEARCH_URL;
    const importRes = await fetch(`${baseUrl}/api/code-search/get-or-create?url=${encodeURIComponent(`${owner}/${repo}`)}`);
    if (!importRes.ok) {
      const text = await importRes.text().catch(() => importRes.statusText);
      throw new Error(`Failed to import repo ${owner}/${repo}: ${text}`);
    }
    const { repoId, defaultBranch } = await importRes.json();
    return {
      repo,
      remoteCommands: createCodeStorageHttpCommands({ baseUrl, repoId, branch: input.branch || defaultBranch })
    };
  }
  async _searchGitHubAsync(input) {
    try {
      const { repo, remoteCommands } = await this._resolveGitHubRepo(input);
      return this.execute({
        query: input.query,
        repoRoot: repo,
        remoteCommands
      });
    } catch (error) {
      return { success: false, error: error instanceof Error ? error.message : String(error) };
    }
  }
  async *_searchGitHubStreaming(input) {
    let repo;
    let remoteCommands;
    try {
      ({ repo, remoteCommands } = await this._resolveGitHubRepo(input));
    } catch (error) {
      return { success: false, error: error instanceof Error ? error.message : String(error) };
    }
    const generator = this.execute({
      query: input.query,
      repoRoot: repo,
      remoteCommands,
      streamSteps: true
    });
    let result;
    for (; ; ) {
      const { value, done } = await generator.next();
      if (done) {
        result = value;
        break;
      }
      yield value;
    }
    return result;
  }
};
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) {
    const errorDetails = result.errors?.map((e) => e.message).join("; ") || "unknown reason";
    console.error(`[warp_grep] executeToolCall failed. Reason: ${result.terminationReason}. Errors: ${errorDetails}. Turns: ${result.timings?.turns?.length ?? 0}`);
    return { success: false, error: `Search did not complete: ${errorDetails}` };
  }
  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) {
    const errorDetails = result.errors?.map((e) => e.message).join("; ") || "unknown reason";
    console.error(`[warp_grep] processAgentResult failed. Reason: ${result.terminationReason}. Errors: ${errorDetails}. Turns: ${result.timings?.turns?.length ?? 0}`);
    return { success: false, error: `Search did not complete: ${errorDetails}` };
  }
  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);
}
async function fetchRawGitHubFile(owner, repo, filePath, branch, timeout) {
  const branches = branch ? [branch] : ["HEAD"];
  const fallbacks = ["main", "master", "develop"];
  const fetchWithTimeout = (url) => timeout ? fetch(url, { signal: AbortSignal.timeout(timeout) }) : fetch(url);
  for (const b of branches) {
    const url = `https://raw.githubusercontent.com/${owner}/${repo}/${b}/${filePath}`;
    const res = await fetchWithTimeout(url);
    if (res.ok) return { content: await res.text(), branch: b };
  }
  for (const fb of fallbacks) {
    if (branch && fb === branch) continue;
    const url = `https://raw.githubusercontent.com/${owner}/${repo}/${fb}/${filePath}`;
    const res = await fetchWithTimeout(url);
    if (res.ok) return { content: await res.text(), branch: fb };
  }
  throw new Error(`File not found: ${owner}/${repo}/${filePath}`);
}
async function executeGitHubReadFile(input, config) {
  try {
    const { owner, repo } = parseGitHubUrl(input.github);
    const { content: raw, branch } = await fetchRawGitHubFile(
      owner,
      repo,
      input.path,
      input.branch,
      config?.timeout
    );
    const allLines = raw.split("\n");
    const totalLines = allLines.length;
    const start = Math.max(1, input.startLine ?? 1);
    const end = Math.min(totalLines, input.endLine ?? totalLines);
    const selected = allLines.slice(start - 1, end);
    const padWidth = String(end).length;
    const numbered = selected.map((line, i) => `${String(start + i).padStart(padWidth)}|${line}`).join("\n");
    return {
      success: true,
      content: numbered,
      path: input.path,
      github: `${owner}/${repo}`,
      branch,
      lineRange: [start, end],
      totalLines
    };
  } catch (error) {
    return {
      success: false,
      error: error instanceof Error ? error.message : String(error)
    };
  }
}
function formatGitHubReadFileResult(result) {
  if (!result.success) {
    return `Failed to read file: ${result.error}`;
  }
  const rangeStr = result.lineRange ? ` (lines ${result.lineRange[0]}-${result.lineRange[1]} of ${result.totalLines})` : "";
  return `File: ${result.github}/${result.path}${rangeStr}

${result.content}`;
}
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,
  executeToolCallStreaming,
  executeGitHubReadFile,
  formatGitHubReadFileResult,
  formatResult
};
//# sourceMappingURL=chunk-S4RLT56A.js.map