class ConfigScreen(ModalScreen[ConfigChoice | None]):
"""Session options (mode, model, ...) as the agent exposes them; returns a new value."""
DEFAULT_CSS = "ConfigScreen {" + DIALOG_CSS + "}"
BINDINGS: ClassVar = [Binding("escape", "back", "Back")]
def __init__(self, options: list[SessionConfigOption]) -> None:
super().__init__()
self.options = options
self.current: SelectSessionConfigOption | None = None
def compose(self) -> ComposeResult:
with Vertical(id="dialog"):
yield Static("Session options", id="dialog-title")
yield OptionList(id="config")
yield Static("enter: change ยท escape: close", id="dialog-hint")
def on_mount(self) -> None:
self._show_options()
def _show_options(self) -> None:
self.current = None
self.query_one("#dialog-title", Static).update("Session options")
options = self.query_one("#config", OptionList)
options.clear_options()
if not self.options:
self.query_one("#dialog-hint", Static).update(
"The agent offers no options for this session."
)
for index, option in enumerate(self.options):
category = option.category if is_set(option.category) and option.category else ""
label = Content.assemble(
(option.name, "bold"),
": ",
(option_value_label(option), "$text-accent"),
(f" ({category})" if category else "", "$text-muted"),
)
options.add_option(Option(label, id=f"o:{index}"))
if self.options:
options.highlighted = 0
options.focus()
def _show_values(self, option: SelectSessionConfigOption) -> None:
self.current = option
self.query_one("#dialog-title", Static).update(Content(option.name))
options = self.query_one("#config", OptionList)
options.clear_options()
group = None
highlight = 0
for index, (value, name, group_name) in enumerate(_select_values(option)):
if group_name and group_name != group:
group = group_name
options.add_option(
Option(Content(group_name).stylize("bold $text-muted"), disabled=True)
)
marker = "โ " if value == option.current_value else " "
options.add_option(Option(Content(marker + name), id=f"v:{index}"))
if value == option.current_value:
highlight = options.option_count - 1
options.highlighted = highlight
options.focus()
def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
option_id = event.option_id or ""
if option_id.startswith("o:"):
option = self.options[int(option_id[2:])]
if isinstance(option, SelectSessionConfigOption):
self._show_values(option)
elif isinstance(option, BooleanSessionConfigOption):
self.dismiss((option.config_id, not option.current_value))
elif option_id.startswith("v:") and self.current is not None:
value = _select_values(self.current)[int(option_id[2:])][0]
self.dismiss((self.current.config_id, value))
def action_back(self) -> None:
"""Back to the option list, or close."""
if self.current is not None:
self._show_options()
else:
self.dismiss(None)