Files
pipelines/app/lib/features/chat/conversation/chat_command_actions.dart
T
alan ad4233f7b8 Chat: /new continues the same chat on a fresh session
The composer's /new used to create a separate chat. It now sends
chat_new_session. The server starts a fresh native session on the chat's
own harness (OpenCode, Codex or Claude Code) and re-keys the
operator_sessions row in one transaction. The chat keeps its title,
directory, backend, colour, star, model and mode. The old row is kept
unlisted with replaced_by, and its native session stays resumable.

The reply and the chat_session_changed push carry previous_id, so every
client swaps the row in place and an open conversation follows it to the
fresh, empty transcript with a notice.
2026-11-03 14:06:44 +00:00

134 lines
4.8 KiB
Dart

import '../../../core/api_gateway.dart';
import '../../../core/error_reason.dart';
import '../../../core/transport/connection_state.dart';
import '../data/chat_repository.dart';
import '../domain/chat_commands.dart';
/// Shows one local line in the conversation the command ran in.
typedef ChatCommandNoticeCallback = void Function(String text, {bool failed});
/// Hands the chat's row under its fresh native session id to the destination,
/// with the id it replaces and the line the fresh conversation opens on. The
/// destination owns navigation: it swaps the list row in place, opens the new
/// id and refreshes the list from the server.
typedef ChatNewSessionCallback =
void Function(String previousId, JsonMap session, String notice);
/// Runs the recorded commands that act on the open session: `/new`, `/close`
/// and `/rename <title>`.
///
/// Every outcome is a local notice, never a prompt. `/new` is the server's
/// `chat_new_session` (the same chat on a fresh context, as the harness's own
/// `/new` or `/clear`); `/close` and `/rename` are the Close chat confirm's and
/// Rename dialog's requests. `/stop` and `/help` are not run here; [runs] says
/// which commands are.
class ChatCommandActions {
ChatCommandActions({
required this.repository,
required this.sessionId,
required this.showNotice,
required this.confirmClose,
required this.startedNewSession,
this.onRenamed,
this.onClosed,
});
final ChatRepository repository;
final String sessionId;
final ChatCommandNoticeCallback showNotice;
/// Opens the row's `Close chat?` confirm; false sends nothing.
final Future<bool> Function() confirmClose;
final ChatNewSessionCallback startedNewSession;
/// The server accepted the shared rename with this trimmed title.
final void Function(String title)? onRenamed;
/// The server accepted the close; the conversation is finished here too.
final void Function()? onClosed;
/// Whether [command] is one this runner performs.
static bool runs(ChatCommand command) => const {
chatCommandNew,
chatCommandClose,
chatCommandRename,
}.contains(command.name);
/// Performs [command] with the draft's trimmed [argument]. The caller has
/// already consumed the draft; a usage error here is a failed notice and
/// sends nothing.
Future<void> run(ChatCommand command, String argument) async {
if (!command.takesArgument && argument.isNotEmpty) {
showNotice(chatCommandArgumentReport(command), failed: true);
return;
}
switch (command.name) {
case chatCommandNew:
await _startSession();
case chatCommandClose:
await _closeSession();
case chatCommandRename:
await _renameSession(argument);
}
}
/// `/new`: `chat_new_session` keeps this chat (row, title, colour, star,
/// directory, model and mode) and moves it to a fresh native session, so the
/// agent starts from an empty context. The reply's row, under its new id, is
/// handed to the destination with the notice the fresh conversation opens
/// on; a refusal (a running turn, say) stays in this conversation.
Future<void> _startSession() async {
try {
final reply = await repository.newSession(
sessionId,
instructions: chatDefaultSessionInstructions,
);
final session = reply['chat_session'];
final id = session is Map ? '${session['id'] ?? ''}'.trim() : '';
if (session is! Map || id.isEmpty || id == sessionId) {
throw const ProtocolError(
'The server did not return the new session for this chat.',
);
}
final row = Map<String, dynamic>.from(session)..remove('previous_id');
startedNewSession(sessionId, row, chatNewSessionNotice);
} catch (error) {
showNotice(chatNewSessionFailedNotice(_reason(error)), failed: true);
}
}
/// `/close`: the row's confirm, then the existing `chat_close`. Cancelling
/// sends nothing; a refusal is the server's words.
Future<void> _closeSession() async {
if (!await confirmClose()) return;
try {
await repository.close(sessionId);
onClosed?.call();
} catch (error) {
showNotice(_reason(error), failed: true);
}
}
/// `/rename <title>`: the same shared `chat_rename` the Rename dialog sends,
/// with the trimmed title. No title is a usage error and sends nothing.
Future<void> _renameSession(String title) async {
if (title.isEmpty) {
showNotice(chatRenameUsageReport, failed: true);
return;
}
try {
await repository.rename(sessionId, title);
onRenamed?.call(title);
showNotice(chatSessionRenamedNotice(title));
} catch (error) {
showNotice(_reason(error), failed: true);
}
}
/// The server's words without a Dart runtime prefix, as the web prints them.
String _reason(Object error) =>
describeErrorReason(error, fallback: 'The server gave no reason.');
}