Skip to content

trading_agent.tools.read_file_tool

The read_file tool: shows a text file with line numbers.

Typical usage example:

tool = ReadFileTool(policy)

ReadFileArguments

Bases: BaseModel

The arguments of read_file.

Attributes:

Name Type Description
path str

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

start_line int

The first line to show, counting from 1.

maximum_lines int

How many lines to show at most.

ReadFileTool

ReadFileTool(policy)

Bases: Tool

Reads part of a text file.

Creates the tool.

Parameters:

Name Type Description Default
policy PathPolicy

The desk's read and write rules.

required
Source code in src/trading_agent/tools/read_file_tool.py
def __init__(self, policy: path_policy_module.PathPolicy):
    """Creates the tool.

    Args:
        policy (path_policy_module.PathPolicy): The desk's read and write rules.
    """
    self._policy = policy

run async

run(arguments)

Reads the requested lines.

Parameters:

Name Type Description Default
arguments ReadFileArguments

The file and which lines.

required

Returns:

Type Description
ToolResult

The numbered lines, with a note when more follow, or an error.

Source code in src/trading_agent/tools/read_file_tool.py
async def run(self, arguments: ReadFileArguments) -> tool.ToolResult:
    """Reads the requested lines.

    Args:
        arguments (ReadFileArguments): The file and which lines.

    Returns:
        tool.ToolResult: The numbered lines, with a note when more follow, or an error.
    """
    try:
        path = self._policy.readable(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} is not a file.')
    lines = path.read_text(encoding='utf-8', errors='replace').splitlines()
    first = arguments.start_line - 1
    shown = lines[first : first + arguments.maximum_lines]
    numbered = []
    for offset, line in enumerate(shown):
        numbered.append(f'{first + offset + 1:6}  {line}')
    remaining = len(lines) - first - len(shown)
    if remaining > 0:
        numbered.append(
            f'[{remaining} more lines; read again with start_line={first + len(shown) + 1}]'
        )
    if not numbered:
        numbered.append(f'[{arguments.path} has {len(lines)} lines]')
    return tool.ToolResult.success('\n'.join(numbered))