class ProviderEditScreen(ModalScreen[bool]):
"""Edit (or disable) one provider."""
DEFAULT_CSS = (
"ProviderEditScreen {"
+ DIALOG_CSS
+ " TextArea { height: 5; } #buttons { height: auto; } }"
)
BINDINGS: ClassVar = [Binding("escape", "close", "Cancel")]
def __init__(self, connection: AgentConnection, provider: ProviderInfo) -> None:
super().__init__()
self.connection = connection
self.provider = provider
def compose(self) -> ComposeResult:
provider = self.provider
current = provider.current if is_set(provider.current) else None
with Vertical(id="dialog"):
yield Static(Content(f"Provider {provider.provider_id}"), id="dialog-title")
yield Static("API type")
yield Select(
[(api, api) for api in provider.supported],
value=current.api_type if current is not None else Select.NULL,
id="api-type",
)
yield Static("Base URL")
yield Input(current.base_url if current is not None else "", id="base-url")
yield Static("Headers (one 'Name: value' per line, replaces all headers)")
yield TextArea("", id="headers")
with Horizontal(id="buttons"):
yield Button("Save", variant="success", id="save", compact=True)
if not provider.required:
yield Button("Disable", variant="error", id="disable", compact=True)
yield Button("Cancel", id="cancel", compact=True)
async def on_button_pressed(self, event: Button.Pressed) -> None:
match event.button.id:
case "save":
api = self.query_one("#api-type", Select).value
url = self.query_one("#base-url", Input).value.strip()
if api is Select.NULL or not url:
self.notify("API type and base URL are required", severity="error")
return
headers = parse_headers(self.query_one("#headers", TextArea).text)
await self._run(
self.connection.set_provider(self.provider.provider_id, str(api), url, headers)
)
case "disable":
await self._run(self.connection.disable_provider(self.provider.provider_id))
case _:
self.dismiss(False)
async def _run(self, call: Awaitable[None]) -> None:
try:
await call
except (RequestError, ConnectionError) as exc:
self.notify(f"The agent rejected the change: {exc}", severity="error")
return
self.dismiss(True)
def action_close(self) -> None:
"""Cancel."""
self.dismiss(False)