import '../../../core/api_gateway.dart'; import '../../pipelines/pipeline_format.dart' show formatDuration; import '../../runs/domain/run_events.dart' show isTerminalRunStatus; /// A `show_run` item's run, as the server records it /// (`internal/protocol/chat_shown.go` ChatShownRunBody). The server pushes the /// item again whenever the run changes, so this is always its latest state. class ChatShownRun { const ChatShownRun({ required this.id, required this.pipeline, required this.title, required this.status, required this.outcome, required this.currentStage, required this.startedAt, required this.finishedAt, }); /// The run of a `run` item, or null for any other item or a malformed one. static ChatShownRun? of(JsonMap item) { final body = item['run']; if (item['kind'] != 'run' || body is! Map) return null; final id = body['id']; if (id is! num || id <= 0) return null; return ChatShownRun( id: id.toInt(), pipeline: _text(body['pipeline']), title: _text(body['title']), status: _text(body['status']), outcome: _text(body['outcome']), currentStage: _text(body['current_stage']), startedAt: _millis(body['started_at']), finishedAt: _millis(body['finished_at']), ); } final int id; final String pipeline; final String title; final String status; final String outcome; final String currentStage; final int? startedAt; final int? finishedAt; /// Whether the run has ended (succeeded, failed, canceled, interrupted). bool get finished => isTerminalRunStatus(status); /// The status in words: the outcome of a finished run when it says more. String get statusLabel { final word = status.isEmpty ? 'unknown' : status; final label = '${word[0].toUpperCase()}${word.substring(1)}'; return finished && outcome.isNotEmpty && outcome != status ? '$label ยท $outcome' : label; } /// How long it ran, or has been running at [now] (epoch milliseconds); null /// without a start time. String? duration(int now) { final start = startedAt; if (start == null) return null; final end = finishedAt ?? (finished ? null : now); if (end == null) return null; return formatDuration((end - start) ~/ 1000); } } String _text(Object? value) => '${value ?? ''}'.trim(); int? _millis(Object? value) => value is num && value > 0 ? value.toInt() : null;