Files
pipelines/app/lib/features/pipelines/ui/run_pipeline_action.dart
T

257 lines
8.1 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../core/api_gateway.dart';
import '../../../core/transport/connection_state.dart';
import '../../runs/data/runs_repository.dart';
import '../pipeline_models.dart';
import '../pipelines_controller.dart';
import '../run_pipeline_intent.dart';
import '../widgets/pipeline_controls.dart';
import 'run_pipeline_dialog.dart';
import 'run_pipeline_wording.dart';
/// The pipeline detail's Run action.
///
/// Nothing is requested until the dialog's confirm. A confirmed attempt sends
/// the web's exact `run_pipeline` body against the pinned resolved version; a
/// lost reply keeps the intent so the next deliberate press checks by the same
/// request key before resending. Only the id the server names is opened.
class RunPipelineAction extends StatefulWidget {
const RunPipelineAction({
super.key,
required this.controller,
required this.detail,
required this.gateway,
this.onRunStarted,
this.fab = false,
});
/// A floating action button instead of an inline button.
final bool fab;
final PipelinesController controller;
final PipelineDetail detail;
final ApiGateway gateway;
/// Receives the id from an accepted server reply. It is never called for a
/// guessed or malformed response.
final ValueChanged<int>? onRunStarted;
@override
State<RunPipelineAction> createState() => _RunPipelineActionState();
}
class _RunPipelineActionState extends State<RunPipelineAction> {
bool _dialogOpen = false;
int get _pipelineId => widget.detail.id;
int get _version => widget.detail.resolvedVersion;
Future<void> _open() async {
if (_dialogOpen) return;
_dialogOpen = true;
try {
final pending = await widget.controller.loadPendingRun(
_pipelineId,
_version,
);
if (!mounted) return;
await showRunPipelineDialog(
context: context,
detail: widget.detail,
gateway: widget.gateway,
pending: pending,
onSubmit: _submit,
onPendingChanged: (intent) {
if (intent == null) {
widget.controller.clearPendingRun(_pipelineId, _version);
} else {
widget.controller.rememberPendingRun(intent);
}
},
onAccepted: _accepted,
);
} finally {
_dialogOpen = false;
}
}
void _accepted(JsonMap run) {
final id = runPipelineRunId(run);
if (id == null) return;
widget.controller.clearPendingRun(_pipelineId, _version);
widget.onRunStarted?.call(id);
}
Future<RunPipelineOutcome> _submit(RunPipelineIntent intent) async {
final repository = RunsRepository(widget.gateway);
final pending = widget.controller.pendingRunFor(
intent.pipelineId,
intent.version,
);
if (pending?.requestKey == intent.requestKey) {
// A previous send may have been admitted even though its reply was lost.
// Check before resending the same key; the server dedupes either way.
try {
final existing = await repository.runByRequestKey(intent.requestKey);
if (existing != null) return _accept(existing);
} catch (error) {
widget.controller.rememberPendingRun(intent);
return RunPipelineRefused(
'${describeRunPipelineError(error)}$runPipelineUnknownSuffix',
admissionUnknown: true,
);
}
}
try {
final run = await repository.startRun(
pipelineId: intent.pipelineId,
version: intent.version,
prompt: intent.prompt,
workspace: intent.workspace,
requestKey: intent.requestKey,
notifyMode: intent.notifyMode.wireValue,
outputFormat: intent.outputFormat.wireValue,
);
final accepted = _accept(run);
if (accepted is RunPipelineAccepted) return accepted;
// A reply without a usable run id is not navigable: read the admitted run
// by key rather than inventing an id or starting another run.
return _refuse(repository, intent, 'The server returned no run id.');
} on ProtocolError catch (error) {
return _refuse(repository, intent, describeRunPipelineError(error));
} on RunsRepositoryException catch (error) {
return _refuse(repository, intent, describeRunPipelineError(error));
} catch (error) {
widget.controller.rememberPendingRun(intent);
return RunPipelineRefused(
'${describeRunPipelineError(error)}$runPipelineUnknownSuffix',
admissionUnknown: true,
);
}
}
/// A reply is only accepted when it names a usable run id.
RunPipelineOutcome _accept(JsonMap run) {
if (runPipelineRunId(run) == null) {
return const RunPipelineRefused(
'The server returned no run id.',
admissionUnknown: false,
);
}
widget.controller.clearPendingRun(widget.detail.id, _version);
return RunPipelineAccepted(run);
}
/// A confirmed refusal: prove by key that no run was admitted before letting
/// the operator correct the fields and try with a fresh key.
Future<RunPipelineOutcome> _refuse(
RunsRepository repository,
RunPipelineIntent intent,
String message,
) async {
try {
final existing = await repository.runByRequestKey(intent.requestKey);
if (existing != null) return _accept(existing);
widget.controller.clearPendingRun(intent.pipelineId, intent.version);
return RunPipelineRefused(message, admissionUnknown: false);
} catch (_) {
// Admission cannot be proven either way: keep the intent immutable.
widget.controller.rememberPendingRun(intent);
return RunPipelineRefused(
'$message$runPipelineUnknownSuffix',
admissionUnknown: true,
);
}
}
@override
Widget build(BuildContext context) {
final label = const Text(runPipelineActionLabel);
if (widget.fab) {
return pipelineFab(
context,
key: runPipelineActionKey,
primary: true,
materialIcon: Icons.play_arrow,
cupertinoIcon: CupertinoIcons.play_fill,
label: runPipelineActionLabel,
onPressed: _open,
);
}
if (useCupertinoControls(context)) {
return CupertinoButton.filled(
key: runPipelineActionKey,
onPressed: _open,
child: label,
);
}
return FilledButton(
key: runPipelineActionKey,
onPressed: _open,
child: label,
);
}
}
/// A floating action for a pipeline: Material's extended FAB, or on Cupertino
/// a filled pill with the same icon and word and a soft shadow.
Widget pipelineFab(
BuildContext context, {
required Key key,
required bool primary,
required IconData materialIcon,
required IconData cupertinoIcon,
required String label,
required VoidCallback? onPressed,
}) {
if (useCupertinoControls(context)) {
final content = Row(
mainAxisSize: MainAxisSize.min,
children: [
Icon(cupertinoIcon, size: 18),
const SizedBox(width: 6),
Text(label),
],
);
return DecoratedBox(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(28),
boxShadow: const [
BoxShadow(
color: Color(0x33000000),
blurRadius: 8,
offset: Offset(0, 2),
),
],
),
child: primary
? CupertinoButton.filled(
key: key,
borderRadius: BorderRadius.circular(28),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 14),
onPressed: onPressed,
child: content,
)
: CupertinoButton.tinted(
key: key,
borderRadius: BorderRadius.circular(28),
padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 14),
onPressed: onPressed,
child: content,
),
);
}
final scheme = Theme.of(context).colorScheme;
return FloatingActionButton.extended(
key: key,
heroTag: key,
backgroundColor: primary ? null : scheme.secondaryContainer,
foregroundColor: primary ? null : scheme.onSecondaryContainer,
onPressed: onPressed,
icon: Icon(materialIcon),
label: Text(label),
);
}