import '../../../core/api_gateway.dart'; import '../../graph/domain/stage_definition.dart'; import '../../pipelines/pipeline_format.dart'; import 'run_detail_content.dart'; /// The exact empty wording for a visit's recorded Input and Output. They are /// the run tabs' own sentences, so a visit never says something different for /// the same absent fact. const String visitEmptyInput = 'No input recorded.'; const String visitEmptyOutput = 'No output recorded.'; /// One always-visible fact of a recorded visit. typedef VisitFact = ({String label, String value}); /// The detail's own recorded steps as maps, in recorded order. List detailRunSteps(JsonMap run) { final steps = run['steps']; if (steps is! List) return const []; return steps.whereType().map(JsonMap.from).toList(); } /// The raw step at [index] of the detail's own `steps`, or null when the reply /// has no such recorded visit. /// /// The order is the server's recorded order, the same order [detailTimeline] /// uses, so a row and its pane always describe one visit. JsonMap? detailVisitStepAt(JsonMap run, int index) { final steps = detailRunSteps(run); if (index < 0 || index >= steps.length) return null; return steps[index]; } /// The row index of [stageKey]'s latest recorded visit, or null when the run /// never recorded that stage. /// /// The order matches [routeDetailVisitIndex] and the web client's /// `visitsByStage`: a stage's visits follow their recorded sequence, not their /// position in the reply's step list. So a Graph click and `/runs/:id/stage/:key` /// open the same visit even when the steps are not in sequence order. int? latestDetailVisitIndex(dynamic steps, String stageKey) => routeDetailVisitIndex(steps, stageKey, null); /// The timeline row index a route's [stageKey] and [visit] name, or null when /// the run recorded no visit of that stage. /// /// A stage's visits follow their recorded sequence, as the web client's /// `visitsByStage` does, so the same `?visit=N` selects the same visit in both /// clients while equal sequences keep the timeline's own order. A null [visit] /// is the stage's last recorded visit, the one a Graph node click opens; an /// out-of-range index is clamped to that last visit, exactly as the web client /// clamps it. Nothing is invented for a stage with no recorded visit. int? routeDetailVisitIndex(dynamic steps, String stageKey, int? visit) { final visits = detailTimeline(steps); final indices = [ for (var index = 0; index < visits.length; index += 1) if (visits[index].stage == stageKey) index, ]; if (indices.isEmpty) return null; indices.sort((a, b) { final bySequence = (visits[a].sequence ?? 0).compareTo( visits[b].sequence ?? 0, ); return bySequence != 0 ? bySequence : a.compareTo(b); }); final wanted = visit == null ? indices.length - 1 : visit.clamp(0, indices.length - 1); return indices[wanted]; } /// The visit's recorded facts, in the pane's order. A field the server did not /// record is absent rather than zero, false or a guessed value. List detailVisitFacts(JsonMap step) { final facts = []; final status = _text(step['status']); if (status != null) facts.add((label: 'Status', value: status)); final outcome = _text(step['outcome']); if (outcome != null) facts.add((label: 'Outcome', value: outcome)); final duration = detailVisitDuration(step); if (duration != null) facts.add((label: 'Duration', value: duration)); final model = detailVisitProviderModel(step); if (model != null) facts.add((label: 'Provider/model', value: model)); final target = _text(step['target']); if (target != null) facts.add((label: 'Target', value: target)); return facts; } /// The visit's duration, only when the server recorded both timestamps and the /// finish is not before the start. One stamp is not a zero-length visit. String? detailVisitDuration(JsonMap step) { final seconds = durationSeconds( _text(step['started_at']), _text(step['finished_at']), ); return seconds == null ? null : formatDuration(seconds); } /// The effective provider/model this visit ran with, joined as /// `provider / model` when both were recorded. A visit that recorded neither /// has no row; one recorded part is shown alone, never completed locally. String? detailVisitProviderModel(JsonMap step) { final provider = _text(step['provider']); final model = _text(step['model']); if (provider != null && model != null) return '$provider / $model'; return provider ?? model; } /// A recorded verdict, or null when the server recorded no option and no /// reason. An empty recorded verdict is not rendered as a decision. ({String? option, String? reason})? detailVisitVerdict(JsonMap step) { final verdict = step['verdict']; if (verdict is! Map) return null; final option = _text(verdict['option']); final reason = _text(verdict['reason']); if (option == null && reason == null) return null; return (option: option, reason: reason); } /// The visit's diagnostic exactly as recorded, or null when it is blank. String? detailVisitDiagnostic(JsonMap step) => _text(step['diagnostic']); /// Whether the reply's pinned graph declares [stageKey], so the Definition /// section shows the authored definition rather than its missing wording. bool detailVisitHasDefinition(JsonMap? graph, String stageKey) => graph != null && stageDefinitionFor(graph, stageKey) != null; String? _text(dynamic value) { if (value == null) return null; final text = '$value'.trim(); return text.isEmpty ? null : text; }