a6f0f0a1cb
A draft starting with ! shows a Shell mode indicator and runs through chat_shell_run instead of being sent to the agent; the chat's actions menu gains Run command. Each run is a terminal-style card among the turns with live output, Stop, its exit status, Copy output and Send to agent.
93 lines
3.2 KiB
Dart
93 lines
3.2 KiB
Dart
import '../../../core/api_gateway.dart';
|
||
import '../domain/chat_transcript.dart';
|
||
import 'chat_shell_run.dart';
|
||
|
||
/// The composer's shell prefix: a draft starting with it runs as a command in
|
||
/// the chat's directory instead of being sent to the agent.
|
||
const String chatShellPrefix = '!';
|
||
|
||
/// The `/help` line for shell mode.
|
||
const String chatShellHelpLine =
|
||
'!<command> — Run a shell command in this chat’s folder; '
|
||
'the agent does not see it unless you send it';
|
||
|
||
/// Shell mode's visible label and its explanation beside the composer.
|
||
const String chatShellModeLabel = 'Shell';
|
||
const String chatShellModeHint =
|
||
'Runs in this chat’s folder on its machine. The agent does not see it.';
|
||
|
||
/// The chat actions menu entry and its dialog.
|
||
const String chatShellRunActionLabel = 'Run command…';
|
||
const String chatShellRunDialogTitle = 'Run command';
|
||
const String chatShellRunDialogConfirm = 'Run';
|
||
|
||
/// Whether [draft] is in shell mode: its first non-blank character is `!`.
|
||
bool chatDraftIsShell(String draft) =>
|
||
draft.trimLeft().startsWith(chatShellPrefix);
|
||
|
||
/// The command a shell-mode draft runs (trimmed, without the `!`), or null
|
||
/// when [draft] is not in shell mode. An empty string means only `!` was typed.
|
||
String? chatShellCommandOf(String draft) {
|
||
if (!chatDraftIsShell(draft)) return null;
|
||
return draft.trimLeft().substring(chatShellPrefix.length).trim();
|
||
}
|
||
|
||
/// [rows] with one card row per run, placed by start time among the rows
|
||
/// that carry a recorded time. Rows without a time (a just-sent message, the
|
||
/// pending reply) keep their place; runs newer than every timed row follow.
|
||
List<JsonMap> withChatShellRunRows(
|
||
List<JsonMap> rows,
|
||
List<ChatShellRun> runs,
|
||
) {
|
||
if (runs.isEmpty) return rows;
|
||
final ordered = [...runs]..sort((a, b) => a.startedAt.compareTo(b.startedAt));
|
||
final merged = <JsonMap>[];
|
||
var next = 0;
|
||
for (final row in rows) {
|
||
final time = _rowTime(row);
|
||
while (time != null &&
|
||
next < ordered.length &&
|
||
ordered[next].startedAt < time) {
|
||
merged.add(ordered[next++].toRow());
|
||
}
|
||
merged.add(row);
|
||
}
|
||
while (next < ordered.length) {
|
||
merged.add(ordered[next++].toRow());
|
||
}
|
||
return merged;
|
||
}
|
||
|
||
int? _rowTime(JsonMap row) {
|
||
final created = row['created_at'];
|
||
if (created is int && created > 0) return created;
|
||
final message = row['message'];
|
||
if (message is Map) {
|
||
return chatMessageCreatedAt(Map<String, dynamic>.from(message));
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/// The finished card's status words.
|
||
String chatShellStatusLabel(ChatShellRun run) => switch (run.state) {
|
||
ChatShellRunState.running => 'Running',
|
||
ChatShellRunState.succeeded => 'Exit 0',
|
||
ChatShellRunState.stopped => 'Stopped',
|
||
ChatShellRunState.interrupted => 'Interrupted',
|
||
ChatShellRunState.failed =>
|
||
run.signal.isNotEmpty
|
||
? 'Killed (${run.signal})'
|
||
: run.exitCode != null
|
||
? 'Exit ${run.exitCode}'
|
||
: 'Failed',
|
||
};
|
||
|
||
/// How long the command ran, as the status chip's tail.
|
||
String chatShellDuration(int milliseconds) {
|
||
if (milliseconds < 1000) return '$milliseconds ms';
|
||
final seconds = milliseconds / 1000;
|
||
if (seconds < 60) return '${seconds.toStringAsFixed(1)} s';
|
||
final minutes = seconds ~/ 60;
|
||
return '${minutes}m ${(seconds % 60).round()}s';
|
||
}
|