From 661dfc6668f4e414461a11f0ee3b934ed1a3d303 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sun, 25 Oct 2026 21:51:53 +0000 Subject: [PATCH] feat(app): add prompt presets to chat creation --- .../chat/ui/sessions/chat_prompt_chooser.dart | 81 +++++++++ .../chat/ui/sessions/create_chat_dialog.dart | 68 +++++--- .../ui/sessions/chat_create_prompt_test.dart | 161 ++++++++++++++++-- 3 files changed, 276 insertions(+), 34 deletions(-) create mode 100644 app/lib/features/chat/ui/sessions/chat_prompt_chooser.dart diff --git a/app/lib/features/chat/ui/sessions/chat_prompt_chooser.dart b/app/lib/features/chat/ui/sessions/chat_prompt_chooser.dart new file mode 100644 index 00000000..c5b86634 --- /dev/null +++ b/app/lib/features/chat/ui/sessions/chat_prompt_chooser.dart @@ -0,0 +1,81 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; + +import 'chat_create_fields.dart'; + +/// Native platform controls for choosing what editable prompt to start with. +class ChatPromptChooser extends StatelessWidget { + const ChatPromptChooser({ + super.key, + required this.cupertino, + required this.enabled, + required this.choice, + required this.hasLastUsed, + required this.onEnabled, + required this.onChoice, + }); + + static const custom = 'custom'; + static const careful = 'careful'; + static const lastUsed = 'last-used'; + + final bool cupertino; + final bool enabled; + final String choice; + final bool hasLastUsed; + final ValueChanged onEnabled; + final ValueChanged onChoice; + + @override + Widget build(BuildContext context) { + final choices = [custom, careful, if (hasLastUsed) lastUsed]; + String label(String value) => switch (value) { + custom => 'Custom', + careful => 'Careful coding', + lastUsed => 'Last used prompt', + _ => value, + }; + + return Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Row( + children: [ + const Expanded(child: Text('Start with a prompt')), + if (cupertino) + CupertinoSwitch( + key: const Key('chat-create-send-prompt'), + value: enabled, + onChanged: onEnabled, + ) + else + Switch( + key: const Key('chat-create-send-prompt'), + value: enabled, + onChanged: onEnabled, + ), + ], + ), + if (enabled) + if (cupertino) + CupertinoPickerField( + fieldKey: const Key('chat-create-prompt-choice'), + label: 'Prompt', + value: choices.contains(choice) ? choice : custom, + values: choices, + labelOf: label, + onSelected: onChoice, + ) + else + MaterialPickerField( + fieldKey: const Key('chat-create-prompt-choice'), + label: 'Prompt', + value: choices.contains(choice) ? choice : custom, + values: choices, + labelOf: label, + onSelected: onChoice, + ), + ], + ); + } +} diff --git a/app/lib/features/chat/ui/sessions/create_chat_dialog.dart b/app/lib/features/chat/ui/sessions/create_chat_dialog.dart index b8e7a2b2..5904d518 100644 --- a/app/lib/features/chat/ui/sessions/create_chat_dialog.dart +++ b/app/lib/features/chat/ui/sessions/create_chat_dialog.dart @@ -11,6 +11,7 @@ import '../../domain/chat_commands.dart'; import 'chat_adaptive.dart'; import 'chat_create_dialog_frame.dart'; import 'chat_create_fields.dart'; +import 'chat_prompt_chooser.dart'; /// The instructions field's helper and the model note, verbatim from the web. const String chatCreateInstructionsHelper = @@ -93,8 +94,10 @@ class _CreateChatDialogState extends State { /// Whether the new chat starts with the prompt below. bool _sendPrompt = false; - /// The remembered prompt fills in only until the operator types one. - bool _promptEdited = false; + /// A user's choice or edit wins over any pending saved-prompt read. + bool _promptTouched = false; + String _promptChoice = ChatPromptChooser.custom; + ChatCreatePrompt? _savedPrompt; int _recall = 0; List _directories = const []; List _backends = const []; @@ -168,12 +171,30 @@ class _CreateChatDialogState extends State { final recall = ++_recall; final saved = await _prompts.read(backend, directory); if (!mounted || recall != _recall) return; - if (_promptEdited) return; + if (_promptTouched) return; setState(() { // Prompt text can be recalled, but sending it always requires an // explicit choice in this new-chat dialog. _sendPrompt = false; _instructions.text = saved?.text ?? chatDefaultSessionInstructions; + _savedPrompt = saved; + _promptChoice = ChatPromptChooser.custom; + }); + } + + void _selectPrompt(String choice) { + setState(() { + _promptTouched = true; + _promptChoice = choice; + if (choice == ChatPromptChooser.careful) { + _instructions.text = chatDefaultSessionInstructions; + _sendPrompt = true; + } else if (choice == ChatPromptChooser.lastUsed) { + _instructions.text = _savedPrompt!.text; + _sendPrompt = true; + } else { + _sendPrompt = true; + } }); } @@ -258,33 +279,31 @@ class _CreateChatDialogState extends State { onChanged: (_) => _nameEdited = true, ), const SizedBox(height: 12), - Row( - children: [ - const Expanded(child: Text('Start with a prompt')), - if (cupertino) - CupertinoSwitch( - key: const Key('chat-create-send-prompt'), - value: _sendPrompt, - onChanged: (value) => setState(() => _sendPrompt = value), - ) - else - Switch( - key: const Key('chat-create-send-prompt'), - value: _sendPrompt, - onChanged: (value) => setState(() => _sendPrompt = value), - ), - ], + ChatPromptChooser( + cupertino: cupertino, + enabled: _sendPrompt, + choice: _promptChoice, + hasLastUsed: _savedPrompt?.text.trim().isNotEmpty == true, + onEnabled: (value) => setState(() { + _promptTouched = true; + _sendPrompt = value; + }), + onChoice: _selectPrompt, ), if (_sendPrompt) ...[ const SizedBox(height: 4), - const Text('Pre-filled prompt (editable)'), if (cupertino) CupertinoTextField( key: const Key('chat-create-instructions'), controller: _instructions, minLines: 2, maxLines: 4, - onChanged: (_) => _promptEdited = true, + onChanged: (_) { + setState(() { + _promptTouched = true; + _promptChoice = ChatPromptChooser.custom; + }); + }, ) else TextField( @@ -292,7 +311,12 @@ class _CreateChatDialogState extends State { controller: _instructions, minLines: 2, maxLines: 4, - onChanged: (_) => _promptEdited = true, + onChanged: (_) { + setState(() { + _promptTouched = true; + _promptChoice = ChatPromptChooser.custom; + }); + }, ), const SizedBox(height: 4), const Text(chatCreateInstructionsHelper), diff --git a/app/test/features/chat/ui/sessions/chat_create_prompt_test.dart b/app/test/features/chat/ui/sessions/chat_create_prompt_test.dart index 1a3501e0..64573584 100644 --- a/app/test/features/chat/ui/sessions/chat_create_prompt_test.dart +++ b/app/test/features/chat/ui/sessions/chat_create_prompt_test.dart @@ -1,8 +1,12 @@ +import 'dart:async'; + import 'package:flutter/material.dart'; +import 'package:flutter/cupertino.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:pipelines_app/features/chat/data/chat_create_prompt_store.dart'; import 'package:pipelines_app/features/chat/data/chat_repository.dart'; import 'package:pipelines_app/features/chat/ui/sessions/create_chat_dialog.dart'; +import 'package:pipelines_app/features/chat/domain/chat_commands.dart'; import 'fake_chat_gateway.dart'; @@ -21,6 +25,14 @@ class _MemoryPrompts implements ChatCreatePromptStore { ) async => saved['$backend|$directory'] = value; } +class _DelayedPrompts extends _MemoryPrompts { + final Completer result = Completer(); + + @override + Future read(String backend, String directory) => + result.future; +} + Future _create( WidgetTester tester, _MemoryPrompts prompts, { @@ -72,19 +84,18 @@ void main() { expect(defaultChatName('~', 'OpenCode'), 'Home OpenCode'); }); - testWidgets( - 'a custom prompt is sent, but the next chat defaults to off', - (tester) async { - final prompts = _MemoryPrompts(); - final first = await _create(tester, prompts, prompt: 'Review only.'); - expect(first!.instructions, 'Review only.'); - expect(prompts.saved.values.single, (send: true, text: 'Review only.')); + testWidgets('a custom prompt is sent, but the next chat defaults to off', ( + tester, + ) async { + final prompts = _MemoryPrompts(); + final first = await _create(tester, prompts, prompt: 'Review only.'); + expect(first!.instructions, 'Review only.'); + expect(prompts.saved.values.single, (send: true, text: 'Review only.')); - await tester.pumpWidget(const SizedBox()); - final second = await _create(tester, prompts); - expect(second!.instructions, isEmpty); - }, - ); + await tester.pumpWidget(const SizedBox()); + final second = await _create(tester, prompts); + expect(second!.instructions, isEmpty); + }); testWidgets('a chat starts without a prompt even after a prior choice', ( tester, @@ -98,4 +109,130 @@ void main() { await _create(tester, prompts); expect(prompts.saved.values.single.send, isFalse); }); + + testWidgets('the prompt menu selects a preset and permits custom edits', ( + tester, + ) async { + final prompts = _MemoryPrompts(); + await _open(tester, prompts); + await tester.tap(find.byKey(const Key('chat-create-send-prompt'))); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-create-prompt-choice'))); + await tester.pumpAndSettle(); + await tester.tap(find.text('Careful coding').last); + await tester.pumpAndSettle(); + expect( + tester + .widget(find.byKey(const Key('chat-create-instructions'))) + .controller! + .text, + chatDefaultSessionInstructions, + ); + await tester.enterText( + find.byKey(const Key('chat-create-instructions')), + 'My own instructions', + ); + await tester.pumpAndSettle(); + expect(find.text('Custom'), findsOneWidget); + expect(tester.takeException(), isNull); + }); + + testWidgets('a recalled prompt is offered as an editable preset', ( + tester, + ) async { + final prompts = _MemoryPrompts() + ..saved['opencode|~'] = (send: true, text: 'Previously used'); + await _open(tester, prompts); + expect(find.byKey(const Key('chat-create-instructions')), findsNothing); + await tester.tap(find.byKey(const Key('chat-create-send-prompt'))); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-create-prompt-choice'))); + await tester.pumpAndSettle(); + await tester.tap(find.text('Last used prompt').last); + await tester.pumpAndSettle(); + expect( + tester + .widget(find.byKey(const Key('chat-create-instructions'))) + .controller! + .text, + 'Previously used', + ); + }); + + testWidgets('a delayed saved prompt cannot override a user edit', ( + tester, + ) async { + final prompts = _DelayedPrompts(); + await _open(tester, prompts, settle: false); + await tester.pump(const Duration(milliseconds: 1)); + await tester.tap(find.byKey(const Key('chat-create-send-prompt'))); + await tester.pump(); + await tester.enterText( + find.byKey(const Key('chat-create-instructions')), + 'User choice wins', + ); + prompts.result.complete((send: true, text: 'Stale saved prompt')); + await tester.pumpAndSettle(); + expect( + tester + .widget(find.byKey(const Key('chat-create-instructions'))) + .controller! + .text, + 'User choice wins', + ); + }); + + testWidgets('Cupertino prompt chooser has native preset and custom choices', ( + tester, + ) async { + final prompts = _MemoryPrompts(); + await tester.pumpWidget( + CupertinoApp( + home: CupertinoPageScaffold( + child: CreateChatDialog( + repository: ChatRepository(FakeChatGateway()), + operationId: 'op', + prompts: prompts, + onSubmit: (_, _) async => {'id': 'new'}, + ), + ), + ), + ); + await tester.pumpAndSettle(); + expect(find.byKey(const Key('chat-create-send-prompt')), findsOneWidget); + expect(find.byKey(const Key('chat-create-instructions')), findsNothing); + await tester.tap(find.byKey(const Key('chat-create-send-prompt'))); + await tester.pumpAndSettle(); + await tester.tap(find.byKey(const Key('chat-create-prompt-choice'))); + await tester.pumpAndSettle(); + expect(find.byType(CupertinoActionSheet), findsOneWidget); + expect(find.text('Careful coding'), findsOneWidget); + await tester.tap(find.text('Cancel').last); + await tester.pumpAndSettle(); + expect(tester.takeException(), isNull); + }); +} + +Future _open( + WidgetTester tester, + ChatCreatePromptStore prompts, { + bool settle = true, +}) async { + final gateway = FakeChatGateway() + ..directoriesHandler = () async => { + 'chat_directories': ['/work/pipelines'], + }; + await tester.pumpWidget( + MaterialApp( + home: Scaffold( + body: CreateChatDialog( + repository: ChatRepository(gateway), + operationId: 'op', + prompts: prompts, + onSubmit: (_, _) async => {'id': 'new'}, + ), + ), + ), + ); + if (settle) await tester.pumpAndSettle(); }