338 lines
10 KiB
Dart
338 lines
10 KiB
Dart
import '../../schedules/data/schedules_repository.dart';
|
|
import '../../schedules/domain/editor/schedule_draft.dart';
|
|
import '../../schedules/ui/pipeline/schedule_pipeline_editor.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
import '../../../core/servers/server_scope.dart';
|
|
|
|
import '../../../core/api_gateway.dart';
|
|
import '../../settings/settings_store.dart';
|
|
import '../../../ui/adaptive/host.dart';
|
|
import '../pipeline_models.dart';
|
|
import '../run_pipeline_intent.dart';
|
|
import 'run_pipeline_fields.dart';
|
|
import 'run_pipeline_wording.dart';
|
|
|
|
/// Opens the native start-a-run prompt; resolves when it closes.
|
|
///
|
|
/// Opening, typing and Cancel send nothing. Only the confirm action validates
|
|
/// and submits. [pending] restores an intent whose admission is still unknown,
|
|
/// so its fields are locked and the same request key is offered for check/retry.
|
|
Future<void> showRunPipelineDialog({
|
|
required BuildContext context,
|
|
required PipelineDetail detail,
|
|
required ApiGateway gateway,
|
|
required RunPipelineIntent? pending,
|
|
required Future<RunPipelineOutcome> Function(RunPipelineIntent intent)
|
|
onSubmit,
|
|
required ValueChanged<RunPipelineIntent?> onPendingChanged,
|
|
required ValueChanged<JsonMap> onAccepted,
|
|
}) {
|
|
// Schedule instead: the editor opens as the next page from the screen that
|
|
// opened this dialog, seeded with what was typed here.
|
|
Future<void> schedule(ScheduleDraft draft) {
|
|
final requestKey = newScheduleCreateRequestKey();
|
|
final repository = SchedulesRepository(gateway);
|
|
return openSchedulePipelineEditor(
|
|
context,
|
|
draft: draft,
|
|
onSubmit: (record) =>
|
|
repository.saveSchedule(record, requestKey: requestKey),
|
|
);
|
|
}
|
|
|
|
final dialog = _RunPipelineDialog(
|
|
detail: detail,
|
|
gateway: gateway,
|
|
pending: pending,
|
|
cupertino: usesCupertinoHost(context),
|
|
onSubmit: onSubmit,
|
|
onPendingChanged: onPendingChanged,
|
|
onAccepted: onAccepted,
|
|
onSchedule: schedule,
|
|
);
|
|
return dialog.cupertino
|
|
? showCupertinoDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => dialog,
|
|
)
|
|
: showDialog<void>(
|
|
context: context,
|
|
barrierDismissible: false,
|
|
builder: (_) => dialog,
|
|
);
|
|
}
|
|
|
|
class _RunPipelineDialog extends StatefulWidget {
|
|
const _RunPipelineDialog({
|
|
required this.detail,
|
|
required this.gateway,
|
|
required this.pending,
|
|
required this.cupertino,
|
|
required this.onSubmit,
|
|
required this.onPendingChanged,
|
|
required this.onAccepted,
|
|
required this.onSchedule,
|
|
});
|
|
|
|
final Future<void> Function(ScheduleDraft draft) onSchedule;
|
|
|
|
final PipelineDetail detail;
|
|
final ApiGateway gateway;
|
|
final RunPipelineIntent? pending;
|
|
final bool cupertino;
|
|
final Future<RunPipelineOutcome> Function(RunPipelineIntent intent) onSubmit;
|
|
final ValueChanged<RunPipelineIntent?> onPendingChanged;
|
|
final ValueChanged<JsonMap> onAccepted;
|
|
|
|
@override
|
|
State<_RunPipelineDialog> createState() => _RunPipelineDialogState();
|
|
}
|
|
|
|
class _RunPipelineDialogState extends State<_RunPipelineDialog> {
|
|
late final TextEditingController _prompt;
|
|
late final TextEditingController _workspace;
|
|
late RunNotifyMode _notify;
|
|
late RunOutputFormat _outputFormat;
|
|
late RunPipelineIntent? _intent;
|
|
bool _sending = false;
|
|
bool _directoriesLoading = true;
|
|
List<String> _directories = const [];
|
|
String? _error;
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
final pending = widget.pending;
|
|
final restored =
|
|
pending != null &&
|
|
pending.matches(widget.detail.id, widget.detail.resolvedVersion)
|
|
? pending
|
|
: null;
|
|
_intent = restored;
|
|
_notify = restored?.notifyMode ?? RunNotifyMode.pipelineDefault;
|
|
_outputFormat = restored?.outputFormat ?? RunOutputFormat.none;
|
|
_prompt = TextEditingController(text: restored?.prompt ?? '');
|
|
_workspace = TextEditingController(text: restored?.workspace ?? '');
|
|
_error = restored == null ? null : runPipelineUnknownAdmission;
|
|
_loadDirectories(restored);
|
|
}
|
|
|
|
Future<void> _loadDirectories(RunPipelineIntent? restored) async {
|
|
final savedWorkspace = SharedPreferencesSettingsStore(
|
|
serverId: serverStorageScope(serverIdentity(_gateway)),
|
|
).load().then((settings) => settings.workspace);
|
|
try {
|
|
final reply = await _gateway.call('chat_directories');
|
|
final value = reply['chat_directories'];
|
|
final directories = value is List
|
|
? value.whereType<String>().toList()
|
|
: <String>[];
|
|
final saved = await savedWorkspace;
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_directories = directories
|
|
.where((path) => path.startsWith('/'))
|
|
.toList();
|
|
if (_workspace.text.isEmpty) {
|
|
_workspace.text = saved.startsWith('/')
|
|
? saved
|
|
: _directories.isEmpty
|
|
? ''
|
|
: _directories.first;
|
|
}
|
|
_directoriesLoading = false;
|
|
});
|
|
} catch (error) {
|
|
if (!mounted) return;
|
|
setState(() {
|
|
_directoriesLoading = false;
|
|
_error = '$error';
|
|
});
|
|
}
|
|
}
|
|
|
|
ApiGateway get _gateway => widget.gateway;
|
|
|
|
@override
|
|
void dispose() {
|
|
_prompt.dispose();
|
|
_workspace.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
bool get _locked => _intent != null || _sending;
|
|
|
|
void _cancel() {
|
|
if (!_sending) Navigator.of(context).pop();
|
|
}
|
|
|
|
Future<void> _confirm() async {
|
|
if (_sending) return;
|
|
final prompt = _prompt.text.trim();
|
|
if (prompt.isEmpty) {
|
|
setState(() => _error = runPipelinePromptRequired);
|
|
return;
|
|
}
|
|
// The browser checks the field as typed; only the sent value is trimmed.
|
|
final workspace = _workspace.text;
|
|
if (!workspace.startsWith('/')) {
|
|
setState(() => _error = runPipelineWorkspaceAbsolute);
|
|
return;
|
|
}
|
|
final intent =
|
|
_intent ??
|
|
RunPipelineIntent(
|
|
pipelineId: widget.detail.id,
|
|
version: widget.detail.resolvedVersion,
|
|
prompt: prompt,
|
|
workspace: workspace.trim(),
|
|
notifyMode: _notify,
|
|
outputFormat: _outputFormat,
|
|
requestKey: newRunPipelineRequestKey(
|
|
widget.detail.id,
|
|
widget.detail.resolvedVersion,
|
|
),
|
|
);
|
|
setState(() {
|
|
_sending = true;
|
|
_error = null;
|
|
});
|
|
final outcome = await widget.onSubmit(intent);
|
|
if (!mounted) return;
|
|
switch (outcome) {
|
|
case RunPipelineAccepted(:final run):
|
|
widget.onPendingChanged(null);
|
|
widget.onAccepted(run);
|
|
Navigator.of(context).pop();
|
|
case RunPipelineRefused(:final message, :final admissionUnknown):
|
|
final keep = admissionUnknown ? intent : null;
|
|
widget.onPendingChanged(keep);
|
|
setState(() {
|
|
_intent = keep;
|
|
_sending = false;
|
|
_error = message;
|
|
});
|
|
}
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final dialog = widget.cupertino ? _cupertino(context) : _material(context);
|
|
return PopScope(canPop: !_sending, child: dialog);
|
|
}
|
|
|
|
Widget _material(BuildContext context) {
|
|
return AlertDialog(
|
|
scrollable: true,
|
|
title: Text(runPipelineDialogTitle(widget.detail.name)),
|
|
content: _content(context),
|
|
actions: [
|
|
TextButton(
|
|
key: runPipelineCancelKey,
|
|
onPressed: _sending ? null : _cancel,
|
|
child: const Text(runPipelineCancelLabel),
|
|
),
|
|
TextButton(
|
|
key: const Key('run-pipeline-schedule'),
|
|
onPressed: _sending ? null : _schedule,
|
|
child: const Text('Schedule…'),
|
|
),
|
|
FilledButton(
|
|
key: runPipelineConfirmKey,
|
|
onPressed: _sending ? null : _confirm,
|
|
child: Text(_confirmLabel),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
Widget _cupertino(BuildContext context) {
|
|
return CupertinoAlertDialog(
|
|
title: Text(runPipelineDialogTitle(widget.detail.name)),
|
|
content: _content(context),
|
|
actions: [
|
|
CupertinoDialogAction(
|
|
key: runPipelineCancelKey,
|
|
onPressed: _sending ? null : _cancel,
|
|
child: const Text(runPipelineCancelLabel),
|
|
),
|
|
CupertinoDialogAction(
|
|
key: const Key('run-pipeline-schedule'),
|
|
onPressed: _sending ? null : _schedule,
|
|
child: const Text('Schedule…'),
|
|
),
|
|
CupertinoDialogAction(
|
|
key: runPipelineConfirmKey,
|
|
isDefaultAction: true,
|
|
onPressed: _sending ? null : _confirm,
|
|
child: Text(_confirmLabel),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// Leaves the dialog for the schedule editor with the typed prompt,
|
|
/// workspace and the pinned version.
|
|
void _schedule() {
|
|
final draft = ScheduleDraft.create(
|
|
pipelineId: widget.detail.id,
|
|
version: '${widget.detail.resolvedVersion}',
|
|
workspace: _workspace.text.trim(),
|
|
)..prompt = _prompt.text;
|
|
final schedule = widget.onSchedule;
|
|
Navigator.of(context).pop();
|
|
schedule(draft);
|
|
}
|
|
|
|
String get _confirmLabel {
|
|
if (_sending) return runPipelineSendingLabel;
|
|
return _intent == null
|
|
? runPipelineConfirmLabel
|
|
: runPipelineCheckRetryLabel;
|
|
}
|
|
|
|
Widget _content(BuildContext context) {
|
|
final errorStyle = widget.cupertino
|
|
? TextStyle(color: CupertinoColors.systemRed.resolveFrom(context))
|
|
: TextStyle(color: Theme.of(context).colorScheme.error);
|
|
return Column(
|
|
mainAxisSize: MainAxisSize.min,
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: [
|
|
RunPipelineFields(
|
|
prompt: _prompt,
|
|
workspace: _workspace,
|
|
directories: _directories,
|
|
cupertino: widget.cupertino,
|
|
onChooseDirectory: _chooseDirectory,
|
|
notifyMode: _notify,
|
|
outputFormat: _outputFormat,
|
|
locked: _locked,
|
|
onNotifyChanged: (mode) => setState(() => _notify = mode),
|
|
onOutputFormatChanged: (format) =>
|
|
setState(() => _outputFormat = format),
|
|
),
|
|
if (_directoriesLoading)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 4),
|
|
child: Text(runPipelineDirectoriesLoading),
|
|
),
|
|
if (_error case final error?)
|
|
Padding(
|
|
padding: const EdgeInsets.only(top: 12),
|
|
child: Semantics(
|
|
liveRegion: true,
|
|
child: Text(error, key: runPipelineErrorKey, style: errorStyle),
|
|
),
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
void _chooseDirectory(String directory) {
|
|
_workspace.text = directory;
|
|
}
|
|
}
|