import '../../../core/api_gateway.dart'; import 'schedule_json.dart'; import 'schedule_rule.dart'; /// One saved run configuration from `list_schedules` or `save_schedule`. /// /// A saved schedule is a complete run request pinned to one pipeline version. /// It is deliberately not a pipeline definition: nothing here is published, /// validated as a graph or treated as the pipeline's current version. /// /// [raw] keeps the exact server record, including fields this client does not /// know yet. Typed getters are views; they never rewrite [raw]. class SavedSchedule { SavedSchedule(JsonMap raw) : raw = cloneJsonMap(raw); final JsonMap raw; /// Server identity; null when an older reply omits it. Never defaulted to 0, /// because 0 is the create sentinel rather than a saved identity. int? get id => _integer(raw['id']); /// Compare-and-swap token. Updates reuse it and deletes send it as /// `expected_revision`; this client never invents or increments it. int? get revision => _integer(raw['revision']); int? get pipelineId => _integer(raw['pipeline_id']); int? get version => _integer(raw['version']); String get name => _text(raw['name']); bool get enabled => raw['enabled'] == true; JsonMap get input => _map(raw['input']); String get prompt => _text(input['prompt']); String get workspace => _text(raw['workspace']); ScheduleRule get rule => ScheduleRule(_map(raw['rule'])); List get writePaths => _texts(raw['write_paths']); bool get allowTools => raw['allow_tools'] == true; String get model => _text(raw['model']); Map get stageModels => { for (final entry in _map(raw['stage_models']).entries) if (entry.value is String) entry.key: entry.value as String, }; String get notifyMode => _text(raw['notify_mode']); String get title => _text(raw['title']); String get outputFormat => _text(raw['output_format']); String get nextDue => _text(raw['next_due']); String get createdAt => _text(raw['created_at']); String get updatedAt => _text(raw['updated_at']); /// The record to pass to `save_schedule`, deep-copied so callers cannot /// mutate this snapshot by aliasing. JsonMap toJson() => cloneJsonMap(raw); /// Pause/enable copy. Identity, revision and every unknown field are kept so /// the update still targets the revision this record was read at. SavedSchedule withEnabled(bool value) { final copy = toJson(); copy['enabled'] = value; return SavedSchedule(copy); } } int? _integer(dynamic value) { if (value is int) return value; if (value is num && value.isFinite && value == value.truncateToDouble()) { return value.toInt(); } return null; } String _text(dynamic value) => value is String ? value : ''; JsonMap _map(dynamic value) { if (value is! Map) return {}; return { for (final entry in value.entries) '${entry.key}': cloneJsonValue(entry.value), }; } List _texts(dynamic value) => value is List ? [ for (final item in value) if (item is String) item, ] : const [];