/// 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 chatCommands = [ 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', usage: '/rename ', 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 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. 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` session is created 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.'; /// The title every `/new` session is created with, as the web client sends it. const String chatNewSessionTitle = 'New chat'; /// `/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.'; /// `/new` needs the open session's directory; without one nothing is sent. const String chatNoDirectoryReport = "Can't start a new session here: the server reported no directory for " 'this chat.'; /// The notice the created session opens with. String chatSessionStartedNotice(String title, String directory) => 'Started a new session “$title” in $directory.'; /// The failed notice the current session shows when the create is refused. String chatSessionStartFailedNotice(String directory, String reason) => "Couldn't start a new session in $directory: $reason"; /// The notice after the server accepted the shared rename. String chatSessionRenamedNotice(String title) => 'Renamed this session to “$title”.';