Skip to content

trading_agent.tools.edit_file_tool

The edit_file tool: replaces exact text in a file in the desk's own folders.

Typical usage example:

tool = EditFileTool(policy, checkpoints)

EditFileArguments

Bases: BaseModel

The arguments of edit_file.

Attributes:

Name Type Description
path str

The file; a relative path starts at your desk folder.

old_text str

The exact text to replace, copied from the file.

new_text str

The text to put in its place.

replace_all bool

Whether to replace every occurrence instead of exactly one.

EditFileTool

EditFileTool(policy, checkpoints=None)

Bases: Tool

Replaces exact text in a file.

Creates the tool.

Parameters:

Name Type Description Default
policy PathPolicy

The desk's read and write rules.

required
checkpoints Checkpoints | None

Snapshots the desk's files before each change, or None to take none.

None
Source code in src/trading_agent/tools/edit_file_tool.py
def __init__(
    self,
    policy: path_policy_module.PathPolicy,
    checkpoints: checkpoints_module.Checkpoints | None = None,
):
    """Creates the tool.

    Args:
        policy (path_policy_module.PathPolicy): The desk's read and write rules.
        checkpoints (checkpoints_module.Checkpoints | None): Snapshots the desk's files before each change, or None to take none.
    """
    self._policy = policy
    self._checkpoints = checkpoints

run async

run(arguments)

Makes the replacement.

Parameters:

Name Type Description Default
arguments EditFileArguments

The file, the text to find and its replacement.

required

Returns:

Type Description
ToolResult

How many places changed, or an error saying why nothing did.

Source code in src/trading_agent/tools/edit_file_tool.py
async def run(self, arguments: EditFileArguments) -> tool.ToolResult:
    """Makes the replacement.

    Args:
        arguments (EditFileArguments): The file, the text to find and its replacement.

    Returns:
        tool.ToolResult: How many places changed, or an error saying why nothing did.
    """
    try:
        path = self._policy.writable(arguments.path)
    except path_policy_module.PathRefusedError as error:
        return tool.ToolResult.error(str(error))
    if not path.is_file():
        return tool.ToolResult.error(
            f'{arguments.path} does not exist; create it with write_file.'
        )
    text = path.read_text(encoding='utf-8')
    occurrences = text.count(arguments.old_text)
    if occurrences == 0:
        return tool.ToolResult.error(
            'old_text was not found. Read the file again and copy the text exactly.'
        )
    if occurrences > 1 and not arguments.replace_all:
        return tool.ToolResult.error(
            f'old_text appears {occurrences} times. Include more surrounding text so it appears once, or set replace_all to true.'
        )
    await self._snapshot(path)
    path.write_text(
        text.replace(arguments.old_text, arguments.new_text),
        encoding='utf-8',
    )
    return tool.ToolResult.success(
        f'Replaced {occurrences} occurrence(s) in {path.relative_to(self._policy.desk_directory)}.'
    )