Skip to content

trading_agent.terminal.widgets.desk_message_view

A message the desk sent with send_message or send_image: its text, its pictures and its documents.

Pictures are drawn in the chat; documents are listed, and Enter on one opens it with the desktop's default program.

Typical usage example:

view = DeskMessageView('general', event)

DocumentLine

DocumentLine(desk_name, attachment)

Bases: Static

One document in a desk message; Enter or a click opens it.

Creates the line.

Parameters:

Name Type Description Default
desk_name str

The desk that sent it.

required
attachment dict[str, Any]

The document.

required
Source code in src/trading_agent/terminal/widgets/desk_message_view.py
def __init__(self, desk_name: str, attachment: dict[str, Any]):
    """Creates the line.

    Args:
        desk_name (str): The desk that sent it.
        attachment (dict[str, Any]): The document.
    """
    super().__init__(
        Content.from_markup(
            '▣ [u]$name[/u] [dim]enter or click to open[/dim]',
            name=str(attachment.get('file_name', 'document')),
        ),
        classes='document-line',
    )
    self._desk_name = desk_name
    self._attachment = attachment

on_click

on_click()

Opens the document on a click.

Source code in src/trading_agent/terminal/widgets/desk_message_view.py
def on_click(self) -> None:
    """Opens the document on a click."""
    self.action_open_document()

action_open_document

action_open_document()

Downloads the document and opens it.

Source code in src/trading_agent/terminal/widgets/desk_message_view.py
def action_open_document(self) -> None:
    """Downloads the document and opens it."""
    self._open()

DeskMessageView

DeskMessageView(desk_name, event, with_time=False)

Bases: Vertical

A message from the desk.

Creates the view.

Parameters:

Name Type Description Default
desk_name str

The desk that sent it.

required
event dict[str, Any]

The message event.

required
with_time bool

Whether to show the time, for a message outside a turn.

False
Source code in src/trading_agent/terminal/widgets/desk_message_view.py
def __init__(
    self,
    desk_name: str,
    event: dict[str, Any],
    with_time: bool = False,
):
    """Creates the view.

    Args:
        desk_name (str): The desk that sent it.
        event (dict[str, Any]): The message event.
        with_time (bool): Whether to show the time, for a message outside a turn.
    """
    payload = event.get('payload') or {}
    classes = 'desk-message'
    if payload.get('urgent'):
        classes += ' urgent'
    super().__init__(classes=classes)
    self._desk_name = desk_name
    self._event = event
    self._with_time = with_time

compose

compose()

Builds the view.

Yields:

Type Description
Widget

The heading, the text, the pictures and the documents.

Source code in src/trading_agent/terminal/widgets/desk_message_view.py
def compose(self):
    """Builds the view.

    Yields:
        Widget: The heading, the text, the pictures and the documents.
    """
    payload = self._event.get('payload') or {}
    attachments = payload.get('attachments')
    if not isinstance(attachments, list):
        attachments = []
    pictures = []
    documents = []
    for attachment in attachments:
        if attachment.get('kind') == 'image':
            pictures.append(attachment)
        else:
            documents.append(attachment)
    heading = 'Message from the desk'
    if payload.get('urgent'):
        heading = 'Urgent message from the desk'
    elif pictures and not documents:
        heading = 'Picture from the desk'
    time_text = ''
    if self._with_time:
        time_text = formatter.Formatter().time(
            str(self._event.get('created_at', ''))
        )
    yield widgets.Static(
        Content.from_markup(
            '[b]$heading[/b] [dim]$time[/dim]',
            heading=heading,
            time=time_text,
        ),
        classes='desk-message-heading',
    )
    text = str(payload.get('text') or '')
    if text:
        yield widgets.Markdown(text)
    for attachment in pictures:
        yield picture_view.PictureView(
            self._desk_name, attachment=attachment
        )
    for attachment in documents:
        yield DocumentLine(self._desk_name, attachment)