Claude 可以使用 Anthropic 定义的文本编辑器工具来查看和修改文本文件,帮助您调试、修复和改进您的代码或其他文本文档。这使 Claude 能够直接与您的文件交互,提供实际操作帮助,而不仅仅是建议更改。
| 模型 | 工具版本 |
|---|---|
| Claude 4.x 模型 | text_editor_20250728 |
| Claude Sonnet 3.7 (已弃用) | text_editor_20250124 |
Claude 4 模型的 text_editor_20250728 工具不包括 undo_edit 命令。如果您需要此功能,您需要使用 Claude Sonnet 3.7 (已弃用)。
较旧的工具版本不保证与较新的模型向后兼容。始终使用与您的模型版本相对应的工具版本。
以下是何时使用文本编辑器工具的一些示例:
文本编辑器工具可以按以下方式使用:
文本编辑器工具支持多个用于查看和修改文件的命令:
view 命令允许 Claude 检查文件的内容或列出目录的内容。它可以读取整个文件或特定行范围。
参数:
command:必须为 "view"path:要查看的文件或目录的路径view_range(可选):一个包含两个整数的数组,指定要查看的开始和结束行号。行号从 1 开始,结束行为 -1 表示读到文件末尾。此参数仅在查看文件时适用,不适用于目录。str_replace 命令允许 Claude 用新字符串替换文件中的特定字符串。这用于进行精确编辑。
参数:
command:必须为 "str_replace"path:要修改的文件的路径old_str:要替换的文本(必须完全匹配,包括空格和缩进)new_str:要插入以替换旧文本的新文本create 命令允许 Claude 创建具有指定内容的新文件。
参数:
command:必须为 "create"path:应创建新文件的路径file_text:要写入新文件的内容insert 命令允许 Claude 在文件中的特定位置插入文本。
参数:
command:必须为 "insert"path:要修改的文件的路径insert_line:要在其后插入文本的行号(0 表示文件开头)new_str:要插入的文本undo_edit 命令允许 Claude 撤销对文件所做的最后一次编辑。
此命令仅在 Claude Sonnet 3.7 (已弃用) 中可用。Claude 4 模型使用 text_editor_20250728 不支持此命令。
参数:
command:必须为 "undo_edit"path:应撤销其最后一次编辑的文件的路径文本编辑器工具实现为无模式工具。使用此工具时,您不需要像其他工具那样提供输入模式;该模式内置于 Claude 的模型中,无法修改。
工具类型取决于模型版本:
type: "text_editor_20250728"type: "text_editor_20250124"实现文本编辑器工具时,请记住:
使用文本编辑器工具时,可能会发生各种错误。以下是如何处理它们的指导:
The text editor tool uses the same pricing structure as other tools used with Claude. It follows the standard input and output token pricing based on the Claude model you're using.
In addition to the base tokens, the following additional input tokens are needed for the text editor tool:
| Tool | Additional input tokens |
|---|---|
text_editor_20250429 (Claude 4.x) | 700 tokens |
text_editor_20250124 (Claude Sonnet 3.7 (deprecated)) | 700 tokens |
有关工具定价的更多详细信息,请参阅工具使用定价。
文本编辑器工具可以与其他 Claude 工具一起使用。组合工具时,请确保:
| 日期 | 版本 | 更改 |
|---|---|---|
| 2025 年 7 月 28 日 | text_editor_20250728 | 发布更新的文本编辑器工具,修复了一些问题并添加了可选的 max_characters 参数。除此之外,它与 text_editor_20250429 相同。 |
| 2025 年 4 月 29 日 | text_editor_20250429 | 发布 Claude 4 的文本编辑器工具。此版本删除了 undo_edit 命令,但保持了所有其他功能。工具名称已更新以反映其基于 str_replace 的架构。 |
| 2025 年 3 月 13 日 | text_editor_20250124 | 独立文本编辑器工具文档的介绍。此版本针对 Claude Sonnet 3.7 进行了优化,但具有与以前版本相同的功能。 |
| 2024 年 10 月 22 日 | text_editor_20241022 | 文本编辑器工具与 Claude Sonnet 3.5 (已停用) 的初始发布。通过 view、create、、 和 命令提供查看、创建和编辑文件的功能。 |
以下是如何以更便利和强大的方式使用文本编辑器工具的一些想法:
当您使用文本编辑器工具构建应用程序时,我们很高兴看到您如何利用 Claude 的功能来增强您的开发工作流和生产力。
向 Claude 提供文本编辑器工具和用户提示
Claude 使用工具检查文件或目录
view 命令检查文件内容或列出目录内容view 命令的 tool_use 内容块执行 view 命令并返回结果
max_characters 参数,请将文件内容截断到该长度tool_result 内容块的新 user 消息将结果返回给 ClaudeClaude 使用工具修改文件
str_replace 等命令进行更改或使用 insert 在特定行号处添加文本。str_replace 命令,Claude 会构造一个格式正确的工具使用请求,其中包含旧文本和新文本以替换它执行编辑并返回结果
Claude 提供其分析和解释
初始化您的编辑器实现
创建辅助函数来处理文件操作,如读取、写入和修改文件。考虑实现备份功能以从错误中恢复。
处理编辑器工具调用
创建一个函数来处理来自 Claude 的工具调用,基于命令类型:
def handle_editor_tool(tool_call, model_version):
input_params = tool_call.input
command = input_params.get('command', '')
file_path = input_params.get('path', '')
if command == 'view':
# 读取并返回文件内容
pass
elif command == 'str_replace':
# 替换文件中的文本
pass
elif command == 'create':
# 创建新文件
pass
elif command == 'insert':
# 在位置处插入文本
pass
elif command == 'undo_edit':
# 检查是否为 Claude 4 模型
if 'str_replace_based_edit_tool' in model_version:
return {"error": "undo_edit command is not supported in Claude 4"}
# 为 Claude 3.7 从备份恢复
pass实现安全措施
添加验证和安全检查:
处理 Claude 的响应
从 Claude 的响应中提取和处理工具调用:
# 处理 Claude 响应中的工具使用
for content in response.content:
if content.type == "tool_use":
# 根据命令执行工具
result = handle_editor_tool(content)
# 将结果返回给 Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result
}str_replaceinsertundo_edit在使用 Claude Sonnet 3.7 的工具时降低延迟和成本。