Files
pipelines/app/lib/features/chat/domain/chat_shown_run_map.dart
T
alan 7872e57d40 Run card mini-map and stage strip; graph edge hover highlight
The show_run card carries the run's graph and per-stage progress (bounded by
the graph, not the run length) and draws a still mini-map with the run graph
plus a stage strip with status, visits and time. Hovering or tapping an edge
in the pipeline and run graphs highlights it, its label and endpoints, with a
tooltip naming from -> to, the outcome and, for runs, how often it was taken.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-03 12:56:51 +00:00

120 lines
3.8 KiB
Dart

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 = <ChatShownRunStage>[];
final definitions = <JsonMap>[];
for (final stage in rawStages.whereType<Map>()) {
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 <String, dynamic>{},
'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 = <JsonMap>[
if (rawVisits is List)
for (final visit in rawVisits.whereType<Map>())
{
'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<JsonMap> steps;
final List<ChatShownRunStage> 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;