内置工具

txcode-sdk 提供 7 个内置工具,覆盖文件操作、搜索和命令执行。

工具列表

工具用途分类
read_file读取文件或目录内容文件操作
write_file写入新文件文件操作
edit_file精确字符串替换编辑文件操作
glob文件名模式匹配搜索
grep正则表达式内容搜索搜索
bash执行 shell 命令命令执行
memory_tool记忆管理(内部使用)内部

read_file

参数类型必填说明
file_pathstr文件或目录绝对路径
offsetint起始行号,默认 1
limitint最大读取行数,默认 2000

返回内容每行带行号前缀,支持读取图片和 PDF 作为附件。


write_file

参数类型必填说明
file_pathstr文件绝对路径
contentstr要写入的内容

已存在的文件必须用 edit_file 修改。


edit_file

参数类型必填说明
file_pathstr文件绝对路径
old_stringstr要被替换的内容
new_stringstr替换后的新内容
replace_allbool替换所有匹配项,默认 false

编辑前必须先 read_file。old_string 找不到或出现多次会失败。


glob

参数类型必填说明
patternstrglob 模式,如 **/*.py
directorystr搜索目录

grep

参数类型必填说明
patternstr正则表达式
directorystr搜索目录
includestr文件过滤

bash

参数类型必填说明
commandstr要执行的命令
workdirstr工作目录
timeoutint超时毫秒,默认 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])