Files
pipelines/app/lib/features/chat/domain/chat_commands.dart
T
alan 01040a7b12 Stop the replaced session on /new for every harness
/new already released the old native session through the chat-close
release path, but two gaps let the replaced session stay alive, and the
client told the operator it could still be resumed in a terminal:

- Claude Code: ReleaseSession only closed a host this server was
  attached to. A host registered for the session but not attached (a
  failed reattach, or one left by an earlier server) kept running. It is
  now asked to close over its socket and killed as a fallback.
- Codex: ReleaseSession only interrupted an active turn. It now also
  sends thread/unsubscribe so the App Server unloads the thread; a later
  prompt reloads it through the existing "thread not found" resume path.
- The /new notice now says the previous session was stopped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-21 18:10:41 +00:00

182 lines
6.9 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,
});
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 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. 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 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”.';