Skip to content

trading_agent.terminal.terminal_application

The terminal program: a full-screen chat with the desks of a running gateway, like Claude Code.

It is a client of the gateway, as the web page is. One event stream connection carries every live event; the desks' states are read every 5 seconds and again after events that change them.

Typical usage example:

application = TerminalApplication(gateway, settings, history) application.run()

TerminalApplication

TerminalApplication(gateway, settings, history, desk_name=None)

Bases: App[int]

The terminal program.

Creates the program without connecting.

Parameters:

Name Type Description Default
gateway GatewayClient

The gateway's routes.

required
settings TerminalSettings

The remembered choices.

required
history PromptHistory

The earlier prompts.

required
desk_name str | None

The desk to open on, or None for the one used last.

None
Source code in src/trading_agent/terminal/terminal_application.py
def __init__(
    self,
    gateway: gateway_client.GatewayClient,
    settings: terminal_settings.TerminalSettings,
    history: prompt_history_module.PromptHistory,
    desk_name: str | None = None,
):
    """Creates the program without connecting.

    Args:
        gateway (gateway_client.GatewayClient): The gateway's routes.
        settings (terminal_settings.TerminalSettings): The remembered choices.
        history (prompt_history_module.PromptHistory): The earlier prompts.
        desk_name (str | None): The desk to open on, or None for the one used last.
    """
    super().__init__()
    self._gateway = gateway
    self.gateway = gateway
    self._settings = settings
    self._history = history
    self._theme_set = terminal_themes.TerminalThemes()
    self._first_desk = desk_name or settings.desk or _DEFAULT_DESK
    self.chat: chat_screen.ChatScreen | None = None
    self.opener = attachment_opener.AttachmentOpener(gateway)
    self.desks_document: dict[str, Any] = {}
    self.problem: str | None = None

on_mount

on_mount()

Applies the theme, opens the chat, and starts the event stream and the desk polling.

Source code in src/trading_agent/terminal/terminal_application.py
def on_mount(self) -> None:
    """Applies the theme, opens the chat, and starts the event stream and the desk polling."""
    for theme in self._theme_set.all():
        self.register_theme(theme)
    self.theme = self._settings.theme
    self.chat = chat_screen.ChatScreen(
        self._gateway,
        self._history,
        self._first_desk,
        self._settings,
        self.opener,
    )
    self.push_screen(self.chat)
    self._follow_events()
    self.refresh_desks()
    self.set_interval(_DESKS_REFRESH_SECONDS, self.refresh_desks)

on_unmount async

on_unmount()

Closes the gateway client's connections.

Source code in src/trading_agent/terminal/terminal_application.py
async def on_unmount(self) -> None:
    """Closes the gateway client's connections."""
    await self._gateway.close()

refresh_desks async

refresh_desks()

Reads the desks' states and passes them to the chat.

Source code in src/trading_agent/terminal/terminal_application.py
@work(exclusive=True, group='desks')
async def refresh_desks(self) -> None:
    """Reads the desks' states and passes them to the chat."""
    try:
        document = await self._gateway.desks()
    except gateway_client.GatewayError as error:
        if error.status_code == 401:
            self._refuse()
        return
    self.desks_document = document
    if self.chat is not None:
        self.chat.take_desks(document)

remember_desk

remember_desk(desk_name)

Remembers the desk the chat shows, for the next run.

Parameters:

Name Type Description Default
desk_name str

The desk.

required
Source code in src/trading_agent/terminal/terminal_application.py
def remember_desk(self, desk_name: str) -> None:
    """Remembers the desk the chat shows, for the next run.

    Args:
        desk_name (str): The desk.
    """
    self._settings.desk = desk_name
    self._settings.save()

open_page

open_page(command)

Opens a page, such as Trading, over the chat.

Parameters:

Name Type Description Default
command str

The page's slash command, such as "/trading".

required

Returns:

Type Description
bool

True when the command names a page.

Source code in src/trading_agent/terminal/terminal_application.py
def open_page(self, command: str) -> bool:
    """Opens a page, such as Trading, over the chat.

    Args:
        command (str): The page's slash command, such as "/trading".

    Returns:
        bool: True when the command names a page.
    """
    page = page_catalogue.PageCatalogue().find(command)
    if page is None:
        return False
    while len(self.screen_stack) > 2:
        self.pop_screen()
    desk_name = self._first_desk
    if self.chat is not None:
        desk_name = self.chat.desk_name
    self.push_screen(page.screen_class(desk_name))
    return True

action_pick_page

action_pick_page()

Opens the list of pages.

Source code in src/trading_agent/terminal/terminal_application.py
def action_pick_page(self) -> None:
    """Opens the list of pages."""

    def opened(command: str | None) -> None:
        """Opens the chosen page.

        Args:
            command (str | None): The page's command, or None.
        """
        if command is not None:
            self.open_page(command)

    self.push_screen(page_picker.PagePicker(), opened)

toggle_theme

toggle_theme()

Switches between the dark and light theme and remembers the choice.

Source code in src/trading_agent/terminal/terminal_application.py
def toggle_theme(self) -> None:
    """Switches between the dark and light theme and remembers the choice."""
    self.theme = self._theme_set.other(self.theme)
    self._settings.theme = self.theme
    self._settings.save()

action_toggle_theme

action_toggle_theme()

Switches between the dark and light theme.

Source code in src/trading_agent/terminal/terminal_application.py
def action_toggle_theme(self) -> None:
    """Switches between the dark and light theme."""
    self.toggle_theme()