import {
  executeCodebaseSearch
} from "./chunk-S4IHNTCC.js";
import {
  CODEBASE_SEARCH_DESCRIPTION,
  CODEBASE_SEARCH_SYSTEM_PROMPT
} from "./chunk-YQMPVJ2L.js";

// tools/codebase_search/anthropic.ts
function createCodebaseSearchTool(config) {
  const toolDefinition = {
    name: "codebase_search",
    description: CODEBASE_SEARCH_DESCRIPTION,
    input_schema: {
      type: "object",
      properties: {
        explanation: {
          type: "string",
          description: "One sentence explanation as to why this tool is being used, and how it contributes to the goal."
        },
        query: {
          type: "string",
          description: 'A complete question about what you want to understand. Ask as if talking to a colleague: "How does X work?", "What happens when Y?", "Where is Z handled?"'
        },
        target_directories: {
          type: "array",
          items: { type: "string" },
          description: "Prefix directory paths to limit search scope (single directory only, no glob patterns). Use [] to search entire repo."
        },
        limit: {
          type: "number",
          description: "Maximum results to return (default: 10)"
        }
      },
      required: ["query", "target_directories", "explanation"]
    },
    cache_control: { type: "ephemeral" }
  };
  return Object.assign(toolDefinition, {
    execute: async (input) => {
      return executeCodebaseSearch(input, config);
    },
    formatResult: (result) => {
      return formatResult(result);
    },
    getSystemPrompt: () => {
      return CODEBASE_SEARCH_SYSTEM_PROMPT;
    }
  });
}
function formatResult(result) {
  if (!result.success) {
    return `Search failed: ${result.error}`;
  }
  if (result.results.length === 0) {
    return "No matching code found. Try rephrasing your query or broadening the search scope.";
  }
  const lines = [];
  lines.push(`Found ${result.results.length} relevant code sections (searched ${result.stats.candidatesRetrieved} candidates in ${result.stats.searchTimeMs}ms):
`);
  result.results.forEach((r, i) => {
    const relevance = (r.rerankScore * 100).toFixed(1);
    lines.push(`${i + 1}. ${r.filepath} (${relevance}% relevant)`);
    lines.push(`   Symbol: ${r.symbolPath}`);
    lines.push(`   Language: ${r.language}`);
    lines.push(`   Lines: ${r.startLine}-${r.endLine}`);
    lines.push(`   Code:`);
    const codeLines = r.content.split("\n");
    codeLines.slice(0, Math.min(codeLines.length, 20)).forEach((line) => {
      lines.push(`     ${line}`);
    });
    if (codeLines.length > 20) {
      lines.push(`     ... (${codeLines.length - 20} more lines)`);
    }
    lines.push("");
  });
  return lines.join("\n");
}

export {
  createCodebaseSearchTool
};
//# sourceMappingURL=chunk-43MMMNOD.js.map