Skip to content

teff.tool.builtin.file

teff.tool.builtin.file

File tools — read, write, and edit files on disk.

Classes:

Name Description
EditFileTool

Edit a file by replacing the first occurrence of a string.

ReadFileTool

Read a file's contents.

WriteFileTool

Write text content to a file.

EditFileTool

Bases: Tool

Edit a file by replacing the first occurrence of a string.

Source code in teff/tool/builtin/file.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class EditFileTool(Tool):
    """Edit a file by replacing the first occurrence of a string."""

    name = "edit_file"
    description = "Edit a file by replacing text"

    def run(self, path: str = "", old: str = "", new: str = "") -> str:  # type: ignore[override]
        with open(path) as f:
            content = f.read()
        if old not in content:
            raise ValueError(f"text not found in {path}")
        content = content.replace(old, new, 1)
        with open(path, "w") as f:
            f.write(content)
        return f"replaced in {path}"

ReadFileTool

Bases: Tool

Read a file's contents.

Source code in teff/tool/builtin/file.py
 6
 7
 8
 9
10
11
12
13
14
class ReadFileTool(Tool):
    """Read a file's contents."""

    name = "read_file"
    description = "Read a file"

    def run(self, path: str = "") -> str:  # type: ignore[override]
        with open(path) as f:
            return f.read()

WriteFileTool

Bases: Tool

Write text content to a file.

Source code in teff/tool/builtin/file.py
17
18
19
20
21
22
23
24
25
26
class WriteFileTool(Tool):
    """Write text content to a file."""

    name = "write_file"
    description = "Write content to a file"

    def run(self, path: str = "", content: str = "") -> str:  # type: ignore[override]
        with open(path, "w") as f:
            f.write(content)
        return f"written {len(content)} bytes to {path}"