feat(app): chat create dialog matches the web create flow

This commit is contained in:
2026-10-22 22:34:46 +01:00
parent 7b48df810c
commit 865616608c
6 changed files with 183 additions and 135 deletions
@@ -3,6 +3,10 @@ import 'package:flutter/material.dart';
import 'chat_adaptive.dart';
/// The create flow's heading and confirm, verbatim from the web client.
const String chatCreateTitle = 'Start a chat';
const String chatCreateConfirmLabel = 'New chat';
/// Native chrome for the create dialog: standard Cupertino alert or Material
/// alert, with the same disabled/submitting state in both hosts.
class ChatCreateDialogFrame extends StatelessWidget {
@@ -24,10 +28,10 @@ class ChatCreateDialogFrame extends StatelessWidget {
@override
Widget build(BuildContext context) {
final enabled = canSubmit && !submitting;
final label = submitting ? 'Creating…' : 'Create';
final label = submitting ? 'Creating…' : chatCreateConfirmLabel;
if (chatControlsStyleOf(context) == ChatControlsStyle.cupertino) {
return CupertinoAlertDialog(
title: const Text('New chat'),
title: const Text(chatCreateTitle),
content: content,
actions: [
CupertinoDialogAction(
@@ -35,6 +39,7 @@ class ChatCreateDialogFrame extends StatelessWidget {
child: const Text('Cancel'),
),
CupertinoDialogAction(
key: const Key('chat-create-confirm'),
isDefaultAction: true,
onPressed: enabled ? onSubmit : null,
child: Text(label),
@@ -43,14 +48,18 @@ class ChatCreateDialogFrame extends StatelessWidget {
);
}
return AlertDialog(
title: const Text('New chat'),
title: const Text(chatCreateTitle),
content: content,
actions: [
TextButton(
onPressed: submitting ? null : onCancel,
child: const Text('Cancel'),
),
FilledButton(onPressed: enabled ? onSubmit : null, child: Text(label)),
FilledButton(
key: const Key('chat-create-confirm'),
onPressed: enabled ? onSubmit : null,
child: Text(label),
),
],
);
}
@@ -3,8 +3,40 @@ import 'package:flutter/material.dart';
import 'chat_adaptive.dart';
/// The web's directory prompt, also the directory picker's label.
const String chatCreatePrompt = 'Where should this chat live?';
/// The home choice: shown as `Home (~)` and sent as the `~` the server
/// documents.
const String chatHomeDirectory = '~';
const String chatHomeLabel = 'Home ($chatHomeDirectory)';
/// The web's directory choices: home first (the server's first directory is
/// its path), then the remaining server directories as sent.
List<String> chatDirectoryChoices(List<String> directories) => [
chatHomeDirectory,
...directories.skip(1),
];
/// The visible label of a directory choice; home names its resolved path.
String chatDirectoryLabel(String choice, List<String> directories) {
if (choice != chatHomeDirectory) return choice;
final home = directories.isEmpty ? chatHomeDirectory : directories.first;
return '$chatHomeLabel — $home';
}
/// The web's names for the chat harnesses; an unknown one shows as sent.
String chatBackendLabel(String backend) => switch (backend) {
'codex' => 'Codex',
'claude' => 'Claude',
'pi' => 'Pi',
'opencode' => 'OpenCode',
_ => backend,
};
/// The create dialog's server catalog area: loading, error with retry, the
/// empty-catalog message, or the two server-backed selectors.
/// directory choices and, only when the server offers more than one, the
/// harness choice.
class ChatCatalogFields extends StatelessWidget {
const ChatCatalogFields({
super.key,
@@ -37,43 +69,49 @@ class ChatCatalogFields extends StatelessWidget {
if (error != null) {
return _message(cupertino, 'Unable to load chat options: $error');
}
if (directories.isEmpty || backends.isEmpty) {
return _message(
cupertino,
'The server has no chat directories or backends available.',
);
}
final choices = chatDirectoryChoices(directories);
String directoryLabel(String choice) =>
chatDirectoryLabel(choice, directories);
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (cupertino)
CupertinoPickerField(
label: 'Server directory',
fieldKey: const Key('chat-create-server-directory'),
label: chatCreatePrompt,
value: directory,
values: directories,
values: choices,
labelOf: directoryLabel,
onSelected: onDirectory,
)
else
MaterialPickerField(
label: 'Server directory',
fieldKey: const Key('chat-create-server-directory'),
label: chatCreatePrompt,
value: directory,
values: directories,
values: choices,
labelOf: directoryLabel,
onSelected: onDirectory,
),
if (cupertino)
CupertinoPickerField(
label: 'Backend',
value: backend,
values: backends,
onSelected: onBackend,
)
else
MaterialPickerField(
label: 'Backend',
value: backend,
values: backends,
onSelected: onBackend,
),
if (backends.length > 1)
if (cupertino)
CupertinoPickerField(
fieldKey: const Key('chat-create-backend'),
label: 'Chat harness',
value: backend,
values: backends,
labelOf: chatBackendLabel,
onSelected: onBackend,
)
else
MaterialPickerField(
fieldKey: const Key('chat-create-backend'),
label: 'Chat harness',
value: backend,
values: backends,
labelOf: chatBackendLabel,
onSelected: onBackend,
),
],
);
}
@@ -101,21 +139,29 @@ class ChatCatalogFields extends StatelessWidget {
class MaterialPickerField extends StatelessWidget {
const MaterialPickerField({
super.key,
required this.fieldKey,
required this.label,
required this.value,
required this.values,
required this.onSelected,
this.labelOf,
});
final String label;
final String? value;
final List<String> values;
final ValueChanged<String> onSelected;
final Key fieldKey;
/// The visible text of a value; the value itself is what is sent.
final String Function(String value)? labelOf;
String _text(String value) => labelOf?.call(value) ?? value;
@override
Widget build(BuildContext context) {
return DropdownButtonFormField<String>(
key: Key('chat-create-${label.toLowerCase().replaceAll(' ', '-')}'),
key: fieldKey,
initialValue: value,
isExpanded: true,
// Intrinsic item height: a 2x label must not be clipped by the 48px
@@ -136,23 +182,31 @@ class MaterialPickerField extends StatelessWidget {
}
Widget _fieldLabel(String item) {
return Text(item, maxLines: 1, overflow: TextOverflow.ellipsis);
return Text(_text(item), maxLines: 1, overflow: TextOverflow.ellipsis);
}
}
class CupertinoPickerField extends StatelessWidget {
const CupertinoPickerField({
super.key,
required this.fieldKey,
required this.label,
required this.value,
required this.values,
required this.onSelected,
this.labelOf,
});
final String label;
final String? value;
final List<String> values;
final ValueChanged<String> onSelected;
final Key fieldKey;
/// The visible text of a value; the value itself is what is sent.
final String Function(String value)? labelOf;
String _text(String value) => labelOf?.call(value) ?? value;
Future<void> _choose(BuildContext context) async {
final choice = await showCupertinoModalPopup<String>(
@@ -163,7 +217,7 @@ class CupertinoPickerField extends StatelessWidget {
for (final item in values)
CupertinoActionSheetAction(
onPressed: () => Navigator.pop(sheetContext, item),
child: Text(item),
child: Text(_text(item)),
),
],
cancelButton: CupertinoActionSheetAction(
@@ -178,15 +232,17 @@ class CupertinoPickerField extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Semantics(
label: '$label: ${value ?? ''}',
label: '$label: ${value == null ? '' : _text(value!)}',
button: true,
child: CupertinoButton(
key: Key('chat-create-${label.toLowerCase().replaceAll(' ', '-')}'),
key: fieldKey,
padding: const EdgeInsets.symmetric(vertical: 4),
onPressed: () => _choose(context),
child: Row(
children: [
Expanded(child: Text('$label: ${value ?? ''}')),
Expanded(
child: Text('$label: ${value == null ? '' : _text(value!)}'),
),
const Icon(CupertinoIcons.chevron_down),
],
),
@@ -3,10 +3,19 @@ import 'package:flutter/material.dart';
import '../../../../core/api_gateway.dart';
import '../../data/chat_repository.dart';
import '../../domain/chat_commands.dart';
import 'chat_adaptive.dart';
import 'chat_create_dialog_frame.dart';
import 'chat_create_fields.dart';
/// The instructions field's helper and the model note, verbatim from the web.
const String chatCreateInstructionsHelper =
"Preloaded as this chat's session instructions. The agent still reads the "
"repository's AGENTS.md and skills.";
const String chatCreateModelNote =
"This server has no request for choosing a chat's model, so a new chat "
"uses the server's default.";
class ChatDraft {
const ChatDraft({
required this.title,
@@ -58,8 +67,9 @@ class CreateChatDialog extends StatefulWidget {
}
class _CreateChatDialogState extends State<CreateChatDialog> {
final _name = TextEditingController(text: 'New chat');
final _instructions = TextEditingController();
final _instructions = TextEditingController(
text: chatDefaultSessionInstructions,
);
List<String> _directories = const [];
List<String> _backends = const [];
String? _directory;
@@ -79,7 +89,6 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
@override
void dispose() {
_attempt++;
_name.dispose();
_instructions.dispose();
super.dispose();
}
@@ -99,15 +108,13 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
setState(() {
_directories = values[0];
_backends = values[1];
_directory = _directories.contains(_directory)
_directory = chatDirectoryChoices(_directories).contains(_directory)
? _directory
: _directories.isEmpty
? null
: _directories.first;
: chatHomeDirectory;
_backend = _backends.contains(_backend)
? _backend
: _backends.isEmpty
? null
: _backends.contains('opencode')
? 'opencode'
: _backends.first;
_loading = false;
});
@@ -120,20 +127,11 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
}
}
bool get _catalogReady =>
!_loading &&
_catalogError == null &&
_directories.isNotEmpty &&
_backends.isNotEmpty;
bool get _catalogReady => !_loading && _catalogError == null;
Future<void> _submit() async {
if (!_catalogReady || _submitting) return;
final title = _name.text.trim();
final directory = _directory ?? '';
if (title.isEmpty || directory.isEmpty) {
setState(() => _formError = 'Enter a name and server directory.');
return;
}
final directory = _directory ?? chatHomeDirectory;
setState(() {
_submitting = true;
_formError = null;
@@ -141,7 +139,7 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
try {
final session = await widget.onSubmit(
ChatDraft(
title: title,
title: chatNewSessionTitle,
directory: directory,
backend: _backend ?? '',
instructions: _instructions.text,
@@ -168,19 +166,6 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
if (cupertino)
CupertinoTextField(
key: const Key('chat-create-name'),
controller: _name,
placeholder: 'Name',
)
else
TextField(
key: const Key('chat-create-name'),
controller: _name,
decoration: const InputDecoration(labelText: 'Name'),
),
const SizedBox(height: 12),
ChatCatalogFields(
loading: _loading,
error: _catalogError,
@@ -192,27 +177,34 @@ class _CreateChatDialogState extends State<CreateChatDialog> {
onBackend: (value) => setState(() => _backend = value),
onRetry: _loadCatalog,
),
if (_catalogReady) ...[
const SizedBox(height: 8),
const Text('New chats use the server default model.'),
],
const SizedBox(height: 12),
const Text('How should it act?'),
const SizedBox(height: 4),
if (cupertino)
CupertinoTextField(
key: const Key('chat-create-instructions'),
controller: _instructions,
placeholder: 'Instructions',
minLines: 1,
maxLines: 3,
minLines: 2,
maxLines: 4,
)
else
TextField(
key: const Key('chat-create-instructions'),
controller: _instructions,
decoration: const InputDecoration(labelText: 'Instructions'),
minLines: 1,
maxLines: 3,
minLines: 2,
maxLines: 4,
),
const SizedBox(height: 4),
const Text(chatCreateInstructionsHelper),
if (_catalogReady) ...[
const SizedBox(height: 8),
const Text(chatCreateModelNote),
const SizedBox(height: 8),
Text(
'Start in ${_directory ?? chatHomeDirectory}',
key: const Key('chat-create-start-in'),
),
],
if (_formError != null) ...[
const SizedBox(height: 8),
Text(
@@ -26,10 +26,6 @@ void main() {
await tester.pump(const Duration(milliseconds: 400));
expect(find.text('Loading directories…'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('chat-create-name')),
'Draft chat',
);
await tester.enterText(
find.byKey(const Key('chat-create-instructions')),
'Keep me',
@@ -44,7 +40,6 @@ void main() {
find.textContaining('Unable to load chat options'),
findsOneWidget,
);
expect(find.text('Draft chat'), findsOneWidget);
expect(find.text('Keep me'), findsOneWidget);
gateway.directoriesHandler = () async => {
@@ -58,27 +53,21 @@ void main() {
findsOneWidget,
);
expect(
find.text('New chats use the server default model.'),
find.text(
"This server has no request for choosing a chat's model, so a new "
"chat uses the server's default.",
),
findsOneWidget,
);
expect(find.text('Start in ~'), findsOneWidget);
await tester.enterText(find.byKey(const Key('chat-create-name')), '');
await tester.tap(find.text('Create'));
await tester.pump();
expect(find.text('Enter a name and server directory.'), findsOneWidget);
expect(gateway.count('chat_create'), 0);
await tester.enterText(
find.byKey(const Key('chat-create-name')),
'Draft chat',
);
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
final fields =
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
expect(fields['title'], 'Draft chat');
expect(fields['directory'], '/work');
expect(fields['title'], 'New chat');
expect(fields['directory'], '~');
expect(fields.containsKey('backend'), isFalse);
expect(fields['instructions'], 'Keep me');
expect(fields.containsKey('operation_id'), isTrue);
@@ -98,15 +87,10 @@ void main() {
await tester.pumpAndSettle();
expect(find.textContaining('Unable to load chat options'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('chat-create-name')),
'Cupertino draft',
);
await tester.enterText(
find.byKey(const Key('chat-create-instructions')),
'Keep me too',
);
expect(find.text('Cupertino draft'), findsOneWidget);
expect(find.text('Keep me too'), findsOneWidget);
gateway.directoriesHandler = () async => {
@@ -115,11 +99,10 @@ void main() {
await tester.tap(find.text('Retry'));
await tester.pumpAndSettle();
expect(find.textContaining('Unable to load chat options'), findsNothing);
expect(find.text('Cupertino draft'), findsOneWidget);
expect(find.text('Keep me too'), findsOneWidget);
});
testWidgets('material create dialog reports an empty catalog', (
testWidgets('an empty directory list still offers home and sends ~', (
tester,
) async {
final gateway = FakeChatGateway();
@@ -128,10 +111,12 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat-fab')));
await tester.pumpAndSettle();
expect(
find.text('The server has no chat directories or backends available.'),
findsOneWidget,
);
expect(find.text('Home (~) — ~'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
final fields =
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
expect(fields['directory'], '~');
});
testWidgets('Cupertino create dialog uses native selectors and fields', (
@@ -148,36 +133,43 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat')));
await tester.pumpAndSettle();
expect(find.byType(CupertinoAlertDialog), findsOneWidget);
expect(find.text('Server directory: /work'), findsOneWidget);
expect(find.text('Backend: opencode'), findsOneWidget);
expect(find.text('Start a chat'), findsOneWidget);
expect(
find.text('Where should this chat live?: Home (~) — /work'),
findsOneWidget,
);
expect(find.text('Chat harness: OpenCode'), findsOneWidget);
expect(
find.textContaining('Act as a careful coding agent.'),
findsOneWidget,
);
await tester.tap(find.byKey(const Key('chat-create-backend')));
await tester.pumpAndSettle();
await tester.tap(find.text('codex'));
await tester.tap(find.text('Codex'));
await tester.pumpAndSettle();
expect(find.text('Backend: codex'), findsOneWidget);
expect(find.text('Chat harness: Codex'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-create-server-directory')));
await tester.pumpAndSettle();
expect(find.text('/archive'), findsOneWidget);
await tester.tap(find.text('/archive'));
await tester.pumpAndSettle();
expect(find.text('Server directory: /archive'), findsOneWidget);
expect(find.text('Where should this chat live?: /archive'), findsOneWidget);
expect(find.text('Start in /archive'), findsOneWidget);
await tester.enterText(
find.byKey(const Key('chat-create-name')),
'Cupertino draft',
);
await tester.enterText(
find.byKey(const Key('chat-create-instructions')),
'Be careful',
);
await tester.tap(find.text('Create'));
await tester.ensureVisible(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
final fields =
gateway.lastFields('chat_create')!['chat_create']! as JsonMap;
expect(fields['title'], 'Cupertino draft');
expect(fields['title'], 'New chat');
expect(fields['directory'], '/archive');
expect(fields['backend'], 'codex');
expect(fields['instructions'], 'Be careful');
@@ -199,7 +191,7 @@ void main() {
gateway.pendingSessions.add(refresh);
await tester.tap(find.byKey(const Key('chat-new-chat-fab')));
await tester.pumpAndSettle();
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pump();
await tester.pump(const Duration(milliseconds: 300));
@@ -78,7 +78,7 @@ void main() {
);
await tester.pumpAndSettle();
expect(tester.takeException(), isNull);
expect(find.byKey(const Key('chat-create-name')), findsOneWidget);
expect(find.byKey(const Key('chat-create-instructions')), findsOneWidget);
});
testWidgets('Cupertino create catalog fits real paths at 320px and 2x', (
@@ -91,7 +91,10 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat')));
await tester.pumpAndSettle();
expect(find.textContaining('Server directory:'), findsOneWidget);
expect(
find.textContaining('Where should this chat live?:'),
findsOneWidget,
);
expect(tester.takeException(), isNull);
});
}
@@ -32,26 +32,22 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat-fab')));
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-create-name')),
'Probe chat',
);
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
expect(find.byKey(const Key('chat-create-name')), findsOneWidget);
expect(find.byKey(const Key('chat-create-instructions')), findsOneWidget);
expect(
find.text('Bad state: no chat harness is available on this server'),
findsOneWidget,
);
expect(opened, isNull);
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
expect(bodies, hasLength(2));
expect(bodies[1]['operation_id'], bodies[0]['operation_id']);
expect(bodies[1]['title'], 'Probe chat');
expect(find.byKey(const Key('chat-create-name')), findsNothing);
expect(bodies[1]['title'], 'New chat');
expect(find.byKey(const Key('chat-create-instructions')), findsNothing);
expect(opened?['id'], 'created');
expect(gateway.count('chat_sessions'), greaterThanOrEqualTo(2));
});
@@ -66,7 +62,7 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat-fab')));
await tester.pumpAndSettle();
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pump();
await tester.tap(find.text('Creating…'), warnIfMissed: false);
await tester.pump();
@@ -189,7 +185,7 @@ void main() {
await tester.tap(find.byKey(const Key('chat-new-chat-fab')));
await tester.pumpAndSettle();
await tester.tap(find.text('Create'));
await tester.tap(find.byKey(const Key('chat-create-confirm')));
await tester.pumpAndSettle();
expect(
@@ -197,7 +193,7 @@ void main() {
findsOneWidget,
);
expect(find.textContaining('Bad state'), findsNothing);
expect(find.byKey(const Key('chat-create-name')), findsOneWidget);
expect(find.byKey(const Key('chat-create-instructions')), findsOneWidget);
});
testWidgets('rename refusal stays in the dialog, not on the row', (