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

// tools/codebase_search/openai.ts
var codebaseSearchTool = {
  type: "function",
  function: {
    name: "codebase_search",
    description: CODEBASE_SEARCH_DESCRIPTION,
    parameters: {
      type: "object",
      properties: {
        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."
        },
        explanation: {
          type: "string",
          description: "One sentence explanation as to why this tool is being used, and how it contributes to the goal."
        },
        limit: {
          type: "number",
          description: "Maximum results to return (default: 10)"
        }
      },
      required: ["query", "target_directories", "explanation"]
    }
  }
};
function createCodebaseSearchTool(config) {
  const toolDefinition = {
    type: "function",
    function: {
      name: "codebase_search",
      description: CODEBASE_SEARCH_DESCRIPTION,
      parameters: {
        type: "object",
        properties: {
          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."
          },
          explanation: {
            type: "string",
            description: "One sentence explanation as to why this tool is being used, and how it contributes to the goal."
          },
          limit: {
            type: "number",
            description: "Maximum results to return (default: 10)"
          }
        },
        required: ["query", "target_directories", "explanation"]
      }
    }
  };
  return Object.assign(toolDefinition, {
    execute: async (input) => {
      const parsedInput = typeof input === "string" ? JSON.parse(input) : input;
      return executeCodebaseSearch(parsedInput, config);
    },
    formatResult: (result) => {
      return formatResult(result);
    },
    getSystemPrompt: () => {
      return CODEBASE_SEARCH_SYSTEM_PROMPT;
    }
  });
}
async function execute(input, config) {
  return executeCodebaseSearch(input, config);
}
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 removing directory filters.";
  }
  const lines = [];
  lines.push(`Found ${result.results.length} relevant code sections (${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");
}
function getSystemPrompt() {
  return CODEBASE_SEARCH_SYSTEM_PROMPT;
}
var openai_default = { createCodebaseSearchTool, execute, formatResult, getSystemPrompt };

export {
  codebaseSearchTool,
  createCodebaseSearchTool,
  execute,
  formatResult,
  getSystemPrompt,
  openai_default
};
//# sourceMappingURL=chunk-2VG5U65J.js.map