Skip to content

trading_agent.terminal.screens.sessions_screen

The Sessions page: a desk's memory search, its sessions with their exchanges, its daily summaries and its notes.

It copies the web page's SessionsPage.tsx.

Typical usage example:

app.push_screen(SessionsScreen('general'))

SessionsScreen

SessionsScreen(desk_name)

Bases: PageScreen

The Sessions page.

Creates the page.

Parameters:

Name Type Description Default
desk_name str

The desk the chat shows.

required
Source code in src/trading_agent/terminal/screens/sessions_screen.py
def __init__(self, desk_name: str):
    """Creates the page.

    Args:
        desk_name (str): The desk the chat shows.
    """
    super().__init__(desk_name)
    self._sessions: list[dict[str, Any]] = []
    self._formatter = formatter.Formatter()
    self._detail_shown = False

compose_page

compose_page()

Builds the search, the session list, the exchanges and the summaries and notes.

Yields:

Type Description
Widget

The page's parts.

Source code in src/trading_agent/terminal/screens/sessions_screen.py
def compose_page(self):
    """Builds the search, the session list, the exchanges and the summaries and notes.

    Yields:
        Widget: The page's parts.
    """
    yield widgets.Input(
        placeholder="Search the desk's memory, then press enter (/ to focus)",
        id='sessions-search',
    )
    with containers.Horizontal(classes='page-columns'):
        with containers.Vertical(classes='page-left'):
            yield widgets.Static('[b]Sessions[/b]', classes='page-heading')
            yield widgets.OptionList(id='sessions-list')
        with containers.VerticalScroll(
            classes='page-right', id='sessions-right'
        ):
            yield widgets.Static('', id='sessions-shown')

refresh_page async

refresh_page()

Reads the desk's sessions, summaries and notes.

Source code in src/trading_agent/terminal/screens/sessions_screen.py
@work(exclusive=True, group='page')
async def refresh_page(self) -> None:
    """Reads the desk's sessions, summaries and notes."""
    gateway = self.app.gateway
    try:
        sessions = await gateway.sessions(self.desk_name)
        summaries = await gateway.summaries(self.desk_name)
        notes = await gateway.notes(self.desk_name)
    except gateway_client.GatewayError as error:
        self.problem(error.message)
        return
    self._sessions = sessions.get('sessions') or []
    current = sessions.get('current')
    listing = self.query_one('#sessions-list', widgets.OptionList)
    highlighted = listing.highlighted
    listing.clear_options()
    for session in self._sessions:
        mark = ''
        if session.get('session_id') == current:
            mark = '  current'
        listing.add_option(
            option_list.Option(
                Content.from_markup(
                    '$session [dim]$count exchanges$mark[/dim]',
                    session=str(session.get('session_id')),
                    count=str(session.get('exchanges', 0)),
                    mark=mark,
                )
            )
        )
    if highlighted is not None and highlighted < len(self._sessions):
        listing.highlighted = highlighted
    elif self._sessions:
        listing.highlighted = 0
    if not self._detail_shown:
        self._show_summaries_and_notes(
            summaries.get('summaries') or [], notes.get('notes') or []
        )

action_next_desk

action_next_desk()

Shows the next desk, starting again from its summaries and notes.

Source code in src/trading_agent/terminal/screens/sessions_screen.py
def action_next_desk(self) -> None:
    """Shows the next desk, starting again from its summaries and notes."""
    self._detail_shown = False
    super().action_next_desk()
action_focus_search()

Moves to the search box.

Source code in src/trading_agent/terminal/screens/sessions_screen.py
def action_focus_search(self) -> None:
    """Moves to the search box."""
    self.query_one('#sessions-search', widgets.Input).focus()

on_option_list_option_selected

on_option_list_option_selected(event)

Shows the chosen session's exchanges.

Parameters:

Name Type Description Default
event OptionSelected

The choice.

required
Source code in src/trading_agent/terminal/screens/sessions_screen.py
def on_option_list_option_selected(
    self, event: widgets.OptionList.OptionSelected
) -> None:
    """Shows the chosen session's exchanges.

    Args:
        event (widgets.OptionList.OptionSelected): The choice.
    """
    event.stop()
    self._show_session(
        str(self._sessions[event.option_index].get('session_id'))
    )

on_input_submitted

on_input_submitted(event)

Searches the desk's memory.

Parameters:

Name Type Description Default
event Submitted

The search words.

required
Source code in src/trading_agent/terminal/screens/sessions_screen.py
def on_input_submitted(self, event: widgets.Input.Submitted) -> None:
    """Searches the desk's memory.

    Args:
        event (widgets.Input.Submitted): The search words.
    """
    event.stop()
    if event.value.strip():
        self._search(event.value.strip())