Files
pipelines/app/lib/features/runs/ui/run_detail_visit_session.dart
T

245 lines
8.4 KiB
Dart

import '../../../ui/theme/app_theme.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../../ui/adaptive/host.dart';
import '../../artifacts/domain/artifact_record.dart';
import '../../artifacts/domain/artifact_url.dart';
import '../../artifacts/ui/artifact_external_open.dart';
import '../actions/session_export_repair_action.dart';
import 'run_detail_visit_recorded.dart';
import 'run_detail_visit_session_state.dart';
/// The visit's recorded session pointer, its recorded export state and, when
/// the visit exported one, a read-only external open of that document.
///
/// The state comes from [detailVisitSession], so a legacy `session.html`, a
/// repair, a still-running session and a failed one each report what the server
/// recorded. Complete, partial, missing, skipped, repair and unrecognised
/// states each keep the server's own word and diagnostic; recorded repair
/// attempts and their open-option labels stay read-only. An export whose
/// recorded evidence is incomplete or truncated offers the web client's
/// export-only repair, which sends that exact visit's recorded sequence and
/// keeps this recorded state until the server replies; a legacy `session.html`
/// with no recorded evidence offers no control, exactly as the web does. The
/// open goes through the landed safe-link helper: the recorded url is resolved
/// by [resolveArtifactUrl] and passed to [openArtifactExternally] without being
/// fetched, rewritten or previewed. A refusal is reported.
class RunVisitSession extends StatefulWidget {
const RunVisitSession({
super.key,
required this.run,
required this.step,
required this.gateway,
required this.onRunChanged,
required this.configuredEndpoint,
this.openExternal = openArtifactExternally,
});
final JsonMap run;
final JsonMap step;
final ApiGateway gateway;
final ValueChanged<JsonMap> onRunChanged;
final String configuredEndpoint;
final ArtifactExternalOpen openExternal;
@override
State<RunVisitSession> createState() => _RunVisitSessionState();
}
class _RunVisitSessionState extends State<RunVisitSession> {
String? _status;
Future<void> _open(ResolvedArtifactUrl resolved) async {
final opened = await widget.openExternal(resolved.uri);
if (!mounted) return;
setState(
() => _status = opened
? 'Link opened externally.'
: 'Could not open the link.',
);
}
@override
Widget build(BuildContext context) {
final session = detailVisitSession(widget.step);
final export = session.export;
final resolved = export == null
? null
: resolveArtifactUrl(widget.configuredEndpoint, export.artifact.url);
final note = export?.statusNote;
final fact = session.exportFact;
final sequence = recordedCount(widget.step['sequence']);
final repair = _repairAction(session, sequence);
final showReason =
export != null &&
fact?.reason != null &&
fact!.state != VisitSessionExportState.repair;
final children = <Widget>[
if (session.sessionId != null)
_SessionFact(label: 'Session', value: session.sessionId!),
if (fact != null)
_SessionFact(
key: const Key('run-visit-session-export'),
label: 'Export',
value: fact.word,
),
if (showReason)
_SessionFact(
key: const Key('run-visit-session-export-reason'),
label: 'Reason',
value: fact.reason!,
),
if (export != null && note != null) Text(note),
..._repairFacts(session.repairs),
if (export == null)
Text(session.message)
else if (resolved == null)
const Text(visitSessionEndpointMissing)
else
_openButton(export.artifact, resolved),
?repair,
if (_status != null)
Padding(
padding: const EdgeInsets.only(top: 8),
child: Semantics(liveRegion: true, child: Text(_status!)),
),
];
if (usesCupertinoHost(context)) {
return ListView(
key: const Key('run-visit-body-session'),
children: [
CupertinoListSection.insetGrouped(
backgroundColor: AppColors.groupedBackground,
children: [
Padding(
padding: const EdgeInsetsDirectional.fromSTEB(16, 12, 16, 16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: _spaced(children),
),
),
],
),
],
);
}
return ListView(
key: const Key('run-visit-body-session'),
padding: const EdgeInsets.all(16),
children: _spaced(children),
);
}
Widget _openButton(ArtifactRecord export, ResolvedArtifactUrl resolved) {
final label = 'Open ${export.name.isEmpty ? 'session.html' : export.name}';
if (usesCupertinoHost(context)) {
return CupertinoButton(
padding: EdgeInsets.zero,
onPressed: () => _open(resolved),
child: Text(label),
);
}
return Align(
alignment: AlignmentDirectional.centerStart,
child: TextButton(onPressed: () => _open(resolved), child: Text(label)),
);
}
/// The export-only repair option: offered only for a visit whose recorded
/// session identity and sequence can be read again, and whose recorded export
/// evidence is incomplete or truncated (or which has no openable document at
/// all). A legacy `session.html` chosen only by the no-evidence fallback has
/// no evidence to repair and keeps the web's absence of the control. The
/// visit's own recorded sequence is sent, never a guessed one.
Widget? _repairAction(VisitSessionState session, int sequence) {
final runId = recordedCount(widget.run['id']);
if (runId < 1 || sequence < 1) return null;
if (detailVisitRepairIdentity(widget.step) == null) return null;
final export = session.export;
if (export != null && !visitSessionExportNeedsRepair(export)) return null;
final status = recordedText(widget.step['status']);
final runStatus = recordedText(widget.run['status']);
final pending = recordedCount(widget.run['export_pending_sequence']);
final running =
widget.step['agent_active'] == true ||
status == 'running' ||
status == 'queued' ||
runStatus == 'running' ||
runStatus == 'queued' ||
(runStatus == 'waiting' && pending != sequence);
return SessionExportRepairAction(
runId: runId,
sequence: sequence,
gateway: widget.gateway,
running: running,
onRepaired: widget.onRunChanged,
);
}
/// Recorded repair attempts, in the pane's own fact rows. These labels stay
/// read-only; the request is made only by [SessionExportRepairAction].
List<Widget> _repairFacts(List<VisitSessionRepair> repairs) {
final facts = <Widget>[];
for (var index = 0; index < repairs.length; index += 1) {
final repair = repairs[index];
facts.add(
_SessionFact(
key: ValueKey('run-visit-session-repair-$index'),
label: 'Repair',
value: repair.label,
),
);
final option = repair.option;
if (option != null) {
facts.add(
_SessionFact(
key: ValueKey('run-visit-session-repair-option-$index'),
label: 'Repair option',
value: option,
),
);
}
}
return facts;
}
List<Widget> _spaced(List<Widget> children) => [
for (var index = 0; index < children.length; index += 1) ...[
if (index > 0) const SizedBox(height: 8),
children[index],
],
];
}
class _SessionFact extends StatelessWidget {
const _SessionFact({super.key, required this.label, required this.value});
final String label;
final String value;
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final cupertino = CupertinoTheme.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
label,
style: usesCupertinoHost(context)
? cupertino.textTheme.tabLabelTextStyle.copyWith(
color: CupertinoColors.secondaryLabel.resolveFrom(context),
)
: (theme.textTheme.labelMedium ?? const TextStyle()).copyWith(
color: theme.colorScheme.onSurfaceVariant,
),
),
const SizedBox(height: 2),
Text(value),
],
);
}
}