class AgentPicker(Screen[None]):
"""Choose an agent from the registry, or type a command / URL."""
DEFAULT_CSS = """
AgentPicker {
align: center top;
#picker {
width: 100%;
max-width: 110;
height: 1fr;
padding: 1 2 0 2;
}
#heading { text-style: bold; color: $accent; margin: 0 0 1 0; }
#agents { height: 1fr; border: round $primary 50%; }
#agents:focus { border: round $primary; }
#custom { margin: 1 0 0 0; }
#hint { color: $text-muted; margin: 1 0 0 0; }
}
"""
BINDINGS: ClassVar = [
Binding("c", "custom", "Command / URL"),
Binding("escape", "focus_list", "Back", show=False),
Binding("q", "app.quit", "Quit"),
]
class Picked(Message):
"""An agent was chosen."""
def __init__(self, spec: AgentSpec) -> None:
super().__init__()
self.spec = spec
def __init__(self, agents: list[AgentSpec], cwd: str) -> None:
super().__init__()
self.agents = agents
self.cwd = cwd
def compose(self) -> ComposeResult:
with Vertical(id="picker"):
home = str(Path.home())
cwd = "~" + self.cwd[len(home) :] if self.cwd.startswith(home) else self.cwd
heading = Content.assemble("acptui · pick an agent for ", (cwd, "italic"))
yield Static(heading, id="heading")
yield OptionList(*(agent_option(spec) for spec in self.agents), id="agents")
yield Input(
placeholder="…or an agent command (e.g. my-agent --acp) or URL, then enter",
id="custom",
)
yield Static(
"acptui speaks ACP v2 only: agents that negotiate another version get an error.",
id="hint",
)
yield Footer()
def on_mount(self) -> None:
options = self.query_one("#agents", OptionList)
options.focus()
first_available = next((i for i, spec in enumerate(self.agents) if spec.available), 0)
options.highlighted = first_available
def on_option_list_option_selected(self, event: OptionList.OptionSelected) -> None:
spec = next((s for s in self.agents if s.id == event.option_id), None)
if spec is not None:
self.post_message(self.Picked(spec))
def on_input_submitted(self, event: Input.Submitted) -> None:
value = event.value.strip()
if not value:
return
spec = url_spec(value) if "://" in value else command_spec(value.split())
self.post_message(self.Picked(spec))
def action_custom(self) -> None:
"""Focus the command input."""
self.query_one("#custom", Input).focus()
def action_focus_list(self) -> None:
"""Focus the agent list."""
self.query_one("#agents", OptionList).focus()