import '../../../core/api_gateway.dart'; /// Whether the operator may cancel [run], matching `canCancel` in /// `web/src/actions.js` exactly: /// /// `(executing || (status == "waiting" && export_pending_sequence > 0)) && /// !cancel_requested && !cleanup_unconfirmed` /// /// The status word alone is not enough: a stale `running` status without /// `executing` is not cancellable, while a run waiting on a pending session /// export is. bool canCancelRun(JsonMap? run) { if (run == null) return false; final exportPending = _number(run['export_pending_sequence']); return (run['executing'] == true || (run['status'] == 'waiting' && exportPending > 0)) && run['cancel_requested'] != true && run['cleanup_unconfirmed'] != true; } /// The run id used by the cancel request, or null for a malformed run. num? cancelRunId(JsonMap run) { final id = run['id']; return id is num ? id : null; } /// `Number(value) > 0`, tolerating numeric strings as the browser does. double _number(dynamic value) { if (value is num) return value.toDouble(); return double.tryParse('${value ?? ''}'.trim()) ?? 0; }