import '../../../core/api_gateway.dart'; import 'run_models.dart'; class PendingDecision { const PendingDecision({ required this.runId, required this.runLabel, required this.stageKey, required this.instructions, required this.options, this.exhausted, }); final num runId; final String runLabel; final String stageKey; final String instructions; final List options; /// The exhausted loop a server-created decision is about; null for a /// declared human stage. final ExhaustedLoop? exhausted; } /// The loop whose visits ran out, from a waiting step's `exhausted` output: /// the model stage, the visit it reached and its limit. class ExhaustedLoop { const ExhaustedLoop({ required this.stage, required this.visit, required this.maxVisits, required this.targets, }); final String stage; final int? visit; final int? maxVisits; /// Where each option leads, by option key. final Map targets; static ExhaustedLoop? parse(dynamic output) { final value = output is Map ? output['exhausted'] : null; if (value is! Map) return null; final targets = value['targets']; return ExhaustedLoop( stage: _string(value['stage']).trim(), visit: _int(value['visit']), maxVisits: _int(value['max_visits']), targets: targets is Map ? {for (final e in targets.entries) '${e.key}': _string(e.value)} : const {}, ); } } /// Readable labels for the options of a server-created exhaustion decision. const exhaustedOptionLabels = { 'accept': 'Accept as is', 'continue': 'One more round', 'abort': 'Abort', }; JsonMap? lastWaitingStep(List steps) { for (final value in steps.reversed) { if (value is Map && value['status'] == 'waiting') { return Map.from(value); } } return null; } String waitingPrompt(dynamic output) => output is Map && output['prompt'] is String ? output['prompt'] as String : ''; List waitingOptions(dynamic output) { final options = output is Map ? output['options'] : null; if (options is! List) return const []; return options .whereType() .where((entry) => entry.trim().isNotEmpty) .toList(); } List humanOptions(dynamic edges) { if (edges is! Map) return const []; return edges.keys.map((key) => '$key').where((key) => key != 'error').toList() ..sort(); } PendingDecision? pendingDecision(JsonMap? run) { if (run == null || run['status'] != 'waiting' || _positive(run['export_pending_sequence'])) { return null; } final steps = run['steps'] is List ? run['steps'] as List : const []; final step = lastWaitingStep(steps); final stage = _string(step?['stage']).trim().isNotEmpty ? _string(step?['stage']) : _string(run['current_stage']); if (stage.trim().isEmpty) return null; JsonMap? declaration; final graph = run['graph']; final graphStages = graph is Map ? graph['stages'] : null; if (graphStages is List) { for (final value in graphStages.whereType()) { if (value['key'] == stage) { declaration = Map.from(value); break; } } } final output = step?['output']; final options = waitingOptions(output); // A loop that exhausted its visits waits on its own (model) stage with the // options the server recorded; any other non-human stage is not a decision. final exhausted = ExhaustedLoop.parse(output); final declared = exhausted == null || options.isEmpty; if (declared && declaration?['type'] != null && declaration!['type'] != 'human') { return null; } final dynamic edges = exhausted != null && !declared ? exhausted.targets : declaration?['edges']; final keys = options.isNotEmpty ? options : humanOptions(edges); if (keys.isEmpty) return null; final instructions = waitingPrompt(output).trim().isNotEmpty ? waitingPrompt(output) : _string(declaration?['prompt']); return PendingDecision( runId: run['id'] is num ? run['id'] as num : 0, runLabel: runIdentity(run['title'], run['name']).label, stageKey: stage, instructions: instructions, exhausted: declared ? null : exhausted, options: keys .map( (key) => DecisionOption( key, target: _targetLabel(key, edges is Map ? edges[key] : null), label: declared ? '' : exhaustedOptionLabels[key] ?? '', ), ) .toList(), ); } String _targetLabel(String option, dynamic target) { if (target == null || '$target'.trim().isEmpty) return ''; final resolved = target == '#' ? (option == 'error' ? '#failure' : '#success') : '$target'; return switch (resolved) { '#success' => 'SUCCESS', '#failure' => 'FAILURE', _ => resolved, }; } bool _positive(dynamic value) => value is num && value > 0; String _string(dynamic value) => value == null ? '' : '$value'; int? _int(dynamic value) => value is num ? value.toInt() : int.tryParse(_string(value).trim());