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 = '! — 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 withChatShellRunRows( List rows, List runs, ) { if (runs.isEmpty) return rows; final ordered = [...runs]..sort((a, b) => a.startedAt.compareTo(b.startedAt)); final merged = []; 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.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'; }