在使用 Aider 时,Repo Map 是动态生成的代码库概览,默认情况下,它会被包含在发送给 LLM 的系统消息中,而不是直接保存到文件中。不过,通过一些自定义操作或利用 Aider 的现有功能,你可以让 Aider 将 Repo Map 输出并保存到一个文件中。以下是实现这一目标的详细步骤和方法。
前提
- Aider 的 Repo Map 由
RepoMap
类(位于aider/repomap.py
)生成,依赖 Tree-sitter 解析代码。 - 默认行为是将其作为上下文传递给 LLM,而不是持久化存储。
- 我们需要利用 Aider 的命令行选项、调试输出或代码修改来实现保存。
方法 1:利用 --verbose
输出并手动保存
Aider 的 --verbose
模式会打印详细日志,包括 Repo Map 的内容。你可以捕获这些输出并保存到文件。
步骤
-
运行 Aider 并启用 verbose 模式:
aider main.py utils.py --verbose > repo_map_output.txt
main.py utils.py
: 指定项目中的文件。--verbose
: 开启详细日志。> repo_map_output.txt
: 将输出重定向到文件。
-
触发 Repo Map 生成:
- 输入一个简单的修改请求(比如
/add 添加注释
),让 Aider 生成并显示 Repo Map。 - 在
repo_map_output.txt
中,搜索类似RepoMap:
的部分,那里会包含生成的 Repo Map。
- 输入一个简单的修改请求(比如
-
提取 Repo Map:
- 打开
repo_map_output.txt
,手动复制 Repo Map 内容到一个新文件(如repo_map.txt
)。 - 示例内容可能如下:
main.py: main() - def main(): ... utils.py: calculate_sum(a, b) - def calculate_sum(a, b): return a + b
- 打开
优点
- 无需修改代码,简单直接。
缺点
- 需要手动筛选日志,Repo Map 混在其他输出中。
方法 2:使用 /map
命令并重定向
Aider 提供了一个交互命令 /map
,可以手动触发 Repo Map 的生成并显示。我们可以利用它。
步骤
-
启动 Aider:
aider main.py utils.py
-
在交互模式中运行 /map:
- 输入:
/map
- Aider 会打印当前的 Repo Map 到终端。
- 输入:
-
重定向到文件:
- 因为
/map
是交互命令,直接重定向启动命令不行。你需要在终端手动复制输出,或者使用脚本:aider main.py utils.py | tee repo_map.txt
- 输入
/map
,然后 Ctrl+D 或/exit
退出,repo_map.txt
会包含输出。
- 因为
优点
- 直接获取 Repo Map,无需过多杂信息。
缺点
- 需要手动操作,不适合自动化。
方法 3:修改 Aider 源码保存 Repo Map
为了自动化生成并保存 Repo Map 到文件,最可靠的方法是修改 Aider 的源码,在生成 Repo Map 时写入文件。
步骤
-
克隆 Aider 仓库:
git clone https://github.com/Aider-AI/aider.git cd aider pip install -e .
-
修改
base_coder.py
:- 打开
aider/coders/base_coder.py
。 - 找到
update_repo_map(self)
方法,添加保存逻辑:def update_repo_map(self): if not self.repo_map or self.map_tokens <= 0: self.last_repo_map_content = None return None chat_files = self.get_inchat_relative_files() if len(chat_files) <= 1 and not self.git_repo: return None if self.refresh == "auto" and not self.needs_refresh(chat_files): return self.last_repo_map_content repo_map_content = self.repo_map.get_repo_map(chat_files, self.all_fnames) self.last_repo_map_content = repo_map_content # 新增:将 Repo Map 保存到文件 if repo_map_content: with open("repo_map.txt", "w", encoding="utf-8") as f: f.write(repo_map_content) return repo_map_content
- 改动说明: 在生成
repo_map_content
后,将其写入repo_map.txt
。
- 打开
-
运行修改后的 Aider:
aider main.py utils.py --model gpt-4o
- 每次 Repo Map 更新时,它会自动保存到项目目录下的
repo_map.txt
。
- 每次 Repo Map 更新时,它会自动保存到项目目录下的
优点
- 全自动化,Repo Map 直接保存为文件。
- 可自定义保存路径或格式(修改代码)。
缺点
- 需要修改源码,维护成本稍高。
方法 4:添加自定义命令
Aider 支持通过 Commands
类添加自定义命令。你可以扩展 Aider,增加一个 /save-map
命令来保存 Repo Map。
步骤
-
修改
commands.py
:- 打开
aider/commands.py
。 - 在
Commands
类中添加新命令:def cmd_save_map(self, args): """Save the current Repo Map to a file.""" if not self.coder.repo_map: self.io.tool_error("Repo Map is not enabled or not available.") return chat_files = self.coder.get_inchat_relative_files() repo_map_content = self.coder.repo_map.get_repo_map(chat_files, self.coder.all_fnames) if repo_map_content: with open("repo_map.txt", "w", encoding="utf-8") as f: f.write(repo_map_content) self.io.tool_output(f"Repo Map saved to repo_map.txt") else: self.io.tool_error("No Repo Map content to save.")
- 在
commands
字典中注册:self.commands["save-map"] = self.cmd_save_map
- 打开
-
运行并使用:
aider main.py utils.py
- 在交互模式输入:
/save-map
- Repo Map 将保存到
repo_map.txt
。
- 在交互模式输入:
优点
- 集成到 Aider 的命令系统中,使用灵活。
- 不影响核心逻辑,易于维护。
缺点
- 需要修改源码并重新安装。
推荐方案
- 短期使用: 用 方法 1 或 方法 2,简单快捷。
- 长期需求: 用 方法 3 或 方法 4,实现自动化保存,适合持续开发。
示例运行
假设项目有 main.py
和 utils.py
:
aider main.py utils.py --map-tokens 2048 --map-refresh always
/save-map # 如果使用方法 4
生成的文件 repo_map.txt
可能如下:
main.py:
main() - def main(): ...
utils.py:
calculate_sum(a, b) - def calculate_sum(a, b): return a + b
注意事项
- 文件路径: 默认保存到当前目录,可修改代码指定路径(如
./output/repo_map.txt
)。 - Repo Map 触发: 确保
--map-tokens > 0
且多于一个文件,否则不会生成。 - 编码: 使用 UTF-8 编码保存,避免乱码。
如果你需要更具体的实现代码或测试某个方法,可以告诉我,我帮你进一步完善!