🧠 代码库RLM导航器
RooCode的递归语言模型智能
一个使用模型上下文协议(MCP)的服务器 递归语言模型(RLM) 将您的代码库转换为智能的分层知识图。专为RooCode集成而构建,它消除了上下文窗口的限制,并通过分析从单个文件到模块再到系统架构的层中的代码来防止上下文腐烂,从而创建了一个可以立即查询的持久智能图。
现在使用独立模式:直接从文件系统分析代码库,无需Repomix!
🔄 什么是RLM(递归语言模型)?
德意志帝国航空部 是一种架构模式,它将大型代码库分解为层次化的理解层:
- 级别1(文件层):每个文件都会单独分析函数、类和依赖关系
- 第二级(模块层):对相关文件进行分组和分析,以确定协作模式
- 3级(系统层):整个架构由模块摘要综合而成
这 递归方法 通过以下方式解决上下文窗口问题:
- ✅ 防止上下文腐烂:分析被缓存和重用,而不是每次都重新生成
- ✅ 打破上下文限制:仅按需加载相关层
- ✅ 保持一致性:每一层都建立在前一层的基础上,保持关系
- ✅ 启用即时查询:预先计算的摘要意味着零重新分析时间
   
______________________________________________________________________
🎯 这解决了什么问题?
在RooCode(或任何AI编码助手)中处理大型代码库时,您将面临严峻的挑战:
- 上下文窗口限制:AI助手不能一次加载整个代码库
- 语境腐化:AI在对话之间忘记架构决策
- 手动文件导航:你浪费时间反复解释项目结构
- 低效的重新分析:多次分析同一代码,浪费令牌和时间
- 失去的关系:AI不理解模块如何交互
Codebase RLM Navigator解决了这个问题 使用 递归语言模型 创建一个RooCode可以立即查询的持久的、分层的“智能图”——没有上下文腐烂、没有重新分析、没有令牌浪费。
______________________________________________________________________
✨ 主要特点
🚀 智能高速缓存
- 分析文件一次,永久缓存结果(直到代码更改)
- 基于MD5哈希的变化检测
- 使用ThreadPoolExecutor进行并行处理(5个并发工作线程)
🏗️ 三级RLM架构
- 文件级别:函数签名、依赖关系、核心职责
- 模块级别:文件如何在文件夹中协作
- 系统级:总体架构模式和数据流
⚡ 即时检索
- 后续查询无需重新解析
- 直接文件内容访问
- 模糊文件路径匹配
🔒 100%本地和私人
- 使用Ollama进行LLM分析(无云API)
- 所有数据都保留在您的机器上
- 初始设置后脱机工作
______________________________________________________________________
🎓 用例
1️⃣ 加入新的代码库 (适合初级开发人员)
场景:你刚刚克隆了一个50个文件的Python项目,需要快速理解它。
# In RooCode, simply ask:
"Use the codebase-rlm MCP tool to analyze this codebase and explain the architecture"重要:始终指定 “使用代码库rlm MCP工具” 以确保RooCode使用正确的MCP服务器。
发生了什么:
- RooCode呼叫
mcp--codebase-rlm--analyze_codebase()→ 直接扫描文件系统,并使用RLM并行分析所有文件。 - 呼叫
mcp--codebase-rlm--get_architecture()→ 获取高级系统概述。 - 呼叫
mcp--codebase-rlm--list_modules()→ 显示所有带有摘要的模块。
结果:在30秒内,您无需读取任何文件即可了解整个项目结构。RLM层次结构可防止上下文腐烂——此分析将永远缓存。
______________________________________________________________________
2️⃣ 查找添加功能的位置
场景:您需要添加用户身份验证,但不知道身份验证逻辑在哪里。
# In RooCode:
"Use the codebase-rlm MCP tool to show me all modules in this project"
# Then:
"Use the codebase-rlm tool to read the details of the 'auth' module"
# Then:
"Use the codebase-rlm tool to get the full content of auth/login.py"重要:指定MCP工具名称,以避免与其他工具混淆。
发生了什么:
mcp--codebase-rlm--list_modules()→ 显示所有带有简要说明的文件夹mcp--codebase-rlm--read_module_details("auth")→ 显示auth文件夹中的所有文件及其摘要mcp--codebase-rlm--get_file_content("auth/login.py")→ 检索确切的文件内容
结果:您可以直接导航到正确的文件,而无需猜测。RLM结构跨查询维护上下文。
______________________________________________________________________
3️⃣ 代码审查准备
场景:您正在审阅一份涉及3个模块中10个文件的PR。
# In RooCode:
"Use the codebase-rlm MCP tool to analyze the 'api', 'database', and 'utils' modules and explain how they interact"发生了什么:
- RooCode从RLM层次结构查询模块摘要
- 了解模块之间的数据流
- 为您的审查提供架构背景
结果:您全面回顾了变化对系统的影响。审查会议之间没有上下文腐烂。
______________________________________________________________________
4️⃣ 重构大型项目
场景:您需要将单体应用程序重构为微服务。
# In RooCode:
"Use the codebase-rlm MCP tool to show me the system architecture and identify tightly coupled modules"发生了什么:
mcp--codebase-rlm--get_architecture()→ 从RLM分析中揭示架构模式- 模块摘要显示依赖关系
- 确定重构边界
结果:数据驱动的重构决策,而不是猜测。递归分析揭示了隐藏的耦合。
______________________________________________________________________
🛠️ 安装和设置
先决条件
- Python 3.12+
python --version # Must be 3.12 or higher- 配备6GB+VRAM的NVIDIA GPU (ministral-3型号需要)
- 最低配置:RTX 3060、RTX 4060、GTX 1660 Ti或任何具有6GB+VRAM的GPU - 推荐:RTX 4070(12GB+)可实现更快的分析速度 - 检查你的GPU: nvidia-smi
- 奥拉玛 (本地LLM运行时-在您的计算机上运行AI模型)
# Install from https://ollama.ai/
# After installation, verify it's working:
ollama list
# Download the ministral-3 model (requires 6GB VRAM minimum):
ollama pull ministral-3:latest- Repomix (可选-用于手动快照)
npm install -g repomix备注:如果没有兼容的NVIDIA GPU,Ollama将在CPU上运行(速度慢10-50倍)。Ollama目前不支持AMD/Intel GPU。
步骤1:安装依赖项
pip install fastmcp requestsGit忽略安装程序
该项目包括 .gitignore 不包括以下内容的文件:
.repomix_rlm_cache/-RLM分析缓存目录rlm-main/-RLM主目录(外部/子模块)RLM-Research.pdf-研究文件guidelines.md-项目指南
这些文件会自动从git提交中排除。
步骤2:配置MCP服务器
将此添加到您的RooCode MCP设置文件中:
视窗: %APPDATA%\Code\User\globalStorage\rooveterinaryinc.roo-cline\settings\cline_mcp_settings.json
macOS/Linux: ~/.config/Code/User/globalStorage/rooveterinaryinc.roo-cline/settings/cline_mcp_settings.json
{
"mcpServers": {
"codebase-rlm": {
"command": "python",
"args": ["C:/Users/YourUsername/path/to/smart_context.py"],
"disabled": false,
"description": "Recursive Language Model codebase analyzer"
}
}
}重要说明:
- 用实际位置替换路径
smart_context.py - 服务器名称为
codebase-rlm(这是您在提示中引用的内容) - 在提示RooCode时,始终指定此MCP工具名称,以避免混淆
步骤3:重新启动RooCode
关闭并重新打开VS Code以加载MCP服务器。
______________________________________________________________________
📖 如何使用
基本工作流程(独立-推荐)
# 1. Navigate to your project directory in VS Code
# 2. In RooCode, ALWAYS specify the MCP tool:
"Use the codebase-rlm MCP tool to analyze this codebase and show me the architecture"高级工作流(使用Repomix)
如果您更喜欢使用预先生成的Repomix XML文件:
# 1. Generate Repomix snapshot manually
repomix .
# 2. In RooCode:
"Use the codebase-rlm MCP tool to analyze this codebase using the repomix XML"🚨 重要提示:如何提示RooCode
为了避免与其他MCP工具混淆, 始终指定工具名称:
✅ 正确提示:
"Use the codebase-rlm MCP tool to analyze this codebase"
"Call the analyze_codebase tool from codebase-rlm"
"Use codebase-rlm to show me all modules"❌ 提示不正确(会混淆RooCode):
"Analyze this codebase" # Too vague, might use wrong tool
"Show me the architecture" # Doesn't specify which MCP server可用的MCP工具
当您指定 codebase-rlm MCP服务器:
| 工具 | 目的 | 示例查询 |
|---|---|---|
mcp--codebase-rlm--analyze_codebase(path, use_repomix_xml, repomix_xml_path) | 扫描文件并构建RLM情报图 | “使用代码库RLM分析此代码库” |
mcp--codebase-rlm--get_architecture() | 获取系统级概述 | “使用代码库rlm显示架构模式” |
mcp--codebase-rlm--list_modules() | 列出所有带有摘要的模块 | “使用代码库rlm向我显示所有模块” |
mcp--codebase-rlm--read_module_details(module_path) | 获取详细的模块信息 | “使用代码库rlm解释‘api’模块” |
mcp--codebase-rlm--get_file_content(file_path) | 检索文件内容 | “使用代码库rlm向我显示api/routes.py” |
______________________________________________________________________
⚙️ 配置
编辑 Config 类在 smart_context.py:
class Config:
OLLAMA_BASE_URL = "http://localhost:11434" # Ollama API endpoint
LLM_MODEL = "ministral-3:latest" # Model for analysis
CACHE_DIR = "./.repomix_rlm_cache" # Cache location推荐型号
| 型号 | 速度 | 质量 | 所需VRAM | 用例 |
|---|---|---|---|---|
ministral-3:latest | ⚡⚡⚡ | ⭐⭐⭐ | 6GB | 默认 -速度快,质量好 |
llama3.2:latest | ⚡⚡ | ⭐⭐⭐⭐ | 8GB | 更大的项目,更好的总结 |
qwen2.5-coder:latest | ⚡⚡ | ⭐⭐⭐⭐⭐ | 8GB | 代码专用,最高质量 |
备注:VRAM=图形卡上的视频RAM。检查你的 nvidia-smi.
______________________________________________________________________
🚀 相对于传统方法的优势
与手动代码读取
| 传统 | Repomix RLM |
|---|---|
| ❌ 逐一读取文件 | ✅ 即时分层概述 |
| ❌ 忘记会话之间的上下文 | ✅ 持久缓存分析 |
| ❌ 怀念建筑模式 | ✅ 人工智能生成的架构见解 |
对比Grep/搜索工具
| Grep/Ripgrep | Repomix RLM |
|---|---|
| ❌ 仅关键字匹配 | ✅ 语义理解 |
| ❌ 没有关于文件用途的上下文 | ✅ 功能签名+职责 |
| ❌ 无法解释关系 | ✅ 模块协作分析 |
vs.将整个代码库加载到AI中
| 完整上下文转储 | 代码库RLM |
|---|---|
| ❌ 超出上下文窗口 | ✅ 递归层次结构打破了上下文限制 |
| ❌ 昂贵的代币使用 | ✅ 一次性分析,无限查询 |
| ❌ 处理速度慢 | ✅ 缓存,即时检索 |
| ❌ 会话之间的上下文腐烂 | ✅ 持久RLM情报图 |
______________________________________________________________________
🐛 故障排除
问题:“Ollama连接被拒绝”
解决方案:
# Check if Ollama is running
ollama list
# Start Ollama service
ollama serve问题:“未找到repomix-output.xml”(使用xml模式时)
解决方案:
# Either run without XML mode (recommended):
"Use codebase-rlm to analyze this codebase"
# Or generate the XML file manually:
repomix .问题:“分析速度慢”
原因和修复:
- 无GPU:ministral-3需要配备6GB+VRAM的NVIDIA GPU。与核对
nvidia-smi - 大文件:RLM只分析每个文件的前500000个字符(可配置)
- 慢速模型:默认值
ministral-3:latest是最快的选择 - CPU回退:CPU上的Ollama比GPU慢10-50x
问题:“代码更改后缓存不更新”
解决方案:
# Delete cache and re-analyze
rm -rf .repomix_rlm_cache
# Then in RooCode:
"Use codebase-rlm to analyze this codebase"问题:“RooCode使用了错误的MCP工具”
解决方案:始终在提示中指定工具名称:
"Use the codebase-rlm MCP tool to [your request]"______________________________________________________________________
🔧 高级用法
自定义提示
在中编辑提示 RLMIndexer 要自定义分析样式,请执行以下操作:
# File-level analysis (line 139)
prompt = f"""You are a Senior Code Reviewer. Summarize this code file.
...
"""
# Module-level analysis (line 182)
prompt = f"""You are a Software Architect. Summarize this MODULE.
...
"""
# System-level analysis (line 256)
prompt = f"""You are the CTO. Provide a System Architecture Overview...
...
"""多项目支持
缓存是特定于项目的(基于XML路径哈希):
# Project A
cd /project-a
repomix .
# RooCode analyzes → cached in .repomix_rlm_cache/abc12345/
# Project B
cd /project-b
repomix .
# RooCode analyzes → cached in .repomix_rlm_cache/def67890/______________________________________________________________________
🤝 与RooCode工作流集成
工作流程1:功能开发
1. Ask RooCode: "Use the codebase-rlm MCP tool to analyze this codebase"
→ RooCode calls mcp--codebase-rlm--analyze_codebase()
→ Builds RLM hierarchy (file → module → system)
2. Ask RooCode: "Use codebase-rlm to find where I should add user profile editing"
→ RooCode calls mcp--codebase-rlm--list_modules() + read_module_details()
→ Navigates the RLM structure
3. Ask RooCode: "Use codebase-rlm to show me the user model file"
→ RooCode calls mcp--codebase-rlm--get_file_content()
→ Retrieves from cached RLM map
4. Ask RooCode: "Add a profile_picture field to the User model"
→ RooCode edits the file with full architectural context
→ No context rot—RLM maintains relationships工作流程2:Bug调查
1. Ask RooCode: "Use codebase-rlm to explain the authentication flow"
→ RooCode queries auth module from RLM hierarchy
2. Ask RooCode: "Use codebase-rlm to show me where JWT tokens are validated"
→ RooCode retrieves specific file content
3. Ask RooCode: "Fix the token expiration bug"
→ RooCode makes targeted changes with full context______________________________________________________________________
📊 性能指标
测试项目:50个Python文件,约10000行代码
| 操作 | 时间 | 备注 |
|---|---|---|
| 初步分析(GPU) | ~30秒 | RTX 4070的一次性成本 |
| 初始分析(CPU) | ~15min | 不带GPU(不推荐) |
| 后续查询 | \<1s | 缓存检索 |
| 架构概述 | \<1s | 预先计算 |
| 文件内容检索 | \<0.1s | 直接XML访问 |
缓存大小:50个文件约500KB(JSON摘要)
______________________________________________________________________
🛡️ 安全与隐私
- ✅ 没有云API:一切都通过Ollama在当地运行
- ✅ 无遥测:零数据收集
- ✅ Gitignore受到尊重:Repomix排除敏感文件
- ✅ 缓存是本地的:存储在
.repomix_rlm_cache/(添加到.gitignore)
______________________________________________________________________
🗺️ 路线图
- \[\]支持非XML Repomix格式(Markdown、纯文本)
- \[\]独立使用的交互式CLI
- \[\]用于直接集成的VS代码扩展
- \[\]支持增量更新(仅重新分析更改的文件)
- \[\]摘要中突出显示多语言语法
- \[\]导出架构图(Mermaid/PlantUML)
- \[\]AMD/英特尔GPU支持(Ollama添加支持时)
______________________________________________________________________
🤔 常见问题解答
Q: 这适用于非Python项目吗?\ A: 是的!Repomix支持所有语言。LLM分析任何代码。
Q: 我可以使用其他LLM提供商(OpenAI、Anthropic)吗?\ A: 目前只有Ollama,但您可以修改 OllamaClient 以支持其他API。
Q: 我真的需要NVIDIA GPU吗?\ A: 从技术上讲没有,但CPU分析速度慢10-50倍。对于50个文件的项目:GPU=30秒,CPU=15分钟。
Q: 缓存使用了多少磁盘空间?\ A: 每个文件约10KB(JSON摘要)。100个文件项目=~1MB缓存。
Q: 这会取代阅读代码吗?\ A: 不,这是一个导航工具。使用它来查找要阅读的内容,然后阅读实际的代码。
Q: 我可以与我的团队共享缓存吗?\ A: 不推荐。缓存路径是特定于机器的。每个开发人员都应该生成自己的。
Q: RLM和RAG有什么区别?\ A: RAG按需检索块。RLM预分析并缓存分层摘要,防止上下文腐烂。
______________________________________________________________________
📄 许可证
MIT许可证-您可以自由使用、修改和分发。
______________________________________________________________________
🙏 致谢
______________________________________________________________________
📞 支持
问题? 在GitHub上打开一个问题或询问RooCode:
"Use the codebase-rlm MCP tool to help me debug the Repomix RLM server"______________________________________________________________________
内置于❤️ 对于希望更快地理解代码的开发人员
_停止逐一读取文件。开始使用递归语言模型智能进行导航。_
