159 lines
5.3 KiB
Dart
159 lines
5.3 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 a created session row to the destination with the line the new
|
|
/// conversation opens on. The destination owns navigation: it lists the row,
|
|
/// opens it and refreshes the list from the server.
|
|
typedef ChatCreatedSessionCallback =
|
|
void Function(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. The requests are the
|
|
/// repository's existing create, close and rename calls, exactly as the list's
|
|
/// New chat dialog, Close chat confirm and Rename dialog send them. `/stop` and
|
|
/// `/help` are not run here; [runs] says which commands are.
|
|
class ChatCommandActions {
|
|
ChatCommandActions({
|
|
required this.repository,
|
|
required this.sessionId,
|
|
required this.directory,
|
|
required this.showNotice,
|
|
required this.confirmClose,
|
|
required this.openCreatedSession,
|
|
this.onRenamed,
|
|
this.onClosed,
|
|
this.newOperationId,
|
|
});
|
|
|
|
final ChatRepository repository;
|
|
final String sessionId;
|
|
|
|
/// The open session's recorded directory, blank when the row is unknown.
|
|
/// `/new` starts the created session there.
|
|
final String directory;
|
|
|
|
final ChatCommandNoticeCallback showNotice;
|
|
|
|
/// Opens the row's `Close chat?` confirm; false sends nothing.
|
|
final Future<bool> Function() confirmClose;
|
|
|
|
final ChatCreatedSessionCallback openCreatedSession;
|
|
|
|
/// 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;
|
|
|
|
/// Test/fixture seam; production mints one id per create intent.
|
|
final String Function()? newOperationId;
|
|
|
|
/// 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`: the existing `chat_create` with the web's fixed title and brief,
|
|
/// in this session's directory, with no backend or model field. The reply's
|
|
/// row is handed to the destination with the notice the new conversation
|
|
/// opens on; a refusal stays in this conversation.
|
|
Future<void> _startSession() async {
|
|
if (directory.isEmpty) {
|
|
showNotice(chatNoDirectoryReport, failed: true);
|
|
return;
|
|
}
|
|
try {
|
|
final reply = await repository.create(
|
|
title: chatNewSessionTitle,
|
|
directory: directory,
|
|
operationId: _operationId(),
|
|
instructions: chatDefaultSessionInstructions,
|
|
);
|
|
final session = reply['chat_session'];
|
|
if (session is! Map) {
|
|
throw const ProtocolError(
|
|
'The server did not return the new chat session.',
|
|
);
|
|
}
|
|
final row = Map<String, dynamic>.from(session);
|
|
final title = '${row['title'] ?? ''}'.trim();
|
|
openCreatedSession(
|
|
row,
|
|
chatSessionStartedNotice(
|
|
title.isEmpty ? chatNewSessionTitle : title,
|
|
directory,
|
|
),
|
|
);
|
|
} catch (error) {
|
|
showNotice(
|
|
chatSessionStartFailedNotice(directory, _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);
|
|
}
|
|
}
|
|
|
|
String _operationId() =>
|
|
newOperationId?.call() ??
|
|
'native-chat-${DateTime.now().microsecondsSinceEpoch}';
|
|
|
|
/// 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.');
|
|
}
|