展示HN:一个更简单的编码代理工具

1作者: pyrolistical2 天前原帖
系统提示,编码代理所利用的语言模型的能力是巨大的。它们详细描述了每一个可用的工具——甚至是那些你从未使用过的工具。 所以我想,如果我构建一个更简约的工具会怎样呢? 我通常在小项目上工作,所有代码都可以放在上下文窗口中。因此,我构建了一个只包含一个工具的系统:让大型语言模型(LLM)编辑我的文件的能力。 核心算法如下: 1. 读取项目目录中的每个文件 2. 将它们与用户的提示合并成一条消息 3. 发送给任何语言模型,指示其以消息和编辑列表的形式回应——每个编辑都是对特定文件的搜索和替换 4. 应用这些编辑 我尝试让LLM生成统一的差异(unified diffs),但它总是产生格式错误的结果。搜索和替换显得更为可靠——模型会逐字复制上下文中已有文件的文本。 这可以通过几个额外的功能进行扩展: 1. 一个命令行界面(CLI),让你可以继续对话或重置对话。较弱的LLM需要这个功能,以便能看到它们犯的错误。 2. 自动重试——如果编辑失败,重新发送当前文件,让LLM修复它自己的搜索块。 3. 文件删除。我设置了一个功能,使得在空文件上进行空的搜索和替换会删除该文件。 我测试的LLM是通过LM Studio API运行的本地模型。Gemma 4表现相当糟糕,而Qwen 3.5则好得多。 源代码在哪里?你应该能够通过这篇文章让任何编码代理为你生成一个。
查看原文
The system prompts that coding agent harnesses pass to language models are massive. They describe every available tool in detail — even the ones you never use.<p>So I wondered, what if I built something more minimal?<p>I tend to work on small projects where all the code fits in the context window. So I built a harness with a single tool: the ability for an LLM to edit my files.<p>The core algorithm:<p>1. Read every file in the project directory<p>2. Concatenate them with the user&#x27;s prompt into a single message<p>3. Send it to any language model, instructed to respond with a message and a list of edits — each edit being a search and replace on a specific file<p>4. Apply the edits<p>I tried getting the LLM to generate unified diffs, but it kept producing malformed ones. Search and replace turned out to be far more reliable — the model copies text verbatim from the files already in its context.<p>This can be extended with a few more features:<p>1. A CLI that lets you continue the conversation or reset it. Dumber LLMs need this so it can see the mistake they made.<p>2. Automatic retries — if an edit fails, re-send the current files and have the LLM fix its own search blocks.<p>3. File deletion. I made it so that an empty search-and-replace on an empty file deletes it.<p>The LLMs I tested were local ones running through the LM Studio API. Gemma 4 was pretty bad. Qwen 3.5 was significantly better.<p>Where is the source code? You should be able to get any coding agent to generate you one using this post.