d1390994cc
The server reads the commands Claude Code's initialize response lists (bundled, user, project, plugin and claude.ai-synced skills plus CLI commands, with descriptions and argument hints), drops terminal-only ones, and serves them per chat over a new chat_commands request without waiting for a CLI start. The composer palette lists them after the recorded commands; choosing one inserts "/<name> " and sending passes it to the harness as an ordinary prompt. Codex, OpenCode and Pi chats reply with an empty list. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
229 lines
8.7 KiB
Dart
229 lines
8.7 KiB
Dart
import '../shell/chat_shell_command.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,
|
|
this.fromHarness = false,
|
|
});
|
|
|
|
/// A slash command or skill the session's harness reported (`chat_commands`).
|
|
/// The client never runs it: the draft `/<name> args` is sent as an ordinary
|
|
/// prompt and the harness expands it.
|
|
const ChatCommand.harness({
|
|
required this.name,
|
|
required this.summary,
|
|
required this.usage,
|
|
}) : takesArgument = true,
|
|
fromHarness = true;
|
|
|
|
final String name;
|
|
final String summary;
|
|
final String usage;
|
|
final bool takesArgument;
|
|
final bool fromHarness;
|
|
}
|
|
|
|
const String chatCommandNew = 'new';
|
|
const String chatCommandClose = 'close';
|
|
const String chatCommandRename = 'rename';
|
|
const String chatCommandStop = 'stop';
|
|
const String chatCommandStopTasks = 'stop_tasks';
|
|
const String chatCommandHelp = 'help';
|
|
|
|
/// The recorded command list, in the order the palette shows it.
|
|
const List<ChatCommand> chatCommands = <ChatCommand>[
|
|
ChatCommand(
|
|
name: chatCommandNew,
|
|
summary: 'Clear the context: continue this chat in a fresh session',
|
|
usage: '/new',
|
|
),
|
|
ChatCommand(
|
|
name: chatCommandClose,
|
|
summary: 'Finish this session for every client, keeping its history',
|
|
usage: '/close',
|
|
),
|
|
ChatCommand(
|
|
name: chatCommandRename,
|
|
summary: 'Rename this session',
|
|
usage: '/rename <title>',
|
|
takesArgument: true,
|
|
),
|
|
ChatCommand(
|
|
name: chatCommandStop,
|
|
summary: 'Stop the turn that is running',
|
|
usage: '/stop',
|
|
),
|
|
ChatCommand(
|
|
name: chatCommandStopTasks,
|
|
summary: 'Stop all background tasks in this chat',
|
|
usage: '/stop_tasks',
|
|
),
|
|
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, then the session's [harness] commands (skills) matching it. A
|
|
/// harness command named like a recorded one is shadowed: the client's own
|
|
/// command runs. A namespaced harness name (`plugin:skill`) also matches on
|
|
/// the part after its colon. Empty when no palette should show.
|
|
List<ChatCommand> chatCommandSuggestions(
|
|
String draft, [
|
|
List<ChatCommand> harness = const <ChatCommand>[],
|
|
]) {
|
|
final query = chatCommandQuery(draft);
|
|
if (query == null) return const <ChatCommand>[];
|
|
return <ChatCommand>[
|
|
for (final command in chatCommands)
|
|
if (command.name.toLowerCase().startsWith(query)) command,
|
|
for (final command in harness)
|
|
if (!_isRecorded(command.name) && _harnessMatches(command.name, query))
|
|
command,
|
|
];
|
|
}
|
|
|
|
bool _isRecorded(String name) {
|
|
final lower = name.toLowerCase();
|
|
return chatCommands.any((command) => command.name == lower);
|
|
}
|
|
|
|
bool _harnessMatches(String name, String query) {
|
|
final lower = name.toLowerCase();
|
|
if (lower.startsWith(query)) return true;
|
|
final colon = lower.lastIndexOf(':');
|
|
return colon >= 0 && lower.substring(colon + 1).startsWith(query);
|
|
}
|
|
|
|
/// The session [harness] command [draft]'s slash word names exactly
|
|
/// (case-insensitive), or null. A recorded command of the same name wins, so
|
|
/// this is asked only after [chatCommandNamed] found none.
|
|
ChatCommand? chatHarnessCommandNamed(String draft, List<ChatCommand> harness) {
|
|
final word = chatCommandWord(draft);
|
|
if (word == null) return null;
|
|
final name = word.substring(1).toLowerCase();
|
|
if (_isRecorded(name)) return null;
|
|
for (final command in harness) {
|
|
if (command.name.toLowerCase() == name) return command;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/// 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 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,
|
|
/// then the `!` shell-command hint.
|
|
String chatCommandHelpText() => [
|
|
for (final command in chatCommands) '${command.usage} — ${command.summary}',
|
|
chatShellHelpLine,
|
|
].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.
|
|
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.';
|
|
|
|
/// The behaviour brief a new chat, and a `/new` fresh session, starts with,
|
|
/// verbatim from the web client's `DEFAULT_CHAT_INSTRUCTIONS`.
|
|
const String chatDefaultSessionInstructions =
|
|
"Act as a careful coding agent. Read the repository's AGENTS.md and the "
|
|
'relevant skills before acting. Inspect the current state, keep changes '
|
|
'scoped to the request, run the relevant checks, and report exactly what '
|
|
'changed and what was not verified. Ask only about real ambiguity, '
|
|
'destructive actions or changes outside the request.';
|
|
|
|
/// `/new` and `/close` act on the open session alone, so an argument is a
|
|
/// usage error the web reports in these exact words.
|
|
String chatCommandArgumentReport(ChatCommand command) =>
|
|
'${command.usage} does not take an argument.';
|
|
|
|
/// `/rename` without a title sends nothing and reports its usage.
|
|
const String chatRenameUsageReport =
|
|
'Usage: /rename <title> — renames this session.';
|
|
|
|
/// The line the fresh conversation opens on after `/new`: the chat is the
|
|
/// same, the agent's context is empty, and the server has already stopped the
|
|
/// replaced session's agent and background work before it answered.
|
|
const String chatNewSessionNotice =
|
|
'New session started. The agent no longer sees the earlier conversation; '
|
|
'the previous session was stopped.';
|
|
|
|
/// The line a conversation opens on when another device started its new
|
|
/// session.
|
|
const String chatNewSessionElsewhereNotice =
|
|
'Another device started a new session in this chat. The agent no longer '
|
|
'sees the earlier conversation.';
|
|
|
|
/// The failed notice the current session shows when `/new` is refused.
|
|
String chatNewSessionFailedNotice(String reason) =>
|
|
"Couldn't start a new session: $reason";
|
|
|
|
/// The notice after the server accepted the shared rename.
|
|
String chatSessionRenamedNotice(String title) =>
|
|
'Renamed this session to “$title”.';
|