From 7c80500280036b2d13326910568233659aa450e5 Mon Sep 17 00:00:00 2001 From: Alvin Date: Fri, 13 Nov 2026 10:08:51 +0100 Subject: [PATCH] Add back button to model picker --- README.md | 2 +- ai_chat.py | 6 ++++++ test_ai_chat.py | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index e78886b..858e3a6 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ python run_example.py --help Command-line options `--ai1-name`, `--ai1-url`, `--ai1-key`, `--ai1-model`, `--ai1-prompt` and matching `--ai2-*` options override `.env` values for a run. When any connection option is supplied, both connections need a base URL and API key after applying defaults. Prefer environment variables over command-line arguments for hosted-provider secrets, since command-line values can enter shell history. -Press **Ctrl+M** to add, edit, delete, and select saved connections. They are stored in `sloppy.json` in the current working directory: +Press **Ctrl+M** to add, edit, delete, and select saved connections. Use **Back** to leave the picker without saving; if you leave it at launch, press **Ctrl+M** to reopen it. Connections are stored in `sloppy.json` in the current working directory: ```json { diff --git a/ai_chat.py b/ai_chat.py index 79f2339..a390997 100644 --- a/ai_chat.py +++ b/ai_chat.py @@ -240,6 +240,7 @@ class ModelSettings(ModalScreen[tuple[AIConfig, AIConfig] | None]): def compose(self) -> ComposeResult: with VerticalScroll(id="model-settings"): yield Static("[bold]Models and connections[/bold] • saved in sloppy.json") + yield Button("Back (Esc)", id="back-models") yield Label("AI 1") yield Select([], id="ai1-select", allow_blank=True) yield Label("AI 2") @@ -290,6 +291,9 @@ class ModelSettings(ModalScreen[tuple[AIConfig, AIConfig] | None]): def on_button_pressed(self, event: Button.Pressed) -> None: from settings import model_from_settings, save_settings, store_model action = event.button.id + if action == 'back-models': + self.dismiss(None) + return identifier = self.query_one('#connection-id', Input).value.strip() try: if action == 'save-model': @@ -493,6 +497,8 @@ class AIChatApp(App): def _models_selected(self, models: tuple[AIConfig, AIConfig] | None) -> None: if models is None: + if not self.messages: + self.update_status("No conversation started. Press Ctrl+M to choose models.") return if self._is_running: self.notify("Saved selection applies to the next conversation") diff --git a/test_ai_chat.py b/test_ai_chat.py index 83895a6..fb29401 100644 --- a/test_ai_chat.py +++ b/test_ai_chat.py @@ -11,10 +11,11 @@ from unittest.mock import patch from rich.console import Console from rich.markdown import Markdown +from textual.widgets import Button import httpx -from ai_chat import AIChatApp, AIConfig, MessageBubble, ChatMessage, ControlsHelp, ScreenshotOptions, main +from ai_chat import AIChatApp, AIConfig, MessageBubble, ChatMessage, ControlsHelp, ScreenshotOptions, ModelSettings, main from settings import load_settings, save_settings, store_model, model_from_settings @@ -211,6 +212,20 @@ class ChatTests(unittest.IsolatedAsyncioTestCase): await self.wait_finished(app, pilot) self.assertEqual(app.messages[-1].content, 'working') + async def test_back_exits_model_picker_without_saving(self): + app = await self.make_app(lambda request: httpx.Response(500)) + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / 'sloppy.json' + app.settings_path = path + app.choose_models = True + async with app.run_test() as pilot: + self.assertIsInstance(app.screen, ModelSettings) + app.screen.query_one('#back-models', Button).press() + await pilot.pause() + self.assertNotIsInstance(app.screen, ModelSettings) + self.assertFalse(path.exists()) + self.assertIn('Ctrl+M', str(app.status_bar.render())) + async def test_empty_stream_uses_fallback(self): calls = [] def handler(request):