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(已弃用)。
旧版工具不保证与新模型向后兼容。请始终使用与您的模型版本对应的工具版本。
以下是一些使用文本编辑器工具的场景示例:
文本编辑器工具可以按以下方式使用:
向 Claude 提供文本编辑器工具和用户提示
Claude 使用工具检查文件或目录
view 命令检查文件内容或列出目录内容view 命令的 tool_use 内容块执行查看命令并返回结果
max_characters 参数,则将文件内容截断到该长度tool_result 内容块的新 user 消息,将结果返回给 ClaudeClaude 使用工具修改文件
str_replace 等命令进行更改,或使用 insert 在特定行号添加文本。str_replace 命令,Claude 会构造一个格式正确的工具使用请求,包含要替换的旧文本和新文本执行编辑并返回结果
Claude 提供分析和解释
文本编辑器工具支持多个用于查看和修改文件的命令:
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 表示文件开头)insert_text:要插入的文本undo_edit 命令允许 Claude 撤销对文件所做的最后一次编辑。
此命令仅在 Claude Sonnet 3.7(已弃用)中可用。使用 text_editor_20250728 的 Claude 4 模型不支持此命令。
参数:
command:必须为 "undo_edit"path:要撤销最后一次编辑的文件的路径文本编辑器工具作为无模式工具实现。使用此工具时,您无需像其他工具那样提供输入模式;该模式内置于 Claude 的模型中,无法修改。
工具类型取决于模型版本:
type: "text_editor_20250728"type: "text_editor_20250124"初始化编辑器实现
创建辅助函数来处理文件操作,如读取、写入和修改文件。考虑实现备份功能以从错误中恢复。
处理编辑器工具调用
创建一个函数,根据命令类型处理来自 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':
# Read and return file contents
pass
elif command == 'str_replace':
# Replace text in file
pass
elif command == 'create':
# Create new file
pass
elif command == 'insert':
# Insert text at location
pass
elif command == 'undo_edit':
# Check if it's a Claude 4 model
if 'str_replace_based_edit_tool' in model_version:
return {"error": "undo_edit command is not supported in Claude 4"}
# Restore from backup for Claude 3.7
pass实施安全措施
添加验证和安全检查:
处理 Claude 的响应
从 Claude 的响应中提取和处理工具调用:
# Process tool use in Claude's response
for content in response.content:
if content.type == "tool_use":
# Execute the tool based on command
result = handle_editor_tool(content)
# Return result to Claude
tool_result = {
"type": "tool_result",
"tool_use_id": content.id,
"content": result
}实现文本编辑器工具时,请注意以下几点:
使用文本编辑器工具时,可能会出现各种错误。以下是处理这些错误的指南:
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、str_replace、insert 和 undo_edit 命令提供查看、创建和编辑文件的功能。 |
以下是一些关于如何以更便捷和强大的方式使用文本编辑器工具的想法:
在您使用文本编辑器工具构建应用程序时,我们期待看到您如何利用 Claude 的能力来增强您的开发工作流程和生产力。
Was this page helpful?