Skip to content

trading_agent.tools.search_sessions_tool

The search_sessions tool: finds what the desk said and did before.

Typical usage example:

tool = SearchSessionsTool(SessionSearch(desk_directory))

SearchSessionsArguments

Bases: BaseModel

The arguments of search_sessions.

Attributes:

Name Type Description
keywords str

A few keywords separated by spaces, such as "NIFTY gap down".

limit int

The most results to return.

SearchSessionsTool

SearchSessionsTool(search)

Bases: Tool

Searches the desk's past sessions, summaries, notes and journal.

Creates the tool.

Parameters:

Name Type Description Default
search SessionSearch

The desk's search.

required
Source code in src/trading_agent/tools/search_sessions_tool.py
def __init__(self, search: session_search_module.SessionSearch):
    """Creates the tool.

    Args:
        search (session_search_module.SessionSearch): The desk's search.
    """
    self._search = search

run async

run(arguments)

Runs the search in a worker thread, because it reads many files.

Parameters:

Name Type Description Default
arguments SearchSessionsArguments

The keywords and the limit.

required

Returns:

Type Description
ToolResult

The hits, one block each, or a note that nothing matched.

Source code in src/trading_agent/tools/search_sessions_tool.py
async def run(self, arguments: SearchSessionsArguments) -> tool.ToolResult:
    """Runs the search in a worker thread, because it reads many files.

    Args:
        arguments (SearchSessionsArguments): The keywords and the limit.

    Returns:
        tool.ToolResult: The hits, one block each, or a note that nothing matched.
    """
    result = await asyncio.to_thread(
        self._search.search,
        arguments.keywords,
        arguments.limit,
    )
    if not result.words:
        return tool.ToolResult.error(
            'Give at least one keyword made of letters or digits.'
        )
    if not result.hits:
        return tool.ToolResult.success(
            f'Nothing in your past matches any of: {", ".join(result.words)}.'
        )
    if result.matched_every_word:
        heading = f'{len(result.hits)} result(s) containing every keyword:'
    else:
        heading = f'Nothing contains every keyword, so these {len(result.hits)} result(s) contain some of them:'
    lines = [
        heading,
    ]
    for hit in result.hits:
        lines.append('')
        lines.append(f'[{hit.record.day} · {hit.record.kind}]')
        lines.append(hit.snippet)
    return tool.ToolResult.success('\n'.join(lines))