内置工具
txcode-sdk 提供 7 个内置工具,覆盖文件操作、搜索和命令执行。
工具列表
| 工具 | 用途 | 分类 |
| read_file | 读取文件或目录内容 | 文件操作 |
| write_file | 写入新文件 | 文件操作 |
| edit_file | 精确字符串替换编辑 | 文件操作 |
| glob | 文件名模式匹配 | 搜索 |
| grep | 正则表达式内容搜索 | 搜索 |
| bash | 执行 shell 命令 | 命令执行 |
| memory_tool | 记忆管理(内部使用) | 内部 |
read_file
| 参数 | 类型 | 必填 | 说明 |
file_path | str | 是 | 文件或目录绝对路径 |
offset | int | 否 | 起始行号,默认 1 |
limit | int | 否 | 最大读取行数,默认 2000 |
返回内容每行带行号前缀,支持读取图片和 PDF 作为附件。
write_file
| 参数 | 类型 | 必填 | 说明 |
file_path | str | 是 | 文件绝对路径 |
content | str | 是 | 要写入的内容 |
已存在的文件必须用 edit_file 修改。
edit_file
| 参数 | 类型 | 必填 | 说明 |
file_path | str | 是 | 文件绝对路径 |
old_string | str | 是 | 要被替换的内容 |
new_string | str | 是 | 替换后的新内容 |
replace_all | bool | 否 | 替换所有匹配项,默认 false |
编辑前必须先 read_file。old_string 找不到或出现多次会失败。
glob
| 参数 | 类型 | 必填 | 说明 |
pattern | str | 是 | glob 模式,如 **/*.py |
directory | str | 否 | 搜索目录 |
grep
| 参数 | 类型 | 必填 | 说明 |
pattern | str | 是 | 正则表达式 |
directory | str | 否 | 搜索目录 |
include | str | 否 | 文件过滤 |
bash
| 参数 | 类型 | 必填 | 说明 |
command | str | 是 | 要执行的命令 |
workdir | str | 否 | 工作目录 |
timeout | int | 否 | 超时毫秒,默认 120000 |
仅用于终端操作,禁止用于文件操作。
创建自定义工具
from txcode_sdk import Tool
from txcode_sdk.types import ToolContext, ToolResult
def my_execute(params: dict, ctx: ToolContext) -> ToolResult:
name = params.get("name", "World")
return ToolResult(success=True, output=f"Hello, {name}!")
my_tool = Tool(
name="hello",
description="Print a greeting message",
parameters={
"type": "object",
"properties": {"name": {"type": "string", "description": "Name to greet"}},
"required": ["name"]
},
execute=my_execute,
)
client = TxCodeClient(api_key="sk-xxx", tools=[my_tool])