Skip to content

trading_agent.tools.write_file_tool

The write_file tool: creates or replaces a file in the desk's own folders.

Typical usage example:

tool = WriteFileTool(policy, checkpoints)

WriteFileArguments

Bases: BaseModel

The arguments of write_file.

Attributes:

Name Type Description
path str

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

content str

The complete text to write.

WriteFileTool

WriteFileTool(policy, checkpoints=None)

Bases: Tool

Writes a whole 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/write_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)

Writes the file, creating its folders when needed.

Parameters:

Name Type Description Default
arguments WriteFileArguments

The file and its content.

required

Returns:

Type Description
ToolResult

How much was written, or an error.

Source code in src/trading_agent/tools/write_file_tool.py
async def run(self, arguments: WriteFileArguments) -> tool.ToolResult:
    """Writes the file, creating its folders when needed.

    Args:
        arguments (WriteFileArguments): The file and its content.

    Returns:
        tool.ToolResult: How much was written, or an error.
    """
    try:
        path = self._policy.writable(arguments.path)
    except path_policy_module.PathRefusedError as error:
        return tool.ToolResult.error(str(error))
    await self._snapshot(path)
    path.parent.mkdir(parents=True, exist_ok=True)
    path.write_text(arguments.content, encoding='utf-8')
    line_count = len(arguments.content.splitlines())
    return tool.ToolResult.success(
        f'Wrote {line_count} lines to {path.relative_to(self._policy.desk_directory)}.'
    )