fix flutter runs foundation parity gaps
This commit is contained in:
@@ -0,0 +1,105 @@
|
||||
import '../../../core/api_gateway.dart';
|
||||
import '../domain/run_models.dart';
|
||||
|
||||
/// Protocol failure kept separate from transport errors; server error envelopes
|
||||
/// are otherwise propagated unchanged by [ApiGateway].
|
||||
class RunsRepositoryException implements Exception {
|
||||
const RunsRepositoryException(this.message);
|
||||
final String message;
|
||||
@override
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
/// Run protocol requests over the application's shared gateway.
|
||||
class RunsRepository {
|
||||
RunsRepository(this._gateway);
|
||||
final ApiGateway _gateway;
|
||||
|
||||
Future<RunListResult> listRuns() async {
|
||||
final reply = await _gateway.call('list_runs');
|
||||
final runs = reply['runs'];
|
||||
return RunListResult(
|
||||
runs is List
|
||||
? runs
|
||||
.whereType<Map>()
|
||||
.map((r) => Map<String, dynamic>.from(r))
|
||||
.toList()
|
||||
: const [],
|
||||
reply['usage'] is Map
|
||||
? Map<String, dynamic>.from(reply['usage'] as Map)
|
||||
: null,
|
||||
);
|
||||
}
|
||||
|
||||
Future<JsonMap> getRun(num id) async =>
|
||||
_required(await _gateway.call('get_run', {'run_id': id}), 'run_detail');
|
||||
Future<JsonMap> cancelRun(num id) async => _required(
|
||||
await _gateway.call('cancel_run', {'run_id': id}),
|
||||
'run_detail',
|
||||
);
|
||||
|
||||
Future<JsonMap> retryRun(
|
||||
num id, {
|
||||
required String requestKey,
|
||||
String? mode,
|
||||
}) async {
|
||||
final fields = <String, dynamic>{'run_id': id, 'request_key': requestKey};
|
||||
if (mode == 'restart') fields['retry_mode'] = mode;
|
||||
return _required(await _gateway.call('retry_run', fields), 'run_detail');
|
||||
}
|
||||
|
||||
Future<JsonMap> decideRun(
|
||||
num id, {
|
||||
String? stage,
|
||||
required String option,
|
||||
String feedback = '',
|
||||
}) async {
|
||||
final selected = option.trim();
|
||||
if (selected.isEmpty) {
|
||||
throw const RunsRepositoryException(
|
||||
'Choose an option before continuing.',
|
||||
);
|
||||
}
|
||||
final fields = <String, dynamic>{'run_id': id, 'option': selected};
|
||||
if (stage != null && stage.trim().isNotEmpty) fields['stage'] = stage;
|
||||
if (feedback.trim().isNotEmpty) fields['feedback'] = feedback.trim();
|
||||
return _required(await _gateway.call('decide_run', fields), 'run_detail');
|
||||
}
|
||||
|
||||
Future<JsonMap> setRunTitle(num id, String title) async => _required(
|
||||
await _gateway.call('set_run_title', {'run_id': id, 'title': title}),
|
||||
'run_detail',
|
||||
);
|
||||
|
||||
Future<JsonMap> startRun({
|
||||
required num pipelineId,
|
||||
required num version,
|
||||
required String prompt,
|
||||
required String workspace,
|
||||
required String requestKey,
|
||||
String title = '',
|
||||
String outputFormat = '',
|
||||
String notifyMode = 'auto',
|
||||
}) async {
|
||||
final fields = <String, dynamic>{
|
||||
'pipeline_id': pipelineId,
|
||||
'version': version,
|
||||
'request_key': requestKey,
|
||||
'workspace': workspace.trim(),
|
||||
'input': {'prompt': prompt},
|
||||
};
|
||||
if (title.trim().isNotEmpty) fields['title'] = title.trim();
|
||||
if (outputFormat.isNotEmpty) fields['output_format'] = outputFormat;
|
||||
final mode = notifyMode.trim().toLowerCase();
|
||||
if (mode == 'on' || mode == 'off') fields['notify_mode'] = mode;
|
||||
return _required(await _gateway.call('run_pipeline', fields), 'run_detail');
|
||||
}
|
||||
|
||||
JsonMap _required(JsonMap reply, String key) {
|
||||
final value = reply[key];
|
||||
if (value is! Map) {
|
||||
throw RunsRepositoryException('The server returned no $key.');
|
||||
}
|
||||
return Map<String, dynamic>.from(value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user