Files
pipelines/app/lib/features/chat/domain/chat_shown_run.dart
T
alan 32df4c8569 Merge branch 'worktree-agent-a5f621112ab3cad4f'
# Conflicts:
#	app/lib/features/chat/domain/chat_shown_run.dart
2026-12-03 14:43:11 +00:00

83 lines
2.7 KiB
Dart

import '../../../core/api_gateway.dart';
import '../../pipelines/pipeline_format.dart' show formatDuration;
import '../../runs/domain/run_events.dart' show isTerminalRunStatus;
import '../../runs/domain/run_quota.dart';
import 'chat_shown_run_map.dart';
export 'chat_shown_run_map.dart';
/// 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,
this.map,
});
/// 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']),
map: ChatShownRunMap.of(body['map']),
);
}
final int id;
final String pipeline;
final String title;
final String status;
final String outcome;
final String currentStage;
final int? startedAt;
final int? finishedAt;
/// The graph and stage progress for the card's mini-map, when sent.
final ChatShownRunMap? map;
/// 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 {
if (isWaitingQuotaStatus(status)) return runWaitingQuotaLabel;
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;