feat(app): schedule a recorded run with its options
The run detail now offers the web's "Schedule this run" action when the
run names a positive pipeline and pinned version. It opens the existing
create editor seeded from the run and sends nothing until Save.
The seed preserves every field the browser's scheduleDraft({ run })
copies: title (following the title_source rule), all input keys with
prompt kept editable, pinned version, workspace, notify mode, write
paths, tool access, model, stage models and output format. build() emits
the same conditional payload as serializeSchedule.
One request key is minted per editor opening so a refused save retries
without duplicating the schedule. Advanced options are seed-only for
now: the editor has no controls for them yet.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import '../../../../core/api_gateway.dart';
|
||||
import '../saved_schedule.dart';
|
||||
import '../schedule_json.dart';
|
||||
import '../schedule_rule.dart';
|
||||
|
||||
/// Which half of the timer rule the editor is writing.
|
||||
@@ -43,6 +44,18 @@ class ScheduleDraft {
|
||||
String timezone = '';
|
||||
String notifyMode = 'auto';
|
||||
|
||||
/// Advanced run options the editor does not render yet. A create from a run
|
||||
/// seeds them from that run and an edit seeds them from the saved record, so
|
||||
/// [build] always saves the run's original configuration instead of silently
|
||||
/// dropping it.
|
||||
String model = '';
|
||||
JsonMap stageModels = <String, dynamic>{};
|
||||
List<String> writePaths = [];
|
||||
bool allowTools = false;
|
||||
JsonMap otherInput = <String, dynamic>{};
|
||||
String title = '';
|
||||
String outputFormat = '';
|
||||
|
||||
/// New schedule: the server's create sentinel is `id: 0` with no revision.
|
||||
factory ScheduleDraft.create({
|
||||
required int pipelineId,
|
||||
@@ -54,6 +67,31 @@ class ScheduleDraft {
|
||||
..workspace = workspace
|
||||
..timezone = timezone;
|
||||
|
||||
/// New schedule seeded from one recorded run, mirroring `scheduleDraft({
|
||||
/// run })` in `web/src/schedules.js`: the run's title, prompt, pinned version,
|
||||
/// absolute workspace, notification mode and advanced run options are copied.
|
||||
/// Every input key survives with `prompt` kept as the editable field; the
|
||||
/// schedule `title` follows the browser's `title_source` rule. Timer,
|
||||
/// weekday and timezone fields keep the create defaults (an empty timezone
|
||||
/// means the server's zone), and nothing is sent until the editor is saved.
|
||||
factory ScheduleDraft.fromRun(JsonMap run) {
|
||||
final input = run['input'];
|
||||
final notifyMode = run['notify_mode'];
|
||||
return ScheduleDraft._(null, jsonInteger(run['pipeline_id']))
|
||||
..name = jsonText(run['title'])
|
||||
..prompt = jsonText(input is Map ? input['prompt'] : null)
|
||||
..version = _runVersion(run['version'])
|
||||
..workspace = jsonText(run['workspace'])
|
||||
..notifyMode = notifyMode is String ? notifyMode : 'auto'
|
||||
..model = jsonText(run['model'])
|
||||
..stageModels = _jsonObject(run['stage_models'])
|
||||
..writePaths = _jsonTexts(run['write_paths'])
|
||||
..allowTools = run['allow_tools'] == true
|
||||
..otherInput = _inputExceptPrompt(input)
|
||||
..title = _runScheduleTitle(run)
|
||||
..outputFormat = jsonText(run['output_format']);
|
||||
}
|
||||
|
||||
/// Existing record: every field starts from the server value, never from a
|
||||
/// default that could overwrite it.
|
||||
factory ScheduleDraft.fromSchedule(SavedSchedule s, {int? pipelineId}) {
|
||||
@@ -71,7 +109,14 @@ class ScheduleDraft {
|
||||
..times = rule.times.join(', ')
|
||||
..days = [...rule.days]
|
||||
..timezone = rule.timezone
|
||||
..notifyMode = s.raw['notify_mode'] is String ? s.notifyMode : 'auto';
|
||||
..notifyMode = s.raw['notify_mode'] is String ? s.notifyMode : 'auto'
|
||||
..model = s.model
|
||||
..stageModels = _jsonObject(s.raw['stage_models'])
|
||||
..writePaths = s.writePaths
|
||||
..allowTools = s.allowTools
|
||||
..otherInput = _inputExceptPrompt(s.input)
|
||||
..title = s.title
|
||||
..outputFormat = s.outputFormat;
|
||||
}
|
||||
|
||||
/// Validates every field and assembles the exact `save_schedule` record.
|
||||
@@ -138,8 +183,11 @@ class ScheduleDraft {
|
||||
record['pipeline_id'] = selectedPipeline;
|
||||
record['version'] = pinned;
|
||||
record['enabled'] = enabled;
|
||||
record['input'] = (source?.input ?? <String, dynamic>{})
|
||||
..['prompt'] = prompt;
|
||||
// Every run/record input key survives; only the editable prompt is replaced.
|
||||
record['input'] = <String, dynamic>{
|
||||
...cloneJsonMap(otherInput),
|
||||
'prompt': prompt,
|
||||
};
|
||||
// Untrimmed like `workspace: draft.workspace`; validation also runs on the
|
||||
// untrimmed value, so a valid workspace is sent exactly as the operator
|
||||
// entered it.
|
||||
@@ -163,6 +211,38 @@ class ScheduleDraft {
|
||||
if (timezone.trim().isNotEmpty) rule['timezone'] = timezone.trim();
|
||||
record['rule'] = rule;
|
||||
record['notify_mode'] = notifyMode;
|
||||
// Advanced options follow `serializeSchedule`: an empty value removes the
|
||||
// key so a create stays minimal and an edit drops what the operator cleared.
|
||||
if (writePaths.isNotEmpty) {
|
||||
record['write_paths'] = [...writePaths];
|
||||
} else {
|
||||
record.remove('write_paths');
|
||||
}
|
||||
if (allowTools) {
|
||||
record['allow_tools'] = true;
|
||||
} else {
|
||||
record.remove('allow_tools');
|
||||
}
|
||||
if (model.isNotEmpty) {
|
||||
record['model'] = model;
|
||||
} else {
|
||||
record.remove('model');
|
||||
}
|
||||
if (stageModels.isNotEmpty) {
|
||||
record['stage_models'] = cloneJsonMap(stageModels);
|
||||
} else {
|
||||
record.remove('stage_models');
|
||||
}
|
||||
if (title.isNotEmpty) {
|
||||
record['title'] = title;
|
||||
} else {
|
||||
record.remove('title');
|
||||
}
|
||||
if (outputFormat.isNotEmpty) {
|
||||
record['output_format'] = outputFormat;
|
||||
} else {
|
||||
record.remove('output_format');
|
||||
}
|
||||
return ScheduleSaveResult(record, const {});
|
||||
}
|
||||
}
|
||||
@@ -170,6 +250,52 @@ class ScheduleDraft {
|
||||
const _maxSafeInteger = 9007199254740991;
|
||||
bool _validIdentity(int? value) =>
|
||||
value != null && value > 0 && value <= _maxSafeInteger;
|
||||
|
||||
/// A run input with `prompt` removed, deep-copied so the draft never aliases the
|
||||
/// reply map. Build re-adds the editable prompt.
|
||||
JsonMap _inputExceptPrompt(dynamic input) {
|
||||
if (input is! Map) return <String, dynamic>{};
|
||||
return <String, dynamic>{
|
||||
for (final entry in input.entries)
|
||||
if ('${entry.key}' != 'prompt')
|
||||
'${entry.key}': cloneJsonValue(entry.value),
|
||||
};
|
||||
}
|
||||
|
||||
/// Deep-copied JSON object for `stage_models`; a non-object field is empty.
|
||||
JsonMap _jsonObject(dynamic value) {
|
||||
if (value is! Map) return <String, dynamic>{};
|
||||
return <String, dynamic>{
|
||||
for (final entry in value.entries)
|
||||
'${entry.key}': cloneJsonValue(entry.value),
|
||||
};
|
||||
}
|
||||
|
||||
List<String> _jsonTexts(dynamic value) => value is List
|
||||
? [
|
||||
for (final item in value)
|
||||
if (item is String) item,
|
||||
]
|
||||
: const [];
|
||||
|
||||
/// Renders like the browser's `String(run.version)`: a number keeps its exact
|
||||
/// text (an integral one without a `.0`), while a malformed value stays visible
|
||||
/// so the editor's own check explains it.
|
||||
String _runVersion(dynamic value) => switch (value) {
|
||||
String() => value,
|
||||
num() when value.isFinite => (jsonInteger(value) ?? value).toString(),
|
||||
_ => '',
|
||||
};
|
||||
|
||||
/// The schedule `title` seeded from a run, mirroring `scheduleDraft({ run })`:
|
||||
/// a generated or fallback run title is not copied, while an explicit/operator
|
||||
/// title (or a run that does not report its source) is.
|
||||
String _runScheduleTitle(JsonMap run) {
|
||||
final source = jsonText(run['title_source']);
|
||||
if (source.isNotEmpty && source != 'explicit' && source != 'user') return '';
|
||||
return jsonText(run['title']);
|
||||
}
|
||||
|
||||
final _digits = RegExp(r'^\d+$');
|
||||
final _timeOfDay = RegExp(r'^([01]\d|2[0-3]):[0-5]\d$');
|
||||
final _workspaceBreaks = RegExp(r'[\x00\r\n]');
|
||||
|
||||
Reference in New Issue
Block a user