import '../../../core/api_gateway.dart'; import '../../pipelines/pipeline_format.dart' show formatDuration; /// One declared stage of a `show_run` card's map with its progress in the run /// (`internal/protocol/chat_shown.go` ChatShownRunStage). class ChatShownRunStage { const ChatShownRunStage({ required this.key, required this.type, required this.visits, required this.status, required this.seconds, required this.runningSince, }); final String key; final String type; /// How many times the run entered the stage; 0 when it never did. final int visits; /// The latest visit's recorded status, empty when never visited. final String status; /// Seconds spent in the stage's finished visits. final int seconds; /// The running visit's start (epoch milliseconds), or null. final int? runningSince; bool get visited => visits > 0; /// Total time in the stage at [now] (epoch milliseconds), counting a running /// visit; null for a stage that never ran. String? duration(int now) { if (!visited) return null; final since = runningSince; final live = since == null || now < since ? 0 : (now - since) ~/ 1000; return formatDuration(seconds + live); } } /// A `show_run` card's map: the run's graph as the graph feature reads it and /// the compact route evidence as run-detail steps, plus the stage list. /// /// The server bounds it by the graph's size (ChatShownRunMapStages), not by /// the run's length. class ChatShownRunMap { const ChatShownRunMap({ required this.graph, required this.steps, required this.stages, }); /// The map of a run body, or null when the server sent none. static ChatShownRunMap? of(Object? raw) { if (raw is! Map) return null; final rawStages = raw['stages']; if (rawStages is! List) return null; final stages = []; final definitions = []; for (final stage in rawStages.whereType()) { final key = '${stage['key'] ?? ''}'.trim(); if (key.isEmpty) continue; final edges = stage['edges']; definitions.add({ 'key': key, 'type': stage['type'], 'edges': edges is Map ? JsonMap.from(edges) : const {}, 'fallback_error': stage['fallback_error'] == true, }); stages.add( ChatShownRunStage( key: key, type: '${stage['type'] ?? ''}', visits: _int(stage['visits']) ?? 0, status: '${stage['status'] ?? ''}'.trim(), seconds: _int(stage['seconds']) ?? 0, runningSince: _positive(stage['running_since']), ), ); } if (stages.isEmpty) return null; final rawVisits = raw['visits']; final steps = [ if (rawVisits is List) for (final visit in rawVisits.whereType()) { 'sequence': visit['sequence'], 'stage': '${visit['stage'] ?? ''}', 'status': '${visit['status'] ?? ''}', 'outcome': '${visit['outcome'] ?? ''}', 'target': '${visit['target'] ?? ''}', }, ]; return ChatShownRunMap( graph: {'stages': definitions}, steps: steps, stages: stages, ); } /// The graph definition shape (`stages` with `key`, `edges`). final JsonMap graph; /// The route evidence in the run-detail step shape, in sequence order. final List steps; final List stages; /// The run-detail map the graph feature reads its evidence from. JsonMap runDetail({required String currentStage, required bool executing}) => {'steps': steps, 'current_stage': currentStage, 'executing': executing}; } int? _int(Object? value) => value is num ? value.toInt() : null; int? _positive(Object? value) => value is num && value > 0 ? value.toInt() : null;