111 lines
3.2 KiB
Dart
111 lines
3.2 KiB
Dart
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,
|
|
});
|
|
final num runId;
|
|
final String runLabel;
|
|
final String stageKey;
|
|
final String instructions;
|
|
final List<DecisionOption> options;
|
|
}
|
|
|
|
JsonMap? lastWaitingStep(List<dynamic> steps) {
|
|
for (final value in steps.reversed) {
|
|
if (value is Map && value['status'] == 'waiting') {
|
|
return Map<String, dynamic>.from(value);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
String waitingPrompt(dynamic output) =>
|
|
output is Map && output['prompt'] is String
|
|
? output['prompt'] as String
|
|
: '';
|
|
|
|
List<String> waitingOptions(dynamic output) {
|
|
final options = output is Map ? output['options'] : null;
|
|
if (options is! List) return const [];
|
|
return options
|
|
.whereType<String>()
|
|
.where((entry) => entry.trim().isNotEmpty)
|
|
.toList();
|
|
}
|
|
|
|
List<String> 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<Map>()) {
|
|
if (value['key'] == stage) {
|
|
declaration = Map<String, dynamic>.from(value);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
if (declaration?['type'] != null && declaration!['type'] != 'human') {
|
|
return null;
|
|
}
|
|
final edges = declaration?['edges'];
|
|
final options = waitingOptions(step?['output']);
|
|
final keys = options.isNotEmpty ? options : humanOptions(edges);
|
|
if (keys.isEmpty) return null;
|
|
final instructions = waitingPrompt(step?['output']).trim().isNotEmpty
|
|
? waitingPrompt(step?['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,
|
|
options: keys
|
|
.map(
|
|
(key) => DecisionOption(
|
|
key,
|
|
target: _targetLabel(key, edges is Map ? edges[key] : null),
|
|
),
|
|
)
|
|
.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';
|