Skip to content

trading_agent.terminal.widgets.model_picker

A dialog for choosing which model a desk tries first, as the web page's model menu does.

It lists the desk's backends in their configured order, then each provider's other models, then "Auto" for the configured order with fallback. The web page shows the other models in submenus; here they follow under their provider's heading. Keys 1 to 9 pick a backend directly.

It returns a (backend, model) pair, with (None, None) for Auto, or None when the user presses Escape.

Typical usage example:

app.push_screen(ModelPicker(desk_summary), on_chosen)

ModelPicker

ModelPicker(desk)

Bases: ModalScreen[ModelChoice | None]

The model list.

Creates the dialog.

Parameters:

Name Type Description Default
desk dict[str, Any]

The desk's summary from GET /api/desks, with its backends.

required
Source code in src/trading_agent/terminal/widgets/model_picker.py
def __init__(self, desk: dict[str, Any]):
    """Creates the dialog.

    Args:
        desk (dict[str, Any]): The desk's summary from GET /api/desks, with its backends.
    """
    super().__init__(classes='picker-screen')
    self._desk = desk
    self._names = model_names.ModelNames()
    self._choices: list[ModelChoice | None] = []
    self._rows: list[option_list.Option] = []
    self._current_row: int | None = None
    self._build()

compose

compose()

Builds the dialog.

Yields:

Type Description
Widget

The title, the list and the hint.

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

    Yields:
        Widget: The title, the list and the hint.
    """
    with containers.Vertical(classes='picker'):
        yield widgets.Static(
            Content.from_markup(
                '[b]Model for $desk[/b]', desk=self._desk['name']
            ),
            classes='picker-title',
        )
        yield widgets.OptionList(*self._rows, id='model-options')
        yield widgets.Static(
            '[dim]↑↓ to move · enter or 1-9 to choose · esc to close[/dim]',
            classes='picker-hint',
        )

on_mount

on_mount()

Highlights the current choice.

Source code in src/trading_agent/terminal/widgets/model_picker.py
def on_mount(self) -> None:
    """Highlights the current choice."""
    options = self.query_one('#model-options', widgets.OptionList)
    options.highlighted = self._current_index()
    options.focus()

on_key

on_key(event)

Picks a backend with its number key.

Parameters:

Name Type Description Default
event Key

The key press.

required
Source code in src/trading_agent/terminal/widgets/model_picker.py
def on_key(self, event: events.Key) -> None:
    """Picks a backend with its number key.

    Args:
        event (events.Key): The key press.
    """
    if not event.character or not event.character.isdigit():
        return
    number = int(event.character)
    backends = self._desk.get('backends') or []
    if 1 <= number <= len(backends):
        event.stop()
        self.dismiss(
            (
                backends[number - 1]['name'],
                None,
            )
        )

on_option_list_option_selected

on_option_list_option_selected(event)

Returns the chosen backend and model.

Parameters:

Name Type Description Default
event OptionSelected

The choice.

required
Source code in src/trading_agent/terminal/widgets/model_picker.py
def on_option_list_option_selected(
    self, event: widgets.OptionList.OptionSelected
) -> None:
    """Returns the chosen backend and model.

    Args:
        event (widgets.OptionList.OptionSelected): The choice.
    """
    choice = self._choices[event.option_index]
    if choice is None:
        return
    self.dismiss(choice)

action_cancel

action_cancel()

Closes the dialog without a choice.

Source code in src/trading_agent/terminal/widgets/model_picker.py
def action_cancel(self) -> None:
    """Closes the dialog without a choice."""
    self.dismiss(None)