代码语义搜索工具

一个基于向量嵌入的代码语义搜索工具,支持多模型集成和离线操作,适用于代码库的语义搜索和调用图分析。

  • by anrgct
  • TypeScript
  • 本地部署
  • 121 星标
  • 更新于
在 GitHub 查看 anrgct/autodev-codebase

使用说明

@autodev/代码库

一种基于向量嵌入的代码语义搜索工具,具有MCP服务器和多模型集成。可以用作纯CLI工具。支持Ollama进行完全本地嵌入和重新登录,为您的代码库提供完整的离线操作和隐私保护。

# Semantic code search - Find code by meaning, not just keywords
╭─ ~/workspace/autodev-codebase 
╰─❯ codebase search "user manage" --demo
Found 20 results in 5 files for: "user manage"

==================================================
File: "hello.js"
==================================================
< class UserManager > (L7-20)
class UserManager {
  constructor() {
    this.users = [];
  }

  addUser(user) {
    this.users.push(user);
    console.log('User added:', user.name);
  }

  getUsers() {
    return this.users;
  }
}
……

# Call graph analysis - Trace function call relationships and execution paths
╭─ ~/workspace/autodev-codebase 
╰─❯ codebase call --demo --query="app,addUser"
Connections between app, addUser:

Found 2 matching node(s):
  - demo/app:L1-29
  - demo/hello.UserManager.addUser:L12-15

Direct connections:
  - demo/app:L1-29 → demo/hello.UserManager.addUser:L12-15

Chains found:
  - demo/app:L1-29 → demo/hello.UserManager.addUser:L12-15

# Code outline with AI summaries - Understand code structure at a glance
╭─ ~/workspace/autodev-codebase 
╰─❯ codebase outline 'hello.js' --demo --summarize
# hello.js (23 lines)
└─ Defines a greeting function that logs a personalized hello message and returns a welcome string. Implements a UserManager class managing an array of users with methods to add users and retrieve the current user list. Exports both components for external use.

   2--5 | function greetUser
   └─ Implements user greeting logic by logging a personalized hello message and returning a welcome message

   7--20 | class UserManager
   └─ Manages user data with methods to add users to a list and retrieve all stored users

   12--15 | method addUser
   └─ Adds a user to the users array and logs a confirmation message with the user's name.

🚀 特性

  • 🔍 语义代码搜索:使用高级嵌入模型的基于矢量的搜索
  • 🔗 调用图分析:跟踪函数调用关系和执行路径
  • 🌐 MCP 服务器:基于HTTP的MCP服务器,带SSE和stdio适配器
  • 💻 纯CLI工具:没有GUI依赖关系的独立命令行界面
  • ⚙️ 分层配置:CLI、项目和全局配置管理
  • 🎯 高级路径过滤:带支架扩展和排除的球形图案
  • 🌲 树保姆解析:支持40多种编程语言
  • 💾 Qdrant集成:高性能矢量数据库
  • 🔄 多个供应商:OpenAI、Ollama、Jina、Gemini、Mistral、OpenRouter、Vercel
  • 📊 实时观看:自动索引更新
  • ⚡ 批处理:高效的并行处理
  • 📝 代码大纲提取:生成带有AI摘要的结构化代码大纲
  • 💨 依赖性分析缓存:智能缓存,重新分析速度提高10-50倍

📦 安装

1.依赖关系

brew install ollama ripgrep
ollama serve
ollama pull nomic-embed-text

2.Qdrant

docker run -d -p 6333:6333 -p 6334:6334 --name qdrant qdrant/qdrant

3.安装

npm install -g @autodev/codebase
codebase config --set embedderProvider=ollama,embedderModelId=nomic-embed-text

🛠️ 快速开始

# Demo mode (recommended for first-time)
# Creates a demo directory in current working directory for testing

# Index & search
codebase index --demo
codebase search "user greet" --demo

# Call graph analysis
codebase call --demo --query="app,addUser"

# MCP server
codebase index --serve --demo

📋 命令

📝 代码大纲

# Extract code structure (functions, classes, methods)
codebase outline "src/**/*.ts"

# Generate code structure with AI summaries
codebase outline "src/**/*.ts" --summarize

# View only file-level summaries
codebase outline "src/**/*.ts" --summarize --title

# Clear summary cache
codebase outline --clear-summarize-cache

🔗 调用图分析

# 📊 Statistics Overview (no --query)
codebase call                           # Show statistics overview
codebase call --json                    # JSON format
codebase call src/commands              # Analyze specific directory

# 🔍 Function Query (with --query)
codebase call --query="getUser"         # Single function call tree (default depth: 3)
codebase call --query="main" --depth=5  # Custom depth
codebase call --query="getUser,validateUser"  # Multi-function connections (default depth: 10)

# 🎨 Visualization
codebase call --viz graph.json          # Export Cytoscape.js format
codebase call --open                    # Open interactive viewer
codebase call --viz graph.json --open   # Export and open

# Specify workspace (works for both modes)
codebase call --path=/my/project --query="main"

查询模式:

  • 精确匹配: --query="functionName" 或 --query="*ClassName.methodName"
  • 通配符: * (任何字符), ? (单个字符)
    • 示例: --query="get*", --query="*User*", --query="*.*.get*"
  • 单一功能: --query="main" -显示呼叫树(向上+向下)
    • 默认深度: 3 (避免输出过多)
  • 多重功能: --query="main,helper" -分析函数之间的连接路径
    • 默认深度: 10 (路径查找需要更深入的搜索)

支持的语言:

  • Types/JavaScript (.ts、.tsx、.js、.jsx)
  • python 南美国家巴拉圭的缩写(Paraguay)
  • Java Java
  • C/C++ (.c、.h、.cpp、.cc、.cxx、.hpp、.hxx、.cc++)
  • C (.cs)
  • 锈 (.rs)
  • 去 (.go)

🔍 索引和搜索

# Index the codebase
codebase index --path=/my/project --force

# Search with filters
codebase search "error handling" --path-filters="src/**/*.ts"

# Search with custom limit and minimum score
codebase search "authentication" --limit=20 --min-score=0.7
codebase search "API" -l 30 -S 0.5

# Search in JSON format
codebase search "authentication" --json

# Clear index data
codebase index --clear-cache --path=/my/project

🌐 MCP 服务器

# HTTP mode (recommended)
codebase index --serve --port=3001 --path=/my/project

# Stdio adapter
codebase stdio --server-url=http://localhost:3001/mcp

⚙️ 配置

# View config
codebase config --get
codebase config --get embedderProvider --json

# Set config
codebase config --set embedderProvider=ollama,embedderModelId=nomic-embed-text
codebase config --set --global qdrantUrl=http://localhost:6333

🚀 高级功能

🔍 LLM助力搜索重新排名

启用LLM重新排名以显著提高搜索相关性:

# Enable reranking with Ollama (recommended)
codebase config --set rerankerEnabled=true,rerankerProvider=ollama,rerankerOllamaModelId=qwen3-vl:4b-instruct

# Or use OpenAI-compatible providers
codebase config --set rerankerEnabled=true,rerankerProvider=openai-compatible,rerankerOpenAiCompatibleModelId=deepseek-chat

# Search with automatic reranking
codebase search "user authentication"  # Results are automatically reranked by LLM

优点:

  • 🎯 精度高:LLM理解向量相似性之外的语义相关性
  • 📊 智能评分:根据查询相关性,结果以0-10的等级重新排序
  • ⚡ 批量处理:高效处理具有可配置批大小的大型结果集
  • 🎛️ 阈值控制:使用筛选结果 rerankerMinScore 只保留高质量的比赛

路径过滤和导出

# Path filtering with brace expansion and exclusions
codebase search "API" --path-filters="src/**/*.ts,lib/**/*.js"
codebase search "utils" --path-filters="{src,test}/**/*.ts"

# Export results in JSON format for scripts
codebase search "auth" --json

路径过滤和导出

# Path filtering with brace expansion and exclusions
codebase search "API" --path-filters="src/**/*.ts,lib/**/*.js"
codebase search "utils" --path-filters="{src,test}/**/*.ts"

# Export results in JSON format for scripts
codebase search "auth" --json

⚙️ 配置

配置层(优先级顺序)

  1. CLI参数 -运行时参数(--path, --config, --log-level, --force等等)
  2. 项目配置 - ./autodev-config.json (或通过自定义路径 --config)
  3. 全局配置 - ~/.autodev-cache/autodev-config.json
  4. 内置默认值 -回退值

注: CLI参数为路径、日志记录和操作行为提供运行时覆盖。对于持久配置(embedderProvider、API键、搜索参数),请使用 config --set 保存到配置文件。

常见配置示例

奥拉马:

{
  "embedderProvider": "ollama",
  "embedderModelId": "nomic-embed-text",
  "qdrantUrl": "http://localhost:6333"
}

OpenAI:

{
  "embedderProvider": "openai",
  "embedderModelId": "text-embedding-3-small",
  "embedderOpenAiApiKey": "sk-your-key",
  "qdrantUrl": "http://localhost:6333"
}

OpenAI兼容:

{
  "embedderProvider": "openai-compatible",
  "embedderModelId": "text-embedding-3-small",
  "embedderOpenAiCompatibleApiKey": "sk-your-key",
  "embedderOpenAiCompatibleBaseUrl": "https://api.openai.com/v1"
}

关键配置选项

类别 选项 描述
嵌入 embedderProvider, embedderModelId, embedderModelDimension 提供者和模型设置
API密钥 embedderOpenAiApiKey, embedderOpenAiCompatibleApiKey 身份验证
向量存储 qdrantUrl, qdrantApiKey Qdrant连接
搜索 vectorSearchMinScore, vectorSearchMaxResults 搜索行为
重排序器 rerankerEnabled, rerankerProvider 结果重新排名
总结者 summarizerProvider, summarizerLanguage, summarizerBatchSize AI摘要生成

关键CLI参数:

  • index -为代码库建立索引
  • search <query> -搜索代码库(必需的位置参数)
  • outline <pattern> -提取代码大纲(支持glob模式)
  • call -分析函数调用关系和依赖关系图
  • stdio -启动MCP的stdio适配器
  • config -管理配置(与--get或--set一起使用)
  • --serve -启动MCP HTTP服务器(与一起使用 index 命令)
  • --summarize -为代码大纲生成AI摘要
  • --dry-run -执行前预览操作
  • --title -仅显示文件级摘要
  • --clear-summarize-cache -清除所有摘要缓存
  • --path, --demo, --force -常见选项
  • --limit / -l <number> -最大搜索结果数(默认值:来自配置,最多50个)
  • --min-score / -S <number> -搜索结果的最小相似性得分(0-1,默认值:来自配置)
  • --query <patterns> -调用图分析的查询模式(逗号分隔)
  • --viz <file> -导出完整的依赖关系数据以进行可视化(不能与--query一起使用)
  • --open -打开交互式图形查看器
  • --depth <number> -设置调用图的分析深度
  • --help -显示所有可用选项

配置命令:

# View config
codebase config --get
codebase config --get --json

# Set config (saves to file)
codebase config --set embedderProvider=ollama,embedderModelId=nomic-embed-text
codebase config --set --global embedderProvider=openai,embedderOpenAiApiKey=sk-xxx

# Use custom config file
codebase --config=/path/to/config.json config --get
codebase --config=/path/to/config.json config --set embedderProvider=ollama

# Runtime override (paths, logging, etc.)
codebase index --path=/my/project --log-level=info --force

有关完整的配置参考,请参阅 CONFIG.md.

🔌 MCP集成

HTTP流模式(推荐)

codebase index --serve --port=3001

IDE配置:

{
  "mcpServers": {
    "codebase": {
      "url": "http://localhost:3001/mcp"
    }
  }
}

标准适配器

# First start the MCP server in one terminal
codebase index --serve --port=3001

# Then connect via stdio adapter in another terminal (for IDEs that require stdio)
codebase stdio --server-url=http://localhost:3001/mcp

IDE配置:

{
  "mcpServers": {
    "codebase": {
      "command": "codebase",
      "args": ["stdio", "--server-url=http://localhost:3001/mcp"]
    }
  }
}

🤝 贡献

欢迎投稿!请随时提交Pull Request或在 GitHub.

📄 许可证

该项目根据 MIT许可证.

🙏 致谢

这个项目是一个基于 鲁代码。我们在他们出色的基础上创建了这个具有增强功能和MCP服务器功能的专用代码库分析工具。


🌟 如果你觉得这个工具有用,请给我们一个 星在GitHub!

由...制作❤️ 面向开发者社区