simonw/llm-coding-agent

GitHub: simonw/llm-coding-agent

基于 LLM 框架的交互式编程 Agent 插件,让大语言模型在受控沙盒内自主读取、编辑代码并执行 Shell 命令来完成开发任务。

Stars: 21 | Forks: 3

# llm-coding-agent [![PyPI](https://img.shields.io/pypi/v/llm-coding-agent.svg)](https://pypi.org/project/llm-coding-agent/) [![测试](https://static.pigsec.cn/wp-content/uploads/repos/cas/ce/ce733292a922c08274cf5a2096f8fa4cf01023bfa51a36ef6beecaaef371a9d9.svg)](https://github.com/simonw/llm-coding-agent/actions/workflows/test.yml) [![更新日志](https://img.shields.io/github/v/release/simonw/llm-coding-agent?include_prereleases&label=changelog)](https://github.com/simonw/llm-coding-agent/releases) [![许可证](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/simonw/llm-coding-agent/blob/main/LICENSE) 一个基于 LLM 构建的编程 agent ## 由 Fable 5 构建 第一个可用的 alpha 版本是[使用以下 prompt](https://claude.ai/code/session_01TEUBvBbMipbFSoqjMiJ7ha) 构建的: 然后: ## 安装说明 使用 `pip` 安装此库(由于它依赖于 LLM 的 alpha 发布版本,因此需要使用 `--pre` 标志): ``` pip install --pre llm-coding-agent ``` ## 用法 完整设计请参阅 [spec.md](spec.md)。 这个包是一个 [LLM 插件](https://llm.datasette.io/en/latest/plugins/index.html):安装后它会添加一个 `llm code` 命令,使用 LLM 已知的任何支持工具的模型,在当前目录下启动一个交互式编程 agent 会话: ``` llm code # interactive session, default model llm code "add type hints to utils.py" # start with an initial task llm code -m gpt-4.1 -d ~/dev/myproject # pick a model and directory llm code --yolo # auto-approve every tool call llm code --allow "pytest*" --allow "git diff*" # pre-approve some commands ``` 模型可以读取、搜索和编辑会话目录下的文件,并在此运行 shell 命令。只读工具可以自由运行;文件写入、编辑和 shell 命令会向你展示模型想要执行的操作并请求批准 - `y` 批准一次,`a` 批准本次会话中余下的类似操作,输入其他任何内容则表示拒绝(模型会收到通知,并可以尝试其他方法)。在会话中,`!quit` 退出,`!yolo` 切换自动批准,`!model MODEL` 在对话中途切换模型,`!tokens` 报告 token 使用情况。 与 `llm chat` 一样,会话会记录到 LLM 的 SQLite 数据库中,因此 `llm logs` 会显示包含每次工具调用的完整记录,并且可以恢复对话: ``` llm code -c # continue the most recent conversation llm code --cid 01ab... # continue a specific conversation ID llm logs --short # review what the agent did ``` 该插件还会向 LLM 本身注册工具箱,因此相同的工具也可以与 [llm chat 或 llm prompt](https://llm.datasette.io/en/latest/usage.html) 一起使用: ``` llm chat --tool CodingTools --chain-limit 20 ``` ### CodingAgent `CodingAgent` 可针对任何支持工具的 [LLM 模型](https://llm.datasette.io/en/latest/python-api.html) 运行完整的 agent 循环: ``` from llm_coding_agent import CodingAgent agent = CodingAgent( model="gpt-4.1-mini", # any llm model ID, or a model instance root="/path/to/project", approve=True, # approve every tool call ) result = agent.run("Fix the failing test in tests/test_parser.py") print(result.text) # the model's final answer for tool_call, tool_result in result.tool_calls: print(tool_call.name, tool_call.arguments) agent.run("Now add a changelog entry") # the conversation continues ``` `approve=` 参数用于控制当模型想要运行修改类工具(`write_file`、`edit_file`、`execute_command` - 只读工具从不询问)时的行为: - `approve=True` 批准所有操作 - `approve=callable` - 一个 `(tool, tool_call) -> bool` 函数;返回 `False` 会取消该调用,并告知模型其被拒绝 - `approve=None`(默认值)会暂停运行:`result.paused` 为 true,`result.pending_tool_calls` 列出了模型想要执行的操作,而调用 `agent.resume()` 会批准这些调用并继续循环 - 这在没有附加终端的应用程序审批流程中非常有用 `chain_limit=`(默认为 25)限制单次 `run()` 可以执行多少轮工具调用;如果达到限制,`result.hit_limit` 将为 true。 ### CodingTools 这些工具本身存在于 `CodingTools` 中,这是一个限制在根目录下的 [llm.Toolbox](https://llm.datasette.io/en/latest/python-api.html#python-api-toolbox),可直接与 `model.chain()` 一起使用: ``` from llm_coding_agent import CodingTools tools = CodingTools("/path/to/project") print(tools.read_file("README.md")) # numbered lines, like cat -n print(tools.read_file("big.log", offset=100, limit=50)) tools.write_file("notes/todo.md", "- ship it\n") # creates parent dirs print(tools.edit_file("app.py", "DEBUG = True", "DEBUG = False")) ``` `edit_file` 执行精确的字符串替换:`old_string` 必须在文件中仅出现一次(传入 `replace_all=True` 可替换所有出现的地方),并且该工具会返回更改的 unified diff,以便模型验证它所做的操作。 为了探索项目,提供了两个搜索工具: ``` print(tools.list_files("**/*.py")) # newest first, respects .gitignore print(tools.search_files("TODO", glob="*.py")) # path:line:content matches ``` 当安装了 [ripgrep](https://github.com/BurntSushi/ripgrep) 时,`search_files` 会使用它;如果未安装,则回退到纯 Python 扫描(输出格式相同)。 Shell 命令在会话根目录下运行,stdout 和 stderr 交错输出,并在末尾报告退出代码: ``` print(tools.execute_command("pytest -x", timeout=300)) ``` 超时(默认 120 秒,上限为 600 秒)时,整个进程树将被终止,并返回任何部分输出。 所有文件访问都限制在根目录下 - 相对路径会根据它进行解析,任何试图脱离该目录的路径(通过 `..`、绝对路径或符号链接)都会返回 `Error:` 字符串而不是文件内容,因此使用这些工具的模型可以看到并纠正它的错误。 ## 开发 要为这个库做贡献,首先检出代码。然后创建一个新的虚拟环境: ``` cd llm-coding-agent python -m venv venv source venv/bin/activate ``` 现在安装依赖项和测试依赖项: ``` python -m pip install -e '.[test]' ``` 要运行测试: ``` python -m pytest ```
标签:AI编程助手, C2, Python, SOC Prime, 开发工具, 无后门, 逆向工具