Files
pipelines/app/lib/features/chat/domain/chat_commands.dart
T
alan 63babc597a feat(app): offer the chat slash command palette
A leading slash opens the recorded command palette above the composer,
filtered by name prefix and driven by Up/Down, Enter, tap and Escape.

Slash drafts never reach the prompt path: a recorded command reports
locally that its action is not implemented yet, and an unknown word takes
the web client's no-send report with an explicit Send as message offer.
Enter and the primary control share that route.

Adds the chat commands domain vocabulary, the palette widget, composer
wiring and regression tests; notes/C9-commands.md records behaviour and
the remaining command-action gap.
2026-10-22 15:47:46 +01:00

121 lines
4.3 KiB
Dart

/// One command the chat composer offers. [name] is the word after the slash,
/// [usage] is what the operator types (including any argument) and [summary] is
/// the few-word description shown beside it. [takesArgument] is false for the
/// commands that act on the current session alone.
///
/// The words and order are the web client's `CHAT_COMMANDS`, which the Android
/// client records too. They are a fixed client-side vocabulary, not server data.
class ChatCommand {
const ChatCommand({
required this.name,
required this.summary,
required this.usage,
this.takesArgument = false,
});
final String name;
final String summary;
final String usage;
final bool takesArgument;
}
const String chatCommandNew = 'new';
const String chatCommandClose = 'close';
const String chatCommandRename = 'rename';
const String chatCommandStop = 'stop';
const String chatCommandHelp = 'help';
/// The recorded command list, in the order the palette shows it.
const List<ChatCommand> chatCommands = <ChatCommand>[
ChatCommand(
name: chatCommandNew,
summary: 'Start a fresh session in this directory',
usage: '/new',
),
ChatCommand(
name: chatCommandClose,
summary: 'Finish this session for every client, keeping its history',
usage: '/close',
),
ChatCommand(
name: chatCommandRename,
summary: 'Rename this session on this device',
usage: '/rename <title>',
takesArgument: true,
),
ChatCommand(
name: chatCommandStop,
summary: 'Stop the turn that is running',
usage: '/stop',
),
ChatCommand(
name: chatCommandHelp,
summary: 'List every command',
usage: '/help',
),
];
final RegExp _whitespace = RegExp(r'\s');
/// The lower-case name prefix a draft offers commands for, or null when the
/// draft is not a single slash word.
///
/// Only the start of the draft counts. Once whitespace follows the word an
/// argument has begun, so the palette hides; text that does not start with a
/// slash offers nothing.
String? chatCommandQuery(String draft) {
final trimmed = draft.trimLeft();
if (!trimmed.startsWith('/')) return null;
if (_whitespace.hasMatch(trimmed)) return null;
return trimmed.substring(1).toLowerCase();
}
/// The recorded commands matching [draft], filtered case-insensitively by name
/// prefix. Empty when no palette should show.
List<ChatCommand> chatCommandSuggestions(String draft) {
final query = chatCommandQuery(draft);
if (query == null) return const <ChatCommand>[];
return <ChatCommand>[
for (final command in chatCommands)
if (command.name.toLowerCase().startsWith(query)) command,
];
}
/// The slash word [draft] starts with, including its `/`, or null when the
/// draft does not start with one. Only the first word counts, so an argument
/// (`/rename My chat`) still names `/rename`.
String? chatCommandWord(String draft) {
final trimmed = draft.trimLeft();
if (!trimmed.startsWith('/')) return null;
final boundary = _whitespace.firstMatch(trimmed);
return boundary == null ? trimmed : trimmed.substring(0, boundary.start);
}
/// The recorded command [draft] names, matched case-insensitively against the
/// whole slash word, or null when that word names no recorded command.
///
/// This is the routing question, not the palette filter: a completed command
/// (`/close`) and one carrying its argument (`/rename My chat`) both resolve.
ChatCommand? chatCommandNamed(String draft) {
final word = chatCommandWord(draft);
if (word == null) return null;
final name = word.substring(1).toLowerCase();
for (final command in chatCommands) {
if (command.name.toLowerCase() == name) return command;
}
return null;
}
/// 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.
String chatUnknownCommandReport(String word) =>
'“$word” is not a command, so nothing was sent to the agent. '
'Send it as a message, or cancel.';
/// The local no-send report for a recorded command whose action this client
/// does not run yet. Command actions are a later slice; until they land the
/// text must never become an implicit prompt.
String chatCommandUnavailableReport(String word) =>
'“$word” is not available yet, so nothing was sent to the agent.';