merge(app): land chat-cmd-stop

This commit is contained in:
2026-10-22 22:24:04 +01:00
3 changed files with 186 additions and 35 deletions
@@ -27,6 +27,9 @@ const String chatAttachLabel = 'Attach file';
/// The unknown-command report's two actions, verbatim from the web client.
const String chatCommandSendAsMessageLabel = 'Send as message';
const String chatCommandCancelLabel = 'Cancel';
const String chatCommandNoticeLabel = 'Command';
const String chatCommandFailedLabel = 'Command failed';
const String chatCommandDismissLabel = 'Dismiss command notice';
/// Shown while the reply says this prompt waits behind the running turn.
const String chatQueuedNotice =
@@ -42,7 +45,7 @@ const String chatQueuedNotice =
/// above the field: rows are filtered by name prefix, Up/Down move the
/// highlight, Enter or a tap completes the highlighted command, Escape closes
/// the palette. A slash draft is never handed to the prompt path: a completed
/// command is not run yet (command actions are a later slice), and text naming
/// recorded commands run locally or through the existing Stop path; text naming
/// no recorded command becomes the web client's local no-send report with its
/// explicit "Send as message" offer. Picked files are memory-only chips:
/// their names are shown with the host's own remove action and the bounded
@@ -100,6 +103,7 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
/// command carries [_commandOffer] back for an explicit "Send as message".
String? _commandNotice;
String? _commandOffer;
bool _commandFailed = false;
ChatAttachmentPicker get _picker => widget.picker ?? hostChatAttachmentPicker;
ChatClipboardPaste get _paste => widget.paste ?? hostChatClipboardPaste;
@@ -259,25 +263,39 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
return KeyEventResult.handled;
}
/// Routes the current slash draft to the command surface. Command actions
/// are not implemented in this slice, so a recorded command reports that it
/// is unavailable and keeps its draft; an unknown word takes the web client's
/// report and consumes the draft into the offer. Neither posts a prompt.
/// Routes a slash draft locally or through the existing Stop controller path.
void _runSlashDraft() {
final draft = widget.composer.draft;
final word = chatCommandWord(draft);
if (word == null) return;
final command = chatCommandNamed(draft);
if (command != null) {
setState(() {
_commandOffer = null;
_commandNotice = chatCommandUnavailableReport('/${command.name}');
});
_focus.requestFocus();
final argument = chatCommandArgument(draft);
if (!command.takesArgument && argument.isNotEmpty) {
_consumeCommandDraft();
_showCommandNotice(
'${command.usage} does not take an argument.',
failed: true,
);
} else if (command.name == chatCommandStop) {
_consumeCommandDraft();
if (widget.running) {
unawaited(widget.composer.stop());
} else {
_showCommandNotice('No turn is running in this chat.', failed: true);
}
} else if (command.name == chatCommandHelp) {
_consumeCommandDraft();
_showCommandNotice(chatCommandHelpText());
} else {
_consumeCommandDraft();
_showCommandNotice(chatCommandUnavailableReport('/${command.name}'));
}
return;
}
final offer = draft.trim();
setState(() {
_commandFailed = true;
_commandOffer = offer;
_commandNotice = chatUnknownCommandReport(word);
});
@@ -286,11 +304,26 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
_focus.requestFocus();
}
void _consumeCommandDraft() {
_text.clear();
unawaited(widget.composer.setDraft(''));
_focus.requestFocus();
}
void _showCommandNotice(String text, {bool failed = false}) {
setState(() {
_commandFailed = failed;
_commandOffer = null;
_commandNotice = text;
});
}
/// Closes whatever local command report is showing. Callers wrap this in
/// [setState]; it owns no rebuild of its own.
void _dismissCommandReport() {
_commandNotice = null;
_commandOffer = null;
_commandFailed = false;
}
/// The offer's one explicit route to the agent: take the reported draft back
@@ -716,12 +749,48 @@ class _ChatComposerBarState extends State<ChatComposerBar> {
key: const Key('chat-composer-command-notice'),
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Row(
children: [
Expanded(
child: Text(
_commandFailed
? chatCommandFailedLabel
: chatCommandNoticeLabel,
style: TextStyle(
color: _commandFailed
? (cupertino
? CupertinoColors.systemRed.resolveFrom(context)
: Theme.of(context).colorScheme.error)
: null,
),
),
),
if (cupertino)
Semantics(
button: true,
label: chatCommandDismissLabel,
child: CupertinoButton(
key: const Key('chat-command-dismiss'),
padding: EdgeInsets.zero,
onPressed: () => setState(_dismissCommandReport),
child: const Text('Dismiss'),
),
)
else
IconButton(
key: const Key('chat-command-dismiss'),
tooltip: chatCommandDismissLabel,
onPressed: () => setState(_dismissCommandReport),
icon: const Icon(Icons.close),
),
],
),
_notice(
context,
cupertino,
key: const Key('chat-composer-command-text'),
text: _commandNotice!,
failed: true,
failed: _commandFailed,
),
if (offer != null)
Padding(
@@ -106,6 +106,18 @@ ChatCommand? chatCommandNamed(String draft) {
return null;
}
/// The trimmed text after the command word, or empty when there is no argument.
String chatCommandArgument(String draft) {
final word = chatCommandWord(draft);
if (word == null) return '';
return draft.trimLeft().substring(word.length).trim();
}
/// `/help`'s local listing, in the same order and wording as the palette.
String chatCommandHelpText() => chatCommands
.map((command) => '${command.usage} — ${command.summary}')
.join('\n');
/// The local no-send report for a slash draft naming no recorded command,
/// verbatim from the web client's unknown-command notice. The report offers the
/// draft back, so only an explicit "Send as message" reaches the agent.
@@ -200,7 +200,7 @@ void main() {
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
expect(fieldText(tester), '');
expect(
find.text(
'“/close” is not available yet, so nothing was sent to the agent.',
@@ -208,28 +208,100 @@ void main() {
findsOneWidget,
);
// The primary control and the explicit send shortcut take the same path.
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
await tester.sendKeyDownEvent(LogicalKeyboardKey.controlLeft);
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.sendKeyUpEvent(LogicalKeyboardKey.controlLeft);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close');
// An argument does not make the slash draft sendable either.
// An argument is validated locally, then consumed too.
await tester.enterText(field, '/close now');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/close now');
expect(fieldText(tester), '');
expect(find.text('/close does not take an argument.'), findsOneWidget);
});
testWidgets('/help consumes the draft and lists commands in recorded order', (
tester,
) async {
await pumpComposer(tester);
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/help',
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty);
expect(
find.text(
'/new — Start a fresh session in this directory\n'
'/close — Finish this session for every client, keeping its history\n'
'/rename <title> — Rename this session on this device\n'
'/stop — Stop the turn that is running\n'
'/help — List every command',
),
findsOneWidget,
);
expect(find.text('Command'), findsOneWidget);
await tester.tap(find.byKey(const Key('chat-command-dismiss')));
await tester.pump();
expect(find.byKey(const Key('chat-composer-command-notice')), findsNothing);
});
testWidgets('/stop without a running turn and extra arguments fail locally', (
tester,
) async {
await pumpComposer(tester);
final field = find.byKey(const Key('chat-composer-field'));
await tester.enterText(field, '/stop');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(gateway.interruptBodies, isEmpty);
expect(find.text('Command failed'), findsOneWidget);
expect(find.text('No turn is running in this chat.'), findsOneWidget);
await tester.enterText(field, '/help x');
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(fieldText(tester), '');
expect(find.text('/help does not take an argument.'), findsOneWidget);
expect(gateway.promptBodies, isEmpty);
});
testWidgets('/stop interrupts the current session through the composer', (
tester,
) async {
await pumpComposer(tester);
gateway.emitEvent({
'session_id': 's',
'kind': 'step',
'step': {'status': 'started', 'turn_id': 'turn', 'intermediate': false},
});
await tester.pumpAndSettle();
await tester.enterText(
find.byKey(const Key('chat-composer-field')),
'/stop',
);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pump();
await tester.sendKeyEvent(LogicalKeyboardKey.enter);
await tester.pumpAndSettle();
expect(gateway.interruptBodies, [
<String, dynamic>{'session_id': 's'},
]);
expect(fieldText(tester), '');
expect(gateway.promptBodies, isEmpty);
});
testWidgets('a command picked from the palette is never sent either', (
@@ -244,17 +316,15 @@ void main() {
await tester.pumpAndSettle();
expect(fieldText(tester), '/help');
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
await tester.tap(find.byKey(const Key('chat-composer-primary')));
await tester.pumpAndSettle();
expect(gateway.promptBodies, isEmpty);
expect(fieldText(tester), '/help');
expect(
find.text(
'“/help” is not available yet, so nothing was sent to the agent.',
),
findsOneWidget,
);
expect(fieldText(tester), '');
expect(find.text('Command'), findsOneWidget);
expect(find.textContaining('/help — List every command'), findsOneWidget);
});
testWidgets('a row completes the command and never prompts', (tester) async {