Skip to content

trading_agent.tools.browser_screenshot_tool

The browser_screenshot tool: shows the model a picture of the current page.

Typical usage example:

tool = BrowserScreenshotTool(session, desk_directory)

BrowserScreenshotTool

BrowserScreenshotTool(session, desk_directory)

Bases: BrowserTool

Takes a screenshot of the visible part of the page.

Creates the tool.

Parameters:

Name Type Description Default
session BrowserSession

The desk's browser.

required
desk_directory Path

The desk's folder, so the saved path can be shown relative to it.

required
Source code in src/trading_agent/tools/browser_screenshot_tool.py
def __init__(
    self,
    session: browser_session_module.BrowserSession,
    desk_directory: Path,
):
    """Creates the tool.

    Args:
        session (browser_session_module.BrowserSession): The desk's browser.
        desk_directory (Path): The desk's folder, so the saved path can be shown relative to it.
    """
    super().__init__(session)
    self._desk_directory = desk_directory.resolve()

run async

run(arguments)

Takes the screenshot.

Parameters:

Name Type Description Default
arguments NoArguments

No arguments.

required

Returns:

Type Description
ToolResult

Where the picture was saved, with the picture attached, or an error.

Source code in src/trading_agent/tools/browser_screenshot_tool.py
async def run(self, arguments: tool.NoArguments) -> tool.ToolResult:
    """Takes the screenshot.

    Args:
        arguments (tool.NoArguments): No arguments.

    Returns:
        tool.ToolResult: Where the picture was saved, with the picture attached, or an error.
    """
    del arguments
    try:
        picture, path = await self._session.screenshot()
    except browser_session_module.BrowserError as error:
        return tool.ToolResult.error(str(error))
    shown_path = path.resolve().relative_to(self._desk_directory)
    return tool.ToolResult(
        text=f'Screenshot of the current page, saved as {shown_path}.',
        image=messages.MessageImage('image/png', picture),
        picture_desk_path=shown_path.as_posix(),
    )